Added for wrapping compressor/decompressor
authorjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Mon, 28 Apr 2008 03:12:10 +0000 (03:12 +0000)
committerjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Mon, 28 Apr 2008 03:12:10 +0000 (03:12 +0000)
git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@1179 7335b38e-4728-0410-8992-fb3ffe349368

Source/C/PyEfiCompressor/EfiCompressor.c [new file with mode: 0644]
Source/C/PyEfiCompressor/Makefile [new file with mode: 0644]
Source/C/PyEfiCompressor/setup.py [new file with mode: 0644]

diff --git a/Source/C/PyEfiCompressor/EfiCompressor.c b/Source/C/PyEfiCompressor/EfiCompressor.c
new file mode 100644 (file)
index 0000000..39569e4
--- /dev/null
@@ -0,0 +1,199 @@
+#include <Python.h>\r
+#include <Decompress.h>\r
+\r
+/*\r
+ UefiDecompress(data_buffer, size, original_size)\r
+*/\r
+STATIC\r
+PyObject*\r
+UefiDecompress(\r
+  PyObject    *Self,\r
+  PyObject    *Args,\r
+  PyObject    *Keywords\r
+  ) \r
+{\r
+  PyTypeObject  *SrcData;\r
+  UINT32        SrcDataSize;\r
+  UINT32        DstDataSize;\r
+  UINT          Status;\r
+  UINT8         *SrcBuf;\r
+  UINT8         *DstBuf;\r
+  UINT8         *TmpBuf;\r
+  Py_ssize_t    SegNum;\r
+  Py_ssize_t    Index;\r
+\r
+  Status = PyArg_ParseTupleAndKeywords(\r
+            Args,\r
+            Keywords,\r
+            "0ii",\r
+            &SrcData,\r
+            &SrcDataSize,\r
+            &DstDataSize\r
+            );\r
+  if (Status == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  if (SrcData->tp_as_buffer == NULL\r
+      || SrcData->tp_as_buffer->bf_getreadbuffer == NULL\r
+      || SrcData->tp_as_buffer->bf_getsegcount == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  // Because some Python objects which support "buffer" protocol have more than one\r
+  // memory segment, we have to copy them into a contiguous memory.\r
+  SrcBuf = malloc(SrcDataSize);\r
+  DstBuf = malloc(DstDataSize);\r
+  if (SrcBuf == NULL || DstBuf == NULL) {\r
+    goto ERROR;\r
+  }\r
+\r
+  SegNum = SrcData->tp_as_buffer->bf_getsegcount(SrcData, NULL);\r
+  TmpBuf = SrcBuf\r
+  for (Index = 0; Index < SegNum; ++Index) {\r
+    VOID *BufSeg;\r
+    Py_ssize_t Len;\r
+\r
+    Len = SrcData->tp_as_buffer->bf_getreadbuffer(SrcData, Index, &BufSeg);\r
+    if (Len < 0) {\r
+      goto ERROR;\r
+    }\r
+    memcpy(TmpBuf, BufSeg, Len);\r
+    TmpBuf += Len;\r
+  }\r
+\r
+  Status = Extract((VOID *)SrcBuf, SrcDataSize, (VOID *)DstBuf, DstDataSize, 1);\r
+  if (Status != EFI_SUCCESS) {\r
+    goto ERROR;\r
+  }\r
+\r
+  return PyBuffer_FromMemory(DstBuf, (Py_ssize_t)DstDataSize);\r
+\r
+ERROR:\r
+  if (SrcBuf != NULL) {\r
+    free(SrcBuf);\r
+  }\r
+\r
+  if (DstBuf != NULL) {\r
+    free(DstBuf);\r
+  }\r
+  return NULL;\r
+}\r
+\r
+\r
+STATIC\r
+PyObject*\r
+FrameworkDecompress(\r
+  PyObject    *Self,\r
+  PyObject    *Args,\r
+  PyObject    *Keywords\r
+  )\r
+{\r
+  PyTypeObject  *SrcData;\r
+  UINT32        SrcDataSize;\r
+  UINT32        DstDataSize;\r
+  UINT          Status;\r
+  UINT8         *SrcBuf;\r
+  UINT8         *DstBuf;\r
+  UINT8         *TmpBuf;\r
+  Py_ssize_t    SegNum;\r
+  Py_ssize_t    Index;\r
+\r
+  Status = PyArg_ParseTupleAndKeywords(\r
+            Args,\r
+            Keywords,\r
+            "0ii",\r
+            &SrcData,\r
+            &SrcDataSize,\r
+            &DstDataSize\r
+            );\r
+  if (Status == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  if (SrcData->tp_as_buffer == NULL\r
+      || SrcData->tp_as_buffer->bf_getreadbuffer == NULL\r
+      || SrcData->tp_as_buffer->bf_getsegcount == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  // Because some Python objects which support "buffer" protocol have more than one\r
+  // memory segment, we have to copy them into a contiguous memory.\r
+  SrcBuf = malloc(SrcDataSize);\r
+  DstBuf = malloc(DstDataSize);\r
+  if (SrcBuf == NULL || DstBuf == NULL) {\r
+    goto ERROR;\r
+  }\r
+\r
+  SegNum = SrcData->tp_as_buffer->bf_getsegcount(SrcData, NULL);\r
+  TmpBuf = SrcBuf\r
+  for (Index = 0; Index < SegNum; ++Index) {\r
+    VOID *BufSeg;\r
+    Py_ssize_t Len;\r
+\r
+    Len = SrcData->tp_as_buffer->bf_getreadbuffer(SrcData, Index, &BufSeg);\r
+    if (Len < 0) {\r
+      goto ERROR;\r
+    }\r
+    memcpy(TmpBuf, BufSeg, Len);\r
+    TmpBuf += Len;\r
+  }\r
+\r
+  Status = Extract((VOID *)SrcBuf, SrcDataSize, (VOID *)DstBuf, DstDataSize, 2);\r
+  if (Status != EFI_SUCCESS) {\r
+    goto ERROR;\r
+  }\r
+\r
+  return PyBuffer_FromMemory(DstBuf, (Py_ssize_t)DstDataSize);\r
+\r
+ERROR:\r
+  if (SrcBuf != NULL) {\r
+    free(SrcBuf);\r
+  }\r
+\r
+  if (DstBuf != NULL) {\r
+    free(DstBuf);\r
+  }\r
+  return NULL;\r
+}\r
+\r
+STATIC\r
+PyObject*\r
+UefiCompress(\r
+  PyObject    *Self,\r
+  PyObject    *Args,\r
+  PyObject    *Keywords\r
+  ) \r
+{\r
+  return NULL;\r
+}\r
+\r
+\r
+STATIC\r
+PyObject*\r
+FrameworkCompress(\r
+  PyObject    *Self,\r
+  PyObject    *Args,\r
+  PyObject    *Keywords\r
+  )\r
+{\r
+  return NULL;\r
+}\r
+\r
+STATIC CHAR DecompressDocs[] = "Decompress(): Decompress data using UEFI standard algorithm\n";\r
+STATIC CHAR CompressDocs[] = "Compress(): Compress data using UEFI standard algorithm\n";\r
+\r
+STATIC PyMethodDef EfiCompressor_Funcs[] = {\r
+  {"UefiDecompress", (PyCFunction)UefiDecompress, METH_KEYWORDS, DecompressDocs},\r
+  {"UefiCompress", (PyCFunction)UefiCompress, METH_KEYWORDS, DecompressDocs},\r
+  {"FrameworkDecompress", (PyCFunction)FrameworkDecompress, METH_KEYWORDS, DecompressDocs},\r
+  {"FrameworkCompress", (PyCFunction)FrameworkCompress, METH_KEYWORDS, DecompressDocs},\r
+  {NULL, NULL, 0, NULL}\r
+};\r
+\r
+PyMODINIT_FUNC\r
+InitEfiCompressor(VOID) {\r
+  Py_InitModule3("EfiCompressor", EfiCompressor_Funcs, "EFI Compression Algorithm Extension Module");\r
+}\r
+\r
+\r
diff --git a/Source/C/PyEfiCompressor/Makefile b/Source/C/PyEfiCompressor/Makefile
new file mode 100644 (file)
index 0000000..502735b
--- /dev/null
@@ -0,0 +1,12 @@
+!INCLUDE ..\Makefiles\ms.common
+
+APPNAME = GenSec
+
+LIBS = $(LIB_PATH)\Common.lib
+
+OBJECTS = GenSec.obj
+
+#CFLAGS = $(CFLAGS) /nodefaultlib:libc.lib
+
+!INCLUDE ..\Makefiles\ms.app
+
diff --git a/Source/C/PyEfiCompressor/setup.py b/Source/C/PyEfiCompressor/setup.py
new file mode 100644 (file)
index 0000000..2a2edca
--- /dev/null
@@ -0,0 +1,46 @@
+## @file\r
+# package and install PyEfiCompressor extension\r
+#\r
+#  Copyright (c) 2008, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+from distutils.core import setup, Extension\r
+import os\r
+\r
+if 'BASE_TOOLS_PATH' not in os.environ:\r
+    raise "Please define BASE_TOOLS_PATH to the root of base tools tree"\r
+\r
+BaseToolsDir = os.environ['BASE_TOOLS_PATH']\r
+setup(\r
+    name="EfiCompressor",\r
+    version="0.01",\r
+    ext_modules=[\r
+        Extension(\r
+            'EfiCompressor',\r
+            sources=[\r
+                'EfiCompressor.c'\r
+                ],\r
+            include_dirs=[\r
+                os.path.join(BaseToolsDir, 'Source', 'C', 'Include'),\r
+                os.path.join(BaseToolsDir, 'Source', 'C', 'Common')\r
+                ],\r
+            library_dirs=[\r
+                os.path.join(os.environ['BASE_TOOLS_PATH'], 'Lib', 'Win32')\r
+                ],\r
+            libraries=[\r
+                'Common.lib'\r
+                ],\r
+        ],\r
+  )\r
+\r