mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
submodules
This commit is contained in:

committed by
Alexander Medvednikov

parent
36908fa304
commit
8a2d25247f
112
vlib/encoding/base64/base64.v
Normal file
112
vlib/encoding/base64/base64.v
Normal file
@ -0,0 +1,112 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
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]
|
||||
)
|
||||
|
||||
pub fn decode(data string) string {
|
||||
mut padding := 0
|
||||
if data.ends_with('=') {
|
||||
if data.ends_with('==') {
|
||||
padding = 2
|
||||
} else {
|
||||
padding = 1
|
||||
}
|
||||
}
|
||||
//input_length is the length of meaningful data
|
||||
input_length := data.len - padding
|
||||
output_length := input_length * 3 / 4
|
||||
|
||||
mut i := 0
|
||||
mut j := 0
|
||||
mut str := malloc(output_length)
|
||||
|
||||
for i < input_length {
|
||||
mut char_a := 0
|
||||
mut char_b := 0
|
||||
mut char_c := 0
|
||||
mut char_d := 0
|
||||
|
||||
if i < input_length {
|
||||
char_a = Index[int(data[i])]
|
||||
i++
|
||||
}
|
||||
if i < input_length {
|
||||
char_b = Index[int(data[i])]
|
||||
i++
|
||||
}
|
||||
if i < input_length {
|
||||
char_c = Index[int(data[i])]
|
||||
i++
|
||||
}
|
||||
if i < input_length {
|
||||
char_d = Index[int(data[i])]
|
||||
i++
|
||||
}
|
||||
|
||||
decoded_bytes := (char_a << 18) | (char_b << 12) | (char_c << 6) | (char_d << 0)
|
||||
str[j] = decoded_bytes >> 16
|
||||
str[j+1] = (decoded_bytes >> 8) & 0xff
|
||||
str[j+2] = (decoded_bytes >> 0) & 0xff
|
||||
|
||||
j += 3
|
||||
}
|
||||
return tos(str, output_length)
|
||||
}
|
||||
|
||||
const (
|
||||
EncodingTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
)
|
||||
|
||||
pub fn encode(data string) string {
|
||||
input_length := data.len
|
||||
output_length := 4 * ((input_length + 2) / 3)
|
||||
|
||||
mut i := 0
|
||||
mut j := 0
|
||||
mut str := malloc(output_length)
|
||||
|
||||
for i < input_length {
|
||||
mut octet_a := 0
|
||||
mut octet_b := 0
|
||||
mut octet_c := 0
|
||||
|
||||
if i < input_length {
|
||||
octet_a = int(data[i])
|
||||
i++
|
||||
}
|
||||
if i < input_length {
|
||||
octet_b = int(data[i])
|
||||
i++
|
||||
}
|
||||
if i < input_length {
|
||||
octet_c = int(data[i])
|
||||
i++
|
||||
}
|
||||
|
||||
triple := ((octet_a << 0x10) + (octet_b << 0x08) + octet_c)
|
||||
|
||||
str[j+0] = EncodingTable[(triple >> 3 * 6) & 63] // 63 is 0x3F
|
||||
str[j+1] = EncodingTable[(triple >> 2 * 6) & 63]
|
||||
str[j+2] = EncodingTable[(triple >> 1 * 6) & 63]
|
||||
str[j+3] = EncodingTable[(triple >> 0 * 6) & 63]
|
||||
j += 4
|
||||
}
|
||||
|
||||
mod_table := [0, 2, 1]
|
||||
for i = 0; i < mod_table[input_length % 3]; i++ {
|
||||
str[output_length - 1 - i] = `=`
|
||||
}
|
||||
|
||||
return tos(str, output_length)
|
||||
}
|
65
vlib/encoding/base64/base64_test.v
Normal file
65
vlib/encoding/base64/base64_test.v
Normal file
@ -0,0 +1,65 @@
|
||||
import encoding.base64
|
||||
|
||||
struct testpair {
|
||||
decoded string
|
||||
encoded string
|
||||
}
|
||||
|
||||
const (
|
||||
pairs = [
|
||||
// RFC 3548 examples
|
||||
testpair{'\x14\xfb\x9c\x03\xd9\x7e', 'FPucA9l+'},
|
||||
testpair{'\x14\xfb\x9c\x03\xd9', 'FPucA9k='},
|
||||
testpair{'\x14\xfb\x9c\x03', 'FPucAw=='},
|
||||
|
||||
// RFC 4648 examples
|
||||
testpair{'', ''},
|
||||
testpair{'f', 'Zg=='},
|
||||
testpair{'fo', 'Zm8='},
|
||||
testpair{'foo', 'Zm9v'},
|
||||
testpair{'foob', 'Zm9vYg=='},
|
||||
testpair{'fooba', 'Zm9vYmE='},
|
||||
testpair{'foobar', 'Zm9vYmFy'},
|
||||
|
||||
// Wikipedia examples
|
||||
testpair{'sure.', 'c3VyZS4='},
|
||||
testpair{'sure', 'c3VyZQ=='},
|
||||
testpair{'sur', 'c3Vy'},
|
||||
testpair{'su', 'c3U='},
|
||||
testpair{'leasure.', 'bGVhc3VyZS4='},
|
||||
testpair{'easure.', 'ZWFzdXJlLg=='},
|
||||
testpair{'asure.', 'YXN1cmUu'},
|
||||
testpair{'sure.', 'c3VyZS4='},
|
||||
]
|
||||
)
|
||||
|
||||
fn test_decode() {
|
||||
assert base64.decode('TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=')
|
||||
== 'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'
|
||||
|
||||
// Test for incorrect padding.
|
||||
assert base64.decode('aGk') == 'hi'
|
||||
assert base64.decode('aGk=') == 'hi'
|
||||
assert base64.decode('aGk==') == 'hi'
|
||||
|
||||
for i, p in pairs {
|
||||
got := base64.decode(p.encoded)
|
||||
if got != p.decoded {
|
||||
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
|
||||
assert false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode() {
|
||||
assert base64.encode('Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.')
|
||||
== 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='
|
||||
|
||||
for i, p in pairs {
|
||||
got := base64.encode(p.decoded)
|
||||
if got != p.encoded {
|
||||
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
|
||||
assert false
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user