Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 33 | #include <google/protobuf/message_lite.h> // TODO(gerbens) ideally remove this. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 34 | #include <google/protobuf/stubs/common.h> |
| 35 | #include <google/protobuf/stubs/once.h> |
| 36 | #include <google/protobuf/stubs/status.h> |
| 37 | #include <google/protobuf/stubs/stringpiece.h> |
| 38 | #include <google/protobuf/stubs/strutil.h> |
| 39 | #include <google/protobuf/stubs/int128.h> |
| 40 | #include <errno.h> |
| 41 | #include <sstream> |
| 42 | #include <stdio.h> |
| 43 | #include <vector> |
| 44 | |
| 45 | #ifdef _WIN32 |
| 46 | #define WIN32_LEAN_AND_MEAN // We only need minimal includes |
| 47 | #include <windows.h> |
| 48 | #define snprintf _snprintf // see comment in strutil.cc |
| 49 | #elif defined(HAVE_PTHREAD) |
| 50 | #include <pthread.h> |
| 51 | #else |
| 52 | #error "No suitable threading library available." |
| 53 | #endif |
| 54 | #if defined(__ANDROID__) |
| 55 | #include <android/log.h> |
| 56 | #endif |
| 57 | |
| 58 | namespace google { |
| 59 | namespace protobuf { |
| 60 | |
| 61 | namespace internal { |
| 62 | |
| 63 | void VerifyVersion(int headerVersion, |
| 64 | int minLibraryVersion, |
| 65 | const char* filename) { |
| 66 | if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) { |
| 67 | // Library is too old for headers. |
| 68 | GOOGLE_LOG(FATAL) |
| 69 | << "This program requires version " << VersionString(minLibraryVersion) |
| 70 | << " of the Protocol Buffer runtime library, but the installed version " |
| 71 | "is " << VersionString(GOOGLE_PROTOBUF_VERSION) << ". Please update " |
| 72 | "your library. If you compiled the program yourself, make sure that " |
| 73 | "your headers are from the same version of Protocol Buffers as your " |
| 74 | "link-time library. (Version verification failed in \"" |
| 75 | << filename << "\".)"; |
| 76 | } |
| 77 | if (headerVersion < kMinHeaderVersionForLibrary) { |
| 78 | // Headers are too old for library. |
| 79 | GOOGLE_LOG(FATAL) |
| 80 | << "This program was compiled against version " |
| 81 | << VersionString(headerVersion) << " of the Protocol Buffer runtime " |
| 82 | "library, which is not compatible with the installed version (" |
| 83 | << VersionString(GOOGLE_PROTOBUF_VERSION) << "). Contact the program " |
| 84 | "author for an update. If you compiled the program yourself, make " |
| 85 | "sure that your headers are from the same version of Protocol Buffers " |
| 86 | "as your link-time library. (Version verification failed in \"" |
| 87 | << filename << "\".)"; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | string VersionString(int version) { |
| 92 | int major = version / 1000000; |
| 93 | int minor = (version / 1000) % 1000; |
| 94 | int micro = version % 1000; |
| 95 | |
| 96 | // 128 bytes should always be enough, but we use snprintf() anyway to be |
| 97 | // safe. |
| 98 | char buffer[128]; |
| 99 | snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro); |
| 100 | |
| 101 | // Guard against broken MSVC snprintf(). |
| 102 | buffer[sizeof(buffer)-1] = '\0'; |
| 103 | |
| 104 | return buffer; |
| 105 | } |
| 106 | |
| 107 | } // namespace internal |
| 108 | |
| 109 | // =================================================================== |
| 110 | // emulates google3/base/logging.cc |
| 111 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 112 | // If the minimum logging level is not set, we default to logging messages for |
| 113 | // all levels. |
| 114 | #ifndef GOOGLE_PROTOBUF_MIN_LOG_LEVEL |
| 115 | #define GOOGLE_PROTOBUF_MIN_LOG_LEVEL LOGLEVEL_INFO |
| 116 | #endif |
| 117 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 118 | namespace internal { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 119 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 120 | #if defined(__ANDROID__) |
| 121 | inline void DefaultLogHandler(LogLevel level, const char* filename, int line, |
| 122 | const string& message) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 123 | if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { |
| 124 | return; |
| 125 | } |
| 126 | static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"}; |
| 127 | |
| 128 | static const int android_log_levels[] = { |
| 129 | ANDROID_LOG_INFO, // LOG(INFO), |
| 130 | ANDROID_LOG_WARN, // LOG(WARNING) |
| 131 | ANDROID_LOG_ERROR, // LOG(ERROR) |
| 132 | ANDROID_LOG_FATAL, // LOG(FATAL) |
| 133 | }; |
| 134 | |
| 135 | // Bound the logging level. |
| 136 | const int android_log_level = android_log_levels[level]; |
| 137 | ::std::ostringstream ostr; |
| 138 | ostr << "[libprotobuf " << level_names[level] << " " << filename << ":" |
| 139 | << line << "] " << message.c_str(); |
| 140 | |
| 141 | // Output the log string the Android log at the appropriate level. |
| 142 | __android_log_write(android_log_level, "libprotobuf-native", |
| 143 | ostr.str().c_str()); |
| 144 | // Also output to std::cerr. |
| 145 | fprintf(stderr, "%s", ostr.str().c_str()); |
| 146 | fflush(stderr); |
| 147 | |
| 148 | // Indicate termination if needed. |
| 149 | if (android_log_level == ANDROID_LOG_FATAL) { |
| 150 | __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native", |
| 151 | "terminating.\n"); |
| 152 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 153 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 154 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 155 | #else |
| 156 | void DefaultLogHandler(LogLevel level, const char* filename, int line, |
| 157 | const string& message) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 158 | if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { |
| 159 | return; |
| 160 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 161 | static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" }; |
| 162 | |
| 163 | // We use fprintf() instead of cerr because we want this to work at static |
| 164 | // initialization time. |
| 165 | fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", |
| 166 | level_names[level], filename, line, message.c_str()); |
| 167 | fflush(stderr); // Needed on MSVC. |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | void NullLogHandler(LogLevel /* level */, const char* /* filename */, |
| 172 | int /* line */, const string& /* message */) { |
| 173 | // Nothing. |
| 174 | } |
| 175 | |
| 176 | static LogHandler* log_handler_ = &DefaultLogHandler; |
| 177 | static int log_silencer_count_ = 0; |
| 178 | |
| 179 | static Mutex* log_silencer_count_mutex_ = NULL; |
| 180 | GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_); |
| 181 | |
| 182 | void DeleteLogSilencerCount() { |
| 183 | delete log_silencer_count_mutex_; |
| 184 | log_silencer_count_mutex_ = NULL; |
| 185 | } |
| 186 | void InitLogSilencerCount() { |
| 187 | log_silencer_count_mutex_ = new Mutex; |
| 188 | OnShutdown(&DeleteLogSilencerCount); |
| 189 | } |
| 190 | void InitLogSilencerCountOnce() { |
| 191 | GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount); |
| 192 | } |
| 193 | |
| 194 | LogMessage& LogMessage::operator<<(const string& value) { |
| 195 | message_ += value; |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | LogMessage& LogMessage::operator<<(const char* value) { |
| 200 | message_ += value; |
| 201 | return *this; |
| 202 | } |
| 203 | |
| 204 | LogMessage& LogMessage::operator<<(const StringPiece& value) { |
| 205 | message_ += value.ToString(); |
| 206 | return *this; |
| 207 | } |
| 208 | |
| 209 | LogMessage& LogMessage::operator<<( |
| 210 | const ::google::protobuf::util::Status& status) { |
| 211 | message_ += status.ToString(); |
| 212 | return *this; |
| 213 | } |
| 214 | |
| 215 | LogMessage& LogMessage::operator<<(const uint128& value) { |
| 216 | std::ostringstream str; |
| 217 | str << value; |
| 218 | message_ += str.str(); |
| 219 | return *this; |
| 220 | } |
| 221 | |
| 222 | // Since this is just for logging, we don't care if the current locale changes |
| 223 | // the results -- in fact, we probably prefer that. So we use snprintf() |
| 224 | // instead of Simple*toa(). |
| 225 | #undef DECLARE_STREAM_OPERATOR |
| 226 | #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \ |
| 227 | LogMessage& LogMessage::operator<<(TYPE value) { \ |
| 228 | /* 128 bytes should be big enough for any of the primitive */ \ |
| 229 | /* values which we print with this, but well use snprintf() */ \ |
| 230 | /* anyway to be extra safe. */ \ |
| 231 | char buffer[128]; \ |
| 232 | snprintf(buffer, sizeof(buffer), FORMAT, value); \ |
| 233 | /* Guard against broken MSVC snprintf(). */ \ |
| 234 | buffer[sizeof(buffer)-1] = '\0'; \ |
| 235 | message_ += buffer; \ |
| 236 | return *this; \ |
| 237 | } |
| 238 | |
| 239 | DECLARE_STREAM_OPERATOR(char , "%c" ) |
| 240 | DECLARE_STREAM_OPERATOR(int , "%d" ) |
| 241 | DECLARE_STREAM_OPERATOR(unsigned int , "%u" ) |
| 242 | DECLARE_STREAM_OPERATOR(long , "%ld") |
| 243 | DECLARE_STREAM_OPERATOR(unsigned long, "%lu") |
| 244 | DECLARE_STREAM_OPERATOR(double , "%g" ) |
| 245 | DECLARE_STREAM_OPERATOR(void* , "%p" ) |
| 246 | DECLARE_STREAM_OPERATOR(long long , "%" GOOGLE_LL_FORMAT "d") |
| 247 | DECLARE_STREAM_OPERATOR(unsigned long long, "%" GOOGLE_LL_FORMAT "u") |
| 248 | #undef DECLARE_STREAM_OPERATOR |
| 249 | |
| 250 | LogMessage::LogMessage(LogLevel level, const char* filename, int line) |
| 251 | : level_(level), filename_(filename), line_(line) {} |
| 252 | LogMessage::~LogMessage() {} |
| 253 | |
| 254 | void LogMessage::Finish() { |
| 255 | bool suppress = false; |
| 256 | |
| 257 | if (level_ != LOGLEVEL_FATAL) { |
| 258 | InitLogSilencerCountOnce(); |
| 259 | MutexLock lock(log_silencer_count_mutex_); |
| 260 | suppress = log_silencer_count_ > 0; |
| 261 | } |
| 262 | |
| 263 | if (!suppress) { |
| 264 | log_handler_(level_, filename_, line_, message_); |
| 265 | } |
| 266 | |
| 267 | if (level_ == LOGLEVEL_FATAL) { |
| 268 | #if PROTOBUF_USE_EXCEPTIONS |
| 269 | throw FatalException(filename_, line_, message_); |
| 270 | #else |
| 271 | abort(); |
| 272 | #endif |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void LogFinisher::operator=(LogMessage& other) { |
| 277 | other.Finish(); |
| 278 | } |
| 279 | |
| 280 | } // namespace internal |
| 281 | |
| 282 | LogHandler* SetLogHandler(LogHandler* new_func) { |
| 283 | LogHandler* old = internal::log_handler_; |
| 284 | if (old == &internal::NullLogHandler) { |
| 285 | old = NULL; |
| 286 | } |
| 287 | if (new_func == NULL) { |
| 288 | internal::log_handler_ = &internal::NullLogHandler; |
| 289 | } else { |
| 290 | internal::log_handler_ = new_func; |
| 291 | } |
| 292 | return old; |
| 293 | } |
| 294 | |
| 295 | LogSilencer::LogSilencer() { |
| 296 | internal::InitLogSilencerCountOnce(); |
| 297 | MutexLock lock(internal::log_silencer_count_mutex_); |
| 298 | ++internal::log_silencer_count_; |
| 299 | }; |
| 300 | |
| 301 | LogSilencer::~LogSilencer() { |
| 302 | internal::InitLogSilencerCountOnce(); |
| 303 | MutexLock lock(internal::log_silencer_count_mutex_); |
| 304 | --internal::log_silencer_count_; |
| 305 | }; |
| 306 | |
| 307 | // =================================================================== |
| 308 | // emulates google3/base/callback.cc |
| 309 | |
| 310 | Closure::~Closure() {} |
| 311 | |
| 312 | namespace internal { FunctionClosure0::~FunctionClosure0() {} } |
| 313 | |
| 314 | void DoNothing() {} |
| 315 | |
| 316 | // =================================================================== |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 317 | // emulates google3/util/endian/endian.h |
| 318 | // |
| 319 | // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in |
| 320 | // google/protobuf/io/coded_stream.h and therefore can not be used here. |
| 321 | // Maybe move that macro definition here in the furture. |
| 322 | uint32 ghtonl(uint32 x) { |
| 323 | union { |
| 324 | uint32 result; |
| 325 | uint8 result_array[4]; |
| 326 | }; |
| 327 | result_array[0] = static_cast<uint8>(x >> 24); |
| 328 | result_array[1] = static_cast<uint8>((x >> 16) & 0xFF); |
| 329 | result_array[2] = static_cast<uint8>((x >> 8) & 0xFF); |
| 330 | result_array[3] = static_cast<uint8>(x & 0xFF); |
| 331 | return result; |
| 332 | } |
| 333 | |
| 334 | // =================================================================== |
| 335 | // Shutdown support. |
| 336 | |
| 337 | namespace internal { |
| 338 | |
| 339 | typedef void OnShutdownFunc(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 340 | struct ShutdownData { |
| 341 | ~ShutdownData() { |
| 342 | std::reverse(functions.begin(), functions.end()); |
| 343 | for (auto pair : functions) pair.first(pair.second); |
| 344 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 345 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 346 | static ShutdownData* get() { |
| 347 | static auto* data = new ShutdownData; |
| 348 | return data; |
| 349 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 350 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 351 | std::vector<std::pair<void (*)(const void*), const void*>> functions; |
| 352 | Mutex mutex; |
| 353 | }; |
| 354 | |
| 355 | static void RunZeroArgFunc(const void* arg) { |
| 356 | reinterpret_cast<void (*)()>(const_cast<void*>(arg))(); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void OnShutdown(void (*func)()) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 360 | OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func)); |
| 361 | } |
| 362 | |
| 363 | void OnShutdownRun(void (*f)(const void*), const void* arg) { |
| 364 | auto shutdown_data = ShutdownData::get(); |
| 365 | MutexLock lock(&shutdown_data->mutex); |
| 366 | shutdown_data->functions.push_back(std::make_pair(f, arg)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | } // namespace internal |
| 370 | |
| 371 | void ShutdownProtobufLibrary() { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 372 | // This function should be called only once, but accepts multiple calls. |
| 373 | static bool is_shutdown = false; |
| 374 | if (!is_shutdown) { |
| 375 | delete internal::ShutdownData::get(); |
| 376 | is_shutdown = true; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 377 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #if PROTOBUF_USE_EXCEPTIONS |
| 381 | FatalException::~FatalException() throw() {} |
| 382 | |
| 383 | const char* FatalException::what() const throw() { |
| 384 | return message_.c_str(); |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | } // namespace protobuf |
| 389 | } // namespace google |