build/stlport/src/strstream.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright (c) 1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18
michael@0 19 // Implementation of the classes in header <strstream>.
michael@0 20 // WARNING: The classes defined in <strstream> are DEPRECATED. This
michael@0 21 // header is defined in section D.7.1 of the C++ standard, and it
michael@0 22 // MAY BE REMOVED in a future standard revision. You should use the
michael@0 23 // header <sstream> instead.
michael@0 24
michael@0 25 #include "stlport_prefix.h"
michael@0 26
michael@0 27 #include <strstream>
michael@0 28 #include <algorithm>
michael@0 29 #include <limits>
michael@0 30
michael@0 31 _STLP_BEGIN_NAMESPACE
michael@0 32
michael@0 33 // strstreambuf constructor, destructor.
michael@0 34 strstreambuf::strstreambuf(streamsize initial_capacity)
michael@0 35 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 36 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
michael@0 37 size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
michael@0 38 (max)(initial_capacity, streamsize(16))))
michael@0 39 : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
michael@0 40
michael@0 41 char* buf = _M_alloc(n);
michael@0 42 if (buf) {
michael@0 43 setp(buf, buf + n);
michael@0 44 setg(buf, buf, buf);
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
michael@0 49 : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
michael@0 50 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
michael@0 51 size_t n = 16;
michael@0 52
michael@0 53 char* buf = _M_alloc(n);
michael@0 54 if (buf) {
michael@0 55 setp(buf, buf + n);
michael@0 56 setg(buf, buf, buf);
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
michael@0 61 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 62 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
michael@0 63 _M_setup(get, put, n);
michael@0 64 }
michael@0 65
michael@0 66 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
michael@0 67 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 68 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
michael@0 69 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
michael@0 70 }
michael@0 71
michael@0 72 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
michael@0 73 unsigned char* put)
michael@0 74 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 75 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
michael@0 76 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
michael@0 77 }
michael@0 78
michael@0 79 strstreambuf::strstreambuf(const char* get, streamsize n)
michael@0 80 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 81 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
michael@0 82 _M_setup(__CONST_CAST(char*,get), 0, n);
michael@0 83 }
michael@0 84
michael@0 85 strstreambuf::strstreambuf(const signed char* get, streamsize n)
michael@0 86 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 87 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
michael@0 88 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
michael@0 89 }
michael@0 90
michael@0 91 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
michael@0 92 : _M_alloc_fun(0), _M_free_fun(0),
michael@0 93 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
michael@0 94 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
michael@0 95 }
michael@0 96
michael@0 97 strstreambuf::~strstreambuf() {
michael@0 98 if (_M_dynamic && !_M_frozen)
michael@0 99 _M_free(eback());
michael@0 100 }
michael@0 101
michael@0 102 void strstreambuf::freeze(bool frozenflag) {
michael@0 103 if (_M_dynamic)
michael@0 104 _M_frozen = frozenflag;
michael@0 105 }
michael@0 106
michael@0 107 char* strstreambuf::str() {
michael@0 108 freeze(true);
michael@0 109 return eback();
michael@0 110 }
michael@0 111
michael@0 112 int strstreambuf::pcount() const {
michael@0 113 return int(pptr() ? pptr() - pbase() : 0);
michael@0 114 }
michael@0 115
michael@0 116 strstreambuf::int_type strstreambuf::overflow(int_type c) {
michael@0 117 if (c == traits_type::eof())
michael@0 118 return traits_type::not_eof(c);
michael@0 119
michael@0 120 // Try to expand the buffer.
michael@0 121 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
michael@0 122 ptrdiff_t old_size = epptr() - pbase();
michael@0 123 ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
michael@0 124
michael@0 125 char* buf = _M_alloc(new_size);
michael@0 126 if (buf) {
michael@0 127 memcpy(buf, pbase(), old_size);
michael@0 128
michael@0 129 char* old_buffer = pbase();
michael@0 130 bool reposition_get = false;
michael@0 131 ptrdiff_t old_get_offset;
michael@0 132 if (gptr() != 0) {
michael@0 133 reposition_get = true;
michael@0 134 old_get_offset = gptr() - eback();
michael@0 135 }
michael@0 136
michael@0 137 setp(buf, buf + new_size);
michael@0 138 pbump((int)old_size);
michael@0 139
michael@0 140 if (reposition_get)
michael@0 141 setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
michael@0 142
michael@0 143 _M_free(old_buffer);
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 if (pptr() != epptr()) {
michael@0 148 *pptr() = traits_type::to_char_type(c);
michael@0 149 pbump(1);
michael@0 150 return c;
michael@0 151 }
michael@0 152 else
michael@0 153 return traits_type::eof();
michael@0 154 }
michael@0 155
michael@0 156 strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
michael@0 157 if (gptr() != eback()) {
michael@0 158 if (c == traits_type::eof()) {
michael@0 159 gbump(-1);
michael@0 160 return traits_type::not_eof(c);
michael@0 161 }
michael@0 162 else if (c == gptr()[-1]) {
michael@0 163 gbump(-1);
michael@0 164 return c;
michael@0 165 }
michael@0 166 else if (!_M_constant) {
michael@0 167 gbump(-1);
michael@0 168 *gptr() = traits_type::to_char_type(c);
michael@0 169 return c;
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 return traits_type::eof();
michael@0 174 }
michael@0 175
michael@0 176 strstreambuf::int_type strstreambuf::underflow() {
michael@0 177 if (gptr() == egptr() && pptr() && pptr() > egptr())
michael@0 178 setg(eback(), gptr(), pptr());
michael@0 179
michael@0 180 if (gptr() != egptr())
michael@0 181 return (unsigned char) *gptr();
michael@0 182 else
michael@0 183 return _Traits::eof();
michael@0 184 }
michael@0 185
michael@0 186 basic_streambuf<char, char_traits<char> >*
michael@0 187 strstreambuf::setbuf(char*, streamsize) {
michael@0 188 return this;
michael@0 189 }
michael@0 190
michael@0 191 strstreambuf::pos_type
michael@0 192 strstreambuf::seekoff(off_type off,
michael@0 193 ios_base::seekdir dir, ios_base::openmode mode) {
michael@0 194 bool do_get = false;
michael@0 195 bool do_put = false;
michael@0 196
michael@0 197 if ((mode & (ios_base::in | ios_base::out)) ==
michael@0 198 (ios_base::in | ios_base::out) &&
michael@0 199 (dir == ios_base::beg || dir == ios_base::end))
michael@0 200 do_get = do_put = true;
michael@0 201 else if (mode & ios_base::in)
michael@0 202 do_get = true;
michael@0 203 else if (mode & ios_base::out)
michael@0 204 do_put = true;
michael@0 205
michael@0 206 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
michael@0 207 // area is undefined if there is no get area.
michael@0 208 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
michael@0 209 return pos_type(off_type(-1));
michael@0 210
michael@0 211 char* seeklow = eback();
michael@0 212 char* seekhigh = epptr() ? epptr() : egptr();
michael@0 213
michael@0 214 off_type newoff;
michael@0 215 switch(dir) {
michael@0 216 case ios_base::beg:
michael@0 217 newoff = 0;
michael@0 218 break;
michael@0 219 case ios_base::end:
michael@0 220 newoff = seekhigh - seeklow;
michael@0 221 break;
michael@0 222 case ios_base::cur:
michael@0 223 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
michael@0 224 break;
michael@0 225 default:
michael@0 226 return pos_type(off_type(-1));
michael@0 227 }
michael@0 228
michael@0 229 off += newoff;
michael@0 230 if (off < 0 || off > seekhigh - seeklow)
michael@0 231 return pos_type(off_type(-1));
michael@0 232
michael@0 233 if (do_put) {
michael@0 234 if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
michael@0 235 setp(seeklow, epptr());
michael@0 236 pbump((int)off);
michael@0 237 }
michael@0 238 else {
michael@0 239 setp(pbase(), epptr());
michael@0 240 pbump((int)(off - (pbase() - seeklow)));
michael@0 241 }
michael@0 242 }
michael@0 243 if (do_get) {
michael@0 244 if (off <= egptr() - seeklow)
michael@0 245 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
michael@0 246 else if (off <= pptr() - seeklow)
michael@0 247 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
michael@0 248 else
michael@0 249 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
michael@0 250 }
michael@0 251
michael@0 252 return pos_type(newoff);
michael@0 253 }
michael@0 254
michael@0 255 strstreambuf::pos_type
michael@0 256 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
michael@0 257 return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
michael@0 258 }
michael@0 259
michael@0 260
michael@0 261 char* strstreambuf::_M_alloc(size_t n) {
michael@0 262 if (_M_alloc_fun)
michael@0 263 return __STATIC_CAST(char*,_M_alloc_fun(n));
michael@0 264 else
michael@0 265 return new char[n];
michael@0 266 }
michael@0 267
michael@0 268 void strstreambuf::_M_free(char* p) {
michael@0 269 if (p) {
michael@0 270 if (_M_free_fun)
michael@0 271 _M_free_fun(p);
michael@0 272 else
michael@0 273 delete[] p;
michael@0 274 }
michael@0 275 }
michael@0 276
michael@0 277 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
michael@0 278 if (get) {
michael@0 279 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
michael@0 280
michael@0 281 if (put) {
michael@0 282 setg(get, get, get + N);
michael@0 283 setp(put, put + N);
michael@0 284 }
michael@0 285 else {
michael@0 286 setg(get, get, get + N);
michael@0 287 }
michael@0 288 }
michael@0 289 }
michael@0 290
michael@0 291 //----------------------------------------------------------------------
michael@0 292 // Class istrstream
michael@0 293
michael@0 294 istrstream::istrstream(char* s)
michael@0 295 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
michael@0 296 this->init(&_M_buf);
michael@0 297 }
michael@0 298
michael@0 299 istrstream::istrstream(const char* s)
michael@0 300 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
michael@0 301 this->init(&_M_buf);
michael@0 302 }
michael@0 303
michael@0 304 istrstream::istrstream(char* s, streamsize n)
michael@0 305 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
michael@0 306 this->init(&_M_buf);
michael@0 307 }
michael@0 308
michael@0 309 istrstream::istrstream(const char* s, streamsize n)
michael@0 310 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
michael@0 311 this->init(&_M_buf);
michael@0 312 }
michael@0 313
michael@0 314 istrstream::~istrstream() {}
michael@0 315
michael@0 316 strstreambuf* istrstream::rdbuf() const {
michael@0 317 return __CONST_CAST(strstreambuf*,&_M_buf);
michael@0 318 }
michael@0 319
michael@0 320 char* istrstream::str() { return _M_buf.str(); }
michael@0 321
michael@0 322 //----------------------------------------------------------------------
michael@0 323 // Class ostrstream
michael@0 324
michael@0 325 ostrstream::ostrstream()
michael@0 326 : basic_ostream<char, char_traits<char> >(0), _M_buf() {
michael@0 327 basic_ios<char, char_traits<char> >::init(&_M_buf);
michael@0 328 }
michael@0 329
michael@0 330 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
michael@0 331 : basic_ostream<char, char_traits<char> >(0),
michael@0 332 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
michael@0 333 basic_ios<char, char_traits<char> >::init(&_M_buf);
michael@0 334 }
michael@0 335
michael@0 336 ostrstream::~ostrstream() {}
michael@0 337
michael@0 338 strstreambuf* ostrstream::rdbuf() const {
michael@0 339 return __CONST_CAST(strstreambuf*,&_M_buf);
michael@0 340 }
michael@0 341
michael@0 342 void ostrstream::freeze(bool freezeflag) {
michael@0 343 _M_buf.freeze(freezeflag);
michael@0 344 }
michael@0 345
michael@0 346 char* ostrstream::str() {
michael@0 347 return _M_buf.str();
michael@0 348 }
michael@0 349
michael@0 350 int ostrstream::pcount() const {
michael@0 351 return _M_buf.pcount();
michael@0 352 }
michael@0 353
michael@0 354
michael@0 355 //----------------------------------------------------------------------
michael@0 356 // Class strstream
michael@0 357
michael@0 358 strstream::strstream()
michael@0 359 : basic_iostream<char, char_traits<char> >(0), _M_buf() {
michael@0 360 basic_ios<char, char_traits<char> >::init(&_M_buf);
michael@0 361 }
michael@0 362
michael@0 363 strstream::strstream(char* s, int n, ios_base::openmode mode)
michael@0 364 : basic_iostream<char, char_traits<char> >(0),
michael@0 365 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
michael@0 366 basic_ios<char, char_traits<char> >::init(&_M_buf);
michael@0 367 }
michael@0 368
michael@0 369 strstream::~strstream() {}
michael@0 370
michael@0 371 strstreambuf* strstream::rdbuf() const {
michael@0 372 return __CONST_CAST(strstreambuf*,&_M_buf);
michael@0 373 }
michael@0 374
michael@0 375 void strstream::freeze(bool freezeflag) {
michael@0 376 _M_buf.freeze(freezeflag);
michael@0 377 }
michael@0 378
michael@0 379 int strstream::pcount() const {
michael@0 380 return _M_buf.pcount();
michael@0 381 }
michael@0 382
michael@0 383 char* strstream::str() {
michael@0 384 return _M_buf.str();
michael@0 385 }
michael@0 386
michael@0 387 _STLP_END_NAMESPACE
michael@0 388
michael@0 389 // Local Variables:
michael@0 390 // mode:C++
michael@0 391 // End:

mercurial