uuencoding

uuencoding is a form of binary-to-text encoding that originated in the Unix programs uuencode and uudecode written by Mary Ann Horton at UC Berkeley in 1980,[1] for encoding binary data for transmission in email systems.

The name "uuencoding" is derived from "Unix-to-Unix encoding", i.e. the idea of using a safe encoding to transfer Unix files from one Unix system to another Unix system but without guarantee that the intervening links would all be Unix systems. Since an email message might be forwarded through or to computers with different character sets or through transports which are not 8-bit clean, or handled by programs that are not 8-bit clean, forwarding a binary file via email might cause it to be corrupted. By encoding such data into a character subset common to most character sets, the encoded form of such data files was unlikely to be "translated" or corrupted, and would thus arrive intact and unchanged at the destination. The program uudecode reverses the effect of uuencode, recreating the original binary file exactly. uuencode/decode became popular for sending binary (and especially compressed) files by email and posting to Usenet newsgroups, etc.

It has now been largely replaced by MIME and yEnc. With MIME, files that might have been uuencoded are instead transferred with base64 encoding.

Encoded format

A uuencoded file starts with a header line of the form:

begin <mode> <file><newline>

<mode> is the file's Unix file permissions as three octal digits (e.g. 644, 744). This is typically only significant to unix-like operating systems.

<file> is the file name to be used when recreating the binary data.

<newline> signifies a newline character, used to terminate each line.

Each data line uses the format:

<length character><formatted characters><newline>

<length character> is a character indicating the number of data bytes which have been encoded on that line. This is an ASCII character determined by adding 32 to the actual byte count, with the sole exception of a grave accent "`" (ASCII code 96) signifying zero bytes. All data lines except the last (if the data length was not divisible by 45), have 45 bytes of encoded data (60 characters after encoding). Therefore, the vast majority of length values is 'M', (32 + 45 = ASCII code 77 or "M").

<formatted characters> are encoded characters. See Formatting Mechanism for more details on the actual implementation.

The file ends with two lines:

`<newline>
end<newline>

The second to last line is also a character indicating the line length with the grave accent signifying zero bytes.

As a complete file, the uuencoded output for a plain text file named cat.txt containing only the characters Cat would be

begin 644 cat.txt
#0V%T
`
end

The begin line is a standard uuencode header; the '#' indicates that its line encodes three characters; the last two lines appear at the end of all uuencoded files.

Formatting mechanism

The mechanism of uuencoding repeats the following for every 3 bytes, encoding them into 4 printable characters, each character representing a radix-64 numerical digit:

  1. Start with 3 bytes from the source, 24 bits in total.
  2. Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).
  3. Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (" " space) and 95 ("_" underline). 96 ("`" grave accent) as the "special character" is a logical extension of this range.
  4. Output the ASCII equivalent of these numbers.

If the source length is not divisible by 3 then the last 4-byte section will contain padding bytes to make it cleanly divisible. These bytes are subtracted from the line's <length character> so that the decoder does not append unwanted characters to the file.

uudecoding is reverse of the above, subtract 32 from each character's ASCII code to get a 6-bit value, concatenate 4 6-bit groups to get 24 bits, then output 3 bytes.

The encoding process is demonstrated by this table, which shows the derivation of the above encoding for "Cat".

Original characters C a t
Original ASCII, decimal 67 97 116
ASCII, binary 0 1 0 0 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 1 0 1 0 0
New decimal values 16 54 5 52
+32 48 86 37 84
Uuencoded characters 0 V % T

uuencode table

The following table shows the conversion of the decimal value of the 6-bit fields obtained during the conversion process and their corresponding ASCII character output code and character.

Note that 96 ("`" grave accent) is a character that is seen in uuencoded files but is typically only used to signify a 0-length line, usually at the end of a file. It will never naturally occur in the actual converted data since it is outside the range of 32 to 95. The sole exception to this is that some uuencoding programs use the grave accent to signify padding bytes instead of a space. However, the character used for the padding byte is not standardized, so either is a possibility.

