Annotation of src/usr.bin/dc/inout.c, Revision 1.5
1.5 ! otto 1: /* $OpenBSD: inout.c,v 1.4 2003/09/28 19:29:32 otto Exp $ */
1.1 otto 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: #ifndef lint
1.5 ! otto 20: static const char rcsid[] = "$OpenBSD: inout.c,v 1.4 2003/09/28 19:29:32 otto Exp $";
1.1 otto 21: #endif /* not lint */
22:
23: #include <ssl/ssl.h>
24: #include <ctype.h>
25: #include <err.h>
26: #include <string.h>
27:
28: #include "extern.h"
29:
30: #define MAX_CHARS_PER_LINE 68
31:
32: static int charCount;
33:
34:
35: static int src_getcharstream(struct source *);
36: static int src_ungetcharstream(struct source *);
37: static char *src_getlinestream(struct source *);
38: static void src_freestream(struct source *);
39: static int src_getcharstring(struct source *);
40: static int src_ungetcharstring(struct source *);
41: static char *src_getlinestring(struct source *);
42: static void src_freestring(struct source *);
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
62: src_setstream(struct source *src, FILE *stream)
63: {
64: src->u.stream = stream;
65: src->vtable = &stream_vtable;
66: }
67:
68: void
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
77: src_getcharstream(struct source *src)
78: {
79: return src->lastchar = getc(src->u.stream);
80: }
81:
82: static int
83: src_ungetcharstream(struct source *src)
84: {
85: return ungetc(src->lastchar, src->u.stream);
86: }
87:
88: static void
89: src_freestream(struct source *src)
90: {
91: }
92:
93: static char *
94: src_getlinestream(struct source *src)
95: {
96: char buf[BUFSIZ];
97:
98: if (fgets(buf, BUFSIZ, src->u.stream) == NULL)
99: return bstrdup("");
100: return bstrdup(buf);
101: }
102:
103: static int
104: src_getcharstring(struct source *src)
105: {
106: src->lastchar = src->u.string.buf[src->u.string.pos];
107: if (src->lastchar == '\0')
108: return EOF;
109: else {
110: src->u.string.pos++;
111: return src->lastchar;
112: }
113: }
114:
115: static int
116: src_ungetcharstring(struct source *src)
117: {
118: if (src->u.string.pos > 0)
119: return src->u.string.buf[--src->u.string.pos];
120: else
121: return EOF;
122: }
123:
124: static char *
125: src_getlinestring(struct source *src)
126: {
127: char buf[BUFSIZ];
128: int ch, i;
129:
130: i = 0;
131: while (i < BUFSIZ-1) {
132: ch = src_getcharstring(src);
133: if (ch == EOF)
134: break;
135: buf[i++] = ch;
136: if (ch == '\n')
137: break;
138: }
139: buf[i] = '\0';
140: return bstrdup(buf);
141: }
142:
143: static void
144: src_freestring(struct source *src)
145: {
146: free(src->u.string.buf);
147: }
148:
149: static void
150: putcharwrap(FILE *f, int ch)
151: {
152: putc(ch, f);
153: if (++charCount > MAX_CHARS_PER_LINE) {
154: charCount = 0;
155: fputs("\\\n", f);
156: }
157: }
158:
159: static void
160: printwrap(FILE *f, const char *p)
161: {
162: char buf[12];
163: char *q = buf;
164:
165: snprintf(buf, sizeof(buf), "%s", p);
166: while (*q)
167: putcharwrap(f, *q++);
168: }
169:
170: struct number *
171: readnumber(struct source *src, u_int base)
172: {
173: struct number *n;
174: int ch;
175: bool sign = false;
176: bool dot = false;
177: BN_ULONG v;
178:
179: n = new_number();
180: bn_check(BN_zero(n->number));
181:
182: while ((ch = (*src->vtable->readchar)(src)) != EOF) {
183:
184: if ('0' <= ch && ch <= '9')
185: v = ch - '0';
186: else if ('A' <= ch && ch <= 'F')
187: v = ch - 'A' + 10;
188: else if (ch == '_') {
189: sign = true;
190: continue;
191: } else if (ch == '.') {
192: if (dot)
193: break;
194: dot = true;
195: continue;
196: } else {
197: (*src->vtable->unreadchar)(src);
198: break;
199: }
200: if (dot)
201: n->scale++;
202:
203: bn_check(BN_mul_word(n->number, base));
204:
205: /* work around a bug in BN_add_word: 0 += 0 is buggy.... */
206: if (v > 0)
207: bn_check(BN_add_word(n->number, v));
208: }
209: if (sign)
210: negate(n);
211: return n;
212: }
213:
214: char *
215: read_string(struct source *src)
216: {
1.4 otto 217: int count, i, sz, new_sz, ch;
1.1 otto 218: char *p;
1.5 ! otto 219: bool escape;
1.1 otto 220:
1.5 ! otto 221: escape = false;
1.1 otto 222: count = 1;
223: i = 0;
224: sz = 15;
225: p = bmalloc(sz + 1);
226:
227: while ((ch = (*src->vtable->readchar)(src)) != EOF) {
1.5 ! otto 228: if (!escape) {
! 229: if (ch == '[')
! 230: count++;
! 231: else if (ch == ']')
! 232: count--;
! 233: if (count == 0)
! 234: break;
! 235: }
! 236: if (ch == '\\' && !escape)
! 237: escape = true;
! 238: else {
! 239: escape = false;
! 240: if (i == sz) {
! 241: new_sz = sz * 2;
! 242: p = brealloc(p, new_sz + 1);
! 243: sz = new_sz;
! 244: }
! 245: p[i++] = ch;
1.1 otto 246: }
247: }
248: p[i] = '\0';
249: return p;
250: }
251:
252: static char *
253: get_digit(u_long num, int digits, u_int base)
254: {
255: char *p;
1.3 deraadt 256:
1.1 otto 257: if (base <= 16) {
258: p = bmalloc(2);
1.2 deraadt 259: p[0] = num >= 10 ? num + 'A' - 10 : num + '0';
1.1 otto 260: p[1] = '\0';
261: } else {
262: if (asprintf(&p, "%0*lu", digits, num) == -1)
263: err(1, "cannot allocate string");
264: }
265: return p;
266: }
267:
268: void
269: printnumber(FILE *f, const struct number *b, u_int base)
270: {
271: struct number *int_part, *fract_part;
272: int digits;
273: char buf[11];
274: size_t sz;
275: int i;
276: struct stack stack;
277: char *p;
278:
279: if (BN_is_zero(b->number))
280: putcharwrap(f, '0');
281:
282: int_part = new_number();
283: fract_part = new_number();
284: fract_part->scale = b->scale;
285:
286: if (base <= 16)
287: digits = 1;
288: else {
289: digits = snprintf(buf, sizeof(buf), "%u", base-1);
290: }
291: split_number(b, int_part->number, fract_part->number);
292:
293: i = 0;
294: stack_init(&stack);
295: while (!BN_is_zero(int_part->number)) {
296: BN_ULONG rem = BN_div_word(int_part->number, base);
297: stack_pushstring(&stack, get_digit(rem, digits, base));
298: i++;
299: }
300: sz = i;
301: charCount = 0;
302: if (BN_cmp(b->number, &zero) < 0)
303: putcharwrap(f, '-');
304: for (i = 0; i < sz; i++) {
305: p = stack_popstring(&stack);
306: if (base > 16)
307: putcharwrap(f, ' ');
308: printwrap(f, p);
309: free(p);
310: }
311: stack_clear(&stack);
312: if (b->scale > 0) {
313: struct number *num_base;
314: BIGNUM mult, stop;
315:
316: putcharwrap(f, '.');
317: num_base = new_number();
318: BN_set_word(num_base->number, base);
319: BN_init(&mult);
320: BN_one(&mult);
321: BN_init(&stop);
322: BN_one(&stop);
323: scale_number(&stop, b->scale);
324:
325: i = 0;
326: while (BN_cmp(&mult, &stop) < 0) {
327: u_long rem;
328:
329: if (i && base > 16)
330: putcharwrap(f, ' ');
331: i = 1;
332:
333: bmul_number(fract_part, fract_part, num_base);
334: split_number(fract_part, int_part->number, NULL);
335: rem = BN_get_word(int_part->number);
336: p = get_digit(rem, digits, base);
337: int_part->scale = 0;
338: normalize(int_part, fract_part->scale);
339: BN_sub(fract_part->number, fract_part->number,
340: int_part->number);
341: printwrap(f, p);
342: free(p);
343: BN_mul_word(&mult, base);
344: }
345: free_number(num_base);
346: BN_free(&mult);
347: BN_free(&stop);
348: }
349: free_number(int_part);
350: free_number(fract_part);
351: }
352:
353: void
354: print_value(FILE *f, const struct value *value, const char *prefix, u_int base)
355: {
356: fputs(prefix, f);
357: switch (value->type) {
358: case BCODE_NONE:
359: if (value->array != NULL)
360: fputs("<array>", f);
361: break;
362: case BCODE_NUMBER:
363: printnumber(f, value->u.num, base);
364: break;
365: case BCODE_STRING:
366: fputs(value->u.string, f);
367: break;
368: }
369: }
370:
371: void
372: print_ascii(FILE *f, const struct number *n)
373: {
374: BIGNUM *v;
375: int numbits, i, ch;
376:
377: v = BN_dup(n->number);
378: bn_checkp(v);
379:
380: if (BN_cmp(v, &zero) < 0)
381: bn_check(BN_sub(v, &zero, v));
382:
383: numbits = BN_num_bytes(v) * 8;
384: while (numbits > 0) {
385: ch = 0;
386: for (i = 0; i < 8; i++)
387: ch |= BN_is_bit_set(v, numbits-i-1) << (7 - i);
388: putc(ch, f);
389: numbits -= 8;
390: }
391: BN_free(v);
392: }