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 
33 #include <google/protobuf/message_lite.h>  // TODO(gerbens) ideally remove this.
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 
VerifyVersion(int headerVersion,int minLibraryVersion,const char * filename)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 
VersionString(int version)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 
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 
118 namespace internal {
119 
120 #if defined(__ANDROID__)
DefaultLogHandler(LogLevel level,const char * filename,int line,const string & message)121 inline void DefaultLogHandler(LogLevel level, const char* filename, int line,
122                               const string& message) {
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   }
153 }
154 
155 #else
156 void DefaultLogHandler(LogLevel level, const char* filename, int line,
157                        const string& message) {
158   if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) {
159     return;
160   }
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 
NullLogHandler(LogLevel,const char *,int,const string &)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 
DeleteLogSilencerCount()182 void DeleteLogSilencerCount() {
183   delete log_silencer_count_mutex_;
184   log_silencer_count_mutex_ = NULL;
185 }
InitLogSilencerCount()186 void InitLogSilencerCount() {
187   log_silencer_count_mutex_ = new Mutex;
188   OnShutdown(&DeleteLogSilencerCount);
189 }
InitLogSilencerCountOnce()190 void InitLogSilencerCountOnce() {
191   GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount);
192 }
193 
operator <<(const string & value)194 LogMessage& LogMessage::operator<<(const string& value) {
195   message_ += value;
196   return *this;
197 }
198 
operator <<(const char * value)199 LogMessage& LogMessage::operator<<(const char* value) {
200   message_ += value;
201   return *this;
202 }
203 
operator <<(const StringPiece & value)204 LogMessage& LogMessage::operator<<(const StringPiece& value) {
205   message_ += value.ToString();
206   return *this;
207 }
208 
operator <<(const::google::protobuf::util::Status & status)209 LogMessage& LogMessage::operator<<(
210     const ::google::protobuf::util::Status& status) {
211   message_ += status.ToString();
212   return *this;
213 }
214 
operator <<(const uint128 & value)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 
LogMessage(LogLevel level,const char * filename,int line)250 LogMessage::LogMessage(LogLevel level, const char* filename, int line)
251   : level_(level), filename_(filename), line_(line) {}
~LogMessage()252 LogMessage::~LogMessage() {}
253 
Finish()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 
operator =(LogMessage & other)276 void LogFinisher::operator=(LogMessage& other) {
277   other.Finish();
278 }
279 
280 }  // namespace internal
281 
SetLogHandler(LogHandler * new_func)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 
LogSilencer()295 LogSilencer::LogSilencer() {
296   internal::InitLogSilencerCountOnce();
297   MutexLock lock(internal::log_silencer_count_mutex_);
298   ++internal::log_silencer_count_;
299 };
300 
~LogSilencer()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 
~Closure()310 Closure::~Closure() {}
311 
~FunctionClosure0()312 namespace internal { FunctionClosure0::~FunctionClosure0() {} }
313 
DoNothing()314 void DoNothing() {}
315 
316 // ===================================================================
317 // emulates google3/base/mutex.cc
318 
319 #ifdef _WIN32
320 
321 struct Mutex::Internal {
322   CRITICAL_SECTION mutex;
323 #ifndef NDEBUG
324   // Used only to implement AssertHeld().
325   DWORD thread_id;
326 #endif
327 };
328 
Mutex()329 Mutex::Mutex()
330   : mInternal(new Internal) {
331   InitializeCriticalSection(&mInternal->mutex);
332 }
333 
~Mutex()334 Mutex::~Mutex() {
335   DeleteCriticalSection(&mInternal->mutex);
336   delete mInternal;
337 }
338 
Lock()339 void Mutex::Lock() {
340   EnterCriticalSection(&mInternal->mutex);
341 #ifndef NDEBUG
342   mInternal->thread_id = GetCurrentThreadId();
343 #endif
344 }
345 
Unlock()346 void Mutex::Unlock() {
347 #ifndef NDEBUG
348   mInternal->thread_id = 0;
349 #endif
350   LeaveCriticalSection(&mInternal->mutex);
351 }
352 
AssertHeld()353 void Mutex::AssertHeld() {
354 #ifndef NDEBUG
355   GOOGLE_DCHECK_EQ(mInternal->thread_id, GetCurrentThreadId());
356 #endif
357 }
358 
359 #elif defined(HAVE_PTHREAD)
360 
361 struct Mutex::Internal {
362   pthread_mutex_t mutex;
363 };
364 
Mutex()365 Mutex::Mutex()
366   : mInternal(new Internal) {
367   pthread_mutex_init(&mInternal->mutex, NULL);
368 }
369 
~Mutex()370 Mutex::~Mutex() {
371   pthread_mutex_destroy(&mInternal->mutex);
372   delete mInternal;
373 }
374 
Lock()375 void Mutex::Lock() {
376   int result = pthread_mutex_lock(&mInternal->mutex);
377   if (result != 0) {
378     GOOGLE_LOG(FATAL) << "pthread_mutex_lock: " << strerror(result);
379   }
380 }
381 
Unlock()382 void Mutex::Unlock() {
383   int result = pthread_mutex_unlock(&mInternal->mutex);
384   if (result != 0) {
385     GOOGLE_LOG(FATAL) << "pthread_mutex_unlock: " << strerror(result);
386   }
387 }
388 
AssertHeld()389 void Mutex::AssertHeld() {
390   // pthreads dosn't provide a way to check which thread holds the mutex.
391   // TODO(kenton):  Maybe keep track of locking thread ID like with WIN32?
392 }
393 
394 #endif
395 
396 // ===================================================================
397 // emulates google3/util/endian/endian.h
398 //
399 // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
400 // google/protobuf/io/coded_stream.h and therefore can not be used here.
401 // Maybe move that macro definition here in the furture.
ghtonl(uint32 x)402 uint32 ghtonl(uint32 x) {
403   union {
404     uint32 result;
405     uint8 result_array[4];
406   };
407   result_array[0] = static_cast<uint8>(x >> 24);
408   result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
409   result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
410   result_array[3] = static_cast<uint8>(x & 0xFF);
411   return result;
412 }
413 
414 // ===================================================================
415 // Shutdown support.
416 
417 namespace internal {
418 
419 typedef void OnShutdownFunc();
420 struct ShutdownData {
~ShutdownDatagoogle::protobuf::internal::ShutdownData421   ~ShutdownData() {
422     for (int i = 0; i < functions.size(); i++) {
423       functions[i]();
424     }
425     for (int i = 0; i < strings.size(); i++) {
426       strings[i]->~string();
427     }
428     for (int i = 0; i < messages.size(); i++) {
429       messages[i]->~MessageLite();
430     }
431   }
432 
433   vector<void (*)()> functions;
434   vector<const std::string*> strings;
435   vector<const MessageLite*> messages;
436   Mutex mutex;
437 };
438 
439 ShutdownData* shutdown_data = NULL;
440 GOOGLE_PROTOBUF_DECLARE_ONCE(shutdown_functions_init);
441 
InitShutdownFunctions()442 void InitShutdownFunctions() {
443   shutdown_data = new ShutdownData;
444 }
445 
InitShutdownFunctionsOnce()446 inline void InitShutdownFunctionsOnce() {
447   GoogleOnceInit(&shutdown_functions_init, &InitShutdownFunctions);
448 }
449 
OnShutdown(void (* func)())450 void OnShutdown(void (*func)()) {
451   InitShutdownFunctionsOnce();
452   MutexLock lock(&shutdown_data->mutex);
453   shutdown_data->functions.push_back(func);
454 }
455 
OnShutdownDestroyString(const std::string * ptr)456 void OnShutdownDestroyString(const std::string* ptr) {
457   InitShutdownFunctionsOnce();
458   MutexLock lock(&shutdown_data->mutex);
459   shutdown_data->strings.push_back(ptr);
460 }
461 
OnShutdownDestroyMessage(const void * ptr)462 void OnShutdownDestroyMessage(const void* ptr) {
463   InitShutdownFunctionsOnce();
464   MutexLock lock(&shutdown_data->mutex);
465   shutdown_data->messages.push_back(static_cast<const MessageLite*>(ptr));
466 }
467 
468 }  // namespace internal
469 
ShutdownProtobufLibrary()470 void ShutdownProtobufLibrary() {
471   internal::InitShutdownFunctionsOnce();
472 
473   // We don't need to lock shutdown_functions_mutex because it's up to the
474   // caller to make sure that no one is using the library before this is
475   // called.
476 
477   // Make it safe to call this multiple times.
478   if (internal::shutdown_data == NULL) return;
479 
480   delete internal::shutdown_data;
481   internal::shutdown_data = NULL;
482 }
483 
484 #if PROTOBUF_USE_EXCEPTIONS
~FatalException()485 FatalException::~FatalException() throw() {}
486 
what() const487 const char* FatalException::what() const throw() {
488   return message_.c_str();
489 }
490 #endif
491 
492 }  // namespace protobuf
493 }  // namespace google
494