base32.odin 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 :: proc(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 {
  87. return nil
  88. }
  89. outi := 0
  90. data := data
  91. out := make([]byte, len(data) / 8 * 5, allocator)
  92. end := false
  93. for len(data) > 0 && !end {
  94. dbuf : [8]byte
  95. dlen := 8
  96. for j := 0; j < 8; {
  97. if len(data) == 0 {
  98. dlen, end = j, true
  99. break
  100. }
  101. input := data[0]
  102. data = data[1:]
  103. if input == byte(PADDING) && j >= 2 && len(data) < 8 {
  104. assert(!(len(data) + j < 8 - 1), "Corrupted input")
  105. for k := 0; k < 8-1-j; k +=1 {
  106. assert(len(data) < k || data[k] == byte(PADDING), "Corrupted input")
  107. }
  108. dlen, end = j, true
  109. assert(dlen != 1 && dlen != 3 && dlen != 6, "Corrupted input")
  110. break
  111. }
  112. dbuf[j] = DEC_TABLE[input]
  113. assert(dbuf[j] != 0xff, "Corrupted input")
  114. j += 1
  115. }
  116. switch dlen {
  117. case 8:
  118. out[outi + 4] = dbuf[6] << 5 | dbuf[7]
  119. fallthrough
  120. case 7:
  121. out[outi + 3] = dbuf[4] << 7 | dbuf[5] << 2 | dbuf[6] >> 3
  122. fallthrough
  123. case 5:
  124. out[outi + 2] = dbuf[3] << 4 | dbuf[4] >> 1
  125. fallthrough
  126. case 4:
  127. out[outi + 1] = dbuf[1] << 6 | dbuf[2] << 1 | dbuf[3] >> 4
  128. fallthrough
  129. case 2:
  130. out[outi + 0] = dbuf[0] << 3 | dbuf[1] >> 2
  131. }
  132. outi += 5
  133. }
  134. return out
  135. }