haskus-binary-1.5: Haskus binary format manipulation

Safe HaskellNone
LanguageHaskell2010

Haskus.Number.VariableLength

Description

Variable length encodings

  • Unsigned Little Endian Base 128 (ULEB128)

The word is splitted in chunks of 7 bits, starting from least significant bits. Each chunk is put in a Word8. The highest bit indicates if there is a following byte (0 false, 1 true)

Synopsis

Documentation

fromULEB128 :: (Bits a, Monad m, Integral a) => m Word8 -> m a Source #

Convert a stream of ULEB 128 bytes into an Integral

>>> :set -XBinaryLiterals
>>> import Control.Monad.Trans.State
>>> getNext = do { ~(x:xs) <- get; put xs; pure x }
>>> let x = evalState (fromULEB128 getNext) [0b10000001, 0b01111111] :: Word64
>>> x == 0b11111110000001
True

toULEB128 :: (Bits a, Monad m, Integral a) => (Word8 -> m ()) -> a -> m () Source #

Convert an Integral into a stream of ULEB128 bytes

>>> :set -XBinaryLiterals
>>> :set -XFlexibleContexts
>>> let f = toULEB128 (putStr . (++ " ") . bitsToString)
>>> f (0b1001001010101010 :: Word64)
10101010 10100101 00000010

getULEB128 :: (Integral a, Bits a) => Get a Source #

Get an unsigned word in Little Endian Base 128

putULEB128 :: (Integral a, Bits a) => a -> Put Source #

Put an unsigned word in Little Endian Base 128

getSLEB128 :: (Integral a, Bits a) => Get a Source #

Get a signed int in Little Endian Base 128

putSLEB128 :: (Integral a, Bits a) => a -> Put Source #

Put a signed int in Little Endian Base 128

getLEB128Buffer :: BitOrder -> Get Buffer Source #

Get a bytestring containing a decoded LEB128 string