rencode 1.0.3__tar.gz → 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: rencode
3
- Version: 1.0.3
3
+ Version: 1.0.4
4
4
  Summary: The rencode module is similar to bencode from the BitTorrent project. For
5
5
  complex, heterogeneous data structures with many small elements, r-encodings
6
6
  take up significantly less space than b-encodings. This version of rencode is
@@ -32,7 +32,7 @@ from cpython cimport bool
32
32
  from libc.stdlib cimport realloc, malloc, free
33
33
  from libc.string cimport memcpy
34
34
 
35
- __version__ = ("Cython", 1, 0, 3)
35
+ __version__ = ("Cython", 1, 0, 4)
36
36
 
37
37
  cdef long long data_length = 0
38
38
  cdef bool _decode_utf8 = False
@@ -149,17 +149,17 @@ cdef swap_byte_order_double(char *c):
149
149
  p[7] = c[0]
150
150
  return d
151
151
 
152
- cdef write_buffer_char(char **buf, int *pos, char c):
152
+ cdef write_buffer_char(char **buf, unsigned int *pos, char c):
153
153
  buf[0] = <char*>realloc(buf[0], pos[0] + 1)
154
154
  memcpy(&buf[0][pos[0]], &c, 1)
155
155
  pos[0] += 1
156
156
 
157
- cdef write_buffer(char **buf, int *pos, void* data, int size):
157
+ cdef write_buffer(char **buf, unsigned int *pos, void* data, int size):
158
158
  buf[0] = <char*>realloc(buf[0], pos[0] + size)
159
159
  memcpy(&buf[0][pos[0]], data, size)
160
160
  pos[0] += size
161
161
 
162
- cdef encode_char(char **buf, int *pos, signed char x):
162
+ cdef encode_char(char **buf, unsigned int *pos, signed char x):
163
163
  if 0 <= x < INT_POS_FIXED_COUNT:
164
164
  write_buffer_char(buf, pos, INT_POS_FIXED_START + x)
165
165
  elif -INT_NEG_FIXED_COUNT <= x < 0:
@@ -168,7 +168,7 @@ cdef encode_char(char **buf, int *pos, signed char x):
168
168
  write_buffer_char(buf, pos, CHR_INT1)
169
169
  write_buffer_char(buf, pos, x)
170
170
 
171
- cdef encode_short(char **buf, int *pos, short x):
171
+ cdef encode_short(char **buf, unsigned int *pos, short x):
172
172
  write_buffer_char(buf, pos, CHR_INT2)
173
173
  if not big_endian:
174
174
  if x > 0:
@@ -178,7 +178,7 @@ cdef encode_short(char **buf, int *pos, short x):
178
178
 
179
179
  write_buffer(buf, pos, &x, sizeof(x))
180
180
 
181
- cdef encode_int(char **buf, int *pos, int x):
181
+ cdef encode_int(char **buf, unsigned int *pos, int x):
182
182
  write_buffer_char(buf, pos, CHR_INT4)
183
183
  if not big_endian:
184
184
  if x > 0:
@@ -187,7 +187,7 @@ cdef encode_int(char **buf, int *pos, int x):
187
187
  x = swap_byte_order_int(<char*>&x)
188
188
  write_buffer(buf, pos, &x, sizeof(x))
189
189
 
190
- cdef encode_long_long(char **buf, int *pos, long long x):
190
+ cdef encode_long_long(char **buf, unsigned int *pos, long long x):
191
191
  write_buffer_char(buf, pos, CHR_INT8)
192
192
  if not big_endian:
193
193
  if x > 0:
@@ -196,24 +196,24 @@ cdef encode_long_long(char **buf, int *pos, long long x):
196
196
  x = swap_byte_order_long_long(<char*>&x)
197
197
  write_buffer(buf, pos, &x, sizeof(x))
198
198
 
199
- cdef encode_big_number(char **buf, int *pos, char *x):
199
+ cdef encode_big_number(char **buf, unsigned int *pos, char *x):
200
200
  write_buffer_char(buf, pos, CHR_INT)
201
201
  write_buffer(buf, pos, x, len(x))
202
202
  write_buffer_char(buf, pos, CHR_TERM)
203
203
 
