encoding.base64: change base64 function signatures to use `[]byte` (#8970)

pull/8980/head
Miccah 2021-02-26 00:22:12 -06:00 committed by GitHub
parent c03798e390
commit d81b6e7805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 41 deletions

View File

@ -16,7 +16,20 @@ const (
// decode decodes the base64 encoded `string` value passed in `data`. // 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`. // 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' // Example: assert base64.decode('ViBpbiBiYXNlIDY0') == 'V in base 64'
pub fn decode(data string) string { pub fn decode(data string) []byte {
size := data.len * 3 / 4
if size <= 0 {
return []
}
unsafe {
buffer := malloc(size)
n := decode_in_buffer(data, buffer)
return array{element_size: 1, data: buffer, len: n, cap: size}
}
}
// decode_str is the string variant of decode
pub fn decode_str(data string) string {
size := data.len * 3 / 4 size := data.len * 3 / 4
if size <= 0 { if size <= 0 {
return '' return ''
@ -27,41 +40,64 @@ pub fn decode(data string) string {
} }
} }
// encode encodes the `string` value passed in `data` to base64. // encode encodes the `[]byte` value passed in `data` to base64.
// Please note: base64 encoding returns a `string` that is ~ 4/3 larger than the input. // 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`. // 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' // Example: assert base64.encode('V in base 64') == 'ViBpbiBiYXNlIDY0'
pub fn encode(data string) string { pub fn encode(data []byte) string {
size := 4 * ((data.len + 2) / 3) 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)
if size <= 0 { if size <= 0 {
return '' return ''
} }
unsafe { unsafe {
buffer := malloc(size) buffer := malloc(size)
return tos(buffer, encode_in_buffer(data, buffer)) return tos(buffer, encode_from_buffer(buffer, src, len))
} }
} }
// decode_url returns a decoded URL `string` version of // url_decode returns a decoded URL `string` version of
// the a base64 url encoded `string` passed in `data`. // the a base64 url encoded `string` passed in `data`.
pub fn decode_url(data string) string { pub fn url_decode(data string) []byte {
mut result := data.replace('-', '+') // 62nd char of encoding mut result := data.replace_each(['-', '+', '_', '/'])
result = data.replace('_', '/') // 63rd char of encoding
match result.len % 4 { // Pad with trailing '='s match result.len % 4 { // Pad with trailing '='s
2 { result += '==' } // 2 pad chars 2 { result += '==' } // 2 pad chars
3 { result += '=' } // 1 pad char 3 { result += '=' } // 1 pad char
else {} // no padding else {} // no padding
} }
return decode(data) return decode(result)
} }
// encode_url returns a base64 URL encoded `string` version // url_decode_str is the string variant of url_decode
pub fn url_decode_str(data string) string {
mut result := data.replace_each(['-', '+', '_', '/'])
match result.len % 4 { // Pad with trailing '='s
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
// of the value passed in `data`. // of the value passed in `data`.
pub fn encode_url(data string) string { pub fn url_encode(data []byte) string {
mut result := encode(data) return encode(data).replace_each(['+', '-', '/', '_', '=', ''])
// 62nd char of encoding, 63rd char of encoding, remove any trailing '='s }
result = result.replace_each(['+', '-', '/', '_', '=', ''])
return result // url_encode_str is the string variant of url_encode
pub fn url_encode_str(data string) string {
return encode_str(data).replace_each(['+', '-', '/', '_', '=', ''])
} }
// decode_in_buffer decodes the base64 encoded `string` reference passed in `data` into `buffer`. // decode_in_buffer decodes the base64 encoded `string` reference passed in `data` into `buffer`.
@ -123,25 +159,28 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int {
return output_length return output_length
} }
// encode_in_buffer base64 encodes the `string` reference passed in `data` into `buffer`. // encode_in_buffer base64 encodes the `[]byte` passed in `data` into `buffer`.
// encode_in_buffer returns the size of the encoded data in the buffer. // 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 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. // Please note: The function does NOT allocate new memory, and is suitable for handling very large strings.
pub fn encode_in_buffer(data &string, buffer byteptr) int { pub fn encode_in_buffer(data []byte, buffer byteptr) int {
input_length := data.len 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
output_length := 4 * ((input_length + 2) / 3) output_length := 4 * ((input_length + 2) / 3)
mut i := 0 mut i := 0
mut j := 0 mut j := 0
mut d := byteptr(0) mut d := src
mut b := byteptr(0) mut b := dest
mut etable := byteptr(0) mut etable := byteptr(enc_table.str)
unsafe {
d = data.str
b = buffer
etable = enc_table.str
}
for i < input_length { for i < input_length {
mut octet_a := 0 mut octet_a := 0
mut octet_b := 0 mut octet_b := 0

View File

@ -4,7 +4,7 @@ fn test_long_encoding() {
repeats := 1000 repeats := 1000
input_size := 3000 input_size := 3000
s_original := 'a'.repeat(input_size) s_original := []byte{len: input_size, init: `a`}
s_encoded := base64.encode(s_original) s_encoded := base64.encode(s_original)
s_decoded := base64.decode(s_encoded) s_decoded := base64.decode(s_encoded)

View File

@ -40,15 +40,32 @@ const (
) )
fn test_decode() { fn test_decode() {
assert base64.decode(man_pair.encoded) == man_pair.decoded assert base64.decode(man_pair.encoded) == man_pair.decoded.bytes()
// Test for incorrect padding. // Test for incorrect padding.
assert base64.decode('aGk') == 'hi' assert base64.decode('aGk') == 'hi'.bytes()
assert base64.decode('aGk=') == 'hi' assert base64.decode('aGk=') == 'hi'.bytes()
assert base64.decode('aGk==') == 'hi' assert base64.decode('aGk==') == 'hi'.bytes()
for i, p in pairs { for i, p in pairs {
got := base64.decode(p.encoded) got := base64.decode(p.encoded)
if got != p.decoded.bytes() {
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
assert false
}
}
}
fn test_decode_str() {
assert base64.decode_str(man_pair.encoded) == man_pair.decoded
// Test for incorrect padding.
assert base64.decode_str('aGk') == 'hi'
assert base64.decode_str('aGk=') == 'hi'
assert base64.decode_str('aGk==') == 'hi'
for i, p in pairs {
got := base64.decode_str(p.encoded)
if got != p.decoded { if got != p.decoded {
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}') eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
assert false assert false
@ -57,10 +74,10 @@ fn test_decode() {
} }
fn test_encode() { fn test_encode() {
assert base64.encode(man_pair.decoded) == man_pair.encoded assert base64.encode(man_pair.decoded.bytes()) == man_pair.encoded
for i, p in pairs { for i, p in pairs {
got := base64.encode(p.decoded) got := base64.encode(p.decoded.bytes())
if got != p.encoded { if got != p.encoded {
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}') eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
assert false assert false
@ -68,12 +85,54 @@ fn test_encode() {
} }
} }
fn test_encode_url() { fn test_encode_str() {
test := base64.encode_url('Hello Base64Url encoding!') assert base64.encode_str(man_pair.decoded) == man_pair.encoded
for i, p in pairs {
got := base64.encode_str(p.decoded)
if got != p.encoded {
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
assert false
}
}
}
fn test_url_encode() {
test := base64.url_encode('Hello Base64Url encoding!'.bytes())
assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ' assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
} }
fn test_decode_url() { fn test_url_encode_str() {
test := base64.decode_url("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ") test := base64.url_encode_str('Hello Base64Url encoding!')
assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
}
fn test_url_decode() {
test := base64.url_decode("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ")
assert test == 'Hello Base64Url encoding!'.bytes()
}
fn test_url_decode_str() {
test := base64.url_decode_str("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ")
assert test == 'Hello Base64Url encoding!' assert test == 'Hello Base64Url encoding!'
} }
fn test_encode_null_byte() {
assert base64.encode([byte(`A`) 0 `C`]) == 'QQBD'
}
fn test_encode_null_byte_str() {
// While this works, bytestr() does a memcpy
s := [byte(`A`) 0 `C`].bytestr()
assert base64.encode_str(s) == 'QQBD'
}
fn test_decode_null_byte() {
assert base64.decode('QQBD') == [byte(`A`) 0 `C`]
}
fn test_decode_null_byte_str() {
// While this works, bytestr() does a memcpy
s := [byte(`A`) 0 `C`].bytestr()
assert base64.decode_str('QQBD') == s
}

View File

@ -149,7 +149,7 @@ fn (mut c Client) send_auth() ? {
sb.write_b(0) sb.write_b(0)
sb.write_string(c.password) sb.write_string(c.password)
a := sb.str() a := sb.str()
auth := 'AUTH PLAIN ${base64.encode(a)}\r\n' auth := 'AUTH PLAIN ${base64.encode_str(a)}\r\n'
c.send_str(auth) ? c.send_str(auth) ?
c.expect_reply(.auth_ok) ? c.expect_reply(.auth_ok) ?
} }

View File

@ -7,7 +7,7 @@ import strings
// handshake manages the websocket handshake process // handshake manages the websocket handshake process
fn (mut ws Client) handshake() ? { fn (mut ws Client) handshake() ? {
nonce := get_nonce(ws.nonce_size) nonce := get_nonce(ws.nonce_size)
seckey := base64.encode(nonce) seckey := base64.encode_str(nonce)
mut sb := strings.new_builder(1024) mut sb := strings.new_builder(1024)
defer { defer {
unsafe { sb.free() } unsafe { sb.free() }

View File

@ -35,7 +35,7 @@ fn create_key_challenge_response(seckey string) ?string {
sha1buf := seckey + guid sha1buf := seckey + guid
shabytes := sha1buf.bytes() shabytes := sha1buf.bytes()
hash := sha1.sum(shabytes) hash := sha1.sum(shabytes)
b64 := base64.encode(unsafe { tos(hash.data, hash.len) }) b64 := base64.encode(hash)
unsafe { unsafe {
hash.free() hash.free()
shabytes.free() shabytes.free()