]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/strstream.cc
re PR libstdc++/47921 (pbump will overflow when input n is larger than 2G-1)
[thirdparty/gcc.git] / libstdc++-v3 / src / strstream.cc
CommitLineData
42526146
PE
1// strstream definitions -*- C++ -*-
2
1139a735
PC
3// Copyright (C) 2001, 2002, 2003, 2005, 2009, 2010, 2011
4// Free Software Foundation
42526146 5//
38cca750
BK
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
748086b7 9// Free Software Foundation; either version 3, or (at your option)
42526146 10// any later version.
38cca750
BK
11
12// This library is distributed in the hope that it will be useful,
42526146
PE
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
38cca750 16
748086b7
JJ
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
42526146 20
748086b7
JJ
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
42526146 25
b2dad0e3
BK
26/*
27 * Copyright (c) 1998
28 * Silicon Graphics Computer Systems, Inc.
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 */
38
39// Implementation of the classes in header <strstream>.
40// WARNING: The classes defined in <strstream> are DEPRECATED. This
41// header is defined in section D.7.1 of the C++ standard, and it
42// MAY BE REMOVED in a future standard revision. You should use the
43// header <sstream> instead.
44
9a56333e 45#include <strstream>
5feb272b 46#include <algorithm>
b2dad0e3
BK
47#include <new>
48#include <stdlib.h>
49#include <string.h>
50#include <limits.h>
51
12ffa228
BK
52namespace std _GLIBCXX_VISIBILITY(default)
53{
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 55
9a56333e
BK
56 strstreambuf::strstreambuf(streamsize initial_capacity)
57 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
58 _M_frozen(false), _M_constant(false)
59 {
4977bab6 60 streamsize n = std::max(initial_capacity, streamsize(16));
49bc23b7
BK
61
62 char* buf = _M_alloc(n);
63 if (buf)
9a56333e 64 {
49bc23b7
BK
65 setp(buf, buf + n);
66 setg(buf, buf, buf);
9a56333e
BK
67 }
68 }
69
70 strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
71 : _Base(), _M_alloc_fun(alloc_f), _M_free_fun(free_f), _M_dynamic(true),
72 _M_frozen(false), _M_constant(false)
73 {
49bc23b7
BK
74 streamsize n = 16;
75
76 char* buf = _M_alloc(n);
77 if (buf)
9a56333e 78 {
49bc23b7
BK
79 setp(buf, buf + n);
80 setg(buf, buf, buf);
9a56333e
BK
81 }
82 }
83
50a681c4 84 strstreambuf::strstreambuf(char* get, streamsize n, char* put) throw ()
9a56333e
BK
85 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
86 _M_frozen(false), _M_constant(false)
87 { _M_setup(get, put, n); }
88
50a681c4 89 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put) throw ()
9a56333e 90 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
497e42fd 91 _M_frozen(false), _M_constant(false)
9a56333e
BK
92 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
93
94 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
50a681c4 95 unsigned char* put) throw ()
9a56333e
BK
96 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
97 _M_frozen(false), _M_constant(false)
98 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
99
50a681c4 100 strstreambuf::strstreambuf(const char* get, streamsize n) throw ()
9a56333e
BK
101 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
102 _M_frozen(false), _M_constant(true)
103 { _M_setup(const_cast<char*>(get), 0, n); }
104
50a681c4 105 strstreambuf::strstreambuf(const signed char* get, streamsize n) throw ()
9a56333e
BK
106 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
107 _M_frozen(false), _M_constant(true)
108 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get)), 0, n); }
109
50a681c4 110 strstreambuf::strstreambuf(const unsigned char* get, streamsize n) throw ()
9a56333e
BK
111 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
112 _M_frozen(false), _M_constant(true)
113 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n); }
114
115 strstreambuf::~strstreambuf()
116 {
117 if (_M_dynamic && !_M_frozen)
49bc23b7 118 _M_free(eback());
9a56333e
BK
119 }
120
121 void
50a681c4 122 strstreambuf::freeze(bool frozenflag) throw ()
9a56333e
BK
123 {
124 if (_M_dynamic)
125 _M_frozen = frozenflag;
126 }
127
128 char*
50a681c4 129 strstreambuf::str() throw ()
9a56333e
BK
130 {
131 freeze(true);
132 return eback();
133 }
134
135 int
50a681c4 136 strstreambuf::pcount() const throw ()
9a56333e
BK
137 { return pptr() ? pptr() - pbase() : 0; }
138
139 strstreambuf::int_type
140 strstreambuf::overflow(int_type c)
141 {
142 if (c == traits_type::eof())
143 return traits_type::not_eof(c);
497e42fd 144
9a56333e
BK
145 // Try to expand the buffer.
146 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
147 {
148 ptrdiff_t old_size = epptr() - pbase();
dd6eaaed 149 ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
497e42fd 150
9a56333e
BK
151 char* buf = _M_alloc(new_size);
152 if (buf)
153 {
154 memcpy(buf, pbase(), old_size);
155 char* old_buffer = pbase();
156 bool reposition_get = false;
157 ptrdiff_t old_get_offset;
158 if (gptr() != 0)
159 {
160 reposition_get = true;
161 old_get_offset = gptr() - eback();
162 }
497e42fd 163
9a56333e 164 setp(buf, buf + new_size);
1139a735 165 __safe_pbump(old_size);
9a56333e
BK
166
167 if (reposition_get)
168 setg(buf, buf + old_get_offset, buf +
4977bab6 169 std::max(old_get_offset, old_size));
9a56333e
BK
170
171 _M_free(old_buffer);
172 }
173 }
497e42fd 174
9a56333e
BK
175 if (pptr() != epptr())
176 {
177 *pptr() = c;
178 pbump(1);
179 return c;
180 }
181 else
182 return traits_type::eof();
183 }
184
185 strstreambuf::int_type
186 strstreambuf::pbackfail(int_type c)
187 {
188 if (gptr() != eback())
189 {
497e42fd
BK
190 if (c == _Traits::eof())
191 {
192 gbump(-1);
193 return _Traits::not_eof(c);
194 }
a62e7311 195 else if (c == _Traits::to_int_type(gptr()[-1]))
497e42fd
BK
196 { // KLUDGE
197 gbump(-1);
198 return c;
199 }
200 else if (!_M_constant)
201 {
202 gbump(-1);
203 *gptr() = c;
204 return c;
205 }
b2dad0e3 206 }
eafa1afa 207 return _Traits::eof();
9a56333e 208 }
eafa1afa 209
9a56333e
BK
210 strstreambuf::int_type
211 strstreambuf::underflow()
212 {
213 if (gptr() == egptr() && pptr() && pptr() > egptr())
214 setg(eback(), gptr(), pptr());
eafa1afa 215
9a56333e
BK
216 if (gptr() != egptr())
217 return (unsigned char) *gptr();
218 else
219 return _Traits::eof();
220 }
221
222 basic_streambuf<char, char_traits<char> >*
223 strstreambuf::setbuf(char*, streamsize)
224 { return this; }
225
226 strstreambuf::pos_type
227 strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
228 ios_base::openmode mode)
229 {
230 bool do_get = false;
231 bool do_put = false;
232
233 if ((mode & (ios_base::in | ios_base::out))
234 == (ios_base::in | ios_base::out) &&
235 (dir == ios_base::beg || dir == ios_base::end))
46bbeb65 236 do_get = do_put = true;
9a56333e
BK
237 else if (mode & ios_base::in)
238 do_get = true;
239 else if (mode & ios_base::out)
240 do_put = true;
241
242 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
243 // area is undefined if there is no get area.
244 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
245 return pos_type(off_type(-1));
eafa1afa 246
9a56333e
BK
247 char* seeklow = eback();
248 char* seekhigh = epptr() ? epptr() : egptr();
249
250 off_type newoff;
251 switch (dir)
252 {
253 case ios_base::beg:
254 newoff = 0;
255 break;
256 case ios_base::end:
257 newoff = seekhigh - seeklow;
258 break;
259 case ios_base::cur:
260 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
261 break;
262 default:
263 return pos_type(off_type(-1));
264 }
265
266 off += newoff;
267 if (off < 0 || off > seekhigh - seeklow)
268 return pos_type(off_type(-1));
eafa1afa 269
9a56333e
BK
270 if (do_put)
271 {
272 if (seeklow + off < pbase())
273 {
274 setp(seeklow, epptr());
1139a735 275 __safe_pbump(off);
9a56333e
BK
276 }
277 else
278 {
279 setp(pbase(), epptr());
1139a735 280 __safe_pbump(off - (pbase() - seeklow));
9a56333e
BK
281 }
282 }
283 if (do_get)
284 {
285 if (off <= egptr() - seeklow)
286 setg(seeklow, seeklow + off, egptr());
287 else if (off <= pptr() - seeklow)
288 setg(seeklow, seeklow + off, pptr());
289 else
290 setg(seeklow, seeklow + off, epptr());
291 }
292 return pos_type(newoff);
293 }
294
295 strstreambuf::pos_type
296 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
297 { return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); }
298
299 char*
300 strstreambuf::_M_alloc(size_t n)
301 {
302 if (_M_alloc_fun)
303 return static_cast<char*>(_M_alloc_fun(n));
eafa1afa 304 else
9a56333e
BK
305 return new char[n];
306 }
307
308 void
309 strstreambuf::_M_free(char* p)
310 {
311 if (p)
2a67bec2
ILT
312 {
313 if (_M_free_fun)
314 _M_free_fun(p);
315 else
316 delete[] p;
317 }
9a56333e
BK
318 }
319
320 void
50a681c4 321 strstreambuf::_M_setup(char* get, char* put, streamsize n) throw ()
9a56333e
BK
322 {
323 if (get)
324 {
325 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
497e42fd 326
9a56333e
BK
327 if (put)
328 {
329 setg(get, get, put);
330 setp(put, put + N);
331 }
332 else
333 setg(get, get, get + N);
334 }
335 }
336
337 istrstream::istrstream(char* s)
338 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
339 { basic_ios<char>::init(&_M_buf); }
340
341 istrstream::istrstream(const char* s)
342 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
343 { basic_ios<char>::init(&_M_buf); }
344
345 istrstream::istrstream(char* s, streamsize n)
346 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
347 { basic_ios<char>::init(&_M_buf); }
348
349 istrstream::istrstream(const char* s, streamsize n)
350 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
351 { basic_ios<char>::init(&_M_buf); }
352
353 istrstream::~istrstream() { }
354
355 strstreambuf*
50a681c4 356 istrstream::rdbuf() const throw ()
9a56333e
BK
357 { return const_cast<strstreambuf*>(&_M_buf); }
358
359 char*
50a681c4 360 istrstream::str() throw ()
9a56333e
BK
361 { return _M_buf.str(); }
362
363 ostrstream::ostrstream()
364 : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
365 { basic_ios<char>::init(&_M_buf); }
366
367 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
368 : basic_ios<char>(), basic_ostream<char>(0),
369 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
370 { basic_ios<char>::init(&_M_buf); }
371
372 ostrstream::~ostrstream() {}
373
374 strstreambuf*
50a681c4 375 ostrstream::rdbuf() const throw ()
9a56333e
BK
376 { return const_cast<strstreambuf*>(&_M_buf); }
377
378 void
50a681c4 379 ostrstream::freeze(bool freezeflag) throw ()
9a56333e
BK
380 { _M_buf.freeze(freezeflag); }
381
382 char*
50a681c4 383 ostrstream::str() throw ()
9a56333e
BK
384 { return _M_buf.str(); }
385
386 int
50a681c4 387 ostrstream::pcount() const throw ()
9a56333e
BK
388 { return _M_buf.pcount(); }
389
390 strstream::strstream()
391 : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
392 { basic_ios<char>::init(&_M_buf); }
393
394 strstream::strstream(char* s, int n, ios_base::openmode mode)
395 : basic_ios<char>(), basic_iostream<char>(0),
396 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
397 { basic_ios<char>::init(&_M_buf); }
398
399 strstream::~strstream() { }
400
401 strstreambuf*
50a681c4 402 strstream::rdbuf() const throw ()
9a56333e
BK
403 { return const_cast<strstreambuf*>(&_M_buf); }
404
405 void
50a681c4 406 strstream::freeze(bool freezeflag) throw ()
9a56333e
BK
407 { _M_buf.freeze(freezeflag); }
408
409 int
50a681c4 410 strstream::pcount() const throw ()
9a56333e
BK
411 { return _M_buf.pcount(); }
412
413 char*
50a681c4 414 strstream::str() throw ()
9a56333e 415 { return _M_buf.str(); }
3cbc7af0 416
12ffa228
BK
417_GLIBCXX_END_NAMESPACE_VERSION
418} // namespace