_info 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <string.h>
  2. #include "config.h"
  3. /**
  4. * bdelta - Generate and apply binary deltas
  5. *
  6. * This library takes two strings containing binary data, and produces a
  7. * "patch" that can be applied to the first one to produce the second one.
  8. * It can be used to save bandwidth and disk space when many texts differing
  9. * by a small number of bytes need to be transmitted or stored.
  10. *
  11. * Patches produced by this version of the library can be applied using future
  12. * versions, but not past versions.
  13. *
  14. * bdelta implements the algorithm described in
  15. * An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers.
  16. * Because its memory usage and expected running time are O(N + D^2),
  17. * it works well only when the strings differ by a small number of bytes.
  18. * This implementation stops trying when the strings differ by more than
  19. * 1000 bytes, and falls back to producing a patch that simply emits the new
  20. * string.
  21. *
  22. * Thus, bdelta does not save any space when given two strings that differ by
  23. * more than 1000 bytes. This may be improved in a future version of the
  24. * library.
  25. *
  26. * Example:
  27. * #include <ccan/bdelta/bdelta.h>
  28. * #include <stdio.h>
  29. * #include <stdlib.h>
  30. * #include <string.h>
  31. *
  32. * static void gulp(const char *filename, void **data_out, size_t *size_out);
  33. *
  34. * static int usage(const char *prog)
  35. * {
  36. * fprintf(
  37. * stderr,
  38. * "Usage: %s diff <old> <new> > <patch>\n"
  39. * " %s patch <old> <patch> > <new>\n",
  40. * prog, prog
  41. * );
  42. * return 1;
  43. * }
  44. *
  45. * int main(int argc, char *argv[])
  46. * {
  47. * void *old, *new_, *patch;
  48. * size_t old_size, new_size, patch_size;
  49. * BDELTAcode rc;
  50. *
  51. * if (argc != 4)
  52. * return usage(argv[0]);
  53. *
  54. * if (strcmp(argv[1], "diff") == 0) {
  55. * gulp(argv[2], &old, &old_size);
  56. * gulp(argv[3], &new_, &new_size);
  57. *
  58. * rc = bdelta_diff(old, old_size, new_, new_size, &patch, &patch_size);
  59. * if (rc != BDELTA_OK) {
  60. * bdelta_perror("bdelta_diff", rc);
  61. * return 1;
  62. * }
  63. *
  64. * if (fwrite(patch, 1, patch_size, stdout) != patch_size) {
  65. * perror("stdout");
  66. * return 1;
  67. * }
  68. * } else if (strcmp(argv[1], "patch") == 0) {
  69. * gulp(argv[2], &old, &old_size);
  70. * gulp(argv[3], &patch, &patch_size);
  71. *
  72. * rc = bdelta_patch(old, old_size, patch, patch_size, &new_, &new_size);
  73. * if (rc != BDELTA_OK) {
  74. * bdelta_perror("bdelta_patch", rc);
  75. * return 1;
  76. * }
  77. *
  78. * if (fwrite(new_, 1, new_size, stdout) != new_size) {
  79. * perror("stdout");
  80. * return 1;
  81. * }
  82. * } else {
  83. * return usage(argv[0]);
  84. * }
  85. *
  86. * free(old);
  87. * free(new_);
  88. * free(patch);
  89. * return 0;
  90. * }
  91. *
  92. * static void gulp(const char *filename, void **data_out, size_t *size_out)
  93. * {
  94. * FILE *f = fopen(filename, "rb");
  95. * size_t size = 0;
  96. * size_t alloc = 16;
  97. * char *data = malloc(alloc);
  98. *
  99. * if (f == NULL || data == NULL)
  100. * goto error;
  101. *
  102. * for (;;) {
  103. * size += fread(data + size, 1, alloc - size, f);
  104. * if (size < alloc) {
  105. * if (!feof(f))
  106. * goto error;
  107. * break;
  108. * }
  109. * data = realloc(data, alloc *= 2);
  110. * if (data == NULL)
  111. * goto error;
  112. * }
  113. *
  114. * if (fclose(f) != 0)
  115. * goto error;
  116. *
  117. * *data_out = data;
  118. * *size_out = size;
  119. * return;
  120. *
  121. * error:
  122. * perror(filename);
  123. * exit(EXIT_FAILURE);
  124. * }
  125. *
  126. * Author: Joey Adams <joeyadams3.14159@gmail.com>
  127. * License: MIT
  128. * Version: 0.1.1
  129. */
  130. int main(int argc, char *argv[])
  131. {
  132. /* Expect exactly one argument */
  133. if (argc != 2)
  134. return 1;
  135. if (strcmp(argv[1], "depends") == 0) {
  136. /* Nothing */
  137. return 0;
  138. }
  139. return 1;
  140. }