base64.odin 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package base64
  2. // @note(zh): Encoding utility for Base64
  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 Base64 alphabet.
  6. // Incase your specific version does not use padding, you may
  7. // truncate it from the encoded output.
  8. ENC_TABLE := [64]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', 'a', 'b', 'c', 'd', 'e', 'f',
  13. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  14. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  15. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  16. '4', '5', '6', '7', '8', '9', '+', '/',
  17. }
  18. PADDING :: '='
  19. DEC_TABLE := [128]int {
  20. -1, -1, -1, -1, -1, -1, -1, -1,
  21. -1, -1, -1, -1, -1, -1, -1, -1,
  22. -1, -1, -1, -1, -1, -1, -1, -1,
  23. -1, -1, -1, -1, -1, -1, -1, -1,
  24. -1, -1, -1, -1, -1, -1, -1, -1,
  25. -1, -1, -1, 62, -1, -1, -1, 63,
  26. 52, 53, 54, 55, 56, 57, 58, 59,
  27. 60, 61, -1, -1, -1, -1, -1, -1,
  28. -1, 0, 1, 2, 3, 4, 5, 6,
  29. 7, 8, 9, 10, 11, 12, 13, 14,
  30. 15, 16, 17, 18, 19, 20, 21, 22,
  31. 23, 24, 25, -1, -1, -1, -1, -1,
  32. -1, 26, 27, 28, 29, 30, 31, 32,
  33. 33, 34, 35, 36, 37, 38, 39, 40,
  34. 41, 42, 43, 44, 45, 46, 47, 48,
  35. 49, 50, 51, -1, -1, -1, -1, -1,
  36. }
  37. encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> string #no_bounds_check {
  38. length := len(data)
  39. if length == 0 {
  40. return ""
  41. }
  42. out_length := ((4 * length / 3) + 3) &~ 3
  43. out := make([]byte, out_length, allocator)
  44. c0, c1, c2, block: int
  45. for i, d := 0, 0; i < length; i, d = i + 3, d + 4 {
  46. c0, c1, c2 = int(data[i]), -1, -1
  47. if i + 1 < length { c1 = int(data[i + 1]) }
  48. if i + 2 < length { c2 = int(data[i + 2]) }
  49. block = (c0 << 16) | (max(c1, 0) << 8) | max(c2, 0)
  50. out[d] = ENC_TBL[block >> 18 & 63]
  51. out[d + 1] = ENC_TBL[block >> 12 & 63]
  52. out[d + 2] = c1 == -1 ? PADDING : ENC_TBL[block >> 6 & 63]
  53. out[d + 3] = c2 == -1 ? PADDING : ENC_TBL[block & 63]
  54. }
  55. return string(out)
  56. }
  57. decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> []byte #no_bounds_check {
  58. length := len(data)
  59. if length == 0 {
  60. return nil
  61. }
  62. pad_count := data[length - 1] == PADDING ? (data[length - 2] == PADDING ? 2 : 1) : 0
  63. out_length := ((length * 6) >> 3) - pad_count
  64. out := make([]byte, out_length, allocator)
  65. c0, c1, c2, c3: int
  66. b0, b1, b2: int
  67. for i, j := 0, 0; i < length; i, j = i + 4, j + 3 {
  68. c0 = DEC_TBL[data[i]]
  69. c1 = DEC_TBL[data[i + 1]]
  70. c2 = DEC_TBL[data[i + 2]]
  71. c3 = DEC_TBL[data[i + 3]]
  72. b0 = (c0 << 2) | (c1 >> 4)
  73. b1 = (c1 << 4) | (c2 >> 2)
  74. b2 = (c2 << 6) | c3
  75. out[j] = byte(b0)
  76. out[j + 1] = byte(b1)
  77. out[j + 2] = byte(b2)
  78. }
  79. return out
  80. }