]> git.ozlabs.org Git - ccan/blob - ccan/bdelta/bdelta.h
bdelta: new module for binary diff/patch
[ccan] / ccan / bdelta / bdelta.h
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
23 #ifndef CCAN_BDELTA_H
24 #define CCAN_BDELTA_H
25
26 #include <stddef.h>
27
28 typedef enum {
29         BDELTA_OK               = 0,  /* Operation succeeded. */
30
31         BDELTA_MEMORY           = 1,  /* Memory allocation failed. */
32         BDELTA_PATCH_INVALID    = 2,  /* Patch is malformed. */
33         BDELTA_PATCH_MISMATCH   = 3,  /* Patch applied to wrong original string. */
34         
35         /* Internal error codes.  These will never be returned by API functions. */
36         BDELTA_INTERNAL_DMAX_EXCEEDED    = -10,
37         BDELTA_INTERNAL_INPUTS_TOO_LARGE = -11,
38 } BDELTAcode;
39
40 /*
41  * bdelta_diff - Given two byte strings, generate a "patch" (also a byte string)
42  * that describes how to transform the old string into the new string.
43  *
44  * On success, returns BDELTA_OK, and passes a malloc'd block
45  * and its size through *patch_out and *patch_size_out.
46  *
47  * On failure, returns an error code, and clears *patch_out and *patch_size_out.
48  *
49  * Example:
50  *      const char *old = "abcabba";
51  *      const char *new_ = "cbabac";
52  *      void *patch;
53  *      size_t patch_size;
54  *      BDELTAcode rc;
55  *
56  *      rc = bdelta_diff(old, strlen(old), new_, strlen(new_), &patch, &patch_size);
57  *      if (rc != BDELTA_OK) {
58  *              bdelta_perror("bdelta_diff", rc);
59  *              return;
60  *      }
61  *      ...
62  *      free(patch);
63  */
64 BDELTAcode bdelta_diff(
65         const void  *old,       size_t  old_size,
66         const void  *new_,      size_t  new_size,
67         void       **patch_out, size_t *patch_size_out
68 );
69
70 /*
71  * bdelta_patch - Apply a patch produced by bdelta_diff to the
72  * old string to recover the new string.
73  *
74  * On success, returns BDELTA_OK, and passes a malloc'd block
75  * and its size through *new_out and *new_size_out.
76  *
77  * On failure, returns an error code, and clears *new_out and *new_size_out.
78  *
79  * Example:
80  *      const char *old = "abcabba";
81  *      void *new_;
82  *      size_t new_size;
83  *      BDELTAcode rc;
84  *
85  *      rc = bdelta_patch(old, strlen(old), patch, patch_size, &new_, &new_size);
86  *      if (rc != BDELTA_OK) {
87  *              bdelta_perror("bdelta_patch", rc);
88  *              return;
89  *      }
90  *      fwrite(new_, 1, new_size, stdout);
91  *      putchar('\n');
92  *      free(new_);
93  */
94 BDELTAcode bdelta_patch(
95         const void  *old,     size_t  old_size,
96         const void  *patch,   size_t  patch_size,
97         void       **new_out, size_t *new_size_out
98 );
99
100 /*
101  * bdelta_strerror - Return a string describing a bdelta error code.
102  */
103 const char *bdelta_strerror(BDELTAcode code);
104
105 /*
106  * bdelta_perror - Print a bdelta error message to stderr.
107  *
108  * This function handles @s the same way perror does.
109  */
110 void bdelta_perror(const char *s, BDELTAcode code);
111
112 #endif