1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
cdef extern from "python-compat.h":
23
ctypedef unsigned int size_t
24
int memcmp(void *, void*, size_t)
25
void memcpy(void *, void*, size_t)
26
void *memchr(void *s, int c, size_t len)
27
long strtol(char *, char **, int)
28
void sprintf(char *, char *, ...)
30
cdef extern from "Python.h":
31
ctypedef int Py_ssize_t # Required for older pyrex versions
34
ctypedef _PyObject PyObject
35
int PyTuple_CheckExact(object p)
36
Py_ssize_t PyTuple_GET_SIZE(object t)
37
int PyString_CheckExact(object)
38
char *PyString_AS_STRING(object s)
39
Py_ssize_t PyString_GET_SIZE(object)
41
int PyDict_SetItem(object d, object k, object v) except -1
43
object PyTuple_New(Py_ssize_t count)
44
void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
46
void Py_INCREF(object)
48
PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
50
int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
51
Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
52
char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
53
object PyString_FromStringAndSize(char*, Py_ssize_t)
55
cdef extern from "zlib.h":
56
ctypedef unsigned long uLong
57
ctypedef unsigned int uInt
58
ctypedef unsigned char Bytef
60
uLong crc32(uLong crc, Bytef *buf, uInt len)
67
# We shouldn't just copy this from _dirstate_helpers_pyx
68
cdef void* _my_memrchr(void *s, int c, size_t n):
69
# memrchr seems to be a GNU extension, so we have to implement it ourselves
82
def _search_key_16(key):
83
"""See chk_map._search_key_16."""
84
cdef Py_ssize_t num_bits
86
cdef Py_ssize_t num_out_bytes
90
cdef Py_ssize_t out_off
94
if not PyTuple_CheckExact(key):
95
raise TypeError('key %r is not a tuple' % (key,))
96
num_bits = PyTuple_GET_SIZE(key)
97
# 4 bytes per crc32, and another 1 byte between bits
98
num_out_bytes = (9 * num_bits) - 1
99
out = PyString_FromStringAndSize(NULL, num_out_bytes)
100
c_out = PyString_AS_STRING(out)
101
for i from 0 <= i < num_bits:
105
# We use the _ptr variant, because GET_ITEM returns a borrowed
106
# reference, and Pyrex assumes that returned 'object' are a new
108
bit = PyTuple_GET_ITEM_ptr(key, i)
109
if not PyString_CheckExact_ptr(bit):
110
raise TypeError('Bit %d of %r is not a string' % (i, key))
111
c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
112
c_len = PyString_GET_SIZE_ptr(bit)
113
crc_val = crc32(0, c_bit, c_len)
115
sprintf(c_out, '%08X', crc_val)
120
def _search_key_255(key):
121
"""See chk_map._search_key_255."""
122
cdef Py_ssize_t num_bits
124
cdef Py_ssize_t num_out_bytes
128
cdef Py_ssize_t out_off
132
if not PyTuple_CheckExact(key):
133
raise TypeError('key %r is not a tuple' % (key,))
134
num_bits = PyTuple_GET_SIZE(key)
135
# 4 bytes per crc32, and another 1 byte between bits
136
num_out_bytes = (5 * num_bits) - 1
137
out = PyString_FromStringAndSize(NULL, num_out_bytes)
138
c_out = PyString_AS_STRING(out)
139
for i from 0 <= i < num_bits:
143
bit = PyTuple_GET_ITEM_ptr(key, i)
144
if not PyString_CheckExact_ptr(bit):
145
raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
147
c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
148
c_len = PyString_GET_SIZE_ptr(bit)
149
crc_val = crc32(0, c_bit, c_len)
151
c_out[0] = (crc_val >> 24) & 0xFF
152
c_out[1] = (crc_val >> 16) & 0xFF
153
c_out[2] = (crc_val >> 8) & 0xFF
154
c_out[3] = (crc_val >> 0) & 0xFF
155
for j from 0 <= j < 4:
156
if c_out[j] == c'\n':
162
cdef int _get_int_from_line(char **cur, char *end, char *message) except -1:
163
"""Read a positive integer from the data stream.
165
:param cur: The start of the data, this will be moved to after the
166
trailing newline when done.
167
:param end: Do not parse any data past this byte.
168
:return: The integer stored in those bytes
171
cdef char *next_line, *next
173
next_line = <char *>memchr(cur[0], c'\n', end - cur[0])
174
if next_line == NULL:
175
raise ValueError("Missing %s line\n" % message)
177
value = strtol(cur[0], &next, 10)
178
if next != next_line:
179
raise ValueError("%s line not a proper int\n" % message)
180
cur[0] = next_line + 1
184
def _deserialise_leaf_node(bytes, key, search_key_func=None):
185
"""Deserialise bytes, with key key, into a LeafNode.
187
:param bytes: The bytes of the node.
188
:param key: The key that the serialised node has.
190
cdef char *c_bytes, *cur, *next, *end
192
cdef Py_ssize_t c_bytes_len, prefix_length, items_length
193
cdef int maximum_size, width, length, i, prefix_tail_len
194
cdef int num_value_lines, num_prefix_bits
195
cdef char *prefix, *value_start, *prefix_tail
196
cdef char *next_null, *last_null, *line_start
197
cdef char *c_entry, *entry_start
199
if _LeafNode is None:
200
from bzrlib import chk_map
201
_LeafNode = chk_map.LeafNode
202
_InternalNode = chk_map.InternalNode
203
_unknown = chk_map._unknown
205
result = _LeafNode(search_key_func=search_key_func)
206
# Splitlines can split on '\r' so don't use it, split('\n') adds an
207
# extra '' if the bytes ends in a final newline.
208
if not PyString_CheckExact(bytes):
209
raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
211
c_bytes = PyString_AS_STRING(bytes)
212
c_bytes_len = PyString_GET_SIZE(bytes)
214
if c_bytes_len < 9 or memcmp(c_bytes, "chkleaf:\n", 9) != 0:
215
raise ValueError("not a serialised leaf node: %r" % bytes)
216
if c_bytes[c_bytes_len - 1] != c'\n':
217
raise ValueError("bytes does not end in a newline")
219
end = c_bytes + c_bytes_len
221
maximum_size = _get_int_from_line(&cur, end, "maximum_size")
222
width = _get_int_from_line(&cur, end, "width")
223
length = _get_int_from_line(&cur, end, "length")
225
next_line = <char *>memchr(cur, c'\n', end - cur)
226
if next_line == NULL:
227
raise ValueError('Missing the prefix line\n')
229
prefix_length = next_line - cur
235
next_null = <char *>memchr(prefix, c'\0', prefix_length)
236
while next_null != NULL:
237
num_prefix_bits = num_prefix_bits + 1
239
PyString_FromStringAndSize(prefix_tail, next_null - prefix_tail))
240
prefix_tail = next_null + 1
241
next_null = <char *>memchr(prefix_tail, c'\0', next_line - prefix_tail)
242
prefix_tail_len = next_line - prefix_tail
244
if num_prefix_bits >= width:
245
raise ValueError('Prefix has too many nulls versus width')
247
items_length = end - cur
251
next_line = <char *>memchr(cur, c'\n', end - cur)
252
if next_line == NULL:
253
raise ValueError('null line\n')
254
last_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
255
if last_null == NULL:
256
raise ValueError('fail to find the num value lines null')
257
next_null = last_null + 1 # move past NULL
258
num_value_lines = _get_int_from_line(&next_null, next_line + 1,
262
# Walk num_value_lines forward
263
for i from 0 <= i < num_value_lines:
264
next_line = <char *>memchr(cur, c'\n', end - cur)
265
if next_line == NULL:
266
raise ValueError('missing trailing newline')
268
entry_bits = PyTuple_New(width)
269
for i from 0 <= i < num_prefix_bits:
270
entry = prefix_bits[i]
271
# SET_ITEM 'steals' a reference
273
PyTuple_SET_ITEM(entry_bits, i, entry)
274
value = PyString_FromStringAndSize(value_start, next_line - value_start)
275
# The next entry bit needs the 'tail' from the prefix, and first part
277
entry_start = line_start
278
next_null = <char *>memchr(entry_start, c'\0',
279
last_null - entry_start + 1)
280
if next_null == NULL:
281
raise ValueError('bad no null, bad')
282
entry = PyString_FromStringAndSize(NULL,
283
prefix_tail_len + next_null - line_start)
284
c_entry = PyString_AS_STRING(entry)
285
if prefix_tail_len > 0:
286
memcpy(c_entry, prefix_tail, prefix_tail_len)
287
if next_null - line_start > 0:
288
memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
291
PyTuple_SET_ITEM(entry_bits, i, entry)
292
while next_null != last_null: # We have remaining bits
295
raise ValueError("Too many bits for entry")
296
entry_start = next_null + 1
297
next_null = <char *>memchr(entry_start, c'\0',
298
last_null - entry_start + 1)
299
if next_null == NULL:
300
raise ValueError('bad no null')
301
entry = PyString_FromStringAndSize(entry_start,
302
next_null - entry_start)
304
PyTuple_SET_ITEM(entry_bits, i, entry)
305
if len(entry_bits) != width:
306
raise AssertionError(
307
'Incorrect number of elements (%d vs %d)'
308
% (len(entry_bits)+1, width + 1))
309
PyDict_SetItem(items, entry_bits, value)
310
if len(items) != length:
311
raise ValueError("item count (%d) mismatch for key %s,"
312
" bytes %r" % (length, entry_bits, bytes))
313
result._items = items
315
result._maximum_size = maximum_size
317
result._key_width = width
318
result._raw_size = items_length + length * prefix_length
320
result._search_prefix = None
321
result._common_serialised_prefix = None
323
result._search_prefix = _unknown
324
result._common_serialised_prefix = PyString_FromStringAndSize(prefix,
326
if c_bytes_len != result._current_size():
327
raise AssertionError('_current_size computed incorrectly %d != %d',
328
c_bytes_len, result._current_size())
332
def _deserialise_internal_node(bytes, key, search_key_func=None):
333
cdef char *c_bytes, *cur, *next, *end
335
cdef Py_ssize_t c_bytes_len, prefix_length
336
cdef int maximum_size, width, length, i, prefix_tail_len
337
cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
339
if _InternalNode is None:
340
from bzrlib import chk_map
341
_LeafNode = chk_map.LeafNode
342
_InternalNode = chk_map.InternalNode
343
_unknown = chk_map._unknown
344
result = _InternalNode(search_key_func=search_key_func)
346
if not PyString_CheckExact(bytes):
347
raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
349
c_bytes = PyString_AS_STRING(bytes)
350
c_bytes_len = PyString_GET_SIZE(bytes)
352
if c_bytes_len < 9 or memcmp(c_bytes, "chknode:\n", 9) != 0:
353
raise ValueError("not a serialised internal node: %r" % bytes)
354
if c_bytes[c_bytes_len - 1] != c'\n':
355
raise ValueError("bytes does not end in a newline")
359
end = c_bytes + c_bytes_len
360
maximum_size = _get_int_from_line(&cur, end, "maximum_size")
361
width = _get_int_from_line(&cur, end, "width")
362
length = _get_int_from_line(&cur, end, "length")
364
next_line = <char *>memchr(cur, c'\n', end - cur)
365
if next_line == NULL:
366
raise ValueError('Missing the prefix line\n')
368
prefix_length = next_line - cur
372
# Find the null separator
373
next_line = <char *>memchr(cur, c'\n', end - cur)
374
if next_line == NULL:
375
raise ValueError('missing trailing newline')
376
next_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
377
if next_null == NULL:
378
raise ValueError('bad no null')
379
item_prefix = PyString_FromStringAndSize(NULL,
380
prefix_length + next_null - cur)
381
c_item_prefix = PyString_AS_STRING(item_prefix)
383
memcpy(c_item_prefix, prefix, prefix_length)
384
memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
385
flat_key = PyString_FromStringAndSize(next_null + 1,
386
next_line - next_null - 1)
387
PyDict_SetItem(items, item_prefix, (flat_key,))
389
assert len(items) > 0
390
result._items = items
392
result._maximum_size = maximum_size
394
result._key_width = width
395
# XXX: InternalNodes don't really care about their size, and this will
396
# change if we add prefix compression
397
result._raw_size = None # len(bytes)
398
result._node_width = len(item_prefix)
399
result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)