1/*	$OpenBSD: mem.c,v 1.6 2014/12/01 13:13:00 deraadt Exp $	*/
2
3/*
4 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/cdefs.h>
20__FBSDID("$FreeBSD$");
21
22#include <openssl/err.h>
23
24#include <err.h>
25#include <limits.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "extern.h"
30
31struct number *
32new_number(void)
33{
34	struct number *n;
35
36	n = bmalloc(sizeof(*n));
37	n->scale = 0;
38	n->number = BN_new();
39	if (n->number == NULL)
40		err(1, NULL);
41	return (n);
42}
43
44void
45free_number(struct number *n)
46{
47
48	BN_free(n->number);
49	free(n);
50}
51
52/*
53 * Divide dividend by divisor, returning the result.  Retain bscale places of
54 * precision.
55 * The result must be freed when no longer in use
56 */
57struct number *
58div_number(struct number *dividend, struct number *divisor, u_int bscale)
59{
60	struct number *quotient;
61	BN_CTX *ctx;
62	u_int scale;
63
64	quotient = new_number();
65	quotient->scale = bscale;
66	scale = max(divisor->scale, dividend->scale);
67
68	if (BN_is_zero(divisor->number))
69		warnx("divide by zero");
70	else {
71		normalize(divisor, scale);
72		normalize(dividend, scale + quotient->scale);
73
74		ctx = BN_CTX_new();
75		bn_checkp(ctx);
76		bn_check(BN_div(quotient->number, NULL, dividend->number,
77				divisor->number, ctx));
78		BN_CTX_free(ctx);
79	}
80	return (quotient);
81}
82
83struct number *
84dup_number(const struct number *a)
85{
86	struct number *n;
87
88	n = bmalloc(sizeof(*n));
89	n->scale = a->scale;
90	n->number = BN_dup(a->number);
91	bn_checkp(n->number);
92	return (n);
93}
94
95void *
96bmalloc(size_t sz)
97{
98	void *p;
99
100	p = malloc(sz);
101	if (p == NULL)
102		err(1, NULL);
103	return (p);
104}
105
106void *
107breallocarray(void *p, size_t nmemb, size_t size)
108{
109	void *q;
110
111	q = reallocarray(p, nmemb, size);
112	if (q == NULL)
113		err(1, NULL);
114	return (q);
115}
116
117char *
118bstrdup(const char *p)
119{
120	char *q;
121
122	q = strdup(p);
123	if (q == NULL)
124		err(1, NULL);
125	return (q);
126}
127
128void
129bn_check(int x)						\
130{
131
132	if (x == 0)
133		err(1, "big number failure %lx", ERR_get_error());
134}
135
136void
137bn_checkp(const void *p)						\
138{
139
140	if (p == NULL)
141		err(1, "allocation failure %lx", ERR_get_error());
142}
143