other-licenses/snappy/src/snappy-stubs-internal.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Copyright 2011 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28 //
michael@0 29 // Various stubs for the open-source version of Snappy.
michael@0 30
michael@0 31 #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
michael@0 32 #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
michael@0 33
michael@0 34 #ifdef HAVE_CONFIG_H
michael@0 35 #include "config.h"
michael@0 36 #endif
michael@0 37
michael@0 38 #include <iostream>
michael@0 39 #include <string>
michael@0 40
michael@0 41 #include <assert.h>
michael@0 42 #include <stdlib.h>
michael@0 43 #include <string.h>
michael@0 44
michael@0 45 #ifdef HAVE_SYS_MMAN_H
michael@0 46 #include <sys/mman.h>
michael@0 47 #endif
michael@0 48
michael@0 49 #include "snappy-stubs-public.h"
michael@0 50
michael@0 51 #if defined(__x86_64__)
michael@0 52
michael@0 53 // Enable 64-bit optimized versions of some routines.
michael@0 54 #define ARCH_K8 1
michael@0 55
michael@0 56 #endif
michael@0 57
michael@0 58 // Needed by OS X, among others.
michael@0 59 #ifndef MAP_ANONYMOUS
michael@0 60 #define MAP_ANONYMOUS MAP_ANON
michael@0 61 #endif
michael@0 62
michael@0 63 // Pull in std::min, std::ostream, and the likes. This is safe because this
michael@0 64 // header file is never used from any public header files.
michael@0 65 using namespace std;
michael@0 66
michael@0 67 // The size of an array, if known at compile-time.
michael@0 68 // Will give unexpected results if used on a pointer.
michael@0 69 // We undefine it first, since some compilers already have a definition.
michael@0 70 #ifdef ARRAYSIZE
michael@0 71 #undef ARRAYSIZE
michael@0 72 #endif
michael@0 73 #define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
michael@0 74
michael@0 75 // Static prediction hints.
michael@0 76 #ifdef HAVE_BUILTIN_EXPECT
michael@0 77 #define PREDICT_FALSE(x) (__builtin_expect(x, 0))
michael@0 78 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
michael@0 79 #else
michael@0 80 #define PREDICT_FALSE(x) x
michael@0 81 #define PREDICT_TRUE(x) x
michael@0 82 #endif
michael@0 83
michael@0 84 // This is only used for recomputing the tag byte table used during
michael@0 85 // decompression; for simplicity we just remove it from the open-source
michael@0 86 // version (anyone who wants to regenerate it can just do the call
michael@0 87 // themselves within main()).
michael@0 88 #define DEFINE_bool(flag_name, default_value, description) \
michael@0 89 bool FLAGS_ ## flag_name = default_value
michael@0 90 #define DECLARE_bool(flag_name) \
michael@0 91 extern bool FLAGS_ ## flag_name
michael@0 92
michael@0 93 namespace snappy {
michael@0 94
michael@0 95 static const uint32 kuint32max = static_cast<uint32>(0xFFFFFFFF);
michael@0 96 static const int64 kint64max = static_cast<int64>(0x7FFFFFFFFFFFFFFFLL);
michael@0 97
michael@0 98 // Logging.
michael@0 99
michael@0 100 #define LOG(level) LogMessage()
michael@0 101 #define VLOG(level) true ? (void)0 : \
michael@0 102 snappy::LogMessageVoidify() & snappy::LogMessage()
michael@0 103
michael@0 104 class LogMessage {
michael@0 105 public:
michael@0 106 LogMessage() { }
michael@0 107 ~LogMessage() {
michael@0 108 cerr << endl;
michael@0 109 }
michael@0 110
michael@0 111 LogMessage& operator<<(const std::string& msg) {
michael@0 112 cerr << msg;
michael@0 113 return *this;
michael@0 114 }
michael@0 115 LogMessage& operator<<(int x) {
michael@0 116 cerr << x;
michael@0 117 return *this;
michael@0 118 }
michael@0 119 };
michael@0 120
michael@0 121 // Asserts, both versions activated in debug mode only,
michael@0 122 // and ones that are always active.
michael@0 123
michael@0 124 #define CRASH_UNLESS(condition) \
michael@0 125 PREDICT_TRUE(condition) ? (void)0 : \
michael@0 126 snappy::LogMessageVoidify() & snappy::LogMessageCrash()
michael@0 127
michael@0 128 class LogMessageCrash : public LogMessage {
michael@0 129 public:
michael@0 130 LogMessageCrash() { }
michael@0 131 ~LogMessageCrash() {
michael@0 132 cerr << endl;
michael@0 133 abort();
michael@0 134 }
michael@0 135 };
michael@0 136
michael@0 137 // This class is used to explicitly ignore values in the conditional
michael@0 138 // logging macros. This avoids compiler warnings like "value computed
michael@0 139 // is not used" and "statement has no effect".
michael@0 140
michael@0 141 class LogMessageVoidify {
michael@0 142 public:
michael@0 143 LogMessageVoidify() { }
michael@0 144 // This has to be an operator with a precedence lower than << but
michael@0 145 // higher than ?:
michael@0 146 void operator&(const LogMessage&) { }
michael@0 147 };
michael@0 148
michael@0 149 #define CHECK(cond) CRASH_UNLESS(cond)
michael@0 150 #define CHECK_LE(a, b) CRASH_UNLESS((a) <= (b))
michael@0 151 #define CHECK_GE(a, b) CRASH_UNLESS((a) >= (b))
michael@0 152 #define CHECK_EQ(a, b) CRASH_UNLESS((a) == (b))
michael@0 153 #define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
michael@0 154 #define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
michael@0 155 #define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
michael@0 156
michael@0 157 #ifdef NDEBUG
michael@0 158
michael@0 159 #define DCHECK(cond) CRASH_UNLESS(true)
michael@0 160 #define DCHECK_LE(a, b) CRASH_UNLESS(true)
michael@0 161 #define DCHECK_GE(a, b) CRASH_UNLESS(true)
michael@0 162 #define DCHECK_EQ(a, b) CRASH_UNLESS(true)
michael@0 163 #define DCHECK_NE(a, b) CRASH_UNLESS(true)
michael@0 164 #define DCHECK_LT(a, b) CRASH_UNLESS(true)
michael@0 165 #define DCHECK_GT(a, b) CRASH_UNLESS(true)
michael@0 166
michael@0 167 #else
michael@0 168
michael@0 169 #define DCHECK(cond) CHECK(cond)
michael@0 170 #define DCHECK_LE(a, b) CHECK_LE(a, b)
michael@0 171 #define DCHECK_GE(a, b) CHECK_GE(a, b)
michael@0 172 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
michael@0 173 #define DCHECK_NE(a, b) CHECK_NE(a, b)
michael@0 174 #define DCHECK_LT(a, b) CHECK_LT(a, b)
michael@0 175 #define DCHECK_GT(a, b) CHECK_GT(a, b)
michael@0 176
michael@0 177 #endif
michael@0 178
michael@0 179 // Potentially unaligned loads and stores.
michael@0 180
michael@0 181 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
michael@0 182
michael@0 183 #define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
michael@0 184 #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
michael@0 185 #define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
michael@0 186
michael@0 187 #define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
michael@0 188 #define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
michael@0 189 #define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
michael@0 190
michael@0 191 #else
michael@0 192
michael@0 193 // These functions are provided for architectures that don't support
michael@0 194 // unaligned loads and stores.
michael@0 195
michael@0 196 inline uint16 UNALIGNED_LOAD16(const void *p) {
michael@0 197 uint16 t;
michael@0 198 memcpy(&t, p, sizeof t);
michael@0 199 return t;
michael@0 200 }
michael@0 201
michael@0 202 inline uint32 UNALIGNED_LOAD32(const void *p) {
michael@0 203 uint32 t;
michael@0 204 memcpy(&t, p, sizeof t);
michael@0 205 return t;
michael@0 206 }
michael@0 207
michael@0 208 inline uint64 UNALIGNED_LOAD64(const void *p) {
michael@0 209 uint64 t;
michael@0 210 memcpy(&t, p, sizeof t);
michael@0 211 return t;
michael@0 212 }
michael@0 213
michael@0 214 inline void UNALIGNED_STORE16(void *p, uint16 v) {
michael@0 215 memcpy(p, &v, sizeof v);
michael@0 216 }
michael@0 217
michael@0 218 inline void UNALIGNED_STORE32(void *p, uint32 v) {
michael@0 219 memcpy(p, &v, sizeof v);
michael@0 220 }
michael@0 221
michael@0 222 inline void UNALIGNED_STORE64(void *p, uint64 v) {
michael@0 223 memcpy(p, &v, sizeof v);
michael@0 224 }
michael@0 225
michael@0 226 #endif
michael@0 227
michael@0 228 // The following guarantees declaration of the byte swap functions.
michael@0 229 #ifdef WORDS_BIGENDIAN
michael@0 230
michael@0 231 #ifdef HAVE_SYS_BYTEORDER_H
michael@0 232 #include <sys/byteorder.h>
michael@0 233 #endif
michael@0 234
michael@0 235 #ifdef HAVE_SYS_ENDIAN_H
michael@0 236 #include <sys/endian.h>
michael@0 237 #endif
michael@0 238
michael@0 239 #ifdef _MSC_VER
michael@0 240 #include <stdlib.h>
michael@0 241 #define bswap_16(x) _byteswap_ushort(x)
michael@0 242 #define bswap_32(x) _byteswap_ulong(x)
michael@0 243 #define bswap_64(x) _byteswap_uint64(x)
michael@0 244
michael@0 245 #elif defined(__APPLE__)
michael@0 246 // Mac OS X / Darwin features
michael@0 247 #include <libkern/OSByteOrder.h>
michael@0 248 #define bswap_16(x) OSSwapInt16(x)
michael@0 249 #define bswap_32(x) OSSwapInt32(x)
michael@0 250 #define bswap_64(x) OSSwapInt64(x)
michael@0 251
michael@0 252 #elif defined(HAVE_BYTESWAP_H)
michael@0 253 #include <byteswap.h>
michael@0 254
michael@0 255 #elif defined(bswap32)
michael@0 256 // FreeBSD defines bswap{16,32,64} in <sys/endian.h> (already #included).
michael@0 257 #define bswap_16(x) bswap16(x)
michael@0 258 #define bswap_32(x) bswap32(x)
michael@0 259 #define bswap_64(x) bswap64(x)
michael@0 260
michael@0 261 #elif defined(BSWAP_64)
michael@0 262 // Solaris 10 defines BSWAP_{16,32,64} in <sys/byteorder.h> (already #included).
michael@0 263 #define bswap_16(x) BSWAP_16(x)
michael@0 264 #define bswap_32(x) BSWAP_32(x)
michael@0 265 #define bswap_64(x) BSWAP_64(x)
michael@0 266
michael@0 267 #else
michael@0 268
michael@0 269 inline uint16 bswap_16(uint16 x) {
michael@0 270 return (x << 8) | (x >> 8);
michael@0 271 }
michael@0 272
michael@0 273 inline uint32 bswap_32(uint32 x) {
michael@0 274 x = ((x & 0xff00ff00UL) >> 8) | ((x & 0x00ff00ffUL) << 8);
michael@0 275 return (x >> 16) | (x << 16);
michael@0 276 }
michael@0 277
michael@0 278 inline uint64 bswap_64(uint64 x) {
michael@0 279 x = ((x & 0xff00ff00ff00ff00ULL) >> 8) | ((x & 0x00ff00ff00ff00ffULL) << 8);
michael@0 280 x = ((x & 0xffff0000ffff0000ULL) >> 16) | ((x & 0x0000ffff0000ffffULL) << 16);
michael@0 281 return (x >> 32) | (x << 32);
michael@0 282 }
michael@0 283
michael@0 284 #endif
michael@0 285
michael@0 286 #endif // WORDS_BIGENDIAN
michael@0 287
michael@0 288 // Convert to little-endian storage, opposite of network format.
michael@0 289 // Convert x from host to little endian: x = LittleEndian.FromHost(x);
michael@0 290 // convert x from little endian to host: x = LittleEndian.ToHost(x);
michael@0 291 //
michael@0 292 // Store values into unaligned memory converting to little endian order:
michael@0 293 // LittleEndian.Store16(p, x);
michael@0 294 //
michael@0 295 // Load unaligned values stored in little endian converting to host order:
michael@0 296 // x = LittleEndian.Load16(p);
michael@0 297 class LittleEndian {
michael@0 298 public:
michael@0 299 // Conversion functions.
michael@0 300 #ifdef WORDS_BIGENDIAN
michael@0 301
michael@0 302 static uint16 FromHost16(uint16 x) { return bswap_16(x); }
michael@0 303 static uint16 ToHost16(uint16 x) { return bswap_16(x); }
michael@0 304
michael@0 305 static uint32 FromHost32(uint32 x) { return bswap_32(x); }
michael@0 306 static uint32 ToHost32(uint32 x) { return bswap_32(x); }
michael@0 307
michael@0 308 static bool IsLittleEndian() { return false; }
michael@0 309
michael@0 310 #else // !defined(WORDS_BIGENDIAN)
michael@0 311
michael@0 312 static uint16 FromHost16(uint16 x) { return x; }
michael@0 313 static uint16 ToHost16(uint16 x) { return x; }
michael@0 314
michael@0 315 static uint32 FromHost32(uint32 x) { return x; }
michael@0 316 static uint32 ToHost32(uint32 x) { return x; }
michael@0 317
michael@0 318 static bool IsLittleEndian() { return true; }
michael@0 319
michael@0 320 #endif // !defined(WORDS_BIGENDIAN)
michael@0 321
michael@0 322 // Functions to do unaligned loads and stores in little-endian order.
michael@0 323 static uint16 Load16(const void *p) {
michael@0 324 return ToHost16(UNALIGNED_LOAD16(p));
michael@0 325 }
michael@0 326
michael@0 327 static void Store16(void *p, uint16 v) {
michael@0 328 UNALIGNED_STORE16(p, FromHost16(v));
michael@0 329 }
michael@0 330
michael@0 331 static uint32 Load32(const void *p) {
michael@0 332 return ToHost32(UNALIGNED_LOAD32(p));
michael@0 333 }
michael@0 334
michael@0 335 static void Store32(void *p, uint32 v) {
michael@0 336 UNALIGNED_STORE32(p, FromHost32(v));
michael@0 337 }
michael@0 338 };
michael@0 339
michael@0 340 // Some bit-manipulation functions.
michael@0 341 class Bits {
michael@0 342 public:
michael@0 343 // Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
michael@0 344 static int Log2Floor(uint32 n);
michael@0 345
michael@0 346 // Return the first set least / most significant bit, 0-indexed. Returns an
michael@0 347 // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
michael@0 348 // that it's 0-indexed.
michael@0 349 static int FindLSBSetNonZero(uint32 n);
michael@0 350 static int FindLSBSetNonZero64(uint64 n);
michael@0 351
michael@0 352 private:
michael@0 353 DISALLOW_COPY_AND_ASSIGN(Bits);
michael@0 354 };
michael@0 355
michael@0 356 #ifdef HAVE_BUILTIN_CTZ
michael@0 357
michael@0 358 inline int Bits::Log2Floor(uint32 n) {
michael@0 359 return n == 0 ? -1 : 31 ^ __builtin_clz(n);
michael@0 360 }
michael@0 361
michael@0 362 inline int Bits::FindLSBSetNonZero(uint32 n) {
michael@0 363 return __builtin_ctz(n);
michael@0 364 }
michael@0 365
michael@0 366 inline int Bits::FindLSBSetNonZero64(uint64 n) {
michael@0 367 return __builtin_ctzll(n);
michael@0 368 }
michael@0 369
michael@0 370 #else // Portable versions.
michael@0 371
michael@0 372 inline int Bits::Log2Floor(uint32 n) {
michael@0 373 if (n == 0)
michael@0 374 return -1;
michael@0 375 int log = 0;
michael@0 376 uint32 value = n;
michael@0 377 for (int i = 4; i >= 0; --i) {
michael@0 378 int shift = (1 << i);
michael@0 379 uint32 x = value >> shift;
michael@0 380 if (x != 0) {
michael@0 381 value = x;
michael@0 382 log += shift;
michael@0 383 }
michael@0 384 }
michael@0 385 assert(value == 1);
michael@0 386 return log;
michael@0 387 }
michael@0 388
michael@0 389 inline int Bits::FindLSBSetNonZero(uint32 n) {
michael@0 390 int rc = 31;
michael@0 391 for (int i = 4, shift = 1 << 4; i >= 0; --i) {
michael@0 392 const uint32 x = n << shift;
michael@0 393 if (x != 0) {
michael@0 394 n = x;
michael@0 395 rc -= shift;
michael@0 396 }
michael@0 397 shift >>= 1;
michael@0 398 }
michael@0 399 return rc;
michael@0 400 }
michael@0 401
michael@0 402 // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
michael@0 403 inline int Bits::FindLSBSetNonZero64(uint64 n) {
michael@0 404 const uint32 bottombits = static_cast<uint32>(n);
michael@0 405 if (bottombits == 0) {
michael@0 406 // Bottom bits are zero, so scan in top bits
michael@0 407 return 32 + FindLSBSetNonZero(static_cast<uint32>(n >> 32));
michael@0 408 } else {
michael@0 409 return FindLSBSetNonZero(bottombits);
michael@0 410 }
michael@0 411 }
michael@0 412
michael@0 413 #endif // End portable versions.
michael@0 414
michael@0 415 // Variable-length integer encoding.
michael@0 416 class Varint {
michael@0 417 public:
michael@0 418 // Maximum lengths of varint encoding of uint32.
michael@0 419 static const int kMax32 = 5;
michael@0 420
michael@0 421 // Attempts to parse a varint32 from a prefix of the bytes in [ptr,limit-1].
michael@0 422 // Never reads a character at or beyond limit. If a valid/terminated varint32
michael@0 423 // was found in the range, stores it in *OUTPUT and returns a pointer just
michael@0 424 // past the last byte of the varint32. Else returns NULL. On success,
michael@0 425 // "result <= limit".
michael@0 426 static const char* Parse32WithLimit(const char* ptr, const char* limit,
michael@0 427 uint32* OUTPUT);
michael@0 428
michael@0 429 // REQUIRES "ptr" points to a buffer of length sufficient to hold "v".
michael@0 430 // EFFECTS Encodes "v" into "ptr" and returns a pointer to the
michael@0 431 // byte just past the last encoded byte.
michael@0 432 static char* Encode32(char* ptr, uint32 v);
michael@0 433
michael@0 434 // EFFECTS Appends the varint representation of "value" to "*s".
michael@0 435 static void Append32(string* s, uint32 value);
michael@0 436 };
michael@0 437
michael@0 438 inline const char* Varint::Parse32WithLimit(const char* p,
michael@0 439 const char* l,
michael@0 440 uint32* OUTPUT) {
michael@0 441 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(p);
michael@0 442 const unsigned char* limit = reinterpret_cast<const unsigned char*>(l);
michael@0 443 uint32 b, result;
michael@0 444 if (ptr >= limit) return NULL;
michael@0 445 b = *(ptr++); result = b & 127; if (b < 128) goto done;
michael@0 446 if (ptr >= limit) return NULL;
michael@0 447 b = *(ptr++); result |= (b & 127) << 7; if (b < 128) goto done;
michael@0 448 if (ptr >= limit) return NULL;
michael@0 449 b = *(ptr++); result |= (b & 127) << 14; if (b < 128) goto done;
michael@0 450 if (ptr >= limit) return NULL;
michael@0 451 b = *(ptr++); result |= (b & 127) << 21; if (b < 128) goto done;
michael@0 452 if (ptr >= limit) return NULL;
michael@0 453 b = *(ptr++); result |= (b & 127) << 28; if (b < 16) goto done;
michael@0 454 return NULL; // Value is too long to be a varint32
michael@0 455 done:
michael@0 456 *OUTPUT = result;
michael@0 457 return reinterpret_cast<const char*>(ptr);
michael@0 458 }
michael@0 459
michael@0 460 inline char* Varint::Encode32(char* sptr, uint32 v) {
michael@0 461 // Operate on characters as unsigneds
michael@0 462 unsigned char* ptr = reinterpret_cast<unsigned char*>(sptr);
michael@0 463 static const int B = 128;
michael@0 464 if (v < (1<<7)) {
michael@0 465 *(ptr++) = v;
michael@0 466 } else if (v < (1<<14)) {
michael@0 467 *(ptr++) = v | B;
michael@0 468 *(ptr++) = v>>7;
michael@0 469 } else if (v < (1<<21)) {
michael@0 470 *(ptr++) = v | B;
michael@0 471 *(ptr++) = (v>>7) | B;
michael@0 472 *(ptr++) = v>>14;
michael@0 473 } else if (v < (1<<28)) {
michael@0 474 *(ptr++) = v | B;
michael@0 475 *(ptr++) = (v>>7) | B;
michael@0 476 *(ptr++) = (v>>14) | B;
michael@0 477 *(ptr++) = v>>21;
michael@0 478 } else {
michael@0 479 *(ptr++) = v | B;
michael@0 480 *(ptr++) = (v>>7) | B;
michael@0 481 *(ptr++) = (v>>14) | B;
michael@0 482 *(ptr++) = (v>>21) | B;
michael@0 483 *(ptr++) = v>>28;
michael@0 484 }
michael@0 485 return reinterpret_cast<char*>(ptr);
michael@0 486 }
michael@0 487
michael@0 488 // If you know the internal layout of the std::string in use, you can
michael@0 489 // replace this function with one that resizes the string without
michael@0 490 // filling the new space with zeros (if applicable) --
michael@0 491 // it will be non-portable but faster.
michael@0 492 inline void STLStringResizeUninitialized(string* s, size_t new_size) {
michael@0 493 s->resize(new_size);
michael@0 494 }
michael@0 495
michael@0 496 // Return a mutable char* pointing to a string's internal buffer,
michael@0 497 // which may not be null-terminated. Writing through this pointer will
michael@0 498 // modify the string.
michael@0 499 //
michael@0 500 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
michael@0 501 // next call to a string method that invalidates iterators.
michael@0 502 //
michael@0 503 // As of 2006-04, there is no standard-blessed way of getting a
michael@0 504 // mutable reference to a string's internal buffer. However, issue 530
michael@0 505 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
michael@0 506 // proposes this as the method. It will officially be part of the standard
michael@0 507 // for C++0x. This should already work on all current implementations.
michael@0 508 inline char* string_as_array(string* str) {
michael@0 509 return str->empty() ? NULL : &*str->begin();
michael@0 510 }
michael@0 511
michael@0 512 } // namespace snappy
michael@0 513
michael@0 514 #endif // UTIL_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_

mercurial