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 |
|
|
{ |
30 |
|
|
struct number *n; |
31 |
|
|
|
32 |
|
835974 |
n = bmalloc(sizeof(*n)); |
33 |
|
417987 |
n->scale = 0; |
34 |
|
417987 |
n->number = BN_new(); |
35 |
✗✓ |
417987 |
if (n->number == NULL) |
36 |
|
|
err(1, NULL); |
37 |
|
417987 |
return n; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
void |
41 |
|
|
free_number(struct number *n) |
42 |
|
|
{ |
43 |
|
1313118 |
BN_free(n->number); |
44 |
|
656559 |
free(n); |
45 |
|
656559 |
} |
46 |
|
|
|
47 |
|
|
struct number * |
48 |
|
|
dup_number(const struct number *a) |
49 |
|
|
{ |
50 |
|
|
struct number *n; |
51 |
|
|
|
52 |
|
412398 |
n = bmalloc(sizeof(*n)); |
53 |
|
206199 |
n->scale = a->scale; |
54 |
|
206199 |
n->number = BN_dup(a->number); |
55 |
|
206199 |
bn_checkp(n->number); |
56 |
|
206199 |
return n; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void * |
60 |
|
|
bmalloc(size_t sz) |
61 |
|
|
{ |
62 |
|
|
void *p; |
63 |
|
|
|
64 |
|
1831338 |
p = malloc(sz); |
65 |
✗✓ |
915669 |
if (p == NULL) |
66 |
|
|
err(1, NULL); |
67 |
|
915669 |
return p; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
void * |
71 |
|
|
breallocarray(void *p, size_t nmemb, size_t size) |
72 |
|
|
{ |
73 |
|
|
void *q; |
74 |
|
|
|
75 |
|
190512 |
q = reallocarray(p, nmemb, size); |
76 |
✗✓ |
95256 |
if (q == NULL) |
77 |
|
|
err(1, NULL); |
78 |
|
95256 |
return q; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
char * |
82 |
|
|
bstrdup(const char *p) |
83 |
|
|
{ |
84 |
|
|
char *q; |
85 |
|
|
|
86 |
|
78984 |
q = strdup(p); |
87 |
✗✓ |
39492 |
if (q == NULL) |
88 |
|
|
err(1, NULL); |
89 |
|
39492 |
return q; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void |
93 |
|
|
bn_check(int x) \ |
94 |
|
|
{ |
95 |
✗✓ |
3994902 |
if (x == 0) |
96 |
|
|
err(1, "big number failure %lx", ERR_get_error()); |
97 |
|
1997451 |
} |
98 |
|
|
|
99 |
|
|
void |
100 |
|
|
bn_checkp(const void *p) \ |
101 |
|
|
{ |
102 |
✗✓ |
1703952 |
if (p == NULL) |
103 |
|
|
err(1, "allocation failure %lx", ERR_get_error()); |
104 |
|
851976 |
} |