]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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 return RETURN_INVALID_PARAMETER;
65 }
66
67 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
68
69 return BrotliUefiDecompressGetInfo (
70 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
71 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
72 OutputBufferSize,
73 ScratchBufferSize
74 );
75 } else {
76 if (!CompareGuid (
77 &gBrotliCustomDecompressGuid,
78 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
79 return RETURN_INVALID_PARAMETER;
80 }
81
82 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
83
84 return BrotliUefiDecompressGetInfo (
85 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
86 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
87 OutputBufferSize,
88 ScratchBufferSize
89 );
90 }
91 }
92
93 /**
94 Decompress a BROTLI compressed GUIDed section into a caller allocated output buffer.
95
96 Decodes the GUIDed section specified by InputSection.
97 If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
98 If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
99 If the GUID of InputSection does match the GUID that this handler supports, then InputSection
100 is decoded into the buffer specified by OutputBuffer and the authentication status of this
101 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the
102 data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise,
103 the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
104
105 If InputSection is NULL, then ASSERT().
106 If OutputBuffer is NULL, then ASSERT().
107 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
108 If AuthenticationStatus is NULL, then ASSERT().
109
110 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
111 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
112 @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
113 as a scratch buffer to perform the decode operation.
114 @param[out] AuthenticationStatus
115 A pointer to the authentication status of the decoded output buffer.
116 See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
117 section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
118 never be set by this handler.
119
120 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
121 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
122 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
123
124 **/
125 RETURN_STATUS
126 EFIAPI
127 BrotliGuidedSectionExtraction (
128 IN CONST VOID *InputSection,
129 OUT VOID **OutputBuffer,
130 OUT VOID *ScratchBuffer, OPTIONAL
131 OUT UINT32 *AuthenticationStatus
132 )
133 {
134 ASSERT (OutputBuffer != NULL);
135 ASSERT (InputSection != NULL);
136
137 if (IS_SECTION2 (InputSection)) {
138 if (!CompareGuid (
139 &gBrotliCustomDecompressGuid,
140 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
141 return RETURN_INVALID_PARAMETER;
142 }
143 //
144 // Authentication is set to Zero, which may be ignored.
145 //
146 *AuthenticationStatus = 0;
147
148 return BrotliUefiDecompress (
149 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
150 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
151 *OutputBuffer,
152 ScratchBuffer
153 );
154 } else {
155 if (!CompareGuid (
156 &gBrotliCustomDecompressGuid,
157 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
158 return RETURN_INVALID_PARAMETER;
159 }
160 //
161 // Authentication is set to Zero, which may be ignored.
162 //
163 *AuthenticationStatus = 0;
164
165 return BrotliUefiDecompress (
166 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
167 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
168 *OutputBuffer,
169 ScratchBuffer
170 );
171 }
172 }
173
174 /**
175 Register BrotliDecompress and BrotliDecompressGetInfo handlers with BrotliCustomerDecompressGuid.
176
177 @retval EFI_SUCCESS Register successfully.
178 @retval EFI_OUT_OF_RESOURCES No enough memory to store this handler.
179 **/
180 EFI_STATUS
181 EFIAPI
182 BrotliDecompressLibConstructor (
183 VOID
184 )
185 {
186 return ExtractGuidedSectionRegisterHandlers (
187 &gBrotliCustomDecompressGuid,
188 BrotliGuidedSectionGetInfo,
189 BrotliGuidedSectionExtraction
190 );
191 }