OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / nmap / charpool.cc
1
2 /***************************************************************************
3  * charpool.cc -- Handles Nmap's "character pool" memory allocation        *
4  * system.                                                                 *
5  *                                                                         *
6  ***********************IMPORTANT NMAP LICENSE TERMS************************
7  *                                                                         *
8  * The Nmap Security Scanner is (C) 1996-2006 Insecure.Com LLC. Nmap is    *
9  * also a registered trademark of Insecure.Com LLC.  This program is free  *
10  * software; you may redistribute and/or modify it under the terms of the  *
11  * GNU General Public License as published by the Free Software            *
12  * Foundation; Version 2 with the clarifications and exceptions described  *
13  * below.  This guarantees your right to use, modify, and redistribute     *
14  * this software under certain conditions.  If you wish to embed Nmap      *
15  * technology into proprietary software, we sell alternative licenses      *
16  * (contact sales@insecure.com).  Dozens of software vendors already       *
17  * license Nmap technology such as host discovery, port scanning, OS       *
18  * detection, and version detection.                                       *
19  *                                                                         *
20  * Note that the GPL places important restrictions on "derived works", yet *
21  * it does not provide a detailed definition of that term.  To avoid       *
22  * misunderstandings, we consider an application to constitute a           *
23  * "derivative work" for the purpose of this license if it does any of the *
24  * following:                                                              *
25  * o Integrates source code from Nmap                                      *
26  * o Reads or includes Nmap copyrighted data files, such as                *
27  *   nmap-os-fingerprints or nmap-service-probes.                          *
28  * o Executes Nmap and parses the results (as opposed to typical shell or  *
29  *   execution-menu apps, which simply display raw Nmap output and so are  *
30  *   not derivative works.)                                                * 
31  * o Integrates/includes/aggregates Nmap into a proprietary executable     *
32  *   installer, such as those produced by InstallShield.                   *
33  * o Links to a library or executes a program that does any of the above   *
34  *                                                                         *
35  * The term "Nmap" should be taken to also include any portions or derived *
36  * works of Nmap.  This list is not exclusive, but is just meant to        *
37  * clarify our interpretation of derived works with some common examples.  *
38  * These restrictions only apply when you actually redistribute Nmap.  For *
39  * example, nothing stops you from writing and selling a proprietary       *
40  * front-end to Nmap.  Just distribute it by itself, and point people to   *
41  * http://insecure.org/nmap/ to download Nmap.                             *
42  *                                                                         *
43  * We don't consider these to be added restrictions on top of the GPL, but *
44  * just a clarification of how we interpret "derived works" as it applies  *
45  * to our GPL-licensed Nmap product.  This is similar to the way Linus     *
46  * Torvalds has announced his interpretation of how "derived works"        *
47  * applies to Linux kernel modules.  Our interpretation refers only to     *
48  * Nmap - we don't speak for any other GPL products.                       *
49  *                                                                         *
50  * If you have any questions about the GPL licensing restrictions on using *
51  * Nmap in non-GPL works, we would be happy to help.  As mentioned above,  *
52  * we also offer alternative license to integrate Nmap into proprietary    *
53  * applications and appliances.  These contracts have been sold to dozens  *
54  * of software vendors, and generally include a perpetual license as well  *
55  * as providing for priority support and updates as well as helping to     *
56  * fund the continued development of Nmap technology.  Please email        *
57  * sales@insecure.com for further information.                             *
58  *                                                                         *
59  * As a special exception to the GPL terms, Insecure.Com LLC grants        *
60  * permission to link the code of this program with any version of the     *
61  * OpenSSL library which is distributed under a license identical to that  *
62  * listed in the included Copying.OpenSSL file, and distribute linked      *
63  * combinations including the two. You must obey the GNU GPL in all        *
64  * respects for all of the code used other than OpenSSL.  If you modify    *
65  * this file, you may extend this exception to your version of the file,   *
66  * but you are not obligated to do so.                                     *
67  *                                                                         *
68  * If you received these files with a written license agreement or         *
69  * contract stating terms other than the terms above, then that            *
70  * alternative license agreement takes precedence over these comments.     *
71  *                                                                         *
72  * Source is provided to this software because we believe users have a     *
73  * right to know exactly what a program is going to do before they run it. *
74  * This also allows you to audit the software for security holes (none     *
75  * have been found so far).                                                *
76  *                                                                         *
77  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
78  * and add new features.  You are highly encouraged to send your changes   *
79  * to fyodor@insecure.org for possible incorporation into the main         *
80  * distribution.  By sending these changes to Fyodor or one the            *
81  * Insecure.Org development mailing lists, it is assumed that you are      *
82  * offering Fyodor and Insecure.Com LLC the unlimited, non-exclusive right *
83  * to reuse, modify, and relicense the code.  Nmap will always be          *
84  * available Open Source, but this is important because the inability to   *
85  * relicense code has caused devastating problems for other Free Software  *
86  * projects (such as KDE and NASM).  We also occasionally relicense the    *
87  * code to third parties as discussed above.  If you wish to specify       *
88  * special license conditions of your contributions, just say so when you  *
89  * send them.                                                              *
90  *                                                                         *
91  * This program is distributed in the hope that it will be useful, but     *
92  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
93  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
94  * General Public License for more details at                              *
95  * http://www.gnu.org/copyleft/gpl.html , or in the COPYING file included  *
96  * with Nmap.                                                              *
97  *                                                                         *
98  ***************************************************************************/
99
100 /* $Id: charpool.cc 3869 2006-08-25 01:47:49Z fyodor $ */
101
102
103 /* Character pool memory allocation */
104 #include "charpool.h"
105
106 static char *charpool[16];
107 static int currentcharpool;
108 static int currentcharpoolsz;
109 static char *nextchar;
110
111 #define ALIGN_ON sizeof(char *)
112
113 static int cp_init(void) {
114   static int charpool_initialized = 0;
115   if (charpool_initialized) return 0;
116
117   /* Create our char pool */
118   currentcharpool = 0;
119   currentcharpoolsz = 16384;
120   nextchar = charpool[0] = (char *) safe_malloc(currentcharpoolsz);
121   charpool_initialized = 1;
122   return 0;
123 }
124
125 void cp_free(void) {
126   int ccp;
127   for(ccp=0; ccp <= currentcharpool; ccp++)
128     if(charpool[ccp]){
129       free(charpool[ccp]);
130       charpool[ccp] = NULL;
131   }
132   currentcharpool = 0;
133 }
134
135 static inline void cp_grow(void) {
136   /* Doh!  We've got to make room */
137   if (++currentcharpool > 15) {
138     fatal("Character Pool is out of buckets!");
139   }
140   currentcharpoolsz <<= 1;
141
142   nextchar = charpool[currentcharpool] = (char *)
143     safe_malloc(currentcharpoolsz);
144 }
145
146 void *cp_alloc(int sz) {
147   char *p;
148   int modulus;
149
150   cp_init();
151
152   if ((modulus = sz % ALIGN_ON))
153     sz += ALIGN_ON - modulus;
154   
155   if ((nextchar - charpool[currentcharpool]) + sz <= currentcharpoolsz) {
156     p = nextchar;
157     nextchar += sz;
158     return p;
159   }
160   /* Doh!  We've got to make room */
161   cp_grow();
162
163  return cp_alloc(sz);
164  
165 }
166
167 char *cp_strdup(const char *src) {
168 const char *p;
169 char *q;
170 /* end points to the first illegal char */
171 char *end;
172 int modulus;
173
174  cp_init();
175
176  end = charpool[currentcharpool] + currentcharpoolsz;
177  q = nextchar;
178  p = src;
179  while((nextchar < end) && *p) {
180    *nextchar++ = *p++;
181  }
182
183  if (nextchar < end) {
184    /* Goody, we have space */
185    *nextchar++ = '\0';
186    if ((modulus = (nextchar - q) % ALIGN_ON))
187      nextchar += ALIGN_ON - modulus;
188    return q;
189  }
190
191  /* Doh!  We ran out -- need to allocate more */
192  cp_grow();
193
194  return cp_strdup(src);
195 }