4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Decompress.h>
19 UefiDecompress(data_buffer, size, original_size)
38 Status
= PyArg_ParseTuple(
48 if (SrcData
->ob_type
->tp_as_buffer
== NULL
49 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
50 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
51 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
55 // Because some Python objects which support "buffer" protocol have more than one
56 // memory segment, we have to copy them into a contiguous memory.
57 SrcBuf
= PyMem_Malloc(SrcDataSize
);
59 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
63 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
65 for (Index
= 0; Index
< SegNum
; ++Index
) {
69 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
71 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
74 memcpy(TmpBuf
, BufSeg
, Len
);
78 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 1);
79 if (Status
!= EFI_SUCCESS
) {
80 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
84 return PyBuffer_FromMemory(DstBuf
, (Py_ssize_t
)DstDataSize
);
115 Status
= PyArg_ParseTuple(
125 if (SrcData
->ob_type
->tp_as_buffer
== NULL
126 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
127 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
128 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
132 // Because some Python objects which support "buffer" protocol have more than one
133 // memory segment, we have to copy them into a contiguous memory.
134 SrcBuf
= PyMem_Malloc(SrcDataSize
);
135 if (SrcBuf
== NULL
) {
136 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
140 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
142 for (Index
= 0; Index
< SegNum
; ++Index
) {
146 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
148 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
151 memcpy(TmpBuf
, BufSeg
, Len
);
155 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 2);
156 if (Status
!= EFI_SUCCESS
) {
157 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
161 return PyString_FromStringAndSize((CONST INT8
*)DstBuf
, (Py_ssize_t
)DstDataSize
);
164 if (SrcBuf
!= NULL
) {
168 if (DstBuf
!= NULL
) {
196 STATIC INT8 DecompressDocs
[] = "Decompress(): Decompress data using UEFI standard algorithm\n";
197 STATIC INT8 CompressDocs
[] = "Compress(): Compress data using UEFI standard algorithm\n";
199 STATIC PyMethodDef EfiCompressor_Funcs
[] = {
200 {"UefiDecompress", (PyCFunction
)UefiDecompress
, METH_VARARGS
, DecompressDocs
},
201 {"UefiCompress", (PyCFunction
)UefiCompress
, METH_VARARGS
, DecompressDocs
},
202 {"FrameworkDecompress", (PyCFunction
)FrameworkDecompress
, METH_VARARGS
, DecompressDocs
},
203 {"FrameworkCompress", (PyCFunction
)FrameworkCompress
, METH_VARARGS
, DecompressDocs
},
204 {NULL
, NULL
, 0, NULL
}
208 initEfiCompressor(VOID
) {
209 Py_InitModule3("EfiCompressor", EfiCompressor_Funcs
, "EFI Compression Algorithm Extension Module");