aboutsummaryrefslogtreecommitdiff
path: root/src/pack.c
blob: 3998595c9db0a28c3388c45929d63f72ca9c583e (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/* This file is part of vmod-binlog
   Copyright (C) 2013-2014 Sergey Poznyakoff

   Vmod-binlog is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Vmod-binlog is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with vmod-binlog.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <arpa/inet.h>
#include <errno.h>
#include "pack.h"

/* Data format specification is a simplified form of Perl pack() template.
   It consists of a series of template letters optionally followed by
   numeric repeat count (which may be enclosed in square brackets).  The
   valid template letters are:
   
   Z  A null-terminated (ASCIZ) string of at most N-1 characters, will be
      null padded.  This letter must be followed by repeat count.

   c  A signed char (8-bit) value.

   s  A signed short (16-bit) value.
   S  An unsigned short value.

   l  A signed long (32-bit) value.
   L  An unsigned long value.

   q  A signed quad (64-bit) value.
   Q  An unsigned quad value.

   i  A signed integer value.
   I  A unsigned integer value.

   n  An unsigned short (16-bit) in "network" (big-endian) order.
   N  An unsigned long (32-bit) in "network" (big-endian) order.
   v  An unsigned short (16-bit) in "VAX" (little-endian) order.
   V  An unsigned long (32-bit) in "VAX" (little-endian) order.
   
   f  A single-precision float in native format.
   d  A double-precision float in native format.
   
   x  A null byte (a.k.a ASCII NUL, "\000", chr(0))
   X  Back up a byte.
   @  Null-fill or truncate to absolute position, counted from the
      current position.
   .  Null-fill or truncate to absolute position specified by
      the repeat count.
*/

#define F_REP 0x01

struct packspec {
	int ch;
	int flags;
	void (*packer)(struct packenv *, int);
	void (*unpacker)(struct packenv *, int);
};

struct packinst {
	struct packinst *next;
	struct packspec *spec;
	int rep;
	int cur;
};

static char *
packenv_get(struct packenv *env)
{
	if (env->argi == env->argc)
		return NULL;
	return env->argv[env->argi++];
}

static void
printunum(FILE *fp, uintmax_t num)
{
	char numbuf[512];
	char *p = numbuf + sizeof(numbuf);
	*--p = 0;
	do
		*--p = num % 10 + '0';
	while (num /= 10);
	fputs(p, fp);
}

static void
printsnum(FILE *fp, intmax_t num)
{
	if (num < 0) {
		fputc('-', fp);
		num = - num;
	}
	printunum(fp, num);
}

static int
getunum(char *s, uintmax_t maxval, uintmax_t *retval)
{
	uintmax_t x = 0;

	for (; *s && isspace(*s); s++);
	for (; *s; s++) {
		if (!isdigit(*s)) {
			errno = EINVAL;
			return -1;
		}
		x = x*10 + *s - '0';
	}
	if (x > maxval) {
		errno = ERANGE;
		return -1;
	}
	*retval = x;
	return 0;
}

static int
getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval)
{
	uintmax_t x = 0;
	int neg = 0;
	
	for (; *s && isspace(*s); s++);
	if (*s == '-') {
		neg = 1;
		++s;
	}
	for (; *s; s++) {
		if (!isdigit(*s)) {
			errno = EINVAL;
			return -1;
		}
		x = x*10 + *s - '0';
	}

	if (neg) {
		if (x > -minval) {
			errno = ERANGE;
			return -1;
		}
		*retval = -x;
	} else if (x > maxval) {
		errno = ERANGE;
		return -1;
	} else
		*retval = x;
	return 0;
}

#define GETSARG(env, type, minv, maxv) do {				\
	if ((env)->buf_base) {					        \
		char *arg = packenv_get(env);				\
		if (arg) {						\
			intmax_t x;					\
			if (getsnum(arg, minv, maxv, &x) == 0) {	\
				type v = (type) x;			\
				memcpy(env->buf_base + env->buf_pos,	\
				       &v, sizeof(v));			\
			} else						\
				packerror("error converting %s to %s: %s", \
					  arg, #type, strerror(errno));	\
		} else							\
			packerror("out of arguments");			\
	}								\
 } while(0)

