1 /* 2 * $OpenBSD: bcode.h,v 1.7 2012/11/07 11:06:14 otto Exp $ 3 * $DragonFly: src/usr.bin/dc/bcode.h,v 1.1 2004/09/20 04:20:39 dillon Exp $ 4 */ 5 6 /* 7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> 8 * 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. 12 * 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. 20 */ 21 22 #include <sys/types.h> 23 #include <openssl/bn.h> 24 25 26 struct number { 27 BIGNUM *number; 28 u_int scale; 29 }; 30 31 enum stacktype { 32 BCODE_NONE, 33 BCODE_NUMBER, 34 BCODE_STRING 35 }; 36 37 enum bcode_compare { 38 BCODE_EQUAL, 39 BCODE_NOT_EQUAL, 40 BCODE_LESS, 41 BCODE_NOT_LESS, 42 BCODE_GREATER, 43 BCODE_NOT_GREATER 44 }; 45 46 struct array; 47 48 struct value { 49 union { 50 struct number *num; 51 char *string; 52 } u; 53 struct array *array; 54 enum stacktype type; 55 }; 56 57 struct array { 58 struct value *data; 59 size_t size; 60 }; 61 62 struct stack { 63 struct value *stack; 64 ssize_t sp; 65 size_t size; 66 }; 67 68 struct source; 69 70 struct vtable { 71 int (*readchar)(struct source *); 72 void (*unreadchar)(struct source *); 73 char *(*readline)(struct source *); 74 void (*free)(struct source *); 75 }; 76 77 struct source { 78 struct vtable *vtable; 79 union { 80 FILE *stream; 81 struct { 82 u_char *buf; 83 size_t pos; 84 } string; 85 } u; 86 int lastchar; 87 }; 88 89 void init_bmachine(bool); 90 void reset_bmachine(struct source *); 91 u_int bmachine_scale(void); 92 void scale_number(BIGNUM *, int); 93 void normalize(struct number *, u_int); 94 void eval(void); 95 void pn(const char *, const struct number *); 96 void pbn(const char *, const BIGNUM *); 97 void negate(struct number *); 98 void split_number(const struct number *, BIGNUM *, BIGNUM *); 99 void bmul_number(struct number *, struct number *, 100 struct number *, u_int scale); 101