Codec.Binary.Base85

sandi-0.3.5: Data encoding library

Copyright | (c) 2012 Magnus Therning
License | BSD3
Safe Haskell | None
Language | Haskell98

Codec.Binary.Base85

Description

Implemented as described at http://en.wikipedia.org/wiki/Ascii85.

Synopsis

- b85_encode_part :: ByteString -> (ByteString, ByteString)
- b85_encode_final :: ByteString -> Maybe ByteString
- b85_decode_part :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)
- b85_decode_final :: ByteString -> Maybe ByteString
- encode :: ByteString -> ByteString
- decode :: ByteString -> Either (ByteString, ByteString) ByteString

# Documentation

b85_encode_part :: ByteString -> (ByteString, ByteString)

Encoding function.

Encodes as large a part as possible of the indata.

    >>> b85_encode_part $ Data.ByteString.Char8.pack "foobar"
    ("AoDTs","ar")

It supports special handling of both all-zero groups and all-space groups.

    >>> b85_encode_part $ Data.ByteString.Char8.pack "    "
    ("y", "")
    >>> b85_encode_part $ Data.ByteString.Char8.pack "\0\0\0\0"
    ("z", "")

b85_encode_final :: ByteString -> Maybe ByteString

Encoding function for the final block.

    >>> b85_encode_final $ Data.ByteString.Char8.pack "ar"
    Just "@<)"

b85_decode_part :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)

Decoding function.

Decode as large a portion of the input as possible.

    >>> b85_decode_part $ Data.ByteString.Char8.pack "AoDTs"
    Right ("foob","")
    >>> b85_decode_part $ Data.ByteString.Char8.pack "AoDTs@<)"
    Right ("foob","@<)")
    >>> b85_decode_part $ Data.ByteString.Char8.pack "@<)"
    Right ("","@<)")

At least 512 bytes of data is allocated for the output, but because of the special handling of all-zero and all-space groups it is possible that the space won't be enough. (To be sure to always fit the output one would have to allocate 5 times the length of the input. It seemed a good trade-off to sometimes have to call the function more than once instead.)

    >>> either snd snd $ b85_decode_part $ Data.ByteString.Char8.pack $ Prelude.take 129 $ repeat 'y'
    "y"

b85_decode_final :: ByteString -> Maybe ByteString

Decoding function for the final block.

    >>> b85_decode_final $ Data.ByteString.Char8.pack "@<)"
    Just "ar"
    >>> b85_decode_final $ Data.ByteString.Char8.pack ""
    Just ""
    >>> b85_decode_final $ Data.ByteString.Char8.pack "AoDTs"
    Nothing

encode :: ByteString -> ByteString

Convenience function that combines `b85_encode_part` and `b85_encode_final` to encode a complete string.

    >>> encode  $ Data.ByteString.Char8.pack "foob"
    "AoDTs"
    >>> encode  $ Data.ByteString.Char8.pack "foobar"
    "AoDTs@<)"

decode :: ByteString -> Either (ByteString, ByteString) ByteString

Convenience function that combines `b85_decode_part` and `b85_decode_final` to decode a complete string.

    >>> decode  $ Data.ByteString.Char8.pack "AoDTs"
    "foob"
    >>> encode  $ Data.ByteString.Char8.pack "AoDTs@<)"
    "foobar"