summaryrefslogtreecommitdiff
path: root/xdelta1/libedsio/default.c
blob: 737b07b060e7f0456bb0f3995372d2bff4d7ae19 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* -*-Mode: C;-*-
 * $Id: default.c 1.5 Wed, 31 Mar 1999 17:39:58 -0800 jmacd $
 *
 * Copyright (C) 1998, 1999, Josh MacDonald.
 * All Rights Reserved.
 *
 * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
 */

#include "edsio.h"

/* Default Sink methods
 */

static gboolean
sink_type_default (SerialSink* sink, SerialType type, guint len, gboolean set_allocation)
{
  if (! sink->next_uint32 (sink, type))
    return FALSE;

  if (set_allocation && !sink->next_uint32 (sink, len))
    return FALSE;

  return TRUE;
}

static gboolean
sink_next_uint16 (SerialSink* sink, guint16 num)
{
  num = g_htons (num);

  return sink->sink_write (sink, (guint8*) &num, sizeof (num));
}

static gboolean
sink_next_uint32 (SerialSink* sink, guint32 num)
{
  num = g_htonl (num);

  return sink->sink_write (sink, (guint8*) &num, sizeof (num));
}

static gboolean
sink_next_uint (SerialSink* sink, guint32 num)
{
  /* This is mostly because I dislike endian, and less to save space
   * on small ints.  However, the uint32 and uint16 functions are used
   * when the number is expected to be large, in which case this
   * format can expand the number. */

  guint8 sink_buf[16];  /* this is enough room for a 12-byte int */
  guint  sink_count = 0;

  do
    {
      guint left = num & 0x7f;
      guint outnum;

      num >>= 7;

      outnum = left | (num ? 0x80 : 0);

      sink_buf[sink_count++] = outnum;
    }
  while (num);

  return sink->sink_write (sink, sink_buf, sink_count);
}

static gboolean
sink_next_uint8 (SerialSink* sink, guint8 val)
{
  return sink->sink_write (sink, &val, 1);
}

static gboolean
sink_next_bool (SerialSink* sink, gboolean val)
{
  guint8 sink_buf[1];
  sink_buf[0] = val;
  return sink->sink_write (sink, sink_buf, 1);
}

static gboolean
sink_next_string (SerialSink* sink, const char   *ptr)
{
  return sink->next_bytes (sink, ptr, strlen (ptr));
}

static gboolean
sink_next_bytes (SerialSink* sink, const guint8   *ptr, guint32 len)
{
  return sink->next_uint (sink, len) &&
         sink->sink_write (sink, ptr, len);
}

static gboolean
sink_next_bytes_known (SerialSink* sink, const guint8   *ptr, guint32 len)
{
  return sink->sink_write (sink, ptr, len);
}

void
serializeio_sink_init (SerialSink* it,
		       gboolean (* sink_type) (SerialSink* sink,
					       SerialType type,
					       guint mem_size,
					       gboolean set_allocation),
		       gboolean (* sink_close) (SerialSink* sink),
		       gboolean (* sink_write) (SerialSink* sink,
						const guint8 *ptr,
						guint32 len),
		       void     (* sink_free) (SerialSink* sink),
		       gboolean (* sink_quantum) (SerialSink* sink))
{
  it->next_bytes_known = sink_next_bytes_known;
  it->next_bytes = sink_next_bytes;
  it->next_uint = sink_next_uint;
  it->next_uint32 = sink_next_uint32;
  it->next_uint16 = sink_next_uint16;
  it->next_uint8 = sink_next_uint8;
  it->next_bool = sink_next_bool;
  it->next_string = sink_next_string;

  if (sink_type)
    it->sink_type = sink_type;
  else
    it->sink_type = sink_type_default;

  it->sink_close = sink_close;
  it->sink_write = sink_write;
  it->sink_free = sink_free;
  it->sink_quantum = sink_quantum;
}

/* Default Source methods
 */

static SerialType
source_type_default (SerialSource* source, gboolean set_allocation)
{
  guint32 x;

  if (! source->next_uint32 (source, & x))
    return ST_Error;

  if (set_allocation)
    {
      if (! source->next_uint32 (source, &source->alloc_total))
	return ST_Error;
    }

  return x;
}

static gboolean
source_next_uint32 (SerialSource* source, guint32 *ptr)
{
  guint32 x;

  if (! source->source_read (source, (guint8*) &x, sizeof (x)))
    return FALSE;

  (*ptr) = g_ntohl (x);

  return TRUE;
}

static gboolean
source_next_uint16 (SerialSource* source, guint16 *ptr)
{
  guint16 x;

  if (! source->source_read (source, (guint8*) &x, sizeof (x)))
    return FALSE;

  (*ptr) = g_ntohs (x);

  return TRUE;
}

