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