Porting uuencode/uudecode from NetBSD

Lionel: I fixed small mistakes in the mi file, typos, missing keywords,
        and whitespace fixes.

Change-Id: If0c04b923af328838f2d0950e189bf28995bc0f0
This commit is contained in:
Sky Liu
2014-09-07 10:05:15 +08:00
committed by Lionel Sambuc
parent 31b808b8fa
commit 2fde3a4846
17 changed files with 884 additions and 904 deletions

View File

@@ -0,0 +1,8 @@
# $NetBSD: Makefile,v 1.5 1995/12/05 03:02:31 jtc Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= uuencode
MAN= uuencode.1 uuencode.5
MLINKS= uuencode.1 uudecode.1
.include <bsd.prog.mk>

175
usr.bin/uuencode/uuencode.1 Normal file
View File

@@ -0,0 +1,175 @@
.\" $NetBSD: uuencode.1,v 1.26 2014/09/06 21:21:36 wiz Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)uuencode.1 8.1 (Berkeley) 6/6/93
.\"
.Dd September 6, 2014
.Dt UUENCODE 1
.Os
.Sh NAME
.Nm uuencode ,
.Nm uudecode
.Nd encode/decode a binary file
.Sh SYNOPSIS
.Nm
.Op Fl m
.Op Ar inputfile
.Ar headername
.Nm uudecode
.Op Fl m
.Op Fl p | Fl o Ar outputfile
.Op Ar encoded-file ...
.Sh DESCRIPTION
.Nm
and
.Nm uudecode
are used to transmit binary files over transmission mediums
that do not support other than simple
.Tn ASCII
data.
.Pp
The following options are available:
.Bl -tag -width ".Fl m"
.It Fl m
Use base64 encoding.
For
.Nm ,
the historical uuencode algorithm is the default.
For
.Nm uudecode ,
by default the encoding is automatically detected.
.It Fl o Ar outputfile
.Po Nm uudecode No only . Pc
Send the decoded output data to
.Ar outputfile .
By default,
.Nm uudecode
uses the
.Ar headername
recorded in the header of the encoded data stream.
.It Fl p
.Po Nm uudecode No only . Pc
Write the decoded file to standard output instead of to a file.
.El
.Pp
.Nm
reads
.Ar inputfile
(or by default the standard input) and writes an encoded version
to (always) the standard output.
The encoding uses only printing
.Tn ASCII
characters suitable for text-only transport media.
The string
.Ar headername
is inserted into the output header as the
.Ar outputfile
to use at
.Nm uudecode
time.
The header also includes the mode (permissions) of the file.
.Pp
.Nm uudecode
transforms
.Em uuencoded
files (or by default, the standard input) into the original form.
The resulting file is named
.Ar headername
as recorded in the encoded file,
or as specified by the
.Fl o
option,
and will have the mode of the original file except that setuid
and execute bits are not retained.
If the
.Fl p
option is specified, or if the output file name is given as
.Pa /dev/stdout ,
then the data will be written to the standard output
instead of to a named file.
.Nm uudecode
ignores any leading and trailing lines.
.Pp
The encoded form of the file is expanded by 35%.
Every 3 bytes become 4 plus control information.
.Sh EXIT STATUS
The
.Nm uudecode
and
.Nm
utilities exits 0 on success, and \*[Gt]0 if an error occurs.
.Sh EXAMPLES
The following example packages up a source tree, compresses it,
uuencodes it and mails it to a user on another system.
.Pp
.Bd -literal -offset indent -compact
tar czf \- src_tree \&| uuencode src_tree.tgz \&| mail user@example.com
.Ed
.Pp
On the other system, if the user saves the mail to the file
.Pa temp ,
the following example creates the file
.Pa src_tree.tgz
and extracts it to make a copy of the original tree.
.Pp
.Bd -literal -offset indent -compact
uudecode temp
tar xzf src_tree.tgz
.Ed
.Sh SEE ALSO
.Xr gzip 1 ,
.Xr mail 1 ,
.Xr tar 1 ,
.\".Xr uucp 1 ,
.Xr uuencode 5
.Sh STANDARDS
The
.Nm uudecode
and
.Nm
utilities conform to
.St -p1003.1-2008 .
.Sh HISTORY
The
.Nm uudecode
and
.Nm
utilities appeared in
.Bx 4.0 .
.Sh SECURITY CONSIDERATIONS
When using
.Nm uudecode
with files coming from dubious sources,
always either explicitly pass the
.Fl o
option or check the header (the first line) of the encoded file for
safety.
Blindly using a
.Ar headername
from a hostile source can overwrite important files.

146
usr.bin/uuencode/uuencode.5 Normal file
View File

