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 #include <openssl/err.h>
34 n = bmalloc(sizeof(*n));
37 if (n->number == NULL)
43 free_number(struct number *n)
51 * Divide dividend by divisor, returning the result. Retain bscale places of
53 * The result must be freed when no longer in use
56 div_number(struct number *dividend, struct number *divisor, u_int bscale)
58 struct number *quotient;
62 quotient = new_number();
63 quotient->scale = bscale;
64 scale = max(divisor->scale, dividend->scale);
66 if (BN_is_zero(divisor->number))
67 warnx("divide by zero");
69 normalize(divisor, scale);
70 normalize(dividend, scale + quotient->scale);
74 bn_check(BN_div(quotient->number, NULL, dividend->number,
75 divisor->number, ctx));
82 dup_number(const struct number *a)
86 n = bmalloc(sizeof(*n));
88 n->number = BN_dup(a->number);
105 breallocarray(void *p, size_t nmemb, size_t size)
109 q = reallocarray(p, nmemb, size);
116 bstrdup(const char *p)
131 err(1, "big number failure %lx", ERR_get_error());
135 bn_checkp(const void *p) \
139 err(1, "allocation failure %lx", ERR_get_error());