summaryrefslogtreecommitdiff
path: root/xdelta3/examples
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3/examples')
-rwxr-xr-xxdelta3/examples/Makefile11
-rw-r--r--xdelta3/examples/README8
-rw-r--r--xdelta3/examples/encode_decode_test.c190
3 files changed, 206 insertions, 3 deletions
diff --git a/xdelta3/examples/Makefile b/xdelta3/examples/Makefile
index dd1f036..f626326 100755
--- a/xdelta3/examples/Makefile
+++ b/xdelta3/examples/Makefile
@@ -1,9 +1,14 @@
1CFLAGS = -g -Wall -I .. 1CFLAGS = -g -Wall -I.. -DXD3_DEBUG=1
2 2
3SOURCES = small_page_test.c 3SOURCES = small_page_test.c
4 4
5small_page_test: $(SOURCES) 5all: small_page_test encode_decode_test
6 $(CC) $(CFLAGS) small_page_test.c -o small_page_test -DXD3_USE_LARGEFILE64=1 -DSECONDARY_DJW=1 -DXD3_DEBUG=1 6
7small_page_test: small_page_test.c
8 $(CC) $(CFLAGS) small_page_test.c -o small_page_test -DXD3_USE_LARGEFILE64=1 -DSECONDARY_DJW=1
9
10encode_decode_test: encode_decode_test.c
11 $(CC) $(CFLAGS) encode_decode_test.c -o encode_decode_test
7 12
8clean: 13clean:
9 rm -f *.exe *.stackdump 14 rm -f *.exe *.stackdump
diff --git a/xdelta3/examples/README b/xdelta3/examples/README
new file mode 100644
index 0000000..60840bf
--- /dev/null
+++ b/xdelta3/examples/README
@@ -0,0 +1,8 @@
1Files in this directory demonstrate how to use the Xdelta3 API. Copyrights
2are held by the respective authors and these files are not covered by the GPL.
3
4small_page_test.c -- how to use xdelta3 in an environment such as the kernel
5for small pages with little memory
6
7encode_decode_test.c -- how to use xdelta3 to process (encode/decode) data in
8multiple windows with the non-blocking API
diff --git a/xdelta3/examples/encode_decode_test.c b/xdelta3/examples/encode_decode_test.c
new file mode 100644
index 0000000..2f28d78
--- /dev/null
+++ b/xdelta3/examples/encode_decode_test.c
@@ -0,0 +1,190 @@
1//
2// Permission to distribute this example by
3// Copyright (C) 2007 Ralf Junker
4// Ralf Junker <delphi@yunqa.de>
5// http://www.yunqa.de/delphi/
6
7//---------------------------------------------------------------------------
8
9#include <stdio.h>
10#include <sys\stat.h>
11#include "xdelta3.h"
12#include "xdelta3.c"
13
14//---------------------------------------------------------------------------
15
16int code (
17 int encode,
18 FILE* InFile,
19 FILE* SrcFile ,
20 FILE* OutFile,
21 int BufSize )
22{
23 int r, ret;
24 struct stat statbuf;
25 xd3_stream stream;
26 xd3_config config;
27 xd3_source source;
28 void* Input_Buf;
29 int Input_Buf_Read;
30
31 if (BufSize < XD3_ALLOCSIZE)
32 BufSize = XD3_ALLOCSIZE;
33
34 memset (&stream, 0, sizeof (stream));
35 memset (&source, 0, sizeof (source));
36
37 xd3_init_config(&config, XD3_ADLER32);
38 config.winsize = BufSize;
39 xd3_config_stream(&stream, &config);
40
41 if (SrcFile)
42 {
43 r = fstat(fileno(SrcFile), &statbuf);
44 if (r)
45 return r;
46 source.size = statbuf.st_size;
47 source.blksize = BufSize;
48 source.curblk = malloc(source.blksize);
49
50 /* Load 1st block of stream. */
51 r = fseek(SrcFile, 0, SEEK_SET);
52 if (r)
53 return r;
54 source.onblk = fread((void*)source.curblk, 1, source.blksize, SrcFile);
55 source.curblkno = 0;
56 /* Set the stream. */
57 xd3_set_source(&stream, &source);
58 }
59
60 Input_Buf = malloc(BufSize);
61
62 fseek(InFile, 0, SEEK_SET);
63 do
64 {
65 Input_Buf_Read = fread(Input_Buf, 1, BufSize, InFile);
66 if (Input_Buf_Read < BufSize)
67 {
68 xd3_set_flags(&stream, XD3_FLUSH);
69 }
70 xd3_avail_input(&stream, Input_Buf, Input_Buf_Read);
71
72process:
73 if (encode)
74 ret = xd3_encode_input(&stream);
75 else
76 ret = xd3_decode_input(&stream);
77
78 switch (ret)
79 {
80 case XD3_INPUT:
81 {
82 fprintf (stderr,"XD3_INPUT\n");
83 continue;
84 }
85
86 case XD3_OUTPUT:
87 {
88 fprintf (stderr,"XD3_OUTPUT\n");
89 r = fwrite(stream.next_out, 1, stream.avail_out, OutFile);
90 if (r != (int)stream.avail_out)
91 return r;
92 xd3_consume_output(&stream);
93 goto process;
94 }
95
96 case XD3_GETSRCBLK:
97 {
98 fprintf (stderr,"XD3_GETSRCBLK %qd\n", source.getblkno);
99 if (SrcFile)
100 {
101 r = fseek(SrcFile, source.blksize * source.getblkno, SEEK_SET);
102 if (r)
103 return r;
104 source.onblk = fread((void*)source.curblk, 1,
105 source.blksize, SrcFile);
106 source.curblkno = source.getblkno;
107 }
108 goto process;
109 }
110
111 case XD3_GOTHEADER:
112 {
113 fprintf (stderr,"XD3_GOTHEADER\n");
114 goto process;
115 }
116
117 case XD3_WINSTART:
118 {
119 fprintf (stderr,"XD3_WINSTART\n");
120 goto process;
121 }
122
123 case XD3_WINFINISH:
124 {
125 fprintf (stderr,"XD3_WINFINISH\n");
126 goto process;
127 }
128
129 default:
130 {
131 fprintf (stderr,"!!! INVALID %s %d !!!\n",
132 stream.msg, ret);
133 return ret;
134 }
135
136 }
137
138 }
139 while (Input_Buf_Read == BufSize);
140
141 free(Input_Buf);
142
143 free((void*)source.curblk);
144 xd3_close_stream(&stream);
145 xd3_free_stream(&stream);
146
147 return 0;
148
149};
150
151
152int main(int argc, char* argv[])
153{
154 FILE* InFile;
155 FILE* SrcFile;
156 FILE* OutFile;
157 int r;
158
159 /* Encode */
160
161 InFile = fopen("input.txt", "rb");
162 SrcFile = fopen("source.txt", "rb");
163 OutFile = fopen("encoded.txt", "wb");
164
165 r = code (1, InFile, SrcFile, OutFile, 0x1000);
166
167 fclose(OutFile);
168 fclose(SrcFile);
169 fclose(InFile);
170
171 if (r)
172 return r;
173
174 /* Decode */
175
176 InFile = fopen("encoded.txt", "rb");
177 SrcFile = fopen("source.txt", "rb");
178 OutFile = fopen("decoded.txt", "wb");
179
180 r = code (0, InFile, SrcFile, OutFile, 0x1000);
181
182 fclose(OutFile);
183 fclose(SrcFile);
184 fclose(InFile);
185
186 if (r)
187 return r;
188
189 return 0;
190}