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