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