base64.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 do return "";
  40. out_length := ((4 * length / 3) + 3) &~ 3;
  41. out := make([]byte, out_length, allocator);
  42. c0, c1, c2, block: int;
  43. for i, d := 0, 0; i < length; i, d = i + 3, d + 4 {
  44. c0, c1, c2 = int(data[i]), 0, 0;
  45. if i + 1 < length do c1 = int(data[i + 1]);
  46. if i + 2 < length do c2 = int(data[i + 2]);
  47. block = (c0 << 16) | (max(c1, 0) << 8) | max(c2, 0);
  48. out[d] = ENC_TBL[block >> 18 & 63];
  49. out[d + 1] = ENC_TBL[block >> 12 & 63];
  50. out[d + 2] = c1 == 0 ? PADDING : ENC_TBL[block >> 6 & 63];
  51. out[d + 3] = c2 == 0 ? PADDING : ENC_TBL[block & 63];
  52. }
  53. return string(out);
  54. }
  55. decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> []byte #no_bounds_check{
  56. length := len(data);
  57. if length == 0 do return []byte{};
  58. pad_count := data[length - 1] == PADDING ? (data[length - 2] == PADDING ? 2 : 1) : 0;
  59. out_length := ((length * 6) >> 3) - pad_count;
  60. out := make([]byte, out_length, allocator);
  61. c0, c1, c2, c3: int;
  62. b0, b1, b2: int;
  63. for i, j := 0, 0; i < length; i, j = i + 4, j + 3 {
  64. c0 = DEC_TBL[data[i]];
  65. c1 = DEC_TBL[data[i + 1]];
  66. c2 = DEC_TBL[data[i + 2]];
  67. c3 = DEC_TBL[data[i + 3]];
  68. b0 = (c0 << 2) | (c1 >> 4);
  69. b1 = (c1 << 4) | (c2 >> 2);
  70. b2 = (c2 << 6) | c3;
  71. out[j] = byte(b0);
  72. out[j + 1] = byte(b1);
  73. out[j + 2] = byte(b2);
  74. }
  75. return out;
  76. }