#define GETUARG(env, type, maxv) do {					\
	if ((env)->buf_base) {						\
		char *arg = packenv_get(env);				\
		if (arg) {						\
			uintmax_t x;					\
			if (getunum(arg, maxv, &x) == 0) {		\
				type v = (type) x;			\
				memcpy(env->buf_base + env->buf_pos,	\
				       &v, sizeof(v));			\
			} else						\
				packerror("error converting %s to %s: %s", \
					  arg, #type, strerror(errno));	\
		} else							\
			packerror("out of arguments");			\
        }								\
 } while(0)


static void
Z_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);

		memset(env->buf_base + env->buf_pos, 0, rep);
		if (arg) {
			size_t len = strlen(arg);
			if (len > rep - 1)
				len = rep - 1;
			memcpy(env->buf_base + env->buf_pos, arg, len);
		} else
			packerror("out of arguments");
	}
	env->buf_pos += rep;
}
		
static void
Z_unpacker(struct packenv *env, int rep)
{
	fprintf(env->fp, "%-*.*s", rep-1, rep-1, env->buf_base + env->buf_pos);
 	env->buf_pos += rep;
}

static void
c_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg)
			env->buf_base[env->buf_pos] = *arg;
		else
			packerror("out of arguments");
	}
	env->buf_pos++;
}

static void
c_unpacker(struct packenv *env, int rep)
{
	fprintf(env->fp, "%c", env->buf_base[env->buf_pos++]);
}

static void
s_packer(struct packenv *env, int rep)
{
	GETSARG(env, int16_t, INT16_MIN, INT16_MAX);
	env->buf_pos += sizeof(int16_t);
}

