lbnmem.c 3.0 KB

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