1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino * $OpenBSD: mem.c,v 1.4 2004/07/11 06:41:48 otto Exp $
3*86d7f5d3SJohn Marino * $DragonFly: src/usr.bin/dc/mem.c,v 1.1 2004/09/20 04:20:39 dillon Exp $
4*86d7f5d3SJohn Marino */
5*86d7f5d3SJohn Marino
6*86d7f5d3SJohn Marino /*
7*86d7f5d3SJohn Marino * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
8*86d7f5d3SJohn Marino *
9*86d7f5d3SJohn Marino * Permission to use, copy, modify, and distribute this software for any
10*86d7f5d3SJohn Marino * purpose with or without fee is hereby granted, provided that the above
11*86d7f5d3SJohn Marino * copyright notice and this permission notice appear in all copies.
12*86d7f5d3SJohn Marino *
13*86d7f5d3SJohn Marino * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*86d7f5d3SJohn Marino * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*86d7f5d3SJohn Marino * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*86d7f5d3SJohn Marino * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*86d7f5d3SJohn Marino * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*86d7f5d3SJohn Marino * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*86d7f5d3SJohn Marino * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*86d7f5d3SJohn Marino */
21*86d7f5d3SJohn Marino
22*86d7f5d3SJohn Marino #include <openssl/err.h>
23*86d7f5d3SJohn Marino
24*86d7f5d3SJohn Marino #include <err.h>
25*86d7f5d3SJohn Marino #include <stdlib.h>
26*86d7f5d3SJohn Marino #include <string.h>
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino #include "extern.h"
29*86d7f5d3SJohn Marino
30*86d7f5d3SJohn Marino struct number *
new_number(void)31*86d7f5d3SJohn Marino new_number(void)
32*86d7f5d3SJohn Marino {
33*86d7f5d3SJohn Marino struct number *n;
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino n = bmalloc(sizeof(*n));
36*86d7f5d3SJohn Marino n->scale = 0;
37*86d7f5d3SJohn Marino n->number = BN_new();
38*86d7f5d3SJohn Marino if (n->number == NULL)
39*86d7f5d3SJohn Marino err(1, NULL);
40*86d7f5d3SJohn Marino return n;
41*86d7f5d3SJohn Marino }
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino void
free_number(struct number * n)44*86d7f5d3SJohn Marino free_number(struct number *n)
45*86d7f5d3SJohn Marino {
46*86d7f5d3SJohn Marino BN_free(n->number);
47*86d7f5d3SJohn Marino free(n);
48*86d7f5d3SJohn Marino }
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino struct number *
dup_number(const struct number * a)51*86d7f5d3SJohn Marino dup_number(const struct number *a)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino struct number *n;
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marino n = bmalloc(sizeof(*n));
56*86d7f5d3SJohn Marino n->scale = a->scale;
57*86d7f5d3SJohn Marino n->number = BN_dup(a->number);
58*86d7f5d3SJohn Marino bn_checkp(n->number);
59*86d7f5d3SJohn Marino return n;
60*86d7f5d3SJohn Marino }
61*86d7f5d3SJohn Marino
62*86d7f5d3SJohn Marino void *
bmalloc(size_t sz)63*86d7f5d3SJohn Marino bmalloc(size_t sz)
64*86d7f5d3SJohn Marino {
65*86d7f5d3SJohn Marino void *p;
66*86d7f5d3SJohn Marino
67*86d7f5d3SJohn Marino p = malloc(sz);
68*86d7f5d3SJohn Marino if (p == NULL)
69*86d7f5d3SJohn Marino err(1, NULL);
70*86d7f5d3SJohn Marino return p;
71*86d7f5d3SJohn Marino }
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino void *
brealloc(void * p,size_t sz)74*86d7f5d3SJohn Marino brealloc(void *p, size_t sz)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino void *q;
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino q = realloc(p, sz);
79*86d7f5d3SJohn Marino if (q == NULL)
80*86d7f5d3SJohn Marino err(1, NULL);
81*86d7f5d3SJohn Marino return q;
82*86d7f5d3SJohn Marino }
83*86d7f5d3SJohn Marino
84*86d7f5d3SJohn Marino char *
bstrdup(const char * p)85*86d7f5d3SJohn Marino bstrdup(const char *p)
86*86d7f5d3SJohn Marino {
87*86d7f5d3SJohn Marino char *q;
88*86d7f5d3SJohn Marino
89*86d7f5d3SJohn Marino q = strdup(p);
90*86d7f5d3SJohn Marino if (q == NULL)
91*86d7f5d3SJohn Marino err(1, NULL);
92*86d7f5d3SJohn Marino return q;
93*86d7f5d3SJohn Marino }
94*86d7f5d3SJohn Marino
95*86d7f5d3SJohn Marino void
bn_check(int x)96*86d7f5d3SJohn Marino bn_check(int x) \
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino if (x == 0)
99*86d7f5d3SJohn Marino err(1, "big number failure %lx", ERR_get_error());
100*86d7f5d3SJohn Marino }
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino void
bn_checkp(const void * p)103*86d7f5d3SJohn Marino bn_checkp(const void *p) \
104*86d7f5d3SJohn Marino {
105*86d7f5d3SJohn Marino if (p == NULL)
106*86d7f5d3SJohn Marino err(1, "allocation failure %lx", ERR_get_error());
107*86d7f5d3SJohn Marino }
108