Explorar el Código

Add (optional!) C implementations of hex_to_sha and sha_to_hex.

Jelmer Vernooij hace 16 años
padre
commit
fcf383bdad
Se han modificado 3 ficheros con 95 adiciones y 0 borrados
  1. 87 0
      dulwich/_objects.c
  2. 6 0
      dulwich/objects.py
  3. 2 0
      setup.py

+ 87 - 0
dulwich/_objects.c

@@ -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;
+}

+ 6 - 0
dulwich/objects.py

@@ -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
+

+ 2 - 0
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'])],
       )