Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
bytes.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 #include "google/cloud/status.h"
17 #include <array>
18 #include <cctype>
19 #include <climits>
20 #include <cstdio>
21 
22 namespace google {
23 namespace cloud {
24 namespace spanner {
25 inline namespace SPANNER_CLIENT_NS {
26 
27 namespace {
28 
29 constexpr char kPadding = '=';
30 
31 // The extra braces are working around an old clang bug that was fixed in 6.0
32 // https://bugs.llvm.org/show_bug.cgi?id=21629
33 constexpr std::array<char, 64> kIndexToChar = {{
34  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
35  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
36  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
37  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
38  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
39 }};
40 
41 // The extra braces are working around an old clang bug that was fixed in 6.0
42 // https://bugs.llvm.org/show_bug.cgi?id=21629
43 constexpr std::array<unsigned char, UCHAR_MAX + 1> kCharToIndexExcessOne = {{
44  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46  0, 0, 0, 0, 0, 0, 0, 63, 0, 0, 0, 64, 53, 54, 55, 56, 57, 58,
47  59, 60, 61, 62, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7,
48  8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
49  26, 0, 0, 0, 0, 0, 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
50  38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
51 }};
52 
53 // kCharToIndexExcessOne[] assumes an ASCII execution character set.
54 static_assert('A' == 65, "required by base64 decoder");
55 
56 // UCHAR_MAX is required to be at least 255, meaning std::string::value_type
57 // can always hold an octet. If UCHAR_MAX > 255, however, we have no way to
58 // base64 encode large values. So, we demand exactly 255.
59 static_assert(UCHAR_MAX == 255, "required by base64 decoder");
60 
61 } // namespace
62 
63 // Prints the bytes in the form B"...", where printable bytes are output
64 // normally, double quotes are backslash escaped, and non-printable characters
65 // are printed as a 3-digit octal escape sequence.
66 std::ostream& operator<<(std::ostream& os, Bytes const& bytes) {
67  os << R"(B")";
68  for (auto const byte : Bytes::Decoder(bytes.base64_rep_)) {
69  if (byte == '"') {
70  os << R"(\")";
71  } else if (std::isprint(byte)) {
72  os << byte;
73  } else {
74  // This uses snprintf rather than iomanip so we don't mess up the
75  // formatting on `os` for other streaming operations.
76  std::array<char, sizeof(R"(\000)")> buf;
77  auto n = std::snprintf(buf.data(), buf.size(), R"(\%03o)", byte);
78  if (n == static_cast<int>(buf.size() - 1)) {
79  os << buf.data();
80  } else {
81  os << R"(\?)";
82  }
83  }
84  }
85  // Can't use raw string literal here because of a doxygen bug.
86  return os << "\"";
87 }
88 
89 void Bytes::Encoder::Flush() {
90  unsigned int const v = buf_[0] << 16 | buf_[1] << 8 | buf_[2];
91  rep_.push_back(kIndexToChar[v >> 18]);
92  rep_.push_back(kIndexToChar[v >> 12 & 0x3f]);
93  rep_.push_back(kIndexToChar[v >> 6 & 0x3f]);
94  rep_.push_back(kIndexToChar[v & 0x3f]);
95  len_ = 0;
96 }
97 
98 void Bytes::Encoder::FlushAndPad() {
99  switch (len_) {
100  case 2: {
101  unsigned int const v = buf_[0] << 16 | buf_[1] << 8;
102  rep_.push_back(kIndexToChar[v >> 18]);
103  rep_.push_back(kIndexToChar[v >> 12 & 0x3f]);
104  rep_.push_back(kIndexToChar[v >> 6 & 0x3f]);
105  rep_.push_back(kPadding);
106  break;
107  }
108  case 1: {
109  unsigned int const v = buf_[0] << 16;
110  rep_.push_back(kIndexToChar[v >> 18]);
111  rep_.push_back(kIndexToChar[v >> 12 & 0x3f]);
112  rep_.append(2, kPadding);
113  break;
114  }
115  }
116 }
117 
118 void Bytes::Decoder::Iterator::Fill() {
119  if (pos_ != end_) {
120  unsigned char p0 = *pos_++;
121  unsigned char p1 = *pos_++;
122  unsigned char p2 = *pos_++;
123  unsigned char p3 = *pos_++;
124  auto i0 = kCharToIndexExcessOne[p0] - 1;
125  auto i1 = kCharToIndexExcessOne[p1] - 1;
126  if (p3 == kPadding) {
127  if (p2 == kPadding) {
128  buf_[++len_] = i0 << 2 | i1 >> 4;
129  } else {
130  auto i2 = kCharToIndexExcessOne[p2] - 1;
131  buf_[++len_] = i1 << 4 | i2 >> 2;
132  buf_[++len_] = i0 << 2 | i1 >> 4;
133  }
134  } else {
135  auto i2 = kCharToIndexExcessOne[p2] - 1;
136  auto i3 = kCharToIndexExcessOne[p3] - 1;
137  buf_[++len_] = i2 << 6 | i3;
138  buf_[++len_] = i1 << 4 | i2 >> 2;
139  buf_[++len_] = i0 << 2 | i1 >> 4;
140  }
141  }
142 }
143 
144 namespace internal {
145 
146 // Construction from a base64-encoded US-ASCII `std::string`.
147 StatusOr<Bytes> BytesFromBase64(std::string input) {
148  auto* p = reinterpret_cast<unsigned char const*>(input.data());
149  auto* ep = p + input.size();
150  while (ep - p >= 4) {
151  auto i0 = kCharToIndexExcessOne[p[0]];
152  auto i1 = kCharToIndexExcessOne[p[1]];
153  if (--i0 >= 64 || --i1 >= 64) break;
154  if (p[3] == kPadding) {
155  if (p[2] == kPadding) {
156  if ((i1 & 0xf) != 0) break;
157  } else {
158  auto i2 = kCharToIndexExcessOne[p[2]];
159  if (--i2 >= 64 || (i2 & 0x3) != 0) break;
160  }
161  p += 4;
162  break;
163  }
164  auto i2 = kCharToIndexExcessOne[p[2]];
165  auto i3 = kCharToIndexExcessOne[p[3]];
166  if (--i2 >= 64 || --i3 >= 64) break;
167  p += 4;
168  }
169  if (p != ep) {
170  auto const offset = reinterpret_cast<char const*>(p) - input.data();
171  auto const bad_chunk = input.substr(offset, 4);
172  auto message = "Invalid base64 chunk \"" + bad_chunk + "\"" +
173  " at offset " + std::to_string(offset);
174  return Status(StatusCode::kInvalidArgument, std::move(message));
175  }
176  Bytes bytes;
177  bytes.base64_rep_ = std::move(input);
178  return bytes;
179 }
180 
181 // Conversion to a base64-encoded US-ASCII `std::string`.
182 std::string BytesToBase64(Bytes b) { return std::move(b.base64_rep_); }
183 
184 } // namespace internal
185 } // namespace SPANNER_CLIENT_NS
186 } // namespace spanner
187 } // namespace cloud
188 } // namespace google
std::ostream & operator<<(std::ostream &os, Bytes const &bytes)
Definition: bytes.cc:66
Contains all the Cloud Spanner C++ client types and functions.
A representation of the Spanner BYTES type: variable-length binary data.
Definition: bytes.h:45
#define SPANNER_CLIENT_NS
Definition: version.h:22