When input is not a multiple of three bytes in size, pad null

characters instead of garbage. This makes output reproducible.

Taken from FreeBSD:
https://svnweb.freebsd.org/base?view=revision&revision=84715

Even though this is not demanded by POSIX, uuencode(1) in
FreeBSD, OpenBSD, macOS, and GNU, behaves that way.
This commit is contained in:
rin 2019-03-04 05:37:08 +00:00
parent 2342c5d283
commit 270ad452f4
2 changed files with 13 additions and 7 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: uuencode.5,v 1.12 2016/06/06 15:09:33 abhinav Exp $
.\" $NetBSD: uuencode.5,v 1.13 2019/03/04 05:37:08 rin Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)uuencode.format.5 8.2 (Berkeley) 1/12/94
.\"
.Dd June 2, 2016
.Dd March 4, 2019
.Dt UUENCODE 5
.Os
.Sh NAME
@ -118,11 +118,11 @@ backquote character \`).
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
will pad the remaining one or two bytes of data with null characters 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.
null padding will reflect the actual number of bytes encoded, making
it possible to convey how many bytes are null.
.Pp
The trailer line consists of
.Dq end

View File

@ -1,4 +1,4 @@
/* $NetBSD: uuencode.c,v 1.16 2014/09/06 18:58:35 dholland Exp $ */
/* $NetBSD: uuencode.c,v 1.17 2019/03/04 05:37:08 rin Exp $ */
/*-
* Copyright (c) 1983, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
#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 $");
__RCSID("$NetBSD: uuencode.c,v 1.17 2019/03/04 05:37:08 rin Exp $");
#endif
#endif /* not lint */
@ -165,6 +165,12 @@ encode(void)
if (putchar(ch) == EOF)
break;
for (p = buf; n > 0; n -= 3, p += 3) {
/* Pad with nulls if not a multiple of 3. */
if (n < 3) {
p[2] = '\0';
if (n < 2)
p[1] = '\0';
}
ch = *p >> 2;
ch = ENC(ch);
if (putchar(ch) == EOF)