@@ -0,0 +1,146 @@
.\" $NetBSD: uuencode.5,v 1.11 2008/08/25 09:36:24 wiz Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)uuencode.format.5 8.2 (Berkeley) 1/12/94
.\"
.Dd April 9, 1997
.Dt UUENCODE 5
.Os
.Sh NAME
.Nm uuencode
.Nd format of an encoded uuencode file
.Sh DESCRIPTION
Files output by
.Xr uuencode 1
consist of a header line,
followed by a number of body lines,
and a trailer line.
The
.Xr uudecode 1
command
will ignore any lines preceding the header or
following the trailer.
Lines preceding a header must not, of course,
look like a header.
.Pp
The header line starts with the word
.Dq begin ,
a space,
a file mode (in octal),
a space,
and finally a string which names the file being encoded.
.Pp
The central engine of
.Xr uuencode 1
is a six-bit encoding function which outputs an
.Tn ASCII
character.
The six bits to be encoded are treated as a small integer and added
with the
.Tn ASCII
value for the space character (octal 40).
The result is a printable
.Tn ASCII
character.
In the case where all six bits to be encoded are zero,
the
.Tn ASCII
backquote character \` (octal 140) is emitted instead of what
would normally be a space.
.Pp
The body of an encoded file consists of one or more lines,
each of which may be a maximum of 86 characters long (including the trailing
newline).
Each line represents an encoded chunk of data from the input file and begins
with a byte count,
followed by encoded bytes,
followed by a newline.
.Pp
The byte count is a six-bit integer encoded with the above function,
representing the number of bytes encoded in the rest of the line.
The method used to encode the data expands its size by
133% (described below).
Therefore it is important to note that the byte count describes the size of
the chunk of data before it is encoded, not afterwards.
The six bit size of this number effectively limits the number of bytes
that can be encoded in each line to a maximum of 63.
While
.Xr uuencode 1
will not encode more than 45 bytes per line,
.Xr uudecode 1
will tolerate the maximum line size.
.Pp
The remaining characters in the line represent the data of the input
file encoded as follows.
Input data are broken into groups of three eight-bit bytes,
which are then interpreted together as a 24-bit block.
The first bit of the block is the highest order bit of the first character,
and the last is the lowest order bit of the third character.
This block is then broken into four six-bit integers which are encoded one by
one starting from the first bit of the block.
The result is a four character
.Tn ASCII
string for every three bytes of input data.
.Pp
Encoded lines of data continue in this manner until the input file is
exhausted.
The end of the body is signaled by an encoded line with a byte count
of zero (the
.Tn ASCII
backquote character \`).
.Pp
Obviously, not every input file will be a multiple of three bytes in size.
In these cases,
.Xr uuencode 1
will pad the remaining one or two bytes of data with garbage bytes until
a three byte group is created.
The byte count in a line containing
garbage padding will reflect the actual number of bytes encoded, making
it possible to convey how many bytes are garbage.
.Pp
The trailer line consists of
.Dq end
on a line by itself.
.Sh SEE ALSO
.Xr mail 1 ,
.Xr uucp 1 ,
.Xr uudecode 1 ,
.Xr uuencode 1 ,
.Xr ascii 7
.Sh HISTORY
The
.Nm
file format appeared in
.Bx 4.0 .
.Sh BUGS
The interpretation of the
.Nm
format relies on properties of the
.Tn ASCII
character set and may not work correctly on non-ASCII systems.

202
usr.bin/uuencode/uuencode.c Normal file
View File

@@ -0,0 +1,202 @@
/* $NetBSD: uuencode.c,v 1.16 2014/09/06 18:58:35 dholland Exp $ */
/*-
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)uuencode.c 8.2 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: uuencode.c,v 1.16 2014/09/06 18:58:35 dholland Exp $");
#endif
#endif /* not lint */
/*
* uuencode [input] output
*
* Encode a file so it can be mailed to a remote system.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <locale.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void encode(void);
static void base64_encode(void);
__dead static void usage(void);
int
main(int argc, char *argv[])
{
struct stat sb;
int base64, ch, mode;
mode = 0;
base64 = 0;
setlocale(LC_ALL, "");
setprogname(argv[0]);
while ((ch = getopt(argc, argv, "m")) != -1) {
switch(ch) {
case 'm':
base64 = 1;
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
switch(argc) {
case 2: /* optional first argument is input file */
if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
err(1, "%s", *argv);
#define RWX (S_IRWXU|S_IRWXG|S_IRWXO)
mode = sb.st_mode & RWX;
++argv;
break;
case 1:
#define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
mode = RW & ~umask(RW);
break;
case 0:
default:
usage();
}
if (base64) {
(void)printf("begin-base64 %o %s\n", mode, *argv);
base64_encode();
(void)printf("====\n");
} else {
(void)printf("begin %o %s\n", mode, *argv);
encode();
(void)printf("end\n");
}
if (ferror(stdout))
err(1, "write error");
exit(0);
}
/* ENC is the basic 1 character encoding function to make a char printing */
#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
/*
* copy from in to out, encoding in base64 as you go along.
*/
static void
base64_encode(void)
{
/*
* Output must fit into 80 columns, chunks come in 4, leave 1.
*/
#define GROUPS ((70 / 4) - 1)
unsigned char buf[3];
char buf2[sizeof(buf) * 2 + 1];
size_t n;
int rv, sequence;
sequence = 0;
while ((n = fread(buf, 1, sizeof(buf), stdin))) {
++sequence;
rv = b64_ntop(buf, n, buf2, (sizeof(buf2) / sizeof(buf2[0])));
if (rv == -1)
errx(1, "b64_ntop: error encoding base64");
printf("%s%s", buf2, (sequence % GROUPS) ? "" : "\n");
}
if (sequence % GROUPS)
printf("\n");
}
/*
* copy from in to out, encoding as you go along.
*/
static void
encode(void)
{
int ch, n;
char *p;
char buf[80];
while ((n = fread(buf, 1, 45, stdin)) > 0) {
ch = ENC(n);
if (putchar(ch) == EOF)
break;
for (p = buf; n > 0; n -= 3, p += 3) {
ch = *p >> 2;
ch = ENC(ch);
if (putchar(ch) == EOF)
break;
ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
ch = ENC(ch);
if (putchar(ch) == EOF)
break;
ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
ch = ENC(ch);
if (putchar(ch) == EOF)
break;
ch = p[2] & 077;
ch = ENC(ch);
if (putchar(ch) == EOF)
break;
}
if (putchar('\n') == EOF)
break;
}
if (ferror(stdin))
err(1, "read error.");
ch = ENC('\0');
(void)putchar(ch);
(void)putchar('\n');
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: %s [-m] [inputfile] headername > encodedfile\n",
getprogname());
exit(1);
}