Skip to content

LSB support in the bits module #808

@onkoe

Description

@onkoe

Please complete the following tasks

winnow version

0.7.11

Describe your use case

I'm writing a small parser for WebP. However, WebP uses LSB for all its values. This means that the byte ordering in winnow::binary::bits will be incorrect here:

        // skip `rsv`, `I`, and `L` bits.
        //
        // then, check the `E` (Exif) and `X` (XMP) presence bits and consume
        // the rest of the input.
        let (_, has_exif, has_xmp): ((), bool, bool) = bits((
            bits::take::<_, usize, _, EmptyError>(4_usize).void(),
            bits::bool,
            bits::bool,
        ))
        .parse(file_info_bytes) // consume the rest of the header, too
        .unwrap_or_else(|_| unreachable!("..."));

where the file info is mentioned here.

To fix this, I have to reverse the bytes, which feels pretty weird.

Describe the solution you'd like

Either a bits_le/bits_be, bits::<E: EndiannessMarker, ...>, or bits(Endianness, ...).

Then, I could simply replace bits with bits_le in the code above.

A special Bytes wrapper would also be acceptable, but its implementation would require a reverse iterator or fancy reverse slicing, and I'm not sure how it'd feel in sub-sliced inputs.

Alternatives, if applicable

You can manually reverse the bytes, but it doesn't feel great.

Additional Context

You can find Google's WebP Container spec at this link: https://developers.google.com/speed/webp/docs/riff_container

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-combinatorArea: combinatorsC-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions