Debian Codegraph @

Source/growbuf.cpp

raw
1 /*
2 * growbuf.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support and Doxygen comments by Jim Park -- 07/31/2007
17 */
18
19 #include "Platform.h"
20 #include "growbuf.h"
21
22 #include <cstdlib> // for malloc/free
23 #include <cstring> // for memcpy
24 #include <cstdio> // for f*
25 #include <algorithm> // for std::min
26 #include <cassert>
27 #include "tchar.h"
28 #include "util.h"
29
30
31 using namespace std;
32
33 // Default constructor
34 GrowBuf::GrowBuf() { m_alloc=m_used=0, m_zero=false, m_s=NULL, m_bs=32768; }
35
36 // Destructor
37 GrowBuf::~GrowBuf() { free(m_s); }
38
39 void GrowBuf::set_zeroing(bool zero) { m_zero=zero; }
40
41 GrowBuf::size_type GrowBuf::add(const void *data, GrowBuf::size_type len)
42 {
43 if (len<=0) return 0; // BUGBUG: Why is this returning 0? It should return m_used?
44 resize(m_used+len);
45 memcpy((BYTE*)m_s+m_used-len,data,len);
46 return m_used-len;
47 }
48
49 void GrowBuf::resize(GrowBuf::size_type newlen)
50 {
51 const size_type orgalloc=m_alloc;
52 const size_type orgused=m_used;
53
54 m_used=newlen;
55 if (newlen > m_alloc)
56 {
57 void *newstor;
58
59 // Jim Park: Not sure why we don't just add m_bs. Multiplying by 2
60 // makes m_bs meaningless after a few resizes. So TinyGrowBuf
61 // isn't very tiny.
62 m_alloc = newlen*2 + m_bs;
63 newstor = realloc(m_s, m_alloc);
64 if (!newstor)
65 {
66 extern int g_display_errors;
67 #ifdef _DEBUG
68 if (g_display_errors)
69 {
70 PrintColorFmtMsg_WARN(_T("\nwarning: realloc(%d) failed, trying malloc(%d)!\n"),m_alloc,newlen);
71 };
72 #endif
73 m_alloc=newlen; // try to malloc the minimum needed
74 newstor=malloc(m_alloc);
75 if (!newstor)
76 {
77 extern void quit();
78 if (g_display_errors)
79 {
80 assert(sizeof(size_type) == sizeof(int));
81 PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: GrowBuf realloc/malloc(%d) failed.\n"),m_alloc);
82 }
83 quit();
84 }
85 memcpy(newstor,m_s,min(newlen,orgalloc));
86 free(m_s);
87 }
88 m_s=newstor;
89 }
90
91 // Zero out the new buffer area
92 if (m_zero && m_used > orgused)
93 memset((BYTE*)m_s + orgused, 0, m_used - orgused);
94
95 if (!m_used && m_alloc > 2*m_bs) // only free if you resize to 0 and we're > 64k or
96 // 2K in the case of TinyGrowBuf
97 {
98 m_alloc=0;
99 free(m_s);
100 m_s=NULL;
101 }
102 }
103
104 GrowBuf::size_type GrowBuf::getlen() const { return m_used; }
105 void *GrowBuf::get() const { return m_s; }
106
107 void GrowBuf::swap(GrowBuf&other)
108 {
109 std::swap(m_s, other.m_s);
110 std::swap(m_alloc, other.m_alloc);
111 std::swap(m_used, other.m_used);
112 std::swap(m_zero, other.m_zero);
113 std::swap(m_bs, other.m_bs);
114 }