1 |
|
|
/* $OpenBSD: mem.c,v 1.7 2015/02/16 20:53:34 jca 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 <openssl/err.h> |
20 |
|
|
|
21 |
|
|
#include <err.h> |
22 |
|
|
#include <stdlib.h> |
23 |
|
|
#include <string.h> |
24 |
|
|
|
25 |
|
|
#include "extern.h" |
26 |
|
|
|
27 |
|
|
struct number * |
28 |
|
|
new_number(void) |
29 |
|
46443 |
{ |
30 |
|
|
struct number *n; |
31 |
|
|
|
32 |
|
46443 |
n = bmalloc(sizeof(*n)); |
33 |
|
46443 |
n->scale = 0; |
34 |
|
46443 |
n->number = BN_new(); |
35 |
✗✓ |
46443 |
if (n->number == NULL) |
36 |
|
|
err(1, NULL); |
37 |
|
46443 |
return n; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
void |
41 |
|
|
free_number(struct number *n) |
42 |
|
72951 |
{ |
43 |
|
72951 |
BN_free(n->number); |
44 |
|
72951 |
free(n); |
45 |
|
72951 |
} |
46 |
|
|
|
47 |
|
|
struct number * |
48 |
|
|
dup_number(const struct number *a) |
49 |
|
22911 |
{ |
50 |
|
|
struct number *n; |
51 |
|
|
|
52 |
|
22911 |
n = bmalloc(sizeof(*n)); |
53 |
|
22911 |
n->scale = a->scale; |
54 |
|
22911 |
n->number = BN_dup(a->number); |
55 |
|
22911 |
bn_checkp(n->number); |
56 |
|
22911 |
return n; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void * |
60 |
|
|
bmalloc(size_t sz) |
61 |
|
101741 |
{ |
62 |
|
|
void *p; |
63 |
|
|
|
64 |
|
101741 |
p = malloc(sz); |
65 |
✗✓ |
101741 |
if (p == NULL) |
66 |
|
|
err(1, NULL); |
67 |
|
101741 |
return p; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
void * |
71 |
|
|
breallocarray(void *p, size_t nmemb, size_t size) |
72 |
|
10584 |
{ |
73 |
|
|
void *q; |
74 |
|
|
|
75 |
|
10584 |
q = reallocarray(p, nmemb, size); |
76 |
✗✓ |
10584 |
if (q == NULL) |
77 |
|
|
err(1, NULL); |
78 |
|
10584 |
return q; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
char * |
82 |
|
|
bstrdup(const char *p) |
83 |
|
4388 |
{ |
84 |
|
|
char *q; |
85 |
|
|
|
86 |
|
4388 |
q = strdup(p); |
87 |
✗✓ |
4388 |
if (q == NULL) |
88 |
|
|
err(1, NULL); |
89 |
|
4388 |
return q; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void |
93 |
|
221939 |
bn_check(int x) \ |
94 |
|
|
{ |
95 |
✗✓ |
221939 |
if (x == 0) |
96 |
|
|
err(1, "big number failure %lx", ERR_get_error()); |
97 |
|
221939 |
} |
98 |
|
|
|
99 |
|
|
void |
100 |
|
94664 |
bn_checkp(const void *p) \ |
101 |
|
|
{ |
102 |
✗✓ |
94664 |
if (p == NULL) |
103 |
|
|
err(1, "allocation failure %lx", ERR_get_error()); |
104 |
|
94664 |
} |