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