base64.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package encoding_base64
  2. import "core:io"
  3. import "core:mem"
  4. import "core:strings"
  5. // @note(zh): Encoding utility for Base64
  6. // A secondary param can be used to supply a custom alphabet to
  7. // @link(encode) and a matching decoding table to @link(decode).
  8. // If none is supplied it just uses the standard Base64 alphabet.
  9. // Incase your specific version does not use padding, you may
  10. // truncate it from the encoded output.
  11. ENC_TABLE := [64]byte {
  12. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  13. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  14. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  15. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  16. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  17. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  18. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  19. '4', '5', '6', '7', '8', '9', '+', '/',
  20. }
  21. PADDING :: '='
  22. DEC_TABLE := [128]int {
  23. -1, -1, -1, -1, -1, -1, -1, -1,
  24. -1, -1, -1, -1, -1, -1, -1, -1,
  25. -1, -1, -1, -1, -1, -1, -1, -1,
  26. -1, -1, -1, -1, -1, -1, -1, -1,
  27. -1, -1, -1, -1, -1, -1, -1, -1,
  28. -1, -1, -1, 62, -1, -1, -1, 63,
  29. 52, 53, 54, 55, 56, 57, 58, 59,
  30. 60, 61, -1, -1, -1, -1, -1, -1,
  31. -1, 0, 1, 2, 3, 4, 5, 6,
  32. 7, 8, 9, 10, 11, 12, 13, 14,
  33. 15, 16, 17, 18, 19, 20, 21, 22,
  34. 23, 24, 25, -1, -1, -1, -1, -1,
  35. -1, 26, 27, 28, 29, 30, 31, 32,
  36. 33, 34, 35, 36, 37, 38, 39, 40,
  37. 41, 42, 43, 44, 45, 46, 47, 48,
  38. 49, 50, 51, -1, -1, -1, -1, -1,
  39. }
  40. encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> (encoded: string, err: mem.Allocator_Error) #optional_allocator_error {
  41. out_length := encoded_len(data)
  42. if out_length == 0 {
  43. return
  44. }
  45. out := strings.builder_make(0, out_length, allocator) or_return
  46. ioerr := encode_into(strings.to_stream(&out), data, ENC_TBL)
  47. assert(ioerr == nil, "string builder should not IO error")
  48. assert(strings.builder_cap(out) == out_length, "buffer resized, `encoded_len` was wrong")
  49. return strings.to_string(out), nil
  50. }
  51. encode_into :: proc(w: io.Writer, data: []byte, ENC_TBL := ENC_TABLE) -> io.Error {
  52. length := len(data)
  53. if length == 0 {
  54. return nil
  55. }
  56. c0, c1, c2, block: int
  57. out: [4]byte
  58. for i := 0; i < length; i += 3 {
  59. #no_bounds_check {
  60. c0, c1, c2 = int(data[i]), -1, -1
  61. if i + 1 < length { c1 = int(data[i + 1]) }
  62. if i + 2 < length { c2 = int(data[i + 2]) }
  63. block = (c0 << 16) | (max(c1, 0) << 8) | max(c2, 0)
  64. out[0] = ENC_TBL[block >> 18 & 63]
  65. out[1] = ENC_TBL[block >> 12 & 63]
  66. out[2] = c1 == -1 ? PADDING : ENC_TBL[block >> 6 & 63]
  67. out[3] = c2 == -1 ? PADDING : ENC_TBL[block & 63]
  68. }
  69. io.write_full(w, out[:]) or_return
  70. }
  71. return nil
  72. }
  73. encoded_len :: proc(data: []byte) -> int {
  74. length := len(data)
  75. if length == 0 {
  76. return 0
  77. }
  78. return ((4 * length / 3) + 3) &~ 3
  79. }
  80. decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> (decoded: []byte, err: mem.Allocator_Error) #optional_allocator_error {
  81. out_length := decoded_len(data)
  82. out := strings.builder_make(0, out_length, allocator) or_return
  83. ioerr := decode_into(strings.to_stream(&out), data, DEC_TBL)
  84. assert(ioerr == nil, "string builder should not IO error")
  85. assert(strings.builder_cap(out) == out_length, "buffer resized, `decoded_len` was wrong")
  86. return out.buf[:], nil
  87. }
  88. decode_into :: proc(w: io.Writer, data: string, DEC_TBL := DEC_TABLE) -> io.Error {
  89. length := decoded_len(data)
  90. if length == 0 {
  91. return nil
  92. }
  93. c0, c1, c2, c3: int
  94. b0, b1, b2: int
  95. buf: [3]byte
  96. i, j: int
  97. for ; j + 3 <= length; i, j = i + 4, j + 3 {
  98. #no_bounds_check {
  99. c0 = DEC_TBL[data[i]]
  100. c1 = DEC_TBL[data[i + 1]]
  101. c2 = DEC_TBL[data[i + 2]]
  102. c3 = DEC_TBL[data[i + 3]]
  103. b0 = (c0 << 2) | (c1 >> 4)
  104. b1 = (c1 << 4) | (c2 >> 2)
  105. b2 = (c2 << 6) | c3
  106. buf[0] = byte(b0)
  107. buf[1] = byte(b1)
  108. buf[2] = byte(b2)
  109. }
  110. io.write_full(w, buf[:]) or_return
  111. }
  112. rest := length - j
  113. if rest > 0 {
  114. #no_bounds_check {
  115. c0 = DEC_TBL[data[i]]
  116. c1 = DEC_TBL[data[i + 1]]
  117. c2 = DEC_TBL[data[i + 2]]
  118. b0 = (c0 << 2) | (c1 >> 4)
  119. b1 = (c1 << 4) | (c2 >> 2)
  120. }
  121. switch rest {
  122. case 1: io.write_byte(w, byte(b0)) or_return
  123. case 2: io.write_full(w, {byte(b0), byte(b1)}) or_return
  124. }
  125. }
  126. return nil
  127. }
  128. decoded_len :: proc(data: string) -> int {
  129. length := len(data)
  130. if length == 0 {
  131. return 0
  132. }
  133. padding: int
  134. if data[length - 1] == PADDING {
  135. if length > 1 && data[length - 2] == PADDING {
  136. padding = 2
  137. } else {
  138. padding = 1
  139. }
  140. }
  141. return ((length * 6) >> 3) - padding
  142. }