2 #include <Decompress.h>
5 UefiDecompress(data_buffer, size, original_size)
24 Status
= PyArg_ParseTuple(
34 if (SrcData
->ob_type
->tp_as_buffer
== NULL
35 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
36 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
37 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
41 // Because some Python objects which support "buffer" protocol have more than one
42 // memory segment, we have to copy them into a contiguous memory.
43 SrcBuf
= PyMem_Malloc(SrcDataSize
);
45 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
49 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
51 for (Index
= 0; Index
< SegNum
; ++Index
) {
55 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
57 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
60 memcpy(TmpBuf
, BufSeg
, Len
);
64 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 1);
65 if (Status
!= EFI_SUCCESS
) {
66 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
70 return PyBuffer_FromMemory(DstBuf
, (Py_ssize_t
)DstDataSize
);
101 Status
= PyArg_ParseTuple(
111 if (SrcData
->ob_type
->tp_as_buffer
== NULL
112 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
113 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
114 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
118 // Because some Python objects which support "buffer" protocol have more than one
119 // memory segment, we have to copy them into a contiguous memory.
120 SrcBuf
= PyMem_Malloc(SrcDataSize
);
121 if (SrcBuf
== NULL
) {
122 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
126 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
128 for (Index
= 0; Index
< SegNum
; ++Index
) {
132 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
134 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
137 memcpy(TmpBuf
, BufSeg
, Len
);
141 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 2);
142 if (Status
!= EFI_SUCCESS
) {
143 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
147 return PyString_FromStringAndSize((CONST INT8
*)DstBuf
, (Py_ssize_t
)DstDataSize
);
150 if (SrcBuf
!= NULL
) {
154 if (DstBuf
!= NULL
) {
182 STATIC INT8 DecompressDocs
[] = "Decompress(): Decompress data using UEFI standard algorithm\n";
183 STATIC INT8 CompressDocs
[] = "Compress(): Compress data using UEFI standard algorithm\n";
185 STATIC PyMethodDef EfiCompressor_Funcs
[] = {
186 {"UefiDecompress", (PyCFunction
)UefiDecompress
, METH_VARARGS
, DecompressDocs
},
187 {"UefiCompress", (PyCFunction
)UefiCompress
, METH_VARARGS
, DecompressDocs
},
188 {"FrameworkDecompress", (PyCFunction
)FrameworkDecompress
, METH_VARARGS
, DecompressDocs
},
189 {"FrameworkCompress", (PyCFunction
)FrameworkCompress
, METH_VARARGS
, DecompressDocs
},
190 {NULL
, NULL
, 0, NULL
}
194 initEfiCompressor(VOID
) {
195 Py_InitModule3("EfiCompressor", EfiCompressor_Funcs
, "EFI Compression Algorithm Extension Module");