MCPcopy Create free account
hub / github.com/LUX-Core/lux / EncodeBase32

Function EncodeBase32

src/utilstrencodings.cpp:287–341  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

285}
286
287string EncodeBase32(const unsigned char* pch, size_t len)
288{
289 static const char* pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
290
291 string strRet = "";
292 strRet.reserve((len + 4) / 5 * 8);
293
294 int mode = 0, left = 0;
295 const unsigned char* pchEnd = pch + len;
296
297 while (pch < pchEnd) {
298 int enc = *(pch++);
299 switch (mode) {
300 case 0: // we have no bits
301 strRet += pbase32[enc >> 3];
302 left = (enc & 7) << 2;
303 mode = 1;
304 break;
305
306 case 1: // we have three bits
307 strRet += pbase32[left | (enc >> 6)];
308 strRet += pbase32[(enc >> 1) & 31];
309 left = (enc & 1) << 4;
310 mode = 2;
311 break;
312
313 case 2: // we have one bit
314 strRet += pbase32[left | (enc >> 4)];
315 left = (enc & 15) << 1;
316 mode = 3;
317 break;
318
319 case 3: // we have four bits
320 strRet += pbase32[left | (enc >> 7)];
321 strRet += pbase32[(enc >> 2) & 31];
322 left = (enc & 3) << 3;
323 mode = 4;
324 break;
325
326 case 4: // we have two bits
327 strRet += pbase32[left | (enc >> 5)];
328 strRet += pbase32[enc & 31];
329 mode = 0;
330 }
331 }
332
333 static const int nPadding[5] = {0, 6, 4, 3, 1};
334 if (mode) {
335 strRet += pbase32[left];
336 for (int n = 0; n < nPadding[mode]; n++)
337 strRet += '=';
338 }
339
340 return strRet;
341}
342
343string EncodeBase32(const string& str)
344{

Callers 2

ToStringIPMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 2

reserveMethod · 0.45
sizeMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68