Show More
@@ -1,720 +1,740 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | parsers.c - efficient content parsing |
|
3 | 3 | |
|
4 | 4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #include <Python.h> |
|
11 | 11 | #include <ctype.h> |
|
12 | 12 | #include <string.h> |
|
13 | 13 | |
|
14 | 14 | #include "util.h" |
|
15 | 15 | |
|
16 | 16 | static int hexdigit(char c) |
|
17 | 17 | { |
|
18 | 18 | if (c >= '0' && c <= '9') |
|
19 | 19 | return c - '0'; |
|
20 | 20 | if (c >= 'a' && c <= 'f') |
|
21 | 21 | return c - 'a' + 10; |
|
22 | 22 | if (c >= 'A' && c <= 'F') |
|
23 | 23 | return c - 'A' + 10; |
|
24 | 24 | |
|
25 | 25 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
26 | 26 | return 0; |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | /* |
|
30 | 30 | * Turn a hex-encoded string into binary. |
|
31 | 31 | */ |
|
32 | 32 | static PyObject *unhexlify(const char *str, int len) |
|
33 | 33 | { |
|
34 | 34 | PyObject *ret; |
|
35 | 35 | const char *c; |
|
36 | 36 | char *d; |
|
37 | 37 | |
|
38 | 38 | ret = PyBytes_FromStringAndSize(NULL, len / 2); |
|
39 | 39 | |
|
40 | 40 | if (!ret) |
|
41 | 41 | return NULL; |
|
42 | 42 | |
|
43 | 43 | d = PyBytes_AsString(ret); |
|
44 | 44 | |
|
45 | 45 | for (c = str; c < str + len;) { |
|
46 | 46 | int hi = hexdigit(*c++); |
|
47 | 47 | int lo = hexdigit(*c++); |
|
48 | 48 | *d++ = (hi << 4) | lo; |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | return ret; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | /* |
|
55 | 55 | * This code assumes that a manifest is stitched together with newline |
|
56 | 56 | * ('\n') characters. |
|
57 | 57 | */ |
|
58 | 58 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
59 | 59 | { |
|
60 | 60 | PyObject *mfdict, *fdict; |
|
61 | 61 | char *str, *cur, *start, *zero; |
|
62 | 62 | int len; |
|
63 | 63 | |
|
64 | 64 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
65 | 65 | &PyDict_Type, &mfdict, |
|
66 | 66 | &PyDict_Type, &fdict, |
|
67 | 67 | &str, &len)) |
|
68 | 68 | goto quit; |
|
69 | 69 | |
|
70 | 70 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { |
|
71 | 71 | PyObject *file = NULL, *node = NULL; |
|
72 | 72 | PyObject *flags = NULL; |
|
73 | 73 | int nlen; |
|
74 | 74 | |
|
75 | 75 | if (!*cur) { |
|
76 | 76 | zero = cur; |
|
77 | 77 | continue; |
|
78 | 78 | } |
|
79 | 79 | else if (*cur != '\n') |
|
80 | 80 | continue; |
|
81 | 81 | |
|
82 | 82 | if (!zero) { |
|
83 | 83 | PyErr_SetString(PyExc_ValueError, |
|
84 | 84 | "manifest entry has no separator"); |
|
85 | 85 | goto quit; |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | file = PyBytes_FromStringAndSize(start, zero - start); |
|
89 | 89 | |
|
90 | 90 | if (!file) |
|
91 | 91 | goto bail; |
|
92 | 92 | |
|
93 | 93 | nlen = cur - zero - 1; |
|
94 | 94 | |
|
95 | 95 | node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen); |
|
96 | 96 | if (!node) |
|
97 | 97 | goto bail; |
|
98 | 98 | |
|
99 | 99 | if (nlen > 40) { |
|
100 | 100 | flags = PyBytes_FromStringAndSize(zero + 41, |
|
101 | 101 | nlen - 40); |
|
102 | 102 | if (!flags) |
|
103 | 103 | goto bail; |
|
104 | 104 | |
|
105 | 105 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
106 | 106 | goto bail; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
110 | 110 | goto bail; |
|
111 | 111 | |
|
112 | 112 | start = cur + 1; |
|
113 | 113 | zero = NULL; |
|
114 | 114 | |
|
115 | 115 | Py_XDECREF(flags); |
|
116 | 116 | Py_XDECREF(node); |
|
117 | 117 | Py_XDECREF(file); |
|
118 | 118 | continue; |
|
119 | 119 | bail: |
|
120 | 120 | Py_XDECREF(flags); |
|
121 | 121 | Py_XDECREF(node); |
|
122 | 122 | Py_XDECREF(file); |
|
123 | 123 | goto quit; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | if (len > 0 && *(cur - 1) != '\n') { |
|
127 | 127 | PyErr_SetString(PyExc_ValueError, |
|
128 | 128 | "manifest contains trailing garbage"); |
|
129 | 129 | goto quit; |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | Py_INCREF(Py_None); |
|
133 | 133 | return Py_None; |
|
134 | 134 | quit: |
|
135 | 135 | return NULL; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | #ifdef _WIN32 |
|
139 | 139 | #ifdef _MSC_VER |
|
140 | 140 | /* msvc 6.0 has problems */ |
|
141 | 141 | #define inline __inline |
|
142 | 142 | typedef unsigned long uint32_t; |
|
143 | 143 | typedef unsigned __int64 uint64_t; |
|
144 | 144 | #else |
|
145 | 145 | #include <stdint.h> |
|
146 | 146 | #endif |
|
147 | 147 | static uint32_t ntohl(uint32_t x) |
|
148 | 148 | { |
|
149 | 149 | return ((x & 0x000000ffUL) << 24) | |
|
150 | 150 | ((x & 0x0000ff00UL) << 8) | |
|
151 | 151 | ((x & 0x00ff0000UL) >> 8) | |
|
152 | 152 | ((x & 0xff000000UL) >> 24); |
|
153 | 153 | } |
|
154 | 154 | #else |
|
155 | 155 | /* not windows */ |
|
156 | 156 | #include <sys/types.h> |
|
157 | 157 | #if defined __BEOS__ && !defined __HAIKU__ |
|
158 | 158 | #include <ByteOrder.h> |
|
159 | 159 | #else |
|
160 | 160 | #include <arpa/inet.h> |
|
161 | 161 | #endif |
|
162 | 162 | #include <inttypes.h> |
|
163 | 163 | #endif |
|
164 | 164 | |
|
165 | 165 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
166 | 166 | { |
|
167 | 167 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
168 | 168 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
169 | 169 | char *str, *cur, *end, *cpos; |
|
170 | 170 | int state, mode, size, mtime; |
|
171 | 171 | unsigned int flen; |
|
172 | 172 | int len; |
|
173 | 173 | uint32_t decode[4]; /* for alignment */ |
|
174 | 174 | |
|
175 | 175 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
176 | 176 | &PyDict_Type, &dmap, |
|
177 | 177 | &PyDict_Type, &cmap, |
|
178 | 178 | &str, &len)) |
|
179 | 179 | goto quit; |
|
180 | 180 | |
|
181 | 181 | /* read parents */ |
|
182 | 182 | if (len < 40) |
|
183 | 183 | goto quit; |
|
184 | 184 | |
|
185 | 185 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
186 | 186 | if (!parents) |
|
187 | 187 | goto quit; |
|
188 | 188 | |
|
189 | 189 | /* read filenames */ |
|
190 | 190 | cur = str + 40; |
|
191 | 191 | end = str + len; |
|
192 | 192 | |
|
193 | 193 | while (cur < end - 17) { |
|
194 | 194 | /* unpack header */ |
|
195 | 195 | state = *cur; |
|
196 | 196 | memcpy(decode, cur + 1, 16); |
|
197 | 197 | mode = ntohl(decode[0]); |
|
198 | 198 | size = ntohl(decode[1]); |
|
199 | 199 | mtime = ntohl(decode[2]); |
|
200 | 200 | flen = ntohl(decode[3]); |
|
201 | 201 | cur += 17; |
|
202 | 202 | if (cur + flen > end || cur + flen < cur) { |
|
203 | 203 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); |
|
204 | 204 | goto quit; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | entry = Py_BuildValue("ciii", state, mode, size, mtime); |
|
208 | 208 | if (!entry) |
|
209 | 209 | goto quit; |
|
210 | 210 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
211 | 211 | |
|
212 | 212 | cpos = memchr(cur, 0, flen); |
|
213 | 213 | if (cpos) { |
|
214 | 214 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
215 | 215 | cname = PyBytes_FromStringAndSize(cpos + 1, |
|
216 | 216 | flen - (cpos - cur) - 1); |
|
217 | 217 | if (!fname || !cname || |
|
218 | 218 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
219 | 219 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
220 | 220 | goto quit; |
|
221 | 221 | Py_DECREF(cname); |
|
222 | 222 | } else { |
|
223 | 223 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
224 | 224 | if (!fname || |
|
225 | 225 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
226 | 226 | goto quit; |
|
227 | 227 | } |
|
228 | 228 | cur += flen; |
|
229 | 229 | Py_DECREF(fname); |
|
230 | 230 | Py_DECREF(entry); |
|
231 | 231 | fname = cname = entry = NULL; |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | ret = parents; |
|
235 | 235 | Py_INCREF(ret); |
|
236 | 236 | quit: |
|
237 | 237 | Py_XDECREF(fname); |
|
238 | 238 | Py_XDECREF(cname); |
|
239 | 239 | Py_XDECREF(entry); |
|
240 | 240 | Py_XDECREF(parents); |
|
241 | 241 | return ret; |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | /* |
|
245 | 245 | * A list-like object that decodes the contents of a RevlogNG index |
|
246 | 246 | * file on demand. It has limited support for insert and delete at the |
|
247 | 247 | * last element before the end. The last entry is always a sentinel |
|
248 | 248 | * nullid. |
|
249 | 249 | */ |
|
250 | 250 | typedef struct { |
|
251 | 251 | PyObject_HEAD |
|
252 | 252 | /* Type-specific fields go here. */ |
|
253 | 253 | PyObject *data; /* raw bytes of index */ |
|
254 | 254 | PyObject **cache; /* cached tuples */ |
|
255 | 255 | const char **offsets; /* populated on demand */ |
|
256 | 256 | Py_ssize_t raw_length; /* original number of elements */ |
|
257 | 257 | Py_ssize_t length; /* current number of elements */ |
|
258 | 258 | PyObject *added; /* populated on demand */ |
|
259 | 259 | int inlined; |
|
260 | 260 | } indexObject; |
|
261 | 261 | |
|
262 | 262 | static Py_ssize_t index_length(indexObject *self) |
|
263 | 263 | { |
|
264 | 264 | if (self->added == NULL) |
|
265 | 265 | return self->length; |
|
266 | 266 | return self->length + PyList_GET_SIZE(self->added); |
|
267 | 267 | } |
|
268 | 268 | |
|
269 | 269 | static PyObject *nullentry; |
|
270 | 270 | |
|
271 | 271 | static long inline_scan(indexObject *self, const char **offsets); |
|
272 | 272 | |
|
273 | 273 | #if LONG_MAX == 0x7fffffffL |
|
274 | 274 | static const char *tuple_format = "Kiiiiiis#"; |
|
275 | 275 | #else |
|
276 | 276 | static const char *tuple_format = "kiiiiiis#"; |
|
277 | 277 | #endif |
|
278 | 278 | |
|
279 | 279 | /* RevlogNG format (all in big endian, data may be inlined): |
|
280 | 280 | * 6 bytes: offset |
|
281 | 281 | * 2 bytes: flags |
|
282 | 282 | * 4 bytes: compressed length |
|
283 | 283 | * 4 bytes: uncompressed length |
|
284 | 284 | * 4 bytes: base revision |
|
285 | 285 | * 4 bytes: link revision |
|
286 | 286 | * 4 bytes: parent 1 revision |
|
287 | 287 | * 4 bytes: parent 2 revision |
|
288 | 288 | * 32 bytes: nodeid (only 20 bytes used) |
|
289 | 289 | */ |
|
290 | 290 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
291 | 291 | { |
|
292 | 292 | uint32_t decode[8]; /* to enforce alignment with inline data */ |
|
293 | 293 | uint64_t offset_flags; |
|
294 | 294 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
295 | 295 | const char *c_node_id; |
|
296 | 296 | const char *data; |
|
297 | 297 | Py_ssize_t length = index_length(self); |
|
298 | 298 | PyObject *entry; |
|
299 | 299 | |
|
300 | 300 | if (pos >= length) { |
|
301 | 301 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
302 | 302 | return NULL; |
|
303 | 303 | } |
|
304 | 304 | |
|
305 | 305 | if (pos == length - 1) { |
|
306 | 306 | Py_INCREF(nullentry); |
|
307 | 307 | return nullentry; |
|
308 | 308 | } |
|
309 | 309 | |
|
310 | 310 | if (pos >= self->length - 1) { |
|
311 | 311 | PyObject *obj; |
|
312 | 312 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
313 | 313 | Py_INCREF(obj); |
|
314 | 314 | return obj; |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | if (self->cache) { |
|
318 | 318 | if (self->cache[pos]) { |
|
319 | 319 | Py_INCREF(self->cache[pos]); |
|
320 | 320 | return self->cache[pos]; |
|
321 | 321 | } |
|
322 | 322 | } else { |
|
323 | 323 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
324 | 324 | if (self->cache == NULL) |
|
325 | 325 | return PyErr_NoMemory(); |
|
326 | 326 | } |
|
327 | 327 | |
|
328 | 328 | if (self->inlined && pos > 0) { |
|
329 | 329 | if (self->offsets == NULL) { |
|
330 | 330 | self->offsets = malloc(self->raw_length * |
|
331 | 331 | sizeof(*self->offsets)); |
|
332 | 332 | if (self->offsets == NULL) |
|
333 | 333 | return PyErr_NoMemory(); |
|
334 | 334 | inline_scan(self, self->offsets); |
|
335 | 335 | } |
|
336 | 336 | data = self->offsets[pos]; |
|
337 | 337 | } else |
|
338 | 338 | data = PyString_AS_STRING(self->data) + pos * 64; |
|
339 | 339 | |
|
340 | 340 | memcpy(decode, data, 8 * sizeof(uint32_t)); |
|
341 | 341 | |
|
342 | 342 | offset_flags = ntohl(decode[1]); |
|
343 | 343 | if (pos == 0) /* mask out version number for the first entry */ |
|
344 | 344 | offset_flags &= 0xFFFF; |
|
345 | 345 | else { |
|
346 | 346 | uint32_t offset_high = ntohl(decode[0]); |
|
347 | 347 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | comp_len = ntohl(decode[2]); |
|
351 | 351 | uncomp_len = ntohl(decode[3]); |
|
352 | 352 | base_rev = ntohl(decode[4]); |
|
353 | 353 | link_rev = ntohl(decode[5]); |
|
354 | 354 | parent_1 = ntohl(decode[6]); |
|
355 | 355 | parent_2 = ntohl(decode[7]); |
|
356 | 356 | c_node_id = data + 32; |
|
357 | 357 | |
|
358 | 358 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, |
|
359 | 359 | uncomp_len, base_rev, link_rev, |
|
360 | 360 | parent_1, parent_2, c_node_id, 20); |
|
361 | 361 | |
|
362 | 362 | if (entry) |
|
363 | 363 | PyObject_GC_UnTrack(entry); |
|
364 | 364 | |
|
365 | 365 | self->cache[pos] = entry; |
|
366 | 366 | Py_INCREF(entry); |
|
367 | 367 | |
|
368 | 368 | return entry; |
|
369 | 369 | } |
|
370 | 370 | |
|
371 | 371 | static PyObject *index_insert(indexObject *self, PyObject *args) |
|
372 | 372 | { |
|
373 | 373 | PyObject *obj, *node; |
|
374 | 374 | long offset; |
|
375 | 375 | Py_ssize_t len; |
|
376 | 376 | |
|
377 | 377 | if (!PyArg_ParseTuple(args, "lO", &offset, &obj)) |
|
378 | 378 | return NULL; |
|
379 | 379 | |
|
380 | 380 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
381 | 381 | PyErr_SetString(PyExc_ValueError, "8-tuple required"); |
|
382 | 382 | return NULL; |
|
383 | 383 | } |
|
384 | 384 | |
|
385 | 385 | node = PyTuple_GET_ITEM(obj, 7); |
|
386 | 386 | if (!PyString_Check(node) || PyString_GET_SIZE(node) != 20) { |
|
387 | 387 | PyErr_SetString(PyExc_ValueError, |
|
388 | 388 | "20-byte hash required as last element"); |
|
389 | 389 | return NULL; |
|
390 | 390 | } |
|
391 | 391 | |
|
392 | 392 | len = index_length(self); |
|
393 | 393 | |
|
394 | 394 | if (offset < 0) |
|
395 | 395 | offset += len; |
|
396 | 396 | |
|
397 | 397 | if (offset != len - 1) { |
|
398 | 398 | PyErr_SetString(PyExc_IndexError, |
|
399 | 399 | "insert only supported at index -1"); |
|
400 | 400 | return NULL; |
|
401 | 401 | } |
|
402 | 402 | |
|
403 | 403 | if (self->added == NULL) { |
|
404 | 404 | self->added = PyList_New(0); |
|
405 | 405 | if (self->added == NULL) |
|
406 | 406 | return NULL; |
|
407 | 407 | } |
|
408 | 408 | |
|
409 | 409 | if (PyList_Append(self->added, obj) == -1) |
|
410 | 410 | return NULL; |
|
411 | 411 | |
|
412 | 412 | Py_RETURN_NONE; |
|
413 | 413 | } |
|
414 | 414 | |
|
415 | static void _index_clearcaches(indexObject *self) | |
|
416 | { | |
|
417 | if (self->cache) { | |
|
418 | Py_ssize_t i; | |
|
419 | ||
|
420 | for (i = 0; i < self->raw_length; i++) { | |
|
421 | Py_XDECREF(self->cache[i]); | |
|
422 | self->cache[i] = NULL; | |
|
423 | } | |
|
424 | free(self->cache); | |
|
425 | self->cache = NULL; | |
|
426 | } | |
|
427 | if (self->offsets) { | |
|
428 | free(self->offsets); | |
|
429 | self->offsets = NULL; | |
|
430 | } | |
|
431 | } | |
|
432 | ||
|
433 | static PyObject *index_clearcaches(indexObject *self) | |
|
434 | { | |
|
435 | _index_clearcaches(self); | |
|
436 | Py_RETURN_NONE; | |
|
437 | } | |
|
438 | ||
|
415 | 439 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
416 | 440 | PyObject *value) |
|
417 | 441 | { |
|
418 | 442 | Py_ssize_t start, stop, step, slicelength; |
|
419 | 443 | Py_ssize_t length = index_length(self); |
|
420 | 444 | |
|
421 | 445 | if (!PySlice_Check(item) || value != NULL) { |
|
422 | 446 | PyErr_SetString(PyExc_TypeError, |
|
423 | 447 | "revlog index only supports slice deletion"); |
|
424 | 448 | return -1; |
|
425 | 449 | } |
|
426 | 450 | |
|
427 | 451 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, |
|
428 | 452 | &start, &stop, &step, &slicelength) < 0) |
|
429 | 453 | return -1; |
|
430 | 454 | |
|
431 | 455 | if (slicelength <= 0) |
|
432 | 456 | return 0; |
|
433 | 457 | |
|
434 | 458 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
435 | 459 | stop = start; |
|
436 | 460 | |
|
437 | 461 | if (step < 0) { |
|
438 | 462 | stop = start + 1; |
|
439 | 463 | start = stop + step*(slicelength - 1) - 1; |
|
440 | 464 | step = -step; |
|
441 | 465 | } |
|
442 | 466 | |
|
443 | 467 | if (step != 1) { |
|
444 | 468 | PyErr_SetString(PyExc_ValueError, |
|
445 | 469 | "revlog index delete requires step size of 1"); |
|
446 | 470 | return -1; |
|
447 | 471 | } |
|
448 | 472 | |
|
449 | 473 | if (stop != length - 1) { |
|
450 | 474 | PyErr_SetString(PyExc_IndexError, |
|
451 | 475 | "revlog index deletion indices are invalid"); |
|
452 | 476 | return -1; |
|
453 | 477 | } |
|
454 | 478 | |
|
455 | 479 | if (start < self->length) { |
|
456 | 480 | self->length = start + 1; |
|
457 | 481 | if (self->added) { |
|
458 | 482 | Py_DECREF(self->added); |
|
459 | 483 | self->added = NULL; |
|
460 | 484 | } |
|
461 | 485 | return 0; |
|
462 | 486 | } |
|
463 | 487 | |
|
464 | 488 | return PyList_SetSlice(self->added, start - self->length + 1, |
|
465 | 489 | PyList_GET_SIZE(self->added), |
|
466 | 490 | NULL); |
|
467 | 491 | } |
|
468 | 492 | |
|
469 | 493 | static long inline_scan(indexObject *self, const char **offsets) |
|
470 | 494 | { |
|
471 | 495 | const char *data = PyString_AS_STRING(self->data); |
|
472 | 496 | const char *end = data + PyString_GET_SIZE(self->data); |
|
473 | 497 | const long hdrsize = 64; |
|
474 | 498 | long incr = hdrsize; |
|
475 | 499 | Py_ssize_t len = 0; |
|
476 | 500 | |
|
477 | 501 | while (data + hdrsize <= end) { |
|
478 | 502 | uint32_t comp_len; |
|
479 | 503 | const char *old_data; |
|
480 | 504 | /* 3rd element of header is length of compressed inline data */ |
|
481 | 505 | memcpy(&comp_len, data + 8, sizeof(uint32_t)); |
|
482 | 506 | incr = hdrsize + ntohl(comp_len); |
|
483 | 507 | if (incr < hdrsize) |
|
484 | 508 | break; |
|
485 | 509 | if (offsets) |
|
486 | 510 | offsets[len] = data; |
|
487 | 511 | len++; |
|
488 | 512 | old_data = data; |
|
489 | 513 | data += incr; |
|
490 | 514 | if (data <= old_data) |
|
491 | 515 | break; |
|
492 | 516 | } |
|
493 | 517 | |
|
494 | 518 | if (data != end && data + hdrsize != end) { |
|
495 | 519 | if (!PyErr_Occurred()) |
|
496 | 520 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
497 | 521 | return -1; |
|
498 | 522 | } |
|
499 | 523 | |
|
500 | 524 | return len; |
|
501 | 525 | } |
|
502 | 526 | |
|
503 | 527 | static int index_real_init(indexObject *self, const char *data, int size, |
|
504 | 528 | PyObject *inlined_obj, PyObject *data_obj) |
|
505 | 529 | { |
|
506 | 530 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
507 | 531 | self->data = data_obj; |
|
508 | 532 | self->cache = NULL; |
|
509 | 533 | |
|
510 | 534 | self->added = NULL; |
|
511 | 535 | self->offsets = NULL; |
|
512 | 536 | Py_INCREF(self->data); |
|
513 | 537 | |
|
514 | 538 | if (self->inlined) { |
|
515 | 539 | long len = inline_scan(self, NULL); |
|
516 | 540 | if (len == -1) |
|
517 | 541 | goto bail; |
|
518 | 542 | self->raw_length = len; |
|
519 | 543 | self->length = len + 1; |
|
520 | 544 | } else { |
|
521 | 545 | if (size % 64) { |
|
522 | 546 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
523 | 547 | goto bail; |
|
524 | 548 | } |
|
525 | 549 | self->raw_length = size / 64; |
|
526 | 550 | self->length = self->raw_length + 1; |
|
527 | 551 | } |
|
528 | 552 | |
|
529 | 553 | return 0; |
|
530 | 554 | bail: |
|
531 | 555 | return -1; |
|
532 | 556 | } |
|
533 | 557 | |
|
534 | 558 | static int index_init(indexObject *self, PyObject *args, PyObject *kwds) |
|
535 | 559 | { |
|
536 | 560 | const char *data; |
|
537 | 561 | int size; |
|
538 | 562 | PyObject *inlined_obj; |
|
539 | 563 | |
|
540 | 564 | if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj)) |
|
541 | 565 | return -1; |
|
542 | 566 | |
|
543 | 567 | return index_real_init(self, data, size, inlined_obj, |
|
544 | 568 | PyTuple_GET_ITEM(args, 0)); |
|
545 | 569 | } |
|
546 | 570 | |
|
547 | 571 | static void index_dealloc(indexObject *self) |
|
548 | 572 | { |
|
573 | _index_clearcaches(self); | |
|
549 | 574 | Py_DECREF(self->data); |
|
550 | if (self->cache) { | |
|
551 | Py_ssize_t i; | |
|
552 | ||
|
553 | for (i = 0; i < self->raw_length; i++) | |
|
554 | Py_XDECREF(self->cache[i]); | |
|
555 | } | |
|
556 | 575 | Py_XDECREF(self->added); |
|
557 | free(self->offsets); | |
|
558 | 576 | PyObject_Del(self); |
|
559 | 577 | } |
|
560 | 578 | |
|
561 | 579 | static PySequenceMethods index_sequence_methods = { |
|
562 | 580 | (lenfunc)index_length, /* sq_length */ |
|
563 | 581 | 0, /* sq_concat */ |
|
564 | 582 | 0, /* sq_repeat */ |
|
565 | 583 | (ssizeargfunc)index_get, /* sq_item */ |
|
566 | 584 | }; |
|
567 | 585 | |
|
568 | 586 | static PyMappingMethods index_mapping_methods = { |
|
569 | 587 | (lenfunc)index_length, /* mp_length */ |
|
570 | 588 | NULL, /* mp_subscript */ |
|
571 | 589 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
572 | 590 | }; |
|
573 | 591 | |
|
574 | 592 | static PyMethodDef index_methods[] = { |
|
593 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, | |
|
594 | "clear the index caches"}, | |
|
575 | 595 | {"insert", (PyCFunction)index_insert, METH_VARARGS, |
|
576 | 596 | "insert an index entry"}, |
|
577 | 597 | {NULL} /* Sentinel */ |
|
578 | 598 | }; |
|
579 | 599 | |
|
580 | 600 | static PyTypeObject indexType = { |
|
581 | 601 | PyObject_HEAD_INIT(NULL) |
|
582 | 602 | 0, /* ob_size */ |
|
583 | 603 | "parsers.index", /* tp_name */ |
|
584 | 604 | sizeof(indexObject), /* tp_basicsize */ |
|
585 | 605 | 0, /* tp_itemsize */ |
|
586 | 606 | (destructor)index_dealloc, /* tp_dealloc */ |
|
587 | 607 | 0, /* tp_print */ |
|
588 | 608 | 0, /* tp_getattr */ |
|
589 | 609 | 0, /* tp_setattr */ |
|
590 | 610 | 0, /* tp_compare */ |
|
591 | 611 | 0, /* tp_repr */ |
|
592 | 612 | 0, /* tp_as_number */ |
|
593 | 613 | &index_sequence_methods, /* tp_as_sequence */ |
|
594 | 614 | &index_mapping_methods, /* tp_as_mapping */ |
|
595 | 615 | 0, /* tp_hash */ |
|
596 | 616 | 0, /* tp_call */ |
|
597 | 617 | 0, /* tp_str */ |
|
598 | 618 | 0, /* tp_getattro */ |
|
599 | 619 | 0, /* tp_setattro */ |
|
600 | 620 | 0, /* tp_as_buffer */ |
|
601 | 621 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
602 | 622 | "revlog index", /* tp_doc */ |
|
603 | 623 | 0, /* tp_traverse */ |
|
604 | 624 | 0, /* tp_clear */ |
|
605 | 625 | 0, /* tp_richcompare */ |
|
606 | 626 | 0, /* tp_weaklistoffset */ |
|
607 | 627 | 0, /* tp_iter */ |
|
608 | 628 | 0, /* tp_iternext */ |
|
609 | 629 | index_methods, /* tp_methods */ |
|
610 | 630 | 0, /* tp_members */ |
|
611 | 631 | 0, /* tp_getset */ |
|
612 | 632 | 0, /* tp_base */ |
|
613 | 633 | 0, /* tp_dict */ |
|
614 | 634 | 0, /* tp_descr_get */ |
|
615 | 635 | 0, /* tp_descr_set */ |
|
616 | 636 | 0, /* tp_dictoffset */ |
|
617 | 637 | (initproc)index_init, /* tp_init */ |
|
618 | 638 | 0, /* tp_alloc */ |
|
619 | 639 | PyType_GenericNew, /* tp_new */ |
|
620 | 640 | }; |
|
621 | 641 | |
|
622 | 642 | /* |
|
623 | 643 | * returns a tuple of the form (index, None, cache) with elements as |
|
624 | 644 | * follows: |
|
625 | 645 | * |
|
626 | 646 | * index: an index object that lazily parses the RevlogNG records |
|
627 | 647 | * cache: if data is inlined, a tuple (index_file_content, 0), else None |
|
628 | 648 | * |
|
629 | 649 | * added complications are for backwards compatibility |
|
630 | 650 | */ |
|
631 | 651 | static PyObject *parse_index2(PyObject *self, PyObject *args) |
|
632 | 652 | { |
|
633 | 653 | const char *data; |
|
634 | 654 | int size, ret; |
|
635 | 655 | PyObject *inlined_obj, *tuple = NULL, *cache = NULL; |
|
636 | 656 | indexObject *idx; |
|
637 | 657 | |
|
638 | 658 | if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj)) |
|
639 | 659 | return NULL; |
|
640 | 660 | |
|
641 | 661 | idx = PyObject_New(indexObject, &indexType); |
|
642 | 662 | |
|
643 | 663 | if (idx == NULL) |
|
644 | 664 | goto bail; |
|
645 | 665 | |
|
646 | 666 | ret = index_real_init(idx, data, size, inlined_obj, |
|
647 | 667 | PyTuple_GET_ITEM(args, 0)); |
|
648 | 668 | if (ret) |
|
649 | 669 | goto bail; |
|
650 | 670 | |
|
651 | 671 | if (idx->inlined) { |
|
652 | 672 | Py_INCREF(idx->data); |
|
653 | 673 | cache = Py_BuildValue("iO", 0, idx->data); |
|
654 | 674 | if (cache == NULL) |
|
655 | 675 | goto bail; |
|
656 | 676 | } else { |
|
657 | 677 | cache = Py_None; |
|
658 | 678 | Py_INCREF(cache); |
|
659 | 679 | } |
|
660 | 680 | |
|
661 | 681 | tuple = Py_BuildValue("NN", idx, cache); |
|
662 | 682 | if (!tuple) |
|
663 | 683 | goto bail; |
|
664 | 684 | return tuple; |
|
665 | 685 | |
|
666 | 686 | bail: |
|
667 | 687 | Py_XDECREF(idx); |
|
668 | 688 | Py_XDECREF(cache); |
|
669 | 689 | Py_XDECREF(tuple); |
|
670 | 690 | return NULL; |
|
671 | 691 | } |
|
672 | 692 | |
|
673 | 693 | static char parsers_doc[] = "Efficient content parsing."; |
|
674 | 694 | |
|
675 | 695 | static PyMethodDef methods[] = { |
|
676 | 696 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
677 | 697 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
678 | 698 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
679 | 699 | {NULL, NULL} |
|
680 | 700 | }; |
|
681 | 701 | |
|
682 | 702 | static void module_init(PyObject *mod) |
|
683 | 703 | { |
|
684 | 704 | static const char nullid[20]; |
|
685 | 705 | |
|
686 | 706 | if (PyType_Ready(&indexType) < 0) |
|
687 | 707 | return; |
|
688 | 708 | Py_INCREF(&indexType); |
|
689 | 709 | |
|
690 | 710 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); |
|
691 | 711 | |
|
692 | 712 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, |
|
693 | 713 | -1, -1, -1, -1, nullid, 20); |
|
694 | 714 | if (nullentry) |
|
695 | 715 | PyObject_GC_UnTrack(nullentry); |
|
696 | 716 | } |
|
697 | 717 | |
|
698 | 718 | #ifdef IS_PY3K |
|
699 | 719 | static struct PyModuleDef parsers_module = { |
|
700 | 720 | PyModuleDef_HEAD_INIT, |
|
701 | 721 | "parsers", |
|
702 | 722 | parsers_doc, |
|
703 | 723 | -1, |
|
704 | 724 | methods |
|
705 | 725 | }; |
|
706 | 726 | |
|
707 | 727 | PyMODINIT_FUNC PyInit_parsers(void) |
|
708 | 728 | { |
|
709 | 729 | PyObject *mod = PyModule_Create(&parsers_module); |
|
710 | 730 | module_init(mod); |
|
711 | 731 | return mod; |
|
712 | 732 | } |
|
713 | 733 | #else |
|
714 | 734 | PyMODINIT_FUNC initparsers(void) |
|
715 | 735 | { |
|
716 | 736 | PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
717 | 737 | module_init(mod); |
|
718 | 738 | } |
|
719 | 739 | #endif |
|
720 | 740 |
General Comments 0
You need to be logged in to leave comments.
Login now