Add (optional!) C implementations of hex_to_sha and sha_to_hex.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 29 Mar 2009 02:43:01 +0000 (02:43 +0000)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 29 Mar 2009 02:43:01 +0000 (02:43 +0000)
dulwich/_objects.c [new file with mode: 0644]
dulwich/objects.py
setup.py

diff --git a/dulwich/_objects.c b/dulwich/_objects.c
new file mode 100644 (file)
index 0000000..7e5a635
--- /dev/null
@@ -0,0 +1,87 @@
+/* 
+ * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License or (at your option) a later version of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+
+#include <Python.h>
+
+#define hexbyte(x) (isdigit(x)?(x)-'0':(x)-'a'+0xa)
+#define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
+
+static PyObject *py_hex_to_sha(PyObject *self, PyObject *py_hexsha)
+{
+       char *hexsha;
+       char sha[20];
+       int i;
+
+       if (!PyString_Check(py_hexsha)) {
+               PyErr_SetString(PyExc_TypeError, "hex sha is not a string");
+               return NULL;
+       }
+
+       if (PyString_Size(py_hexsha) != 40) {
+               PyErr_SetString(PyExc_ValueError, "hex sha is not 40 bytes long");
+               return NULL;
+       }
+
+       hexsha = PyString_AsString(py_hexsha);
+
+       for (i = 0; i < 20; i++) {
+               sha[i] = (hexbyte(hexsha[i*2]) << 4) + hexbyte(hexsha[i*2+1]);
+       }
+
+       return PyString_FromStringAndSize(sha, 20);
+}
+
+static PyObject *py_sha_to_hex(PyObject *self, PyObject *py_sha)
+{
+       char hexsha[41];
+       unsigned char *sha;
+       int i;
+
+       if (!PyString_Check(py_sha)) {
+               PyErr_SetString(PyExc_TypeError, "sha is not a string");
+               return NULL;
+       }
+
+       if (PyString_Size(py_sha) != 20) {
+               PyErr_SetString(PyExc_ValueError, "sha is not 20 bytes long");
+               return NULL;
+       }
+
+       sha = (unsigned char *)PyString_AsString(py_sha);
+       for (i = 0; i < 20; i++) {
+               hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
+               hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
+       }
+       
+       return PyString_FromStringAndSize(hexsha, 40);
+}
+
+static PyMethodDef py_objects_methods[] = {
+       { "hex_to_sha", (PyCFunction)py_hex_to_sha, METH_O, NULL },
+       { "sha_to_hex", (PyCFunction)py_sha_to_hex, METH_O, NULL },
+};
+
+void init_objects(void)
+{
+       PyObject *m;
+
+       m = Py_InitModule3("_objects", py_objects_methods, NULL);
+       if (m == NULL)
+               return;
+}
index e38ef772f9639f88c798df68f9b32faf8d49f8aa..f8b8d96ed42ef3c77e8dd86eab0eb196c75c4cea 100644 (file)
@@ -516,3 +516,9 @@ num_type_map = {
     # 5 Is reserved for further expansion
 }
 
+try:
+    # Try to import C versions
+    from dulwich._objects import hex_to_sha, sha_to_hex
+except ImportError:
+    pass
+
index 659b6e54a4660f417103f32410b9193e8e612cc1..8321d62be1ce65a62e4b00ec8c6a62578bc3beea 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -3,6 +3,7 @@
 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 
 from distutils.core import setup
+from distutils.extension import Extension
 
 dulwich_version_string = '0.1.1'
 
@@ -22,4 +23,5 @@ setup(name='dulwich',
       """,
       packages=['dulwich', 'dulwich.tests'],
       scripts=['bin/dulwich', 'bin/dul-daemon'],
+      ext_modules=[Extension('dulwich/_objects', ['dulwich/_objects.c'])],
       )