base_policy.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2019 Aleksey Moskvin <zerg1996@yandex.ru>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_BASE_POLICY_HPP
27 #define CRYPTO3_BASE_POLICY_HPP
28 
29 #include <array>
30 
31 #include <boost/integer.hpp>
32 
33 #include <boost/container/small_vector.hpp>
34 
35 #include <nil/crypto3/multiprecision/cpp_int.hpp>
36 #include <nil/crypto3/multiprecision/integer.hpp>
37 
38 #include <nil/crypto3/detail/inline_variable.hpp>
39 
40 using namespace nil::crypto3::multiprecision;
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace codec {
49  template<std::uint8_t Version>
50  struct base_decode_error : virtual boost::exception, virtual std::exception {};
51 
56  template<std::uint8_t Version>
57  struct wrong_input_range : virtual base_decode_error<Version> {};
58 
63  template<std::uint8_t Version>
64  struct wrong_input_symbol : virtual base_decode_error<Version> {};
65 
71  template<std::uint8_t Version>
72  struct non_base_input : virtual base_decode_error<Version> {};
73 
74  typedef boost::error_info<struct bad_char_, char> bad_char;
75 
76  namespace detail {
77 
78  template<std::size_t Version>
80 
81  template<>
82  class basic_base_policy<32> {
83  public:
84  typedef typename boost::uint_t<CHAR_BIT>::exact byte_type;
85 
86  constexpr static const std::size_t decoded_value_bits = CHAR_BIT;
88 
89  constexpr static const std::size_t encoded_value_bits = CHAR_BIT;
91 
92  constexpr static const std::size_t decoded_block_values = 5;
93  constexpr static const std::size_t decoded_block_bits = decoded_block_values * decoded_value_bits;
94  typedef std::array<decoded_value_type, decoded_block_bits / CHAR_BIT> decoded_block_type;
95 
96  constexpr static const std::size_t encoded_block_values = 8;
97  constexpr static const std::size_t encoded_block_bits = encoded_block_values * encoded_value_bits;
98  typedef std::array<encoded_value_type, encoded_block_bits / CHAR_BIT> encoded_block_type;
99 
100  constexpr static const std::size_t padding_block_bits = 5;
101  constexpr static const std::size_t padding_bits = 6;
102 
103  constexpr static const std::size_t constants_size = 32;
104  typedef std::array<byte_type, constants_size> constants_type;
105 
107  ({'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
108  'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
109  'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7'}));
110 
111  constexpr static const std::size_t inverted_constants_size = constants_size * 8;
112  typedef std::array<byte_type, inverted_constants_size> inverted_constants_type;
113 
115  inverted_constants_type, inverted_constants,
116  ({0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0xFF,
117  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0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F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
130  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
131  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
132  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
133  0xFF}));
134  };
135 
136  template<>
137  class basic_base_policy<64> {
138  public:
139  typedef typename boost::uint_t<CHAR_BIT>::exact byte_type;
140 
141  constexpr static const std::size_t decoded_value_bits = CHAR_BIT;
143 
144  constexpr static const std::size_t encoded_value_bits = CHAR_BIT;
146 
147  constexpr static const std::size_t decoded_block_values = 3;
148  constexpr static const std::size_t decoded_block_bits = decoded_block_values * decoded_value_bits;
149  typedef std::array<decoded_value_type, decoded_block_bits / CHAR_BIT> decoded_block_type;
150 
151  constexpr static const std::size_t encoded_block_values = 4;
152  constexpr static const std::size_t encoded_block_bits = encoded_block_values * encoded_value_bits;
153  typedef std::array<encoded_value_type, encoded_block_bits / CHAR_BIT> encoded_block_type;
154 
155  constexpr static const std::size_t padding_block_bits = 6;
156  constexpr static const std::size_t padding_bits = 8;
157 
158  constexpr static const std::size_t constants_size = 64;
159  typedef std::array<byte_type, constants_size> constants_type;
160 
162  ({'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
163  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
164  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
165  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
166  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}));
167 
168  constexpr static const std::size_t inverted_constants_size = constants_size * 4;
169  typedef std::array<byte_type, inverted_constants_size> inverted_constants_type;
170 
172  inverted_constants_type, inverted_constants,
173  ({0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0xFF,
174  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
175  0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF,
176  0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF,
177  0xFF, 0x81, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
178  0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
179  0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,
180  0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
181  0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
182  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
184  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
185  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
186  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
187  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
188  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
189  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
190  0xFF}));
191  };
192 
193  template<std::size_t Version>
194  class base_functions : public basic_base_policy<Version> {};
195 
196  template<>
197  class base_functions<32> : public basic_base_policy<32> {
198  public:
199  static inline encoded_block_type encode_block(const decoded_block_type &plaintext) {
200  encoded_block_type out = {0};
201 
202  out[0] = constants()[(plaintext[0] & 0xF8U) >> 3];
203  out[1] = constants()[((plaintext[0] & 0x07U) << 2) | (plaintext[1] >> 6)];
204  out[2] = constants()[((plaintext[1] & 0x3EU) >> 1)];
205  out[3] = constants()[((plaintext[1] & 0x01U) << 4) | (plaintext[2] >> 4)];
206  out[4] = constants()[((plaintext[2] & 0x0FU) << 1) | (plaintext[3] >> 7)];
207  out[5] = constants()[((plaintext[3] & 0x7CU) >> 2)];
208  out[6] = constants()[((plaintext[3] & 0x03U) << 3) | (plaintext[4] >> 5)];
209  out[7] = constants()[plaintext[4] & 0x1FU];
210 
211  return out;
212  }
213 
214  static inline decoded_block_type decode_block(const encoded_block_type &encoded) {
215  decoded_block_type out = {0};
216  encoded_block_type output_buffer = {0};
217 
218  auto oit = std::begin(output_buffer);
219 
220  for (auto it = std::begin(encoded); it != std::end(encoded); ++it) {
221  const uint8_t bin = inverted_constants()[*it];
222 
223  if (bin <= 0x3f) {
224  *oit++ = bin;
225  } else if (!(bin == 0x81 || bin == 0x80)) {
226  BOOST_THROW_EXCEPTION(non_base_input<32>());
227  }
228 
229  /*
230  * If we're at the end of the input, pad with 0s and truncate
231  */
232  if (std::distance(it, encoded.end()) == 1 && std::distance(output_buffer.begin(), oit)) {
233  for (auto itr = oit;
234  std::distance(output_buffer.begin(), itr) < decoded_block_bits / CHAR_BIT;
235  ++itr) {
236  *itr = 0x00U;
237  }
238 
239  oit = output_buffer.end();
240  }
241 
242  if (oit == output_buffer.end()) {
243  out[0] = (output_buffer[0] << 3U) | (output_buffer[1] >> 2U);
244  out[1] = (output_buffer[1] << 6U) | (output_buffer[2] << 1U) | (output_buffer[3] >> 4U);
245  out[2] = (output_buffer[3] << 4U) | (output_buffer[4] >> 1U);
246  out[3] = (output_buffer[4] << 7U) | (output_buffer[5] << 2U) | (output_buffer[6] >> 3U);
247  out[4] = (output_buffer[6] << 5U) | output_buffer[7];
248 
249  oit = output_buffer.begin();
250  }
251  }
252 
253  return out;
254  }
255  };
256 
257  template<>
258  class basic_base_policy<58> {
259  public:
260  typedef typename boost::uint_t<CHAR_BIT>::exact byte_type;
263 
264  constexpr static const std::size_t constants_size = 58;
265  constexpr static const std::size_t inverted_constants_size = 256;
266 
267  typedef std::array<encoded_value_type, constants_size> constants_type;
268  typedef std::array<decoded_value_type, inverted_constants_size> inverted_constants_type;
269 
271  ({'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
272  'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
273  'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm',
274  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}));
275 
277  ({0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
278  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
279  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
280  0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x80, 0x80,
281  0x80, 0x80, 0x80, 0x80, 0x80, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x80, 0x11,
282  0x12, 0x13, 0x14, 0x15, 0x80, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
283  0x20, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
284  0x29, 0x2A, 0x2B, 0x80, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
285  0x37, 0x38, 0x39, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
286  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
287  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
288  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
289  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
290  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
291  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
292  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
293  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
294  0x80}));
295 
296  constexpr static const std::size_t decoded_value_bits = CHAR_BIT;
297 
298  constexpr static const std::size_t encoded_value_bits = CHAR_BIT;
299 
300  constexpr static const std::size_t decoded_block_values = 1;
301  constexpr static const std::size_t decoded_block_bits = decoded_block_values * decoded_value_bits;
302  typedef boost::container::small_vector<decoded_value_type, decoded_block_values> decoded_block_type;
303 
304  constexpr static const std::size_t encoded_block_values = 1;
305  constexpr static const std::size_t encoded_block_bits = encoded_block_values * encoded_value_bits;
306  typedef boost::container::small_vector<encoded_value_type, encoded_block_values> encoded_block_type;
307  };
308 
309  template<>
310  class base_functions<58> : public basic_base_policy<58> {
311  public:
312  template<typename NumberType = cpp_int>
313  static inline encoded_block_type encode_block(const decoded_block_type &plaintext) {
314  encoded_block_type out;
315  std::size_t csint = basic_base_policy<58>::constants_size;
316  NumberType v = 0, q = 0, r = 0, cs(csint);
317 
318  import_bits(v, plaintext.begin(), plaintext.end(), encoded_value_bits);
319  while (v != 0) {
320  divide_qr(v, cs, q, r);
321  out.emplace_back(constants()[r.template convert_to<std::uint8_t>()]);
322  v = q;
323  }
324  return out;
325  }
326 
327  template<typename NumberType = cpp_int>
328  static inline decoded_block_type decode_block(const encoded_block_type &plaintext) {
329  decoded_block_type out;
330  std::size_t csint = basic_base_policy<58>::constants_size;
331  NumberType v = 0;
332 
333  for (const typename encoded_block_type::value_type &c : plaintext) {
334  if (c == ' ' || c == '\n') {
335  continue;
336  }
337  const size_t idx = inverted_constants()[c];
338 
339  if (idx == 0x80) {
340  throw wrong_input_symbol<58>();
341  // throw std::invalid_argument("Invalid base58");
342  }
343  v *= csint;
344  v += idx;
345  }
346  export_bits(v, std::inserter(out, out.end()), decoded_value_bits);
347  std::reverse(out.begin(), out.end());
348  return out;
349  }
350  };
351 
352  template<>
353  class base_functions<64> : public basic_base_policy<64> {
354  public:
355  static inline encoded_block_type encode_block(const decoded_block_type &plaintext) {
356  encoded_block_type output = {0};
357 
358  output[0] = constants()[(plaintext[0] & 0xfcU) >> 2U];
359  output[1] = constants()[((plaintext[0] & 0x03U) << 4U) | (plaintext[1] >> 4U)];
360  output[2] = constants()[((plaintext[1] & 0x0fU) << 2U) | (plaintext[2] >> 6U)];
361  output[3] = constants()[plaintext[2] & 0x3fU];
362 
363  return output;
364  }
365 
366  static inline decoded_block_type decode_block(const encoded_block_type &encoded) {
367  decoded_block_type out = {0};
368  encoded_block_type output_buffer = {0};
369 
370  typename decoded_block_type::iterator oit = std::begin(output_buffer);
371 
372  for (typename encoded_block_type::const_iterator it = std::begin(encoded);
373  it != std::end(encoded);
374  ++it) {
375  const uint8_t bin = inverted_constants()[*it];
376 
377  if (bin <= 0x3f) {
378  *oit++ = bin;
379  } else if (!(bin == 0x81 || bin == 0x80)) {
380  BOOST_THROW_EXCEPTION(non_base_input<64>());
381  }
382 
383  /*
384  * If we're at the end of the input, pad with 0s and truncate
385  */
386  if (std::distance(it, encoded.end()) == 1 && std::distance(output_buffer.begin(), oit)) {
387  for (auto itr = oit;
388  std::distance(output_buffer.begin(), itr) < decoded_block_bits / CHAR_BIT;
389  ++itr) {
390  *itr = 0x00;
391  }
392 
393  oit = output_buffer.end();
394  }
395 
396  if (oit == output_buffer.end()) {
397  out[0] = (output_buffer[0] << 2U) | (output_buffer[1] >> 4U);
398  out[1] = (output_buffer[1] << 4U) | (output_buffer[2] >> 2U);
399  out[2] = (output_buffer[2] << 6U) | output_buffer[3];
400 
401  oit = output_buffer.begin();
402  }
403  }
404 
405  return out;
406  }
407  };
408 
409  template<std::size_t Version>
410  class base_policy : public base_functions<Version> {
411  public:
412  };
413  } // namespace detail
414  } // namespace codec
415  } // namespace crypto3
416 } // namespace nil
417 
418 #endif // CRYPTO3_BASE_POLICY_HPP
static encoded_block_type encode_block(const decoded_block_type &plaintext)
Definition: base_policy.hpp:199
static decoded_block_type decode_block(const encoded_block_type &encoded)
Definition: base_policy.hpp:214
static encoded_block_type encode_block(const decoded_block_type &plaintext)
Definition: base_policy.hpp:313
static decoded_block_type decode_block(const encoded_block_type &plaintext)
Definition: base_policy.hpp:328
static encoded_block_type encode_block(const decoded_block_type &plaintext)
Definition: base_policy.hpp:355
static decoded_block_type decode_block(const encoded_block_type &encoded)
Definition: base_policy.hpp:366
Definition: base_policy.hpp:194
Definition: base_policy.hpp:410
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: base_policy.hpp:84
CRYPTO3_INLINE_VARIABLE(inverted_constants_type, inverted_constants,({0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x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F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}))
std::array< byte_type, constants_size > constants_type
Definition: base_policy.hpp:104
std::array< decoded_value_type, decoded_block_bits/CHAR_BIT > decoded_block_type
Definition: base_policy.hpp:94
std::array< encoded_value_type, encoded_block_bits/CHAR_BIT > encoded_block_type
Definition: base_policy.hpp:98
CRYPTO3_INLINE_VARIABLE(constants_type, constants,({ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7'}))
std::array< byte_type, inverted_constants_size > inverted_constants_type
Definition: base_policy.hpp:112
byte_type encoded_value_type
Definition: base_policy.hpp:90
byte_type decoded_value_type
Definition: base_policy.hpp:87
std::array< decoded_value_type, inverted_constants_size > inverted_constants_type
Definition: base_policy.hpp:268
byte_type encoded_value_type
Definition: base_policy.hpp:261
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: base_policy.hpp:260
byte_type decoded_value_type
Definition: base_policy.hpp:262
std::array< encoded_value_type, constants_size > constants_type
Definition: base_policy.hpp:267
boost::container::small_vector< decoded_value_type, decoded_block_values > decoded_block_type
Definition: base_policy.hpp:302
boost::container::small_vector< encoded_value_type, encoded_block_values > encoded_block_type
Definition: base_policy.hpp:306
CRYPTO3_INLINE_VARIABLE(constants_type, constants,({ '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}))
CRYPTO3_INLINE_VARIABLE(inverted_constants_type, inverted_constants,({0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x80, 0x11, 0x12, 0x13, 0x14, 0x15, 0x80, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x80, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}))
std::array< decoded_value_type, decoded_block_bits/CHAR_BIT > decoded_block_type
Definition: base_policy.hpp:149
CRYPTO3_INLINE_VARIABLE(inverted_constants_type, inverted_constants,({0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x81, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}))
std::array< byte_type, inverted_constants_size > inverted_constants_type
Definition: base_policy.hpp:169
CRYPTO3_INLINE_VARIABLE(constants_type, constants,({ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}))
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: base_policy.hpp:139
byte_type decoded_value_type
Definition: base_policy.hpp:142
byte_type encoded_value_type
Definition: base_policy.hpp:145
std::array< byte_type, constants_size > constants_type
Definition: base_policy.hpp:159
std::array< encoded_value_type, encoded_block_bits/CHAR_BIT > encoded_block_type
Definition: base_policy.hpp:153
boost::mpl::apply< AccumulatorSet, tag::codec< Mode > >::type::result_type codec(const AccumulatorSet &acc)
Definition: accumulators/codec.hpp:261
boost::error_info< struct bad_char_, char > bad_char
Definition: base_policy.hpp:74
void reverse(Range &a, std::size_t n)
Definition: basic_operations.hpp:54
Definition: pair.hpp:31
Base exception class for all base64 decoding errors.
Definition: base_policy.hpp:50
Thrown when a non-base64 value (0-9, A-F) encountered when decoding. Contains the offending character...
Definition: base_policy.hpp:72
Thrown in case of the range passed to base64 encoding is larger than 4 or smaller than 1.
Definition: base_policy.hpp:57
Thrown in case of the symbol passed to base58 encoding isn't in correct set for base58.
Definition: base_policy.hpp:64