-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathbsdiffpatch-test.c
More file actions
130 lines (108 loc) · 3.65 KB
/
bsdiffpatch-test.c
File metadata and controls
130 lines (108 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*-
* Copyright 2015 Colin Walters <walters@verbum.org>
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* Compile with $(pkg-config --cflags --libs glib-2.0) */
#include "bsdiff.h"
#undef BSDIFF_EXECUTABLE
#include "bsdiff.c"
#include "bspatch.c"
#include <glib.h>
#include <err.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
static int
bsdiff_write_gstring (struct bsdiff_stream *stream,
const void *buf,
int size)
{
g_string_append_len ((GString*)stream->opaque, buf, size);
return 0;
}
typedef struct {
GString *buf;
size_t off;
} GStringInputStream;
static int
gstringin_read (const struct bspatch_stream* stream, void* buffer, int length)
{
GStringInputStream *gin = stream->opaque;
size_t rlen = MIN(gin->buf->len - gin->off, length);
if (rlen == 0)
return 0;
memcpy (buffer, gin->buf->str + gin->off, rlen);
gin->off += rlen;
return 0;
}
int main(int argc,char *argv[])
{
const char *original = NULL;
size_t original_len;
const char *modified = NULL;
size_t modified_len;
GString *gmembuf = g_string_new (NULL);
GString *gpatchbuf = g_string_new (NULL);
{
ssize_t bytes_read;
char inbuf[1024];
while ((bytes_read = read (0, inbuf, sizeof (inbuf))) != 0)
g_string_append_len (gmembuf, inbuf, bytes_read);
if (bytes_read < 0)
err (1, "read");
if (gmembuf->len < 2)
errx (1, "Input length is less than 2");
}
original = gmembuf->str;
original_len = gmembuf->len / 2;
modified = original + original_len;
modified_len = gmembuf->len - original_len;
g_string_free (gmembuf, FALSE);
gmembuf = NULL;
{
struct bsdiff_stream stream;
stream.malloc = malloc;
stream.free = free;
stream.write = bsdiff_write_gstring;
stream.opaque = gpatchbuf;
if (bsdiff (original, original_len, modified, modified_len, &stream))
err(1, "bsdiff");
}
{
GStringInputStream gin = { gpatchbuf, 0 };
struct bspatch_stream pstream;
GString *newbuf = g_string_new (NULL);
pstream.opaque = &gin;
pstream.read = gstringin_read;
g_string_set_size (newbuf, modified_len);
if (bspatch (original, original_len, newbuf->str, modified_len, &pstream))
errx (1, "bspatch failed");
if (memcmp (modified, newbuf->str, modified_len) != 0)
errx (1, "bspatch did not reproduce modified content!");
g_string_free (newbuf, TRUE);
}
/* Free the memory we used */
g_string_free (gpatchbuf, TRUE);
return 0;
}