1/***************************************************************************2* charpool.cc -- Handles Nmap's "character pool" memory allocation *3* system. *4* *5***********************IMPORTANT NMAP LICENSE TERMS************************6*7* The Nmap Security Scanner is (C) 1996-2025 Nmap Software LLC ("The Nmap8* Project"). Nmap is also a registered trademark of the Nmap Project.9*10* This program is distributed under the terms of the Nmap Public Source11* License (NPSL). The exact license text applying to a particular Nmap12* release or source code control revision is contained in the LICENSE13* file distributed with that version of Nmap or source code control14* revision. More Nmap copyright/legal information is available from15* https://nmap.org/book/man-legal.html, and further information on the16* NPSL license itself can be found at https://nmap.org/npsl/ . This17* header summarizes some key points from the Nmap license, but is no18* substitute for the actual license text.19*20* Nmap is generally free for end users to download and use themselves,21* including commercial use. It is available from https://nmap.org.22*23* The Nmap license generally prohibits companies from using and24* redistributing Nmap in commercial products, but we sell a special Nmap25* OEM Edition with a more permissive license and special features for26* this purpose. See https://nmap.org/oem/27*28* If you have received a written Nmap license agreement or contract29* stating terms other than these (such as an Nmap OEM license), you may30* choose to use and redistribute Nmap under those terms instead.31*32* The official Nmap Windows builds include the Npcap software33* (https://npcap.com) for packet capture and transmission. It is under34* separate license terms which forbid redistribution without special35* permission. So the official Nmap Windows builds may not be redistributed36* without special permission (such as an Nmap OEM license).37*38* Source is provided to this software because we believe users have a39* right to know exactly what a program is going to do before they run it.40* This also allows you to audit the software for security holes.41*42* Source code also allows you to port Nmap to new platforms, fix bugs, and43* add new features. You are highly encouraged to submit your changes as a44* Github PR or by email to the [email protected] mailing list for possible45* incorporation into the main distribution. Unless you specify otherwise, it46* is understood that you are offering us very broad rights to use your47* submissions as described in the Nmap Public Source License Contributor48* Agreement. This is important because we fund the project by selling licenses49* with various terms, and also because the inability to relicense code has50* caused devastating problems for other Free Software projects (such as KDE51* and NASM).52*53* The free version of Nmap is distributed in the hope that it will be54* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of55* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,56* indemnification and commercial support are all available through the57* Npcap OEM program--see https://nmap.org/oem/58*59***************************************************************************/6061/* $Id$ */6263#include <stddef.h>64#undef NDEBUG65#include <assert.h>6667#include "nbase.h"6869/* Character pool memory allocation */70#include "charpool.h"71#include "nmap_error.h"7273static CharPool g_charpool (16384);7475const char *cp_strndup(const char *src, int len) {76return g_charpool.dup(src, len);77}78const char *cp_strdup(const char *src) {79return g_charpool.dup(src);80}81void cp_free(void) {82return g_charpool.clear();83}8485class StrTable {86public:87StrTable() {88memset(table, 0, sizeof(table));89for (int i = 1; i <= CHAR_MAX; i++) {90table[i*2] = static_cast<char>(i);91}92}93const char *get(char c) { assert(c >= 0); return &table[c*2]; }94private:95char table[2*(CHAR_MAX + 1)];96};97static StrTable g_table;9899const char *cp_char2str(char c) {100return g_table.get(c);101}102103CharPool::CharPool(size_t init_sz) {104assert(init_sz >= 256);105/* Create our char pool */106currentbucketsz = init_sz;107nexti = 0;108char *b = (char *) safe_malloc(currentbucketsz);109buckets.push_back(b);110}111112void CharPool::clear(void) {113for (BucketList::iterator it=buckets.begin(); it != buckets.end(); it++) {114free(*it);115}116buckets.clear();117}118119const char *CharPool::dup(const char *src, int len) {120if (len < 0)121len = strlen(src);122if (len == 0)123return g_table.get('\0');124else if (len == 1)125return g_table.get(*src);126127int sz = len + 1;128char *p = buckets.back() + nexti;129130while (nexti + sz > currentbucketsz) {131/* Doh! We've got to make room */132currentbucketsz <<= 1;133nexti = 0;134p = (char *) safe_malloc(currentbucketsz);135buckets.push_back(p);136}137138nexti += sz;139p[len] = '\0';140return (const char *) memcpy(p, src, len);141}142143144