base32.odin 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package base32
  2. // @note(zh): Encoding utility for Base32
  3. // A secondary param can be used to supply a custom alphabet to
  4. // @link(encode) and a matching decoding table to @link(decode).
  5. // If none is supplied it just uses the standard Base32 alphabet.
  6. // Incase your specific version does not use padding, you may
  7. // truncate it from the encoded output.
  8. ENC_TABLE := [32]byte {
  9. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  10. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  11. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  12. 'Y', 'Z', '2', '3', '4', '5', '6', '7'
  13. };
  14. PADDING :: '=';
  15. DEC_TABLE := [?]u8 {
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  21. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
  22. 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  23. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  30. };
  31. encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> string {
  32. out_length := (len(data) + 4) / 5 * 8;
  33. out := make([]byte, out_length);
  34. _encode(out, data);
  35. return string(out);
  36. }
  37. @private
  38. _encode :: inline proc "contextless"(out, data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) {
  39. out := out;
  40. data := data;
  41. for len(data) > 0 {
  42. carry: byte;
  43. switch len(data) {
  44. case:
  45. out[7] = ENC_TABLE[data[4] & 0x1f];
  46. carry = data[4] >> 5;
  47. fallthrough;
  48. case 4:
  49. out[6] = ENC_TABLE[carry | (data[3] << 3) & 0x1f];
  50. out[5] = ENC_TABLE[(data[3] >> 2) & 0x1f];
  51. carry = data[3] >> 7;
  52. fallthrough;
  53. case 3:
  54. out[4] = ENC_TABLE[carry | (data[2] << 1) & 0x1f];
  55. carry = (data[2] >> 4) & 0x1f;
  56. fallthrough;
  57. case 2:
  58. out[3] = ENC_TABLE[carry | (data[1] << 4) & 0x1f];
  59. out[2] = ENC_TABLE[(data[1] >> 1) & 0x1f];
  60. carry = (data[1] >> 6) & 0x1f;
  61. fallthrough;
  62. case 1:
  63. out[1] = ENC_TABLE[carry | (data[0] << 2) & 0x1f];
  64. out[0] = ENC_TABLE[data[0] >> 3];
  65. }
  66. if len(data) < 5 {
  67. out[7] = byte(PADDING);
  68. if len(data) < 4 {
  69. out[6] = byte(PADDING);
  70. out[5] = byte(PADDING);
  71. if len(data) < 3 {
  72. out[4] = byte(PADDING);
  73. if len(data) < 2 {
  74. out[3] = byte(PADDING);
  75. out[2] = byte(PADDING);
  76. }
  77. }
  78. }
  79. break;
  80. }
  81. data = data[5:];
  82. out = out[8:];
  83. }
  84. }
  85. decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> []byte #no_bounds_check{
  86. if len(data) == 0 do return []byte{};
  87. outi := 0;
  88. olen := len(data);
  89. data := data;
  90. out := make([]byte, len(data) / 8 * 5, allocator);
  91. end := false;
  92. for len(data) > 0 && !end {
  93. dbuf : [8]byte;
  94. dlen := 8;
  95. for j := 0; j < 8; {
  96. if len(data) == 0 {
  97. dlen, end = j, true;
  98. break;
  99. }
  100. input := data[0];
  101. data = data[1:];
  102. if input == byte(PADDING) && j >= 2 && len(data) < 8 {
  103. assert(!(len(data) + j < 8 - 1), "Corrupted input");
  104. for k := 0; k < 8-1-j; k +=1 do assert(len(data) < k || data[k] == byte(PADDING), "Corrupted input");
  105. dlen, end = j, true;
  106. assert(dlen != 1 && dlen != 3 && dlen != 6, "Corrupted input");
  107. break;
  108. }
  109. dbuf[j] = DEC_TABLE[input];
  110. assert(dbuf[j] != 0xff, "Corrupted input");
  111. j += 1;
  112. }
  113. switch dlen {
  114. case 8:
  115. out[outi + 4] = dbuf[6] << 5 | dbuf[7];
  116. fallthrough;
  117. case 7:
  118. out[outi + 3] = dbuf[4] << 7 | dbuf[5] << 2 | dbuf[6] >> 3;
  119. fallthrough;
  120. case 5:
  121. out[outi + 2] = dbuf[3] << 4 | dbuf[4] >> 1;
  122. fallthrough;
  123. case 4:
  124. out[outi + 1] = dbuf[1] << 6 | dbuf[2] << 1 | dbuf[3] >> 4;
  125. fallthrough;
  126. case 2:
  127. out[outi + 0] = dbuf[0] << 3 | dbuf[1] >> 2;
  128. }
  129. outi += 5;
  130. }
  131. return out;
  132. }