xref: /freebsd/usr.bin/dc/mem.c (revision aa0a1e58)
1 /*	$OpenBSD: mem.c,v 1.5 2009/10/27 23:59:37 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
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 *
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
44 free_number(struct number *n)
45 {
46 
47 	BN_free(n->number);
48 	free(n);
49 }
50 
51 struct number *
52 dup_number(const struct number *a)
53 {
54 	struct number *n;
55 
56 	n = bmalloc(sizeof(*n));
57 	n->scale = a->scale;
58 	n->number = BN_dup(a->number);
59 	bn_checkp(n->number);
60 	return (n);
61 }
62 
63 void *
64 bmalloc(size_t sz)
65 {
66 	void *p;
67 
68 	p = malloc(sz);
69 	if (p == NULL)
70 		err(1, NULL);
71 	return (p);
72 }
73 
74 void *
75 brealloc(void *p, size_t sz)
76 {
77 	void *q;
78 
79 	q = realloc(p, sz);
80 	if (q == NULL)
81 		err(1, NULL);
82 	return (q);
83 }
84 
85 char *
86 bstrdup(const char *p)
87 {
88 	char *q;
89 
90 	q = strdup(p);
91 	if (q == NULL)
92 		err(1, NULL);
93 	return (q);
94 }
95 
96 void
97 bn_check(int x)						\
98 {
99 
100 	if (x == 0)
101 		err(1, "big number failure %lx", ERR_get_error());
102 }
103 
104 void
105 bn_checkp(const void *p)						\
106 {
107 
108 	if (p == NULL)
109 		err(1, "allocation failure %lx", ERR_get_error());
110 }
111