leb128-cereal-1.1: LEB128 and SLEB128 encoding

LicenseMIT
MaintainerJoachim Breitner
Safe HaskellNone
LanguageHaskell2010

Data.Serialize.LEB128

Contents

Description

| This module implements encoding and decoding of Natural and Integer values according to LEB128 and SLEB128. See https://en.wikipedia.org/wiki/LEB128 for a specification.

The module provides conversion to and from strict bytestrings.

Additionally, to integrate these into your own parsers and serializers, you can use the interfaces based on Builder as well as cereal's Get and Put monad.

The decoders will fail if the input is not in canonical representation, i.e. longer than necessary.

This code is inspired by Andreas Klebinger's LEB128 implementation in GHC.

Synopsis

The class of encodable and decodable types

class (Bits a, Num a, Integral a) => LEB128 a Source #

Unsigned number types can be LEB128-encoded

Instances
LEB128 Natural Source # 
Instance details

Defined in Data.Serialize.LEB128

LEB128 Word Source # 
Instance details

Defined in Data.Serialize.LEB128

LEB128 Word8 Source # 
Instance details

Defined in Data.Serialize.LEB128

LEB128 Word16 Source # 
Instance details

Defined in Data.Serialize.LEB128

LEB128 Word32 Source # 
Instance details

Defined in Data.Serialize.LEB128

LEB128 Word64 Source # 
Instance details

Defined in Data.Serialize.LEB128

class (Bits a, Num a, Integral a) => SLEB128 a Source #

Signed number types can be SLEB128-encoded

Instances
SLEB128 Int Source # 
Instance details

Defined in Data.Serialize.LEB128

SLEB128 Int8 Source # 
Instance details

Defined in Data.Serialize.LEB128

SLEB128 Int16 Source # 
Instance details

Defined in Data.Serialize.LEB128

SLEB128 Int32 Source # 
Instance details

Defined in Data.Serialize.LEB128

SLEB128 Int64 Source # 
Instance details

Defined in Data.Serialize.LEB128

SLEB128 Integer Source # 
Instance details

Defined in Data.Serialize.LEB128

Bytestring-based interface

toLEB128 :: LEB128 a => a -> ByteString Source #

LEB128-encodes a natural number to a strict bytestring

fromLEB128 :: LEB128 a => ByteString -> Either String a Source #

LEB128-decodes a natural number from a strict bytestring

toSLEB128 :: SLEB128 a => a -> ByteString Source #

SLEB128-encodes an integer to a strict bytestring

fromSLEB128 :: SLEB128 a => ByteString -> Either String a Source #

SLEB128-decodes an integer from a strict bytestring

Builder interface

buildLEB128 :: LEB128 a => a -> Builder Source #

LEB128-encodes a natural number via a builder

buildSLEB128 :: SLEB128 a => a -> Builder Source #

SLEB128-encodes an integer via a builder

Cereal interface

getLEB128 :: forall a. LEB128 a => Get a Source #

LEB128-decodes a natural number via cereal

getSLEB128 :: forall a. SLEB128 a => Get a Source #

SLEB128-decodes an integer via cereal

putLEB128 :: LEB128 a => Putter a Source #

LEB128-encodes a natural number in cereal's Put monad

putSLEB128 :: SLEB128 a => Putter a Source #

SLEB128-encodes an integer in cereal's Put monad