2 * $OpenBSD: inout.c,v 1.17 2012/11/07 11:06:14 otto Exp $
3 * $DragonFly: src/usr.bin/dc/inout.c,v 1.2 2005/04/21 18:50:50 swildner Exp $
7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <openssl/ssl.h>
29 #define MAX_CHARS_PER_LINE 68
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);
47 static struct vtable stream_vtable = {
54 static struct vtable string_vtable = {
62 src_setstream(struct source *src, FILE *stream)
64 src->u.stream = stream;
65 src->vtable = &stream_vtable;
69 src_setstring(struct source *src, char *p)
71 src->u.string.buf = (u_char *)p;
72 src->u.string.pos = 0;
73 src->vtable = &string_vtable;
77 src_getcharstream(struct source *src)
79 return src->lastchar = getc(src->u.stream);
83 src_ungetcharstream(struct source *src)
85 ungetc(src->lastchar, src->u.stream);
89 src_freestream(struct source *src)
94 src_getlinestream(struct source *src)
98 if (fgets(buf, BUFSIZ, src->u.stream) == NULL)
104 src_getcharstring(struct source *src)
106 src->lastchar = src->u.string.buf[src->u.string.pos];
107 if (src->lastchar == '\0')
111 return src->lastchar;
116 src_ungetcharstring(struct source *src)
118 if (src->u.string.pos > 0) {
119 if (src->lastchar != '\0')
125 src_getlinestring(struct source *src)
131 while (i < BUFSIZ-1) {
132 ch = src_getcharstring(src);
144 src_freestring(struct source *src)
146 free(src->u.string.buf);
157 putcharwrap(FILE *f, int ch)
159 if (charcount >= MAX_CHARS_PER_LINE) {
163 if (lastchar != -1) {
171 printwrap(FILE *f, const char *p)
176 strlcpy(buf, p, sizeof(buf));
178 putcharwrap(f, *q++);
182 readnumber(struct source *src, u_int base)
191 bn_check(BN_zero(n->number));
193 while ((ch = (*src->vtable->readchar)(src)) != EOF) {
195 if ('0' <= ch && ch <= '9')
197 else if ('A' <= ch && ch <= 'F')
199 else if (ch == '_') {
202 } else if (ch == '.') {
208 (*src->vtable->unreadchar)(src);
214 bn_check(BN_mul_word(n->number, base));
217 /* work around a bug in BN_add_word: 0 += 0 is buggy.... */
220 bn_check(BN_add_word(n->number, v));
228 read_string(struct source *src)
230 int count, i, sz, new_sz, ch;
240 while ((ch = (*src->vtable->readchar)(src)) != EOF) {
249 if (ch == '\\' && !escape)
255 p = brealloc(p, new_sz + 1);
266 get_digit(u_long num, int digits, u_int base)
272 p[0] = num >= 10 ? num + 'A' - 10 : num + '0';
275 if (asprintf(&p, "%0*lu", digits, num) == -1)
282 printnumber(FILE *f, const struct number *b, u_int base)
284 struct number *int_part, *fract_part;
294 if (BN_is_zero(b->number))
297 int_part = new_number();
298 fract_part = new_number();
299 fract_part->scale = b->scale;
304 digits = snprintf(buf, sizeof(buf), "%u", base-1);
306 split_number(b, int_part->number, fract_part->number);
310 while (!BN_is_zero(int_part->number)) {
311 BN_ULONG rem = BN_div_word(int_part->number, base);
312 stack_pushstring(&stack, get_digit(rem, digits, base));
316 if (BN_is_negative(b->number))
318 for (i = 0; i < sz; i++) {
319 p = stack_popstring(&stack);
327 struct number *num_base;
331 num_base = new_number();
332 bn_check(BN_set_word(num_base->number, base));
334 bn_check(BN_one(&mult));
336 bn_check(BN_one(&stop));
337 scale_number(&stop, b->scale);
340 while (BN_cmp(&mult, &stop) < 0) {
347 bmul_number(fract_part, fract_part, num_base,
349 split_number(fract_part, int_part->number, NULL);
350 rem = BN_get_word(int_part->number);
351 p = get_digit(rem, digits, base);
353 normalize(int_part, fract_part->scale);
354 bn_check(BN_sub(fract_part->number, fract_part->number,
358 bn_check(BN_mul_word(&mult, base));
360 free_number(num_base);
365 free_number(int_part);
366 free_number(fract_part);
370 print_value(FILE *f, const struct value *value, const char *prefix, u_int base)
373 switch (value->type) {
375 if (value->array != NULL)
379 printnumber(f, value->u.num, base);
382 fputs(value->u.string, f);
388 print_ascii(FILE *f, const struct number *n)
393 v = BN_dup(n->number);
396 if (BN_is_negative(v))
397 BN_set_negative(v, 0);
399 numbits = BN_num_bytes(v) * 8;
400 while (numbits > 0) {
402 for (i = 0; i < 8; i++)
403 ch |= BN_is_bit_set(v, numbits-i-1) << (7 - i);