bitwrap
Intro
bitwrap is a derive macro and interface to declare a struct data member with explicit size, in bits.
Example
bits attribute accept only one argument - field size in bits.
For example packet has next format:
| Field | Bits |
|---|---|
| f1 | 1 |
| f2 | 1 |
| f3 | 2 |
| f4 | 4 |
| f5 | 16 |
use *;
const DATA: & = &;
let mut packet = default;
packet.unpack;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let mut buffer: = Vecnew;
buffer.resize;
let result = packet.pack;
assert_eq!;
Nested objects
Nested field is an object with BitWrap interface.
For example part of IPv4 packet:
| Field | Bits |
|---|---|
| ttl | 8 |
| protocol | 8 |
| checksum | 16 |
| src | 32 |
| dst | 32 |
use Ipv4Addr;
use *;
const DATA: & = &;
let mut packet = IP4 ;
packet.unpack;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let mut buffer: = Vecnew;
buffer.resize;
let result = packet.pack;
assert_eq!;
Skip bits
Some packets contains reserved or fixed bits.
This bits could be skiped with bits_skip attribute.
For example packet has next format:
| Field | Bits |
|---|---|
| Data | 6 |
| 0b00 | 2 |
| 0b1111 | 4 |
| Data | 4 |
bits_skip attribute accept next arguments:
- size in bits
- value. optional argument. by the default: 0
use *;
const DATA: & = &;
let mut packet = default;
packet.unpack;
assert_eq!;
assert_eq!;
let mut buffer: = Vecnew;
buffer.resize;
let result = packet.pack;
assert_eq!;