static void
s_unpacker(struct packenv *env, int rep)
{
	printsnum(env->fp, *(int16_t *) (env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(int16_t);
}

static void
S_packer(struct packenv *env, int rep)
{
	GETUARG(env, uint16_t, UINT16_MAX);
	env->buf_pos += sizeof(uint16_t);
}

static void
S_unpacker(struct packenv *env, int rep)
{
	printunum(env->fp, *(uint16_t *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(uint16_t);
}

static void
l_packer(struct packenv *env, int rep)
{
	GETSARG(env, int32_t, INT32_MIN, INT32_MAX);
	env->buf_pos += sizeof(int32_t);
}

static void
l_unpacker(struct packenv *env, int rep)
{
	printsnum(env->fp, *(int32_t *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(int32_t);
}

static void
L_packer(struct packenv *env, int rep)
{
	GETUARG(env, uint32_t, UINT32_MAX);
	env->buf_pos += sizeof(uint32_t);
}

static void
L_unpacker(struct packenv *env, int rep)
{
	printunum(env->fp, *(uint32_t *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(uint32_t);
}

static void
q_packer(struct packenv *env, int rep)
{
	GETSARG(env, int64_t, INT64_MIN, INT64_MAX);
	env->buf_pos += sizeof(int64_t);
}

static void
q_unpacker(struct packenv *env, int rep)
{
	printsnum(env->fp, *(int64_t *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(int64_t);
}

static void
Q_packer(struct packenv *env, int rep)
{
	GETUARG(env, uint64_t, UINT64_MAX);
	env->buf_pos += sizeof(uint64_t);
}

static void
Q_unpacker(struct packenv *env, int rep)
{
	printunum(env->fp, *(uint64_t *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(uint64_t);
}

static void
i_packer(struct packenv *env, int rep)
{
	GETSARG(env, int, INT_MIN, INT_MAX);
	env->buf_pos += sizeof(int);
}

static void
i_unpacker(struct packenv *env, int rep)
{
	printsnum(env->fp, *(int *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(int);
}

static void
I_packer(struct packenv *env, int rep)
{
	GETUARG(env, unsigned, UINT_MAX);
	env->buf_pos += sizeof(unsigned);
}

static void
I_unpacker(struct packenv *env, int rep)
{
	printunum(env->fp, *(unsigned *)(env->buf_base + env->buf_pos));
	env->buf_pos += sizeof(unsigned);
}

/* n - An unsigned short (16-bit) in "network" (big-endian) order. */
static void
n_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			uintmax_t x;
			if (getunum(arg, UINT16_MAX, &x) == 0) {
				uint16_t v = htons((uint16_t) x);
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
			} else
				packerror("error converting %s to %s: %s",
					  arg, "uint16_t (network order)",
					  strerror(errno));
		} else
			packerror("out of arguments");
        }
	env->buf_pos += sizeof(uint16_t);
}

static void
n_unpacker(struct packenv *env, int rep)
{
	uint16_t v = ntohs(*(uint16_t *)(env->buf_base + env->buf_pos));
	printunum(env->fp, v);
	env->buf_pos += sizeof(uint16_t);
}

/* N - An unsigned long (32-bit) in "network" (big-endian) order. */
static void
N_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			uintmax_t x;
			if (getunum(arg, UINT32_MAX, &x) == 0) {
				uint32_t v = htonl((uint32_t) x);
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
			} else
				packerror("error converting %s to %s: %s",
					  arg, "uint32_t (network order)",
					  strerror(errno));
		} else
			packerror("out of arguments");
        }
	env->buf_pos += sizeof(uint32_t);
}

static void
N_unpacker(struct packenv *env, int rep)
{
	uint32_t v = ntohl(*(uint32_t *)(env->buf_base + env->buf_pos));
	printunum(env->fp, v);
	env->buf_pos += sizeof(uint32_t);
}

/* v - An unsigned short (16-bit) in "VAX" (little-endian) order. */
static void
v_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			uintmax_t x;
			if (getunum(arg, UINT16_MAX, &x) == 0) {
				uint16_t v = ntohs((uint16_t) x);
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
			} else
				packerror("error converting %s to %s: %s",
					  arg, "uint16_t (host order)",
					  strerror(errno));
		} else
			packerror("out of arguments");
        }
	env->buf_pos += sizeof(uint16_t);
}

static void
v_unpacker(struct packenv *env, int rep)
{
	uint16_t v = htons(*(uint16_t *)(env->buf_base + env->buf_pos));
	printunum(env->fp, v);
	env->buf_pos += sizeof(uint16_t);
}

/* V - An unsigned long (32-bit) in "VAX" (little-endian) order. */
static void
V_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			uintmax_t x;
			if (getunum(arg, UINT32_MAX, &x) == 0) {
				uint32_t v = ntohl((uint32_t) x);
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
			} else
				packerror("error converting %s to %s: %s",
					  arg, "uint32_t (host order)",
					  strerror(errno));
		} else
			packerror("out of arguments");
        }
	env->buf_pos += sizeof(uint32_t);
}

static void
V_unpacker(struct packenv *env, int rep)
{
	uint32_t v = htonl(*(uint32_t *)(env->buf_base + env->buf_pos));
	printunum(env->fp, v);
	env->buf_pos += sizeof(uint32_t);
}

/* f - A single-precision float in native format. */
static void
f_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			float v;
			char *p;
			
			errno = 0;
			v = strtof(arg, &p);
			if (*p)
				packerror("error converting %s to %s (near %s)",
					  arg, "float", p);
			else if (errno)
				packerror("error converting %s to %s: %s",
					  arg, "float", strerror(errno));
			else
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
		}
	}
	env->buf_pos += sizeof(float);
}

static void
f_unpacker(struct packenv *env, int rep)
{
	float v = *(float *)(env->buf_base + env->buf_pos);
	fprintf(env->fp, "%f", v); /* FIXME: Format? */
	env->buf_pos += sizeof(uint32_t);
}

