home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / charpool.c < prev    next >
C/C++ Source or Header  |  2001-03-07  |  5KB  |  138 lines

  1.  
  2. /***********************************************************************/
  3. /* charpool.c -- Handles Nmap's "character pool" memory allocation     */
  4. /* system.                                                             */
  5. /*                                                                     */
  6. /***********************************************************************/
  7. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  8. /*  program is free software; you can redistribute it and/or modify    */
  9. /*  it under the terms of the GNU General Public License as published  */
  10. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  11. /*  right to use, modify, and redistribute this software under certain */
  12. /*  conditions.  If this license is unacceptable to you, we may be     */
  13. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  14. /*                                                                     */
  15. /*  If you received these files with a written license agreement       */
  16. /*  stating terms other than the (GPL) terms above, then that          */
  17. /*  alternative license agreement takes precendence over this comment. */
  18. /*                                                                     */
  19. /*  Source is provided to this software because we believe users have  */
  20. /*  a right to know exactly what a program is going to do before they  */
  21. /*  run it.  This also allows you to audit the software for security   */
  22. /*  holes (none have been found so far).                               */
  23. /*                                                                     */
  24. /*  Source code also allows you to port Nmap to new platforms, fix     */
  25. /*  bugs, and add new features.  You are highly encouraged to send     */
  26. /*  your changes to fyodor@insecure.org for possible incorporation     */
  27. /*  into the main distribution.  By sending these changes to Fyodor or */
  28. /*  one the insecure.org development mailing lists, it is assumed that */
  29. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  30. /*  reuse, modify, and relicense the code.  This is important because  */
  31. /*  the inability to relicense code has caused devastating problems    */
  32. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  33. /*  will always be available Open Source.  If you wish to specify      */
  34. /*  special license conditions of your contributions, just say so      */
  35. /*  when you send them.                                                */
  36. /*                                                                     */
  37. /*  This program is distributed in the hope that it will be useful,    */
  38. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  39. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  40. /*  General Public License for more details (                          */
  41. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  42. /*                                                                     */
  43. /***********************************************************************/
  44.  
  45. /* $Id: charpool.c,v 1.5 2001/03/07 20:34:56 fyodor Exp $ */
  46.  
  47.  
  48. /* Character pool memory allocation */
  49. #include "charpool.h"
  50.  
  51. static char *charpool[16];
  52. static int currentcharpool;
  53. static int currentcharpoolsz;
  54. static char *nextchar;
  55. static int charpool_initialized = 0;
  56.  
  57. #define ALIGN_ON sizeof(char *)
  58.  
  59. static int cp_init(void) {
  60.   /* Create our char pool */
  61.   currentcharpool = 0;
  62.   currentcharpoolsz = 16384;
  63.   nextchar = charpool[0] = (char *) safe_malloc(currentcharpoolsz);
  64.   charpool_initialized = 1;
  65.   return 0;
  66. }
  67.  
  68. static inline void cp_grow(void) {
  69.   /* Doh!  We've got to make room */
  70.   if (++currentcharpool > 15) {
  71.     fatal("Character Pool is out of buckets!");
  72.   }
  73.   currentcharpoolsz <<= 1;
  74.  
  75.   nextchar = charpool[currentcharpool] = (char *)
  76.     safe_malloc(currentcharpoolsz);
  77. }
  78.  
  79. static inline void cp_align(void) {
  80.   int res;
  81.   if ((res = (nextchar - charpool[currentcharpool]) % ALIGN_ON)) {
  82.     nextchar += ALIGN_ON - res;
  83.   }
  84.   return;
  85. }
  86.  
  87. void *cp_alloc(int sz) {
  88.   char *p;
  89.   int modulus;
  90.  
  91.   if (!charpool_initialized) cp_init();
  92.  
  93.   if ((modulus = sz % ALIGN_ON))
  94.     sz += modulus;
  95.   
  96.   if ((nextchar - charpool[currentcharpool]) + sz <= currentcharpoolsz) {
  97.     p = nextchar;
  98.     nextchar += sz;
  99.     return p;
  100.   }
  101.   /* Doh!  We've got to make room */
  102.   cp_grow();
  103.  
  104.  return cp_alloc(sz);
  105.  
  106. }
  107.  
  108. char *cp_strdup(const char *src) {
  109. const char *p;
  110. char *q;
  111. /* end points to the first illegal char */
  112. char *end;
  113. int modulus;
  114.  
  115.  if (!charpool_initialized) 
  116.    cp_init();
  117.  
  118.  end = charpool[currentcharpool] + currentcharpoolsz;
  119.  q = nextchar;
  120.  p = src;
  121.  while((nextchar < end) && *p) {
  122.    *nextchar++ = *p++;
  123.  }
  124.  
  125.  if (nextchar < end) {
  126.    /* Goody, we have space */
  127.    *nextchar++ = '\0';
  128.    if ((modulus = (nextchar - q) % ALIGN_ON))
  129.      nextchar += ALIGN_ON - modulus;
  130.    return q;
  131.  }
  132.  
  133.  /* Doh!  We ran out -- need to allocate more */
  134.  cp_grow();
  135.  
  136.  return cp_strdup(src);
  137. }
  138.