mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2025-07-30 23:23:08 +03:00
basenc: A new program complementary to base64/base32
Encodes/decodes data in various common formats: base64,base64url,base32,base32,base16,base2,z85. Discussed here: https://lists.gnu.org/r/coreutils/2018-11/msg00014.html https://lists.gnu.org/r/coreutils/2018-12/msg00019.html * AUTHORS: Add basenc. * README: Reference the new program. * NEWS: Mention the new program. * build-aux/gen-lists-of-programs.sh: Add basenc. * doc/coreutils.texi: (basenc invocation): Document the new command. * man/.gitignore: Ignore the generated man page. * man/basenc.x: A new template, with few examples. * man/local.mk: Reference the new man page. * scripts/git-hooks/commit-msg: Allow basenc as program prefix. * src/.gitignore: Ignore the new binary. * src/basenc.c: (usage): Mention new options. (main): Handle new options. (isbase*, base*_length, base*_encode, base*_decode_ctx): Implement new encoding/decoding formats. * src/local.mk: Add new program. * tests/local.mk: Add new test. * tests/misc/basenc.pl: New tests. * tests/misc/help-version.sh (basenc_setup): use '--version' for default invocation (basenc errors with no parameters).
This commit is contained in:
1
AUTHORS
1
AUTHORS
@ -6,6 +6,7 @@ b2sum: Padraig Brady, Samuel Neves
|
|||||||
base32: Simon Josefsson
|
base32: Simon Josefsson
|
||||||
base64: Simon Josefsson
|
base64: Simon Josefsson
|
||||||
basename: David MacKenzie
|
basename: David MacKenzie
|
||||||
|
basenc: Simon Josefsson, Assaf Gordon
|
||||||
cat: Torbjorn Granlund, Richard M. Stallman
|
cat: Torbjorn Granlund, Richard M. Stallman
|
||||||
chcon: Russell Coker, Jim Meyering
|
chcon: Russell Coker, Jim Meyering
|
||||||
chgrp: David MacKenzie, Jim Meyering
|
chgrp: David MacKenzie, Jim Meyering
|
||||||
|
6
NEWS
6
NEWS
@ -44,6 +44,12 @@ GNU coreutils NEWS -*- outline -*-
|
|||||||
test now supports the '-N FILE' unary operator (like e.g. bash) to check
|
test now supports the '-N FILE' unary operator (like e.g. bash) to check
|
||||||
whether FILE exists and has been modified since it was last read.
|
whether FILE exists and has been modified since it was last read.
|
||||||
|
|
||||||
|
** New commands
|
||||||
|
|
||||||
|
basenc is added to complement existing base64,base32 commands,
|
||||||
|
and encodes and decodes printable text using various common encodings:
|
||||||
|
base64,base64url,base32,base32hex,base16,base2,z85.
|
||||||
|
|
||||||
|
|
||||||
* Noteworthy changes in release 8.30 (2018-07-01) [stable]
|
* Noteworthy changes in release 8.30 (2018-07-01) [stable]
|
||||||
|
|
||||||
|
18
README
18
README
@ -7,15 +7,15 @@ arbitrary limits.
|
|||||||
|
|
||||||
The programs that can be built with this package are:
|
The programs that can be built with this package are:
|
||||||
|
|
||||||
[ arch b2sum base32 base64 basename cat chcon chgrp chmod chown chroot cksum
|
[ arch b2sum base32 base64 basename basenc cat chcon chgrp chmod chown
|
||||||
comm coreutils cp csplit cut date dd df dir dircolors dirname du echo env
|
chroot cksum comm coreutils cp csplit cut date dd df dir dircolors dirname
|
||||||
expand expr factor false fmt fold groups head hostid hostname id install
|
du echo env expand expr factor false fmt fold groups head hostid hostname
|
||||||
join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl
|
id install join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp
|
||||||
nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd
|
mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx
|
||||||
readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum
|
pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum
|
||||||
sha512sum shred shuf sleep sort split stat stdbuf stty sum sync tac tail
|
sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty sum sync
|
||||||
tee test timeout touch tr true truncate tsort tty uname unexpand uniq
|
tac tail tee test timeout touch tr true truncate tsort tty uname unexpand
|
||||||
unlink uptime users vdir wc who whoami yes
|
uniq unlink uptime users vdir wc who whoami yes
|
||||||
|
|
||||||
See the file NEWS for a list of major changes in the current release.
|
See the file NEWS for a list of major changes in the current release.
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ normal_progs='
|
|||||||
b2sum
|
b2sum
|
||||||
base64
|
base64
|
||||||
base32
|
base32
|
||||||
|
basenc
|
||||||
basename
|
basename
|
||||||
cat
|
cat
|
||||||
chcon
|
chcon
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
* base32: (coreutils)base32 invocation. Base32 encode/decode data.
|
* base32: (coreutils)base32 invocation. Base32 encode/decode data.
|
||||||
* base64: (coreutils)base64 invocation. Base64 encode/decode data.
|
* base64: (coreutils)base64 invocation. Base64 encode/decode data.
|
||||||
* basename: (coreutils)basename invocation. Strip directory and suffix.
|
* basename: (coreutils)basename invocation. Strip directory and suffix.
|
||||||
|
* basenc: (coreutils)basenc invocation. Encoding/decoding of data.
|
||||||
* cat: (coreutils)cat invocation. Concatenate and write files.
|
* cat: (coreutils)cat invocation. Concatenate and write files.
|
||||||
* chcon: (coreutils)chcon invocation. Change SELinux CTX of files.
|
* chcon: (coreutils)chcon invocation. Change SELinux CTX of files.
|
||||||
* chgrp: (coreutils)chgrp invocation. Change file groups.
|
* chgrp: (coreutils)chgrp invocation. Change file groups.
|
||||||
@ -184,7 +185,7 @@ Free Documentation License''.
|
|||||||
@menu
|
@menu
|
||||||
* Introduction:: Caveats, overview, and authors
|
* Introduction:: Caveats, overview, and authors
|
||||||
* Common options:: Common options
|
* Common options:: Common options
|
||||||
* Output of entire files:: cat tac nl od base32 base64
|
* Output of entire files:: cat tac nl od base32 base64 basenc
|
||||||
* Formatting file contents:: fmt pr fold
|
* Formatting file contents:: fmt pr fold
|
||||||
* Output of parts of files:: head tail split csplit
|
* Output of parts of files:: head tail split csplit
|
||||||
* Summarizing files:: wc sum cksum b2sum md5sum sha1sum sha2
|
* Summarizing files:: wc sum cksum b2sum md5sum sha1sum sha2
|
||||||
@ -242,6 +243,7 @@ Output of entire files
|
|||||||
* od invocation:: Write files in octal or other formats
|
* od invocation:: Write files in octal or other formats
|
||||||
* base32 invocation:: Transform data into printable data
|
* base32 invocation:: Transform data into printable data
|
||||||
* base64 invocation:: Transform data into printable data
|
* base64 invocation:: Transform data into printable data
|
||||||
|
* basenc invocation:: Transform data into printable data
|
||||||
|
|
||||||
Formatting file contents
|
Formatting file contents
|
||||||
|
|
||||||
@ -1589,6 +1591,7 @@ in some way.
|
|||||||
* od invocation:: Write files in octal or other formats.
|
* od invocation:: Write files in octal or other formats.
|
||||||
* base32 invocation:: Transform data into printable data.
|
* base32 invocation:: Transform data into printable data.
|
||||||
* base64 invocation:: Transform data into printable data.
|
* base64 invocation:: Transform data into printable data.
|
||||||
|
* basenc invocation:: Transform data into printable data.
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
@node cat invocation
|
@node cat invocation
|
||||||
@ -2252,6 +2255,132 @@ to permit distorted data to be decoded.
|
|||||||
|
|
||||||
@exitstatus
|
@exitstatus
|
||||||
|
|
||||||
|
@node basenc invocation
|
||||||
|
@section @command{basenc}: Transform data into printable data
|
||||||
|
|
||||||
|
@pindex basenc
|
||||||
|
@cindex base32 encoding
|
||||||
|
|
||||||
|
@command{basenc} transforms data read from a file, or standard input,
|
||||||
|
into (or from) various common encoding forms. The encoded form uses
|
||||||
|
printable ASCII characters to represent binary data.
|
||||||
|
|
||||||
|
Synopses:
|
||||||
|
|
||||||
|
@example
|
||||||
|
basenc @var{encoding} [@var{option}]@dots{} [@var{file}]
|
||||||
|
basenc @var{encoding} --decode [@var{option}]@dots{} [@var{file}]
|
||||||
|
@end example
|
||||||
|
|
||||||
|
The @var{encoding} argument is required. If @var{file} is omitted,
|
||||||
|
reads input from stdin. The @option{-w/--wrap},@option{-i/--ignore-garbage},
|
||||||
|
@option{-d/--decode} options of this command are precisely the same as
|
||||||
|
for @command{base64}. @xref{base64 invocation}.
|
||||||
|
|
||||||
|
|
||||||
|
Supported @var{encoding}s are:
|
||||||
|
|
||||||
|
@table @samp
|
||||||
|
|
||||||
|
@item --base64
|
||||||
|
@opindex --base64
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) base64 form.
|
||||||
|
The format conforms to
|
||||||
|
@uref{https://tools.ietf.org/search/rfc4648#section-4, RFC 4648@hashchar{}4}.
|
||||||
|
Equivalent to the @command{base64} command.
|
||||||
|
|
||||||
|
@item --base64url
|
||||||
|
@opindex --base64url
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) file-and-url-safe
|
||||||
|
base64 form (using @samp{_} and @samp{-} instead of @samp{+} and @samp{/}).
|
||||||
|
The format conforms to
|
||||||
|
@uref{https://tools.ietf.org/search/rfc4648#section-5, RFC 4648@hashchar{}5}.
|
||||||
|
|
||||||
|
@item --base32
|
||||||
|
@opindex --base32
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) base32 form.
|
||||||
|
The encoded data uses the @samp{ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=} characters.
|
||||||
|
The format conforms to
|
||||||
|
@uref{https://tools.ietf.org/search/rfc4648#section-6, RFC 4648@hashchar{}6}.
|
||||||
|
Equivalent to the @command{base32} command.
|
||||||
|
|
||||||
|
@item --base32hex
|
||||||
|
@opindex --base32hex
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) Extended Hex Alphabet
|
||||||
|
base32 form. The encoded data uses the
|
||||||
|
@samp{0123456789ABCDEFGHIJKLMNOPQRSTUV=} characters. The format conforms to
|
||||||
|
@uref{https://tools.ietf.org/search/rfc4648#section-7, RFC 4648@hashchar{}7}.
|
||||||
|
|
||||||
|
@item --base16
|
||||||
|
@opindex --base16
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) base16 (hexadecimal)
|
||||||
|
form. The encoded data uses the @samp{0123456789ABCDEF} characters. The format
|
||||||
|
conforms to
|
||||||
|
@uref{https://tools.ietf.org/search/rfc4648#section-8, RFC 4648@hashchar{}8}.
|
||||||
|
|
||||||
|
@item --base2lsbf
|
||||||
|
@opindex --base2lsbf
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) binary string form
|
||||||
|
(@samp{0} and @samp{1}) with the @emph{least} significant bit of every byte
|
||||||
|
first.
|
||||||
|
|
||||||
|
@item --base2msbf
|
||||||
|
@opindex --base2msbf
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) binary string form
|
||||||
|
(@samp{0} and @samp{1}) with the @emph{most} significant bit of every byte
|
||||||
|
first.
|
||||||
|
|
||||||
|
@item --z85
|
||||||
|
@opindex --z85
|
||||||
|
Encode into (or decode from with @option{-d/--decode}) Z85 form
|
||||||
|
(a modified Ascii85 form). The encoded data uses the
|
||||||
|
@samp{0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU@
|
||||||
|
VWXYZ.-:+=^!/*?&<>()[]@{@}@@%$#}.
|
||||||
|
characters. The format conforms to
|
||||||
|
@uref{https://rfc.zeromq.org/spec:32/Z85/, ZeroMQ spec:32/Z85}.
|
||||||
|
|
||||||
|
When encoding with @option{--z85}, input length must be a multiple of 4;
|
||||||
|
when decoding with @option{--z85}, input length must be a multiple of 5.
|
||||||
|
|
||||||
|
@end table
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Encoding/decoding examples:
|
||||||
|
|
||||||
|
@example
|
||||||
|
$ printf '\376\117\202' | basenc --base64
|
||||||
|
/k+C
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base64url
|
||||||
|
_k-C
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base32
|
||||||
|
7ZHYE===
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base32hex
|
||||||
|
VP7O4===
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base16
|
||||||
|
FE4F82
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base2lsbf
|
||||||
|
011111111111001001000001
|
||||||
|
|
||||||
|
$ printf '\376\117\202' | basenc --base2msbf
|
||||||
|
111111100100111110000010
|
||||||
|
|
||||||
|
$ printf '\376\117\202\000' | basenc --z85
|
||||||
|
@@.FaC
|
||||||
|
|
||||||
|
$ printf 01010100 | basenc --base2msbf --decode
|
||||||
|
T
|
||||||
|
|
||||||
|
$ printf 01010100 | basenc --base2lsbf --decode
|
||||||
|
*
|
||||||
|
@end example
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@node Formatting file contents
|
@node Formatting file contents
|
||||||
@chapter Formatting file contents
|
@chapter Formatting file contents
|
||||||
|
1
man/.gitignore
vendored
1
man/.gitignore
vendored
@ -3,6 +3,7 @@ Makefile.in
|
|||||||
b2sum.1
|
b2sum.1
|
||||||
base32.1
|
base32.1
|
||||||
base64.1
|
base64.1
|
||||||
|
basenc.1
|
||||||
basename.1
|
basename.1
|
||||||
cat.1
|
cat.1
|
||||||
chgrp.1
|
chgrp.1
|
||||||
|
38
man/basenc.x
Normal file
38
man/basenc.x
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
'\" Copyright (C) 2018 Free Software Foundation, Inc.
|
||||||
|
'\"
|
||||||
|
'\" This is free software. You may redistribute copies of it under the terms
|
||||||
|
'\" of the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
|
||||||
|
'\" There is NO WARRANTY, to the extent permitted by law.
|
||||||
|
[NAME]
|
||||||
|
basenc \- Encode/decode data and print to standard output
|
||||||
|
[DESCRIPTION]
|
||||||
|
.\" Add any additional description here
|
||||||
|
[ENCODINGS EXAMPLES]
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base64
|
||||||
|
/k+C
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base64url
|
||||||
|
_k-C
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base32
|
||||||
|
7ZHYE===
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base32hex
|
||||||
|
VP7O4===
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base16
|
||||||
|
FE4F82
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base2lsbf
|
||||||
|
011111111111001001000001
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202' | basenc \-\-base2msbf
|
||||||
|
111111100100111110000010
|
||||||
|
|
||||||
|
$ printf '\\376\\117\\202\\000' | basenc \-\-z85
|
||||||
|
@.FaC
|
||||||
|
.RE
|
||||||
|
.fi
|
@ -69,6 +69,7 @@ man/b2sum.1: src/b2sum$(EXEEXT)
|
|||||||
man/base32.1: src/base32$(EXEEXT)
|
man/base32.1: src/base32$(EXEEXT)
|
||||||
man/base64.1: src/base64$(EXEEXT)
|
man/base64.1: src/base64$(EXEEXT)
|
||||||
man/basename.1: src/basename$(EXEEXT)
|
man/basename.1: src/basename$(EXEEXT)
|
||||||
|
man/basenc.1: src/basenc$(EXEEXT)
|
||||||
man/cat.1: src/cat$(EXEEXT)
|
man/cat.1: src/cat$(EXEEXT)
|
||||||
man/chcon.1: src/chcon$(EXEEXT)
|
man/chcon.1: src/chcon$(EXEEXT)
|
||||||
man/chgrp.1: src/chgrp$(EXEEXT)
|
man/chgrp.1: src/chgrp$(EXEEXT)
|
||||||
|
@ -14,10 +14,10 @@ $editor = "vi" if $? != 0 or $editor =~ /^\s*\z/;
|
|||||||
# Keywords allowed before the colon on the first line of a commit message:
|
# Keywords allowed before the colon on the first line of a commit message:
|
||||||
# program names and a few general category names.
|
# program names and a few general category names.
|
||||||
my @valid = qw(
|
my @valid = qw(
|
||||||
arch b2sum base32 base64 basename cat chcon chgrp chmod chown chroot cksum
|
arch b2sum base32 base64 basenc nbasename cat chcon chgrp chmod chown
|
||||||
comm cp csplit cut date dd df dir dircolors dirname du echo env expand
|
chroot cksum comm cp csplit cut date dd df dir dircolors dirname du echo
|
||||||
expr factor false fmt fold groups head hostid hostname id install
|
env expand expr factor false fmt fold groups head hostid hostname id
|
||||||
join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp
|
install join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp
|
||||||
mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf
|
mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf
|
||||||
ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum
|
ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum
|
||||||
sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty
|
sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty
|
||||||
|
1
src/.gitignore
vendored
1
src/.gitignore
vendored
@ -5,6 +5,7 @@ arch
|
|||||||
b2sum
|
b2sum
|
||||||
base32
|
base32
|
||||||
base64
|
base64
|
||||||
|
basenc
|
||||||
basename
|
basename
|
||||||
cat
|
cat
|
||||||
chcon
|
chcon
|
||||||
|
922
src/basenc.c
922
src/basenc.c
File diff suppressed because it is too large
Load Diff
@ -97,6 +97,7 @@ LDADD = src/libver.a lib/libcoreutils.a $(LIBINTL) lib/libcoreutils.a
|
|||||||
src_arch_LDADD = $(LDADD)
|
src_arch_LDADD = $(LDADD)
|
||||||
src_base64_LDADD = $(LDADD)
|
src_base64_LDADD = $(LDADD)
|
||||||
src_base32_LDADD = $(LDADD)
|
src_base32_LDADD = $(LDADD)
|
||||||
|
src_basenc_LDADD = $(LDADD)
|
||||||
src_basename_LDADD = $(LDADD)
|
src_basename_LDADD = $(LDADD)
|
||||||
src_cat_LDADD = $(LDADD)
|
src_cat_LDADD = $(LDADD)
|
||||||
src_chcon_LDADD = $(LDADD)
|
src_chcon_LDADD = $(LDADD)
|
||||||
@ -414,6 +415,8 @@ src_base64_SOURCES = src/basenc.c
|
|||||||
src_base64_CPPFLAGS = -DBASE_TYPE=64 $(AM_CPPFLAGS)
|
src_base64_CPPFLAGS = -DBASE_TYPE=64 $(AM_CPPFLAGS)
|
||||||
src_base32_SOURCES = src/basenc.c
|
src_base32_SOURCES = src/basenc.c
|
||||||
src_base32_CPPFLAGS = -DBASE_TYPE=32 $(AM_CPPFLAGS)
|
src_base32_CPPFLAGS = -DBASE_TYPE=32 $(AM_CPPFLAGS)
|
||||||
|
src_basenc_SOURCES = src/basenc.c
|
||||||
|
src_basenc_CPPFLAGS = -DBASE_TYPE=42 $(AM_CPPFLAGS)
|
||||||
|
|
||||||
src_ginstall_CPPFLAGS = -DENABLE_MATCHPATHCON=1 $(AM_CPPFLAGS)
|
src_ginstall_CPPFLAGS = -DENABLE_MATCHPATHCON=1 $(AM_CPPFLAGS)
|
||||||
|
|
||||||
|
@ -278,6 +278,7 @@ all_tests = \
|
|||||||
tests/misc/cat-self.sh \
|
tests/misc/cat-self.sh \
|
||||||
tests/misc/base64.pl \
|
tests/misc/base64.pl \
|
||||||
tests/misc/basename.pl \
|
tests/misc/basename.pl \
|
||||||
|
tests/misc/basenc.pl \
|
||||||
tests/misc/close-stdout.sh \
|
tests/misc/close-stdout.sh \
|
||||||
tests/misc/chroot-fail.sh \
|
tests/misc/chroot-fail.sh \
|
||||||
tests/misc/comm.pl \
|
tests/misc/comm.pl \
|
||||||
|
284
tests/misc/basenc.pl
Executable file
284
tests/misc/basenc.pl
Executable file
@ -0,0 +1,284 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
# Exercise basenc.
|
||||||
|
|
||||||
|
# Copyright (C) 2006-2018 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# This test exercises the various encoding (other than base64/32).
|
||||||
|
# It also does not test the general options (e.g. --wrap), as that code is
|
||||||
|
# shared and tested in base64.
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
|
||||||
|
(my $program_name = $0) =~ s|.*/||;
|
||||||
|
my $prog = 'basenc';
|
||||||
|
|
||||||
|
# Turn off localization of executable's output.
|
||||||
|
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
|
||||||
|
|
||||||
|
|
||||||
|
my $base64_in = "\x54\x0f\xdc\xf0\x0f\xaf\x4a";
|
||||||
|
my $base64_out = "VA/c8A+vSg==";
|
||||||
|
my $base64url_out = $base64_out;
|
||||||
|
$base64url_out =~ y|+/|-_|;
|
||||||
|
my $base64url_out_nl = $base64url_out;
|
||||||
|
$base64url_out_nl =~ s/(..)/\1\n/g; # add newline every two characters
|
||||||
|
|
||||||
|
|
||||||
|
my $base32_in = "\xfd\xd8\x07\xd1\xa5";
|
||||||
|
my $base32_out = "7XMAPUNF";
|
||||||
|
my $x = $base32_out;
|
||||||
|
$x =~ y|ABCDEFGHIJKLMNOPQRSTUVWXYZ234567|0123456789ABCDEFGHIJKLMNOPQRSTUV|;
|
||||||
|
my $base32hex_out = $x;
|
||||||
|
|
||||||
|
# base32 with padding and newline
|
||||||
|
my $base32_in2 = "\xFF\x00";
|
||||||
|
my $base32_out2 = "74AA====";
|
||||||
|
$x = $base32_out2;
|
||||||
|
$x =~ y|ABCDEFGHIJKLMNOPQRSTUVWXYZ234567|0123456789ABCDEFGHIJKLMNOPQRSTUV|;
|
||||||
|
my $base32hex_out2 = $x;
|
||||||
|
my $base32hex_out2_nl = $x;
|
||||||
|
$base32hex_out2_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters
|
||||||
|
|
||||||
|
my $base16_in = "\xfd\xd8\x07\xd1\xa5";
|
||||||
|
my $base16_out = "FDD807D1A5";
|
||||||
|
|
||||||
|
my $z85_in = "\x86\x4F\xD2\x6F\xB5\x59\xF7\x5B";
|
||||||
|
my $z85_out = 'HelloWorld';
|
||||||
|
|
||||||
|
my $base2lsbf_ab = "1000011001000110";
|
||||||
|
my $base2lsbf_ab_nl = $base2lsbf_ab;
|
||||||
|
$base2lsbf_ab_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters
|
||||||
|
my $base2msbf_ab = "0110000101100010";
|
||||||
|
my $base2msbf_ab_nl = $base2msbf_ab;
|
||||||
|
$base2msbf_ab_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters
|
||||||
|
|
||||||
|
my $try_help = "Try '$prog --help' for more information.\n";
|
||||||
|
|
||||||
|
my @Tests =
|
||||||
|
(
|
||||||
|
# These are mainly for higher coverage
|
||||||
|
['help', '--help', {IN=>''}, {OUT=>""}, {OUT_SUBST=>'s/.*//sm'}],
|
||||||
|
|
||||||
|
# Typical message is " unrecognized option '--foobar'", but on
|
||||||
|
# Open/NetBSD it is " unknown option -- foobar".
|
||||||
|
['error', '--foobar', {IN=>''}, {OUT=>""}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: foobar\n" . $try_help },
|
||||||
|
{ERR_SUBST=>"s/(unrecognized|unknown) option [-' ]*foobar[' ]*/foobar/"}],
|
||||||
|
|
||||||
|
['noenc', '', {IN=>''}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: missing encoding type\n" . $try_help }],
|
||||||
|
|
||||||
|
## TODO: Fix to "B" (this is a bug in the original base64.c:1064
|
||||||
|
['extra', '--base64 A B', {IN=>''}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: extra operand 'A'\n" . $try_help}],
|
||||||
|
|
||||||
|
|
||||||
|
['empty1', '--base64', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty2', '--base64url', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty3', '--base32', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty4', '--base32hex', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty5', '--base16', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty6', '--base2msbf', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty7', '--base2lsbf', {IN=>''}, {OUT=>""}],
|
||||||
|
['empty8', '--z85', {IN=>''}, {OUT=>""}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['b64_1', '--base64', {IN=>$base64_in}, {OUT=>$base64_out}],
|
||||||
|
['b64_2', '--base64 -d', {IN=>$base64_out}, {OUT=>$base64_in}],
|
||||||
|
['b64_3', '--base64 -d -i', {IN=>'&'.$base64_out},{OUT=>$base64_in}],
|
||||||
|
|
||||||
|
['b64u_1', '--base64url', {IN=>$base64_in}, {OUT=>$base64url_out}],
|
||||||
|
['b64u_2', '--base64url -d', {IN=>$base64url_out}, {OUT=>$base64_in}],
|
||||||
|
['b64u_3', '--base64url -di', {IN=>'&'.$base64url_out} , {OUT=>$base64_in}],
|
||||||
|
['b64u_4', '--base64url -di', {IN=>'/'.$base64url_out.'+'},{OUT=>$base64_in}],
|
||||||
|
['b64u_5', '--base64url -d', {IN=>$base64url_out_nl}, {OUT=>$base64_in}],
|
||||||
|
['b64u_6', '--base64url -di', {IN=>$base64url_out_nl}, {OUT=>$base64_in}],
|
||||||
|
# ensure base64url fails to decode base64 input with "+" and "/"
|
||||||
|
['b64u_7', '--base64url -d', {IN=>$base64_out},
|
||||||
|
{EXIT=>1}, {ERR=>"$prog: invalid input\n"}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['b32_1', '--base32', {IN=>$base32_in}, {OUT=>$base32_out}],
|
||||||
|
['b32_2', '--base32 -d', {IN=>$base32_out}, {OUT=>$base32_in}],
|
||||||
|
['b32_3', '--base32 -d -i', {IN=>'&'.$base32_out},{OUT=>$base32_in}],
|
||||||
|
['b32_4', '--base32', {IN=>$base32_in2}, {OUT=>$base32_out2}],
|
||||||
|
['b32_5', '--base32 -d', {IN=>$base32_out2}, {OUT=>$base32_in2}],
|
||||||
|
['b32_6', '--base32 -d -i', {IN=>$base32_out2}, {OUT=>$base32_in2}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['b32h_1', '--base32hex', {IN=>$base32_in}, {OUT=>$base32hex_out}],
|
||||||
|
['b32h_2', '--base32hex -d', {IN=>$base32hex_out}, {OUT=>$base32_in}],
|
||||||
|
['b32h_3', '--base32hex -d -i', {IN=>'/'.$base32hex_out}, {OUT=>$base32_in}],
|
||||||
|
['b32h_4', '--base32hex -d -i', {IN=>'W'.$base32hex_out}, {OUT=>$base32_in}],
|
||||||
|
['b32h_5', '--base32hex -d', {IN=>$base32hex_out.'W'}, , {OUT=>$base32_in},
|
||||||
|
{EXIT=>1}, {ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b32h_6', '--base32hex -d', {IN=>$base32hex_out.'/'}, {OUT=>$base32_in},
|
||||||
|
{EXIT=>1}, {ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b32h_7', '--base32hex', {IN=>$base32_in2}, {OUT=>$base32hex_out2}],
|
||||||
|
['b32h_8', '--base32hex -d', {IN=>$base32hex_out2}, {OUT=>$base32_in2}],
|
||||||
|
['b32h_9', '--base32hex -di', {IN=>$base32hex_out2}, {OUT=>$base32_in2}],
|
||||||
|
['b32h_10', '--base32hex -d', {IN=>$base32hex_out2_nl}, {OUT=>$base32_in2}],
|
||||||
|
['b32h_11', '--base32hex -di', {IN=>$base32hex_out2_nl}, {OUT=>$base32_in2}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['b16_1', '--base16', {IN=>$base16_in}, {OUT=>$base16_out}],
|
||||||
|
['b16_2', '--base16 -d', {IN=>$base16_out}, {OUT=>$base16_in}],
|
||||||
|
['b16_3', '--base16 -d -i', {IN=>'&'. $base16_out}, {OUT=>$base16_in}],
|
||||||
|
['b16_4', '--base16 -d -i', {IN=>$base16_out.'G'}, {OUT=>$base16_in}],
|
||||||
|
['b16_5', '--base16 -d', {IN=>'.'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b16_6', '--base16 -d', {IN=>'='}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b16_7', '--base16 -d', {IN=>'G'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b16_8', '--base16 -d', {IN=>"AB\nCD"}, {OUT=>"\xAB\xCD"}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['b2m_1', '--base2m', {IN=>"\xC1"}, {OUT=>"11000001"}],
|
||||||
|
['b2m_2', '--base2m -d', {IN=>'11000001'}, {OUT=>"\xC1"}],
|
||||||
|
['b2m_3', '--base2m -d', {IN=>"110\n00001"}, {OUT=>"\xC1"}],
|
||||||
|
['b2m_4', '--base2m -di', {IN=>"110x00001"}, {OUT=>"\xC1"}],
|
||||||
|
['b2m_5', '--base2m -d', {IN=>"110x00001"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2m_6', '--base2m -d', {IN=>"11000001x"}, {OUT=>"\xC1"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2m_7', '--base2m -d', {IN=>"1"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2m_8', '--base2m -d', {IN=>"1000100"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2m_9', '--base2m -d', {IN=>"100010000000000"}, {OUT=>"\x88"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2m_10','--base2m', {IN=>"ab"}, {OUT=>$base2msbf_ab}],
|
||||||
|
['b2m_11','--base2m -d', {IN=>$base2msbf_ab}, {OUT=>"ab"}],
|
||||||
|
['b2m_12','--base2m -d', {IN=>$base2msbf_ab_nl}, {OUT=>"ab"}],
|
||||||
|
|
||||||
|
|
||||||
|
['b2l_1', '--base2l', {IN=>"\x83"}, {OUT=>"11000001"}],
|
||||||
|
['b2l_2', '--base2l -d', {IN=>'11000001'}, {OUT=>"\x83"}],
|
||||||
|
['b2l_3', '--base2l -d', {IN=>"110\n00001"}, {OUT=>"\x83"}],
|
||||||
|
['b2l_4', '--base2l -di', {IN=>"110x00001"}, {OUT=>"\x83"}],
|
||||||
|
['b2l_5', '--base2l -d', {IN=>"110x00001"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2l_6', '--base2l -d', {IN=>"11000001x"}, {OUT=>"\x83"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2l_7', '--base2l -d', {IN=>"1"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2l_8', '--base2l -d', {IN=>"1000100"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2l_9', '--base2l -d', {IN=>"100010000000000"}, {OUT=>"\x11"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['b2l_10','--base2l', {IN=>"ab"}, {OUT=>$base2lsbf_ab}],
|
||||||
|
['b2l_11','--base2l -d', {IN=>$base2lsbf_ab}, {OUT=>"ab"}],
|
||||||
|
['b2l_12','--base2l -d', {IN=>$base2lsbf_ab_nl}, {OUT=>"ab"}],
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
['z85_1', '--z85', {IN=>$z85_in}, {OUT=>$z85_out}],
|
||||||
|
['z85_2', '--z85 -d', {IN=>$z85_out}, {OUT=>$z85_in}],
|
||||||
|
['z85_3', '--z85 -d -i', {IN=>'~'. $z85_out}, {OUT=>$z85_in}],
|
||||||
|
['z85_4', '--z85 -d -i', {IN=>' '. $z85_out}, {OUT=>$z85_in}],
|
||||||
|
['z85_5', '--z85 -d', {IN=>'%j$qP'}, {OUT=>"\xFF\xDD\xBB\x99"}],
|
||||||
|
['z85_6', '--z85 -d -i', {IN=>'%j~$qP'}, {OUT=>"\xFF\xDD\xBB\x99"}],
|
||||||
|
|
||||||
|
# z85 encoding require input to be multiple of 5 octets
|
||||||
|
['z85_7', '--z85 -d', {IN=>'hello'}, {OUT=>"5jXu"}],
|
||||||
|
['z85_8', '--z85 -d', {IN=>'helloX'}, {OUT=>"5jXu"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_9', '--z85 -d', {IN=>"he\nl\nlo"}, {OUT=>"5jXu"}],
|
||||||
|
|
||||||
|
# Invalid input characters (space ~ ")
|
||||||
|
['z85_10', '--z85 -d', {IN=>' j$qP'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_11', '--z85 -d', {IN=>'%j$q~'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_12', '--z85 -d', {IN=>'%j$"P'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
|
||||||
|
# Invalid length (binary input must be a multiple of 4 octets,
|
||||||
|
# z85-encoded input must be a multiple of 5 octets)
|
||||||
|
['z85_20', '--z85', {IN=>'A'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}],
|
||||||
|
['z85_21', '--z85', {IN=>'AB'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}],
|
||||||
|
['z85_22', '--z85', {IN=>'ABC'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}],
|
||||||
|
['z85_23', '--z85', {IN=>'ABCD'}, {OUT=>'k%^}b'}],
|
||||||
|
['z85_24', '--z85', {IN=>'ABCDE'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}],
|
||||||
|
|
||||||
|
['z85_30', '--z85 -d', {IN=>'A'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_31', '--z85 -d', {IN=>'AB'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_32', '--z85 -d', {IN=>'ABC'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_33', '--z85 -d', {IN=>'ABCD'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_34', '--z85 -d', {IN=>'ABCDE'}, {OUT=>"\x71\x61\x9e\xb6"}],
|
||||||
|
['z85_35', '--z85 -d', {IN=>'ABCDEF'},{OUT=>"\x71\x61\x9e\xb6"},
|
||||||
|
{EXIT=>1}, {ERR=>"$prog: invalid input\n"}],
|
||||||
|
|
||||||
|
# largest possible value
|
||||||
|
['z85_40', '--z85', {IN=>"\xFF\xFF\xFF\xFF"},{OUT=>"%nSc0"}],
|
||||||
|
['z85_41', '--z85 -d', {IN=>"%nSc0"}, {OUT=>"\xFF\xFF\xFF\xFF"}],
|
||||||
|
# Invalid encoded data - will decode to more than 0xFFFFFFFF
|
||||||
|
['z85_42', '--z85 -d', {IN=>"%nSc1"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_43', '--z85 -d', {IN=>"%nSd0"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_44', '--z85 -d', {IN=>"%nTc0"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_45', '--z85 -d', {IN=>"%oSc0"}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_46', '--z85 -d', {IN=>'$nSc0'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
['z85_47', '--z85 -d', {IN=>'#0000'}, {EXIT=>1},
|
||||||
|
{ERR=>"$prog: invalid input\n"}],
|
||||||
|
);
|
||||||
|
|
||||||
|
# Prepend the command line argument and append a newline to end
|
||||||
|
# of each expected 'OUT' string.
|
||||||
|
my $t;
|
||||||
|
|
||||||
|
Test:
|
||||||
|
foreach $t (@Tests)
|
||||||
|
{
|
||||||
|
foreach my $e (@$t)
|
||||||
|
{
|
||||||
|
ref $e && ref $e eq 'HASH' && defined $e->{OUT_SUBST}
|
||||||
|
and next Test;
|
||||||
|
}
|
||||||
|
|
||||||
|
push @$t, {OUT_SUBST=>'s/\n$//s'};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
my $save_temps = $ENV{DEBUG};
|
||||||
|
my $verbose = $ENV{VERBOSE};
|
||||||
|
|
||||||
|
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
|
||||||
|
|
||||||
|
exit $fail;
|
@ -184,6 +184,7 @@ defid_setup () { args=t; }
|
|||||||
basename_setup () { args=$tmp_in; }
|
basename_setup () { args=$tmp_in; }
|
||||||
dirname_setup () { args=$tmp_in; }
|
dirname_setup () { args=$tmp_in; }
|
||||||
expr_setup () { args=foo; }
|
expr_setup () { args=foo; }
|
||||||
|
basenc_setup () { args=--version; }
|
||||||
|
|
||||||
# Punt, in case GNU 'id' hasn't been installed yet.
|
# Punt, in case GNU 'id' hasn't been installed yet.
|
||||||
groups_setup () { args=--version; }
|
groups_setup () { args=--version; }
|
||||||
|
Reference in New Issue
Block a user