diff options
author | Wink Saville <wink@google.com> | 2010-05-29 13:00:38 -0700 |
---|---|---|
committer | Wink Saville <wink@google.com> | 2010-05-29 13:00:38 -0700 |
commit | d0332953cda33fb4f8e24ebff9c49159b69c43d6 (patch) | |
tree | 81612e8b12f590310aeb0ebf1da37b304eb7baa6 /src/google/protobuf/stubs | |
parent | ede38fe9b9f93888e6e41afc7abb09525f44da95 (diff) | |
download | external_protobuf-d0332953cda33fb4f8e24ebff9c49159b69c43d6.zip external_protobuf-d0332953cda33fb4f8e24ebff9c49159b69c43d6.tar.gz external_protobuf-d0332953cda33fb4f8e24ebff9c49159b69c43d6.tar.bz2 |
Add protobuf 2.3.0 sources
This is the contents of protobuf-2.3.0.tar.bz2 from
http://code.google.com/p/protobuf/downloads/list.
Change-Id: Idfde09ce7ef5ac027b07ee83f2674fbbed5c30b2
Diffstat (limited to 'src/google/protobuf/stubs')
-rw-r--r-- | src/google/protobuf/stubs/common.h | 43 | ||||
-rw-r--r-- | src/google/protobuf/stubs/hash.h | 17 | ||||
-rw-r--r-- | src/google/protobuf/stubs/once.cc | 16 | ||||
-rw-r--r-- | src/google/protobuf/stubs/once.h | 26 | ||||
-rw-r--r-- | src/google/protobuf/stubs/once_unittest.cc | 10 | ||||
-rw-r--r-- | src/google/protobuf/stubs/structurally_valid.cc | 81 | ||||
-rw-r--r-- | src/google/protobuf/stubs/structurally_valid_unittest.cc | 14 | ||||
-rw-r--r-- | src/google/protobuf/stubs/strutil.cc | 37 | ||||
-rw-r--r-- | src/google/protobuf/stubs/strutil.h | 9 |
9 files changed, 175 insertions, 78 deletions
diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index 964fb6a..551ee4a 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -83,24 +83,24 @@ namespace internal { // The current version, represented as a single integer to make comparison // easier: major * 10^6 + minor * 10^3 + micro -#define GOOGLE_PROTOBUF_VERSION 2002000 +#define GOOGLE_PROTOBUF_VERSION 2003000 // The minimum library version which works with the current version of the // headers. -#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2002000 +#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2003000 // The minimum header version which works with the current version of // the library. This constant should only be used by protoc's C++ code // generator. -static const int kMinHeaderVersionForLibrary = 2002000; +static const int kMinHeaderVersionForLibrary = 2003000; // The minimum protoc version which works with the current version of the // headers. -#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2002000 +#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2003000 // The minimum header version which works with the current version of // protoc. This constant should only be used in VerifyVersion(). -static const int kMinHeaderVersionForProtoc = 2002000; +static const int kMinHeaderVersionForProtoc = 2003000; // Verifies that the headers and libraries are compatible. Use the macro // below to call this. @@ -171,7 +171,13 @@ static const int64 kint64min = -kint64max - 1; static const uint32 kuint32max = 0xFFFFFFFFu; static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); -#undef GOOGLE_ATTRIBUTE_ALWAYS_INLINE +// ------------------------------------------------------------------- +// Annotations: Some parts of the code have been annotated in ways that might +// be useful to some compilers or tools, but are not supported universally. +// You can #define these annotations yourself if the default implementation +// is not right for you. + +#ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) // For functions we want to force inline. // Introduced in gcc 3.1. @@ -180,14 +186,35 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); // Other compilers will have to figure it out for themselves. #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE #endif +#endif -#undef GOOGLE_ATTRIBUTE_DEPRECATED +#ifndef GOOGLE_ATTRIBUTE_DEPRECATED #ifdef __GNUC__ // If the method/variable/type is used anywhere, produce a warning. #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) #else #define GOOGLE_ATTRIBUTE_DEPRECATED #endif +#endif + +#ifndef GOOGLE_PREDICT_TRUE +#ifdef __GNUC__ +// Provided at least since GCC 3.0. +#define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) +#else +#define GOOGLE_PREDICT_TRUE +#endif +#endif + +// Delimits a block of code which may write to memory which is simultaneously +// written by other threads, but which has been determined to be thread-safe +// (e.g. because it is an idempotent write). +#ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN +#define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN() +#endif +#ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END +#define GOOGLE_SAFE_CONCURRENT_WRITES_END() +#endif // =================================================================== // from google3/base/basictypes.h @@ -283,7 +310,9 @@ inline To down_cast(From* f) { // so we only accept pointers implicit_cast<From*, To>(0); } +#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI) assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only! +#endif return static_cast<To>(f); } diff --git a/src/google/protobuf/stubs/hash.h b/src/google/protobuf/stubs/hash.h index c341413..822d605 100644 --- a/src/google/protobuf/stubs/hash.h +++ b/src/google/protobuf/stubs/hash.h @@ -98,7 +98,7 @@ template <typename Key, class hash_set : public std::set<Key, HashFcn> { }; -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION) template <typename Key> struct hash : public HASH_NAMESPACE::hash_compare<Key> { @@ -145,21 +145,30 @@ struct hash<const Key*> { } }; +// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So, +// we go ahead and provide our own implementation. template <> -struct hash<const char*> : public HASH_NAMESPACE::hash<const char*> { +struct hash<const char*> { + inline size_t operator()(const char* str) const { + size_t result = 0; + for (; *str != '\0'; str++) { + result = 5 * result + *str; + } + return result; + } }; template <typename Key, typename Data, typename HashFcn = hash<Key>, typename EqualKey = std::equal_to<Key> > -class hash_map : public HASH_NAMESPACE::hash_map< +class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS< Key, Data, HashFcn, EqualKey> { }; template <typename Key, typename HashFcn = hash<Key>, typename EqualKey = std::equal_to<Key> > -class hash_set : public HASH_NAMESPACE::hash_set< +class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS< Key, HashFcn, EqualKey> { }; diff --git a/src/google/protobuf/stubs/once.cc b/src/google/protobuf/stubs/once.cc index 3c53bcd..5b7af9c 100644 --- a/src/google/protobuf/stubs/once.cc +++ b/src/google/protobuf/stubs/once.cc @@ -46,33 +46,33 @@ namespace protobuf { #ifdef _WIN32 -struct GoogleOnceInternal { - GoogleOnceInternal() { +struct ProtobufOnceInternal { + ProtobufOnceInternal() { InitializeCriticalSection(&critical_section); } - ~GoogleOnceInternal() { + ~ProtobufOnceInternal() { DeleteCriticalSection(&critical_section); } CRITICAL_SECTION critical_section; }; -GoogleOnceType::~GoogleOnceType() +ProtobufOnceType::~ProtobufOnceType() { delete internal_; internal_ = NULL; } -GoogleOnceType::GoogleOnceType() { +ProtobufOnceType::ProtobufOnceType() { // internal_ may be non-NULL if Init() was already called. - if (internal_ == NULL) internal_ = new GoogleOnceInternal; + if (internal_ == NULL) internal_ = new ProtobufOnceInternal; } -void GoogleOnceType::Init(void (*init_func)()) { +void ProtobufOnceType::Init(void (*init_func)()) { // internal_ may be NULL if we're still in dynamic initialization and the // constructor has not been called yet. As mentioned in once.h, we assume // that the program is still single-threaded at this time, and therefore it // should be safe to initialize internal_ like so. - if (internal_ == NULL) internal_ = new GoogleOnceInternal; + if (internal_ == NULL) internal_ = new ProtobufOnceInternal; EnterCriticalSection(&internal_->critical_section); if (!initialized_) { diff --git a/src/google/protobuf/stubs/once.h b/src/google/protobuf/stubs/once.h index 22b0d4e..0dee407 100644 --- a/src/google/protobuf/stubs/once.h +++ b/src/google/protobuf/stubs/once.h @@ -38,13 +38,13 @@ // This is basically a portable version of pthread_once(). // // This header declares three things: -// * A type called GoogleOnceType. +// * A type called ProtobufOnceType. // * A macro GOOGLE_PROTOBUF_DECLARE_ONCE() which declares a variable of type -// GoogleOnceType. This is the only legal way to declare such a variable. +// ProtobufOnceType. This is the only legal way to declare such a variable. // The macro may only be used at the global scope (you cannot create local // or class member variables of this type). -// * A function GogoleOnceInit(GoogleOnceType* once, void (*init_func)()). -// This function, when invoked multiple times given the same GoogleOnceType +// * A function GogoleOnceInit(ProtobufOnceType* once, void (*init_func)()). +// This function, when invoked multiple times given the same ProtobufOnceType // object, will invoke init_func on the first call only, and will make sure // none of the calls return before that first call to init_func has finished. // @@ -83,21 +83,21 @@ namespace protobuf { #ifdef _WIN32 -struct GoogleOnceInternal; +struct ProtobufOnceInternal; -struct LIBPROTOBUF_EXPORT GoogleOnceType { - GoogleOnceType(); - ~GoogleOnceType(); +struct LIBPROTOBUF_EXPORT ProtobufOnceType { + ProtobufOnceType(); + ~ProtobufOnceType(); void Init(void (*init_func)()); volatile bool initialized_; - GoogleOnceInternal* internal_; + ProtobufOnceInternal* internal_; }; #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \ - ::google::protobuf::GoogleOnceType NAME + ::google::protobuf::ProtobufOnceType NAME -inline void GoogleOnceInit(GoogleOnceType* once, void (*init_func)()) { +inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) { // Note: Double-checked locking is safe on x86. if (!once->initialized_) { once->Init(init_func); @@ -106,12 +106,12 @@ inline void GoogleOnceInit(GoogleOnceType* once, void (*init_func)()) { #else -typedef pthread_once_t GoogleOnceType; +typedef pthread_once_t ProtobufOnceType; #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \ pthread_once_t NAME = PTHREAD_ONCE_INIT -inline void GoogleOnceInit(GoogleOnceType* once, void (*init_func)()) { +inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) { pthread_once(once, init_func); } diff --git a/src/google/protobuf/stubs/once_unittest.cc b/src/google/protobuf/stubs/once_unittest.cc index 86e0647..b8f86a0 100644 --- a/src/google/protobuf/stubs/once_unittest.cc +++ b/src/google/protobuf/stubs/once_unittest.cc @@ -52,10 +52,10 @@ class OnceInitTest : public testing::Test { current_test_ = this; } - // Since GoogleOnceType is only allowed to be allocated in static storage, - // each test must use a different pair of GoogleOnceType objects which it + // Since ProtobufOnceType is only allowed to be allocated in static storage, + // each test must use a different pair of ProtobufOnceType objects which it // must declare itself. - void SetOnces(GoogleOnceType* once, GoogleOnceType* recursive_once) { + void SetOnces(ProtobufOnceType* once, ProtobufOnceType* recursive_once) { once_ = once; recursive_once_ = recursive_once; } @@ -155,8 +155,8 @@ class OnceInitTest : public testing::Test { Mutex mutex_; Mutex init_blocker_; State state_; - GoogleOnceType* once_; - GoogleOnceType* recursive_once_; + ProtobufOnceType* once_; + ProtobufOnceType* recursive_once_; void Init() { MutexLock lock(&mutex_); diff --git a/src/google/protobuf/stubs/structurally_valid.cc b/src/google/protobuf/stubs/structurally_valid.cc index e385a81..0f6afe6 100644 --- a/src/google/protobuf/stubs/structurally_valid.cc +++ b/src/google/protobuf/stubs/structurally_valid.cc @@ -371,36 +371,44 @@ int UTF8GenericScan(const UTF8ScanObj* st, // Do state-table scan int e = 0; uint8 c; - - // Do fast for groups of 8 identity bytes. - // This covers a lot of 7-bit ASCII ~8x faster then the 1-byte loop, - // including slowing slightly on cr/lf/ht - //---------------------------- const uint8* Tbl2 = &st->fast_state[0]; - uint32 losub = st->losub; - uint32 hiadd = st->hiadd; - while (src < srclimit8) { - uint32 s0123 = (reinterpret_cast<const uint32 *>(src))[0]; - uint32 s4567 = (reinterpret_cast<const uint32 *>(src))[1]; - src += 8; - // This is a fast range check for all bytes in [lowsub..0x80-hiadd) - uint32 temp = (s0123 - losub) | (s0123 + hiadd) | - (s4567 - losub) | (s4567 + hiadd); - if ((temp & 0x80808080) != 0) { - // We typically end up here on cr/lf/ht; src was incremented - int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) | - (Tbl2[src[-6]] | Tbl2[src[-5]]); - if (e0123 != 0) { - src -= 8; - break; - } // Exit on Non-interchange - e0123 = (Tbl2[src[-4]] | Tbl2[src[-3]]) | - (Tbl2[src[-2]] | Tbl2[src[-1]]); - if (e0123 != 0) { - src -= 4; - break; - } // Exit on Non-interchange - // Else OK, go around again + const uint32 losub = st->losub; + const uint32 hiadd = st->hiadd; + // Check initial few bytes one at a time until 8-byte aligned + //---------------------------- + while ((((uintptr_t)src & 0x07) != 0) && + (src < srclimit) && + Tbl2[src[0]] == 0) { + src++; + } + if (((uintptr_t)src & 0x07) == 0) { + // Do fast for groups of 8 identity bytes. + // This covers a lot of 7-bit ASCII ~8x faster then the 1-byte loop, + // including slowing slightly on cr/lf/ht + //---------------------------- + while (src < srclimit8) { + uint32 s0123 = (reinterpret_cast<const uint32 *>(src))[0]; + uint32 s4567 = (reinterpret_cast<const uint32 *>(src))[1]; + src += 8; + // This is a fast range check for all bytes in [lowsub..0x80-hiadd) + uint32 temp = (s0123 - losub) | (s0123 + hiadd) | + (s4567 - losub) | (s4567 + hiadd); + if ((temp & 0x80808080) != 0) { + // We typically end up here on cr/lf/ht; src was incremented + int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) | + (Tbl2[src[-6]] | Tbl2[src[-5]]); + if (e0123 != 0) { + src -= 8; + break; + } // Exit on Non-interchange + e0123 = (Tbl2[src[-4]] | Tbl2[src[-3]]) | + (Tbl2[src[-2]] | Tbl2[src[-1]]); + if (e0123 != 0) { + src -= 4; + break; + } // Exit on Non-interchange + // Else OK, go around again + } } } //---------------------------- @@ -470,10 +478,17 @@ int UTF8GenericScanFastAscii(const UTF8ScanObj* st, int rest_consumed; int exit_reason; do { - while ((src < srclimit8) && - (((reinterpret_cast<const uint32*>(src)[0] | - reinterpret_cast<const uint32*>(src)[1]) & 0x80808080) == 0)) { - src += 8; + // Check initial few bytes one at a time until 8-byte aligned + while ((((uintptr_t)src & 0x07) != 0) && + (src < srclimit) && (src[0] < 0x80)) { + src++; + } + if (((uintptr_t)src & 0x07) == 0) { + while ((src < srclimit8) && + (((reinterpret_cast<const uint32*>(src)[0] | + reinterpret_cast<const uint32*>(src)[1]) & 0x80808080) == 0)) { + src += 8; + } } while ((src < srclimit) && (src[0] < 0x80)) { src++; diff --git a/src/google/protobuf/stubs/structurally_valid_unittest.cc b/src/google/protobuf/stubs/structurally_valid_unittest.cc index 2282551..9088888 100644 --- a/src/google/protobuf/stubs/structurally_valid_unittest.cc +++ b/src/google/protobuf/stubs/structurally_valid_unittest.cc @@ -13,15 +13,25 @@ TEST(StructurallyValidTest, ValidUTF8String) { // On GCC, this string can be written as: // "abcd 1234 - \u2014\u2013\u2212" // MSVC seems to interpret \u differently. - string valid_str("abcd 1234 - \342\200\224\342\200\223\342\210\222"); + string valid_str("abcd 1234 - \342\200\224\342\200\223\342\210\222 - xyz789"); EXPECT_TRUE(IsStructurallyValidUTF8(valid_str.data(), valid_str.size())); + // Additional check for pointer alignment + for (int i = 1; i < 8; ++i) { + EXPECT_TRUE(IsStructurallyValidUTF8(valid_str.data() + i, + valid_str.size() - i)); + } } TEST(StructurallyValidTest, InvalidUTF8String) { - string invalid_str("\xA0\xB0"); + const string invalid_str("abcd\xA0\xB0\xA0\xB0\xA0\xB0 - xyz789"); EXPECT_FALSE(IsStructurallyValidUTF8(invalid_str.data(), invalid_str.size())); + // Additional check for pointer alignment + for (int i = 1; i < 8; ++i) { + EXPECT_FALSE(IsStructurallyValidUTF8(invalid_str.data() + i, + invalid_str.size() - i)); + } } } // namespace diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc index a476475..bb658ba 100644 --- a/src/google/protobuf/stubs/strutil.cc +++ b/src/google/protobuf/stubs/strutil.cc @@ -36,6 +36,7 @@ #include <limits> #include <limits.h> #include <stdio.h> +#include <iterator> #ifdef _WIN32 // MSVC has only _snprintf, not snprintf. @@ -424,8 +425,8 @@ string UnescapeCEscapeString(const string& src) { // // Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped. // ---------------------------------------------------------------------- -static int CEscapeInternal(const char* src, int src_len, char* dest, - int dest_len, bool use_hex) { +int CEscapeInternal(const char* src, int src_len, char* dest, + int dest_len, bool use_hex, bool utf8_safe) { const char* src_end = src + src_len; int used = 0; bool last_hex_escape = false; // true if last output char was \xNN @@ -446,7 +447,9 @@ static int CEscapeInternal(const char* src, int src_len, char* dest, // Note that if we emit \xNN and the src character after that is a hex // digit then that digit must be escaped too to prevent it being // interpreted as part of the character code by C. - if (!isprint(*src) || (last_hex_escape && isxdigit(*src))) { + if ((!utf8_safe || static_cast<uint8>(*src) < 0x80) && + (!isprint(*src) || + (last_hex_escape && isxdigit(*src)))) { if (dest_len - used < 4) // need space for 4 letter escape return -1; sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"), @@ -468,7 +471,7 @@ static int CEscapeInternal(const char* src, int src_len, char* dest, } int CEscapeString(const char* src, int src_len, char* dest, int dest_len) { - return CEscapeInternal(src, src_len, dest, dest_len, false); + return CEscapeInternal(src, src_len, dest, dest_len, false, false); } // ---------------------------------------------------------------------- @@ -485,11 +488,33 @@ string CEscape(const string& src) { const int dest_length = src.size() * 4 + 1; // Maximum possible expansion scoped_array<char> dest(new char[dest_length]); const int len = CEscapeInternal(src.data(), src.size(), - dest.get(), dest_length, false); + dest.get(), dest_length, false, false); GOOGLE_DCHECK_GE(len, 0); return string(dest.get(), len); } +namespace strings { + +string Utf8SafeCEscape(const string& src) { + const int dest_length = src.size() * 4 + 1; // Maximum possible expansion + scoped_array<char> dest(new char[dest_length]); + const int len = CEscapeInternal(src.data(), src.size(), + dest.get(), dest_length, false, true); + GOOGLE_DCHECK_GE(len, 0); + return string(dest.get(), len); +} + +string CHexEscape(const string& src) { + const int dest_length = src.size() * 4 + 1; // Maximum possible expansion + scoped_array<char> dest(new char[dest_length]); + const int len = CEscapeInternal(src.data(), src.size(), + dest.get(), dest_length, true, false); + GOOGLE_DCHECK_GE(len, 0); + return string(dest.get(), len); +} + +} // namespace strings + // ---------------------------------------------------------------------- // strto32_adaptor() // strtou32_adaptor() @@ -1027,7 +1052,7 @@ char* DoubleToBuffer(double value, char* buffer) { bool safe_strtof(const char* str, float* value) { char* endptr; errno = 0; // errno only gets set on errors -#ifdef _WIN32 // has no strtof() +#if defined(_WIN32) || defined (__hpux) // has no strtof() *value = strtod(str, &endptr); #else *value = strtof(str, &endptr); diff --git a/src/google/protobuf/stubs/strutil.h b/src/google/protobuf/stubs/strutil.h index 7f6bd96..777694b 100644 --- a/src/google/protobuf/stubs/strutil.h +++ b/src/google/protobuf/stubs/strutil.h @@ -33,6 +33,7 @@ #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ +#include <stdlib.h> #include <vector> #include <google/protobuf/stubs/common.h> @@ -264,6 +265,14 @@ LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len, // ---------------------------------------------------------------------- LIBPROTOBUF_EXPORT string CEscape(const string& src); +namespace strings { +// Like CEscape() but does not escape bytes with the upper bit set. +LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src); + +// Like CEscape() but uses hex (\x) escapes instead of octals. +LIBPROTOBUF_EXPORT string CHexEscape(const string& src); +} // namespace strings + // ---------------------------------------------------------------------- // strto32() // strtou32() |