| 1 |
/* |
| 2 |
* growbuf.h |
| 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 by Jim Park -- 08/22/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef __GROWBUF_H_ |
| 20 |
#define __GROWBUF_H_ |
| 21 |
|
| 22 |
/** |
| 23 |
* IGrowBuf is the interface to a buffer that grows as you |
| 24 |
* add to the buffer. |
| 25 |
*/ |
| 26 |
class IGrowBuf |
| 27 |
{ |
| 28 |
public: |
| 29 |
typedef int size_type; // Hopefully we can change this at some point |
| 30 |
virtual ~IGrowBuf() {} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add data to the buffer. |
| 34 |
* @param data Pointer to the data to be stored. |
| 35 |
* @param len Size of the data in bytes. |
| 36 |
* @return the previous logical size in bytes before the addition. |
| 37 |
*/ |
| 38 |
virtual size_type add(const void *data, size_type len)=0; |
| 39 |
|
| 40 |
/** |
| 41 |
* Resizes the buffer to hold the number of bytes specified. |
| 42 |
* @param newlen the desired logical size of the buffer. |
| 43 |
*/ |
| 44 |
virtual void resize(size_type newlen)=0; |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the length of the logical buffer in bytes. |
| 48 |
* @return the length in bytes |
| 49 |
*/ |
| 50 |
virtual size_type getlen() const=0; |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the buffer itself. |
| 54 |
* @return Void pointer to the buffer. |
| 55 |
*/ |
| 56 |
virtual void *get() const=0; |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* GrowBuf implements IGrowBuf and grows in 32K chunks. |
| 61 |
*/ |
| 62 |
class GrowBuf : public IGrowBuf |
| 63 |
{ |
| 64 |
private: // don't copy instances |
| 65 |
GrowBuf(const GrowBuf&); |
| 66 |
void operator=(const GrowBuf&); |
| 67 |
|
| 68 |
public: |
| 69 |
GrowBuf(); |
| 70 |
virtual ~GrowBuf(); |
| 71 |
|
| 72 |
/** |
| 73 |
* Set whether to zero out buffer |
| 74 |
* @param zero A boolean value. |
| 75 |
*/ |
| 76 |
void set_zeroing(bool zero); |
| 77 |
|
| 78 |
/** |
| 79 |
* Add data to the buffer. |
| 80 |
* @param data Pointer to the data to be stored. |
| 81 |
* @param len Size of the data in bytes. |
| 82 |
* @return the previous logical size in bytes before the addition. |
| 83 |
*/ |
| 84 |
size_type add(const void *data, size_type len); |
| 85 |
|
| 86 |
/** |
| 87 |
* Resizes the buffer to hold the number of bytes specified. |
| 88 |
* Setting the newlen to 0 will cause the buffer to be at most |
| 89 |
* 2*m_bs bytes long. (It will free the buffer if > 2*m_bs.) |
| 90 |
* @param newlen the desired logical size of the buffer. |
| 91 |
*/ |
| 92 |
void resize(size_type newlen); |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the length of the logical buffer in bytes. |
| 96 |
* @return the length in bytes |
| 97 |
*/ |
| 98 |
size_type getlen() const; |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the buffer itself. |
| 102 |
* @return Void pointer to the buffer. |
| 103 |
*/ |
| 104 |
void *get() const; |
| 105 |
|
| 106 |
void swap(GrowBuf&other); |
| 107 |
|
| 108 |
private: |
| 109 |
void *m_s; /* the storage buffer */ |
| 110 |
size_type m_alloc; /* allocated bytes */ |
| 111 |
size_type m_used; /* how many bytes of the buffer is used? */ |
| 112 |
bool m_zero; /* should storage be zeroed out? */ |
| 113 |
|
| 114 |
protected: |
| 115 |
unsigned short m_bs; // byte-size to grow by |
| 116 |
}; |
| 117 |
|
| 118 |
/** |
| 119 |
* TinyGrowBuf is a derived class that grows the buffer |
| 120 |
* in tiny increments. |
| 121 |
*/ |
| 122 |
class TinyGrowBuf : public GrowBuf { |
| 123 |
public: |
| 124 |
TinyGrowBuf() : GrowBuf() { m_bs=1024; } |
| 125 |
TinyGrowBuf(size_type cb) : GrowBuf() { m_bs=1024; resize(cb); } |
| 126 |
}; |
| 127 |
|
| 128 |
#endif |
| 129 |
|