lbnmem.c
上传用户:zbbssh
上传日期:2007-01-08
资源大小:196k
文件大小:3k
源码类别:

CA认证

开发平台:

C/C++

  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. #if HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #if HAVE_STDLIB_H
  19. #include <stdlib.h> /* For malloc() & co. */
  20. #else
  21. void *malloc();
  22. void free();
  23. #endif
  24. #if HAVE_STRING_H
  25. #include <string.h> /* For memset */
  26. #elif HAVE_STRINGS_H
  27. #include <strings.h>
  28. #endif
  29. #if NEED_MEMORY_H
  30. #include <memory.h>
  31. #endif
  32. #if DBMALLOC
  33. /* Development debugging */
  34. #include "../dbmalloc/malloc.h"
  35. #endif
  36. #include "lbn.h"
  37. #include "lbnmem.h"
  38. #include "kludge.h"
  39. #ifndef lbnMemWipe
  40. void
  41. lbnMemWipe(void *ptr, unsigned bytes)
  42. {
  43. memset(ptr, 0, bytes);
  44. }
  45. #define lbnMemWipe(ptr, bytes) memset(ptr, 0, bytes)
  46. #endif
  47. #ifndef lbnMemAlloc
  48. void *
  49. lbnMemAlloc(unsigned bytes)
  50. {
  51. return malloc(bytes);
  52. }
  53. #define lbnMemAlloc(bytes) malloc(bytes)
  54. #endif
  55. #ifndef lbnMemFree
  56. void
  57. lbnMemFree(void *ptr, unsigned bytes)
  58. {
  59. lbnMemWipe(ptr, bytes);
  60. free(ptr);
  61. }
  62. #endif
  63. #ifndef lbnRealloc
  64. #if defined(lbnMemRealloc) || !BNSECURE
  65. void *
  66. lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes)
  67. {
  68. if (ptr) {
  69. BIG(ptr = (char *)ptr - oldbytes;)
  70. if (newbytes < oldbytes)
  71. memmove(ptr, (char *)ptr + oldbytes-newbytes, oldbytes);
  72. }
  73. #ifdef lbnMemRealloc
  74. ptr = lbnMemRealloc(ptr, oldbytes, newbytes);
  75. #else
  76. ptr = realloc(ptr, newbytes);
  77. #endif
  78. if (ptr) {
  79. if (newbytes > oldbytes)
  80. memmove((char *)ptr + newbytes-oldbytes, ptr, oldbytes);
  81. BIG(ptr = (char *)ptr + newbytes;)
  82. }
  83. return ptr;
  84. }
  85. #else /* BNSECURE */
  86. void *
  87. lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes)
  88. {
  89. void *new = lbnMemAlloc(newbytes);
  90. if (!new)
  91. return new;
  92. if (!ptr)
  93. return BIGLITTLE((char *)new+newbytes, new);
  94. /*
  95.  * The following copies are a bit non-obvious in the big-endian case
  96.  * because one of the pointers points to the *end* of allocated memory.
  97.  */
  98. if (newbytes > oldbytes) { /* Copy all of old into part of new */
  99. BIG(new = (char *)new + newbytes;)
  100. BIG(ptr = (char *)ptr - oldbytes;)
  101. memcpy(BIGLITTLE((char *)new-oldbytes, new), ptr, oldbytes);
  102. } else { /* Copy part of old into all of new */
  103. memcpy(new, BIGLITTLE((char *)ptr-newbytes, ptr), newbytes);
  104. BIG(new = (char *)new + newbytes;)
  105. BIG(ptr = (char *)ptr - oldbytes;)
  106. }
  107. lbnMemFree(ptr, oldbytes);
  108. return new;
  109. }
  110. #endif /* BNSECURE */
  111. #endif /* !lbnRealloc */