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