/* d - A double-precision float in native format. */
static void
d_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		char *arg = packenv_get(env);
		if (arg) {
			double v;
			char *p;
			
			errno = 0;
			v = strtod(arg, &p);
			if (*p)
				packerror("error converting %s to %s (near %s)",
					  arg, "double", p);
			else if (errno)
				packerror("error converting %s to %s: %s",
					  arg, "double", strerror(errno));
			else
				memcpy(env->buf_base + env->buf_pos,
				       &v, sizeof(v));
		}
	}
	env->buf_pos += sizeof(double);
}

static void
d_unpacker(struct packenv *env, int rep)
{
	double v = *(double *)(env->buf_base + env->buf_pos);
	fprintf(env->fp, "%g", v); /* FIXME: Format? */
	env->buf_pos += sizeof(double);
}

static void
x_packer(struct packenv *env, int rep)
{
	if (env->buf_base)
		env->buf_base[env->buf_pos] = 0;
	env->buf_pos++;
}

static void
x_unpacker(struct packenv *env, int rep)
{
	fputc(0, env->fp);
}

static void
X_packer(struct packenv *env, int rep)
{
	if (env->buf_pos)
		--env->buf_pos;
}

static void
at_packer(struct packenv *env, int rep)
{
	if (env->buf_base)
		memset(env->buf_base + env->buf_pos, 0, rep);
	env->buf_pos += rep;
}

static void
at_unpacker(struct packenv *env, int rep)
{
	env->buf_pos += rep;
}

static void
dot_packer(struct packenv *env, int rep)
{
	if (env->buf_base) {
		if (rep < env->buf_pos)
			memset(env->buf_base + rep, 0, env->buf_pos - rep);
		else
			memset(env->buf_base + env->buf_pos, 0,
			       rep - env->buf_pos);
	}
	env->buf_pos = rep;
}

static void
dot_unpacker(struct packenv *env, int rep)
{
	env->buf_pos = rep;
}

static struct packspec packspec[] = {
	{ 'Z', F_REP, Z_packer, Z_unpacker },
	{ 'c', 0, c_packer, c_unpacker },
	{ 's', 0, s_packer, s_unpacker },
	{ 'S', 0, S_packer, S_unpacker },
	{ 'l', 0, l_packer, l_unpacker },
	{ 'L', 0, L_packer, L_unpacker },
	{ 'q', 0, q_packer, q_unpacker },
	{ 'Q', 0, Q_packer, Q_unpacker },
	{ 'i', 0, i_packer, i_unpacker },
	{ 'I', 0, I_packer, I_unpacker },
	{ 'n', 0, n_packer, n_unpacker },
	{ 'N', 0, N_packer, N_unpacker },
	{ 'v', 0, v_packer, v_unpacker },
	{ 'V', 0, V_packer, V_unpacker },
	{ 'f', 0, f_packer, f_unpacker },
	{ 'd', 0, d_packer, d_unpacker },

	{ 'x', 0, x_packer, x_unpacker },
	{ 'X', 0, X_packer, X_packer },
	{ '@', F_REP, at_packer, at_unpacker },
	{ '.', F_REP, dot_packer, dot_unpacker },
	
	{ 0 }
};

static int
getrep(const char *s, char const **endp, int *rep)
{
	int n;
	char *p;

	if (*s == '[') {
		if (s[1]) {
			if (isdigit(s[1])) {
				n = strtol(s + 1, &p, 10);
				if (n <= 0) {
					*endp = s;
					return -1;
				} else if (*p != ']') {
					*endp = p;
					return -1;
				}
				*rep = n;
				*endp = p + 1;
			} else {
				*endp = s;
				return -1;
			}
		} else {
			*endp = s;
			return -1;
		}
	} else if (isdigit(*s)) {
		n = strtol(s, &p, 10);
		if (n <= 0) {
			*endp = s;
			return -1;
		}
		*rep = n;
		*endp = p;
	} else {
		*rep = 1;
		*endp = s;
	}
	return 0;
}

