]>
Commit | Line | Data |
---|---|---|
217acde9 XL |
1 | //! Base64 encoding support. |
2 | use cvt_n; | |
3 | use error::ErrorStack; | |
4 | use ffi; | |
5 | use libc::c_int; | |
6 | ||
7 | /// Encodes a slice of bytes to a base64 string. | |
8 | /// | |
9 | /// This corresponds to [`EVP_EncodeBlock`]. | |
10 | /// | |
11 | /// # Panics | |
12 | /// | |
13 | /// Panics if the input length or computed output length overflow a signed C integer. | |
14 | /// | |
15 | /// [`EVP_EncodeBlock`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_DecodeBlock.html | |
16 | pub fn encode_block(src: &[u8]) -> String { | |
17 | assert!(src.len() <= c_int::max_value() as usize); | |
18 | let src_len = src.len() as c_int; | |
19 | ||
20 | let len = encoded_len(src_len).unwrap(); | |
21 | let mut out = Vec::with_capacity(len as usize); | |
22 | ||
23 | // SAFETY: `encoded_len` ensures space for 4 output characters | |
24 | // for every 3 input bytes including padding and nul terminator. | |
25 | // `EVP_EncodeBlock` will write only single byte ASCII characters. | |
26 | // `EVP_EncodeBlock` will only write to not read from `out`. | |
27 | unsafe { | |
28 | let out_len = ffi::EVP_EncodeBlock(out.as_mut_ptr(), src.as_ptr(), src_len); | |
29 | out.set_len(out_len as usize); | |
30 | String::from_utf8_unchecked(out) | |
31 | } | |
32 | } | |
33 | ||
34 | /// Decodes a base64-encoded string to bytes. | |
35 | /// | |
36 | /// This corresponds to [`EVP_DecodeBlock`]. | |
37 | /// | |
38 | /// # Panics | |
39 | /// | |
40 | /// Panics if the input length or computed output length overflow a signed C integer. | |
41 | /// | |
42 | /// [`EVP_DecodeBlock`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_DecodeBlock.html | |
43 | pub fn decode_block(src: &str) -> Result<Vec<u8>, ErrorStack> { | |
44 | let src = src.trim(); | |
45 | ||
46 | assert!(src.len() <= c_int::max_value() as usize); | |
47 | let src_len = src.len() as c_int; | |
48 | ||
49 | let len = decoded_len(src_len).unwrap(); | |
50 | let mut out = Vec::with_capacity(len as usize); | |
51 | ||
52 | // SAFETY: `decoded_len` ensures space for 3 output bytes | |
53 | // for every 4 input characters including padding. | |
54 | // `EVP_DecodeBlock` can write fewer bytes after stripping | |
55 | // leading and trailing whitespace, but never more. | |
56 | // `EVP_DecodeBlock` will only write to not read from `out`. | |
57 | unsafe { | |
58 | let out_len = cvt_n(ffi::EVP_DecodeBlock( | |
59 | out.as_mut_ptr(), | |
60 | src.as_ptr(), | |
61 | src_len, | |
62 | ))?; | |
63 | out.set_len(out_len as usize); | |
64 | } | |
65 | ||
f5bb8b5f | 66 | if src.ends_with('=') { |
217acde9 XL |
67 | out.pop(); |
68 | if src.ends_with("==") { | |
69 | out.pop(); | |
70 | } | |
71 | } | |
72 | ||
73 | Ok(out) | |
74 | } | |
75 | ||
76 | fn encoded_len(src_len: c_int) -> Option<c_int> { | |
77 | let mut len = (src_len / 3).checked_mul(4)?; | |
78 | ||
79 | if src_len % 3 != 0 { | |
80 | len = len.checked_add(4)?; | |
81 | } | |
82 | ||
83 | len = len.checked_add(1)?; | |
84 | ||
85 | Some(len) | |
86 | } | |
87 | ||
88 | fn decoded_len(src_len: c_int) -> Option<c_int> { | |
89 | let mut len = (src_len / 4).checked_mul(3)?; | |
90 | ||
91 | if src_len % 4 != 0 { | |
92 | len = len.checked_add(3)?; | |
93 | } | |
94 | ||
95 | Some(len) | |
96 | } | |
97 | ||
98 | #[cfg(test)] | |
99 | mod tests { | |
100 | use super::*; | |
101 | ||
102 | #[test] | |
103 | fn test_encode_block() { | |
104 | assert_eq!("".to_string(), encode_block(b"")); | |
105 | assert_eq!("Zg==".to_string(), encode_block(b"f")); | |
106 | assert_eq!("Zm8=".to_string(), encode_block(b"fo")); | |
107 | assert_eq!("Zm9v".to_string(), encode_block(b"foo")); | |
108 | assert_eq!("Zm9vYg==".to_string(), encode_block(b"foob")); | |
109 | assert_eq!("Zm9vYmE=".to_string(), encode_block(b"fooba")); | |
110 | assert_eq!("Zm9vYmFy".to_string(), encode_block(b"foobar")); | |
111 | } | |
112 | ||
113 | #[test] | |
114 | fn test_decode_block() { | |
115 | assert_eq!(b"".to_vec(), decode_block("").unwrap()); | |
116 | assert_eq!(b"f".to_vec(), decode_block("Zg==").unwrap()); | |
117 | assert_eq!(b"fo".to_vec(), decode_block("Zm8=").unwrap()); | |
118 | assert_eq!(b"foo".to_vec(), decode_block("Zm9v").unwrap()); | |
119 | assert_eq!(b"foob".to_vec(), decode_block("Zm9vYg==").unwrap()); | |
120 | assert_eq!(b"fooba".to_vec(), decode_block("Zm9vYmE=").unwrap()); | |
121 | assert_eq!(b"foobar".to_vec(), decode_block("Zm9vYmFy").unwrap()); | |
122 | } | |
123 | ||
124 | #[test] | |
125 | fn test_strip_whitespace() { | |
126 | assert_eq!(b"foobar".to_vec(), decode_block(" Zm9vYmFy\n").unwrap()); | |
127 | assert_eq!(b"foob".to_vec(), decode_block(" Zm9vYg==\n").unwrap()); | |
128 | } | |
129 | } |