-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.rs
88 lines (80 loc) · 2.7 KB
/
base64.rs
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2023 The rust-ggstd authors. All rights reserved.
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
use ggstd::encoding::base64;
use std::io::Write;
fn main() {
example();
example_encoding_encode_to_string();
example_encoding_encode();
example_encoding_decode_string();
example_encoding_decode();
example_new_encoder();
}
fn example() {
let msg = "Hello, 世界";
let encoded = base64::get_std_encoding().encode_to_string(msg.as_bytes());
println!("{}", encoded);
let (decoded, err) = base64::get_std_encoding().decode_string(&encoded);
if err.is_some() {
println!("decode error: {:?}", err);
return;
}
println!("{}", String::from_utf8_lossy(&decoded));
// Output:
// SGVsbG8sIOS4lueVjA==
// Hello, 世界
}
fn example_encoding_encode_to_string() {
let data = "any + old & data".as_bytes();
let str = base64::get_std_encoding().encode_to_string(data);
println!("{}", str)
// Output:
// YW55ICsgb2xkICYgZGF0YQ==
}
fn example_encoding_encode() {
let data = "Hello, world!".as_bytes();
let mut dst = vec![0; base64::get_std_encoding().encoded_len(data.len())];
base64::get_std_encoding().encode(&mut dst, data);
println!("{}", String::from_utf8_lossy(&dst));
// Output:
// SGVsbG8sIHdvcmxkIQ==
}
fn example_encoding_decode_string() {
let str = "c29tZSBkYXRhIHdpdGggACBhbmQg77u/";
let (data, err) = base64::get_std_encoding().decode_string(str);
if err.is_some() {
println!("error: {:?}", err);
return;
}
println!("{}", ggstd::strconv::quote_bytes_string(&data));
// Output:
// "some data with \x00 and \ufeff"
}
fn example_encoding_decode() {
let str = "SGVsbG8sIHdvcmxkIQ==";
let mut dst = vec![0; base64::get_std_encoding().decoded_len(str.len())];
let (n, err) = base64::get_std_encoding().decode(&mut dst, str.as_bytes());
if err.is_some() {
println!("decode error: {:?}", err);
return;
}
let dst = &dst[..n];
println!("\"{}\"", String::from_utf8(dst.to_vec()).unwrap());
// Output:
// "Hello, world!"
}
fn example_new_encoder() {
let input = b"foo\x00bar";
let stdout = std::io::stdout();
let mut stdout_writer = stdout.lock();
let mut encoder = base64::Encoder::new(base64::get_std_encoding(), &mut stdout_writer);
encoder.write_all(input).unwrap();
// Must close the encoder when finished to flush any partial blocks.
// If you comment out the following line, the last partial block "r"
// won't be encoded.
encoder.close().unwrap();
// Output:
// Zm9vAGJhcg==
}