Provide strnlen() on mingw32 which doesn't have it.
[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 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #if (PY_VERSION_HEX < 0x02050000)
25 typedef int Py_ssize_t;
26 #endif
27
28 #ifdef __MINGW32_VERSION
29 size_t strnlen(char *text, size_t maxlen)
30 {
31         const char *last = memchr(text, '\0', maxlen);
32         return last ? (size_t) (last - text) : maxlen;
33 }
34 #endif
35
36 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
37
38 static PyObject *sha_to_pyhex(const unsigned char *sha)
39 {
40         char hexsha[41];
41         int i;
42         for (i = 0; i < 20; i++) {
43                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
44                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
45         }
46
47         return PyString_FromStringAndSize(hexsha, 40);
48 }
49
50 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
51 {
52         char *text, *start, *end;
53         int len, namelen;
54         PyObject *ret, *item, *name;
55
56         if (!PyArg_ParseTuple(args, "s#", &text, &len))
57                 return NULL;
58
59         /* TODO: currently this returns a list; if memory usage is a concern,
60         * consider rewriting as a custom iterator object */
61         ret = PyList_New(0);
62
63         if (ret == NULL) {
64                 return NULL;
65         }
66
67         start = text;
68         end = text + len;
69
70         while (text < end) {
71                 long mode;
72                 mode = strtol(text, &text, 8);
73
74                 if (*text != ' ') {
75                         PyErr_SetString(PyExc_ValueError, "Expected space");
76                         Py_DECREF(ret);
77                         return NULL;
78                 }
79
80                 text++;
81
82                 namelen = strnlen(text, len - (text - start));
83
84                 name = PyString_FromStringAndSize(text, namelen);
85                 if (name == NULL) {
86                         Py_DECREF(ret);
87                         return NULL;
88                 }
89
90                 if (text + namelen + 20 >= end) {
91                         PyErr_SetString(PyExc_ValueError, "SHA truncated");
92                         Py_DECREF(ret);
93                         Py_DECREF(name);
94                         return NULL;
95                 }
96
97                 item = Py_BuildValue("(NlN)", name, mode,
98                                                          sha_to_pyhex((unsigned char *)text+namelen+1));
99                 if (item == NULL) {
100                         Py_DECREF(ret);
101                         Py_DECREF(name);
102                         return NULL;
103                 }
104                 if (PyList_Append(ret, item) == -1) {
105                         Py_DECREF(ret);
106                         Py_DECREF(item);
107                         return NULL;
108                 }
109                 Py_DECREF(item);
110
111                 text += namelen+21;
112         }
113
114         return ret;
115 }
116
117 struct tree_item {
118         const char *name;
119         int mode;
120         PyObject *tuple;
121 };
122
123 int cmp_tree_item(const void *_a, const void *_b)
124 {
125         const struct tree_item *a = _a, *b = _b;
126         const char *remain_a, *remain_b;
127         int ret, common;
128         if (strlen(a->name) > strlen(b->name)) {
129                 common = strlen(b->name);
130                 remain_a = a->name + common;
131                 remain_b = (S_ISDIR(b->mode)?"/":"");
132         } else if (strlen(b->name) > strlen(a->name)) {
133                 common = strlen(a->name);
134                 remain_a = (S_ISDIR(a->mode)?"/":"");
135                 remain_b = b->name + common;
136         } else { /* strlen(a->name) == strlen(b->name) */
137                 common = 0;
138                 remain_a = a->name;
139                 remain_b = b->name;
140         }
141         ret = strncmp(a->name, b->name, common);
142         if (ret != 0)
143                 return ret;
144         return strcmp(remain_a, remain_b);
145 }
146
147 static void free_tree_items(struct tree_item *items, int num) {
148         int i;
149         for (i = 0; i < num; i++) {
150                 Py_DECREF(items[i].tuple);
151         }
152         free(items);
153 }
154
155 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
156 {
157         struct tree_item *qsort_entries;
158         int num, i;
159         PyObject *ret;
160         Py_ssize_t pos = 0;
161         PyObject *key, *value;
162
163         if (!PyDict_Check(entries)) {
164                 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
165                 return NULL;
166         }
167
168         num = PyDict_Size(entries);
169         qsort_entries = malloc(num * sizeof(struct tree_item));
170         if (qsort_entries == NULL) {
171                 PyErr_NoMemory();
172                 return NULL;
173         }
174
175         i = 0;
176         while (PyDict_Next(entries, &pos, &key, &value)) {
177                 PyObject *py_mode, *py_int_mode, *py_sha;
178
179                 if (!PyString_Check(key)) {
180                         PyErr_SetString(PyExc_TypeError, "Name is not a string");
181                         free_tree_items(qsort_entries, i);
182                         return NULL;
183                 }
184
185                 if (PyTuple_Size(value) != 2) {
186                         PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
187                         free_tree_items(qsort_entries, i);
188                         return NULL;
189                 }
190
191                 py_mode = PyTuple_GET_ITEM(value, 0);
192                 py_int_mode = PyNumber_Int(py_mode);
193                 if (!py_int_mode) {
194                         PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
195                         free_tree_items(qsort_entries, i);
196                         return NULL;
197                 }
198
199                 py_sha = PyTuple_GET_ITEM(value, 1);
200                 if (!PyString_Check(py_sha)) {
201                         PyErr_SetString(PyExc_TypeError, "SHA is not a string");
202                         Py_DECREF(py_int_mode);
203                         free_tree_items(qsort_entries, i);
204                         return NULL;
205                 }
206                 qsort_entries[i].name = PyString_AS_STRING(key);
207                 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
208                 qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
209                 Py_DECREF(py_int_mode);
210                 i++;
211         }
212
213         qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
214
215         ret = PyList_New(num);
216         if (ret == NULL) {
217                 free_tree_items(qsort_entries, i);
218                 PyErr_NoMemory();
219                 return NULL;
220         }
221
222         for (i = 0; i < num; i++) {
223                 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
224         }
225
226         free(qsort_entries);
227
228         return ret;
229 }
230
231 static PyMethodDef py_objects_methods[] = {
232         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
233         { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
234         { NULL, NULL, 0, NULL }
235 };
236
237 void init_objects(void)
238 {
239         PyObject *m;
240
241         m = Py_InitModule3("_objects", py_objects_methods, NULL);
242         if (m == NULL)
243                 return;
244 }