silk.encoding.endian.

Read and write 16-, 32- and 64-bit integers as little-endian or big-endian bytes in a slice.

When to use#

Use these functions to read words from a byte buffer in a fixed byte order, for example the message words of a hash block, or to write the fields of a binary format.

Details#

Each call checks the bounds once and then moves the whole word, so a loop over a buffer compiles to word loads and stores, and a big-endian access to one byte swap. The offset needs no alignment. Nothing allocates.

Gotchas#

A call traps when the word does not fit in the slice at offset. Check the length first when the input comes from outside.

Examples#

Read and write words in both byte orders#

silk
import silk.encoding.endian

pub fn main() -> i32 {
  let mut bytes: [u8; 8] = [0; 8]
  endian.writeU32BigEndian(&mut bytes, 0, 0x01020304)
  endian.writeU32LittleEndian(&mut bytes, 4, 0x01020304)
  if bytes[0] != 1 || bytes[3] != 4 || bytes[4] != 4 || bytes[7] != 1 {
    return 1
  }
  if endian.readU16BigEndian(&bytes, 1) != 0x0203 {
    return 2
  }
  if endian.readU64LittleEndian(&bytes, 0) != 0x0102030404030201 {
    return 3
  }
  return 0
}

Import as endian with import silk.encoding.endian as endian.

Public declarations: 12.

readU16LittleEndian#

silk
pub fn readU16LittleEndian(bytes: &[u8], offset: usize) -> u16

Returns the little-endian u16 in the 2 bytes of bytes at offset.

Gotchas#

Traps unless offset + 2 is at most bytes.length.

readU16BigEndian#

silk
pub fn readU16BigEndian(bytes: &[u8], offset: usize) -> u16

Returns the big-endian u16 in the 2 bytes of bytes at offset.

Gotchas#

Traps unless offset + 2 is at most bytes.length.

readU32LittleEndian#

silk
pub fn readU32LittleEndian(bytes: &[u8], offset: usize) -> u32

Returns the little-endian u32 in the 4 bytes of bytes at offset.

Gotchas#

Traps unless offset + 4 is at most bytes.length.

readU32BigEndian#

silk
pub fn readU32BigEndian(bytes: &[u8], offset: usize) -> u32

Returns the big-endian u32 in the 4 bytes of bytes at offset.

Gotchas#

Traps unless offset + 4 is at most bytes.length.

readU64LittleEndian#

silk
pub fn readU64LittleEndian(bytes: &[u8], offset: usize) -> u64

Returns the little-endian u64 in the 8 bytes of bytes at offset.

Gotchas#

Traps unless offset + 8 is at most bytes.length.

readU64BigEndian#

silk
pub fn readU64BigEndian(bytes: &[u8], offset: usize) -> u64

Returns the big-endian u64 in the 8 bytes of bytes at offset.

Gotchas#

Traps unless offset + 8 is at most bytes.length.

writeU16LittleEndian#

silk
pub fn writeU16LittleEndian(bytes: &mut [u8], offset: usize, value: u16) -> ()

Writes value as 2 little-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 2 is at most bytes.length.

writeU16BigEndian#

silk
pub fn writeU16BigEndian(bytes: &mut [u8], offset: usize, value: u16) -> ()

Writes value as 2 big-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 2 is at most bytes.length.

writeU32LittleEndian#

silk
pub fn writeU32LittleEndian(bytes: &mut [u8], offset: usize, value: u32) -> ()

Writes value as 4 little-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 4 is at most bytes.length.

writeU32BigEndian#

silk
pub fn writeU32BigEndian(bytes: &mut [u8], offset: usize, value: u32) -> ()

Writes value as 4 big-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 4 is at most bytes.length.

writeU64LittleEndian#

silk
pub fn writeU64LittleEndian(bytes: &mut [u8], offset: usize, value: u64) -> ()

Writes value as 8 little-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 8 is at most bytes.length.

writeU64BigEndian#

silk
pub fn writeU64BigEndian(bytes: &mut [u8], offset: usize, value: u64) -> ()

Writes value as 8 big-endian bytes into bytes at offset.

Gotchas#

Traps unless offset + 8 is at most bytes.length.

Category: Encoding.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page