home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / BNLIB / LBNMEM.C < prev    next >
Text File  |  1996-09-22  |  4KB  |  162 lines

  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. #if defined( INC_ALL ) || defined( INC_CHILD )
  20.   #include "config.h"
  21. #else
  22.   #include "bnlib/config.h"
  23. #endif /* Compiler-specific includes */
  24. #endif
  25.  
  26. /*
  27.  * Some compilers complain about #if FOO if FOO isn't defined,
  28.  * so do the ANSI-mandated thing explicitly...
  29.  */
  30. #ifndef NO_STDLIB_H
  31. #define NO_STDLIB_H 0
  32. #endif
  33. #ifndef NO_STRING_H
  34. #define NO_STRING_H 0
  35. #endif
  36. #ifndef HAVE_STRINGS_H
  37. #define HAVE_STRINGS_H 0
  38. #endif
  39. #ifndef NEED_MEMORY_H
  40. #define NEED_MEMORY_H 0
  41. #endif
  42.  
  43. #if !NO_STDLIB_H
  44. #include <stdlib.h>    /* For malloc() & co. */
  45. #else
  46. void *malloc();
  47. void *realloc();
  48. void free();
  49. #endif
  50.  
  51. #if !NO_STRING_H
  52. #include <string.h>    /* For memset */
  53. #elif HAVE_STRINGS_H
  54. #include <strings.h>
  55. #endif
  56. #if NEED_MEMORY_H
  57. #include <memory.h>
  58. #endif
  59.  
  60. #ifndef DBMALLOC
  61. #define DBMALLOC 0
  62. #endif
  63. #if DBMALLOC
  64. /* Development debugging */
  65. #include "../dbmalloc/malloc.h"
  66. #endif
  67.  
  68. #if defined( INC_ALL ) || defined( INC_CHILD )
  69.   #include "lbn.h"
  70.   #include "lbnmem.h"
  71.  
  72.   #include "kludge.h"
  73. #else
  74.   #include "bnlib/lbn.h"
  75.   #include "bnlib/lbnmem.h"
  76.  
  77.   #include "bnlib/kludge.h"
  78. #endif /* Compiler-specific includes */
  79.  
  80. #ifndef lbnMemWipe
  81. void
  82. lbnMemWipe(void *ptr, unsigned bytes)
  83. {
  84.     memset(ptr, 0, bytes);
  85. }
  86. #define lbnMemWipe(ptr, bytes) memset(ptr, 0, bytes)
  87. #endif
  88.  
  89. #ifndef lbnMemAlloc
  90. void *
  91. lbnMemAlloc(unsigned bytes)
  92. {
  93.     return malloc(bytes);
  94. }
  95. #define lbnMemAlloc(bytes) malloc(bytes)
  96. #endif
  97.  
  98. #ifndef lbnMemFree
  99. void
  100. lbnMemFree(void *ptr, unsigned bytes)
  101. {
  102.     lbnMemWipe(ptr, bytes);
  103.     free(ptr);
  104. }
  105. #endif
  106.  
  107. #ifndef lbnRealloc
  108. #if defined(lbnMemRealloc) || !BNSECURE
  109. void *
  110. lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes)
  111. {
  112.     if (ptr) {
  113.         BIG(ptr = (char *)ptr - oldbytes;)
  114.         if (newbytes < oldbytes)
  115.             memmove(ptr, (char *)ptr + oldbytes-newbytes, oldbytes);
  116.     }
  117. #ifdef lbnMemRealloc
  118.     ptr = lbnMemRealloc(ptr, oldbytes, newbytes);
  119. #else
  120.     ptr = realloc(ptr, newbytes);
  121. #endif
  122.     if (ptr) {
  123.         if (newbytes > oldbytes)
  124.             memmove((char *)ptr + newbytes-oldbytes, ptr, oldbytes);
  125.         BIG(ptr = (char *)ptr + newbytes;)
  126.     }
  127.  
  128.     return ptr;
  129. }
  130.  
  131. #else /* BNSECURE */
  132.  
  133. void *
  134. lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes)
  135. {
  136.     void *new = lbnMemAlloc(newbytes);
  137.  
  138.     if (!new)
  139.         return new;
  140.     if (!ptr)
  141.         return BIGLITTLE((char *)new+newbytes, new);
  142.  
  143.     /*
  144.      * The following copies are a bit non-obvious in the big-endian case
  145.      * because one of the pointers points to the *end* of allocated memory.
  146.      */
  147.     if (newbytes > oldbytes) {    /* Copy all of old into part of new */
  148.         BIG(new = (char *)new + newbytes;)
  149.         BIG(ptr = (char *)ptr - oldbytes;)
  150.         memcpy(BIGLITTLE((char *)new-oldbytes, new), ptr, oldbytes);
  151.     } else {    /* Copy part of old into all of new */
  152.         memcpy(new, BIGLITTLE((char *)ptr-newbytes, ptr), newbytes);
  153.         BIG(new = (char *)new + newbytes;)
  154.         BIG(ptr = (char *)ptr - oldbytes;)
  155.     }
  156.  
  157.     lbnMemFree(ptr, oldbytes);
  158.     return new;
  159. }
  160. #endif /* BNSECURE */
  161. #endif /* !lbnRealloc */
  162.