1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9
10 #include "google/protobuf/stubs/common.h"
11
12 #include <errno.h>
13 #include <stdio.h>
14
15 #include <atomic>
16 #include <sstream>
17 #include <vector>
18
19 #ifdef _WIN32
20 #ifndef WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN // We only need minimal includes
22 #endif
23 #include <windows.h>
24 #define snprintf _snprintf // see comment in strutil.cc
25 #endif
26 #if defined(__ANDROID__)
27 #include <android/log.h>
28 #endif
29
30 #include "absl/log/absl_log.h"
31 #include "absl/status/status.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/strings/string_view.h"
34 #include "google/protobuf/stubs/callback.h"
35
36 // Must be last.
37 #include "google/protobuf/port_def.inc" // NOLINT
38
39 namespace google {
40 namespace protobuf {
41
42 namespace internal {
43
VerifyVersion(int protobufVersionCompiledWith,const char * filename)44 void VerifyVersion(int protobufVersionCompiledWith, const char* filename) {
45 // If the user's program is linked against a different version of Protobuf,
46 // GOOGLE_PROTOBUF_VERSION will have a different value.
47 if (GOOGLE_PROTOBUF_VERSION != protobufVersionCompiledWith) {
48 ABSL_LOG(FATAL)
49 << "This program was compiled with Protobuf C++ version "
50 << VersionString(protobufVersionCompiledWith)
51 << ", but the linked version is "
52 << VersionString(GOOGLE_PROTOBUF_VERSION)
53 << ". Please update your library. If you compiled the program "
54 "yourself, make sure that"
55 "your headers are from the same version of Protocol Buffers as your "
56 "link-time library. (Version verification failed in \""
57 << filename << "\".)";
58 }
59 }
60
VersionString(int version)61 std::string VersionString(int version) {
62 int major = version / 1000000;
63 int minor = (version / 1000) % 1000;
64 int micro = version % 1000;
65
66 // 128 bytes should always be enough, but we use snprintf() anyway to be
67 // safe.
68 char buffer[128];
69 snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
70
71 // Guard against broken MSVC snprintf().
72 buffer[sizeof(buffer)-1] = '\0';
73
74 return buffer;
75 }
76
ProtocVersionString(int version)77 std::string ProtocVersionString(int version) {
78 int minor = (version / 1000) % 1000;
79 int micro = version % 1000;
80
81 // 128 bytes should always be enough, but we use snprintf() anyway to be
82 // safe.
83 char buffer[128];
84 snprintf(buffer, sizeof(buffer), "%d.%d", minor, micro);
85
86 // Guard against broken MSVC snprintf().
87 buffer[sizeof(buffer) - 1] = '\0';
88
89 return buffer;
90 }
91
92 } // namespace internal
93
94
95 // ===================================================================
96 // emulates google3/base/callback.cc
97
~Closure()98 Closure::~Closure() {}
99
~FunctionClosure0()100 namespace internal { FunctionClosure0::~FunctionClosure0() {} }
101
DoNothing()102 void DoNothing() {}
103
104 // ===================================================================
105 // emulates google3/util/endian/endian.h
ghtonl(uint32_t x)106 uint32_t ghtonl(uint32_t x) {
107 union {
108 uint32_t result;
109 uint8_t result_array[4];
110 };
111 result_array[0] = static_cast<uint8_t>(x >> 24);
112 result_array[1] = static_cast<uint8_t>((x >> 16) & 0xFF);
113 result_array[2] = static_cast<uint8_t>((x >> 8) & 0xFF);
114 result_array[3] = static_cast<uint8_t>(x & 0xFF);
115 return result;
116 }
117
118 } // namespace protobuf
119 } // namespace google
120
121 #include "google/protobuf/port_undef.inc" // NOLINT
122