f213b1b77adfd80fd4fcf1c1bf26086d6cc663e0
[jelmer/dulwich-libgit2.git] / dulwich / _objects.c
1 /* 
2  * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; version 2
7  * of the License or (at your option) a later version of the License.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA  02110-1301, USA.
18  */
19
20 #include <Python.h>
21
22 #define hexbyte(x) (isdigit(x)?(x)-'0':(x)-'a'+0xa)
23 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
24
25 static PyObject *py_hex_to_sha(PyObject *self, PyObject *py_hexsha)
26 {
27         char *hexsha;
28         char sha[20];
29         int i;
30
31         if (!PyString_CheckExact(py_hexsha)) {
32                 PyErr_SetString(PyExc_TypeError, "hex sha is not a string");
33                 return NULL;
34         }
35
36         if (PyString_Size(py_hexsha) != 40) {
37                 PyErr_SetString(PyExc_ValueError, "hex sha is not 40 bytes long");
38                 return NULL;
39         }
40
41         hexsha = PyString_AsString(py_hexsha);
42
43         for (i = 0; i < 20; i++) {
44                 sha[i] = (hexbyte(hexsha[i*2]) << 4) + hexbyte(hexsha[i*2+1]);
45         }
46
47         return PyString_FromStringAndSize(sha, 20);
48 }
49
50 static PyObject *sha_to_pyhex(const unsigned char *sha)
51 {
52         char hexsha[41];
53         int i;
54         for (i = 0; i < 20; i++) {
55                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
56                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
57         }
58         
59         return PyString_FromStringAndSize(hexsha, 40);
60 }
61
62 static PyObject *py_sha_to_hex(PyObject *self, PyObject *py_sha)
63 {
64         if (!PyString_CheckExact(py_sha)) {
65                 PyErr_SetString(PyExc_TypeError, "sha is not a string");
66                 return NULL;
67         }
68
69         if (PyString_Size(py_sha) != 20) {
70                 PyErr_SetString(PyExc_ValueError, "sha is not 20 bytes long");
71                 return NULL;
72         }
73
74         return sha_to_pyhex((unsigned char *)PyString_AsString(py_sha));
75 }
76
77 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
78 {
79         char *text, *end;
80         int len, namelen;
81         PyObject *ret, *item, *name;
82
83         if (!PyArg_ParseTuple(args, "s#", &text, &len))
84                 return NULL;
85
86         ret = PyDict_New();
87         if (ret == NULL) {
88                 return NULL;
89         }
90
91         end = text + len;
92
93     while (text < end) {
94         long mode;
95                 mode = strtol(text, &text, 8);
96
97                 if (*text != ' ') {
98                         PyErr_SetString(PyExc_RuntimeError, "Expected space");
99                         Py_DECREF(ret);
100                         return NULL;
101                 }
102
103                 text++;
104
105         namelen = strlen(text);
106
107                 name = PyString_FromStringAndSize(text, namelen);
108                 if (name == NULL) {
109                         Py_DECREF(ret);
110                         return NULL;
111                 }
112
113         item = Py_BuildValue("(lN)", mode, sha_to_pyhex((unsigned char *)text+namelen+1));
114         if (item == NULL) {
115             Py_DECREF(ret);
116                         Py_DECREF(name);
117             return NULL;
118         }
119                 if (PyDict_SetItem(ret, name, item) == -1) {
120                         Py_DECREF(ret);
121                         Py_DECREF(item);
122                         return NULL;
123                 }
124                 Py_DECREF(name);
125                 Py_DECREF(item);
126
127                 text += namelen+21;
128     }
129
130     return ret;
131 }
132
133 static PyMethodDef py_objects_methods[] = {
134         { "hex_to_sha", (PyCFunction)py_hex_to_sha, METH_O, NULL },
135         { "sha_to_hex", (PyCFunction)py_sha_to_hex, METH_O, NULL },
136         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL, },
137 };
138
139 void init_objects(void)
140 {
141         PyObject *m;
142
143         m = Py_InitModule3("_objects", py_objects_methods, NULL);
144         if (m == NULL)
145                 return;
146 }