Codec.Binary.Base85

sandi-0.4.0: 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

- b85EncodePart :: ByteString -> (ByteString, ByteString)
- b85EncodeFinal :: ByteString -> Maybe ByteString
- b85DecodePart :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)
- b85DecodeFinal :: ByteString -> Maybe ByteString
- encode :: ByteString -> ByteString
- decode :: ByteString -> Either (ByteString, ByteString) ByteString

# Documentation

b85EncodePart :: ByteString -> (ByteString, ByteString) #

Encoding function.

Encodes as large a part as possible of the indata.

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

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

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

b85EncodeFinal :: ByteString -> Maybe ByteString #

Encoding function for the final block.

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

b85DecodePart :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString) #

Decoding function.

Decode as large a portion of the input as possible.

    >>> b85DecodePart $ Data.ByteString.Char8.pack "AoDTs"
    Right ("foob","")
    >>> b85DecodePart $ Data.ByteString.Char8.pack "AoDTs@<)"
    Right ("foob","@<)")
    >>> b85DecodePart $ 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 $ b85DecodePart $ Data.ByteString.Char8.pack $ Prelude.take 129 $ repeat 'y'
    "y"

b85DecodeFinal :: ByteString -> Maybe ByteString #

Decoding function for the final block.

    >>> b85DecodeFinal $ Data.ByteString.Char8.pack "@<)"
    Just "ar"
    >>> b85DecodeFinal $ Data.ByteString.Char8.pack ""
    Just ""
    >>> b85DecodeFinal $ 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"