snappy-stubs-internal.h 18 KB

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