summaryrefslogtreecommitdiff
path: root/xdelta1/libedsio/default.c
diff options
context:
space:
mode:
authorJosh MacDonald <josh.macdonald@gmail.com>2016-02-26 21:43:58 -0800
committerJosh MacDonald <josh.macdonald@gmail.com>2016-02-26 21:43:58 -0800
commitd594fbe514e2a381541a510fe01041e546f56a67 (patch)
tree316072f34cf7dc94518ec14a8d955d57c20d65b7 /xdelta1/libedsio/default.c
parent81aebf78ae67c29f528088d65743643e5355e3d3 (diff)
Remove xdelta1 from this repo
Diffstat (limited to 'xdelta1/libedsio/default.c')
-rw-r--r--xdelta1/libedsio/default.c343
1 files changed, 0 insertions, 343 deletions
diff --git a/xdelta1/libedsio/default.c b/xdelta1/libedsio/default.c
deleted file mode 100644
index 0da2176..0000000
--- a/xdelta1/libedsio/default.c
+++ /dev/null
@@ -1,343 +0,0 @@
1/* -*-Mode: C;-*-
2 * $Id: default.c 1.1 Sun, 28 Jan 2007 10:02:26 -0800 jmacd $
3 *
4 * Copyright (C) 1998, 1999, Josh MacDonald.
5 * All Rights Reserved.
6 *
7 * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
8 */
9
10#include "edsio.h"
11
12/* Default Sink methods
13 */
14
15static gboolean
16sink_type_default (SerialSink* sink, SerialType type, guint32 len, gboolean set_allocation)
17{
18 if (! sink->next_uint32 (sink, type))
19 return FALSE;
20
21 /* Note: set_allocation is deprecated in 1.1.4 */
22 if (set_allocation && !sink->next_uint32 (sink, len))
23 return FALSE;
24
25 return TRUE;
26}
27
28static gboolean
29sink_next_uint16 (SerialSink* sink, guint16 num)
30{
31 num = g_htons (num);
32
33 return sink->sink_write (sink, (guint8*) &num, sizeof (num));
34}
35
36static gboolean
37sink_next_uint32 (SerialSink* sink, guint32 num)
38{
39 num = g_htonl (num);
40
41 return sink->sink_write (sink, (guint8*) &num, sizeof (num));
42}
43
44static gboolean
45sink_next_uint (SerialSink* sink, guint32 num)
46{
47 /* This is mostly because I dislike endian, and less to save space
48 * on small ints. However, the uint32 and uint16 functions are used
49 * when the number is expected to be large, in which case this
50 * format can expand the number. */
51
52 guint8 sink_buf[16]; /* this is enough room for a 12-byte int */
53 guint sink_count = 0;
54
55 do
56 {
57 guint left = num & 0x7f;
58 guint outnum;
59
60 num >>= 7;
61
62 outnum = left | (num ? 0x80 : 0);
63
64 sink_buf[sink_count++] = outnum;
65 }
66 while (num);
67
68 return sink->sink_write (sink, sink_buf, sink_count);
69}
70
71static gboolean
72sink_next_uint8 (SerialSink* sink, guint8 val)
73{
74 return sink->sink_write (sink, &val, 1);
75}
76
77static gboolean
78sink_next_bool (SerialSink* sink, gboolean val)
79{
80 guint8 sink_buf[1];
81 sink_buf[0] = val;
82 return sink->sink_write (sink, sink_buf, 1);
83}
84
85static gboolean
86sink_next_string (SerialSink* sink, const char *ptr)
87{
88 return sink->next_bytes (sink, ptr, strlen (ptr));
89}
90
91static gboolean
92sink_next_bytes (SerialSink* sink, const guint8 *ptr, guint32 len)
93{
94 return sink->next_uint (sink, len) &&
95 sink->sink_write (sink, ptr, len);
96}
97
98static gboolean
99sink_next_bytes_known (SerialSink* sink, const guint8 *ptr, guint32 len)
100{
101 return sink->sink_write (sink, ptr, len);
102}
103
104void
105serializeio_sink_init (SerialSink* it,
106 gboolean (* sink_type) (SerialSink* sink,
107 SerialType type,
108 guint32 mem_size,
109 gboolean set_allocation),
110 gboolean (* sink_close) (SerialSink* sink),
111 gboolean (* sink_write) (SerialSink* sink,
112 const guint8 *ptr,
113 guint32 len),
114 void (* sink_free) (SerialSink* sink),
115 gboolean (* sink_quantum) (SerialSink* sink))
116{
117 it->next_bytes_known = sink_next_bytes_known;
118 it->next_bytes = sink_next_bytes;
119 it->next_uint = sink_next_uint;
120 it->next_uint32 = sink_next_uint32;
121 it->next_uint16 = sink_next_uint16;
122 it->next_uint8 = sink_next_uint8;
123 it->next_bool = sink_next_bool;
124 it->next_string = sink_next_string;
125
126 if (sink_type)
127 it->sink_type = sink_type;
128 else
129 it->sink_type = sink_type_default;
130
131 it->sink_close = sink_close;
132 it->sink_write = sink_write;
133 it->sink_free = sink_free;
134 it->sink_quantum = sink_quantum;
135}
136
137/* Default Source methods
138 */
139
140static SerialType
141source_type_default (SerialSource* source, gboolean set_allocation)
142{
143 guint32 x;
144
145 if (! source->next_uint32 (source, & x))
146 return ST_Error;
147
148 if (set_allocation)
149 {
150 /* Note: set_allocation is deprecated in 1.1.4 */
151 guint32 bogus;
152 if (! source->next_uint32 (source, &bogus))
153 return ST_Error;
154 }
155
156 return x;
157}
158
159static gboolean
160source_next_uint32 (SerialSource* source, guint32 *ptr)
161{
162 guint32 x;
163
164 if (! source->source_read (source, (guint8*) &x, sizeof (x)))
165 return FALSE;
166
167 (*ptr) = g_ntohl (x);
168
169 return TRUE;
170}
171
172static gboolean
173source_next_uint16 (SerialSource* source, guint16 *ptr)
174{
175 guint16 x;
176
177 if (! source->source_read (source, (guint8*) &x, sizeof (x)))
178 return FALSE;
179
180 (*ptr) = g_ntohs (x);
181
182 return TRUE;
183}
184
185static gboolean
186source_next_uint (SerialSource* source, guint32 *ptr)
187{
188 /* This is mostly because I dislike endian, and less to save space
189 * on small ints */
190 guint8 c;
191 guint8 arr[16];
192 gint i = 0;
193 gint donebit = 1;
194 gint bits;
195
196 while (source->next_uint8 (source, &c))
197 {
198 donebit = c & 0x80;
199 bits = c & 0x7f;
200
201 arr[i++] = bits;
202
203 if (!donebit)
204 break;
205 }
206
207 if (donebit)
208 return FALSE;
209
210 *ptr = 0;
211
212 for (i -= 1; i >= 0; i -= 1)
213 {
214 *ptr <<= 7;
215 *ptr |= arr[i];
216 }
217
218 return TRUE;
219}
220
221static gboolean
222source_next_uint8 (SerialSource* source, guint8 *ptr)
223{
224 return source->source_read (source, ptr, 1);
225}
226
227static gboolean
228source_next_bool (SerialSource* source, gboolean *ptr)
229{
230 guint8 sink_buf[1];
231
232 if (! source->source_read (source, sink_buf, 1))
233 return FALSE;
234
235 if (sink_buf[0])
236 *ptr = TRUE;
237 else
238 *ptr = FALSE;
239
240 return TRUE;
241}
242
243static gboolean
244source_next_string (SerialSource* source, const char **ptr)
245{
246 guint32 len;
247 guint8* buf;
248
249 if (! source->next_uint (source, &len))
250 return FALSE;
251
252 if (! (buf = serializeio_source_alloc (source, len+1)))
253 return FALSE;
254
255 buf[len] = 0;
256
257 (*ptr) = buf;
258
259 return source->source_read (source, buf, len);
260}
261
262static gboolean
263source_next_bytes (SerialSource* source, const guint8 **ptr, guint32 *len_ptr)
264{
265 guint32 len;
266 guint8* buf;
267
268 if (! source->next_uint (source, &len))
269 return FALSE;
270
271 if (! (buf = serializeio_source_alloc (source, len)))
272 return FALSE;
273
274 (*len_ptr) = len;
275 (*ptr) = buf;
276
277 return source->source_read (source, buf, len);
278}
279
280static gboolean
281source_next_bytes_known (SerialSource* source, guint8 *ptr, guint32 len)
282{
283 return source->source_read (source, ptr, len);
284}
285
286void*
287serializeio_source_alloc (SerialSource* source, guint32 len)
288{
289 AllocList *al;
290 void* ret;
291
292 if (source->salloc_func)
293 {
294 ret = source->salloc_func (source, len);
295 al = source->salloc_func (source, sizeof(AllocList));
296 }
297 else
298 {
299 ret = g_malloc0 (len);
300 al = g_malloc0 (sizeof(AllocList));
301 }
302
303 al->ptr = ret;
304 al->next = source->alloc_list;
305 source->alloc_list = al;
306
307 return ret;
308}
309
310void
311serializeio_source_init (SerialSource* it,
312 SerialType (* source_type) (SerialSource* source,
313 gboolean set_allocation),
314 gboolean (* source_close) (SerialSource* source),
315 gboolean (* source_read) (SerialSource* source,
316 guint8 *ptr,
317 guint32 len),
318 void (* source_free) (SerialSource* source),
319 void* (* salloc_func) (SerialSource* source,
320 guint32 len),
321 void (* sfree_func) (SerialSource* source,
322 void* ptr))
323{
324 it->alloc_list = NULL;
325 it->next_bytes_known = source_next_bytes_known;
326 it->next_bytes = source_next_bytes;
327 it->next_uint = source_next_uint;
328 it->next_uint32 = source_next_uint32;
329 it->next_uint16 = source_next_uint16;
330 it->next_uint8 = source_next_uint8;
331 it->next_bool = source_next_bool;
332 it->next_string = source_next_string;
333
334 if (source_type != NULL)
335 it->source_type = source_type;
336 else
337 it->source_type = source_type_default;
338 it->source_close = source_close;
339 it->source_read = source_read;
340 it->source_free = source_free;
341 it->salloc_func = salloc_func;
342 it->sfree_func = sfree_func;
343}