Annotation of src/usr.bin/dc/inout.c, Revision 1.1
1.1 ! otto 1: /* $OpenBSD$ */
! 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
! 20: static const char rcsid[] = "$OpenBSD$";
! 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: {
! 217: int count, i, sz, ch;
! 218: char *p;
! 219:
! 220: count = 1;
! 221: i = 0;
! 222: sz = 15;
! 223: p = bmalloc(sz + 1);
! 224:
! 225: while ((ch = (*src->vtable->readchar)(src)) != EOF) {
! 226: if (ch == '[')
! 227: count++;
! 228: else if (ch == ']')
! 229: count--;
! 230: if (count == 0)
! 231: break;
! 232: if (i == sz) {
! 233: sz *= 2;
! 234: p = brealloc(p, sz + 1);
! 235: }
! 236: p[i++] = ch;
! 237: }
! 238: p[i] = '\0';
! 239: return p;
! 240: }
! 241:
! 242: static char *
! 243: get_digit(u_long num, int digits, u_int base)
! 244: {
! 245: char *p;
! 246: if (base <= 16) {
! 247: p = bmalloc(2);
! 248: p[0] = num >= 10 ? num + 'A' - 10 : num + '0';
! 249: p[1] = '\0';
! 250: } else {
! 251: if (asprintf(&p, "%0*lu", digits, num) == -1)
! 252: err(1, "cannot allocate string");
! 253: }
! 254: return p;
! 255: }
! 256:
! 257: void
! 258: printnumber(FILE *f, const struct number *b, u_int base)
! 259: {
! 260: struct number *int_part, *fract_part;
! 261: int digits;
! 262: char buf[11];
! 263: size_t sz;
! 264: int i;
! 265: struct stack stack;
! 266: char *p;
! 267:
! 268: if (BN_is_zero(b->number))
! 269: putcharwrap(f, '0');
! 270:
! 271: int_part = new_number();
! 272: fract_part = new_number();
! 273: fract_part->scale = b->scale;
! 274:
! 275: if (base <= 16)
! 276: digits = 1;
! 277: else {
! 278: digits = snprintf(buf, sizeof(buf), "%u", base-1);
! 279: }
! 280: split_number(b, int_part->number, fract_part->number);
! 281:
! 282: i = 0;
! 283: stack_init(&stack);
! 284: while (!BN_is_zero(int_part->number)) {
! 285: BN_ULONG rem = BN_div_word(int_part->number, base);
! 286: stack_pushstring(&stack, get_digit(rem, digits, base));
! 287: i++;
! 288: }
! 289: sz = i;
! 290: charCount = 0;
! 291: if (BN_cmp(b->number, &zero) < 0)
! 292: putcharwrap(f, '-');
! 293: for (i = 0; i < sz; i++) {
! 294: p = stack_popstring(&stack);
! 295: if (base > 16)
! 296: putcharwrap(f, ' ');
! 297: printwrap(f, p);
! 298: free(p);
! 299: }
! 300: stack_clear(&stack);
! 301: if (b->scale > 0) {
! 302: struct number *num_base;
! 303: BIGNUM mult, stop;
! 304:
! 305: putcharwrap(f, '.');
! 306: num_base = new_number();
! 307: BN_set_word(num_base->number, base);
! 308: BN_init(&mult);
! 309: BN_one(&mult);
! 310: BN_init(&stop);
! 311: BN_one(&stop);
! 312: scale_number(&stop, b->scale);
! 313:
! 314: i = 0;
! 315: while (BN_cmp(&mult, &stop) < 0) {
! 316: u_long rem;
! 317:
! 318: if (i && base > 16)
! 319: putcharwrap(f, ' ');
! 320: i = 1;
! 321:
! 322: bmul_number(fract_part, fract_part, num_base);
! 323: split_number(fract_part, int_part->number, NULL);
! 324: rem = BN_get_word(int_part->number);
! 325: p = get_digit(rem, digits, base);
! 326: int_part->scale = 0;
! 327: normalize(int_part, fract_part->scale);
! 328: BN_sub(fract_part->number, fract_part->number,
! 329: int_part->number);
! 330: printwrap(f, p);
! 331: free(p);
! 332: BN_mul_word(&mult, base);
! 333: }
! 334: free_number(num_base);
! 335: BN_free(&mult);
! 336: BN_free(&stop);
! 337: }
! 338: free_number(int_part);
! 339: free_number(fract_part);
! 340: }
! 341:
! 342: void
! 343: print_value(FILE *f, const struct value *value, const char *prefix, u_int base)
! 344: {
! 345: fputs(prefix, f);
! 346: switch (value->type) {
! 347: case BCODE_NONE:
! 348: if (value->array != NULL)
! 349: fputs("<array>", f);
! 350: break;
! 351: case BCODE_NUMBER:
! 352: printnumber(f, value->u.num, base);
! 353: break;
! 354: case BCODE_STRING:
! 355: fputs(value->u.string, f);
! 356: break;
! 357: }
! 358: }
! 359:
! 360: void
! 361: print_ascii(FILE *f, const struct number *n)
! 362: {
! 363: BIGNUM *v;
! 364: int numbits, i, ch;
! 365:
! 366: v = BN_dup(n->number);
! 367: bn_checkp(v);
! 368:
! 369: if (BN_cmp(v, &zero) < 0)
! 370: bn_check(BN_sub(v, &zero, v));
! 371:
! 372: numbits = BN_num_bytes(v) * 8;
! 373: while (numbits > 0) {
! 374: ch = 0;
! 375: for (i = 0; i < 8; i++)
! 376: ch |= BN_is_bit_set(v, numbits-i-1) << (7 - i);
! 377: putc(ch, f);
! 378: numbits -= 8;
! 379: }
! 380: BN_free(v);
! 381: }