2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-22 21:20:28 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module base64
|
|
|
|
|
|
|
|
const (
|
2021-01-22 23:29:05 +03:00
|
|
|
index = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
|
|
|
|
56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29,
|
|
|
|
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]!
|
2021-01-14 05:51:13 +03:00
|
|
|
ending_table = [0, 2, 1]!
|
2021-01-22 23:29:05 +03:00
|
|
|
enc_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2021-01-22 23:29:05 +03:00
|
|
|
// decode decodes the base64 encoded `string` value passed in `data`.
|
|
|
|
// Please note: If you need to decode many strings repeatedly, take a look at `decode_in_buffer`.
|
|
|
|
// Example: assert base64.decode('ViBpbiBiYXNlIDY0') == 'V in base 64'
|
2021-02-26 09:22:12 +03:00
|
|
|
pub fn decode(data string) []byte {
|
|
|
|
size := data.len * 3 / 4
|
2021-03-24 01:01:29 +03:00
|
|
|
if size <= 0 || data.len % 4 != 0 {
|
2021-02-26 09:22:12 +03:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
buffer := malloc(size)
|
|
|
|
n := decode_in_buffer(data, buffer)
|
2021-03-13 18:20:46 +03:00
|
|
|
return buffer.vbytes(n)
|
2021-02-26 09:22:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode_str is the string variant of decode
|
|
|
|
pub fn decode_str(data string) string {
|
2019-12-16 21:29:32 +03:00
|
|
|
size := data.len * 3 / 4
|
2021-03-24 01:01:29 +03:00
|
|
|
if size <= 0 || data.len % 4 != 0 {
|
2019-12-16 21:29:32 +03:00
|
|
|
return ''
|
|
|
|
}
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
buffer := malloc(size)
|
|
|
|
return tos(buffer, decode_in_buffer(data, buffer))
|
|
|
|
}
|
2019-10-26 18:20:36 +03:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:22:12 +03:00
|
|
|
// encode encodes the `[]byte` value passed in `data` to base64.
|
2021-01-22 23:29:05 +03:00
|
|
|
// Please note: base64 encoding returns a `string` that is ~ 4/3 larger than the input.
|
|
|
|
// Please note: If you need to encode many strings repeatedly, take a look at `encode_in_buffer`.
|
|
|
|
// Example: assert base64.encode('V in base 64') == 'ViBpbiBiYXNlIDY0'
|
2021-02-26 09:22:12 +03:00
|
|
|
pub fn encode(data []byte) string {
|
|
|
|
return alloc_and_encode(data.data, data.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode_str is the string variant of encode
|
|
|
|
pub fn encode_str(data string) string {
|
|
|
|
return alloc_and_encode(data.str, data.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
// alloc_and_encode is a private function that allocates and encodes data into a string
|
|
|
|
// Used by encode and encode_str
|
|
|
|
fn alloc_and_encode(src byteptr, len int) string {
|
|
|
|
size := 4 * ((len + 2) / 3)
|
2019-12-16 21:29:32 +03:00
|
|
|
if size <= 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
buffer := malloc(size)
|
2021-02-26 09:22:12 +03:00
|
|
|
return tos(buffer, encode_from_buffer(buffer, src, len))
|
2021-02-14 21:31:42 +03:00
|
|
|
}
|
2019-10-26 18:20:36 +03:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:22:12 +03:00
|
|
|
// url_decode returns a decoded URL `string` version of
|
2021-01-22 23:29:05 +03:00
|
|
|
// the a base64 url encoded `string` passed in `data`.
|
2021-02-26 09:22:12 +03:00
|
|
|
pub fn url_decode(data string) []byte {
|
|
|
|
mut result := data.replace_each(['-', '+', '_', '/'])
|
2021-03-13 18:20:46 +03:00
|
|
|
match result.len % 4 {
|
|
|
|
// Pad with trailing '='s
|
2021-01-22 23:29:05 +03:00
|
|
|
2 { result += '==' } // 2 pad chars
|
|
|
|
3 { result += '=' } // 1 pad char
|
|
|
|
else {} // no padding
|
2020-10-15 17:04:06 +03:00
|
|
|
}
|
2021-02-26 09:22:12 +03:00
|
|
|
return decode(result)
|
2020-10-15 17:04:06 +03:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:22:12 +03:00
|
|
|
// url_decode_str is the string variant of url_decode
|
|
|
|
pub fn url_decode_str(data string) string {
|
|
|
|
mut result := data.replace_each(['-', '+', '_', '/'])
|
2021-03-13 18:20:46 +03:00
|
|
|
match result.len % 4 {
|
|
|
|
// Pad with trailing '='s
|
2021-02-26 09:22:12 +03:00
|
|
|
2 { result += '==' } // 2 pad chars
|
|
|
|
3 { result += '=' } // 1 pad char
|
|
|
|
else {} // no padding
|
|
|
|
}
|
|
|
|
return decode_str(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// url_encode returns a base64 URL encoded `string` version
|
2021-01-22 23:29:05 +03:00
|
|
|
// of the value passed in `data`.
|
2021-02-26 09:22:12 +03:00
|
|
|
pub fn url_encode(data []byte) string {
|
|
|
|
return encode(data).replace_each(['+', '-', '/', '_', '=', ''])
|
|
|
|
}
|
|
|
|
|
|
|
|
// url_encode_str is the string variant of url_encode
|
|
|
|
pub fn url_encode_str(data string) string {
|
|
|
|
return encode_str(data).replace_each(['+', '-', '/', '_', '=', ''])
|
2020-10-15 17:04:06 +03:00
|
|
|
}
|
|
|
|
|
2021-01-22 23:29:05 +03:00
|
|
|
// decode_in_buffer decodes the base64 encoded `string` reference passed in `data` into `buffer`.
|
|
|
|
// decode_in_buffer returns the size of the decoded data in the buffer.
|
|
|
|
// Please note: The `buffer` should be large enough (i.e. 3/4 of the data.len, or larger)
|
|
|
|
// to hold the decoded data.
|
|
|
|
// Please note: This function does NOT allocate new memory, and is thus suitable for handling very large strings.
|
2019-12-06 23:02:09 +03:00
|
|
|
pub fn decode_in_buffer(data &string, buffer byteptr) int {
|
2019-06-27 21:06:00 +03:00
|
|
|
mut padding := 0
|
|
|
|
if data.ends_with('=') {
|
|
|
|
if data.ends_with('==') {
|
|
|
|
padding = 2
|
|
|
|
} else {
|
|
|
|
padding = 1
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-01-22 23:29:05 +03:00
|
|
|
// input_length is the length of meaningful data
|
2019-06-27 21:06:00 +03:00
|
|
|
input_length := data.len - padding
|
|
|
|
output_length := input_length * 3 / 4
|
|
|
|
|
|
|
|
mut i := 0
|
2019-06-22 21:20:28 +03:00
|
|
|
mut j := 0
|
2019-10-26 18:20:36 +03:00
|
|
|
mut b := &byte(0)
|
|
|
|
mut d := &byte(0)
|
2021-01-22 23:29:05 +03:00
|
|
|
unsafe {
|
2019-10-26 18:20:36 +03:00
|
|
|
d = byteptr(data.str)
|
|
|
|
b = byteptr(buffer)
|
|
|
|
}
|
2019-06-27 21:06:00 +03:00
|
|
|
for i < input_length {
|
|
|
|
mut char_a := 0
|
|
|
|
mut char_b := 0
|
|
|
|
mut char_c := 0
|
|
|
|
mut char_d := 0
|
|
|
|
if i < input_length {
|
2021-03-13 18:20:46 +03:00
|
|
|
char_a = base64.index[unsafe { d[i] }]
|
2019-06-27 21:06:00 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i < input_length {
|
2021-03-13 18:20:46 +03:00
|
|
|
char_b = base64.index[unsafe { d[i] }]
|
2019-06-27 21:06:00 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i < input_length {
|
2021-03-13 18:20:46 +03:00
|
|
|
char_c = base64.index[unsafe { d[i] }]
|
2019-06-27 21:06:00 +03:00
|
|
|
i++
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 21:06:00 +03:00
|
|
|
if i < input_length {
|
2021-03-13 18:20:46 +03:00
|
|
|
char_d = base64.index[unsafe { d[i] }]
|
2019-06-27 21:06:00 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded_bytes := (char_a << 18) | (char_b << 12) | (char_c << 6) | (char_d << 0)
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
2021-01-22 23:29:05 +03:00
|
|
|
b[j] = byte(decoded_bytes >> 16)
|
|
|
|
b[j + 1] = byte((decoded_bytes >> 8) & 0xff)
|
|
|
|
b[j + 2] = byte((decoded_bytes >> 0) & 0xff)
|
2020-07-22 20:28:53 +03:00
|
|
|
}
|
2019-06-27 21:06:00 +03:00
|
|
|
j += 3
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-26 18:20:36 +03:00
|
|
|
return output_length
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:22:12 +03:00
|
|
|
// encode_in_buffer base64 encodes the `[]byte` passed in `data` into `buffer`.
|
2021-01-22 23:29:05 +03:00
|
|
|
// encode_in_buffer returns the size of the encoded data in the buffer.
|
|
|
|
// Please note: The buffer should be large enough (i.e. 4/3 of the data.len, or larger) to hold the encoded data.
|
|
|
|
// Please note: The function does NOT allocate new memory, and is suitable for handling very large strings.
|
2021-02-26 09:22:12 +03:00
|
|
|
pub fn encode_in_buffer(data []byte, buffer byteptr) int {
|
|
|
|
return encode_from_buffer(buffer, data.data, data.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode_from_buffer will perform encoding from any type of src buffer
|
|
|
|
// and write the bytes into `dest`.
|
|
|
|
// Please note: The `dest` buffer should be large enough (i.e. 4/3 of the src_len, or larger) to hold the encoded data.
|
|
|
|
// Please note: This function is for internal base64 encoding
|
|
|
|
fn encode_from_buffer(dest byteptr, src byteptr, src_len int) int {
|
|
|
|
input_length := src_len
|
2019-06-25 20:37:18 +03:00
|
|
|
output_length := 4 * ((input_length + 2) / 3)
|
|
|
|
|
|
|
|
mut i := 0
|
|
|
|
mut j := 0
|
2019-10-26 18:20:36 +03:00
|
|
|
|
2021-02-26 09:22:12 +03:00
|
|
|
mut d := src
|
|
|
|
mut b := dest
|
2021-03-13 18:20:46 +03:00
|
|
|
mut etable := byteptr(base64.enc_table.str)
|
2019-06-25 20:37:18 +03:00
|
|
|
for i < input_length {
|
|
|
|
mut octet_a := 0
|
|
|
|
mut octet_b := 0
|
|
|
|
mut octet_c := 0
|
|
|
|
|
|
|
|
if i < input_length {
|
2021-01-22 23:29:05 +03:00
|
|
|
octet_a = int(unsafe { d[i] })
|
2019-06-25 20:37:18 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i < input_length {
|
2021-01-22 23:29:05 +03:00
|
|
|
octet_b = int(unsafe { d[i] })
|
2019-06-25 20:37:18 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i < input_length {
|
2021-01-22 23:29:05 +03:00
|
|
|
octet_c = int(unsafe { d[i] })
|
2019-06-25 20:37:18 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:58:32 +03:00
|
|
|
triple := ((octet_a << 0x10) + (octet_b << 0x08) + octet_c)
|
2019-06-25 20:37:18 +03:00
|
|
|
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
2021-01-22 23:29:05 +03:00
|
|
|
b[j] = etable[(triple >> 3 * 6) & 63] // 63 is 0x3F
|
|
|
|
b[j + 1] = etable[(triple >> 2 * 6) & 63]
|
|
|
|
b[j + 2] = etable[(triple >> 1 * 6) & 63]
|
|
|
|
b[j + 3] = etable[(triple >> 0 * 6) & 63]
|
2020-07-22 20:28:53 +03:00
|
|
|
}
|
2019-06-25 20:37:18 +03:00
|
|
|
j += 4
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:20:46 +03:00
|
|
|
padding_length := base64.ending_table[input_length % 3]
|
2019-10-26 18:20:36 +03:00
|
|
|
for i = 0; i < padding_length; i++ {
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
|
|
|
b[output_length - 1 - i] = `=`
|
|
|
|
}
|
2019-06-25 20:37:18 +03:00
|
|
|
}
|
2019-10-26 18:20:36 +03:00
|
|
|
return output_length
|
2019-06-25 20:37:18 +03:00
|
|
|
}
|