2
0

lbnmem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 1995 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * lbnmem.c - low-level bignum memory handling.
  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. * Some compilers complain about #if FOO if FOO isn't defined,
  23. * so do the ANSI-mandated thing explicitly...
  24. */
  25. #ifndef NO_STDLIB_H
  26. #define NO_STDLIB_H 0
  27. #endif
  28. #ifndef NO_STRING_H
  29. #define NO_STRING_H 0
  30. #endif
  31. #ifndef HAVE_STRINGS_H
  32. #define HAVE_STRINGS_H 0
  33. #endif
  34. #if !NO_STDLIB_H
  35. #include <stdlib.h> /* For malloc() & co. */
  36. #else
  37. void *malloc();
  38. void *realloc();
  39. void free();
  40. #endif
  41. #if !NO_STRING_H
  42. #include <string.h> /* For memset */
  43. #elif HAVE_STRINGS_H
  44. #include <strings.h>
  45. #endif
  46. #ifndef DBMALLOC
  47. #define DBMALLOC 0
  48. #endif
  49. #if DBMALLOC
  50. /* Development debugging */
  51. #include "../dbmalloc/malloc.h"
  52. #endif
  53. #include "lbn.h"
  54. #include "lbnmem.h"
  55. #include "kludge.h"
  56. #include "zrtp.h"
  57. #ifndef lbnMemWipe
  58. void
  59. lbnMemWipe(void *ptr, unsigned bytes)
  60. {
  61. zrtp_memset(ptr, 0, bytes);
  62. }
  63. #define lbnMemWipe(ptr, bytes) memset(ptr, 0, bytes)
  64. #endif
  65. #ifndef lbnMemAlloc
  66. void *
  67. lbnMemAlloc(unsigned bytes)
  68. {
  69. return zrtp_sys_alloc(bytes);
  70. }
  71. #endif
  72. #ifndef lbnMemFree
  73. void
  74. lbnMemFree(void *ptr, unsigned bytes)
  75. {
  76. lbnMemWipe(ptr, bytes);
  77. zrtp_sys_free(ptr);
  78. }
  79. #endif
  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 */