~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chk_map_pyx.pyx

  • Committer: Aaron Bentley
  • Date: 2010-05-10 11:34:20 UTC
  • mfrom: (5218 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5221.
  • Revision ID: aaron@aaronbentley.com-20100510113420-toh2d5yioobb5uq1
Merged bzr.dev into transform-commit-full.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    int PyString_CheckExact(object)
37
37
    char *PyString_AS_STRING(object s)
38
38
    Py_ssize_t PyString_GET_SIZE(object)
 
39
    unsigned long PyInt_AsUnsignedLongMask(object) except? -1
39
40
 
40
41
    int PyDict_SetItem(object d, object k, object v) except -1
41
42
 
63
64
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
64
65
                                                                Py_ssize_t)
65
66
 
66
 
cdef extern from "zlib.h":
67
 
    ctypedef unsigned long uLong
68
 
    ctypedef unsigned int uInt
69
 
    ctypedef unsigned char Bytef
70
 
 
71
 
    uLong crc32(uLong crc, Bytef *buf, uInt len)
 
67
cdef object crc32
 
68
from zlib import crc32
72
69
 
73
70
 
74
71
# Set up the StaticTuple C_API functionality
101
98
    cdef Py_ssize_t num_bits
102
99
    cdef Py_ssize_t i, j
103
100
    cdef Py_ssize_t num_out_bytes
104
 
    cdef Bytef *c_bit
105
 
    cdef uLong c_len
106
 
    cdef uInt crc_val
 
101
    cdef unsigned long crc_val
107
102
    cdef Py_ssize_t out_off
108
103
    cdef char *c_out
109
 
    cdef PyObject *bit
110
104
 
111
 
    if not StaticTuple_CheckExact(key):
112
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
113
105
    num_bits = len(key)
114
106
    # 4 bytes per crc32, and another 1 byte between bits
115
107
    num_out_bytes = (9 * num_bits) - 1
119
111
        if i > 0:
120
112
            c_out[0] = c'\x00'
121
113
            c_out = c_out + 1
122
 
        # We use the _ptr variant, because GET_ITEM returns a borrowed
123
 
        # reference, and Pyrex assumes that returned 'object' are a new
124
 
        # reference
125
 
        bit = StaticTuple_GET_ITEM_ptr(key, i)
126
 
        if not PyString_CheckExact_ptr(bit):
127
 
            raise TypeError('Bit %d of %r is not a string' % (i, key))
128
 
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
129
 
        c_len = PyString_GET_SIZE_ptr(bit)
130
 
        crc_val = crc32(0, c_bit, c_len)
 
114
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
131
115
        # Hex(val) order
132
116
        sprintf(c_out, '%08X', crc_val)
133
117
        c_out = c_out + 8
139
123
    cdef Py_ssize_t num_bits
140
124
    cdef Py_ssize_t i, j
141
125
    cdef Py_ssize_t num_out_bytes
142
 
    cdef Bytef *c_bit
143
 
    cdef uLong c_len
144
 
    cdef uInt crc_val
 
126
    cdef unsigned long crc_val
145
127
    cdef Py_ssize_t out_off
146
128
    cdef char *c_out
147
 
    cdef PyObject *bit
148
129
 
149
 
    if not StaticTuple_CheckExact(key):
150
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
151
130
    num_bits = len(key)
152
131
    # 4 bytes per crc32, and another 1 byte between bits
153
132
    num_out_bytes = (5 * num_bits) - 1
157
136
        if i > 0:
158
137
            c_out[0] = c'\x00'
159
138
            c_out = c_out + 1
160
 
        bit = StaticTuple_GET_ITEM_ptr(key, i)
161
 
        if not PyString_CheckExact_ptr(bit):
162
 
            raise TypeError('Bit %d of %r is not a string: %r'
163
 
                            % (i, key, <object>bit))
164
 
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
165
 
        c_len = PyString_GET_SIZE_ptr(bit)
166
 
        crc_val = crc32(0, c_bit, c_len)
 
139
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
167
140
        # MSB order
168
141
        c_out[0] = (crc_val >> 24) & 0xFF
169
142
        c_out[1] = (crc_val >> 16) & 0xFF