From 3a77e55d2c8eab1bc3aea5dfe4da6bd6d8f9e7f0 Mon Sep 17 00:00:00 2001 From: edef Date: Fri, 27 Oct 2023 17:06:29 +0000 Subject: refactor(nix-compat/nixbase32): clean up encode Change-Id: I63da5bd47cd9033e1cc13fbe4b4864514a5343cb Reviewed-on: https://cl.tvl.fyi/c/depot/+/9865 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/nixbase32.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/tvix/nix-compat/src/nixbase32.rs b/tvix/nix-compat/src/nixbase32.rs index febc6fe598ac..9aed20054602 100644 --- a/tvix/nix-compat/src/nixbase32.rs +++ b/tvix/nix-compat/src/nixbase32.rs @@ -29,24 +29,23 @@ pub fn encode(input: &[u8]) -> String { let output_len = encode_len(input.len()); let mut output = String::with_capacity(output_len); - if output_len > 0 { - for n in (0..=output_len - 1).rev() { - let b = n * 5; // bit offset within the entire input - let i = b / 8; // input byte index - let j = b % 8; // bit offset within that input byte - - let mut c = input[i] >> j; - if i + 1 < input.len() { - // we want to right shift, and discard shifted out bits (unchecked) - // To do this without panicing, we need to do the shifting in u16 - // and convert back to u8 afterwards. - c |= ((input[i + 1] as u16) << (8 - j as u16)) as u8 + for n in (0..output_len).rev() { + let b = n * 5; // bit offset within the entire input + let i = b / 8; // input byte index + let j = b % 8; // bit offset within that input byte + + // 5-bit words aren't aligned to bytes + // we can only read byte-aligned units + // read 16 bits then shift and mask to 5 + let c = { + let mut word = input[i] as u16; + if let Some(&msb) = input.get(i + 1) { + word |= (msb as u16) << 8; } + (word >> j) & 0x1f + }; - output - .write_char(ALPHABET[(c & 0x1f) as usize] as char) - .unwrap(); - } + output.write_char(ALPHABET[c as usize] as char).unwrap(); } output -- cgit 1.4.1