204
- cdef encode_float32(char **buf, int *pos, float x):
204
+ cdef encode_float32(char **buf, unsigned int *pos, float x):
205
205
  write_buffer_char(buf, pos, CHR_FLOAT32)
206
206
  if not big_endian:
207
207
  x = swap_byte_order_float(<char *>&x)
208
208
  write_buffer(buf, pos, &x, sizeof(x))
209
209
 
210
- cdef encode_float64(char **buf, int *pos, double x):
210
+ cdef encode_float64(char **buf, unsigned int *pos, double x):
211
211
  write_buffer_char(buf, pos, CHR_FLOAT64)
212
212
  if not big_endian:
213
213
  x = swap_byte_order_double(<char *>&x)
214
214
  write_buffer(buf, pos, &x, sizeof(x))
215
215
 
216
- cdef encode_str(char **buf, int *pos, bytes x):
216
+ cdef encode_str(char **buf, unsigned int *pos, bytes x):
217
217
  cdef char *p
218
218
  cdef int lx = len(x)
219
219
  if lx < STR_FIXED_COUNT:
@@ -227,16 +227,16 @@ cdef encode_str(char **buf, int *pos, bytes x):
227
227
  write_buffer(buf, pos, p, len(s))
228
228
  write_buffer(buf, pos, <char *>x, lx)
229
229
 
230
- cdef encode_none(char **buf, int *pos):
230
+ cdef encode_none(char **buf, unsigned int *pos):
231
231
  write_buffer_char(buf, pos, CHR_NONE)
232
232
 
233
- cdef encode_bool(char **buf, int *pos, bool x):
233
+ cdef encode_bool(char **buf, unsigned int *pos, bool x):
234
234
  if x:
235
235
  write_buffer_char(buf, pos, CHR_TRUE)
236
236
  else:
237
237
  write_buffer_char(buf, pos, CHR_FALSE)
238
238
 
239
- cdef encode_list(char **buf, int *pos, x):
239
+ cdef encode_list(char **buf, unsigned int *pos, x):
240
240
  if len(x) < LIST_FIXED_COUNT:
241
241
  write_buffer_char(buf, pos, LIST_FIXED_START + len(x))
242
242
  for i in x:
@@ -247,7 +247,7 @@ cdef encode_list(char **buf, int *pos, x):
247
247
  encode(buf, pos, i)
248
248
  write_buffer_char(buf, pos, CHR_TERM)
249
249
 
250
- cdef encode_dict(char **buf, int *pos, x):
250
+ cdef encode_dict(char **buf, unsigned int *pos, x):
251
251
  if len(x) < DICT_FIXED_COUNT:
252
252
  write_buffer_char(buf, pos, DICT_FIXED_START + len(x))
253
253
  for k, v in x.items():
@@ -267,7 +267,7 @@ cdef object MIN_SIGNED_INT = -MAX_SIGNED_INT
267
267
  cdef object MAX_SIGNED_LONGLONG = int(2**63)
268
268
  cdef object MIN_SIGNED_LONGLONG = -MAX_SIGNED_LONGLONG
269
269
 
270
- cdef encode(char **buf, int *pos, data):
270
+ cdef encode(char **buf, unsigned int *pos, data):
271
271
  t = type(data)
272
272
  if t == int or t == long:
273
273
  if -128 <= data < 128:
@@ -326,55 +326,61 @@ def dumps(data, float_bits=DEFAULT_FLOAT_BITS):
326
326
  global _float_bits
327
327
  _float_bits = float_bits
328
328
  cdef char *buf = NULL
329
- cdef int pos = 0
329
+ cdef unsigned int pos = 0
330
330
  encode(&buf, &pos, data)
331
331
  ret = buf[:pos]
332
332
  free(buf)
333
333
  return ret
334
334
 
335
- cdef decode_char(char *data, int *pos):
335
+ cdef decode_char(char *data, unsigned int *pos):
336
336
  cdef signed char c
337
+ check_pos(data, pos[0]+1)
337
338
  memcpy(&c, &data[pos[0]+1], 1)
338
339
  pos[0] += 2
339
340
  return c
340
341
 
341
- cdef decode_short(char *data, int *pos):
342
+ cdef decode_short(char *data, unsigned int *pos):
342
343
  cdef short s
344
+ check_pos(data, pos[0]+2)
343
345
  memcpy(&s, &data[pos[0]+1], 2)