static gboolean
source_next_uint (SerialSource* source, guint32 *ptr)
{
  /* This is mostly because I dislike endian, and less to save space
   * on small ints */
  guint8 c;
  guint8 arr[16];
  gint i = 0;
  gint donebit = 1;
  gint bits;

  while (source->next_uint8 (source, &c))
    {
      donebit = c & 0x80;
      bits = c & 0x7f;

      arr[i++] = bits;

      if (!donebit)
	break;
    }

  if (donebit)
    return FALSE;

  *ptr = 0;

  for (i -= 1; i >= 0; i -= 1)
    {
      *ptr <<= 7;
      *ptr |= arr[i];
    }

  return TRUE;
}

static gboolean
source_next_uint8 (SerialSource* source, guint8 *ptr)
{
  return source->source_read (source, ptr, 1);
}

static gboolean
source_next_bool (SerialSource* source, gboolean *ptr)
{
  guint8 sink_buf[1];

  if (! source->source_read (source, sink_buf, 1))
    return FALSE;

  if (sink_buf[0])
    *ptr = TRUE;
  else
    *ptr = FALSE;

  return TRUE;
}

static gboolean
source_next_string (SerialSource* source, const char **ptr)
{
  guint32 len;
  guint8* buf;

  if (! source->next_uint (source, &len))
    return FALSE;

  if (! (buf = serializeio_source_alloc (source, len+1)))
    return FALSE;

  buf[len] = 0;

  (*ptr) = buf;

  return source->source_read (source, buf, len);
}

static gboolean
source_next_bytes (SerialSource* source, const guint8 **ptr, guint32 *len_ptr)
{
  guint32 len;
  guint8* buf;

  if (! source->next_uint (source, &len))
    return FALSE;

  if (! (buf = serializeio_source_alloc (source, len)))
    return FALSE;

  (*len_ptr) = len;
  (*ptr) = buf;

  return source->source_read (source, buf, len);
}

static gboolean
source_next_bytes_known (SerialSource* source, guint8 *ptr, guint32 len)
{
  return source->source_read (source, ptr, len);
}

void*
serializeio_source_alloc (SerialSource* source, guint32 len)
{
  void* ret;

  if (! source->alloc_buf)
    {
      if (source->salloc_func)
	source->alloc_buf_orig = source->salloc_func (source, source->alloc_total + 8);
      else
	source->alloc_buf_orig = g_malloc0 (source->alloc_total + 8);

      source->alloc_buf = source->alloc_buf_orig;

	  { long x = source->alloc_buf; ALIGN_8 (x); source->alloc_buf = x; }

    }

  if (len+source->alloc_pos > source->alloc_total)
    {
      edsio_generate_source_event (EC_EdsioIncorrectAllocation, source);
      return NULL;
    }

  ret = ((guint8*)source->alloc_buf) + source->alloc_pos;

  source->alloc_pos += len;

  ALIGN_8 (source->alloc_pos);

  g_assert (((long)ret) % 8 == 0);
  g_assert (source->alloc_pos % 8 == 0);

  return ret;
}

gboolean
serializeio_source_object_received (SerialSource* source)
{
  source->alloc_pos = 0;
  source->alloc_total = 0;
  source->alloc_buf_orig = NULL;
  source->alloc_buf = NULL;

  return TRUE;
}

void
serializeio_source_reset_allocation (SerialSource* source)
{
  source->alloc_pos = 0;
  source->alloc_total = 0;
  source->alloc_buf = NULL;

  if (source->alloc_buf_orig)
    {
      if (source->sfree_func)
	source->sfree_func (source, source->alloc_buf_orig);
      else
	g_free (source->alloc_buf_orig);
    }
}

void
serializeio_source_init (SerialSource* it,
			 SerialType (* source_type) (SerialSource* source,
						     gboolean set_allocation),
			 gboolean   (* source_close) (SerialSource* source),
			 gboolean   (* source_read) (SerialSource* source,
						     guint8 *ptr,
						     guint32 len),
			 void       (* source_free) (SerialSource* source),
			 void*      (* salloc_func) (SerialSource* source,
						     guint32       len),
			 void       (* sfree_func) (SerialSource* source,
						    void*         ptr))
{
  it->next_bytes_known = source_next_bytes_known;
  it->next_bytes = source_next_bytes;
  it->next_uint = source_next_uint;
  it->next_uint32 = source_next_uint32;
  it->next_uint16 = source_next_uint16;
  it->next_uint8 = source_next_uint8;
  it->next_bool = source_next_bool;
  it->next_string = source_next_string;

  if (source_type != NULL)
    it->source_type = source_type;
  else
    it->source_type = source_type_default;
  it->source_close = source_close;
  it->source_read = source_read;
  it->source_free = source_free;
  it->salloc_func = salloc_func;
  it->sfree_func = sfree_func;
}