struct packinst *
packcomp(const char *s, char **endp)
{
	struct packinst *head = NULL, *tail = NULL, *pi;
	struct packspec *ps;
	int rep;
	int ec = 0;
	
	errno = 0;
	while (*s) {
		if (isspace(*s)) {
			++s;
			continue;
		}
		for (ps = packspec; ps->ch; ps++)
			if (ps->ch == *s)
				break;
		if (!ps->ch) {
			ec = EINVAL;
			break;
		}
		if (getrep(s + 1, &s, &rep)) {
			ec = EINVAL;
			break;
		}
		pi = malloc(sizeof(*pi));
		if (!pi) {
			ec = ENOMEM;
			break;
		}
		pi->next = NULL;
		pi->spec = ps;
		pi->rep = rep;
		if (tail)
			tail->next = pi;
		else
			head = pi;
		tail = pi; 
	}
	if (ec) {
		packfree(head);
		head = NULL;
		errno = ec;
	}
	if (endp)
		*endp = (char*) s;
	return head;
}

struct packinst *
packdup(struct packinst *src)
{
	struct packinst *head = NULL, *tail = NULL, *p;

	for (; src; src = src->next) {
		p = malloc(sizeof(*p));
		if (!p) {
			packfree(head);
			errno = ENOMEM;
			return NULL;
		}
		p->next = NULL;
		p->spec = src->spec;
		p->rep = src->rep;
		p->cur = src->cur;
		if (tail)
			tail->next = p;
		else
			head = p;
		tail = p;
	}
	return head;
}
			
void
packfree(struct packinst *pi)
{
	while (pi) {
		struct packinst *next = pi->next;
		free(pi);
		pi = next;
	}
}

void
packin(struct packinst *pi, struct packenv *env)
{
	int i;
	
	for (; pi; pi = pi->next) {
		if (pi->spec->flags & F_REP)
			pi->spec->packer(env, pi->rep);
		else
			for (i = 0; i < pi->rep; i++)
				pi->spec->packer(env, 1);
	}
}

struct packinst *
packinnext(struct packinst *pi, struct packenv *env)
{
	if (!pi)
		return NULL;
	if (pi->spec->flags & F_REP)
		pi->spec->packer(env, pi->rep);
	else {
		pi->spec->packer(env, 1);
		if (++pi->cur < pi->rep)
			return pi;
	}
	return packinit(pi->next);
}

void
packout(struct packinst *pi, struct packenv *env)
{
	int i;

	for (; pi; pi = pi->next) {
		if (pi->spec->flags & F_REP)
			pi->spec->unpacker(env, pi->rep);
		else
			for (i = 0; i < pi->rep; i++) {
				if (i > 0)
					fputc(' ', env->fp);
				pi->spec->unpacker(env, 1);
			}
		if (pi->next)
			fputc(' ', env->fp);
	}
}

size_t
packsize(struct packinst *pi)
{
	struct packenv env;

	memset(&env, 0, sizeof env);
	packin(pi, &env);
	return env.buf_pos;
}

struct packinst *
packinit(struct packinst *inst)
{
	if (inst)
		inst->cur = 0;
	return inst;
}

struct packenv *
packenv_create(size_t size)
{
	struct packenv *env = calloc(1, sizeof(*env));
	if (env) {
		env->buf_base = calloc(1, size);
		if (!env->buf_base) {
			free(env);
			return NULL;
		}
		env->buf_size = size;
	}
	return env;
}

void
packenv_free(struct packenv *env)
{
	if (!env)
		return;
	free(env->buf_base);
	free(env);
}

void
packenv_init(struct packenv *env)
{
	memset(env->buf_base, 0, env->buf_size);
	env->buf_pos = 0;
}

Return to:

Send suggestions and report system problems to the System administrator.