344
346
  pos[0] += 3
345
347
  if not big_endian:
346
348
  s = swap_byte_order_short(<char*>&s)
347
349
  return s
348
350
 
349
- cdef decode_int(char *data, int *pos):
351
+ cdef decode_int(char *data, unsigned int *pos):
350
352
  cdef int i
353
+ check_pos(data, pos[0]+4)
351
354
  memcpy(&i, &data[pos[0]+1], 4)
352
355
  pos[0] += 5
353
356
  if not big_endian:
354
357
  i = swap_byte_order_int(<char*>&i)
355
358
  return i
356
359
 
357
- cdef decode_long_long(char *data, int *pos):
360
+ cdef decode_long_long(char *data, unsigned int *pos):
358
361
  cdef long long l
362
+ check_pos(data, pos[0]+8)
359
363
  memcpy(&l, &data[pos[0]+1], 8)
360
364
  pos[0] += 9
361
365
  if not big_endian:
362
366
  l = swap_byte_order_long_long(<char*>&l)
363
367
  return l
364
368
 
365
- cdef decode_fixed_pos_int(char *data, int *pos):
369
+ cdef decode_fixed_pos_int(char *data, unsigned int *pos):
366
370
  pos[0] += 1
367
371
  return data[pos[0] - 1] - INT_POS_FIXED_START
368
372
 
369
- cdef decode_fixed_neg_int(char *data, int *pos):
373
+ cdef decode_fixed_neg_int(char *data, unsigned int *pos):
370
374
  pos[0] += 1
371
375
  return (data[pos[0] - 1] - INT_NEG_FIXED_START + 1)*-1
372
376
 
373
- cdef decode_big_number(char *data, int *pos):
377
+ cdef decode_big_number(char *data, unsigned int *pos):
374
378
  pos[0] += 1
375
379
  cdef int x = 18
380
+ check_pos(data, pos[0]+x)
376
381
  while (data[pos[0]+x] != CHR_TERM):
377
382
  x += 1
383
+ check_pos(data, pos[0]+x)
378
384
  cdef char *s = <char *>malloc(x)
379
385
  memcpy(s, &data[pos[0]], x)
380
386
  s[x] = '\0'
@@ -383,40 +389,46 @@ cdef decode_big_number(char *data, int *pos):
383
389
  free(s)
384
390
  return big_number
385
391
 
386
- cdef decode_float32(char *data, int *pos):
392
+ cdef decode_float32(char *data, unsigned int *pos):
387
393
  cdef float f
394
+ check_pos(data, pos[0]+4)
388
395
  memcpy(&f, &data[pos[0]+1], 4)
389
396
  pos[0] += 5
390
397
  if not big_endian:
391
398
  f = swap_byte_order_float(<char*>&f)
392
399
  return f
393
400
 
394
- cdef decode_float64(char *data, int *pos):
401
+ cdef decode_float64(char *data, unsigned int *pos):
395
402
  cdef double d
403
+ check_pos(data, pos[0]+8)
396
404
  memcpy(&d, &data[pos[0]+1], 8)
397
405
  pos[0] += 9
398
406
  if not big_endian:
399
407
  d = swap_byte_order_double(<char*>&d)
400
408
  return d
401
409
 
402
- cdef decode_fixed_str(char *data, int *pos):
410
+ cdef decode_fixed_str(char *data, unsigned int *pos):
403
411
  cdef unsigned char size = data[pos[0]] - STR_FIXED_START + 1
412
+ check_pos(data, pos[0] + size - 1)
404
413
  s = data[pos[0]+1:pos[0] + size]
405
414
  pos[0] += size
406
415
  return s
407
416
 
408
- cdef decode_str(char *data, int *pos):
409
- cdef int x = 1
417
+ cdef decode_str(char *data, unsigned int *pos):
418
+ cdef unsigned int x = 1
419
+ check_pos(data, pos[0]+x)
410
420
  while (data[pos[0]+x] != 58):
411
421
  x += 1
422
+ check_pos(data, pos[0]+x)
412
423
 
413
424
  cdef int size = int(data[pos[0]:pos[0]+x])
414
425
  pos[0] += x + 1
426
+ check_pos(data, pos[0] + size - 1)
415
427
  s = data[pos[0]:pos[0] + size]
