1 /*	$OpenBSD: inout.c,v 1.17 2012/11/07 11:06:14 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <ssl/ssl.h>
21 #include <ctype.h>
22 #include <err.h>
23 #include <string.h>
24 
25 #include "extern.h"
26 
27 __RCSID("$MirOS: src/usr.bin/dc/inout.c,v 1.3 2014/08/19 21:16:49 tg Exp $");
28 
29 #define MAX_CHARS_PER_LINE 68
30 
31 static int	lastchar;
32 static int	charcount;
33 
34 static int	src_getcharstream(struct source *);
35 static void	src_ungetcharstream(struct source *);
36 static char	*src_getlinestream(struct source *);
37 static void	src_freestream(struct source *);
38 static int	src_getcharstring(struct source *);
39 static void	src_ungetcharstring(struct source *);
40 static char	*src_getlinestring(struct source *);
41 static void	src_freestring(struct source *);
42 static void	flushwrap(FILE *);
43 static void	putcharwrap(FILE *, int);
44 static void	printwrap(FILE *, const char *);
45 static char	*get_digit(u_long, int, u_int);
46 
47 static struct vtable stream_vtable = {
48 	src_getcharstream,
49 	src_ungetcharstream,
50 	src_getlinestream,
51 	src_freestream
52 };
53 
54 static struct vtable string_vtable = {
55 	src_getcharstring,
56 	src_ungetcharstring,
57 	src_getlinestring,
58 	src_freestring
59 };
60 
61 void
src_setstream(struct source * src,FILE * stream)62 src_setstream(struct source *src, FILE *stream)
63 {
64 	src->u.stream = stream;
65 	src->vtable = &stream_vtable;
66 }
67 
68 void
src_setstring(struct source * src,char * p)69 src_setstring(struct source *src, char *p)
70 {
71 	src->u.string.buf = (u_char *)p;
72 	src->u.string.pos = 0;
73 	src->vtable = &string_vtable;
74 }
75 
76 static int
src_getcharstream(struct source * src)77 src_getcharstream(struct source *src)
78 {
79 	return src->lastchar = getc(src->u.stream);
80 }
81 
82 static void
src_ungetcharstream(struct source * src)83 src_ungetcharstream(struct source *src)
84 {
85 	(void)ungetc(src->lastchar, src->u.stream);
86 }
87 
88 /* ARGSUSED */
89 static void
src_freestream(struct source * src __unused)90 src_freestream(struct source *src __unused)
91 {
92 }
93 
94 static char *
src_getlinestream(struct source * src)95 src_getlinestream(struct source *src)
96 {
97 	char buf[BUFSIZ];
98 
99 	if (fgets(buf, BUFSIZ, src->u.stream) == NULL)
100 		return bstrdup("");
101 	return bstrdup(buf);
102 }
103 
104 static int
src_getcharstring(struct source * src)105 src_getcharstring(struct source *src)
106 {
107 	src->lastchar = src->u.string.buf[src->u.string.pos];
108 	if (src->lastchar == '\0')
109 		return EOF;
110 	else {
111 		src->u.string.pos++;
112 		return src->lastchar;
113 	}
114 }
115 
116 static void
src_ungetcharstring(struct source * src)117 src_ungetcharstring(struct source *src)
118 {
119 	if (src->u.string.pos > 0) {
120 		if (src->lastchar != '\0')
121 			--src->u.string.pos;
122 	}
123 }
124 
125 static char *
src_getlinestring(struct source * src)126 src_getlinestring(struct source *src)
127 {
128 	char buf[BUFSIZ];
129 	int ch, i;
130 
131 	i = 0;
132 	while (i < BUFSIZ-1) {
133 		ch = src_getcharstring(src);
134 		if (ch == EOF)
135 			break;
136 		buf[i++] = ch;
137 		if (ch == '\n')
138 			break;
139 	}
140 	buf[i] = '\0';
141 	return bstrdup(buf);
142 }
143 
144 static void
src_freestring(struct source * src)145 src_freestring(struct source *src)
146 {
147 	free(src->u.string.buf);
148 }
149 
150 static void
flushwrap(FILE * f)151 flushwrap(FILE *f)
152 {
153 	if (lastchar != -1)
154 		(void)putc(lastchar, f);
155 }
156 
157 static void
putcharwrap(FILE * f,int ch)158 putcharwrap(FILE *f, int ch)
159 {
160 	if (charcount >= MAX_CHARS_PER_LINE) {
161 		charcount = 0;
162 		(void)fputs("\\\n", f);
163 	}
164 	if (lastchar != -1) {
165 		charcount++;
166 		(void)putc(lastchar, f);
167 	}
168 	lastchar = ch;
169 }
170 
171 static void
printwrap(FILE * f,const char * p)172 printwrap(FILE *f, const char *p)
173 {
174 	char	buf[12];
175 	char	*q = buf;
176 
177 	(void)strlcpy(buf, p, sizeof(buf));
178 	while (*q)
179 		putcharwrap(f, *q++);
180 }
181 
182 struct number *
readnumber(struct source * src,u_int base)183 readnumber(struct source *src, u_int base)
184 {
185 	struct number	*n;
186 	int		ch;
187 	bool		sign = false;
188 	bool		dot = false;
189 	BN_ULONG	v;
190 	u_int		i;
191 
192 	n = new_number();
193 	bn_check(BN_zero(n->number));
194 
195 	while ((ch = (*src->vtable->readchar)(src)) != EOF) {
196 
197 		if ('0' <= ch && ch <= '9')
198 			v = ch - '0';
199 		else if ('A' <= ch && ch <= 'F')
200 			v = ch - 'A' + 10;
201 		else if (ch == '_') {
202 			sign = true;
203 			continue;
204 		} else if (ch == '.') {
205 			if (dot)
206 				break;
207 			dot = true;
208 			continue;
209 		} else {
210 			(*src->vtable->unreadchar)(src);
211 			break;
212 		}
213 		if (dot)
214 			n->scale++;
215 
216 		bn_check(BN_mul_word(n->number, base));
217 
218 #if 0
219 		/* work around a bug in BN_add_word: 0 += 0 is buggy.... */
220 		if (v > 0)
221 #endif
222 			bn_check(BN_add_word(n->number, v));
223 	}
224 	if (base != 10) {
225 		u_int iscale, dscale;
226 
227 		if ((dscale = bmachine_scale()) > n->scale) {
228 			iscale = dscale;
229 			dscale -= n->scale;
230 		} else {
231 			iscale = n->scale;
232 			dscale = 0;
233 		}
234 		scale_number(n->number, iscale);
235 		for (i = 0; i < n->scale; i++)
236 			(void)BN_div_word(n->number, base);
237 		n->scale += dscale;
238 		/*
239 		 * This makes the number have the k (scale) value
240 		 * if set. What we really want is to use a sane
241 		 * value in the default case, i.e. 0.1 -> one digit
242 		 * -> 0xF -> 15 -> scale=2; two digits 256 ergo 3.
243 		 */
244 	}
245 	if (sign)
246 		negate(n);
247 	return n;
248 }
249 
250 char *
read_string(struct source * src)251 read_string(struct source *src)
252 {
253 	int count, i, sz, new_sz, ch;
254 	char *p;
255 	bool escape;
256 
257 	escape = false;
258 	count = 1;
259 	i = 0;
260 	sz = 15;
261 	p = bmalloc(sz + 1);
262 
263 	while ((ch = (*src->vtable->readchar)(src)) != EOF) {
264 		if (!escape) {
265 			if (ch == '[')
266 				count++;
267 			else if (ch == ']')
268 				count--;
269 			if (count == 0)
270 				break;
271 		}
272 		if (ch == '\\' && !escape)
273 			escape = true;
274 		else {
275 			escape = false;
276 			if (i == sz) {
277 				new_sz = sz * 2;
278 				p = brealloc(p, new_sz + 1);
279 				sz = new_sz;
280 			}
281 			p[i++] = ch;
282 		}
283 	}
284 	p[i] = '\0';
285 	return p;
286 }
287 
288 static char *
get_digit(u_long num,int digits,u_int base)289 get_digit(u_long num, int digits, u_int base)
290 {
291 	char *p;
292 
293 	if (base <= 16) {
294 		p = bmalloc(2);
295 		p[0] = num >= 10 ? num + 'A' - 10 : num + '0';
296 		p[1] = '\0';
297 	} else {
298 		if (asprintf(&p, "%0*lu", digits, num) == -1)
299 			err(1, NULL);
300 	}
301 	return p;
302 }
303 
304 void
printnumber(FILE * f,const struct number * b,u_int base)305 printnumber(FILE *f, const struct number *b, u_int base)
306 {
307 	struct number	*int_part, *fract_part;
308 	int		digits;
309 	char		buf[11];
310 	size_t		sz;
311 	size_t		i;
312 	struct stack	stack;
313 	char		*p;
314 
315 	charcount = 0;
316 	lastchar = -1;
317 	if (BN_is_zero(b->number))
318 		putcharwrap(f, '0');
319 
320 	int_part = new_number();
321 	fract_part = new_number();
322 	fract_part->scale = b->scale;
323 
324 	if (base <= 16)
325 		digits = 1;
326 	else {
327 		digits = snprintf(buf, sizeof(buf), "%u", base-1);
328 	}
329 	split_number(b, int_part->number, fract_part->number);
330 
331 	i = 0;
332 	stack_init(&stack);
333 	while (!BN_is_zero(int_part->number)) {
334 		BN_ULONG rem = BN_div_word(int_part->number, base);
335 		stack_pushstring(&stack, get_digit(rem, digits, base));
336 		i++;
337 	}
338 	sz = i;
339 	if (BN_is_negative(b->number))
340 		putcharwrap(f, '-');
341 	for (i = 0; i < sz; i++) {
342 		p = stack_popstring(&stack);
343 		if (base > 16)
344 			putcharwrap(f, ' ');
345 		printwrap(f, p);
346 		free(p);
347 	}
348 	stack_clear(&stack);
349 	if (b->scale > 0) {
350 		struct number	*num_base;
351 		BIGNUM		mult, stop;
352 
353 		putcharwrap(f, '.');
354 		num_base = new_number();
355 		bn_check(BN_set_word(num_base->number, base));
356 		BN_init(&mult);
357 		bn_check(BN_one(&mult));
358 		BN_init(&stop);
359 		bn_check(BN_one(&stop));
360 		scale_number(&stop, b->scale);
361 
362 		i = 0;
363 		while (BN_cmp(&mult, &stop) < 0) {
364 			u_long	rem;
365 
366 			if (i && base > 16)
367 				putcharwrap(f, ' ');
368 			i = 1;
369 
370 			bmul_number(fract_part, fract_part, num_base,
371 			    bmachine_scale());
372 			split_number(fract_part, int_part->number, NULL);
373 			rem = BN_get_word(int_part->number);
374 			p = get_digit(rem, digits, base);
375 			int_part->scale = 0;
376 			normalize(int_part, fract_part->scale);
377 			bn_check(BN_sub(fract_part->number, fract_part->number,
378 			    int_part->number));
379 			printwrap(f, p);
380 			free(p);
381 			bn_check(BN_mul_word(&mult, base));
382 		}
383 		free_number(num_base);
384 		BN_free(&mult);
385 		BN_free(&stop);
386 	}
387 	flushwrap(f);
388 	free_number(int_part);
389 	free_number(fract_part);
390 }
391 
392 void
print_value(FILE * f,const struct value * value,const char * prefix,u_int base)393 print_value(FILE *f, const struct value *value, const char *prefix, u_int base)
394 {
395 	(void)fputs(prefix, f);
396 	switch (value->type) {
397 	case BCODE_NONE:
398 		if (value->array != NULL)
399 			(void)fputs("<array>", f);
400 		break;
401 	case BCODE_NUMBER:
402 		printnumber(f, value->u.num, base);
403 		break;
404 	case BCODE_STRING:
405 		(void)fputs(value->u.string, f);
406 		break;
407 	}
408 }
409 
410 void
print_ascii(FILE * f,const struct number * n)411 print_ascii(FILE *f, const struct number *n)
412 {
413 	BIGNUM *v;
414 	int numbits, i, ch;
415 
416 	v = BN_dup(n->number);
417 	bn_checkp(v);
418 
419 	if (BN_is_negative(v))
420 		BN_set_negative(v, 0);
421 
422 	numbits = BN_num_bytes(v) * 8;
423 	while (numbits > 0) {
424 		ch = 0;
425 		for (i = 0; i < 8; i++)
426 			ch |= BN_is_bit_set(v, numbits-i-1) << (7 - i);
427 		(void)putc(ch, f);
428 		numbits -= 8;
429 	}
430 	BN_free(v);
431 }
432