1 /* $OpenBSD: mem.c,v 1.10 2017/12/17 08:21:10 otto 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 n = bmalloc(sizeof(*n)); 33 n->scale = 0; 34 n->number = BN_new(); 35 bn_checkp(n->number); 36 return n; 37 } 38 39 void 40 free_number(struct number *n) 41 { 42 BN_free(n->number); 43 free(n); 44 } 45 46 struct number * 47 dup_number(const struct number *a) 48 { 49 struct number *n; 50 51 n = bmalloc(sizeof(*n)); 52 n->scale = a->scale; 53 n->number = BN_dup(a->number); 54 bn_checkp(n->number); 55 return n; 56 } 57 58 void * 59 bmalloc(size_t sz) 60 { 61 void *p; 62 63 p = malloc(sz); 64 if (p == NULL) 65 err(1, NULL); 66 return p; 67 } 68 69 void * 70 breallocarray(void *p, size_t nmemb, size_t size) 71 { 72 void *q; 73 74 q = reallocarray(p, nmemb, size); 75 if (q == NULL) 76 err(1, NULL); 77 return q; 78 } 79 80 char * 81 bstrdup(const char *p) 82 { 83 char *q; 84 85 q = strdup(p); 86 if (q == NULL) 87 err(1, NULL); 88 return q; 89 } 90 91 void 92 bn_check(int x) 93 { 94 if (x == 0) { 95 ERR_load_BN_strings(); 96 errx(1, "BN failure: %s", 97 ERR_reason_error_string(ERR_get_error())); 98 } 99 } 100 101 void 102 bn_checkp(const void *p) 103 { 104 if (p == NULL) { 105 ERR_load_BN_strings(); 106 errx(1, "BN failure: %s", 107 ERR_reason_error_string(ERR_get_error())); 108 } 109 } 110