6 string vstrprintf(const std::string &format, va_list ap)
10 int limit = sizeof(buffer);
16 ret = vsnprintf(p, limit, format.c_str(), arg_ptr);
18 if (ret >= 0 && ret < limit)
25 throw std::bad_alloc();
33 string EncodeBase32(const unsigned char* pch, size_t len)
35 static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
38 strRet.reserve((len+4)/5*8);
41 const unsigned char *pchEnd = pch+len;
48 case 0: // we have no bits
49 strRet += pbase32[enc >> 3];
50 left = (enc & 7) << 2;
54 case 1: // we have three bits
55 strRet += pbase32[left | (enc >> 6)];
56 strRet += pbase32[(enc >> 1) & 31];
57 left = (enc & 1) << 4;
61 case 2: // we have one bit
62 strRet += pbase32[left | (enc >> 4)];
63 left = (enc & 15) << 1;
67 case 3: // we have four bits
68 strRet += pbase32[left | (enc >> 7)];
69 strRet += pbase32[(enc >> 2) & 31];
70 left = (enc & 3) << 3;
74 case 4: // we have two bits
75 strRet += pbase32[left | (enc >> 5)];
76 strRet += pbase32[enc & 31];
81 static const int nPadding[5] = {0, 6, 4, 3, 1};
84 strRet += pbase32[left];
85 for (int n=0; n<nPadding[mode]; n++)
92 string EncodeBase32(const string& str)
94 return EncodeBase32((const unsigned char*)str.c_str(), str.size());
97 vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
99 static const int decode32_table[256] =
101 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
102 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
103 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
104 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
105 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
106 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
107 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
108 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
109 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
110 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
119 vector<unsigned char> vchRet;
120 vchRet.reserve((strlen(p))*5/8);
127 int dec = decode32_table[(unsigned char)*p];
128 if (dec == -1) break;
132 case 0: // we have no bits and get 5
137 case 1: // we have 5 bits and keep 2
138 vchRet.push_back((left<<3) | (dec>>2));
143 case 2: // we have 2 bits and keep 7
144 left = left << 5 | dec;
148 case 3: // we have 7 bits and keep 4
149 vchRet.push_back((left<<1) | (dec>>4));
154 case 4: // we have 4 bits, and keep 1
155 vchRet.push_back((left<<4) | (dec>>1));
160 case 5: // we have 1 bit, and keep 6
161 left = left << 5 | dec;
165 case 6: // we have 6 bits, and keep 3
166 vchRet.push_back((left<<2) | (dec>>3));
171 case 7: // we have 3 bits, and keep 0
172 vchRet.push_back((left<<5) | dec);
181 case 0: // 8n base32 characters processed: ok
184 case 1: // 8n+1 base32 characters processed: impossible
190 case 2: // 8n+2 base32 characters processed: require '======'
191 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
195 case 4: // 8n+4 base32 characters processed: require '===='
196 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
200 case 5: // 8n+5 base32 characters processed: require '==='
201 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
205 case 7: // 8n+7 base32 characters processed: require '='
206 if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
214 string DecodeBase32(const string& str)
216 vector<unsigned char> vchRet = DecodeBase32(str.c_str());
217 return string((const char*)&vchRet[0], vchRet.size());