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