Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
base36.cpp
Go to the documentation of this file.
1#include <memory>
3#include <fc/io/sstream.hpp>
4#include <fc/log/logger.hpp>
5
6namespace fc
7{
8 fc::string to_base36( const char* data, size_t len )
9 {
10 if( len == 0 ) return fc::string();
11
12 const char* src = data;
13 int src_len = len;
14 std::unique_ptr<char[]> buffer(new char[len+1]);
15 if (*data & 0x80) {
16 buffer[0] = 0;
17 memcpy( buffer.get() + 1, data, len );
18 src = buffer.get();
19 src_len++;
20 }
21 fc::bigint value( src, src_len );
22
23 auto base36 = "0123456789abcdefghijklmnopqrstuvwxyz";
24 std::vector<char> out( static_cast<size_t>(len * 1.6) + 2 );
25 int pos = out.size() - 1;
26 out[pos] = '\0';
27 fc::bigint _36(36);
28 do {
29 if( value ) {
30 --pos;
31 out[pos] = base36[(value % _36).to_int64()];
32 }
33 } while (value /= _36);
34 while (len-- > 0 && *data++ == 0) {
35 out[--pos] = '0';
36 }
37 return &out[pos]; //fc::string( &out[pos], out.size() - pos);
38 }
39
40 fc::string to_base36( const std::vector<char>& vec )
41 {
42 return to_base36( (const char*)vec.data(), vec.size() );
43 }
44
45 std::vector<char> from_base36( const fc::string& b36 )
46 {
47 if ( b36.empty() ) {
48 std::vector<char> empty;
49 return empty;
50 }
51
53
54 fc::bigint pos = 0;
55 fc::bigint _36(36);
56 for( auto itr = b36.rbegin(); itr != b36.rend(); ++itr )
57 {
58 if( *itr >= '0' && *itr <= '9' )
59 value = value + _36.exp(pos) * fc::bigint(*itr - '0');
60 else if( *itr >= 'a' && *itr <= 'z' )
61 value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'a'));
62 else if( *itr >= 'A' && *itr <= 'Z' )
63 value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A'));
64 else
65 {
66 wlog("unknown '${char}'", ("char",fc::string(&*itr,1)) );
67 }
68 ++pos;
69 }
70
71 std::vector<char> bytes = value;
72 int leading_zeros = 0, len = bytes.size();
73 const char *in = b36.c_str();
74 while (*in++ == '0') { leading_zeros++; }
75 char* first = bytes.data();
76 while (len > 0 && *first == 0) { first++; len--; }
77 std::vector<char> result;
78 result.resize(leading_zeros + len, 0);
79 memcpy( result.data() + leading_zeros, first, len );
80 return result;
81 }
82}
bigint exp(const bigint &c) const
Definition bigint.cpp:180
#define wlog(FORMAT,...)
Definition logger.hpp:124
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
std::vector< char > from_base36(const fc::string &b36)
Definition base36.cpp:45
std::vector< char > bytes
Definition alt_bn128.hpp:10
fc::string to_base36(const std::vector< char > &vec)
Definition base36.cpp:40
int64_t to_int64(const fc::string &)
Definition string.cpp:92
#define value
Definition pkcs11.h:157
size_t len
memcpy((char *) pInfo->slotDescription, s, l)