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