Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nmap
GitHub Repository: nmap/nmap
Path: blob/master/charpool.cc
7205 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-2026 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
#include <climits>
68
69
#include "nbase.h"
70
71
/* Character pool memory allocation */
72
#include "charpool.h"
73
#include "nmap_error.h"
74
75
static CharPool g_charpool (16384);
76
77
const char *cp_strndup(const char *src, int len) {
78
return g_charpool.dup(src, len);
79
}
80
const char *cp_strdup(const char *src) {
81
return g_charpool.dup(src);
82
}
83
void cp_free(void) {
84
return g_charpool.clear();
85
}
86
87
class StrTable {
88
public:
89
StrTable() {
90
memset(table, 0, sizeof(table));
91
for (int i = 1; i <= CHAR_MAX; i++) {
92
table[i*2] = static_cast<char>(i);
93
}
94
}
95
const char *get(char c) { assert(c >= 0); return &table[c*2]; }
96
private:
97
char table[2*(CHAR_MAX + 1)];
98
};
99
static StrTable g_table;
100
101
const char *cp_char2str(char c) {
102
return g_table.get(c);
103
}
104
105
CharPool::CharPool(size_t init_sz) {
106
assert(init_sz >= 256);
107
/* Create our char pool */
108
currentbucketsz = init_sz;
109
nexti = 0;
110
char *b = (char *) safe_malloc(currentbucketsz);
111
buckets.push_back(b);
112
}
113
114
void CharPool::clear(void) {
115
for (BucketList::iterator it=buckets.begin(); it != buckets.end(); it++) {
116
free(*it);
117
}
118
buckets.clear();
119
}
120
121
const char *CharPool::dup(const char *src, int len) {
122
if (len < 0)
123
len = strlen(src);
124
if (len == 0)
125
return g_table.get('\0');
126
else if (len == 1)
127
return g_table.get(*src);
128
129
int sz = len + 1;
130
char *p = buckets.back() + nexti;
131
132
while (nexti + sz > currentbucketsz) {
133
/* Doh! We've got to make room */
134
currentbucketsz <<= 1;
135
nexti = 0;
136
p = (char *) safe_malloc(currentbucketsz);
137
buckets.push_back(p);
138
}
139
140
nexti += sz;
141
p[len] = '\0';
142
return (const char *) memcpy(p, src, len);
143
}
144
145