summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
blob: 53e2255f5ffda7ecdb491d1a0edc5e837de6eca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** @file
  Brotli Decompress interfaces

  Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <BrotliDecompressLibInternal.h>

/**
  Allocation routine used by BROTLI decompression.

  @param Ptr              Pointer to the BROTLI_BUFF instance.
  @param Size             The size in bytes to be allocated.

  @return The allocated pointer address, or NULL on failure
**/
VOID *
BrAlloc (
  IN VOID *    Ptr,
  IN size_t    Size
  )
{
  VOID          *Addr;
  BROTLI_BUFF   *Private;

  Private = (BROTLI_BUFF *)Ptr;

  if (Private->BuffSize >= Size) {
    Addr = Private->Buff;
    Private->Buff = (VOID *) ((UINT8 *)Addr + Size);
    Private->BuffSize -= Size;
    return Addr;
  } else {
    ASSERT (FALSE);
    return NULL;
  }
}

/**
  Free routine used by BROTLI decompression.

  @param Ptr              Pointer to the BROTLI_BUFF instance
  @param Address          The address to be freed
**/
VOID
BrFree (
  IN VOID *    Ptr,
  IN VOID *    Address
  )
{
  //
  // We use the 'scratch buffer' for allocations, so there is no free
  // operation required.  The scratch buffer will be freed by the caller
  // of the decompression code.
  //
}

/**
  Decompresses a Brotli compressed source buffer.

  Extracts decompressed data to its original form.
  If the compressed source data specified by Source is successfully decompressed
  into Destination, then EFI_SUCCESS is returned. If the compressed source data
  specified by Source is not in a valid compressed data format,
  then EFI_INVALID_PARAMETER is returned.

  @param  Source      The source buffer containing the compressed data.
  @param  SourceSize  The size of source buffer.
  @param  Destination The destination buffer to store the decompressed data.
  @param  DestSize    The destination buffer size.
  @param  BuffInfo    The pointer to the BROTLI_BUFF instance.

  @retval EFI_SUCCESS Decompression completed successfully, and
                      the uncompressed buffer is returned in Destination.
  @retval EFI_INVALID_PARAMETER
                      The source buffer specified by Source is corrupted
                      (not in a valid compressed format).
**/
EFI_STATUS
BrotliDecompress (
  IN CONST VOID*  Source,
  IN UINTN        SourceSize,
  IN OUT VOID*    Destination,
  IN OUT UINTN    DestSize,
  IN VOID *       BuffInfo
  )
{
  UINT8 *        Input;
  UINT8 *        Output;
  const UINT8 *  NextIn;
  UINT8 *        NextOut;
  size_t         TotalOut;
  size_t         AvailableIn;
  size_t         AvailableOut;
  VOID *         Temp;
  BrotliDecoderResult   Result;
  BrotliDecoderState *  BroState;

  TotalOut = 0;
  AvailableOut = FILE_BUFFER_SIZE;
  Result = BROTLI_DECODER_RESULT_ERROR;
  BroState = BrotliDecoderCreateInstance(BrAlloc, BrFree, BuffInfo);
  Temp = Destination;

  if (BroState == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
  Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
  if ((Input==NULL) || (Output==NULL)) {
    BrFree(BuffInfo, Input);
    BrFree(BuffInfo, Output);
    BrotliDecoderDestroyInstance(BroState);
    return EFI_INVALID_PARAMETER;
  }
  NextOut = Output;
  Result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
  while (1) {
    if (Result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
      if (SourceSize == 0) {
        break;
      }
      if (SourceSize >= FILE_BUFFER_SIZE) {
        AvailableIn = FILE_BUFFER_SIZE;
      }else{
        AvailableIn = SourceSize;
      }
      CopyMem(Input, Source, AvailableIn);
      Source = (VOID *)((UINT8 *)Source + AvailableIn);
      SourceSize -= AvailableIn;
      NextIn = Input;
    } else if (Result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
      CopyMem(Temp, Output, FILE_BUFFER_SIZE);
      AvailableOut = FILE_BUFFER_SIZE;
      Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE);
      NextOut = Output;
    } else {
      break; /* Error or success. */
    }
    Result = BrotliDecoderDecompressStream(
                          BroState,
                          &AvailableIn,
                          &NextIn,
                          &AvailableOut,
                          &NextOut,
                          &TotalOut
                          );
  }
  if (NextOut != Output) {
    CopyMem(Temp, Output, (size_t)(NextOut - Output));
  }

  DestSize = TotalOut;

  BrFree(BuffInfo, Input);
  BrFree(BuffInfo, Output);
  BrotliDecoderDestroyInstance(BroState);
  return (Result == BROTLI_DECODER_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;
}

/**
  Get the size of the uncompressed buffer by parsing EncodeData header.

  @param EncodedData  Pointer to the compressed data.
  @param StartOffset  Start offset of the compressed data.
  @param EndOffset    End offset of the compressed data.

  @return The size of the uncompressed buffer.
**/
UINT64
BrGetDecodedSizeOfBuf(
  IN UINT8 *  EncodedData,
  IN UINT8    StartOffset,
  IN UINT8    EndOffset
  )
{
  UINT64 DecodedSize;
  INTN   Index;

  /* Parse header */
  DecodedSize = 0;
  for (Index = EndOffset - 1; Index >= StartOffset; Index--)
    DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];

  return DecodedSize;
}

