xref: /dragonfly/contrib/less/xbuf.c (revision 320d7c8a)
10c7ad07eSAntonio Huete Jimenez #include "less.h"
20c7ad07eSAntonio Huete Jimenez #include "xbuf.h"
30c7ad07eSAntonio Huete Jimenez 
40c7ad07eSAntonio Huete Jimenez /*
50c7ad07eSAntonio Huete Jimenez  * Initialize an expandable text buffer.
60c7ad07eSAntonio Huete Jimenez  */
xbuf_init(struct xbuffer * xbuf)7*320d7c8aSAaron LI public void xbuf_init(struct xbuffer *xbuf)
80c7ad07eSAntonio Huete Jimenez {
90c7ad07eSAntonio Huete Jimenez 	xbuf->data = NULL;
100c7ad07eSAntonio Huete Jimenez 	xbuf->size = xbuf->end = 0;
110c7ad07eSAntonio Huete Jimenez }
120c7ad07eSAntonio Huete Jimenez 
xbuf_deinit(struct xbuffer * xbuf)13*320d7c8aSAaron LI public void xbuf_deinit(struct xbuffer *xbuf)
140c7ad07eSAntonio Huete Jimenez {
150c7ad07eSAntonio Huete Jimenez 	if (xbuf->data != NULL)
160c7ad07eSAntonio Huete Jimenez 		free(xbuf->data);
170c7ad07eSAntonio Huete Jimenez 	xbuf_init(xbuf);
180c7ad07eSAntonio Huete Jimenez }
190c7ad07eSAntonio Huete Jimenez 
xbuf_reset(struct xbuffer * xbuf)20*320d7c8aSAaron LI public void xbuf_reset(struct xbuffer *xbuf)
210c7ad07eSAntonio Huete Jimenez {
220c7ad07eSAntonio Huete Jimenez 	xbuf->end = 0;
230c7ad07eSAntonio Huete Jimenez }
240c7ad07eSAntonio Huete Jimenez 
250c7ad07eSAntonio Huete Jimenez /*
26*320d7c8aSAaron LI  * Add a byte to an expandable text buffer.
270c7ad07eSAntonio Huete Jimenez  */
xbuf_add_byte(struct xbuffer * xbuf,unsigned char b)28*320d7c8aSAaron LI public void xbuf_add_byte(struct xbuffer *xbuf, unsigned char b)
290c7ad07eSAntonio Huete Jimenez {
300c7ad07eSAntonio Huete Jimenez 	if (xbuf->end >= xbuf->size)
310c7ad07eSAntonio Huete Jimenez 	{
32*320d7c8aSAaron LI 		unsigned char *data;
33*320d7c8aSAaron LI 		if (ckd_add(&xbuf->size, xbuf->size, xbuf->size ? xbuf->size : 16))
34*320d7c8aSAaron LI 			out_of_memory();
35*320d7c8aSAaron LI 		data = (unsigned char *) ecalloc(xbuf->size, sizeof(unsigned char));
360c7ad07eSAntonio Huete Jimenez 		if (xbuf->data != NULL)
370c7ad07eSAntonio Huete Jimenez 		{
380c7ad07eSAntonio Huete Jimenez 			memcpy(data, xbuf->data, xbuf->end);
390c7ad07eSAntonio Huete Jimenez 			free(xbuf->data);
400c7ad07eSAntonio Huete Jimenez 		}
410c7ad07eSAntonio Huete Jimenez 		xbuf->data = data;
420c7ad07eSAntonio Huete Jimenez 	}
43*320d7c8aSAaron LI 	xbuf->data[xbuf->end++] = (unsigned char) b;
440c7ad07eSAntonio Huete Jimenez }
450c7ad07eSAntonio Huete Jimenez 
xbuf_add_data(struct xbuffer * xbuf,unsigned char * data,int len)46*320d7c8aSAaron LI public void xbuf_add_data(struct xbuffer *xbuf, unsigned char *data, int len)
47*320d7c8aSAaron LI {
48*320d7c8aSAaron LI 	int i;
49*320d7c8aSAaron LI 	for (i = 0;  i < len;  i++)
50*320d7c8aSAaron LI 		xbuf_add_byte(xbuf, data[i]);
51*320d7c8aSAaron LI }
52*320d7c8aSAaron LI 
xbuf_pop(struct xbuffer * buf)53*320d7c8aSAaron LI public int xbuf_pop(struct xbuffer *buf)
540c7ad07eSAntonio Huete Jimenez {
550c7ad07eSAntonio Huete Jimenez 	if (buf->end == 0)
560c7ad07eSAntonio Huete Jimenez 		return -1;
57*320d7c8aSAaron LI 	return (int) buf->data[--(buf->end)];
580c7ad07eSAntonio Huete Jimenez }
590c7ad07eSAntonio Huete Jimenez 
xbuf_set(struct xbuffer * dst,struct xbuffer * src)60*320d7c8aSAaron LI public void xbuf_set(struct xbuffer *dst, struct xbuffer *src)
610c7ad07eSAntonio Huete Jimenez {
620c7ad07eSAntonio Huete Jimenez 	xbuf_reset(dst);
63*320d7c8aSAaron LI 	xbuf_add_data(dst, src->data, src->end);
640c7ad07eSAntonio Huete Jimenez }
65*320d7c8aSAaron LI 
xbuf_char_data(struct xbuffer * xbuf)66*320d7c8aSAaron LI public char * xbuf_char_data(struct xbuffer *xbuf)
67*320d7c8aSAaron LI {
68*320d7c8aSAaron LI 	return (char *)(xbuf->data);
69*320d7c8aSAaron LI }
70*320d7c8aSAaron LI 
71*320d7c8aSAaron LI 
72*320d7c8aSAaron LI /*
73*320d7c8aSAaron LI  * Helper functions for the ckd_add and ckd_mul macro substitutes.
74*320d7c8aSAaron LI  * These helper functions do not set *R on overflow, and assume that
75*320d7c8aSAaron LI  * arguments are nonnegative, that INTMAX_MAX <= UINTMAX_MAX, and that
76*320d7c8aSAaron LI  * sizeof is a reliable way to distinguish integer representations.
77*320d7c8aSAaron LI  * Despite these limitations they are good enough for 'less' on all
78*320d7c8aSAaron LI  * known practical platforms.  For more-complicated substitutes
79*320d7c8aSAaron LI  * without most of these limitations, see Gnulib's stdckdint module.
80*320d7c8aSAaron LI  */
81*320d7c8aSAaron LI #if !HAVE_STDCKDINT_H
82*320d7c8aSAaron LI /*
83*320d7c8aSAaron LI  * If the integer *R can represent VAL, store the value and return FALSE.
84*320d7c8aSAaron LI  * Otherwise, possibly set *R to an indeterminate value and return TRUE.
85*320d7c8aSAaron LI  * R has size RSIZE, and is signed if and only if RSIGNED is nonzero.
86*320d7c8aSAaron LI  */
help_fixup(void * r,uintmax val,int rsize,int rsigned)87*320d7c8aSAaron LI static int help_fixup(void *r, uintmax val, int rsize, int rsigned)
88*320d7c8aSAaron LI {
89*320d7c8aSAaron LI 	if (rsigned)
90*320d7c8aSAaron LI 	{
91*320d7c8aSAaron LI 		if (rsize == sizeof (int))
92*320d7c8aSAaron LI 		{
93*320d7c8aSAaron LI 			int *pr = r;
94*320d7c8aSAaron LI 			if (INT_MAX < val)
95*320d7c8aSAaron LI 				return TRUE;
96*320d7c8aSAaron LI 			*pr = (int) val;
97*320d7c8aSAaron LI #ifdef LLONG_MAX
98*320d7c8aSAaron LI 		} else if (rsize == sizeof (long long))
99*320d7c8aSAaron LI 		{
100*320d7c8aSAaron LI 			long long *pr = r;
101*320d7c8aSAaron LI 			if (LLONG_MAX < val)
102*320d7c8aSAaron LI 				return TRUE;
103*320d7c8aSAaron LI 			*pr = val;
104*320d7c8aSAaron LI #endif
105*320d7c8aSAaron LI #ifdef INTMAX_MAX
106*320d7c8aSAaron LI 		} else if (rsize == sizeof (intmax_t)) {
107*320d7c8aSAaron LI 			intmax_t *pr = r;
108*320d7c8aSAaron LI 			if (INTMAX_MAX < val)
109*320d7c8aSAaron LI 				return TRUE;
110*320d7c8aSAaron LI 			*pr = val;
111*320d7c8aSAaron LI #endif
112*320d7c8aSAaron LI 		} else /* rsize == sizeof (long) */
113*320d7c8aSAaron LI 		{
114*320d7c8aSAaron LI 			long *pr = r;
115*320d7c8aSAaron LI 			if (LONG_MAX < val)
116*320d7c8aSAaron LI 				return TRUE;
117*320d7c8aSAaron LI 			*pr = (long) val;
118*320d7c8aSAaron LI 		}
119*320d7c8aSAaron LI 	} else {
120*320d7c8aSAaron LI 		if (rsize == sizeof (unsigned)) {
121*320d7c8aSAaron LI 			unsigned *pr = r;
122*320d7c8aSAaron LI 			if (UINT_MAX < val)
123*320d7c8aSAaron LI 				return TRUE;
124*320d7c8aSAaron LI 			*pr = (unsigned) val;
125*320d7c8aSAaron LI 		} else if (rsize == sizeof (unsigned long)) {
126*320d7c8aSAaron LI 			unsigned long *pr = r;
127*320d7c8aSAaron LI 			if (ULONG_MAX < val)
128*320d7c8aSAaron LI 				return TRUE;
129*320d7c8aSAaron LI 			*pr = (unsigned long) val;
130*320d7c8aSAaron LI #ifdef ULLONG_MAX
131*320d7c8aSAaron LI 		} else if (rsize == sizeof (unsigned long long)) {
132*320d7c8aSAaron LI 			long long *pr = r;
133*320d7c8aSAaron LI 			if (ULLONG_MAX < val)
134*320d7c8aSAaron LI 				return TRUE;
135*320d7c8aSAaron LI 			*pr = val;
136*320d7c8aSAaron LI #endif
137*320d7c8aSAaron LI 		} else /* rsize == sizeof (uintmax) */
138*320d7c8aSAaron LI 		{
139*320d7c8aSAaron LI 			uintmax *pr = r;
140*320d7c8aSAaron LI 			*pr = val;
141*320d7c8aSAaron LI 		}
142*320d7c8aSAaron LI 	}
143*320d7c8aSAaron LI 	return FALSE;
144*320d7c8aSAaron LI }
145*320d7c8aSAaron LI /*
146*320d7c8aSAaron LI  * If *R can represent the mathematical sum of A and B, store the sum
147*320d7c8aSAaron LI  * and return FALSE.  Otherwise, possibly set *R to an indeterminate
148*320d7c8aSAaron LI  * value and return TRUE.  R has size RSIZE, and is signed if and only
149*320d7c8aSAaron LI  * if RSIGNED is nonzero.
150*320d7c8aSAaron LI  */
help_ckd_add(void * r,uintmax a,uintmax b,int rsize,int rsigned)151*320d7c8aSAaron LI public int help_ckd_add(void *r, uintmax a, uintmax b, int rsize, int rsigned)
152*320d7c8aSAaron LI {
153*320d7c8aSAaron LI 	uintmax sum = a + b;
154*320d7c8aSAaron LI 	return sum < a || help_fixup(r, sum, rsize, rsigned);
155*320d7c8aSAaron LI }
156*320d7c8aSAaron LI /* Likewise, but for the product of A and B.  */
help_ckd_mul(void * r,uintmax a,uintmax b,int rsize,int rsigned)157*320d7c8aSAaron LI public int help_ckd_mul(void *r, uintmax a, uintmax b, int rsize, int rsigned)
158*320d7c8aSAaron LI {
159*320d7c8aSAaron LI 	uintmax product = a * b;
160*320d7c8aSAaron LI 	return ((b != 0 && a != product / b)
161*320d7c8aSAaron LI 		|| help_fixup(r, product, rsize, rsigned));
162*320d7c8aSAaron LI }
163*320d7c8aSAaron LI #endif
164