|
| 1 | +use std::ops::{BitAnd, BitOr, Shl, Shr}; |
| 2 | + |
| 3 | +const PAD_BYTE: u8 = b'='; |
| 4 | +const ENCODE_TABLE: &[u8] = |
| 5 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".as_bytes(); |
| 6 | +const LOW_SIX_BITS: u32 = 0x3F; |
| 7 | + |
| 8 | +pub fn encode(input: &[u8]) -> String { |
| 9 | + let rem = input.len() % 3; |
| 10 | + let complete_chunks = input.len() / 3; |
| 11 | + let remainder_chunk = if rem == 0 { 0 } else { 1 }; |
| 12 | + let encoded_size = (complete_chunks + remainder_chunk) * 4; |
| 13 | + |
| 14 | + let mut output = vec![0; encoded_size]; |
| 15 | + |
| 16 | + // complete chunks first |
| 17 | + let complete_chunk_len = input.len() - rem; |
| 18 | + |
| 19 | + let mut input_index = 0_usize; |
| 20 | + let mut output_index = 0_usize; |
| 21 | + while input_index < complete_chunk_len { |
| 22 | + let chunk = &input[input_index..input_index + 3]; |
| 23 | + |
| 24 | + // populate low 24 bits from 3 bytes |
| 25 | + let chunk_int: u32 = |
| 26 | + (chunk[0] as u32).shl(16) | (chunk[1] as u32).shl(8) | (chunk[2] as u32); |
| 27 | + // encode 4x 6-bit output bytes |
| 28 | + output[output_index] = ENCODE_TABLE[chunk_int.shr(18) as usize]; |
| 29 | + output[output_index + 1] = ENCODE_TABLE[chunk_int.shr(12_u8).bitand(LOW_SIX_BITS) as usize]; |
| 30 | + output[output_index + 2] = ENCODE_TABLE[chunk_int.shr(6_u8).bitand(LOW_SIX_BITS) as usize]; |
| 31 | + output[output_index + 3] = ENCODE_TABLE[chunk_int.bitand(LOW_SIX_BITS) as usize]; |
| 32 | + |
| 33 | + input_index += 3; |
| 34 | + output_index += 4; |
| 35 | + } |
| 36 | + |
| 37 | + // then leftovers |
| 38 | + if rem == 2 { |
| 39 | + let chunk = &input[input_index..input_index + 2]; |
| 40 | + |
| 41 | + // high six bits of chunk[0] |
| 42 | + output[output_index] = ENCODE_TABLE[chunk[0].shr(2) as usize]; |
| 43 | + // bottom 2 bits of [0], high 4 bits of [1] |
| 44 | + output[output_index + 1] = ENCODE_TABLE |
| 45 | + [(chunk[0].shl(4_u8).bitor(chunk[1].shr(4_u8)) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 46 | + // bottom 4 bits of [1], with the 2 bottom bits as zero |
| 47 | + output[output_index + 2] = |
| 48 | + ENCODE_TABLE[(chunk[1].shl(2_u8) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 49 | + output[output_index + 3] = PAD_BYTE; |
| 50 | + } else if rem == 1 { |
| 51 | + let byte = input[input_index]; |
| 52 | + output[output_index] = ENCODE_TABLE[byte.shr(2) as usize]; |
| 53 | + output[output_index + 1] = |
| 54 | + ENCODE_TABLE[(byte.shl(4_u8) as u32).bitand(LOW_SIX_BITS) as usize]; |
| 55 | + output[output_index + 2] = PAD_BYTE; |
| 56 | + output[output_index + 3] = PAD_BYTE; |
| 57 | + } |
| 58 | + String::from_utf8(output).expect("Invalid UTF8") |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + fn compare_encode(expected: &str, target: &[u8]) { |
| 64 | + assert_eq!(expected, super::encode(target)); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn encode_rfc4648_0() { |
| 69 | + compare_encode("", b""); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn encode_rfc4648_1() { |
| 74 | + compare_encode("Zg==", b"f"); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn encode_rfc4648_2() { |
| 79 | + compare_encode("Zm8=", b"fo"); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn encode_rfc4648_3() { |
| 84 | + compare_encode("Zm9v", b"foo"); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn encode_rfc4648_4() { |
| 89 | + compare_encode("Zm9vYg==", b"foob"); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn encode_rfc4648_5() { |
| 94 | + compare_encode("Zm9vYmE=", b"fooba"); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn encode_rfc4648_6() { |
| 99 | + compare_encode("Zm9vYmFy", b"foobar"); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn encode_all_ascii() { |
| 104 | + let mut ascii = Vec::<u8>::with_capacity(128); |
| 105 | + |
| 106 | + for i in 0..128 { |
| 107 | + ascii.push(i); |
| 108 | + } |
| 109 | + |
| 110 | + compare_encode( |
| 111 | + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\ |
| 112 | + D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8\ |
| 113 | + =", |
| 114 | + &ascii, |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn encode_all_bytes() { |
| 120 | + let mut bytes = Vec::<u8>::with_capacity(256); |
| 121 | + |
| 122 | + for i in 0..255 { |
| 123 | + bytes.push(i); |
| 124 | + } |
| 125 | + bytes.push(255); //bug with "overflowing" ranges? |
| 126 | + |
| 127 | + compare_encode( |
| 128 | + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\ |
| 129 | + D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn\ |
| 130 | + +AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6\ |
| 131 | + /wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==", |
| 132 | + &bytes, |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
0 commit comments