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