xref: /freebsd/usr.bin/dc/mem.c (revision 1d386b48)
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 #include <openssl/err.h>
21 
22 #include <err.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "extern.h"
28 
29 struct number *
new_number(void)30 new_number(void)
31 {
32 	struct number *n;
33 
34 	n = bmalloc(sizeof(*n));
35 	n->scale = 0;
36 	n->number = BN_new();
37 	if (n->number == NULL)
38 		err(1, NULL);
39 	return (n);
40 }
41 
42 void
free_number(struct number * n)43 free_number(struct number *n)
44 {
45 
46 	BN_free(n->number);
47 	free(n);
48 }
49 
50 /*
51  * Divide dividend by divisor, returning the result.  Retain bscale places of
52  * precision.
53  * The result must be freed when no longer in use
54  */
55 struct number *
div_number(struct number * dividend,struct number * divisor,u_int bscale)56 div_number(struct number *dividend, struct number *divisor, u_int bscale)
57 {
58 	struct number *quotient;
59 	BN_CTX *ctx;
60 	u_int scale;
61 
62 	quotient = new_number();
63 	quotient->scale = bscale;
64 	scale = max(divisor->scale, dividend->scale);
65 
66 	if (BN_is_zero(divisor->number))
67 		warnx("divide by zero");
68 	else {
69 		normalize(divisor, scale);
70 		normalize(dividend, scale + quotient->scale);
71 
72 		ctx = BN_CTX_new();
73 		bn_checkp(ctx);
74 		bn_check(BN_div(quotient->number, NULL, dividend->number,
75 				divisor->number, ctx));
76 		BN_CTX_free(ctx);
77 	}
78 	return (quotient);
79 }
80 
81 struct number *
dup_number(const struct number * a)82 dup_number(const struct number *a)
83 {
84 	struct number *n;
85 
86 	n = bmalloc(sizeof(*n));
87 	n->scale = a->scale;
88 	n->number = BN_dup(a->number);
89 	bn_checkp(n->number);
90 	return (n);
91 }
92 
93 void *
bmalloc(size_t sz)94 bmalloc(size_t sz)
95 {
96 	void *p;
97 
98 	p = malloc(sz);
99 	if (p == NULL)
100 		err(1, NULL);
101 	return (p);
102 }
103 
104 void *
breallocarray(void * p,size_t nmemb,size_t size)105 breallocarray(void *p, size_t nmemb, size_t size)
106 {
107 	void *q;
108 
109 	q = reallocarray(p, nmemb, size);
110 	if (q == NULL)
111 		err(1, NULL);
112 	return (q);
113 }
114 
115 char *
bstrdup(const char * p)116 bstrdup(const char *p)
117 {
118 	char *q;
119 
120 	q = strdup(p);
121 	if (q == NULL)
122 		err(1, NULL);
123 	return (q);
124 }
125 
126 void
bn_check(int x)127 bn_check(int x)						\
128 {
129 
130 	if (x == 0)
131 		err(1, "big number failure %lx", ERR_get_error());
132 }
133 
134 void
bn_checkp(const void * p)135 bn_checkp(const void *p)						\
136 {
137 
138 	if (p == NULL)
139 		err(1, "allocation failure %lx", ERR_get_error());
140 }
141