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-2026 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>66#include <climits>6768#include "nbase.h"6970/* Character pool memory allocation */71#include "charpool.h"72#include "nmap_error.h"7374static CharPool g_charpool (16384);7576const char *cp_strndup(const char *src, int len) {77return g_charpool.dup(src, len);78}79const char *cp_strdup(const char *src) {80return g_charpool.dup(src);81}82void cp_free(void) {83return g_charpool.clear();84}8586class StrTable {87public:88StrTable() {89memset(table, 0, sizeof(table));90for (int i = 1; i <= CHAR_MAX; i++) {91table[i*2] = static_cast<char>(i);92}93}94const char *get(char c) { assert(c >= 0); return &table[c*2]; }95private:96char table[2*(CHAR_MAX + 1)];97};98static StrTable g_table;99100const char *cp_char2str(char c) {101return g_table.get(c);102}103104CharPool::CharPool(size_t init_sz) {105assert(init_sz >= 256);106/* Create our char pool */107currentbucketsz = init_sz;108nexti = 0;109char *b = (char *) safe_malloc(currentbucketsz);110buckets.push_back(b);111}112113void CharPool::clear(void) {114for (BucketList::iterator it=buckets.begin(); it != buckets.end(); it++) {115free(*it);116}117buckets.clear();118}119120const char *CharPool::dup(const char *src, int len) {121if (len < 0)122len = strlen(src);123if (len == 0)124return g_table.get('\0');125else if (len == 1)126return g_table.get(*src);127128int sz = len + 1;129char *p = buckets.back() + nexti;130131while (nexti + sz > currentbucketsz) {132/* Doh! We've got to make room */133currentbucketsz <<= 1;134nexti = 0;135p = (char *) safe_malloc(currentbucketsz);136buckets.push_back(p);137}138139nexti += sz;140p[len] = '\0';141return (const char *) memcpy(p, src, len);142}143144145