/**
  Given a Brotli compressed source buffer, this function retrieves the size of
  the uncompressed buffer and the size of the scratch buffer required
  to decompress the compressed source buffer.

  Retrieves the size of the uncompressed buffer and the temporary scratch buffer
  required to decompress the buffer specified by Source and SourceSize.
  The size of the uncompressed buffer is returned in DestinationSize,
  the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
  This function does not have scratch buffer available to perform a thorough
  checking of the validity of the source data. It just retrieves the "Original Size"
  field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.
  And ScratchSize is specific to the decompression implementation.

  If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().

  @param  Source          The source buffer containing the compressed data.
  @param  SourceSize      The size, in bytes, of the source buffer.
  @param  DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
                          that will be generated when the compressed buffer specified
                          by Source and SourceSize is decompressed.
  @param  ScratchSize     A pointer to the size, in bytes, of the scratch buffer that
                          is required to decompress the compressed buffer specified
                          by Source and SourceSize.

  @retval EFI_SUCCESS     The size of the uncompressed data was returned
                          in DestinationSize and the size of the scratch
                          buffer was returned in ScratchSize.
**/
EFI_STATUS
EFIAPI
BrotliUefiDecompressGetInfo (
  IN  CONST VOID *  Source,
  IN  UINT32        SourceSize,
  OUT UINT32 *      DestinationSize,
  OUT UINT32 *      ScratchSize
  )
{
  UINT64  GetSize;
  UINT8   MaxOffset;

  ASSERT(SourceSize >= BROTLI_SCRATCH_MAX);

  MaxOffset = BROTLI_DECODE_MAX;
  GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
  *DestinationSize = (UINT32)GetSize;
  MaxOffset = BROTLI_SCRATCH_MAX;
  GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
  *ScratchSize = (UINT32)GetSize;
  return EFI_SUCCESS;
}

/**
  Decompresses a Brotli compressed source buffer.

  Extracts decompressed data to its original form.
  If the compressed source data specified by Source is successfully decompressed
  into Destination, then RETURN_SUCCESS is returned.  If the compressed source data
  specified by Source is not in a valid compressed data format,
  then RETURN_INVALID_PARAMETER is returned.

  @param  Source      The source buffer containing the compressed data.
  @param  SourceSize  The size of source buffer.
  @param  Destination The destination buffer to store the decompressed data
  @param  Scratch     A temporary scratch buffer that is used to perform the decompression.
                      This is an optional parameter that may be NULL if the
                      required scratch buffer size is 0.

  @retval EFI_SUCCESS Decompression completed successfully, and
                      the uncompressed buffer is returned in Destination.
  @retval EFI_INVALID_PARAMETER
                      The source buffer specified by Source is corrupted
                      (not in a valid compressed format).
**/
EFI_STATUS
EFIAPI
BrotliUefiDecompress (
  IN CONST VOID *   Source,
  IN UINTN          SourceSize,
  IN OUT VOID *     Destination,
  IN OUT VOID *     Scratch
  )
{
  UINTN          DestSize = 0;
  EFI_STATUS     Status;
  BROTLI_BUFF    BroBuff;
  UINT64         GetSize;
  UINT8          MaxOffset;

  MaxOffset = BROTLI_SCRATCH_MAX;
  GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);

  BroBuff.Buff     = Scratch;
  BroBuff.BuffSize = (UINTN)GetSize;

  Status = BrotliDecompress(
            (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),
            SourceSize - BROTLI_SCRATCH_MAX,
            Destination,
            DestSize,
            (VOID *)(&BroBuff)
            );

  return Status;
}