1202719Sgabor/*	$FreeBSD$						*/
2264573Sdelphij/*	$OpenBSD: bcode.h,v 1.7 2012/11/07 11:06:14 otto Exp $	*/
3202719Sgabor
4202719Sgabor/*
5202719Sgabor * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
6202719Sgabor *
7202719Sgabor * Permission to use, copy, modify, and distribute this software for any
8202719Sgabor * purpose with or without fee is hereby granted, provided that the above
9202719Sgabor * copyright notice and this permission notice appear in all copies.
10202719Sgabor *
11202719Sgabor * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12202719Sgabor * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13202719Sgabor * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14202719Sgabor * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15202719Sgabor * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16202719Sgabor * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17202719Sgabor * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18202719Sgabor */
19202719Sgabor
20202719Sgabor#include <sys/types.h>
21202719Sgabor#include <openssl/bn.h>
22202719Sgabor
23202719Sgaborstruct number {
24202719Sgabor	BIGNUM	*number;
25202719Sgabor	u_int	 scale;
26202719Sgabor};
27202719Sgabor
28202719Sgaborenum stacktype {
29202719Sgabor	BCODE_NONE,
30202719Sgabor	BCODE_NUMBER,
31202719Sgabor	BCODE_STRING
32202719Sgabor};
33202719Sgabor
34202719Sgaborenum bcode_compare {
35202719Sgabor	BCODE_EQUAL,
36202719Sgabor	BCODE_NOT_EQUAL,
37202719Sgabor	BCODE_LESS,
38202719Sgabor	BCODE_NOT_LESS,
39202719Sgabor	BCODE_GREATER,
40202719Sgabor	BCODE_NOT_GREATER
41202719Sgabor};
42202719Sgabor
43202719Sgaborstruct array;
44202719Sgabor
45202719Sgaborstruct value {
46202719Sgabor	union {
47202719Sgabor		struct number	*num;
48202719Sgabor		char		*string;
49202719Sgabor	} u;
50202719Sgabor	struct array	*array;
51202719Sgabor	enum stacktype	 type;
52202719Sgabor};
53202719Sgabor
54202719Sgaborstruct array {
55202719Sgabor	struct value	*data;
56202719Sgabor	size_t		 size;
57202719Sgabor};
58202719Sgabor
59202719Sgaborstruct stack {
60202719Sgabor	struct value	*stack;
61203443Sgabor	ssize_t		 size;
62202719Sgabor	ssize_t		 sp;
63202719Sgabor};
64202719Sgabor
65202719Sgaborstruct source;
66202719Sgabor
67202719Sgaborstruct vtable {
68202719Sgabor	int	(*readchar)(struct source *);
69202719Sgabor	void	(*unreadchar)(struct source *);
70202719Sgabor	char	*(*readline)(struct source *);
71202719Sgabor	void	(*free)(struct source *);
72202719Sgabor};
73202719Sgabor
74202719Sgaborstruct source {
75202719Sgabor	union {
76202719Sgabor			struct {
77202719Sgabor				u_char	*buf;
78202719Sgabor				size_t	 pos;
79202719Sgabor			} string;
80203443Sgabor			FILE	*stream;
81202719Sgabor	} u;
82203443Sgabor	struct vtable	*vtable;
83202719Sgabor	int		 lastchar;
84202719Sgabor};
85202719Sgabor
86264573Sdelphijvoid			init_bmachine(bool);
87264573Sdelphijvoid			reset_bmachine(struct source *);
88264573Sdelphiju_int			bmachine_scale(void);
89264573Sdelphijvoid			scale_number(BIGNUM *, int);
90264573Sdelphijvoid			normalize(struct number *, u_int);
91264573Sdelphijvoid			eval(void);
92264573Sdelphijvoid			pn(const char *, const struct number *);
93264573Sdelphijvoid			pbn(const char *, const BIGNUM *);
94264573Sdelphijvoid			negate(struct number *);
95264573Sdelphijvoid			split_number(const struct number *, BIGNUM *, BIGNUM *);
96264573Sdelphijvoid			bmul_number(struct number *, struct number *,
97264573Sdelphij			    struct number *, u_int scale);
98