1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 08:34:07 +02:00
Reading and writing data types of arbitrary bit length that might not be byte-aligned
  • Rust 99.8%
  • Nix 0.2%
Find a file
2020-01-19 22:09:57 +01:00
benches struct bench 2020-01-07 20:30:49 +01:00
bitstream_reader_derive update syn and friends to 1.0 2020-01-08 12:49:19 +01:00
src harden against dos with crafted input by limiting reserved vec/map size 2020-01-19 22:09:57 +01:00
tests add unchecked reading to BitRead and BitReadSized 2020-01-07 22:31:29 +01:00
.gitignore dont save pointer to data 2019-07-24 12:32:18 +02:00
.travis.yml safe 2019-12-25 23:59:43 +01:00
Cargo.toml remove seperate BitSize traits 2020-01-07 21:59:05 +01:00
LICENSE-APACHE switch licence to apache/mit 2019-02-22 22:47:56 +01:00
LICENSE-MIT switch licence to apache/mit 2019-02-22 22:47:56 +01:00
README.md readme typo 2020-01-07 23:15:12 +01:00
README.tpl update readme 2019-02-28 20:49:56 +01:00

Crates.io Documentation Dependency status Build Status

bitstream_reader

Tools for reading data types of arbitrary bit length and might not be byte-aligned in the source data

The main way of handling with the binary data is to first create a BitBuffer ,wrap it into a BitStream and then read from the stream.

If performance is critical, working directly on the BitBuffer can be faster.

Once you have a BitStream, there are 2 different approaches of reading data

  • read primitives, Strings and byte arrays, using read_bool, read_int, read_float, read_byes and read_string
  • read any type implementing the BitRead or BitReadSized traits using read and read_sized
    • BitRead is for types that can be read without requiring any size info (e.g. null-terminated strings, floats, whole integers, etc)
    • BitReadSized is for types that require external sizing information to be read (fixed length strings, arbitrary length integers

The BitRead and BitReadSized traits can be used with #[derive] if all fields implement BitRead or BitReadSized.

Examples

use bitstream_reader::{BitBuffer, LittleEndian, BitStream, BitRead};

#[derive(BitRead)]
struct ComplexType {
    first: u8,
    #[size = 15]
    second: u16,
    third: bool,
}

fn main() {
    let bytes = vec![
        0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
        0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
    ];
    let buffer = BitBuffer::new(bytes, LittleEndian);
    let mut stream = BitStream::new(buffer);
    let value: u8 = stream.read_int(7)?;
    let complex: ComplexType = stream.read()?;
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.