bdelta.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef CCAN_BDELTA_H
  23. #define CCAN_BDELTA_H
  24. #include <stddef.h>
  25. typedef enum {
  26. BDELTA_OK = 0, /* Operation succeeded. */
  27. BDELTA_MEMORY = 1, /* Memory allocation failed. */
  28. BDELTA_PATCH_INVALID = 2, /* Patch is malformed. */
  29. BDELTA_PATCH_MISMATCH = 3, /* Patch applied to wrong original string. */
  30. /* Internal error codes. These will never be returned by API functions. */
  31. BDELTA_INTERNAL_DMAX_EXCEEDED = -10,
  32. BDELTA_INTERNAL_INPUTS_TOO_LARGE = -11,
  33. } BDELTAcode;
  34. /*
  35. * bdelta_diff - Given two byte strings, generate a "patch" (also a byte string)
  36. * that describes how to transform the old string into the new string.
  37. *
  38. * On success, returns BDELTA_OK, and passes a malloc'd block
  39. * and its size through *patch_out and *patch_size_out.
  40. *
  41. * On failure, returns an error code, and clears *patch_out and *patch_size_out.
  42. *
  43. * Example:
  44. * const char *old = "abcabba";
  45. * const char *new_ = "cbabac";
  46. * void *patch;
  47. * size_t patch_size;
  48. * BDELTAcode rc;
  49. *
  50. * rc = bdelta_diff(old, strlen(old), new_, strlen(new_), &patch, &patch_size);
  51. * if (rc != BDELTA_OK) {
  52. * bdelta_perror("bdelta_diff", rc);
  53. * return;
  54. * }
  55. * ...
  56. * free(patch);
  57. */
  58. BDELTAcode bdelta_diff(
  59. const void *old, size_t old_size,
  60. const void *new_, size_t new_size,
  61. void **patch_out, size_t *patch_size_out
  62. );
  63. /*
  64. * bdelta_patch - Apply a patch produced by bdelta_diff to the
  65. * old string to recover the new string.
  66. *
  67. * On success, returns BDELTA_OK, and passes a malloc'd block
  68. * and its size through *new_out and *new_size_out.
  69. *
  70. * On failure, returns an error code, and clears *new_out and *new_size_out.
  71. *
  72. * Example:
  73. * const char *old = "abcabba";
  74. * void *new_;
  75. * size_t new_size;
  76. * BDELTAcode rc;
  77. *
  78. * rc = bdelta_patch(old, strlen(old), patch, patch_size, &new_, &new_size);
  79. * if (rc != BDELTA_OK) {
  80. * bdelta_perror("bdelta_patch", rc);
  81. * return;
  82. * }
  83. * fwrite(new_, 1, new_size, stdout);
  84. * putchar('\n');
  85. * free(new_);
  86. */
  87. BDELTAcode bdelta_patch(
  88. const void *old, size_t old_size,
  89. const void *patch, size_t patch_size,
  90. void **new_out, size_t *new_size_out
  91. );
  92. /*
  93. * bdelta_strerror - Return a string describing a bdelta error code.
  94. */
  95. const char *bdelta_strerror(BDELTAcode code);
  96. /*
  97. * bdelta_perror - Print a bdelta error message to stderr.
  98. *
  99. * This function handles @s the same way perror does.
  100. */
  101. void bdelta_perror(const char *s, BDELTAcode code);
  102. #endif