416
428
  pos[0] += size
417
429
  return s
418
430
 
419
- cdef decode_fixed_list(char *data, int *pos):
431
+ cdef decode_fixed_list(char *data, unsigned int *pos):
420
432
  l = []
421
433
  size = <unsigned char>data[pos[0]] - LIST_FIXED_START
422
434
  pos[0] += 1
@@ -425,7 +437,7 @@ cdef decode_fixed_list(char *data, int *pos):
425
437
  l.append(decode(data, pos))
426
438
  return tuple(l)
427
439
 
428
- cdef decode_list(char *data, int *pos):
440
+ cdef decode_list(char *data, unsigned int *pos):
429
441
  l = []
430
442
  pos[0] += 1
431
443
  while data[pos[0]] != CHR_TERM:
@@ -433,7 +445,7 @@ cdef decode_list(char *data, int *pos):
433
445
  pos[0] += 1
434
446
  return tuple(l)
435
447
 
436
- cdef decode_fixed_dict(char *data, int *pos):
448
+ cdef decode_fixed_dict(char *data, unsigned int *pos):
437
449
  d = {}
438
450
  size = <unsigned char>data[pos[0]] - DICT_FIXED_START
439
451
  pos[0] += 1
@@ -444,9 +456,10 @@ cdef decode_fixed_dict(char *data, int *pos):
444
456
  d[key] = value
445
457
  return d
446
458
 
447
- cdef decode_dict(char *data, int *pos):
459
+ cdef decode_dict(char *data, unsigned int *pos):
448
460
  d = {}
449
461
  pos[0] += 1
462
+ check_pos(data, pos[0])
450
463
  while data[pos[0]] != CHR_TERM:
451
464
  key = decode(data, pos)
452
465
  value = decode(data, pos)
@@ -454,9 +467,13 @@ cdef decode_dict(char *data, int *pos):
454
467
  pos[0] += 1
455
468
  return d
456
469
 
457
- cdef decode(char *data, int *pos):
458
- if pos[0] > data_length:
459
- raise ValueError("Malformed rencoded string!")
470
+ cdef check_pos(char *data, unsigned int pos):
471
+ if pos >= data_length:
472
+ raise IndexError("Tried to access data[%d] but data len is: %d" % (pos, data_length))
473
+
474
+ cdef decode(char *data, unsigned int *pos):
475
+ if pos[0] >= data_length:
476
+ raise IndexError("Malformed rencoded string: data_length: %d pos: %d" % (data_length, pos[0]))
460
477
 
461
478
  cdef unsigned char typecode = data[pos[0]]
462
479
  if typecode == CHR_INT1:
@@ -516,7 +533,7 @@ def loads(data, decode_utf8=False):
516
533
  :type decode_utf8: bool
517
534
 
518
535
  """
519
- cdef int pos = 0
536
+ cdef unsigned int pos = 0
520
537
  global data_length
521
538
  data_length = len(data)
522
539
  global _decode_utf8
@@ -19,7 +19,7 @@ rencode module versions, so you should check that you are using the
19
19
  same rencode version throughout your project.
20
20
  """
21
21
 
22
- __version__ = ("Python", 1, 0, 3)
22
+ __version__ = ("Python", 1, 0, 4)
23
23
  __all__ = ['dumps', 'loads']
24
24
 
25
25
  # Original bencode module by Petru Paler, et al.
@@ -0,0 +1,12 @@
1
+ import rencode
2
+
3
+ for i in range(1000):
4
+ print("Attempt: ", i)
5
+ if i % 2:
6
+ f = open("/home/aresch/Downloads/rencode-loads-1444800853.32-False", "rb")
7
+ else:
8
+ f = open("/home/aresch/Downloads/rencode-loads-1444800853.96-False", "rb")
9
+ try:
10
+ rencode.loads(f.read())
11
+ except Exception as e:
12
+ print(e)
@@ -72,7 +72,7 @@ pure Python module written by Petru Paler, Connelly Barnes et al.
72
72
 
73
73
  setup(
74
74
  name="rencode",
75
- version="1.0.3",
75
+ version="1.0.4",
76
76
  packages=["rencode"],
77
77
  description=description,
78
78
  author="Andrew Resch",
File without changes
File without changes