1 /* $OpenBSD: mem.c,v 1.6 2014/12/01 13:13:00 deraadt Exp $ */
4 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
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.
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.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <openssl/err.h>
36 n = bmalloc(sizeof(*n));
39 if (n->number == NULL)
45 free_number(struct number *n)
53 * Divide dividend by divisor, returning the result. Retain bscale places of
55 * The result must be freed when no longer in use
58 div_number(struct number *dividend, struct number *divisor, u_int bscale)
60 struct number *quotient;
64 quotient = new_number();
65 quotient->scale = bscale;
66 scale = max(divisor->scale, dividend->scale);
68 if (BN_is_zero(divisor->number))
69 warnx("divide by zero");
71 normalize(divisor, scale);
72 normalize(dividend, scale + quotient->scale);
76 bn_check(BN_div(quotient->number, NULL, dividend->number,
77 divisor->number, ctx));
84 dup_number(const struct number *a)
88 n = bmalloc(sizeof(*n));
90 n->number = BN_dup(a->number);
107 breallocarray(void *p, size_t nmemb, size_t size)
111 q = reallocarray(p, nmemb, size);
118 bstrdup(const char *p)
133 err(1, "big number failure %lx", ERR_get_error());
137 bn_checkp(const void *p) \
141 err(1, "allocation failure %lx", ERR_get_error());