Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * lbnmem.c - low-level bignum memory handling. |
| 3 | * |
| 4 | * Copyright (c) 1995 Colin Plumb. All rights reserved. |
| 5 | * For licensing and other legal details, see the file legal.c. |
| 6 | * |
| 7 | * Note that in all cases, the pointers passed around |
| 8 | * are pointers to the *least* significant end of the word. |
| 9 | * On big-endian machines, these are pointers to the *end* |
| 10 | * of the allocated range. |
| 11 | * |
| 12 | * BNSECURE is a simple level of security; for more security |
| 13 | * change these function to use locked unswappable memory. |
| 14 | */ |
| 15 | #ifndef HAVE_CONFIG_H |
| 16 | #define HAVE_CONFIG_H 0 |
| 17 | #endif |
| 18 | #if HAVE_CONFIG_H |
| 19 | #include <bnconfig.h> |
| 20 | #endif |
| 21 | |
| 22 | /* |
| 23 | * Some compilers complain about #if FOO if FOO isn't defined, |
| 24 | * so do the ANSI-mandated thing explicitly... |
| 25 | */ |
| 26 | #ifndef NO_STDLIB_H |
| 27 | #define NO_STDLIB_H 0 |
| 28 | #endif |
| 29 | #ifndef NO_STRING_H |
| 30 | #define NO_STRING_H 0 |
| 31 | #endif |
| 32 | #ifndef HAVE_STRINGS_H |
| 33 | #define HAVE_STRINGS_H 0 |
| 34 | #endif |
| 35 | #ifndef NEED_MEMORY_H |
| 36 | #define NEED_MEMORY_H 0 |
| 37 | #endif |
| 38 | |
| 39 | #if !NO_STDLIB_H |
| 40 | #include <stdlib.h> /* For malloc() & co. */ |
| 41 | #else |
| 42 | void *malloc(); |
| 43 | void *realloc(); |
| 44 | void free(); |
| 45 | #endif |
| 46 | |
| 47 | #if !NO_STRING_H |
| 48 | #include <string.h> /* For memset */ |
| 49 | #elif HAVE_STRINGS_H |
| 50 | #include <strings.h> |
| 51 | #endif |
| 52 | #if NEED_MEMORY_H |
| 53 | #include <memory.h> |
| 54 | #endif |
| 55 | |
| 56 | #ifndef DBMALLOC |
| 57 | #define DBMALLOC 0 |
| 58 | #endif |
| 59 | #if DBMALLOC |
| 60 | /* Development debugging */ |
| 61 | #include "../dbmalloc/malloc.h" |
| 62 | #endif |
| 63 | |
| 64 | #include "lbn.h" |
| 65 | #include "lbnmem.h" |
| 66 | |
| 67 | #include "kludge.h" |
| 68 | |
| 69 | #ifndef lbnMemWipe |
| 70 | void |
| 71 | lbnMemWipe(void *ptr, unsigned bytes) |
| 72 | { |
| 73 | memset(ptr, 0, bytes); |
| 74 | } |
| 75 | #define lbnMemWipe(ptr, bytes) memset(ptr, 0, bytes) |
| 76 | #endif |
| 77 | |
| 78 | #ifndef lbnMemAlloc |
| 79 | void * |
| 80 | lbnMemAlloc(unsigned bytes) |
| 81 | { |
| 82 | return malloc(bytes); |
| 83 | } |
| 84 | #define lbnMemAlloc(bytes) malloc(bytes) |
| 85 | #endif |
| 86 | |
| 87 | #ifndef lbnMemFree |
| 88 | void |
| 89 | lbnMemFree(void *ptr, unsigned bytes) |
| 90 | { |
| 91 | lbnMemWipe(ptr, bytes); |
| 92 | free(ptr); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #ifndef lbnRealloc |
| 97 | #if defined(lbnMemRealloc) || !BNSECURE |
| 98 | void * |
| 99 | lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes) |
| 100 | { |
| 101 | if (ptr) { |
| 102 | BIG(ptr = (char *)ptr - oldbytes;) |
| 103 | if (newbytes < oldbytes) |
| 104 | memmove(ptr, (char *)ptr + oldbytes-newbytes, oldbytes); |
| 105 | } |
| 106 | #ifdef lbnMemRealloc |
| 107 | ptr = lbnMemRealloc(ptr, oldbytes, newbytes); |
| 108 | #else |
| 109 | ptr = realloc(ptr, newbytes); |
| 110 | #endif |
| 111 | if (ptr) { |
| 112 | if (newbytes > oldbytes) |
| 113 | memmove((char *)ptr + newbytes-oldbytes, ptr, oldbytes); |
| 114 | BIG(ptr = (char *)ptr + newbytes;) |
| 115 | } |
| 116 | |
| 117 | return ptr; |
| 118 | } |
| 119 | |
| 120 | #else /* BNSECURE */ |
| 121 | |
| 122 | void * |
| 123 | lbnRealloc(void *oldptr, unsigned oldbytes, unsigned newbytes) |
| 124 | { |
| 125 | void *newptr = lbnMemAlloc(newbytes); |
| 126 | |
| 127 | if (!newptr) |
| 128 | return newptr; |
| 129 | if (!oldptr) |
| 130 | return BIGLITTLE((char *)newptr+newbytes, newptr); |
| 131 | |
| 132 | /* |
| 133 | * The following copies are a bit non-obvious in the big-endian case |
| 134 | * because one of the pointers points to the *end* of allocated memory. |
| 135 | */ |
| 136 | if (newbytes > oldbytes) { /* Copy all of old into part of new */ |
| 137 | BIG(newptr = (char *)newptr + newbytes;) |
| 138 | BIG(oldptr = (char *)oldptr - oldbytes;) |
| 139 | memcpy(BIGLITTLE((char *)newptr-oldbytes, newptr), oldptr, |
| 140 | oldbytes); |
| 141 | } else { /* Copy part of old into all of new */ |
| 142 | memcpy(newptr, BIGLITTLE((char *)oldptr-newbytes, oldptr), |
| 143 | newbytes); |
| 144 | BIG(newptr = (char *)newptr + newbytes;) |
| 145 | BIG(oldptr = (char *)oldptr - oldbytes;) |
| 146 | } |
| 147 | |
| 148 | lbnMemFree(oldptr, oldbytes); |
| 149 | |
| 150 | return newptr; |
| 151 | } |
| 152 | #endif /* BNSECURE */ |
| 153 | #endif /* !lbnRealloc */ |