2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 The FreeBSD Foundation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
37 extern int main_decode(int, char *[]);
38 extern int main_encode(int, char *[]);
39 extern int main_base64_decode(const char *);
40 extern int main_base64_encode(const char *, const char *);
41 extern int main_quotedprintable(int, char*[]);
43 static int search(const char *const);
44 static void usage_base64(bool);
45 static void version_base64(void);
46 static void base64_encode_or_decode(int, char *[]);
49 uuencode, uudecode, b64encode, b64decode, base64, qp
53 main(int argc, char *argv[])
55 const char *const progname = getprogname();
56 int coder = search(progname);
58 if (coder == -1 && argc > 1) {
61 coder = search(argv[0]);
66 main_encode(argc, argv);
70 main_decode(argc, argv);
73 base64_encode_or_decode(argc, argv);
76 main_quotedprintable(argc, argv);
80 "usage: %1$s <uuencode | uudecode> ...\n"
81 " %1$s <b64encode | b64decode> ...\n"
82 " %1$s <base64> ...\n"
90 search(const char *const progname)
92 #define DESIGNATE(item) [item] = #item
93 const char *const known[] = {
102 for (size_t i = 0; i < nitems(known); i++)
103 if (strcmp(progname, known[i]) == 0)
109 usage_base64(bool failure)
111 (void)fputs("usage: base64 [-w col | --wrap=col] "
112 "[-d | --decode] [FILE]\n"
114 " base64 --version\n", stderr);
115 exit(failure ? EXIT_FAILURE : EXIT_SUCCESS);
121 (void)fputs("FreeBSD base64\n", stderr);
126 base64_encode_or_decode(int argc, char *argv[])
130 const char *w = NULL;
131 enum { HELP, VERSION };
132 static const struct option opts[] =
134 {"decode", no_argument, NULL, 'd'},
135 {"ignore-garbage",no_argument, NULL, 'i'},
136 {"wrap", required_argument, NULL, 'w'},
137 {"help", no_argument, NULL, HELP},
138 {"version", no_argument, NULL, VERSION},
139 {NULL, no_argument, NULL, 0}
142 while ((ch = getopt_long(argc, argv, "diw:", opts, NULL)) != -1)
151 /* silently ignore */
158 usage_base64(ch == '?');
162 main_base64_decode(argv[optind]);
164 main_base64_encode(argv[optind], w);