]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/PyEfiCompressor/EfiCompressor.c
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / C / PyEfiCompressor / EfiCompressor.c
CommitLineData
52302d4d 1/** @file\r
97fa0ee9 2Efi Compressor\r
52302d4d 3\r
f7496d71
LG
4Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials are licensed and made available\r
6under the terms and conditions of the BSD License which accompanies this\r
52302d4d
LG
7distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
30fdf114
LG
15#include <Python.h>\r
16#include <Decompress.h>\r
17\r
18/*\r
19 UefiDecompress(data_buffer, size, original_size)\r
20*/\r
21STATIC\r
22PyObject*\r
23UefiDecompress(\r
24 PyObject *Self,\r
25 PyObject *Args\r
26 )\r
27{\r
28 PyObject *SrcData;\r
29 UINT32 SrcDataSize;\r
30 UINT32 DstDataSize;\r
31 UINTN Status;\r
32 UINT8 *SrcBuf;\r
33 UINT8 *DstBuf;\r
34 UINT8 *TmpBuf;\r
35 Py_ssize_t SegNum;\r
36 Py_ssize_t Index;\r
37\r
38 Status = PyArg_ParseTuple(\r
39 Args,\r
40 "Oi",\r
41 &SrcData,\r
42 &SrcDataSize\r
43 );\r
44 if (Status == 0) {\r
45 return NULL;\r
46 }\r
47\r
48 if (SrcData->ob_type->tp_as_buffer == NULL\r
49 || SrcData->ob_type->tp_as_buffer->bf_getreadbuffer == NULL\r
50 || SrcData->ob_type->tp_as_buffer->bf_getsegcount == NULL) {\r
51 PyErr_SetString(PyExc_Exception, "First argument is not a buffer\n");\r
52 return NULL;\r
53 }\r
54\r
55 // Because some Python objects which support "buffer" protocol have more than one\r
56 // memory segment, we have to copy them into a contiguous memory.\r
57 SrcBuf = PyMem_Malloc(SrcDataSize);\r
58 if (SrcBuf == NULL) {\r
59 PyErr_SetString(PyExc_Exception, "Not enough memory\n");\r
60 goto ERROR;\r
61 }\r
62\r
63 SegNum = SrcData->ob_type->tp_as_buffer->bf_getsegcount((PyObject *)SrcData, NULL);\r
64 TmpBuf = SrcBuf;\r
65 for (Index = 0; Index < SegNum; ++Index) {\r
66 VOID *BufSeg;\r
67 Py_ssize_t Len;\r
68\r
69 Len = SrcData->ob_type->tp_as_buffer->bf_getreadbuffer((PyObject *)SrcData, Index, &BufSeg);\r
70 if (Len < 0) {\r
71 PyErr_SetString(PyExc_Exception, "Buffer segment is not available\n");\r
72 goto ERROR;\r
73 }\r
74 memcpy(TmpBuf, BufSeg, Len);\r
75 TmpBuf += Len;\r
76 }\r
77\r
78 Status = Extract((VOID *)SrcBuf, SrcDataSize, (VOID **)&DstBuf, &DstDataSize, 1);\r
79 if (Status != EFI_SUCCESS) {\r
80 PyErr_SetString(PyExc_Exception, "Failed to decompress\n");\r
81 goto ERROR;\r
82 }\r
83\r
84 return PyBuffer_FromMemory(DstBuf, (Py_ssize_t)DstDataSize);\r
85\r
86ERROR:\r
87 if (SrcBuf != NULL) {\r
88 free(SrcBuf);\r
89 }\r
90\r
91 if (DstBuf != NULL) {\r
92 free(DstBuf);\r
93 }\r
94 return NULL;\r
95}\r
96\r
97\r
98STATIC\r
99PyObject*\r
100FrameworkDecompress(\r
101 PyObject *Self,\r
102 PyObject *Args\r
103 )\r
104{\r
105 PyObject *SrcData;\r
106 UINT32 SrcDataSize;\r
107 UINT32 DstDataSize;\r
108 UINTN Status;\r
109 UINT8 *SrcBuf;\r
110 UINT8 *DstBuf;\r
111 UINT8 *TmpBuf;\r
112 Py_ssize_t SegNum;\r
113 Py_ssize_t Index;\r
114\r
115 Status = PyArg_ParseTuple(\r
116 Args,\r
117 "Oi",\r
118 &SrcData,\r
119 &SrcDataSize\r
120 );\r
121 if (Status == 0) {\r
122 return NULL;\r
123 }\r
124\r
125 if (SrcData->ob_type->tp_as_buffer == NULL\r
126 || SrcData->ob_type->tp_as_buffer->bf_getreadbuffer == NULL\r
127 || SrcData->ob_type->tp_as_buffer->bf_getsegcount == NULL) {\r
128 PyErr_SetString(PyExc_Exception, "First argument is not a buffer\n");\r
129 return NULL;\r
130 }\r
131\r
132 // Because some Python objects which support "buffer" protocol have more than one\r
133 // memory segment, we have to copy them into a contiguous memory.\r
134 SrcBuf = PyMem_Malloc(SrcDataSize);\r
135 if (SrcBuf == NULL) {\r
136 PyErr_SetString(PyExc_Exception, "Not enough memory\n");\r
137 goto ERROR;\r
138 }\r
139\r
140 SegNum = SrcData->ob_type->tp_as_buffer->bf_getsegcount((PyObject *)SrcData, NULL);\r
141 TmpBuf = SrcBuf;\r
142 for (Index = 0; Index < SegNum; ++Index) {\r
143 VOID *BufSeg;\r
144 Py_ssize_t Len;\r
145\r
146 Len = SrcData->ob_type->tp_as_buffer->bf_getreadbuffer((PyObject *)SrcData, Index, &BufSeg);\r
147 if (Len < 0) {\r
148 PyErr_SetString(PyExc_Exception, "Buffer segment is not available\n");\r
149 goto ERROR;\r
150 }\r
151 memcpy(TmpBuf, BufSeg, Len);\r
152 TmpBuf += Len;\r
153 }\r
154\r
155 Status = Extract((VOID *)SrcBuf, SrcDataSize, (VOID **)&DstBuf, &DstDataSize, 2);\r
156 if (Status != EFI_SUCCESS) {\r
157 PyErr_SetString(PyExc_Exception, "Failed to decompress\n");\r
158 goto ERROR;\r
159 }\r
160\r
161 return PyString_FromStringAndSize((CONST INT8*)DstBuf, (Py_ssize_t)DstDataSize);\r
162\r
163ERROR:\r
164 if (SrcBuf != NULL) {\r
165 free(SrcBuf);\r
166 }\r
167\r
168 if (DstBuf != NULL) {\r
169 free(DstBuf);\r
170 }\r
171 return NULL;\r
172}\r
173\r
174\r
175STATIC\r
176PyObject*\r
177UefiCompress(\r
178 PyObject *Self,\r
179 PyObject *Args\r
180 )\r
181{\r
182 return NULL;\r
183}\r
184\r
185\r
186STATIC\r
187PyObject*\r
188FrameworkCompress(\r
189 PyObject *Self,\r
190 PyObject *Args\r
191 )\r
192{\r
193 return NULL;\r
194}\r
195\r
196STATIC INT8 DecompressDocs[] = "Decompress(): Decompress data using UEFI standard algorithm\n";\r
197STATIC INT8 CompressDocs[] = "Compress(): Compress data using UEFI standard algorithm\n";\r
198\r
199STATIC PyMethodDef EfiCompressor_Funcs[] = {\r
200 {"UefiDecompress", (PyCFunction)UefiDecompress, METH_VARARGS, DecompressDocs},\r
201 {"UefiCompress", (PyCFunction)UefiCompress, METH_VARARGS, DecompressDocs},\r
202 {"FrameworkDecompress", (PyCFunction)FrameworkDecompress, METH_VARARGS, DecompressDocs},\r
203 {"FrameworkCompress", (PyCFunction)FrameworkCompress, METH_VARARGS, DecompressDocs},\r
204 {NULL, NULL, 0, NULL}\r
205};\r
206\r
207PyMODINIT_FUNC\r
208initEfiCompressor(VOID) {\r
209 Py_InitModule3("EfiCompressor", EfiCompressor_Funcs, "EFI Compression Algorithm Extension Module");\r
210}\r
211\r
212\r