MyNixOS website logo
Description

Encode binary protocols with some odd bit numbers into a bytestring.

bit-protocol

A package suitable for binary protocols defined in a manner where you have bit counts not aligned by 8.

For example, if you have a protocol for sending user profiles saying:

The value sent must be a base64url-encoded string consisting of four values:

  • 6 bits representing user's age
  • 7 bits for their favorite number
  • 5 bits for their lucky number
  • 6 bits for a random number

you could use the library as follows:

import Data.BitProtocol
import Data.ByteString.Base64.URL (encode)

main :: IO ()
main = do
  let age = 29
      fav = 12
      lucky = 13
      rand = 14
  -- the number in protocol should be base64url(011101_0001100_01101_001110)
  print $ encode $ encodeBS8 $ [BitsVal 6 age, BitsVal 7 fav, BitsVal 5 lucky, BitsVal 6 rand]
  -- will output "dGNO"
  -- which is the same as `encode (BC8.pack (map chr [0b01110100, 0b01100011, 0b01001110]))`

Parsing can be done like this:

  let (Right bs) = decode "dGNO"
  let (xs, BitsVal 0 0, "") = parseBS8 [6, 7, 5, 6] bs
  print xs
-- Will output:
--   ( [BitsVal 6 29, BitsVal 7 12, BitsVal 5 13, BitsVal 6 14]
--   , BitsVal 0 0
--   , "")

Warning! Does not support negative numbers.

TODO:

  • [ ] use shift operations instead of division in more places
  • [ ] add a performance test
  • [ ] consider adding checks upon arithmetic overflows (if you use Int in quickcheck test you'll quickly find some)
Metadata

Version

0.2.3.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows