fn decode_with_tables(
    encoding_table: &[u64],
    decoding_table: &[Code],
    mut input: &[u8],
    input_bit_count: i32,
    run_length_code: u32,
    expected_output_size: usize,
) -> Result<Vec<u16>>
{
    let mut output = Vec::with_capacity(expected_output_size);
    let mut code_bits = 0_u64;
    let mut code_bit_count = 0_u64;

    while input.len() > 0 {
        read_byte(&mut code_bits, &mut code_bit_count, &mut input)?;

        // Access decoding table
        while code_bit_count >= DECODE_BITS {
            let code_index = (code_bits >> (code_bit_count - DECODE_BITS)) & DECODE_MASK;
            let code = &decoding_table[u64_to_usize(code_index)];

            // Get short code
            if let Code::Short(code) = code {
                code_bit_count -= code.len();

                read_code_into_vec(
                    code.value,
                    run_length_code,
                    &mut code_bits,
                    &mut code_bit_count,
                    &mut input,
                    &mut output,
                    expected_output_size,
                )?;
            }
            else if let Code::Long(ref long_codes) = code {
                debug_assert_ne!(long_codes.len(), 0);

                let long_code = long_codes.iter()
                    .filter_map(|&long_code|{
                        let encoded_long_code = encoding_table[u32_to_usize(long_code)];
                        let length = length(encoded_long_code);

                        while code_bit_count < length && input.len() > 0 {
                            let err = read_byte(&mut code_bits, &mut code_bit_count, &mut input);
                            if let Err(err) = err { return Some(Err(err)); }
                        }

                        if code_bit_count >= length {
                            let required_code = (code_bits >> (code_bit_count - length)) & ((1 << length) - 1);

                            if self::code(encoded_long_code) == required_code {
                                code_bit_count -= length;
                                return Some(Ok(long_code));
                            }
                        }

                        None

                    })
                    .next()
                    .ok_or(Error::invalid(INVALID_CODE))?;

                read_code_into_vec(
                    long_code?,
                    run_length_code,
                    &mut code_bits,
                    &mut code_bit_count,
                    &mut input,
                    &mut output,
                    expected_output_size,
                )?;
            }
            else {
                return Err(Error::invalid(INVALID_CODE));
            }
        }
    }

    let count = u64::try_from((8 - input_bit_count) & 7)?;
    code_bits >>= count;
    code_bit_count -= count;

    while code_bit_count > 0 {
        let index = (code_bits << (DECODE_BITS - code_bit_count)) & DECODE_MASK;
        let code = &decoding_table[u64_to_usize(index)];

        if let Code::Short(short_code) = code {
            if short_code.len() > code_bit_count { return Err(Error::invalid("code")) }; // FIXME why does this happen??
            code_bit_count -= short_code.len(); // FIXME may throw "attempted to subtract with overflow"

            read_code_into_vec(
                short_code.value,
                run_length_code,
                &mut code_bits,
                &mut code_bit_count,
                &mut input,
                &mut output,
                expected_output_size,
            )?;
        }
        else {
            return Err(Error::invalid(INVALID_CODE));
        }
    }

    if output.len() != expected_output_size {
        return Err(Error::invalid(NOT_ENOUGH_DATA));
    }

    Ok(output)
}
/// Decode (uncompress) n bits based on encoding & decoding tables:
Decode (uncompress) n bits based on encoding & decoding tables.
Decode ( uncompress ) n bits based on encoding & decoding tables .
fn decode_with_tables(
    encoding_table: &[u64],
    decoding_table: &[Code],
    mut input: &[u8],
    input_bit_count: i32,
    run_length_code: u32,
    expected_output_size: usize,
) -> Result<Vec<u16>>
{
    let mut output = Vec::with_capacity(expected_output_size);
    let mut code_bits = 0_u64;
    let mut code_bit_count = 0_u64;
    while input.len() > 0 {
        read_byte(&mut code_bits, &mut code_bit_count, &mut input)?;
        // Access decoding table
        while code_bit_count >= DECODE_BITS {
            let code_index = (code_bits >> (code_bit_count - DECODE_BITS)) & DECODE_MASK;
            let code = &decoding_table[u64_to_usize(code_index)];
            // Get short code
            if let Code::Short(code) = code {
                code_bit_count -= code.len();
                read_code_into_vec(
                    code.value,
                    run_length_code,
                    &mut code_bits,
                    &mut code_bit_count,
                    &mut input,
                    &mut output,
                    expected_output_size,
                )?;
            }
            else if let Code::Long(ref long_codes) = code {
                debug_assert_ne!(long_codes.len(), 0);
                let long_code = long_codes.iter()
                    .filter_map(|&long_code|{
                        let encoded_long_code = encoding_table[u32_to_usize(long_code)];
                        let length = length(encoded_long_code);
                        while code_bit_count < length && input.len() > 0 {
                            let err = read_byte(&mut code_bits, &mut code_bit_count, &mut input);
                            if let Err(err) = err { return Some(Err(err)); }
                        }
                        if code_bit_count >= length {
                            let required_code = (code_bits >> (code_bit_count - length)) & ((1 << length) - 1);
                            if self::code(encoded_long_code) == required_code {
                                code_bit_count -= length;
                                return Some(Ok(long_code));
                            }
                        }
                        None
                    })
                    .next()
                    .ok_or(Error::invalid(INVALID_CODE))?;
                read_code_into_vec(
                    long_code?,
                    run_length_code,
                    &mut code_bits,
                    &mut code_bit_count,
                    &mut input,
                    &mut output,
                    expected_output_size,
                )?;
            }
            else {
                return Err(Error::invalid(INVALID_CODE));
            }
        }
    }
    let count = u64::try_from((8 - input_bit_count) & 7)?;
    code_bits >>= count;
    code_bit_count -= count;
    while code_bit_count > 0 {
        let index = (code_bits << (DECODE_BITS - code_bit_count)) & DECODE_MASK;
        let code = &decoding_table[u64_to_usize(index)];
        if let Code::Short(short_code) = code {
            if short_code.len() > code_bit_count { return Err(Error::invalid("code")) }; // FIXME why does this happen??
            code_bit_count -= short_code.len(); // FIXME may throw "attempted to subtract with overflow"
            read_code_into_vec(
                short_code.value,
                run_length_code,
                &mut code_bits,
                &mut code_bit_count,
                &mut input,
                &mut output,
                expected_output_size,
            )?;
        }
        else {
            return Err(Error::invalid(INVALID_CODE));
        }
    }
    if output.len() != expected_output_size {
        return Err(Error::invalid(NOT_ENOUGH_DATA));
    }
    Ok(output)
}
fn decode_with_tables ( encoding_table : & [ u64 ] , decoding_table : & [ Code ] , mut input : & [ u8 ] , input_bit_count : i32 , run_length_code : u32 , expected_output_size : usize , ) -> Result < Vec < u16 > > { let mut output = Vec :: with_capacity ( expected_output_size ) ; let mut code_bits = 0_u64 ; let mut code_bit_count = 0_u64 ; while input . len ( ) > 0 { read_byte ( & mut code_bits , & mut code_bit_count , & mut input ) ? ; // Access decoding table while code_bit_count >= DECODE_BITS { let code_index = ( code_bits >> ( code_bit_count - DECODE_BITS ) ) & DECODE_MASK ; let code = & decoding_table [ u64_to_usize ( code_index ) ] ; // Get short code if let Code :: Short ( code ) = code { code_bit_count -= code . len ( ) ; read_code_into_vec ( code . value , run_length_code , & mut code_bits , & mut code_bit_count , & mut input , & mut output , expected_output_size , ) ? ; } else if let Code :: Long ( ref long_codes ) = code { debug_assert_ne ! ( long_codes len ( ) 0 ) ; let long_code = long_codes . iter ( ) . filter_map ( | & long_code | { let encoded_long_code = encoding_table [ u32_to_usize ( long_code ) ] ; let length = length ( encoded_long_code ) ; while code_bit_count < length && input . len ( ) > 0 { let err = read_byte ( & mut code_bits , & mut code_bit_count , & mut input ) ; if let Err ( err ) = err { return Some ( Err ( err ) ) ; } } if code_bit_count >= length { let required_code = ( code_bits >> ( code_bit_count - length ) ) & ( ( 1 << length ) - 1 ) ; if self :: code ( encoded_long_code ) == required_code { code_bit_count -= length ; return Some ( Ok ( long_code ) ) ; } } None } ) . next ( ) . ok_or ( Error :: invalid ( INVALID_CODE ) ) ? ; read_code_into_vec ( long_code ? , run_length_code , & mut code_bits , & mut code_bit_count , & mut input , & mut output , expected_output_size , ) ? ; } else { return Err ( Error :: invalid ( INVALID_CODE ) ) ; } } } let count = u64 :: try_from ( ( 8 - input_bit_count ) & 7 ) ? ; code_bits >>= count ; code_bit_count -= count ; while code_bit_count > 0 { let index = ( code_bits << ( DECODE_BITS - code_bit_count ) ) & DECODE_MASK ; let code = & decoding_table [ u64_to_usize ( index ) ] ; if let Code :: Short ( short_code ) = code { if short_code . len ( ) > code_bit_count { return Err ( Error :: invalid ( " " ) ) } ; // FIXME why does this happen?? code_bit_count -= short_code . len ( ) ; // FIXME may throw "attempted to subtract with overflow" read_code_into_vec ( short_code . value , run_length_code , & mut code_bits , & mut code_bit_count , & mut input , & mut output , expected_output_size , ) ? ; } else { return Err ( Error :: invalid ( INVALID_CODE ) ) ; } } if output . len ( ) != expected_output_size { return Err ( Error :: invalid ( NOT_ENOUGH_DATA ) ) ; } Ok ( output ) }
Decode (uncompress) n bits based on encoding & decoding tables:
Decode ( uncompress ) n bits based on encoding & decoding tables :
encoding_table
&[u64]
decoding_table
&[Code]
input
&[u8]
input_bit_count
i32
run_length_code
u32
expected_output_size
usize
encoding_table
&[u64]
decoding_table
&[Code]
input
&[u8]
input_bit_count
i32
run_length_code
u32
expected_output_size
usize