Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.23 KB | None | 0 0
  1. static const ubyte base32_PAD = '=';
  2. static const char[] _encodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  3.  
  4. /*******************************************************************************
  5.     encodes data and returns as an ASCII base32 string.
  6. *******************************************************************************/
  7. char[] encode(ubyte[] data, char[] buff, bool pad=true)
  8. in
  9. {
  10.     assert(data);
  11.     assert(buff.length >= allocateEncodeSize(data));
  12. }
  13. body
  14. {
  15.     uint i = 0;
  16.     ushort remainder; // Carries overflow bits to next char
  17.     byte remainlen;  // Tracks bits in remainder
  18.     foreach (ubyte j; data)
  19.     {
  20.         remainder = (remainder<<8) | j;
  21.         remainlen += 8;
  22.         do {
  23.             remainlen -= 5;
  24.             buff[i++] = _encodeTable[(remainder>>remainlen)&0b11111];
  25.         } while (remainlen > 5)
  26.     }
  27.     if (remainlen)
  28.         buff[i++] = _encodeTable[(remainder<<(5-remainlen))&0b11111];
  29.     if (pad) {
  30.         for (ubyte padCount=(-i%8);padCount > 0; padCount--)
  31.             buff[i++] = base32_PAD;
  32.     }
  33.  
  34.     return buff[0..i];
  35. }
  36.  
  37.  
  38. /*******************
  39.  * Called like this
  40.  ******************/
  41. scope char[64] idHexBuf;
  42. auto idHex = base32.encode(asset.localId, idHexBuf);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement