summaryrefslogtreecommitdiffstats
path: root/libdnet-stripped/src/blob.c
blob: 57ff0c3af2d10881ca4d3546fa740d3a49e63982 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * blob.c
 *
 * Copyright (c) 2002 Dug Song <dugsong@monkey.org>
 *
 * $Id: blob.c 615 2006-01-08 16:06:49Z dugsong $
 */

#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dnet.h"

static void	*(*bl_malloc)(size_t) = malloc;
static void	*(*bl_realloc)(void *, size_t) = realloc;
static void	 (*bl_free)(void *) = free;
static int	   bl_size = BUFSIZ;

static int	   fmt_D(int, int, blob_t *, va_list *);
static int	   fmt_H(int, int, blob_t *, va_list *);
static int	   fmt_b(int, int, blob_t *, va_list *);
static int	   fmt_c(int, int, blob_t *, va_list *);
static int	   fmt_d(int, int, blob_t *, va_list *);
static int	   fmt_h(int, int, blob_t *, va_list *);
static int	   fmt_s(int, int, blob_t *, va_list *);

static void	   print_hexl(blob_t *);

static blob_fmt_cb blob_ascii_fmt[] = {
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	fmt_D,	NULL,	NULL,	NULL,
	fmt_H,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	fmt_b,	fmt_c,	fmt_d,	NULL,	NULL,	NULL,
	fmt_h,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	fmt_s,	NULL,	NULL,	NULL,	NULL,
	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL
};

struct blob_printer {
	char	  *name;
	void	 (*print)(blob_t *);
} blob_printers[] = {
	{ "hexl",	print_hexl },
	{ NULL,		NULL },
};

blob_t *
blob_new(void)
{
	blob_t *b;

	if ((b = bl_malloc(sizeof(*b))) != NULL) {
		b->off = b->end = 0;
		b->size = bl_size;
		if ((b->base = bl_malloc(b->size)) == NULL) {
			bl_free(b);
			b = NULL;
		}
	}
	return (b);
}

static int
blob_reserve(blob_t *b, int len)
{
	void *p;
	int nsize;

	if (b->size < b->end + len) {
		if (b->size == 0)
			return (-1);

		if ((nsize = b->end + len) > bl_size)
			nsize = ((nsize / bl_size) + 1) * bl_size;
		
		if ((p = bl_realloc(b->base, nsize)) == NULL)
			return (-1);
		
		b->base = p;
		b->size = nsize;
	}
	b->end += len;
	
	return (0);
}

int
blob_read(blob_t *b, void *buf, int len)
{
	if (b->end - b->off < len)
		len = b->end - b->off;
	
	memcpy(buf, b->base + b->off, len);
	b->off += len;
	
	return (len);
}

int
blob_write(blob_t *b, const void *buf, int len)
{
	if (b->off + len <= b->end ||
	    blob_reserve(b, b->off + len - b->end) == 0) {
		memcpy(b->base + b->off, (u_char *)buf, len);
		b->off += len;
		return (len);
	}
	return (-1);
}

int
blob_insert(blob_t *b, const void *buf, int len)
{
	if (blob_reserve(b, len) == 0 && b->size) {
		if (b->end - b->off > 0)
			memmove( b->base + b->off + len, b->base + b->off, b->end - b->off);
		memcpy(b->base + b->off, buf, len);
		b->off += len;
		return (len);
	}
	return (-1);
}

int
blob_delete(blob_t *b, void *buf, int len)
{
	if (b->off + len <= b->end && b->size) {
		if (buf != NULL)
			memcpy(buf, b->base + b->off, len);
		memmove(b->base + b->off, b->base + b->off + len, b->end - (b->off + len));
		b->end -= len;
		return (len);
	}
	return (-1);
}

static int
blob_fmt(blob_t *b, int pack, const char *fmt, va_list *ap)
{
	blob_fmt_cb fmt_cb;
	char *p;
	int len;

	for (p = (char *)fmt; *p != '\0'; p++) {
		if (*p == '%') {
			p++;
			if (isdigit((int) (unsigned char) *p)) {
				len = strtol(p, &p, 10);
			} else if (*p == '*') {
				len = va_arg(*ap, int);
				p++;
			} else
				len = 0;
			
			if ((fmt_cb = blob_ascii_fmt[(int)*p]) == NULL)
				return (-1);

			if ((*fmt_cb)(pack, len, b, ap) < 0)
				return (-1);
		} else {
			if (pack) {
				if (b->off + 1 < b->end ||
				    blob_reserve(b, b->off + 1 - b->end) == 0)
					b->base[b->off++] = *p;
				else
					return (-1);
			} else {
				if (b->base[b->off++] != *p)
					return (-1);
			}
		}
	}
	return (0);
}

int
blob_pack(blob_t *b, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	return (blob_fmt(b, 1, fmt, &ap));
}

int
blob_unpack(blob_t *b, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	return (blob_fmt(b, 0, fmt, &ap));
}

int
blob_seek(blob_t *b, int off, int whence)
{
	if (whence == SEEK_CUR)
		off += b->off;
	else if (whence == SEEK_END)
		off += b->end;

	if (off < 0 || off > b->end)
		return (-1);
	
	return ((b->off = off));
}

int
blob_index(blob_t *b, const void *buf, int len)
{
	int i;

	for (i = b->off; i <= b->end - len; i++) {
		if (memcmp(b->base + i, buf, len) == 0)
			return (i);
	}
	return (-1);
}

