chunker.pyx 1.5 KB

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