1 /* 2 * $OpenBSD: mem.c,v 1.4 2004/07/11 06:41:48 otto Exp $ 3 * $DragonFly: src/usr.bin/dc/mem.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ 4 */ 5 6 /* 7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 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 BN_free(n->number); 47 free(n); 48 } 49 50 struct number * 51 dup_number(const struct number *a) 52 { 53 struct number *n; 54 55 n = bmalloc(sizeof(*n)); 56 n->scale = a->scale; 57 n->number = BN_dup(a->number); 58 bn_checkp(n->number); 59 return n; 60 } 61 62 void * 63 bmalloc(size_t sz) 64 { 65 void *p; 66 67 p = malloc(sz); 68 if (p == NULL) 69 err(1, NULL); 70 return p; 71 } 72 73 void * 74 brealloc(void *p, size_t sz) 75 { 76 void *q; 77 78 q = realloc(p, sz); 79 if (q == NULL) 80 err(1, NULL); 81 return q; 82 } 83 84 char * 85 bstrdup(const char *p) 86 { 87 char *q; 88 89 q = strdup(p); 90 if (q == NULL) 91 err(1, NULL); 92 return q; 93 } 94 95 void 96 bn_check(int x) \ 97 { 98 if (x == 0) 99 err(1, "big number failure %lx", ERR_get_error()); 100 } 101 102 void 103 bn_checkp(const void *p) \ 104 { 105 if (p == NULL) 106 err(1, "allocation failure %lx", ERR_get_error()); 107 } 108