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