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