THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.3KB

  1. #ifndef DELTA_H
  2. #define DELTA_H
  3. /* opaque object for delta index */
  4. struct delta_index;
  5. /*
  6. * create_delta_index: compute index data from given buffer
  7. *
  8. * This returns a pointer to a struct delta_index that should be passed to
  9. * subsequent create_delta() calls, or to free_delta_index(). A NULL pointer
  10. * is returned on failure. The given buffer must not be freed or altered
  11. * before free_delta_index() is called. The returned pointer must be freed
  12. * using free_delta_index().
  13. */
  14. struct delta_index *
  15. create_delta_index(const void *buf, unsigned long bufsize);
  16. /*
  17. * free_delta_index: free the index created by create_delta_index()
  18. *
  19. * Given pointer must be what create_delta_index() returned, or NULL.
  20. */
  21. void free_delta_index(struct delta_index *index);
  22. /*
  23. * sizeof_delta_index: returns memory usage of delta index
  24. *
  25. * Given pointer must be what create_delta_index() returned, or NULL.
  26. */
  27. unsigned long sizeof_delta_index(struct delta_index *index);
  28. /*
  29. * create_delta: create a delta from given index for the given buffer
  30. *
  31. * This function may be called multiple times with different buffers using
  32. * the same delta_index pointer. If max_delta_size is non-zero and the
  33. * resulting delta is to be larger than max_delta_size then NULL is returned.
  34. * On success, a non-NULL pointer to the buffer with the delta data is
  35. * returned and *delta_size is updated with its size. The returned buffer
  36. * must be freed by the caller.
  37. */
  38. void *
  39. create_delta(const struct delta_index *index,
  40. const void *buf, unsigned long bufsize,
  41. unsigned long *delta_size, unsigned long max_delta_size);
  42. /*
  43. * diff_delta: create a delta from source buffer to target buffer
  44. *
  45. * If max_delta_size is non-zero and the resulting delta is to be larger
  46. * than max_delta_size then NULL is returned. On success, a non-NULL
  47. * pointer to the buffer with the delta data is returned and *delta_size is
  48. * updated with its size. The returned buffer must be freed by the caller.
  49. */
  50. static inline void *
  51. diff_delta(const void *src_buf, unsigned long src_bufsize,
  52. const void *trg_buf, unsigned long trg_bufsize,
  53. unsigned long *delta_size, unsigned long max_delta_size)
  54. {
  55. struct delta_index *index = create_delta_index(src_buf, src_bufsize);
  56. if (index) {
  57. void *delta = create_delta(index, trg_buf, trg_bufsize,
  58. delta_size, max_delta_size);
  59. free_delta_index(index);
  60. return delta;
  61. }
  62. return NULL;
  63. }
  64. /*
  65. * patch_delta: recreate target buffer given source buffer and delta data
  66. *
  67. * On success, a non-NULL pointer to the target buffer is returned and
  68. * *trg_bufsize is updated with its size. On failure a NULL pointer is
  69. * returned. The returned buffer must be freed by the caller.
  70. */
  71. void *patch_delta(const void *src_buf, unsigned long src_size,
  72. const void *delta_buf, unsigned long delta_size,
  73. unsigned long *dst_size);
  74. /* the smallest possible delta size is 4 bytes */
  75. #define DELTA_SIZE_MIN 4
  76. /*
  77. * This must be called twice on the delta data buffer, first to get the
  78. * expected source buffer size, and again to get the target buffer size.
  79. */
  80. static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
  81. const unsigned char *top)
  82. {
  83. const unsigned char *data = *datap;
  84. unsigned long cmd, size = 0;
  85. int i = 0;
  86. do {
  87. cmd = *data++;
  88. size |= (cmd & 0x7f) << i;
  89. i += 7;
  90. } while (cmd & 0x80 && data < top);
  91. *datap = data;
  92. return size;
  93. }
  94. #endif