Show More
@@ -1,433 +1,455 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 | #include "util.h" | |
|
15 | ||
|
14 | 16 | static int hexdigit(char c) |
|
15 | 17 | { |
|
16 | 18 | if (c >= '0' && c <= '9') |
|
17 | 19 | return c - '0'; |
|
18 | 20 | if (c >= 'a' && c <= 'f') |
|
19 | 21 | return c - 'a' + 10; |
|
20 | 22 | if (c >= 'A' && c <= 'F') |
|
21 | 23 | return c - 'A' + 10; |
|
22 | 24 | |
|
23 | 25 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
24 | 26 | return 0; |
|
25 | 27 | } |
|
26 | 28 | |
|
27 | 29 | /* |
|
28 | 30 | * Turn a hex-encoded string into binary. |
|
29 | 31 | */ |
|
30 | 32 | static PyObject *unhexlify(const char *str, int len) |
|
31 | 33 | { |
|
32 | 34 | PyObject *ret; |
|
33 | 35 | const char *c; |
|
34 | 36 | char *d; |
|
35 | 37 | |
|
36 |
ret = Py |
|
|
38 | ret = PyBytes_FromStringAndSize(NULL, len / 2); | |
|
39 | ||
|
37 | 40 | if (!ret) |
|
38 | 41 | return NULL; |
|
39 | 42 | |
|
40 |
d = PyString |
|
|
43 | d = PyBytes_AsString(ret); | |
|
44 | ||
|
41 | 45 | for (c = str; c < str + len;) { |
|
42 | 46 | int hi = hexdigit(*c++); |
|
43 | 47 | int lo = hexdigit(*c++); |
|
44 | 48 | *d++ = (hi << 4) | lo; |
|
45 | 49 | } |
|
46 | 50 | |
|
47 | 51 | return ret; |
|
48 | 52 | } |
|
49 | 53 | |
|
50 | 54 | /* |
|
51 | 55 | * This code assumes that a manifest is stitched together with newline |
|
52 | 56 | * ('\n') characters. |
|
53 | 57 | */ |
|
54 | 58 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
55 | 59 | { |
|
56 | 60 | PyObject *mfdict, *fdict; |
|
57 | 61 | char *str, *cur, *start, *zero; |
|
58 | 62 | int len; |
|
59 | 63 | |
|
60 | 64 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
61 | 65 | &PyDict_Type, &mfdict, |
|
62 | 66 | &PyDict_Type, &fdict, |
|
63 | 67 | &str, &len)) |
|
64 | 68 | goto quit; |
|
65 | 69 | |
|
66 | 70 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { |
|
67 | 71 | PyObject *file = NULL, *node = NULL; |
|
68 | 72 | PyObject *flags = NULL; |
|
69 | 73 | int nlen; |
|
70 | 74 | |
|
71 | 75 | if (!*cur) { |
|
72 | 76 | zero = cur; |
|
73 | 77 | continue; |
|
74 | 78 | } |
|
75 | 79 | else if (*cur != '\n') |
|
76 | 80 | continue; |
|
77 | 81 | |
|
78 | 82 | if (!zero) { |
|
79 | 83 | PyErr_SetString(PyExc_ValueError, |
|
80 | 84 | "manifest entry has no separator"); |
|
81 | 85 | goto quit; |
|
82 | 86 | } |
|
83 | 87 | |
|
84 |
file = Py |
|
|
88 | file = PyBytes_FromStringAndSize(start, zero - start); | |
|
89 | ||
|
85 | 90 | if (!file) |
|
86 | 91 | goto bail; |
|
87 | 92 | |
|
88 | 93 | nlen = cur - zero - 1; |
|
89 | 94 | |
|
90 | 95 | node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen); |
|
91 | 96 | if (!node) |
|
92 | 97 | goto bail; |
|
93 | 98 | |
|
94 | 99 | if (nlen > 40) { |
|
95 |
flags = Py |
|
|
100 | flags = PyBytes_FromStringAndSize(zero + 41, | |
|
96 | 101 | nlen - 40); |
|
97 | 102 | if (!flags) |
|
98 | 103 | goto bail; |
|
99 | 104 | |
|
100 | 105 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
101 | 106 | goto bail; |
|
102 | 107 | } |
|
103 | 108 | |
|
104 | 109 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
105 | 110 | goto bail; |
|
106 | 111 | |
|
107 | 112 | start = cur + 1; |
|
108 | 113 | zero = NULL; |
|
109 | 114 | |
|
110 | 115 | Py_XDECREF(flags); |
|
111 | 116 | Py_XDECREF(node); |
|
112 | 117 | Py_XDECREF(file); |
|
113 | 118 | continue; |
|
114 | 119 | bail: |
|
115 | 120 | Py_XDECREF(flags); |
|
116 | 121 | Py_XDECREF(node); |
|
117 | 122 | Py_XDECREF(file); |
|
118 | 123 | goto quit; |
|
119 | 124 | } |
|
120 | 125 | |
|
121 | 126 | if (len > 0 && *(cur - 1) != '\n') { |
|
122 | 127 | PyErr_SetString(PyExc_ValueError, |
|
123 | 128 | "manifest contains trailing garbage"); |
|
124 | 129 | goto quit; |
|
125 | 130 | } |
|
126 | 131 | |
|
127 | 132 | Py_INCREF(Py_None); |
|
128 | 133 | return Py_None; |
|
129 | 134 | quit: |
|
130 | 135 | return NULL; |
|
131 | 136 | } |
|
132 | 137 | |
|
133 | 138 | #ifdef _WIN32 |
|
134 | 139 | #ifdef _MSC_VER |
|
135 | 140 | /* msvc 6.0 has problems */ |
|
136 | 141 | #define inline __inline |
|
137 | 142 | typedef unsigned long uint32_t; |
|
138 | 143 | typedef unsigned __int64 uint64_t; |
|
139 | 144 | #else |
|
140 | 145 | #include <stdint.h> |
|
141 | 146 | #endif |
|
142 | 147 | static uint32_t ntohl(uint32_t x) |
|
143 | 148 | { |
|
144 | 149 | return ((x & 0x000000ffUL) << 24) | |
|
145 | 150 | ((x & 0x0000ff00UL) << 8) | |
|
146 | 151 | ((x & 0x00ff0000UL) >> 8) | |
|
147 | 152 | ((x & 0xff000000UL) >> 24); |
|
148 | 153 | } |
|
149 | 154 | #else |
|
150 | 155 | /* not windows */ |
|
151 | 156 | #include <sys/types.h> |
|
152 | 157 | #if defined __BEOS__ && !defined __HAIKU__ |
|
153 | 158 | #include <ByteOrder.h> |
|
154 | 159 | #else |
|
155 | 160 | #include <arpa/inet.h> |
|
156 | 161 | #endif |
|
157 | 162 | #include <inttypes.h> |
|
158 | 163 | #endif |
|
159 | 164 | |
|
160 | 165 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
161 | 166 | { |
|
162 | 167 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
163 | 168 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
164 | 169 | char *str, *cur, *end, *cpos; |
|
165 | 170 | int state, mode, size, mtime; |
|
166 | 171 | unsigned int flen; |
|
167 | 172 | int len; |
|
168 | 173 | char decode[16]; /* for alignment */ |
|
169 | 174 | |
|
170 | 175 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
171 | 176 | &PyDict_Type, &dmap, |
|
172 | 177 | &PyDict_Type, &cmap, |
|
173 | 178 | &str, &len)) |
|
174 | 179 | goto quit; |
|
175 | 180 | |
|
176 | 181 | /* read parents */ |
|
177 | 182 | if (len < 40) |
|
178 | 183 | goto quit; |
|
179 | 184 | |
|
180 | 185 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
181 | 186 | if (!parents) |
|
182 | 187 | goto quit; |
|
183 | 188 | |
|
184 | 189 | /* read filenames */ |
|
185 | 190 | cur = str + 40; |
|
186 | 191 | end = str + len; |
|
187 | 192 | |
|
188 | 193 | while (cur < end - 17) { |
|
189 | 194 | /* unpack header */ |
|
190 | 195 | state = *cur; |
|
191 | 196 | memcpy(decode, cur + 1, 16); |
|
192 | 197 | mode = ntohl(*(uint32_t *)(decode)); |
|
193 | 198 | size = ntohl(*(uint32_t *)(decode + 4)); |
|
194 | 199 | mtime = ntohl(*(uint32_t *)(decode + 8)); |
|
195 | 200 | flen = ntohl(*(uint32_t *)(decode + 12)); |
|
196 | 201 | cur += 17; |
|
197 | 202 | if (cur + flen > end || cur + flen < cur) { |
|
198 | 203 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); |
|
199 | 204 | goto quit; |
|
200 | 205 | } |
|
201 | 206 | |
|
202 | 207 | entry = Py_BuildValue("ciii", state, mode, size, mtime); |
|
203 | 208 | if (!entry) |
|
204 | 209 | goto quit; |
|
205 | 210 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
206 | 211 | |
|
207 | 212 | cpos = memchr(cur, 0, flen); |
|
208 | 213 | if (cpos) { |
|
209 |
fname = Py |
|
|
210 |
cname = Py |
|
|
214 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); | |
|
215 | cname = PyBytes_FromStringAndSize(cpos + 1, | |
|
211 | 216 | flen - (cpos - cur) - 1); |
|
212 | 217 | if (!fname || !cname || |
|
213 | 218 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
214 | 219 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
215 | 220 | goto quit; |
|
216 | 221 | Py_DECREF(cname); |
|
217 | 222 | } else { |
|
218 |
fname = Py |
|
|
223 | fname = PyBytes_FromStringAndSize(cur, flen); | |
|
219 | 224 | if (!fname || |
|
220 | 225 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
221 | 226 | goto quit; |
|
222 | 227 | } |
|
223 | 228 | cur += flen; |
|
224 | 229 | Py_DECREF(fname); |
|
225 | 230 | Py_DECREF(entry); |
|
226 | 231 | fname = cname = entry = NULL; |
|
227 | 232 | } |
|
228 | 233 | |
|
229 | 234 | ret = parents; |
|
230 | 235 | Py_INCREF(ret); |
|
231 | 236 | quit: |
|
232 | 237 | Py_XDECREF(fname); |
|
233 | 238 | Py_XDECREF(cname); |
|
234 | 239 | Py_XDECREF(entry); |
|
235 | 240 | Py_XDECREF(parents); |
|
236 | 241 | return ret; |
|
237 | 242 | } |
|
238 | 243 | |
|
239 | 244 | const char nullid[20]; |
|
240 | 245 | const int nullrev = -1; |
|
241 | 246 | |
|
242 | 247 | /* create an index tuple, insert into the nodemap */ |
|
243 | 248 | static PyObject * _build_idx_entry(PyObject *nodemap, int n, uint64_t offset_flags, |
|
244 | 249 | int comp_len, int uncomp_len, int base_rev, |
|
245 | 250 | int link_rev, int parent_1, int parent_2, |
|
246 | 251 | const char *c_node_id) |
|
247 | 252 | { |
|
248 | 253 | int err; |
|
249 | 254 | PyObject *entry, *node_id, *n_obj; |
|
250 | 255 | |
|
251 |
node_id = Py |
|
|
256 | node_id = PyBytes_FromStringAndSize(c_node_id, 20); | |
|
252 | 257 | n_obj = PyInt_FromLong(n); |
|
258 | ||
|
253 | 259 | if (!node_id || !n_obj) |
|
254 | 260 | err = -1; |
|
255 | 261 | else |
|
256 | 262 | err = PyDict_SetItem(nodemap, node_id, n_obj); |
|
257 | 263 | |
|
258 | 264 | Py_XDECREF(n_obj); |
|
259 | 265 | if (err) |
|
260 | 266 | goto error_dealloc; |
|
261 | 267 | |
|
262 | 268 | entry = Py_BuildValue("LiiiiiiN", offset_flags, comp_len, |
|
263 | 269 | uncomp_len, base_rev, link_rev, |
|
264 | 270 | parent_1, parent_2, node_id); |
|
265 | 271 | if (!entry) |
|
266 | 272 | goto error_dealloc; |
|
267 | 273 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
268 | 274 | |
|
269 | 275 | return entry; |
|
270 | 276 | |
|
271 | 277 | error_dealloc: |
|
272 | 278 | Py_XDECREF(node_id); |
|
273 | 279 | return NULL; |
|
274 | 280 | } |
|
275 | 281 | |
|
276 | 282 | /* RevlogNG format (all in big endian, data may be inlined): |
|
277 | 283 | * 6 bytes: offset |
|
278 | 284 | * 2 bytes: flags |
|
279 | 285 | * 4 bytes: compressed length |
|
280 | 286 | * 4 bytes: uncompressed length |
|
281 | 287 | * 4 bytes: base revision |
|
282 | 288 | * 4 bytes: link revision |
|
283 | 289 | * 4 bytes: parent 1 revision |
|
284 | 290 | * 4 bytes: parent 2 revision |
|
285 | 291 | * 32 bytes: nodeid (only 20 bytes used) |
|
286 | 292 | */ |
|
287 | 293 | static int _parse_index_ng (const char *data, int size, int inlined, |
|
288 | 294 | PyObject *index, PyObject *nodemap) |
|
289 | 295 | { |
|
290 | 296 | PyObject *entry; |
|
291 | 297 | int n = 0, err; |
|
292 | 298 | uint64_t offset_flags; |
|
293 | 299 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
294 | 300 | const char *c_node_id; |
|
295 | 301 | const char *end = data + size; |
|
296 | 302 | char decode[64]; /* to enforce alignment with inline data */ |
|
297 | 303 | |
|
298 | 304 | while (data < end) { |
|
299 | 305 | unsigned int step; |
|
300 | 306 | |
|
301 | 307 | memcpy(decode, data, 64); |
|
302 | 308 | offset_flags = ntohl(*((uint32_t *) (decode + 4))); |
|
303 | 309 | if (n == 0) /* mask out version number for the first entry */ |
|
304 | 310 | offset_flags &= 0xFFFF; |
|
305 | 311 | else { |
|
306 | 312 | uint32_t offset_high = ntohl(*((uint32_t *)decode)); |
|
307 | 313 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
308 | 314 | } |
|
309 | 315 | |
|
310 | 316 | comp_len = ntohl(*((uint32_t *)(decode + 8))); |
|
311 | 317 | uncomp_len = ntohl(*((uint32_t *)(decode + 12))); |
|
312 | 318 | base_rev = ntohl(*((uint32_t *)(decode + 16))); |
|
313 | 319 | link_rev = ntohl(*((uint32_t *)(decode + 20))); |
|
314 | 320 | parent_1 = ntohl(*((uint32_t *)(decode + 24))); |
|
315 | 321 | parent_2 = ntohl(*((uint32_t *)(decode + 28))); |
|
316 | 322 | c_node_id = decode + 32; |
|
317 | 323 | |
|
318 | 324 | entry = _build_idx_entry(nodemap, n, offset_flags, |
|
319 | 325 | comp_len, uncomp_len, base_rev, |
|
320 | 326 | link_rev, parent_1, parent_2, |
|
321 | 327 | c_node_id); |
|
322 | 328 | if (!entry) |
|
323 | 329 | return 0; |
|
324 | 330 | |
|
325 | 331 | if (inlined) { |
|
326 | 332 | err = PyList_Append(index, entry); |
|
327 | 333 | Py_DECREF(entry); |
|
328 | 334 | if (err) |
|
329 | 335 | return 0; |
|
330 | 336 | } else |
|
331 | 337 | PyList_SET_ITEM(index, n, entry); /* steals reference */ |
|
332 | 338 | |
|
333 | 339 | n++; |
|
334 | 340 | step = 64 + (inlined ? comp_len : 0); |
|
335 | 341 | if (data + step > end || data + step < data) |
|
336 | 342 | break; |
|
337 | 343 | data += step; |
|
338 | 344 | } |
|
339 | 345 | if (data != end) { |
|
340 | 346 | if (!PyErr_Occurred()) |
|
341 | 347 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
342 | 348 | return 0; |
|
343 | 349 | } |
|
344 | 350 | |
|
345 | 351 | /* create the nullid/nullrev entry in the nodemap and the |
|
346 | 352 | * magic nullid entry in the index at [-1] */ |
|
347 | 353 | entry = _build_idx_entry(nodemap, |
|
348 | 354 | nullrev, 0, 0, 0, -1, -1, -1, -1, nullid); |
|
349 | 355 | if (!entry) |
|
350 | 356 | return 0; |
|
351 | 357 | if (inlined) { |
|
352 | 358 | err = PyList_Append(index, entry); |
|
353 | 359 | Py_DECREF(entry); |
|
354 | 360 | if (err) |
|
355 | 361 | return 0; |
|
356 | 362 | } else |
|
357 | 363 | PyList_SET_ITEM(index, n, entry); /* steals reference */ |
|
358 | 364 | |
|
359 | 365 | return 1; |
|
360 | 366 | } |
|
361 | 367 | |
|
362 | 368 | /* This function parses a index file and returns a Python tuple of the |
|
363 | 369 | * following format: (index, nodemap, cache) |
|
364 | 370 | * |
|
365 | 371 | * index: a list of tuples containing the RevlogNG records |
|
366 | 372 | * nodemap: a dict mapping node ids to indices in the index list |
|
367 | 373 | * cache: if data is inlined, a tuple (index_file_content, 0) else None |
|
368 | 374 | */ |
|
369 | 375 | static PyObject *parse_index(PyObject *self, PyObject *args) |
|
370 | 376 | { |
|
371 | 377 | const char *data; |
|
372 | 378 | int size, inlined; |
|
373 | 379 | PyObject *rval = NULL, *index = NULL, *nodemap = NULL, *cache = NULL; |
|
374 | 380 | PyObject *data_obj = NULL, *inlined_obj; |
|
375 | 381 | |
|
376 | 382 | if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj)) |
|
377 | 383 | return NULL; |
|
378 | 384 | inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
379 | 385 | |
|
380 | 386 | /* If no data is inlined, we know the size of the index list in |
|
381 | 387 | * advance: size divided by size of one one revlog record (64 bytes) |
|
382 | 388 | * plus one for the nullid */ |
|
383 | 389 | index = inlined ? PyList_New(0) : PyList_New(size / 64 + 1); |
|
384 | 390 | if (!index) |
|
385 | 391 | goto quit; |
|
386 | 392 | |
|
387 | 393 | nodemap = PyDict_New(); |
|
388 | 394 | if (!nodemap) |
|
389 | 395 | goto quit; |
|
390 | 396 | |
|
391 | 397 | /* set up the cache return value */ |
|
392 | 398 | if (inlined) { |
|
393 | 399 | /* Note that the reference to data_obj is only borrowed */ |
|
394 | 400 | data_obj = PyTuple_GET_ITEM(args, 0); |
|
395 | 401 | cache = Py_BuildValue("iO", 0, data_obj); |
|
396 | 402 | if (!cache) |
|
397 | 403 | goto quit; |
|
398 | 404 | } else { |
|
399 | 405 | cache = Py_None; |
|
400 | 406 | Py_INCREF(Py_None); |
|
401 | 407 | } |
|
402 | 408 | |
|
403 | 409 | /* actually populate the index and the nodemap with data */ |
|
404 | 410 | if (!_parse_index_ng (data, size, inlined, index, nodemap)) |
|
405 | 411 | goto quit; |
|
406 | 412 | |
|
407 | 413 | rval = Py_BuildValue("NNN", index, nodemap, cache); |
|
408 | 414 | if (!rval) |
|
409 | 415 | goto quit; |
|
410 | 416 | return rval; |
|
411 | 417 | |
|
412 | 418 | quit: |
|
413 | 419 | Py_XDECREF(index); |
|
414 | 420 | Py_XDECREF(nodemap); |
|
415 | 421 | Py_XDECREF(cache); |
|
416 | 422 | Py_XDECREF(rval); |
|
417 | 423 | return NULL; |
|
418 | 424 | } |
|
419 | 425 | |
|
420 | 426 | |
|
421 | 427 | static char parsers_doc[] = "Efficient content parsing."; |
|
422 | 428 | |
|
423 | 429 | static PyMethodDef methods[] = { |
|
424 | 430 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
425 | 431 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
426 | 432 | {"parse_index", parse_index, METH_VARARGS, "parse a revlog index\n"}, |
|
427 | 433 | {NULL, NULL} |
|
428 | 434 | }; |
|
429 | 435 | |
|
436 | #ifdef IS_PY3K | |
|
437 | static struct PyModuleDef parsers_module = { | |
|
438 | PyModuleDef_HEAD_INIT, | |
|
439 | "parsers", | |
|
440 | parsers_doc, | |
|
441 | -1, | |
|
442 | methods | |
|
443 | }; | |
|
444 | ||
|
445 | PyMODINIT_FUNC PyInit_parsers(void) | |
|
446 | { | |
|
447 | return PyModule_Create(&parsers_module); | |
|
448 | } | |
|
449 | #else | |
|
430 | 450 | PyMODINIT_FUNC initparsers(void) |
|
431 | 451 | { |
|
432 | 452 | Py_InitModule3("parsers", methods, parsers_doc); |
|
433 | 453 | } |
|
454 | #endif | |
|
455 |
General Comments 0
You need to be logged in to leave comments.
Login now