xref: /netbsd-src/external/bsd/less/dist/xbuf.c (revision e4a6e799a67c2028562d75b4e61407b22434aa36)
1*e4a6e799Ssimonb #include "less.h"
2*e4a6e799Ssimonb #include "xbuf.h"
3*e4a6e799Ssimonb 
4*e4a6e799Ssimonb /*
5*e4a6e799Ssimonb  * Initialize an expandable text buffer.
6*e4a6e799Ssimonb  */
xbuf_init(struct xbuffer * xbuf)7*e4a6e799Ssimonb public void xbuf_init(struct xbuffer *xbuf)
8*e4a6e799Ssimonb {
9*e4a6e799Ssimonb 	xbuf->data = NULL;
10*e4a6e799Ssimonb 	xbuf->size = xbuf->end = 0;
11*e4a6e799Ssimonb }
12*e4a6e799Ssimonb 
xbuf_deinit(struct xbuffer * xbuf)13*e4a6e799Ssimonb public void xbuf_deinit(struct xbuffer *xbuf)
14*e4a6e799Ssimonb {
15*e4a6e799Ssimonb 	if (xbuf->data != NULL)
16*e4a6e799Ssimonb 		free(xbuf->data);
17*e4a6e799Ssimonb 	xbuf_init(xbuf);
18*e4a6e799Ssimonb }
19*e4a6e799Ssimonb 
xbuf_reset(struct xbuffer * xbuf)20*e4a6e799Ssimonb public void xbuf_reset(struct xbuffer *xbuf)
21*e4a6e799Ssimonb {
22*e4a6e799Ssimonb 	xbuf->end = 0;
23*e4a6e799Ssimonb }
24*e4a6e799Ssimonb 
25*e4a6e799Ssimonb /*
26*e4a6e799Ssimonb  * Add a byte to an expandable text buffer.
27*e4a6e799Ssimonb  */
xbuf_add_byte(struct xbuffer * xbuf,unsigned char b)28*e4a6e799Ssimonb public void xbuf_add_byte(struct xbuffer *xbuf, unsigned char b)
29*e4a6e799Ssimonb {
30*e4a6e799Ssimonb 	if (xbuf->end >= xbuf->size)
31*e4a6e799Ssimonb 	{
32*e4a6e799Ssimonb 		unsigned char *data;
33*e4a6e799Ssimonb 		if (ckd_add(&xbuf->size, xbuf->size, xbuf->size ? xbuf->size : 16))
34*e4a6e799Ssimonb 			out_of_memory();
35*e4a6e799Ssimonb 		data = (unsigned char *) ecalloc(xbuf->size, sizeof(unsigned char));
36*e4a6e799Ssimonb 		if (xbuf->data != NULL)
37*e4a6e799Ssimonb 		{
38*e4a6e799Ssimonb 			memcpy(data, xbuf->data, xbuf->end);
39*e4a6e799Ssimonb 			free(xbuf->data);
40*e4a6e799Ssimonb 		}
41*e4a6e799Ssimonb 		xbuf->data = data;
42*e4a6e799Ssimonb 	}
43*e4a6e799Ssimonb 	xbuf->data[xbuf->end++] = (unsigned char) b;
44*e4a6e799Ssimonb }
45*e4a6e799Ssimonb 
xbuf_add_data(struct xbuffer * xbuf,unsigned char * data,int len)46*e4a6e799Ssimonb public void xbuf_add_data(struct xbuffer *xbuf, unsigned char *data, int len)
47*e4a6e799Ssimonb {
48*e4a6e799Ssimonb 	int i;
49*e4a6e799Ssimonb 	for (i = 0;  i < len;  i++)
50*e4a6e799Ssimonb 		xbuf_add_byte(xbuf, data[i]);
51*e4a6e799Ssimonb }
52*e4a6e799Ssimonb 
xbuf_pop(struct xbuffer * buf)53*e4a6e799Ssimonb public int xbuf_pop(struct xbuffer *buf)
54*e4a6e799Ssimonb {
55*e4a6e799Ssimonb 	if (buf->end == 0)
56*e4a6e799Ssimonb 		return -1;
57*e4a6e799Ssimonb 	return (int) buf->data[--(buf->end)];
58*e4a6e799Ssimonb }
59*e4a6e799Ssimonb 
xbuf_set(struct xbuffer * dst,struct xbuffer * src)60*e4a6e799Ssimonb public void xbuf_set(struct xbuffer *dst, struct xbuffer *src)
61*e4a6e799Ssimonb {
62*e4a6e799Ssimonb 	xbuf_reset(dst);
63*e4a6e799Ssimonb 	xbuf_add_data(dst, src->data, src->end);
64*e4a6e799Ssimonb }
65*e4a6e799Ssimonb 
xbuf_char_data(struct xbuffer * xbuf)66*e4a6e799Ssimonb public char * xbuf_char_data(struct xbuffer *xbuf)
67*e4a6e799Ssimonb {
68*e4a6e799Ssimonb 	return (char *)(xbuf->data);
69*e4a6e799Ssimonb }
70*e4a6e799Ssimonb 
71*e4a6e799Ssimonb 
72*e4a6e799Ssimonb /*
73*e4a6e799Ssimonb  * Helper functions for the ckd_add and ckd_mul macro substitutes.
74*e4a6e799Ssimonb  * These helper functions do not set *R on overflow, and assume that
75*e4a6e799Ssimonb  * arguments are nonnegative, that INTMAX_MAX <= UINTMAX_MAX, and that
76*e4a6e799Ssimonb  * sizeof is a reliable way to distinguish integer representations.
77*e4a6e799Ssimonb  * Despite these limitations they are good enough for 'less' on all
78*e4a6e799Ssimonb  * known practical platforms.  For more-complicated substitutes
79*e4a6e799Ssimonb  * without most of these limitations, see Gnulib's stdckdint module.
80*e4a6e799Ssimonb  */
81*e4a6e799Ssimonb #if !HAVE_STDCKDINT_H
82*e4a6e799Ssimonb /*
83*e4a6e799Ssimonb  * If the integer *R can represent VAL, store the value and return FALSE.
84*e4a6e799Ssimonb  * Otherwise, possibly set *R to an indeterminate value and return TRUE.
85*e4a6e799Ssimonb  * R has size RSIZE, and is signed if and only if RSIGNED is nonzero.
86*e4a6e799Ssimonb  */
help_fixup(void * r,uintmax val,int rsize,int rsigned)87*e4a6e799Ssimonb static int help_fixup(void *r, uintmax val, int rsize, int rsigned)
88*e4a6e799Ssimonb {
89*e4a6e799Ssimonb 	if (rsigned)
90*e4a6e799Ssimonb 	{
91*e4a6e799Ssimonb 		if (rsize == sizeof (int))
92*e4a6e799Ssimonb 		{
93*e4a6e799Ssimonb 			int *pr = r;
94*e4a6e799Ssimonb 			if (INT_MAX < val)
95*e4a6e799Ssimonb 				return TRUE;
96*e4a6e799Ssimonb 			*pr = (int) val;
97*e4a6e799Ssimonb #ifdef LLONG_MAX
98*e4a6e799Ssimonb 		} else if (rsize == sizeof (long long))
99*e4a6e799Ssimonb 		{
100*e4a6e799Ssimonb 			long long *pr = r;
101*e4a6e799Ssimonb 			if (LLONG_MAX < val)
102*e4a6e799Ssimonb 				return TRUE;
103*e4a6e799Ssimonb 			*pr = val;
104*e4a6e799Ssimonb #endif
105*e4a6e799Ssimonb #ifdef INTMAX_MAX
106*e4a6e799Ssimonb 		} else if (rsize == sizeof (intmax_t)) {
107*e4a6e799Ssimonb 			intmax_t *pr = r;
108*e4a6e799Ssimonb 			if (INTMAX_MAX < val)
109*e4a6e799Ssimonb 				return TRUE;
110*e4a6e799Ssimonb 			*pr = val;
111*e4a6e799Ssimonb #endif
112*e4a6e799Ssimonb 		} else /* rsize == sizeof (long) */
113*e4a6e799Ssimonb 		{
114*e4a6e799Ssimonb 			long *pr = r;
115*e4a6e799Ssimonb 			if (LONG_MAX < val)
116*e4a6e799Ssimonb 				return TRUE;
117*e4a6e799Ssimonb 			*pr = (long) val;
118*e4a6e799Ssimonb 		}
119*e4a6e799Ssimonb 	} else {
120*e4a6e799Ssimonb 		if (rsize == sizeof (unsigned)) {
121*e4a6e799Ssimonb 			unsigned *pr = r;
122*e4a6e799Ssimonb 			if (UINT_MAX < val)
123*e4a6e799Ssimonb 				return TRUE;
124*e4a6e799Ssimonb 			*pr = (unsigned) val;
125*e4a6e799Ssimonb 		} else if (rsize == sizeof (unsigned long)) {
126*e4a6e799Ssimonb 			unsigned long *pr = r;
127*e4a6e799Ssimonb 			if (ULONG_MAX < val)
128*e4a6e799Ssimonb 				return TRUE;
129*e4a6e799Ssimonb 			*pr = (unsigned long) val;
130*e4a6e799Ssimonb #ifdef ULLONG_MAX
131*e4a6e799Ssimonb 		} else if (rsize == sizeof (unsigned long long)) {
132*e4a6e799Ssimonb 			long long *pr = r;
133*e4a6e799Ssimonb 			if (ULLONG_MAX < val)
134*e4a6e799Ssimonb 				return TRUE;
135*e4a6e799Ssimonb 			*pr = val;
136*e4a6e799Ssimonb #endif
137*e4a6e799Ssimonb 		} else /* rsize == sizeof (uintmax) */
138*e4a6e799Ssimonb 		{
139*e4a6e799Ssimonb 			uintmax *pr = r;
140*e4a6e799Ssimonb 			*pr = val;
141*e4a6e799Ssimonb 		}
142*e4a6e799Ssimonb 	}
143*e4a6e799Ssimonb 	return FALSE;
144*e4a6e799Ssimonb }
145*e4a6e799Ssimonb /*
146*e4a6e799Ssimonb  * If *R can represent the mathematical sum of A and B, store the sum
147*e4a6e799Ssimonb  * and return FALSE.  Otherwise, possibly set *R to an indeterminate
148*e4a6e799Ssimonb  * value and return TRUE.  R has size RSIZE, and is signed if and only
149*e4a6e799Ssimonb  * if RSIGNED is nonzero.
150*e4a6e799Ssimonb  */
help_ckd_add(void * r,uintmax a,uintmax b,int rsize,int rsigned)151*e4a6e799Ssimonb public int help_ckd_add(void *r, uintmax a, uintmax b, int rsize, int rsigned)
152*e4a6e799Ssimonb {
153*e4a6e799Ssimonb 	uintmax sum = a + b;
154*e4a6e799Ssimonb 	return sum < a || help_fixup(r, sum, rsize, rsigned);
155*e4a6e799Ssimonb }
156*e4a6e799Ssimonb /* Likewise, but for the product of A and B.  */
help_ckd_mul(void * r,uintmax a,uintmax b,int rsize,int rsigned)157*e4a6e799Ssimonb public int help_ckd_mul(void *r, uintmax a, uintmax b, int rsize, int rsigned)
158*e4a6e799Ssimonb {
159*e4a6e799Ssimonb 	uintmax product = a * b;
160*e4a6e799Ssimonb 	return ((b != 0 && a != product / b)
161*e4a6e799Ssimonb 		|| help_fixup(r, product, rsize, rsigned));
162*e4a6e799Ssimonb }
163*e4a6e799Ssimonb #endif
164