diff options
Diffstat (limited to 'xdelta3/examples')
-rwxr-xr-x | xdelta3/examples/Makefile | 6 | ||||
-rwxr-xr-x | xdelta3/examples/small_page_test.c | 169 |
2 files changed, 175 insertions, 0 deletions
diff --git a/xdelta3/examples/Makefile b/xdelta3/examples/Makefile new file mode 100755 index 0000000..657e08a --- /dev/null +++ b/xdelta3/examples/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | CFLAGS = -g -O2 -Wall -I .. | ||
2 | |||
3 | SOURCES = small_page_test.c | ||
4 | |||
5 | small_page_test: $(SOURCES) | ||
6 | $(CC) $(CFLAGS) small_page_test.c -o small_page_test -DXD3_USE_LARGEFILE64=1 -DSECONDARY_DJW=1 | ||
diff --git a/xdelta3/examples/small_page_test.c b/xdelta3/examples/small_page_test.c new file mode 100755 index 0000000..16dd1c1 --- /dev/null +++ b/xdelta3/examples/small_page_test.c | |||
@@ -0,0 +1,169 @@ | |||
1 | /* Copyright (C) 2007 Josh MacDonald */ | ||
2 | |||
3 | #include <stdio.h> | ||
4 | |||
5 | #define SPACE_MAX 32768 | ||
6 | #define PAGE_SIZE 4096 | ||
7 | #define OUTPUT_MAX 1024 | ||
8 | #define IOPT_SIZE 1024 | ||
9 | #define XD3_ALLOCSIZE 256 | ||
10 | |||
11 | // typedef void* (xd3_alloc_func) (void *opaque, | ||
12 | // usize_t items, | ||
13 | // usize_t size); | ||
14 | // typedef void (xd3_free_func) (void *opaque, | ||
15 | // void *address); | ||
16 | |||
17 | #include "xdelta3.h" | ||
18 | #include "xdelta3.c" | ||
19 | |||
20 | typedef struct _context { | ||
21 | uint8_t *buffer; | ||
22 | int allocated; | ||
23 | } context_t; | ||
24 | |||
25 | void* | ||
26 | process_alloc (void* opaque, usize_t items, usize_t size) | ||
27 | { | ||
28 | context_t *ctx = (context_t*) opaque; | ||
29 | usize_t t = items * size; | ||
30 | void *ret; | ||
31 | |||
32 | if (ctx->allocated + t > SPACE_MAX) | ||
33 | { | ||
34 | return NULL; | ||
35 | } | ||
36 | |||
37 | ret = ctx->buffer + ctx->allocated; | ||
38 | ctx->allocated += t; | ||
39 | return ret; | ||
40 | } | ||
41 | |||
42 | int | ||
43 | process_page (int is_encode, | ||
44 | int (*func) (xd3_stream *), | ||
45 | const uint8_t *input, | ||
46 | usize_t input_size, | ||
47 | const uint8_t *source, | ||
48 | uint8_t *output, | ||
49 | usize_t *output_size, | ||
50 | usize_t output_size_max, | ||
51 | int flags) { | ||
52 | xd3_stream *stream; | ||
53 | xd3_config *config; | ||
54 | xd3_source *src; | ||
55 | context_t *ctx = calloc(OUTPUT_MAX, 1); | ||
56 | int ret; | ||
57 | |||
58 | ctx->buffer = ((char*)ctx) + sizeof(*ctx); | ||
59 | ctx->allocated = sizeof(*ctx); | ||
60 | |||
61 | stream = process_alloc (ctx, 1, sizeof(*stream)); | ||
62 | config = process_alloc (ctx, 1, sizeof(*config)); | ||
63 | src = process_alloc (ctx, 1, sizeof(*src)); | ||
64 | |||
65 | config->flags = flags; | ||
66 | config->winsize = PAGE_SIZE; | ||
67 | config->sprevsz = PAGE_SIZE; | ||
68 | config->srcwin_maxsz = PAGE_SIZE; | ||
69 | config->iopt_size = IOPT_SIZE; | ||
70 | config->alloc = &process_alloc; | ||
71 | |||
72 | src->size = PAGE_SIZE; | ||
73 | src->blksize = PAGE_SIZE; | ||
74 | src->onblk = PAGE_SIZE; | ||
75 | src->curblk = source; | ||
76 | src->curblkno = 0; | ||
77 | |||
78 | if ((ret = xd3_config_stream (stream, config)) != 0 || | ||
79 | (ret = xd3_set_source (stream, src)) != 0 || | ||
80 | (ret = xd3_process_stream (is_encode, | ||
81 | stream, | ||
82 | func, 1, | ||
83 | input, PAGE_SIZE, | ||
84 | output, output_size, | ||
85 | output_size_max)) != 0) | ||
86 | { | ||
87 | // (void) 0; | ||
88 | } | ||
89 | |||
90 | xd3_free_stream (stream); | ||
91 | return ret; | ||
92 | } | ||
93 | |||
94 | int test(int stride, int encode_flags) | ||
95 | { | ||
96 | uint8_t frompg[PAGE_SIZE]; | ||
97 | uint8_t topg[PAGE_SIZE]; | ||
98 | uint8_t output[OUTPUT_MAX]; | ||
99 | uint8_t reout[OUTPUT_MAX]; | ||
100 | usize_t output_size; | ||
101 | usize_t re_size; | ||
102 | int i, j, ret; | ||
103 | |||
104 | for (i = 0; i < PAGE_SIZE; i++) | ||
105 | { | ||
106 | topg[i] = frompg[i] = lrand48(); | ||
107 | } | ||
108 | |||
109 | // change 1 byte every stride | ||
110 | if (stride > 0) | ||
111 | { | ||
112 | for (j = stride; j <= PAGE_SIZE; j += stride) | ||
113 | { | ||
114 | topg[j - 1] ^= 0xff; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | if ((ret = process_page (1, xd3_encode_input, | ||
119 | topg, PAGE_SIZE, | ||
120 | frompg, output, | ||
121 | &output_size, OUTPUT_MAX, | ||
122 | encode_flags)) != 0) | ||
123 | { | ||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | if ((ret = process_page (1, xd3_decode_input, | ||
128 | output, output_size, | ||
129 | frompg, reout, | ||
130 | &re_size, PAGE_SIZE, | ||
131 | 0)) != 0) | ||
132 | { | ||
133 | return ret; | ||
134 | } | ||
135 | |||
136 | if (output_size > OUTPUT_MAX || re_size != PAGE_SIZE) | ||
137 | { | ||
138 | printf ("internal error\n"); | ||
139 | return -1; | ||
140 | } | ||
141 | |||
142 | printf("stride %d flags 0x%x size %u ", stride, encode_flags, output_size); | ||
143 | printf("%s\n", (ret == 0) ? "OK" : "FAIL"); | ||
144 | |||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | int main() | ||
149 | { | ||
150 | int stride; | ||
151 | int level; | ||
152 | int ret; | ||
153 | |||
154 | for (level = 1; level < 10; level = (level == 1 ? 3 : level + 3)) | ||
155 | { | ||
156 | int lflag = level << XD3_COMPLEVEL_SHIFT; | ||
157 | for (stride = 0; stride <= PAGE_SIZE; stride += PAGE_SIZE / 64) | ||
158 | { | ||
159 | if ((ret = test(stride, lflag)) || | ||
160 | (ret = test(stride, lflag | XD3_ADLER32)) || | ||
161 | (ret = test(stride, lflag | XD3_SEC_DJW))) | ||
162 | { | ||
163 | return ret; | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | |||
168 | return 0; | ||
169 | } | ||