chunkify.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef CHUNKIFY_H_
  2. #define CHUNKIFY_H_
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. /**
  6. * Structure used to store chunkifier state.
  7. */
  8. typedef struct chunkifier_internal CHUNKIFIER;
  9. /**
  10. * Callback when the end of a chunk is reached. The parameters passed are
  11. * the cookie provided to chunkify_init, a pointer to a buffer containing
  12. * the chunk, and the length of the chunk in bytes.
  13. *
  14. * Upon success, the callback should return 0. Upon failure, a nonzero
  15. * value should be returned, and will be passed upstream to the caller of
  16. * chunkify_write or chunkify_end.
  17. */
  18. typedef int chunkify_callback(void *, uint8_t *, size_t);
  19. /**
  20. * chunkify_init(meanlen, maxlen, callback, cookie):
  21. * Initialize and return a CHUNKIFIER structure suitable for dividing a
  22. * stream of bytes into chunks of mean and maximum lengths ${meanlen} and
  23. * ${maxlen}. In most cases, ${maxlen} should be at least ${2 * meanlen};
  24. * values greater than ${4 * meanlen} will have very little effect beyond
  25. * wasting memory.
  26. *
  27. * If an error occurs, NULL is returned.
  28. *
  29. * The parameter ${meanlen} must be at most 1262226.
  30. * The probability of a chunk being x bytes or longer is approximately
  31. * 0.267 ^ ((x / meanlen)^(3/2)).
  32. * The most common chunk length is approximately ${0.65 * meanlen}.
  33. */
  34. CHUNKIFIER * chunkify_init(uint32_t, uint32_t, chunkify_callback *, void *);
  35. /**
  36. * chunkify_write(c, buf, buflen):
  37. * Feed the provided buffer into the CHUNKIFIER; callback(s) are made if
  38. * chunk(s) end during this process.
  39. *
  40. * The value returned is zero, or the first nonzero value returned by the
  41. * callback function.
  42. *
  43. * If ${c} is NULL, do nothing.
  44. */
  45. int chunkify_write(CHUNKIFIER *, const uint8_t *, size_t);
  46. /**
  47. * chunkify_end(c):
  48. * Terminate a chunk by calling chunkdone and initialize the CHUNKIFIER
  49. * for the start of the next chunk.
  50. *
  51. * The value returned is zero or the nonzero value returned by the callback
  52. * function.
  53. *
  54. * If ${c} is NULL, do nothing.
  55. */
  56. int chunkify_end(CHUNKIFIER *);
  57. /**
  58. * chunkify_free(c):
  59. * Free the memory allocated by chunkify_init(...), but do not
  60. * call chunkify_end; the caller is responsible for doing this.
  61. */
  62. void chunkify_free(CHUNKIFIER *);
  63. #endif /* !CHUNKIFY_H_ */