~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_bencode_c.pyx

  • Committer: Jelmer Vernooij
  • Date: 2009-05-27 08:33:37 UTC
  • mto: (4398.5.1 bencode_serializer)
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: jelmer@samba.org-20090527083337-fij2klthhtsdl2hy
Fix copyright headers, add _bencode_py.py to the list of files that do not have to have canonical copyright.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
16
17
 
17
18
"""Pyrex implementation for bencode coder/decoder"""
18
19
 
30
31
    char *PyString_AS_STRING(object o) except NULL
31
32
    Py_ssize_t PyString_GET_SIZE(object o) except -1
32
33
    long PyInt_GetMax()
33
 
    object PyLong_FromString(char *str, char **pend, int base)
 
34
    object PyLong_FromLong(unsigned long)
34
35
 
35
36
cdef extern from "stddef.h":
36
37
    ctypedef unsigned int size_t
39
40
    void free(void *memblock)
40
41
    void *malloc(size_t size)
41
42
    void *realloc(void *memblock, size_t size)
 
43
    long int strtol(char *nptr, char **endptr, int base)
 
44
 
42
45
 
43
46
cdef extern from "string.h":
44
47
    void *memcpy(void *dest, void *src, size_t count)
 
48
    char *memchr(void *dest, int size, char c)
45
49
 
46
50
cdef extern from "python-compat.h":
47
51
    int snprintf(char* buffer, size_t nsize, char* fmt, ...)
116
120
 
117
121
    cdef int _decode_int_until(self, char stop_char) except? -1:
118
122
        """Decode int from stream until stop_char encountered"""
119
 
        cdef int result
120
 
        cdef int i, n
121
 
        cdef int sign
122
 
        cdef char digit
123
 
        cdef char *longstr
124
 
 
125
 
        for n from 0 <= n < self.size:
126
 
            if self.tail[n] == stop_char:
127
 
                break
128
 
        else:
129
 
            raise ValueError
130
 
 
131
 
        sign = 0
132
 
        if c'-' == self.tail[0]:
133
 
            sign = 1
134
 
 
135
 
        if n-sign == 0:
136
 
            raise ValueError    # ie / i-e
137
 
 
138
 
        if self.tail[sign] == c'0':   # special check for zero
139
 
            if sign:
140
 
                raise ValueError    # i-0e
141
 
            if n > 1:
142
 
                raise ValueError    # i00e / i01e
143
 
            self._update_tail(n+1)
144
 
            return 0
145
 
 
146
 
        if n-sign < self._MAXN:
147
 
            # plain int
148
 
            result = 0
149
 
            for i from sign <= i < n:
150
 
                digit = self.tail[i]
151
 
                if c'0' <= digit <= c'9':
152
 
                    result = result * 10 + (digit - c'0')
153
 
                else:
154
 
                    raise ValueError
155
 
            if sign:
156
 
                result = -result
157
 
            self._update_tail(n+1)
158
 
        else:
159
 
            # long int
160
 
            result = self._MAXINT
161
 
            longstr = <char*>malloc(n+1)
162
 
            if NULL == longstr:
163
 
                raise MemoryError 
164
 
            memcpy(longstr, self.tail, n)
165
 
            longstr[n] = 0
166
 
            self._longint = PyLong_FromString(longstr, NULL, 10)
167
 
            free(longstr)
168
 
            self._update_tail(n+1)
169
 
 
170
 
        return result
 
123
        cdef char *actual_tail, *expected_tail
 
124
        cdef int n
 
125
        expected_tail = memchr(self.tail, self.size, stop_char)
 
126
        if expected_tail == NULL:
 
127
            raise ValueError
 
128
        ret = PyLong_FromLong(strtol(self.tail, &actual_tail, 10))
 
129
        if actual_tail != expected_tail or actual_tail == self.tail:
 
130
            raise ValueError
 
131
        self._update_tail(actual_tail - self.tail)
 
132
        return ret
171
133
 
172
134
    cdef object _decode_string(self):
173
135
        cdef int n