Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nmap
GitHub Repository: nmap/nmap
Path: blob/master/charpool.cc
3730 views
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-2025 Nmap Software LLC ("The Nmap
9
* Project"). Nmap is also a registered trademark of the Nmap Project.
10
*
11
* This program is distributed under the terms of the Nmap Public Source
12
* License (NPSL). The exact license text applying to a particular Nmap
13
* release or source code control revision is contained in the LICENSE
14
* file distributed with that version of Nmap or source code control
15
* revision. More Nmap copyright/legal information is available from
16
* https://nmap.org/book/man-legal.html, and further information on the
17
* NPSL license itself can be found at https://nmap.org/npsl/ . This
18
* header summarizes some key points from the Nmap license, but is no
19
* substitute for the actual license text.
20
*
21
* Nmap is generally free for end users to download and use themselves,
22
* including commercial use. It is available from https://nmap.org.
23
*
24
* The Nmap license generally prohibits companies from using and
25
* redistributing Nmap in commercial products, but we sell a special Nmap
26
* OEM Edition with a more permissive license and special features for
27
* this purpose. See https://nmap.org/oem/
28
*
29
* If you have received a written Nmap license agreement or contract
30
* stating terms other than these (such as an Nmap OEM license), you may
31
* choose to use and redistribute Nmap under those terms instead.
32
*
33
* The official Nmap Windows builds include the Npcap software
34
* (https://npcap.com) for packet capture and transmission. It is under
35
* separate license terms which forbid redistribution without special
36
* permission. So the official Nmap Windows builds may not be redistributed
37
* without special permission (such as an Nmap OEM license).
38
*
39
* Source is provided to this software because we believe users have a
40
* right to know exactly what a program is going to do before they run it.
41
* This also allows you to audit the software for security holes.
42
*
43
* Source code also allows you to port Nmap to new platforms, fix bugs, and
44
* add new features. You are highly encouraged to submit your changes as a
45
* Github PR or by email to the [email protected] mailing list for possible
46
* incorporation into the main distribution. Unless you specify otherwise, it
47
* is understood that you are offering us very broad rights to use your
48
* submissions as described in the Nmap Public Source License Contributor
49
* Agreement. This is important because we fund the project by selling licenses
50
* with various terms, and also because the inability to relicense code has
51
* caused devastating problems for other Free Software projects (such as KDE
52
* and NASM).
53
*
54
* The free version of Nmap is distributed in the hope that it will be
55
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
56
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,
57
* indemnification and commercial support are all available through the
58
* Npcap OEM program--see https://nmap.org/oem/
59
*
60
***************************************************************************/
61
62
/* $Id$ */
63
64
#include <stddef.h>
65
#undef NDEBUG
66
#include <assert.h>
67
68
#include "nbase.h"
69
70
/* Character pool memory allocation */
71
#include "charpool.h"
72
#include "nmap_error.h"
73
74
static CharPool g_charpool (16384);
75
76
const char *cp_strndup(const char *src, int len) {
77
return g_charpool.dup(src, len);
78
}
79
const char *cp_strdup(const char *src) {
80
return g_charpool.dup(src);
81
}
82
void cp_free(void) {
83
return g_charpool.clear();
84
}
85
86
class StrTable {
87
public:
88
StrTable() {
89
memset(table, 0, sizeof(table));
90
for (int i = 1; i <= CHAR_MAX; i++) {
91
table[i*2] = static_cast<char>(i);
92
}
93
}
94
const char *get(char c) { assert(c >= 0); return &table[c*2]; }
95
private:
96
char table[2*(CHAR_MAX + 1)];
97
};
98
static StrTable g_table;
99
100
const char *cp_char2str(char c) {
101
return g_table.get(c);
102
}
103
104
CharPool::CharPool(size_t init_sz) {
105
assert(init_sz >= 256);
106
/* Create our char pool */
107
currentbucketsz = init_sz;
108
nexti = 0;
109
char *b = (char *) safe_malloc(currentbucketsz);
110
buckets.push_back(b);
111
}
112
113
void CharPool::clear(void) {
114
for (BucketList::iterator it=buckets.begin(); it != buckets.end(); it++) {
115
free(*it);
116
}
117
buckets.clear();
118
}
119
120
const char *CharPool::dup(const char *src, int len) {
121
if (len < 0)
122
len = strlen(src);
123
if (len == 0)
124
return g_table.get('\0');
125
else if (len == 1)
126
return g_table.get(*src);
127
128
int sz = len + 1;
129
char *p = buckets.back() + nexti;
130
131
while (nexti + sz > currentbucketsz) {
132
/* Doh! We've got to make room */
133
currentbucketsz <<= 1;
134
nexti = 0;
135
p = (char *) safe_malloc(currentbucketsz);
136
buckets.push_back(p);
137
}
138
139
nexti += sz;
140
p[len] = '\0';
141
return (const char *) memcpy(p, src, len);
142
}
143
144