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