crcsync.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef CCAN_CRCSYNC_H
  2. #define CCAN_CRCSYNC_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. /**
  6. * crc_of_blocks - calculate the crc of the blocks.
  7. * @data: pointer to the buffer to CRC
  8. * @len: length of the buffer
  9. * @blocksize: CRC of each block (final block may be shorter)
  10. * @crcbits: the number of bits of crc you want (currently 64 maximum)
  11. * @crc: the crcs (array will have (len + blocksize-1)/blocksize entries).
  12. *
  13. * Calculates the CRC of each block, and output the lower @crcbits to
  14. * @crc array.
  15. */
  16. void crc_of_blocks(const void *data, size_t len, unsigned int blocksize,
  17. unsigned int crcbits, uint64_t crc[]);
  18. /**
  19. * crc_context_new - allocate and initialize state for crc_find_block
  20. * @blocksize: the size of each block
  21. * @crcbits: the bits valid in the CRCs (<= 64)
  22. * @crc: array of block crcs (including final block, if any)
  23. * @num_crcs: number of block crcs
  24. * @tail_size: the size of final partial block, if any (< blocksize).
  25. *
  26. * Returns an allocated pointer to the structure for crc_find_block,
  27. * or NULL. Makes a copy of @crc.
  28. */
  29. struct crc_context *crc_context_new(size_t blocksize, unsigned crcbits,
  30. const uint64_t crc[], unsigned num_crcs,
  31. size_t final_size);
  32. /**
  33. * crc_read_block - search for block matches in the buffer.
  34. * @ctx: struct crc_context from crc_context_new.
  35. * @result: unmatched bytecount, or crc which matched.
  36. * @buf: pointer to bytes
  37. * @buflen: length of buffer
  38. *
  39. * Returns the number of bytes of the buffer which have been digested,
  40. * and sets @result either to a negagive number (== -crc_number - 1)
  41. * to show that a block matched a crc, or zero or more to represent a
  42. * length of unmatched data.
  43. *
  44. * Note that multiple lengths of unmatched data might be returned in a row:
  45. * you'll probably want to merge them yourself.
  46. */
  47. size_t crc_read_block(struct crc_context *ctx, long *result,
  48. const void *buf, size_t buflen);
  49. /**
  50. * crc_read_flush - flush any remaining data from crc_read_block.
  51. * @ctx: the context passed to crc_read_block.
  52. *
  53. * Matches the final data. This can be used to create a boundary, or
  54. * simply flush the final data. Keep calling it until it returns 0.
  55. */
  56. long crc_read_flush(struct crc_context *ctx);
  57. /**
  58. * crc_context_free - free a context returned from crc_context_new.
  59. * @ctx: the context returned from crc_context_new, or NULL.
  60. */
  61. void crc_context_free(struct crc_context *ctx);
  62. #endif /* CCAN_CRCSYNC_H */