six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
six
bits
ASCII
code
ASCII
char
00 32 SP   10 42 *   20 52 4   30 62 >   40 72 H   50 82 R   60 92 \
01 33 !   11 43 +   21 53 5   31 63 ?   41 73 I   51 83 S   61 93 ]
02 34 "   12 44 ,   22 54 6   32 64 @   42 74 J   52 84 T   62 94 ^
03 35 #   13 45 -   23 55 7   33 65 A   43 75 K   53 85 U   63 95 _
04 36 $   14 46 .   24 56 8   34 66 B   44 76 L   54 86 V
05 37 %   15 47 /   25 57 9   35 67 C   45 77 M   55 87 W
06 38 &   16 48 0   26 58 :   36 68 D   46 78 N   56 88 X
07 39 '   17 49 1   27 59 ;   37 69 E   47 79 O   57 89 Y
08 40 (   18 50 2   28 60 <   38 70 F   48 80 P   58 90 Z
09 41 )   19 51 3   29 61 =   39 71 G   49 81 Q   59 91 [

Example

The following is an example of uuencoding a one-line text file. In this example, %0D is the byte representation for carriage return (CR), and %0A is the byte representation for line feed (LF).

file
File Name = wikipedia-url.txt
File Contents = http://www.wikipedia.org%0D%0A
uuencoding
begin 644 wikipedia-url.txt
::'1T<#HO+W=W=RYW:6MI<&5D:6$N;W)G#0H`
`
end

Forks (file, resource)

Unix traditionally has a single fork where file data is stored. However some file systems support multiple forks associated with a single file. For example, classic Mac OS HFS supported a data fork and a resource fork. Mac OS HFS+ supports multiple forks, as does Microsoft Windows NTFS alternate data streams. Most uucoding tools will only handle data from the primary data fork that can result in a loss of information when encoding/decoding (for example, Windows NTFS file comments are kept in a different fork.) Some tools (like the classic Mac OS application UUTool) solved the problem by concatenating the different forks into one file and differentiating them by file name.

Relation to xxencode, Base64, and Ascii85

Despite its limited range of characters, uuencoded data is sometimes corrupted on passage through certain computers using non-ASCII character sets such as EBCDIC. One attempt to fix the problem was the xxencode format, which used only alphanumeric characters and the plus and minus symbols. More common today is the Base64 format which is based on the same concept of alphanumeric-only as opposed to ASCII 32-95. All three formats use 6 bits (64 different characters) to represent their input data.

Base64 can also be generated by the uuencode program and is similar in format, with the exception of the actual character translation:

The header is changed to

begin-base64 <mode> <file>

the trailer becomes

====

and lines between are encoded with characters chosen from

ABCDEFGHIJKLMNOP
QRSTUVWXYZabcdef
ghijklmnopqrstuv
wxyz0123456789+/

Another alternative is Ascii85, which encodes four binary characters in five ASCII characters. Ascii85 is used in PostScript and PDF formats.

Disadvantages

uuencoding takes 3 pre-formatted bytes and turns them into 4 and also adds begin/end tags, filename, and delimiters. This adds at least 33% data overhead compared to the source alone, though this can be at least somewhat compensated for by compressing the file before uuencoding it.

Support in Python

The Python language supports uuencoding using the codecs module with the codec "uu":

For Python 2 (deprecated/sunset as of January 1st 2020):

$ python -c 'print "Cat".encode("uu")'
begin 666 <data>
#0V%T

end

$

For Python 3 where the codecs module needs to be imported and used directly:

$ python3 -c "from codecs import encode;print(encode(b'Cat', 'uu'))"
b'begin 666 <data>\n#0V%T\n \nend\n'
$

Support in Perl

The Perl language supports uuencoding natively using the pack() and unpack() operators with the format string "u":

$ perl -e 'print pack("u","Cat")'
#0V%T

Decoding base64 with unpack can likewise be accomplished by translating the characters:

$ perl -e '$a="Q2F0"; $a=~tr#A-Za-z0-9+/\.\_##cd; # remove non-base64 chars
> $a=~tr#A-Za-z0-9+/# -_#; # translate sets
> print unpack("u",pack("C",32+int(length($1)*6 / 8)) . $1) while($a=~s/(.{60}|.+)//);'
Cat
gollark: Some way to bind to good markdown parsing libraries?
gollark: Multipart ones, though? For file upload?
gollark: A nice templating DSL?
gollark: Multipart HTML forms?
gollark: Does HYPERCAL support SQLite?

See also

References

  1. "http://www.tuhs.org/cgi-bin/utree.pl?file=4BSD/usr/man/cat1/uuencode.1c". www.tuhs.org. Retrieved 2017-03-13. External link in |title= (help)

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.