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