]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
MdeModulePkg BrotliLib: Fix the regression logic issue in loop
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / GuidedSectionExtraction.c
1 /** @file
2 BROTLI Decompress GUIDed Section Extraction Library.
3 It wraps Brotli decompress interfaces to GUIDed Section Extraction interfaces
4 and registers them into GUIDed handler table.
5
6 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <BrotliDecompressLibInternal.h>
18
19 /**
20 Examines a GUIDed section and returns the size of the decoded buffer and the
21 size of an scratch buffer required to actually decode the data in a GUIDed section.
22
23 Examines a GUIDed section specified by InputSection.
24 If GUID for InputSection does not match the GUID that this handler supports,
25 then RETURN_UNSUPPORTED is returned.
26 If the required information can not be retrieved from InputSection,
27 then RETURN_INVALID_PARAMETER is returned.
28 If the GUID of InputSection does match the GUID that this handler supports,
29 then the size required to hold the decoded buffer is returned in OututBufferSize,
30 the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
31 from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
32
33 If InputSection is NULL, then ASSERT().
34 If OutputBufferSize is NULL, then ASSERT().
35 If ScratchBufferSize is NULL, then ASSERT().
36 If SectionAttribute is NULL, then ASSERT().
37
38
39 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
40 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required
41 if the buffer specified by InputSection were decoded.
42 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space
43 if the buffer specified by InputSection were decoded.
44 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes
45 field of EFI_GUID_DEFINED_SECTION in the PI Specification.
46
47 @retval RETURN_SUCCESS The information about InputSection was returned.
48 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
49 @retval RETURN_INVALID_PARAMETER The information can not be retrieved from the section specified by InputSection.
50
51 **/
52 RETURN_STATUS
53 EFIAPI
54 BrotliGuidedSectionGetInfo (
55 IN CONST VOID *InputSection,
56 OUT UINT32 *OutputBufferSize,
57 OUT UINT32 *ScratchBufferSize,
58 OUT UINT16 *SectionAttribute
59 )
60 {
61 ASSERT (InputSection != NULL);
62 ASSERT (OutputBufferSize != NULL);
63 ASSERT (ScratchBufferSize != NULL);
64 ASSERT (SectionAttribute != NULL);
65
66 if (IS_SECTION2 (InputSection)) {
67 if (!CompareGuid (
68 &gBrotliCustomDecompressGuid,
69 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
70 return RETURN_INVALID_PARAMETER;
71 }
72
73 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
74
75 return BrotliUefiDecompressGetInfo (
76 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
77 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
78 OutputBufferSize,
79 ScratchBufferSize
80 );
81 } else {
82 if (!CompareGuid (
83 &gBrotliCustomDecompressGuid,
84 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
85 return RETURN_INVALID_PARAMETER;
86 }
87
88 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
89
90 return BrotliUefiDecompressGetInfo (
91 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
92 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
93 OutputBufferSize,
94 ScratchBufferSize
95 );
96 }
97 }
98
99 /**
100 Decompress a BROTLI compressed GUIDed section into a caller allocated output buffer.
101
102 Decodes the GUIDed section specified by InputSection.
103 If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
104 If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
105 If the GUID of InputSection does match the GUID that this handler supports, then InputSection
106 is decoded into the buffer specified by OutputBuffer and the authentication status of this
107 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the
108 data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise,
109 the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
110
111 If InputSection is NULL, then ASSERT().
112 If OutputBuffer is NULL, then ASSERT().
113 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
114 If AuthenticationStatus is NULL, then ASSERT().
115
116 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
117 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
118 @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
119 as a scratch buffer to perform the decode operation.
120 @param[out] AuthenticationStatus
121 A pointer to the authentication status of the decoded output buffer.
122 See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
123 section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
124 never be set by this handler.
125
126 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
127 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
128 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
129
130 **/
131 RETURN_STATUS
132 EFIAPI
133 BrotliGuidedSectionExtraction (
134 IN CONST VOID *InputSection,
135 OUT VOID **OutputBuffer,
136 OUT VOID *ScratchBuffer, OPTIONAL
137 OUT UINT32 *AuthenticationStatus
138 )
139 {
140 ASSERT (OutputBuffer != NULL);
141 ASSERT (InputSection != NULL);
142
143 if (IS_SECTION2 (InputSection)) {
144 if (!CompareGuid (
145 &gBrotliCustomDecompressGuid,
146 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
147 return RETURN_INVALID_PARAMETER;
148 }
149 //
150 // Authentication is set to Zero, which may be ignored.
151 //
152 *AuthenticationStatus = 0;
153
154 return BrotliUefiDecompress (
155 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
156 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
157 *OutputBuffer,
158 ScratchBuffer
159 );
160 } else {
161 if (!CompareGuid (
162 &gBrotliCustomDecompressGuid,
163 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
164 return RETURN_INVALID_PARAMETER;
165 }
166 //
167 // Authentication is set to Zero, which may be ignored.
168 //
169 *AuthenticationStatus = 0;
170
171 return BrotliUefiDecompress (
172 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
173 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
174 *OutputBuffer,
175 ScratchBuffer
176 );
177 }
178 }
179
180 /**
181 Register BrotliDecompress and BrotliDecompressGetInfo handlers with BrotliCustomerDecompressGuid.
182
183 @retval EFI_SUCCESS Register successfully.
184 @retval EFI_OUT_OF_RESOURCES No enough memory to store this handler.
185 **/
186 EFI_STATUS
187 EFIAPI
188 BrotliDecompressLibConstructor (
189 )
190 {
191 return ExtractGuidedSectionRegisterHandlers (
192 &gBrotliCustomDecompressGuid,
193 BrotliGuidedSectionGetInfo,
194 BrotliGuidedSectionExtraction
195 );
196 }