sandi-0.3.0.1: Data encoding library

Safe HaskellNone

Codec.Binary.Uu

Description

Uuencoding is notoriously badly specified. This implementation aims at being compatible with the GNU Sharutils (http://www.gnu.org/software/sharutils/).

Just like Base64 encoding uuencoding expands blocks of 3 bytes into blocks of 4 bytes. There is however no well defined ending to a piece of encoded data, instead uuencoded data is commonly transferred linewise where each line is prepended with the length of the data in the line.

This module currently only deals with the encoding. Chopping the encoded data into lines, and unchopping lines into encoded data is left as an exercise to the reader. (Patches are welcome.)

Synopsis

Documentation

uu_encode_part :: ByteString -> (ByteString, ByteString)Source

Encoding function.

This function encodes as large a portion of the input as possible and returns the encoded part together with the remaining part. Enough space is allocated for the encoding to make sure that the remaining part is less than 3 bytes long, which means it can be passed to uu_encode_final as is.

>>> uu_encode_part $ Data.ByteString.Char8.pack "foo"
("9F]O","")
>>> uu_encode_part $ Data.ByteString.Char8.pack "foob"
("9F]O","b")

uu_encode_final :: ByteString -> Maybe ByteStringSource

Encoding function for the final block.

The final block has to have a size less than 3.

>>> uu_encode_final $ Data.ByteString.Char8.pack "r"
Just "<@"

Trying to pass in too large a block result in failure:

>>> uu_encode_final $ Data.ByteString.Char8.pack "foo"
Nothing

uu_decode_part :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)Source

Decoding function.

Decode as large a portion of the input as possible. Enough data is allocated for the output to ensure that the remainder is less than 4 bytes in size. Success result in a Right value:

>>> uu_decode_part $ Data.ByteString.Char8.pack "9F]O"
Right ("foo","")
>>> uu_decode_part $ Data.ByteString.Char8.pack "9F]O8F$"
Right ("foo","8F$")

Failures occur on bad input and result in a Left value:

>>> uu_decode_part $ Data.ByteString.Char8.pack "9F 0"
Left ("","9F 0")

uu_decode_final :: ByteString -> Maybe ByteStringSource

Decoding function for the final block.

The final block has to have a size of 0 or 4:

>>> uu_decode_final $ Data.ByteString.Char8.pack "9F\\"
Just "fo"
>>> uu_decode_final $ Data.ByteString.Char8.pack ""
Just ""
>>> uu_decode_final $ Data.ByteString.Char8.pack "9F¬"
Nothing

But it must be the encoding of a block that is less than 3 bytes:

>>> uu_decode_final $ encode $ Data.ByteString.Char8.pack "foo"
Nothing

encode :: ByteString -> ByteStringSource

Convenience function that combines uu_encode_part and uu_encode_final to encode a complete string.

>>> encode $ Data.ByteString.Char8.pack "foo"
"9F]O"
>>> encode $ Data.ByteString.Char8.pack "foobar"
"9F]O8F%R"

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

Convenience function that combines uu_decode_part and uu_decode_final to decode a complete string.

>>> decode $ Data.ByteString.Char8.pack "9F]O"
Right "foo"
>>> decode $ Data.ByteString.Char8.pack "9F]O8F%R"
Right "foobar"

Failures when decoding returns the decoded part and the remainder:

>>> decode $ Data.ByteString.Char8.pack "9F]O8F¬R"
Left ("foo","8F\172R")