macro_toolset/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Base64 related macros

pub use base64::*;

#[macro_export]
/// Encode given buffer into base64 string.
///
/// # Example:
/// ```
/// use macro_toolset::{b64_encode, base64::engine::general_purpose};
///
/// // Default padding: STANDARD, returns `String`.
/// # let example =
/// b64_encode!(b"hello world");
/// # assert_eq!(example, "aGVsbG8gd29ybGQ=");
/// // To given `String`.
/// let mut string = String::new();
/// b64_encode!(b"hello world" => &mut string);
/// # assert_eq!(string, "aGVsbG8gd29ybGQ=");
///
/// // Returns `String`.
/// // Available padding: STANDARD / STANDARD_NO_PAD / URL_SAFE / URL_SAFE_NO_PAD
/// // No need to import!
/// # let example =
/// b64_encode!(URL_SAFE_NO_PAD: b"hello world");
/// # assert_eq!(example, "aGVsbG8gd29ybGQ");
/// // To given `String`.
/// let mut string = String::new();
/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => STRING: &mut string);
/// # assert_eq!(string, "aGVsbG8gd29ybGQ");
///
/// // Returns `bytes::BytesMut`.
/// # let example =
/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => BYTES);
/// # assert_eq!(&example[..], b"aGVsbG8gd29ybGQ");
/// // To given `bytes::BytesMut`.
/// let mut string = bytes::BytesMut::new();
/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => BYTES: &mut string);
/// # assert_eq!(&string[..], b"aGVsbG8gd29ybGQ");
/// ```
macro_rules! b64_encode {
    ($data:expr) => {
        $crate::b64_encode!(STANDARD: $data)
    };
    ($data:expr => $string:expr) => {
        $crate::b64_encode!(STANDARD: $data => STRING: $string)
    };
    (STANDARD: $($tt:tt)+) => {
        $crate::b64_encode!($crate::base64::engine::general_purpose::STANDARD, $($tt)+)
    };
    (STANDARD_NO_PAD: $($tt:tt)+) => {
        $crate::b64_encode!($crate::base64::engine::general_purpose::STANDARD_NO_PAD, $($tt)+)
    };
    (URL_SAFE: $($tt:tt)+) => {
        $crate::b64_encode!($crate::base64::engine::general_purpose::URL_SAFE, $($tt)+)
    };
    (URL_SAFE_NO_PAD: $($tt:tt)+) => {
        $crate::b64_encode!($crate::base64::engine::general_purpose::URL_SAFE_NO_PAD, $($tt)+)
    };
    ($padding:path, $data:expr) => {{
        let mut string = String::with_capacity($data.len() * 4 / 3 + 4 + 64);

        $crate::base64::Engine::encode_string(&$padding, $data, &mut string);

        string
    }};
    ($padding:path, $data:expr => STRING: $string:expr) => {
        $crate::base64::Engine::encode_string(&$padding, $data, $string)
    };
    ($padding:path, $data:expr => BYTES) => {{
        let target_len = $data.len() * 4 / 3 + 4 + 64;

        // Allocate buffer
        let mut bytes_buf = bytes::BytesMut::with_capacity(target_len);
        // `target_len` is the exact length of the base64 string.
        #[allow(unsafe_code)]
        unsafe {
            bytes_buf.set_len(target_len)
        };

        let bytes_written =
            $crate::base64::Engine::encode_slice(&$padding, $data, bytes_buf.as_mut()).unwrap_or(0);

        bytes_buf.truncate(bytes_written);
        bytes_buf
    }};
    ($padding:path, $data:expr => BYTES: $bytes_buf:expr) => {{
        let target_len = $data.len() * 4 / 3 + 4 + 64;

        // Allocate buffer
        $bytes_buf.reserve(target_len);
        // `target_len` is the exact length of the base64 string.
        #[allow(unsafe_code)]
        unsafe {
            $bytes_buf.set_len(target_len)
        };

        let bytes_written =
            $crate::base64::Engine::encode_slice(&$padding, $data, $bytes_buf).unwrap_or(0);

        $bytes_buf.truncate(bytes_written);
    }};
}

#[macro_export]
/// Decode given base64 string to bytes.
///
/// # Example:
/// ```
/// use macro_toolset::{b64_decode, base64::engine::general_purpose};
/// // Default padding: STANDARD, returns `Vec<u8>`.
/// # let example =
/// b64_decode!("aGVsbG8gd29ybGQ=")  
/// # .unwrap();
/// # assert_eq!(example, b"hello world");
/// // Available padding: STANDARD / STANDARD_NO_PAD / URL_SAFE / URL_SAFE_NO_PAD.
/// // No need to import!
/// # let example =
/// b64_decode!(URL_SAFE_NO_PAD: "aGVsbG8gd29ybGQ")
/// # .unwrap();
/// # assert_eq!(example, b"hello world");
/// // To given `Vec<u8>`
/// # let mut example = Vec::new();
/// b64_decode!(URL_SAFE_NO_PAD: "aGVsbG8gd29ybGQ", &mut example);
/// # assert_eq!(&example, b"hello world");
/// // Though not recommended, we support the full path as well.
/// # let example =
/// b64_decode!(general_purpose::URL_SAFE_NO_PAD, "aGVsbG8gd29ybGQ")
/// # .unwrap();
/// # assert_eq!(example, b"hello world");
/// ```
macro_rules! b64_decode {
    ($data:expr) => {
        $crate::b64_decode!(STANDARD: $data)
    };
    (STANDARD: $($tt:tt)+) => {
        $crate::b64_decode!($crate::base64::engine::general_purpose::STANDARD, $($tt)+)
    };
    (STANDARD_NO_PAD: $($tt:tt)+) => {
        $crate::b64_decode!($crate::base64::engine::general_purpose::STANDARD_NO_PAD, $($tt)+)
    };
    (URL_SAFE: $($tt:tt)+) => {
        $crate::b64_decode!($crate::base64::engine::general_purpose::URL_SAFE, $($tt)+)
    };
    (URL_SAFE_NO_PAD: $($tt:tt)+) => {
        $crate::b64_decode!($crate::base64::engine::general_purpose::URL_SAFE_NO_PAD, $($tt)+)
    };
    ($padding:path, $data:expr) => {
        $crate::base64::Engine::decode(&$padding, $data)
    };
    ($padding:path, $data:expr, $buf:expr) => {
        $crate::base64::Engine::decode_vec(&$padding, $data, $buf)
    };
}

// ============ Deprecated ================

#[deprecated(since = "0.8.0-rc.5", note = "use `b64_encode!` instead")]
#[macro_export]
/// Base64 encode with [`bytes::Bytes`] returned
macro_rules! b64_encode_bytes {
    ($data:expr) => {
        $crate::b64_encode_bytes!($data, $crate::base64::engine::general_purpose::STANDARD)
    };
    ($data:expr, $padding:path) => {{
        let data = $data.as_ref();
        let target_len = data.len() * 4 / 3 + 4;
        let mut bytes_buf = bytes::BytesMut::with_capacity(target_len + 64);
        #[allow(unsafe_code)]
        // Safety: `target_len` is the exact length of the base64 string.
        unsafe {
            bytes_buf.set_len(target_len)
        };
        let bytes_written =
            $crate::base64::Engine::encode_slice(&$padding, $data, bytes_buf.as_mut()).unwrap_or(0);
        bytes_buf.truncate(bytes_written);
        bytes_buf.freeze()
    }};
}