]> git.ipfire.org Git - thirdparty/squid.git/blame - src/MemBuf.h
Various audit updates
[thirdparty/squid.git] / src / MemBuf.h
CommitLineData
528b2c61 1/*
528b2c61 2 *
3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
5 *
6 * Squid is the result of efforts by numerous individuals from
7 * the Internet community; see the CONTRIBUTORS file for full
8 * details. Many organizations have provided support for Squid's
9 * development; see the SPONSORS file for full details. Squid is
10 * Copyrighted (C) 2001 by the Regents of the University of
11 * California; see the COPYRIGHT file for full details. Squid
12 * incorporates software developed and/or copyrighted by other
13 * sources; see the CREDITS file for full details.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
26ac0430 19 *
528b2c61 20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26ac0430 24 *
528b2c61 25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
28 *
29 */
30
31#ifndef SQUID_MEMBUF_H
32#define SQUID_MEMBUF_H
33
aa839030 34#include "cbdata.h"
25b6a907 35#include "Packer.h"
36
63be0a78 37/**
38 * Auto-growing memory-resident buffer with printf interface
39 *
40 \todo XXX: convert global memBuf*() functions into methods
41 */
62e76326 42class MemBuf
43{
44
45public:
528b2c61 46 _SQUID_INLINE_ MemBuf();
032785bf 47 _SQUID_INLINE_ ~MemBuf();
48
63be0a78 49 /// start of the added data
50 char *content() { return buf; }
032785bf 51
63be0a78 52 /// start of the added data
53 const char *content() const { return buf; }
9bcd2d11 54
63be0a78 55 /// available data size
56 mb_size_t contentSize() const { return size; }
032785bf 57
63be0a78 58 /**
59 * Whether the buffer contains any data.
60 \retval true if data exists in the buffer
61 \retval false if data exists in the buffer
62 */
032785bf 63 bool hasContent() const { return size > 0; }
64
3aa3d385
AR
65 /// returns buffer after data; does not check space existence
66 char *space() { return buf + size; } ///< space to add data
67
68 /// Returns buffer following data, after possibly growing the buffer to
69 /// accommodate addition of the required bytes PLUS a 0-terminator char.
70 /// The caller is not required to terminate the buffer, but MemBuf does
71 /// terminate internally, trading termination for size calculation bugs.
72 char *space(mb_size_t required) { if (size + required >= capacity) grow(size + required + 1); return buf + size; }
032785bf 73
74 mb_size_t spaceSize() const;
63be0a78 75
76 /**
77 * Whether the buffer contains any data space available.
0477a072
AJ
78 \retval true if data can be added to the buffer
79 \retval false if the buffer is full
63be0a78 80 */
032785bf 81 bool hasSpace() const { return size+1 < capacity; }
82
83 mb_size_t potentialSpaceSize() const; // accounts for possible growth
84 bool hasPotentialSpace() const { return potentialSpaceSize() > 0; }
85
63be0a78 86 /// \note there is currently no stretch() method to grow without appending
032785bf 87
88 void consume(mb_size_t sz); // removes sz bytes, moving content left
24eac830 89 void consumeWhitespacePrefix(); ///< removes all prefix whitespace, moving content left
1177ec50 90
032785bf 91 void append(const char *c, mb_size_t sz); // grows if needed and possible
92 void appended(mb_size_t sz); // updates content size after external append
821beb5e 93 void truncate(mb_size_t sz); // removes sz last bytes
032785bf 94
032785bf 95 void terminate(); // zero-terminates the buffer w/o increasing contentSize
96
9bcd2d11 97 bool wasStolen() const { return stolen; }
98
63be0a78 99 /** init with specific sizes */
2fe7eff9 100 void init(mb_size_t szInit, mb_size_t szMax);
101
63be0a78 102 /** init with defaults */
2fe7eff9 103 void init();
104
63be0a78 105 /** cleans mb; last function to call if you do not give .buf away */
2fe7eff9 106 void clean();
107
63be0a78 108 /** resets mb preserving (or initializing if needed) memory buffer */
2fe7eff9 109 void reset();
110
63be0a78 111 /** unfirtunate hack to test if the buffer has been Init()ialized */
2fe7eff9 112 int isNull();
113
63be0a78 114 /**
115 * calls snprintf, extends buffer if needed
116 \note we use Printf instead of printf so the compiler won't
117 * think we're calling the libc printf()
118 */
2fe7eff9 119 void Printf(const char *fmt,...) PRINTF_FORMAT_ARG2;
2fe7eff9 120
63be0a78 121 /** vPrintf for other printf()'s to use */
2fe7eff9 122 void vPrintf(const char *fmt, va_list ap);
123
63be0a78 124 /**
125 * freezes the object! and returns function to clear it up.
126 *
127 \retval free() function to be used.
128 */
2fe7eff9 129 FREE *freeFunc();
130
032785bf 131private:
63be0a78 132 /**
032785bf 133 * private copy constructor and assignment operator generates
134 * compiler errors if someone tries to copy/assign a MemBuf
135 */
136 MemBuf(const MemBuf& m) {assert(false);};
137
138 MemBuf& operator= (const MemBuf& m) {assert(false); return *this;};
139
2fe7eff9 140 void grow(mb_size_t min_cap);
141
032785bf 142public:
63be0a78 143 /**
144 \deprecated use space*() and content*() methods to access safely instead.
145 * public, read-only.
26ac0430 146 *
63be0a78 147 \todo XXX: hide these members completely and remove 0-termination
148 * so that consume() does not need to memmove all the time
149 */
032785bf 150 char *buf; // available content
151 mb_size_t size; // used space, does not count 0-terminator
528b2c61 152
63be0a78 153 /**
154 * when grows: assert(new_capacity <= max_capacity)
155 \deprecated Use interface function instead
156 \todo XXX: make these private after converting memBuf*() functions to methods
157 */
158 mb_size_t max_capacity;
159
160 /**
161 * allocated space
162 \deprecated Use interface function instead
163 \todo XXX: make these private after converting memBuf*() functions to methods
164 */
26ac0430 165 mb_size_t capacity;
62e76326 166
3d0ac046 167 unsigned stolen:1; /* the buffer has been stolen for use by someone else */
9bcd2d11 168
169#if 0
170
3d0ac046 171 unsigned valid:1; /* to be used for debugging only! */
9bcd2d11 172#endif
c7bf588b
FC
173
174private:
175 CBDATA_CLASS2(MemBuf);
528b2c61 176};
177
32d002cb 178#if _USE_INLINE_
528b2c61 179#include "MemBuf.cci"
180#endif
181
63be0a78 182/** returns free() function to be used, _freezes_ the object! */
d9c252f2 183void memBufReport(MemBuf * mb);
63be0a78 184/** pack content into a mem buf. */
d9c252f2 185void packerToMemInit(Packer * p, MemBuf * mb);
528b2c61 186
528b2c61 187#endif /* SQUID_MEM_H */