|
| 1 | +// A minimal base64 implementation to keep from pulling in a crate for just that. It's based on |
| 2 | +// https://github.com/marshallpierce/rust-base64 but without all the customization options. |
| 3 | +// The biggest portion comes from |
| 4 | +// https://github.com/marshallpierce/rust-base64/blob/a675443d327e175f735a37f574de803d6a332591/src/engine/naive.rs#L42 |
| 5 | +// Thanks, rust-base64! |
| 6 | + |
| 7 | +// The MIT License (MIT) |
| 8 | + |
| 9 | +// Copyright (c) 2015 Alice Maz |
| 10 | + |
| 11 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +// of this software and associated documentation files (the "Software"), to deal |
| 13 | +// in the Software without restriction, including without limitation the rights |
| 14 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +// copies of the Software, and to permit persons to whom the Software is |
| 16 | +// furnished to do so, subject to the following conditions: |
| 17 | + |
| 18 | +// The above copyright notice and this permission notice shall be included in |
| 19 | +// all copies or substantial portions of the Software. |
| 20 | + |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +// THE SOFTWARE. |
| 28 | + |
| 29 | +use std::ops::{BitAnd, BitOr, Shl, Shr}; |
| 30 | + |
| 31 | +const PAD_BYTE: u8 = b'='; |
| 32 | +const ENCODE_TABLE: &[u8] = |
| 33 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".as_bytes(); |
| 34 | +const LOW_SIX_BITS: u32 = 0x3F; |
| 35 | + |
| 36 | +pub fn encode(input: &[u8]) -> String { |
| 37 | + let rem = input.len() % 3; |
| 38 | + let complete_chunks = input.len() / 3; |
| 39 | + let remainder_chunk = if rem == 0 { 0 } else { 1 }; |
| 40 | + let encoded_size = (complete_chunks + remainder_chunk) * 4; |
| 41 | + |
| 42 | + let mut output = vec![0; encoded_size]; |
| 43 | + |
| 44 | + // complete chunks first |
| 45 | + let complete_chunk_len = input.len() - rem; |
| 46 | + |
| 47 | + let mut input_index = 0_usize; |
| 48 | + let mut output_index = 0_usize; |
| 49 | + while input_index < complete_chunk_len { |
| 50 | + let chunk = &input[input_index..input_index + 3]; |
| 51 | + |
| 52 | + // populate low 24 bits from 3 bytes |
| 53 | + let chunk_int: u32 = |
| 54 | + (chunk[0] as u32).shl(16) | (chunk[1] as u32).shl(8) | (chunk[2] as u32); |
| 55 | + // encode 4x 6-bit output bytes |
| 56 | + output[output_index] = ENCODE_TABLE[chunk_int.shr(18) as usize]; |
| 57 | + output[output_index + 1] = ENCODE_TABLE[chunk_int.shr(12_u8).bitand(LOW_SIX_BITS) as usize]; |
| 58 | + output[output_index + 2] = ENCODE_TABLE[chunk_int.shr(6_u8).bitand(LOW_SIX_BITS) as usize]; |
| 59 | + output[output_index + 3] = ENCODE_TABLE[chunk_int.bitand(LOW_SIX_BITS) as usize]; |
| 60 | + |
| 61 | + input_index += 3; |
| 62 | + output_index += 4; |
| 63 | + } |
| 64 | + |
| 65 | + // then leftovers |
| 66 | + if rem == 2 { |
| 67 | + let chunk = &input[input_index..input_index + 2]; |
| 68 | + |
| 69 | + // high six bits of chunk[0] |
| 70 | + output[output_index] = ENCODE_TABLE[chunk[0].shr(2) as usize]; |
| 71 | + // bottom 2 bits of [0], high 4 bits of [1] |
| 72 | + output[output_index + 1] = ENCODE_TABLE |
| 73 | + [(chunk[0].shl(4_u8).bitor(chunk[1].shr(4_u8)) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 74 | + // bottom 4 bits of [1], with the 2 bottom bits as zero |
| 75 | + output[output_index + 2] = |
| 76 | + ENCODE_TABLE[(chunk[1].shl(2_u8) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 77 | + output[output_index + 3] = PAD_BYTE; |
| 78 | + } else if rem == 1 { |
| 79 | + let byte = input[input_index]; |
| 80 | + output[output_index] = ENCODE_TABLE[byte.shr(2) as usize]; |
| 81 | + output[output_index + 1] = |
| 82 | + ENCODE_TABLE[(byte.shl(4_u8) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 83 | + output[output_index + 2] = PAD_BYTE; |
| 84 | + output[output_index + 3] = PAD_BYTE; |
| 85 | + } |
| 86 | + String::from_utf8(output).expect("Invalid UTF8") |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + fn compare_encode(expected: &str, target: &[u8]) { |
| 92 | + assert_eq!(expected, super::encode(target)); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn encode_rfc4648_0() { |
| 97 | + compare_encode("", b""); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn encode_rfc4648_1() { |
| 102 | + compare_encode("Zg==", b"f"); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn encode_rfc4648_2() { |
| 107 | + compare_encode("Zm8=", b"fo"); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn encode_rfc4648_3() { |
| 112 | + compare_encode("Zm9v", b"foo"); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn encode_rfc4648_4() { |
| 117 | + compare_encode("Zm9vYg==", b"foob"); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn encode_rfc4648_5() { |
| 122 | + compare_encode("Zm9vYmE=", b"fooba"); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn encode_rfc4648_6() { |
| 127 | + compare_encode("Zm9vYmFy", b"foobar"); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn encode_all_ascii() { |
| 132 | + let mut ascii = Vec::<u8>::with_capacity(128); |
| 133 | + |
| 134 | + for i in 0..128 { |
| 135 | + ascii.push(i); |
| 136 | + } |
| 137 | + |
| 138 | + compare_encode( |
| 139 | + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\ |
| 140 | + D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8\ |
| 141 | + =", |
| 142 | + &ascii, |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn encode_all_bytes() { |
| 148 | + let mut bytes = Vec::<u8>::with_capacity(256); |
| 149 | + |
| 150 | + for i in 0..255 { |
| 151 | + bytes.push(i); |
| 152 | + } |
| 153 | + bytes.push(255); //bug with "overflowing" ranges? |
| 154 | + |
| 155 | + compare_encode( |
| 156 | + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\ |
| 157 | + D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn\ |
| 158 | + +AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6\ |
| 159 | + /wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==", |
| 160 | + &bytes, |
| 161 | + ); |
| 162 | + } |
| 163 | +} |
0 commit comments