3 Copyright (c) 2009 - 2010 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Decompress.h>
18 UefiDecompress(data_buffer, size, original_size)
37 Status
= PyArg_ParseTuple(
47 if (SrcData
->ob_type
->tp_as_buffer
== NULL
48 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
49 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
50 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
54 // Because some Python objects which support "buffer" protocol have more than one
55 // memory segment, we have to copy them into a contiguous memory.
56 SrcBuf
= PyMem_Malloc(SrcDataSize
);
58 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
62 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
64 for (Index
= 0; Index
< SegNum
; ++Index
) {
68 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
70 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
73 memcpy(TmpBuf
, BufSeg
, Len
);
77 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 1);
78 if (Status
!= EFI_SUCCESS
) {
79 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
83 return PyBuffer_FromMemory(DstBuf
, (Py_ssize_t
)DstDataSize
);
114 Status
= PyArg_ParseTuple(
124 if (SrcData
->ob_type
->tp_as_buffer
== NULL
125 || SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer
== NULL
126 || SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount
== NULL
) {
127 PyErr_SetString(PyExc_Exception
, "First argument is not a buffer\n");
131 // Because some Python objects which support "buffer" protocol have more than one
132 // memory segment, we have to copy them into a contiguous memory.
133 SrcBuf
= PyMem_Malloc(SrcDataSize
);
134 if (SrcBuf
== NULL
) {
135 PyErr_SetString(PyExc_Exception
, "Not enough memory\n");
139 SegNum
= SrcData
->ob_type
->tp_as_buffer
->bf_getsegcount((PyObject
*)SrcData
, NULL
);
141 for (Index
= 0; Index
< SegNum
; ++Index
) {
145 Len
= SrcData
->ob_type
->tp_as_buffer
->bf_getreadbuffer((PyObject
*)SrcData
, Index
, &BufSeg
);
147 PyErr_SetString(PyExc_Exception
, "Buffer segment is not available\n");
150 memcpy(TmpBuf
, BufSeg
, Len
);
154 Status
= Extract((VOID
*)SrcBuf
, SrcDataSize
, (VOID
**)&DstBuf
, &DstDataSize
, 2);
155 if (Status
!= EFI_SUCCESS
) {
156 PyErr_SetString(PyExc_Exception
, "Failed to decompress\n");
160 return PyString_FromStringAndSize((CONST INT8
*)DstBuf
, (Py_ssize_t
)DstDataSize
);
163 if (SrcBuf
!= NULL
) {
167 if (DstBuf
!= NULL
) {
195 STATIC INT8 DecompressDocs
[] = "Decompress(): Decompress data using UEFI standard algorithm\n";
196 STATIC INT8 CompressDocs
[] = "Compress(): Compress data using UEFI standard algorithm\n";
198 STATIC PyMethodDef EfiCompressor_Funcs
[] = {
199 {"UefiDecompress", (PyCFunction
)UefiDecompress
, METH_VARARGS
, DecompressDocs
},
200 {"UefiCompress", (PyCFunction
)UefiCompress
, METH_VARARGS
, DecompressDocs
},
201 {"FrameworkDecompress", (PyCFunction
)FrameworkDecompress
, METH_VARARGS
, DecompressDocs
},
202 {"FrameworkCompress", (PyCFunction
)FrameworkCompress
, METH_VARARGS
, DecompressDocs
},
203 {NULL
, NULL
, 0, NULL
}
207 initEfiCompressor(VOID
) {
208 Py_InitModule3("EfiCompressor", EfiCompressor_Funcs
, "EFI Compression Algorithm Extension Module");