int
blob_rindex(blob_t *b, const void *buf, int len)
{
	int i;

	for (i = b->end - len; i >= 0; i--) {
		if (memcmp(b->base + i, buf, len) == 0)
			return (i);
	}
	return (-1);
}

int
blob_print(blob_t *b, char *style, int len)
{
	struct blob_printer *bp;

	for (bp = blob_printers; bp->name != NULL; bp++) {
		if (strcmp(bp->name, style) == 0)
			bp->print(b);
	}
	return (0);
}

int
blob_sprint(blob_t *b, char *style, int len, char *dst, int size)
{
	return (0);
}

blob_t *
blob_free(blob_t *b)
{
	if (b->size)
		bl_free(b->base);
	bl_free(b);
	return (NULL);
}

int
blob_register_alloc(size_t size, void *(bmalloc)(size_t),
    void (*bfree)(void *), void *(*brealloc)(void *, size_t))
{
	bl_size = size;
	if (bmalloc != NULL)
		bl_malloc = bmalloc;
	if (bfree != NULL)
		bl_free = bfree;
	if (brealloc != NULL)
		bl_realloc = brealloc;
	return (0);
}

int
blob_register_pack(char c, blob_fmt_cb fmt_cb)
{
	if (blob_ascii_fmt[(int)c] == NULL) {
		blob_ascii_fmt[(int)c] = fmt_cb;
		return (0);
	}
	return (-1);
}

static int
fmt_D(int pack, int len, blob_t *b, va_list *ap)
{
	if (len) return (-1);
	
	if (pack) {
		uint32_t n = va_arg(*ap, uint32_t);
		n = htonl(n);
		if (blob_write(b, &n, sizeof(n)) < 0)
			return (-1);
	} else {
		uint32_t *n = va_arg(*ap, uint32_t *);
		if (blob_read(b, n, sizeof(*n)) != sizeof(*n))
			return (-1);
		*n = ntohl(*n);
	}
	return (0);
}

static int
fmt_H(int pack, int len, blob_t *b, va_list *ap)
{
	if (len) return (-1);
	
	if (pack) {
		uint16_t n = va_arg(*ap, int);
		n = htons(n);
		if (blob_write(b, &n, sizeof(n)) < 0)
			return (-1);
	} else {
		uint16_t *n = va_arg(*ap, uint16_t *);
		if (blob_read(b, n, sizeof(*n)) != sizeof(*n))
			return (-1);
		*n = ntohs(*n);
	}
	return (0);
}

static int
fmt_b(int pack, int len, blob_t *b, va_list *ap)
{
	void *p = va_arg(*ap, void *);
	
	if (len <= 0) return (-1);
	
	if (pack)
		return (blob_write(b, p, len));
	else
		return (blob_read(b, p, len));
}

static int
fmt_c(int pack, int len, blob_t *b, va_list *ap)
{
	if (len) return (-1);
	
	if (pack) {
		uint8_t n = va_arg(*ap, int);
		return (blob_write(b, &n, sizeof(n)));
	} else {
		uint8_t *n = va_arg(*ap, uint8_t *);
		return (blob_read(b, n, sizeof(*n)));
	}
}

static int
fmt_d(int pack, int len, blob_t *b, va_list *ap)
{
	if (len) return (-1);
	
	if (pack) {
		uint32_t n = va_arg(*ap, uint32_t);
		return (blob_write(b, &n, sizeof(n)));
	} else {
		uint32_t *n = va_arg(*ap, uint32_t *);
		return (blob_read(b, n, sizeof(*n)));
	}
}

static int
fmt_h(int pack, int len, blob_t *b, va_list *ap)
{
	if (len) return (-1);
	
	if (pack) {
		uint16_t n = va_arg(*ap, int);
		return (blob_write(b, &n, sizeof(n)));
	} else {
		uint16_t *n = va_arg(*ap, uint16_t *);
		return (blob_read(b, n, sizeof(*n)));
	}
}

static int
fmt_s(int pack, int len, blob_t *b, va_list *ap)
{
	char *p = va_arg(*ap, char *);
	char c = '\0';
	int i, end;
	
	if (pack) {
		if (len > 0) {
			if ((c = p[len - 1]) != '\0')
				p[len - 1] = '\0';
		} else
			len = strlen(p) + 1;
		
		if (blob_write(b, p, len) > 0) {
			if (c != '\0')
				p[len - 1] = c;
			return (len);
		}
	} else {
		if (len <= 0) return (-1);

		if ((end = b->end - b->off) < len)
			end = len;
		
		for (i = 0; i < end; i++) {
			if ((p[i] = b->base[b->off + i]) == '\0') {
				b->off += i + 1;
				return (i);
			}
		}
	}
	return (-1);
}

static void
print_hexl(blob_t *b)
{
	u_int i, j, jm, len;
	u_char *p;
	int c;

	p = b->base + b->off;
	len = b->end - b->off;
	
	printf("\n");
	
	for (i = 0; i < len; i += 0x10) {
		printf("  %04x: ", (u_int)(i + b->off));
		jm = len - i;
		jm = jm > 16 ? 16 : jm;
		
		for (j = 0; j < jm; j++) {
			printf((j % 2) ? "%02x " : "%02x", (u_int)p[i + j]);
		}
		for (; j < 16; j++) {
			printf((j % 2) ? "   " : "  ");
		}
		printf(" ");
		
		for (j = 0; j < jm; j++) {
			c = p[i + j];
			printf("%c", isprint(c) ? c : '.');
		}
		printf("\n");
	}
}