4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Decompress.h>
13 UefiDecompress(data_buffer, size, original_size)
32 Status
= PyArg_ParseTuple(
42 if (SrcData
->ob_type
->tp_as_buffer
== NULL
43 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
44 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
45 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
49 // Because some Python objects which support "buffer" protocol have more than one
50 // memory segment, we have to copy them into a contiguous memory.
51 SrcBuf
= PyMem_Malloc(SrcDataSize
);
53 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
57 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
59 for (Index
= 0; Index
< SegNum
; ++Index
) {
63 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
65 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
68 memcpy(TmpBuf
, BufSeg
, Len
);
72 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 1);
73 if (Status
!= EFI_SUCCESS
) {
74 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
78 return PyBuffer_FromMemory(DstBuf
, (Py_ssize_t
)DstDataSize
);
109 Status
= PyArg_ParseTuple(
119 if (SrcData
->ob_type
->tp_as_buffer
== NULL
120 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
121 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
122 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
126 // Because some Python objects which support "buffer" protocol have more than one
127 // memory segment, we have to copy them into a contiguous memory.
128 SrcBuf
= PyMem_Malloc(SrcDataSize
);
129 if (SrcBuf
== NULL
) {
130 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
134 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
136 for (Index
= 0; Index
< SegNum
; ++Index
) {
140 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
142 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
145 memcpy(TmpBuf
, BufSeg
, Len
);
149 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 2);
150 if (Status
!= EFI_SUCCESS
) {
151 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
155 return PyString_FromStringAndSize((CONST INT8
*)DstBuf
, (Py_ssize_t
)DstDataSize
);
158 if (SrcBuf
!= NULL
) {
162 if (DstBuf
!= NULL
) {
190 STATIC INT8 DecompressDocs
[] = "Decompress(): Decompress data using UEFI standard algorithm\n";
191 STATIC INT8 CompressDocs
[] = "Compress(): Compress data using UEFI standard algorithm\n";
193 STATIC PyMethodDef EfiCompressor_Funcs
[] = {
194 {"UefiDecompress", (PyCFunction
)UefiDecompress
, METH_VARARGS
, DecompressDocs
},
195 {"UefiCompress", (PyCFunction
)UefiCompress
, METH_VARARGS
, DecompressDocs
},
196 {"FrameworkDecompress", (PyCFunction
)FrameworkDecompress
, METH_VARARGS
, DecompressDocs
},
197 {"FrameworkCompress", (PyCFunction
)FrameworkCompress
, METH_VARARGS
, DecompressDocs
},
198 {NULL
, NULL
, 0, NULL
}
202 initEfiCompressor(VOID
) {
203 Py_InitModule3("EfiCompressor", EfiCompressor_Funcs
, "EFI Compression Algorithm Extension Module");