chunker.pyx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. API_VERSION = 1
  3. from libc.stdlib cimport free
  4. cdef extern from "_chunker.c":
  5. ctypedef int uint32_t
  6. ctypedef struct Chunker:
  7. pass
  8. Chunker *chunker_init(object fd, int window_size, int chunk_mask, int min_size, uint32_t seed)
  9. void chunker_free(Chunker *chunker)
  10. object chunker_process(Chunker *chunker)
  11. uint32_t *buzhash_init_table(uint32_t seed)
  12. uint32_t c_buzhash "buzhash"(unsigned char *data, size_t len, uint32_t *h)
  13. uint32_t c_buzhash_update "buzhash_update"(uint32_t sum, unsigned char remove, unsigned char add, size_t len, uint32_t *h)
  14. cdef class chunkify:
  15. cdef Chunker *chunker
  16. def __cinit__(self, fd, window_size, chunk_mask, min_size, seed):
  17. self.chunker = chunker_init(fd, window_size, chunk_mask, min_size, seed & 0xffffffff)
  18. def __dealloc__(self):
  19. if self.chunker:
  20. chunker_free(self.chunker)
  21. def __iter__(self):
  22. return self
  23. def __next__(self):
  24. return chunker_process(self.chunker)
  25. def buzhash(unsigned char *data, unsigned long seed):
  26. cdef uint32_t *table
  27. cdef uint32_t sum
  28. table = buzhash_init_table(seed & 0xffffffff)
  29. sum = c_buzhash(data, len(data), table)
  30. free(table)
  31. return sum
  32. def buzhash_update(uint32_t sum, unsigned char remove, unsigned char add, size_t len, unsigned long seed):
  33. cdef uint32_t *table
  34. table = buzhash_init_table(seed & 0xffffffff)
  35. sum = c_buzhash_update(sum, remove, add, len, table)
  36. free(table)
  37. return sum