summaryrefslogtreecommitdiff
path: root/usr.bin/dc/bcode.h
blob: 985237dbcaa8c408a9dfd9f61d71c036982bf943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*	$OpenBSD: bcode.h,v 1.8 2015/02/16 20:53:34 jca Exp $	*/

/*
 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <openssl/bn.h>


struct number {
	BIGNUM	*number;
	u_int	scale;
};

enum stacktype {
	BCODE_NONE,
	BCODE_NUMBER,
	BCODE_STRING
};

enum bcode_compare {
	BCODE_EQUAL,
	BCODE_NOT_EQUAL,
	BCODE_LESS,
	BCODE_NOT_LESS,
	BCODE_GREATER,
	BCODE_NOT_GREATER
};

struct array;

struct value {
	union {
		struct number	*num;
		char		*string;
	} u;
	struct array	*array;
	enum stacktype	type;
};

struct array {
	struct value	*data;
	size_t		size;
};

struct stack {
	struct value	*stack;
	ssize_t		sp;
	size_t		size;
};

struct source;

struct vtable {
	int	(*readchar)(struct source *);
	void	(*unreadchar)(struct source *);
	char	*(*readline)(struct source *);
	void	(*free)(struct source *);
};

struct source {
	struct vtable	*vtable;
	union {
			FILE *stream;
			struct {
				u_char *buf;
				size_t pos;
			} string;
	} u;
	int		lastchar;
};

void			init_bmachine(bool);
void			reset_bmachine(struct source *);
u_int			bmachine_scale(void);
void			scale_number(BIGNUM *, int);
void			normalize(struct number *, u_int);
void			eval(void);
void			pn(const char *, const struct number *);
void			pbn(const char *, const BIGNUM *);
void			negate(struct number *);
void			split_number(const struct number *, BIGNUM *, BIGNUM *);
void			bmul_number(struct number *, struct number *,
			    struct number *, u_int scale);