1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/encoding/base64/base64.v

67 lines
2.6 KiB
V
Raw Normal View History

2023-03-28 23:55:57 +03:00
// Copyright (c) 2019-2023 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.
// Based off: https://github.com/golang/go/blob/master/src/encoding/base64/base64.go
// Last commit: https://github.com/golang/go/commit/9a93baf4d7d13d7d5c67388c93960d78abc8e11e
2019-06-22 21:20:28 +03:00
module base64
const (
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]!
ending_table = [0, 2, 1]!
enc_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
2019-06-22 21:20:28 +03:00
)
// url_decode returns a decoded URL `string` version of
// the a base64 url encoded `string` passed in `data`.
2022-04-15 15:35:35 +03:00
pub fn url_decode(data string) []u8 {
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
2020-10-15 17:04:06 +03:00
}
return decode(result)
2020-10-15 17:04:06 +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(['-', '+', '_', '/'])
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`.
2022-04-15 15:35:35 +03:00
pub fn url_encode(data []u8) 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
}
// assemble64 assembles 8 base64 digits into 6 bytes.
// Each digit comes from the decode map.
// Please note: Invalid base64 digits are not expected and not handled.
2022-04-15 18:25:45 +03:00
fn assemble64(n1 u8, n2 u8, n3 u8, n4 u8, n5 u8, n6 u8, n7 u8, n8 u8) u64 {
return u64(n1) << 58 | u64(n2) << 52 | u64(n3) << 46 | u64(n4) << 40 | u64(n5) << 34 | u64(n6) << 28 | u64(n7) << 22 | u64(n8) << 16
}
// assemble32 assembles 4 base64 digits into 3 bytes.
// Each digit comes from the decode map.
// Please note: Invalid base64 digits are not expected and not handled.
2022-04-15 18:25:45 +03:00
fn assemble32(n1 u8, n2 u8, n3 u8, n4 u8) u32 {
return u32(n1) << 26 | u32(n2) << 20 | u32(n3) << 14 | u32(n4) << 8
}