The UUDeview Decoding LibraryFrank Pilhofer |
Abstract: The UUDeview library is a highly portable set of functions that provide facilities for decoding uuencoded, xxencoded, Base64 and BinHex-Encoded files as well as for encoding binary files into all of these representations except BinHex. This document describes how the features of encoding and decoding can be integrated into your own applications.The information is intended for developers only, and is not required reading material for end users. It is assumed that the reader is familiar with the general issue of encoding and decoding and has some experience with the “C” programming language.
This document describes version 0.5, patchlevel 20 of the library.
The Internet provides us with a fast and reliable means of user-to-user message delivery, using private email or newsgroups. Both systems have originally been designed to transport plain-text messages. Over the years, some methods appeared allowing transport of arbitrary binary data by “encoding” the data into plain-text messages. But after these years, there are still certain problems handling the encoded data, and many recipients have difficulties decoding the messages back into their original form.
It should be the job of the mail delivery agent to handle sending and rend receiving binary data transparently. However, the support of most applications is limited, and several incompatibilities among different software exists.
There are three common formats for encoding binary data, called uuencoding, Base64 and BinHex. Issues are further complicated by slight variations of the formats, the packaging, and some broken implementations.
Further problems arise with multi-part postings, where the encoding of a huge file has been split up into several individual messages to ensure proper transfer over gateways with limited message sizes. Very few software is able to properly sort and decode the parts. Even nowadays, many users are at a loss to decode these kinds of messages.
This is where the UUDeview Decoding Library steps in.
The UUDeview library makes an attempt at decoding nearly all kinds of encoded files. It is supposed to decode multi-part files as well as many files simultaneously. Part numbers are evaluated, thus making it possible to re-arrange parts that aren’t in their correct order.
No assumptions are made on the format of the input file. Usually the input will be an email folder or newsgroup messages. If this is the case, the information found in header lines is evaluated; but plain encoded files with no surrounding information are also accepted. The input may also consist of concatenated parts and files.
Decoding files is done in two passes. During the first pass, all input files are scanned. Information is gathered about each chunk of encoded data. Besides the obvious data about type, position and size of the chunk, some environmental information from the envelope of a mail message is also gathered if available.
If the scanner finds a properly MIME-formatted message, a proper MIME parser steps into action. Because MIME messages include precise information about the message’s contents, there is seldom doubt about its parts.
For other, non-MIME messages, the “Subject” header line is closely examined. Two informations are extracted: the part number (usually given in parentheses) and a unique identifier, which is used to group series of postings. If the subject is, for example, “uudeview.tgz (01/04)”, the scanner concludes that this message is the first in a series of four, and the indicated filename is an ideal key to identify each of the four parts.
If the subject is incomplete (no part number) or missing, the scanner tries to make the best of the available information, but some of the advanced features won’t work. For example, without any information about the part number, it must be assumed that the available parts are in correct order and can’t be automatically rearranged.
All the information is gathered in a linked list. An application can then examine the nodes of the list and pick individual items for decoding. The decoding functions will then visit the parts of a file in correct order and extract the binary data.
Because of heavy testing of the routines against real-life data and many problem reports from users, the functions have become very robust, even against input files with few, missing or broken information.
(3174,2874)(814,-2548) (3376,-886)( 0, 1)525 (3376,-361)(-1, 0)1950 (1426,-361)( 0,-1)1800 (1426,-2161)( 1, 0)1950 (3376,-2161)( 0, 1)525 (3376,-1636)(-1, 0)1725 (1651,-1636)( 0, 1)750 (1651,-886)( 1, 0)1650 (3301,-886)( 1, 0) 75 (1351,-2161)(-1, 0)525 (826,-2161)( 0, 1)2475 (826,314)( 1, 0)3150 (3976,314)( 0,-1)2475 (3976,-2161)(-1, 0)525 (3451,-2161)( 0, 1)1875 (3451,-286)(-1, 0)2100 (1351,-286)( 0,-1)1875 (1351,-2161)( 0, 1) 0 (901,-2536)(3000,300) (1726,-1561)(1650,600) (2551,-1186)(0,0)[b]1214.4ptUUDeview (2401,-1861)(0,0)[b]1012.0ptApplication OS (2401,-2086)(0,0)[b]1012.0ptServices Interface (2401,-586)(0,0)[b]1012.0ptApplication (2401,-811)(0,0)[b]1012.0ptLanguage Interface (2401,-61)(0,0)[b]1214.4ptApplication (2401,-2461)(0,0)[b]1214.4ptOperating System (2551,-1456)(0,0)[b]1214.4ptDecoding Library
Figure 1 displays how the library can be integrated into an application. The library does not assume any capabilities of the operating system or application language, and can thus be used in almost any environment. The few necessary interfaces must be provided by the application, which does usually know a great deal more about the target system.
The idea of the “language interface” is to allow integration of the library services into other programming languages; if the application is itself written in C, there’s no need for a separate interface, of course. Such an interface currently exists for the Tcl scripting language; other examples might be Visual Basic, Perl or Delphi.
These are some buzzwords that will be used in the following text.
On Unix systems, configuration and compilation is trivial. The script configure automatically checks your system and configures the library appropriately. A subsequent “make” compiles the modules and builds the final library.
On other systems, you must manually create the configuration file and the Makefile. The configuration file config.h contains a set of preprocessor definitions and macros that describe the available features on your systems.
You can find all available definitions in config.h.in. This file undefines all possible definitions; you can create your own configuration file starting from config.h.in and editing the necessary differences.
Most definitions are either present or absent, only a few need to have a value. If not explicitly mentioned, you can activate a definition by changing the default undef into define. The following definitions are available:
There are a number of options that define whether header files are available on your system. Don’t worry if some of them are not. If a header file is present, define “HAVE_name-of-header”: HAVE-_ERRNO_H, HAVE-_FCNTL_H, HAVE-_IO_H, HAVE-_MALLOC_H, HAVE-_MEMORY_H, HAVE-_UNISTD_H and HAVE-_SYS_TIME_H (for <sys/time.h>). Some other include files are needed as well, but there are no macros for mandatory include files.
There’s also a number of header-specific definitions that do not fit into the general present-or-not-present scheme.
The tools library fptools defines many functions that aren’t standard on all systems. Most of them do not differ in behavior from their originals, but might be slightly slower. But since they are usually only needed in non-speed-critical sections, the replacements are used throughout the library. For a full listing of the available replacement functions, see section 11.
However, there are two functions, strerror and tempnam, that aren’t fully implemented. The replacement strerror does not have a table of error messages and only produces the error number as string, and the “fake” tempnam does not necessarily use a proper temp directory.
Because some functionality is missing, the replacement functions should only be used if the original is not available.
The Makefile is automatically generated by the configuration script from the template in Makefile.in. This section explains how the template must be edited into a proper Makefile.
Just copy Makefile.in to Makefile and edit the place-holders for the following values.
Some systems do not know Makefiles but offer the concept of a “project”.2 In this case, create a new project targeting a library and add all source codes to the project. Then, make sure that the include path includes the current directory. Add options to the compiler command so that the symbol “HAVE_CONFIG_H” gets defined. Additionally, the symbol “VERSION” must be defined as a string holding the release number, currently “0.5” and “PATCH” must be defined as a string holding the patch level, currently “20”.
On 16-bit systems, the package should be compiled using the “Large” memory model, so that more than just 64k data space is available.
Compiling the parts of your project that use the functions from the decoding library is pretty straightforward:
At some points, the decoding library offers to call your custom procedures to do jobs you want to take care of yourself. Some examples are the “Message Callback” to print a message or the “Busy Callback”, which is frequently called during lengthy processing of data to indicate the progress. You can hook up your functions by calling some library function with a pointer to your function as a parameter.
In some cases, you will want that one of your functions receives certain data as a parameter. One reason to achieve this would be through global data; another possibility is provided through the passing of an opaque data pointer.
All callback functions are declared to take an additional parameter of type void*. When hooking up one of your callbacks, you can specify a value that will passed whenever your function is called. Since this pointer is never touched by the library, it can be any kind of data, usually some composed structure. Some application for the Message Callback might be a FILE* pointer to log the messages to.
For portability reasons, you should declare your callbacks with the first parameter actually being a void* pointer and only cast this pointer to its real type within the function body. This prevents compiler warnings about the callback setup.
For portability reasons, the library does not assume the availability of a terminal, so it does not initially know where to print messages to. The library generates some messages about its progress as well as more serious warnings and errors. An application should provide a message callback that displays them. The function might also choose to ignore informative messages and only display the fatal ones.
A Message Callback takes three parameters. The first one is the opaque data pointer of type void*. The second one is a text message of more or less arbitrary length without line breaks. The last parameter is an indicator of the seriousness of this message. A string representation of the warning level is also prefixed to the message.
Some library functions, like scanning of an input file or decoding an output file, can take quite some time. An application will usually want to inform the user of the progress. A custom “Busy Callback” can be provided to take care of this job. This function will then be called frequently while a large action is being executed within the library. It is not called when the application itself has control.
Apart from the usual opaque data pointer, the Busy Callback receives a structure of type uuprogress with the following members:
In some cases, it is possible that the percent counter jumps backwards. This happens seldom enough not to worry about it, but the callback should take care not to crash in this case.4
The Busy Callback is declared to return an integer value. If a non-zero value is returned, the current operation from which the callback was called is canceled, which then aborts with a return value of UURET-_CANCEL (see later).
Input files are usually needed twice, first for scanning and then for decoding. If the input files are downloaded from a remote server, perhaps by NNTP, they would have to be stored on the local disk and await further handling. However, the user may choose not to decode some files after all.
If disk space is important, it is possible to install a “File Callback”. When scanning a file, it is assigned an “Id”. After scanning has completed, the application can delete the input file. If it should be required later on for decoding, the File Callback is called to map the Id back to a filename, possibly retrieving another copy and disposing of it afterwards.
The File Callback receives four parameters. The first is the opaque data pointer, the second is the Id that was assigned to the file while scanning. The fourth parameter is an integer. If it is non-zero, then the function is supposed to retrieve the file in question, store it on local disk, and write the resulting filename into the area to which the third parameter (a char* pointer) points. A fourth parameter of zero indicates that the decoder is done handling the file, so that the function can decide whether or not to remove the file.
The function must return UURET_OK upon success, or any other appropriate error code upon failure.
Since it can usually be assumed that disk space is plentily available, and storing a file is “cheaper” than retrieving it twice, this mechanism has not been used so far.
For portability reasons, the library does not make any assumptions of the legality of certain filenames. It will pick up a “garbage” file name from the encoded file and happily use it if not told otherwise. For example, on DOS systems many filenames must be truncated in order to be valid.
If a “Filename Filter” is installed, the library will pass each potential filename to the filter and then use the filename that the filter function returns. The filter also has to remove all directory information from the filename – the library itself does not know about directories at all.
The filter function receives the potential filename as string and must return a pointer to a string with the corrected filename. It may either return a pointer to some position in the original string or a pointer to some static area, but it should not modify the source string.
Two examples of filename filters can be found among the UUDeview distribution as uufnflt.c. The DOS filter function disposes directory information, uses only the first 8 characters of the base filename and the first three characters after the last ’.’ (since a filename might have two extensions). Also, space characters are replaced by underscores. The Unix filter just returns a pointer to the filename part of the name (without directory information).
The “garbage” filename mentioned above was just for the sake of argument. It is generally safe to assume that the input filename is not too weird; after all, it is a filename valid on some system. Still, the user should always be granted the possibility of renaming a file before decoding it, to allow decoding of files with insane filenames.
While scanning the input files, a linked list is built. Each node is of type uulist and describes one file, possibly composed of several parts. This section describes the members of the structure that may be of interest to an application.
No more than 256 parts are listed here.
Most of the library functions return a value indicating success or the type of error occurred. The following values can be returned:
An application program can set and query a number of options. Some of them are read-only, but others can modify the behavior quite drastically. Some of them are intended to be set by the end user via an options menu.
This flag does not have any effect on MIME multipart messages, which are always scanned to the end (alas, the Epilogue will be skipped). Actually, with this flag set, the scanner becomes more MIME-compliant.
If the “dumbness” is set to 1, the code to derive a part number is disabled; it will then be assumed that all parts within a group appear in correct order: the first one is assigned number 1 etc. However, part numbers found in MIME-headers are still used (I haven’t yet found a file where these were wrong).
A dumbness of 2 also switches off the code to select a unique identifier from the subject line. This does still work with single-part files6 and might work with multi-part files, as long as they’re in correct order and not mixed. The filename is found on the first part and then passed on to the following parts.
This option only takes effect for files scanned afterwards.
Setting the “bracket policy” to 1 changes this precedence. If now both parentheses and brackets are present, the numbers within brackets will be evaluated first.
This option only takes effect for files scanned afterwards.
Note that this option only affects stand-alone data. Input from Mime messages with the encoding type correctly specified in the “Content-Transfer-Encoding” header is always evaluated.
There is also no problem with encoding types different than Base64, since they have an explicit notion of the beginning and end of a file, and no danger of misinterpretation exists.
This option does not alter the behaviour on multi-part files, where the individual parts always receive the three-digit part number as extension.
You can also set the value of this option to 2 to enforce strict MIME adherance. If the option is 1, the library will still look into plain text attachments and try to find encoded data within it. This causes for example uuencoded files that were then sent in a MIME envelope to be recognized. With an option value of 2, the library won’t even do that, trusting all MIME header information.
After describing all the framework in the previous chapters, it is time to mention some function calls. Still, the functions presented here don’t actually do anything, they just query and modify the behavior of the core functions.
For integer options, cval may be NULL and len 0 and vice versa: for string options, ival is not evaluated.
Now for the more useful functions. The functions within this section are everything you need to scan and decode files.
If id is non-NULL, its value is used instead of the filename, and the file callback is used to map the id back into a filename whenever this input file is needed again. If id is NULL, then the input file must not be deleted or modified until UUCleanUp has been called.
If delflag is non-zero, the input file will automatically be removed within UUCleanUp. This is useful when the decoder’s input are also temporary files – this way, the application can forget about them right after they’re “loaded”. The value of delflag is ignored, however, if id is non-NULL; combining both options does not make sense.
The behavior of this function is influenced by some of the options, most notably UUOPT-_FAST. The two most probable return values are UURET-_OK, indicating successful completion, or UURET-_IOERR in case of some error while reading the file. The other return values are less likely to appear.
Note that files are even scheduled for destruction if an error did happen during scanning (with the exception of a file that could not be opened). But error handling is slightly problematic here anyway, since it might be possible that useful data was found before the error occurred.
UURET-_OK is returned upon successful completion. Most other error codes can occur, too. UURET-_NODATA is returned if you try to decode parts without encoded data or with a missing beginning (uuencoded and xxencoded files only) – of course, this condition would also have been obvious from the state value of the file list structure.
The setting of UUOPT-_DESPERATE changes the behavior if an unexpected end of file was found (usually meaning that one or more parts are missing). Normally, the partially-written target file is removed and the value UURET-_NOEND is returned. In desperate mode, the same error code is returned, but the target file is not removed.
The target file is removed in all other error conditions.
If the target file already exists, the value of the UUOPT-_OVERWRITE option is checked. If it is false (zero), then the error UURET-_EXISTS is generated and decoding fails. If the option is true, the target file is silently overwritten.8
The file is first decoded into a temporary file, then the temporary file is copied to the final location. This is done to prevent overwriting target files with data that turns out too late to be invalid.
The callback can return either zero, meaning that it can accept more data, or non-zero, which immediately stops retrieval of more information.
Usually, the opaque pointer holds some information about a text window, so that the callback knows where to print the next line. In a terminal-oriented application, the user can be queried each 25th line and the callback can return non-zero if the user doesn’t wish to continue.
This function is a bunch of heuristics, and I don’t really trust them. In some cases, the “smart” merge may do more harm than good. This function should only be called as last resort on explicit user request. The first call should be made with pass==0, then with pass==1 and at last with pass=99.
There are a couple of functions to encode data into a file. You will usually need no more than one of them, depending on the job you want to do. The functions also differ in the headers they generate. Some functions do generate full MIME-compliant headers. This may sound like the best choice, but it’s not always the wisest choice. Please follow the following guidelines.
All of the functions have a bunch of parameters for greater flexibility. Don’t be confused by their number, usually you’ll need to fill only a few of them. There’s a number of common parameters which can be explained separately:
Now for the functions …
int UUEncodeMulti | (FILE *outfile, FILE *infile, |
char *infname, int encoding, | |
char *outfname, char *mimetype, | |
int filemode) |
int UUEncodePartial | (FILE *outfile, FILE *infile, |
char *infname, int encoding, | |
char *outfname, char *mimetype, | |
int filemode, int partno, | |
long linperfile) |
Each call produces linperfile lines of encoded output. For uuencoded and xxencoded files, each output line encodes 45 bytes of input data, each Base64 line encodes 57 bytes. If linperfile==0, this function is equivalent to UUEncodeMulti.
Different handling is necessary when reading from an input stream (if infile!=NULL) compared to reading from a file (if infname!=NULL). In the first case, the function must be called until feof() becomes true on the input file, or an error occurs. In the second case, the file will be opened internally. Instead of UURET-_OK, a value of UURET-_CONT is returned for all but the last part.
int UUEncodeToStream | (FILE *outfile, FILE *infile, |
char *infname, int encoding, | |
char *outfname, int filemode) |
int UUEncodeToFile | (FILE *infile, char *infname, |
int encoding, char *outfname, | |
char *diskname, long linperfile) |
If diskname!=NULL and does not contain directory information, the target filename is the concatenation of the save path and diskname. If diskname is an absolute path name, it is used itself.
From the so-generated target filename, the extension is stripped. For single-part output files, the extension set with the UUOPT-_ENCEXT option is used. Otherwise, the three-digit part number is used as extension. If the destination file does already exist, the value of the UUOPT-_OVERWRITE is checked; if overwriting is not allowed, encoding fails with UURET-_EXISTS.
int UUE_PrepSingle | (FILE *outfile, FILE *infile, |
char *infname, int encoding, | |
char *outfname, int filemode, | |
char *destination, char *from, | |
char *subject, int isemail) |
If from!=NULL, it is sent as the sender’s email address in the “From” header field. Some MDA programs are able to provide the sender’s address themselves, so this value may be NULL in certain cases.
If subject!=NULL, the text is included in the “Subject” header field. The subject is extended with information about the file name and part number (in this case, always “(001/001)”).
“Destination” must not be NULL. Depending on the “isemail” flag, its contents are sent either in the “To” or “Newsgroups” header field.
int UUE_PrepPartial | (FILE *outfile, FILE *infile, |
char *infname, int encoding, | |
char *outfname, int filemode, | |
int partno, long linperfile, | |
long filesize, | |
char *destination, char *from, | |
char *subject, int isemail) |
The only additional parameter is filesize. Usually, this value can be 0, as the size of the input file can usually be determined by performing a stat() call. However, this might not be possible if infile refers to a pipe. In that case, the value of filesize is used.
If the size of the input data cannot be determined, and filesize is 0, the function refuses encoding into multiple files and produces only a single stream of output.
If data is read from a file instead from a stream (infile==NULL), the function opens the file internally and returns UURET-_CONT instead of UURET-_OK on successful completion for all but the last part.
In this section, we implement and discuss the “Trivial Decoder”, which illustrates the use of the decoding functions. We start with the absolute minimum and then add more features and actually end up with a limited, but useful tool. For a full-scale frontend, look at the implementation of the “UUDeview” program. The sample code can be found among the documentation files as td-v1.c, td-v2.c and td-v3.c.
The minimal decoding program is displayed in Figure 2. Only four code lines are needed for the implementation. <stdlib.h> defines NULL, <uudeview.h> declares the decoding library functions, and <config.h>, the library’s configuration file, is needed for some configuration details9.
After initialization, the file given as first command line parameter is scanned. No symbolic name is assigned to the file, so that we don’t need a file callback. After the scanning, the encoded file is decoded and stored in the current directory by its native name.
Of course, there is much to complain about:
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <config.h> #include <uudeview.h> int main (int argc, char *argv[]) { uulist *item; int i, res; UUInitialize (); for (i=1; i<argc; i++) if ((res = UULoadFile (argv[i], NULL, 0)) != UURET_OK) fprintf (stderr, "could not load %s: %s\n", argv[i], (res==UURET_IOERR) ? strerror (UUGetOption (UUOPT_ERRNO, NULL, NULL, 0)) : UUstrerror(res)); for (i=0; (item=UUGetFileListItem(i)) != NULL; i++) { if ((item->state & UUFILE_OK) == 0) continue; if ((res = UUDecodeFile (item, NULL)) != UURET_OK) { fprintf (stderr, "error decoding %s: %s\n", (item->filename==NULL)?"oops":item->filename, (res==UURET_IOERR) ? strerror (UUGetOption (UUOPT_ERRNO, NULL, NULL, 0)) : UUstrerror(res)); } else { printf ("successfully decoded '%s'\n", item->filename); } } UUCleanUp (); return 0; }
The second version, printed in figure 3, addresses all of the above problems. The code size more than tripled, but that’s largely because of the error messages.
All files given on the command line are scanned10, and all encoded files are decoded. Of course, it is now also possible for an encoded file to span its parts over more than one input file. Appropriate error messages are printed upon failure of any step, and a success message is printed for successfully decoded files.
Apart from the program’s unfriendliness that there is no user-interaction like selective decoding of files, choice of a target directory etc., there are only three more items to complain about:
… right after the #includes
#include <fptools.h> void MsgCallBack (void *opaque, char *msg, int level) { fprintf (stderr, "%s\n", msg); } char * FNameFilter (void *opaque, char *fname) { static char dname[13]; char *p1, *p2; int i; if ((p1 = _FP_strrchr (fname, '/')) == NULL) p1 = fname; if ((p2 = _FP_strrchr (p1, '\\')) == NULL) p2 = p1; for (i=0, p1=dname; *p2 && *p2!='.' && i<8; i++) *p1++ = (*p2==' ')?(p2++,'_'):*p2++; while (*p2 && *p2 != '.') p2++; if ((*p1++ = *p2++) == '.') for (i=0; *p2 && *p2!='.' && i<3; i++) *p1++ = (*p2==' ')?(p2++,'_'):*p2++; *p1 = '\0'; return dname; }
… within main() after UUInitialize
UUSetMsgCallback (NULL, MsgCallBack); UUSetFNameFilter (NULL, FNameFilter);
… replacing the main loop’s else
else { printf ("successfully decoded '%s' as '%s'\n", item->filename, UUFNameFilter (item->filename)); }
This last section adds a simple filename filter (targeting at a DOS system with 8.3 filenames) and a simple message callback, which just dumps messages to the console. Figure 4 lists the changes with respect to version 2 (for the full listing, refer to the source file on disk).
The message callback, a one-liner, couldn’t be simpler. The filename filter will probably not win an award for good programming style, but it does its job of stripping Unix-style or DOS-style directory names and using only the first 8 characters of the base filename and the first three characters of the extension. If the filename contains space characters, they’re replaced by underscores. Note that dname, the storage for the resulting filename, is declared static, as it must be accessible after the filter function has returned.
For portability, the filename filter uses a replacement function from the fptools library instead of relying of a native implementation of the strrchr function.
Both callbacks are installed right after initializing the library. Since now the filename of the decoded file may be different from the filename of the file list structure, we recreate the resulting filename by calling the filename filter ourselves for display, so that the user knows where to look for the file.
This section is a short reference for the replacement functions from the fptools library. Some of them may be useful in the application code as well. Most of these functions are pretty standard in modern systems, but there’s also a few from the author’s imagination. Each of the functions is tagged with information why this replacement exists:
This section mentions a few known problems with the library, which the author considers to be “features” rather than “bugs”, meaning that they probably won’t be “fixed” in the near future.
RFC documents (“Request for Comments”) can be downloaded from many ftp sites around the world.
The following sections describe the four most widely used formats for encoding binary data into plain text, uuencoding, xxencoding, Base64 and BinHex. Another section shortly mentions Quoted-Printable encoding.
Other formats exist, like btoa and ship, but they are not mentioned here. btoa is much less efficient than the others. ship is slightly more efficient and will probably be supported in future.
Uuencoding, xxencoding and Base 64 basically work the same. They are all “three in four” encodings, which means that they take three octets11 from the input file and encode them into four characters.
Three bytes are 24 bits, and they are divided into 4 sections of 6 bits each. Table 1 describes in detail how the input bits are copied into the output data bits. 6 bits can have values from 0 to 63; each of the “three in four” encodings now uses a character table with 64 entries, where each possible value is mapped to a specific character.
The advantage of three in four encodings is their simplicity, as encoding and decoding can be done by mere bit shifting and two simple tables (one for encoding, mapping values to characters, and one for decoding, with the reverse mapping). The disadvantage is that the encoded data is 33% larger than the input (not counting line breaks and other information added to the encoded data).
The before-mentioned ship data is more effective; it is a so-called Base 85 encoding. Base 85 encodings take four input bytes (32 bits) and encode them into five characters. Each of this characters encode a value from 0 to 84; five characters can therefore encode a value from 0 to 855=4437053125, covering the complete 32 bit range. Base 85 encodings need more “complicated” math and a larger character table, but result in only 25% bigger encoded files.
In order to illustrate the encodings and present some actual data, we will present the following text encoded in each of the formats:
This is a test file for illustrating the various
encoding methods. Let's make this text longer than
57 bytes to wrap lines with Base64 data, too.
Greetings, Frank Pilhofer
A document actually describing uuencoding as a standard does not seem to exist. This is probably the reason why there are so many broken encoders and decoders around that each take their liberties with the definition.
The following text describe the pretty strict rules for uuencoding that are used in the UUEnview encoding engine. The UUDeview decoding engine is much more relaxed, according to the general rule that you should be strict in all that you generate, but liberal in the data that your receive.
Uuencoded data always starts with a begin line and continues until the end line. Encoded data starts on the line following the begin. Immediately before the end line, there must be a single empty line (see below).
begin mode filename
… encoded data …
“empty” line
end
The begin line starts with the word begin in the first column. It is followed, all on the same line, by the mode and the filename.
mode is a three- or four-digit octal number, describing the access permissions of the target file. This mode value is the same as used with the Unix chmod command and by the open system call. Each of the three digits is a binary or of the values 4 (read permission), 2 (write permission) and 1 (execute permission). The first digit gives the user’s permissions, the second one the permissions for the group the user is in, and the third digit describes everyone else’s permissions. On DOS or other systems with only a limited concept of file permissions, only the first digit should be evaluated. If the “2” bit is not set, the resulting file should be read-only, the “1” bit should be set for COM and EXE files. Common values are 644 or 755.
filename is the name of the file. The name should be without any directory information.
The basic version of uencoding simply uses the ASCII characters 32-95 for encoding the 64 values of a three in for encoding. An exception12 is the value 0, which would normally map into the space character (ASCII 32). To prevent problems with mailers that strip space characters at the beginning or end of the line, character 96 “ ‘ ” is used instead. The encoding table is shown in table 2.
Each line of uuencoded data is prefixed, in the first column, with the encoded number of encoded octets on this line. The most common prefix that you’ll see is ‘M’. By looking up ‘M’ in table 2, we see that it represents the number 45. Therefore, this prefix means that the line contains 45 octets (which are encoded into 60 (45/3*4) plain-text characters).
In uuencoding, each line has the same length, normally, the length (excluding the end of line character) is 61. Only the last line of encoded data may be shorter.
If the input data is not a multiple of three octets long, the last triple is filled up with (one or two) nulls. The decoder can determine the number of octets that are to go into the output file from the prefix.
After the last line of data, there must be an empty line, which must be a valid encoded line containing no encoded data. This is achieved by having a line with the single character “ ‘ ” on it (which is the prefix that encodes the value of 0 octets).
The encoded file is then ended with a line consisting of the word end.
Uuencoding does not describe a mechanism for splitting a file into two or more messages for separate mailing or posting. Usually, the encoded file is simply split into parts of more or less equal line count13. Before the age of smart decoders, the recipient had to manually concatenate the parts and remove the headers in between, because the headers of mail messages might just be valid uuencoded data lines, thus potentially corrupting the data.
There are many variations of the above rules which must be taken into account in a decoder program. Here are the most frequent:
There is also some confusion how to properly terminate a line. Most encoders simply use the convention of the local system (DOS encoders using CRLF, Unix encoders using LF, Mac encoders using CR), but with respect to the MIME standard, the encoding library uses CRLF on all systems. This causes a slight problem with some Unix decoders, which look for “end” followed directly by LF (as four characters in total). Such programs report “end not found”, but nevertheless decode the file correctly.
This is what our sample text looks like as uuencoded data:
begin 600 test.txt
M5&AI<R!I<R!A('1E<W0@9FEL92!F;W(@:6QL=7-T<F%T:6YG('1H92!V87)I
M;W5S"F5N8V]D:6YG(&UE=&AO9',N($QE="=S(&UA:V4@=&AI<R!T97AT(&QO
M;F=E<B!T:&%N"C4W(&)Y=&5S('1O('=R87`@;&EN97,@=VET:"!"87-E-C0@
E9&%T82P@=&]O+@I'<F5E=&EN9W,L($9R86YK(%!I;&AO9F5R"@``
`
end
The xxencoding method was conceived shortly after the initial use of uuencoding. The first implementations of uuencoding did not realize the potential problem of using the space character for encoding data. Before this mistake was workarounded with the special case, another author used a different charset for encoding, composed of characters available on any system.
Xxencoding is absolutely identical to uuencoding with the difference of using a different mapping of data values into printable characters (table 3). Instead of ‘M’, a normal-sized xxencoded line is prefixed by ‘h’ (note that ‘h’ encodes 45, just as ‘M’ in uuencoding). The empty data line at the end consists of a single ‘+’ character. Our sample file looks like the following:
begin 600 test.txt
hJ4VdQm-dQm-V65FZQrEUNaZgNG-aPr6UOKlgRLBoQa3oOKtb65FcNG-qML7d
hPrJn0aJiMqxYOKtb64pZR4VjN5Ai62lZR0Rn64pVOqIUR4VdQm-oNLVo64lj
hPaRZQW-oO43i0XIr647tR4Jn65Fj65RmML+UP4ZiNLAURqZoO0-0MLBZBXEU
ZN43oMGkUR4xj9Ud5QaJZR4ZiNrAg62NmMKtf63-dP4VjNaJm0U++
+
end
Base 64 is part of the MIME (Multipurpose Internet Mail Extensions) standard, described in [RFC1521], section 5.2. Sometimes, it is incorrectly referred to as “MIME encoding”; however, the MIME documents specify much more than just how to encode binary data. It defines a complete framework for attachments within E-Mails. Being part of a widely accepted standard, Base64 has the advantage of being the best-specified type of encoding.
The general concept of three-in-four encoding is the same as with the previous two types, just another new character table to represent the values needs to be introduced (table 4). Note that this table differs from the xxencoding table only in a single character (‘/’ versus ‘-’). If a line of encoding does not feature either character, it may be difficult to tell which encoding is used on the line.
The Base64 encoding does not have “begin” and “end” lines; such a concept is not needed, because the framework of a MIME message defines the beginning and end of a part. The encoded data is defined to be a “stream” of characters, and the decoder is supposed to ignore any “illegal” characters in the stream (such as line breaks or other whitespace). Each line must be shorter than 80 characters and terminated with a CRLF sequence. No particular line length is enforced, but most implementations encode 57 octets into 76 encoded characters. Theoretically, a line might hold 79 characters, although this would violate the rule of thumb that the line length is a multiple of four (therefore encoding an integral number of octets).14
The end-of-file handling if the input data has not a multiple of three octets is slightly different in Base64 encoding than it is in uuencoding. If one octet is left at the end of the input stream, the data is padded with 4 zero bits (giving a total of 12 bits) and encoded into two characters. After that, two equal signs ‘=’ are written to complete the four character sequence. If two octets are left, the data is padded with 2 zero bits (giving a total of 18 bits), and encoded into three characters, after which a single equal sign ‘=’ is written.
Here’s our sample file in Base64. Note that this text is only the encoded data. It is not a valid MIME message. Without the required framework, no proper MIME software will read it.
VGhpcyBpcyBhIHRlc3QgZmlsZSBmb3IgaWxsdXN0cmF0aW5nIHRoZSB2YXJpb3VzCmVuY29kaW5n
IG1ldGhvZHMuIExldCdzIG1ha2UgdGhpcyB0ZXh0IGxvbmdlciB0aGFuCjU3IGJ5dGVzIHRvIHdy
YXAgbGluZXMgd2l0aCBCYXNlNjQgZGF0YSwgdG9vLgpHcmVldGluZ3MsIEZyYW5rIFBpbGhvZmVy
Cg==
For a more elaborate documentation of Base64 encoding and details of the MIME framework, I suggest reading [RFC1521].
The MIME standard also defines a way to split a message into multiple parts so that re-assembly of the parts on the remote end is easily possible. For details, see section 7.3.2, “The Message/Partial subtype” of the standard.
The BinHex encoding originates from the Macintosh environment, and it takes the special properties of a Macintosh file into account. There, a file has two parts or “forks”: the “resource” fork holds machine code, and the “data” fork holds arbitrary data. For files from other systems, the data fork is usually empty.
I have not found a “definitive” definition of the format. My knowledge is based on two descriptions I found, one from Yves Lempereur and another from Peter Lewis. A similar description can be found in [RFC1741].
A BinHex file is a stream of characters, beginning and ending with a colon ‘:’; intermediate line breaks are to be ignored by the decoder. Each line but the last should be exactly 64 characters in length. The last line may be shorter, and in a special case can also be 65 characters long. The trailing colon must not stand alone, so if the input data ends on an output line boundary, the colon is appended to this line as 65th character. Thus a BinHex begins with a colon in the first column and ends with a colon not in the first column.
The line before the beginning of encoded data (before the initial ‘:’) should contain the following verbatim text:15
(This file must be converted with BinHex 4.0)
BinHex is another three-in-four encoding, and not surprisingly, another different character table is used (table 5). The documentation does not explicitely mention what is supposed to happen if the original input data does not have a multiple of three octets. But from reading between the lines, it looks like “unnecessary” characters (those that would result in equal signs in Base64 encoding) are not printed.
The encoded characters decode into a RLE-compressed bytestream, which must be handled in the next step (of course, decoding and decompressing are usually handled at the same time). A Run Length Encoding simply replaces multiple subsequent occurrences of one octet are replaced by the character, a special marker, and the repetition count. BinHex uses the marker 0x90 (octal 0220, decimal 128). The octet sequence 0xff 0x90 0x04 would decompress into four times 0xff. If the marker itself occurs, it must be “escaped” by the special sequence 0x90 0x00 (the marker with a repetition count of 0). Table 6 shows four more examples. Note the last example, where the marker itself is repeated.
(4454,2057)(1111,-2523) (1201,-961)(150,300) (1351,-961)(1050,300) (2401,-961)(150,300) (2551,-961)(600,300) (3151,-961)(600,300) (4051,-961)(600,300) (4651,-961)(600,300) (3751,-961)(300,300) (5251,-961)(300,300) (5253,-1713)(300,300) (5253,-2463)(300,300) (4501,-1711)(-1, 0)3300 (1201,-1711)( 0, 1)300 (1201,-1411)( 1, 0)3300 (4501,-2461)(-1, 0)3300 (1201,-2461)( 0, 1)300 (1201,-2161)( 1, 0)3300 (4501,-1411)(115.38462,0.00000)7( 1, 0) 57.692 (4501,-1711)(115.38462,0.00000)7( 1, 0) 57.692 (4501,-2161)(115.38462,0.00000)7( 1, 0) 57.692 (4501,-2461)(115.38462,0.00000)7( 1, 0) 57.692 (1276,-886)(0,0)[b]1214.4ptn (1876,-886)(0,0)[b]1214.4ptName (2476,-886)(0,0)[b]1214.4pt0 (2851,-886)(0,0)[b]1214.4ptType (3451,-886)(0,0)[b]1214.4ptAuth (4351,-886)(0,0)[b]1214.4ptDlen (4951,-886)(0,0)[b]1214.4ptRlen (1876,-586)(0,0)[b]1012.0ptn (2476,-586)(0,0)[b]1012.0pt1 (2851,-586)(0,0)[b]1012.0pt4 (3451,-586)(0,0)[b]1012.0pt4 (3901,-586)(0,0)[b]1012.0pt2 (4351,-586)(0,0)[b]1012.0pt4 (4951,-586)(0,0)[b]1012.0pt4 (5401,-586)(0,0)[b]1012.0pt2 (1126,-736)(0,0)[rb]1214.4ptHeader (1126,-1006)(0,0)[rb]1214.4ptSection (1276,-586)(0,0)[b]1012.0pt1 (5401,-886)(0,0)[b]1012.0ptHC (5402,-1337)(0,0)[b]1012.0pt2 (1128,-1488)(0,0)[rb]1214.4ptData (1128,-1758)(0,0)[rb]1214.4ptSection (3228,-1638)(0,0)[b]1214.4ptData Fork (3228,-1338)(0,0)[b]1012.0ptDlen (5403,-1638)(0,0)[b]1012.0ptDC (5402,-2087)(0,0)[b]1012.0pt2 (1128,-2238)(0,0)[rb]1214.4ptResource (1128,-2508)(0,0)[rb]1214.4ptSection (3228,-2388)(0,0)[b]1214.4ptResource Fork (3228,-2088)(0,0)[b]1012.0ptRlen (5403,-2388)(0,0)[b]1012.0ptRC (3901,-886)(0,0)[b]1012.0ptFlag
The decompression results in a data stream which consists of three parts, the header section, the data fork and the resource fork. Figure 5 shows how the sections are composed. The numbers above each item indicate its size in octets. The header has the following items:
After the header, at offset n+22, follow the Dlen octets of the data fork and a CRC checksum of the data fork (offset n+Dlen+22), then Rlen octets of the resource fork (offset n+Dlen+24) and a CRC checksum of the resource fork (offset n+Dlen+Rlen+24). Note that the CRCs are present even if the forks are empty.
The three CRC checksums are calculated as described in the following text, taken from Peter Lewis’ description:
BinHex 4.0 uses a 16-bit CRC with a 0x1021 seed. The general algorithm is to take data 1 bit at a time and process it through the following:
- Take the old CRC (use 0x0000 if there is no previous CRC) and shift it to the left by 1.
- Put the new data bit in the least significant position (right bit).
- If the bit shifted out in (1) was a 1 then xor the CRC with 0x1021.
- Loop back to (1) until all the data has been processed.
This is the sample file in BinHex. However, the encoder I used replaced the LF characters from the original file with CR characters. It probably noticed that the input file was plain text and reformatted it to Mac-style text, but I consider this a software bug. The assigned filename is “test.txt”.
(This file must be converted with BinHex 4.0)
:#&4&8e3Z9&K8!&4&@&4dG(Kd!!!!!!#X!!!!!+3j9'KTFb"TFb"K)(4PFh3JCQP
XC5"QEh)JD@aXGA0dFQ&dD@jR)(4SC5"fBA*TEh9c$@9ZBfpND@jR)'ePG'K[C(-
Z)%aPG#Gc)'eKDf8JG'KTFb"dCAKd)'a[EQGPFL"dD'&Z$68h)'*jG'9c)(4[)(G
bBA!JE'PZCA-JGfPdD#"#BA0P0M3JC'&dB5`JG'p[,Je(FQ9PG'PZCh-X)%CbB@j
V)&"TE'K[CQ9b$B0A!!!!:
The Quoted-Printable encoding is, like Base64, part of the MIME standard, described in [RFC1521]. It is not suitable for encoding arbitrary binary data, but is intended for “data that largely consists of octets that correspond to printable characters”. It is widely in use in countries with an extended character set, where characters like the German umlauts ‘ä’ or ‘ß’ are represented by non-ASCII characters with the highest bit set.
The essence of the encoding is that arbitrary octets can be represented by an equal sign ‘=’ followed by two hexadecimal digits. The equal sign itself, for example, is encoded as “=3D”.
Quoted-Printable enforces a maximum line length of 76 characters. Longer lines can be wrapped using soft line breaks. If the last character of an encoded line is an equal sign, the following line break is to be ignored.
It would indeed be possible to transfer arbitrary binary data using this encoding, but care must be taken with line breaks, which are converted from native format on the sender’s side and back into native format on the recipient’s side. However, the native representations may differ. But this alternative is hardly worth considering, since for arbitrary data, quoted-printable is substantially less effective than Base64.
Please refer to the original document, [RFC1521], for a complete discussion of the encoding.
Here is how the example file could look like in Quoted-Printable encoding.
This is a test file for =
illustrating the various
encoding methods=2e=20=
Let=27s make this text=
longer than
=357 bytes to wrap lines =
with Base64 data=2c too=2e
Greetings=2c Frank Pilhofer
This document was translated from LATEX by HEVEA.