1*8e25e9fcSjca /* $OpenBSD: bcode.h,v 1.8 2015/02/16 20:53:34 jca Exp $ */ 2fce6fa37Sotto 3fce6fa37Sotto /* 4fce6fa37Sotto * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> 5fce6fa37Sotto * 6fce6fa37Sotto * Permission to use, copy, modify, and distribute this software for any 7fce6fa37Sotto * purpose with or without fee is hereby granted, provided that the above 8fce6fa37Sotto * copyright notice and this permission notice appear in all copies. 9fce6fa37Sotto * 10fce6fa37Sotto * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11fce6fa37Sotto * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12fce6fa37Sotto * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13fce6fa37Sotto * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14fce6fa37Sotto * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15fce6fa37Sotto * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16fce6fa37Sotto * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17fce6fa37Sotto */ 18fce6fa37Sotto 19a6ce4a44Sotto #include <sys/types.h> 20*8e25e9fcSjca #include <openssl/bn.h> 21a6ce4a44Sotto 22a6ce4a44Sotto 23a6ce4a44Sotto struct number { 24a6ce4a44Sotto BIGNUM *number; 25a6ce4a44Sotto u_int scale; 26a6ce4a44Sotto }; 27a6ce4a44Sotto 28a6ce4a44Sotto enum stacktype { 29a6ce4a44Sotto BCODE_NONE, 30a6ce4a44Sotto BCODE_NUMBER, 31a6ce4a44Sotto BCODE_STRING 32a6ce4a44Sotto }; 33a6ce4a44Sotto 34a6ce4a44Sotto enum bcode_compare { 35a6ce4a44Sotto BCODE_EQUAL, 36a6ce4a44Sotto BCODE_NOT_EQUAL, 37a6ce4a44Sotto BCODE_LESS, 38a6ce4a44Sotto BCODE_NOT_LESS, 39a6ce4a44Sotto BCODE_GREATER, 40a6ce4a44Sotto BCODE_NOT_GREATER 41a6ce4a44Sotto }; 42a6ce4a44Sotto 43a6ce4a44Sotto struct array; 44a6ce4a44Sotto 45a6ce4a44Sotto struct value { 46a6ce4a44Sotto union { 47a6ce4a44Sotto struct number *num; 48a6ce4a44Sotto char *string; 49a6ce4a44Sotto } u; 50a6ce4a44Sotto struct array *array; 51a6ce4a44Sotto enum stacktype type; 52a6ce4a44Sotto }; 53a6ce4a44Sotto 54a6ce4a44Sotto struct array { 55a6ce4a44Sotto struct value *data; 56a6ce4a44Sotto size_t size; 57a6ce4a44Sotto }; 58a6ce4a44Sotto 59a6ce4a44Sotto struct stack { 60a6ce4a44Sotto struct value *stack; 61c982bb45Sotto ssize_t sp; 62c982bb45Sotto size_t size; 63a6ce4a44Sotto }; 64a6ce4a44Sotto 65a6ce4a44Sotto struct source; 66a6ce4a44Sotto 67a6ce4a44Sotto struct vtable { 68a6ce4a44Sotto int (*readchar)(struct source *); 69a1a9dfc3Sotto void (*unreadchar)(struct source *); 70a6ce4a44Sotto char *(*readline)(struct source *); 71a6ce4a44Sotto void (*free)(struct source *); 72a6ce4a44Sotto }; 73a6ce4a44Sotto 74a6ce4a44Sotto struct source { 75a6ce4a44Sotto struct vtable *vtable; 76a6ce4a44Sotto union { 77a6ce4a44Sotto FILE *stream; 78a6ce4a44Sotto struct { 79a6ce4a44Sotto u_char *buf; 80a6ce4a44Sotto size_t pos; 81a6ce4a44Sotto } string; 82a6ce4a44Sotto } u; 83a6ce4a44Sotto int lastchar; 84a6ce4a44Sotto }; 85a6ce4a44Sotto 8677f3d9ddSotto void init_bmachine(bool); 87a6ce4a44Sotto void reset_bmachine(struct source *); 888b19764bSotto u_int bmachine_scale(void); 89a6ce4a44Sotto void scale_number(BIGNUM *, int); 90a6ce4a44Sotto void normalize(struct number *, u_int); 91a6ce4a44Sotto void eval(void); 92a6ce4a44Sotto void pn(const char *, const struct number *); 93a6ce4a44Sotto void pbn(const char *, const BIGNUM *); 94a6ce4a44Sotto void negate(struct number *); 95a6ce4a44Sotto void split_number(const struct number *, BIGNUM *, BIGNUM *); 96a6ce4a44Sotto void bmul_number(struct number *, struct number *, 978b19764bSotto struct number *, u_int scale); 98