1 /* base16enc -- an encoder for base16
3 Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
48 /* The number of bytes read in each iteration, we do one line at a time: */
51 /* The *maximum* size of an encoded chunk: */
52 #define ENCODED_SIZE BASE16_ENCODE_LENGTH(CHUNK_SIZE)
55 * Reads bytes from standard input and writes base64-encoded
59 main(int argc UNUSED, char **argv UNUSED)
63 _setmode(0, O_BINARY);
66 /* There is no context to initialize. */
70 /* "buffer" will hold the bytes from disk: */
71 uint8_t buffer[CHUNK_SIZE];
72 /* "result" will hold bytes before output: */
73 uint8_t result[ENCODED_SIZE + 1];
74 unsigned nbytes; /* Number of bytes read from stdin */
75 int encoded_bytes; /* Total number of bytes encoded per iteration */
77 nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
79 /* We overwrite result with more data */
80 base16_encode_update(result, nbytes, buffer);
81 encoded_bytes = BASE16_ENCODE_LENGTH(nbytes);
82 result[encoded_bytes++] = '\n';
84 if (nbytes < CHUNK_SIZE)
88 werror ("Error reading file: %s\n", strerror(errno));
91 if (!write_string (stdout, encoded_bytes, result)
92 || fflush (stdout) != 0)
94 werror ("Error writing file: %s\n", strerror(errno));
99 /* The result vector is printed */
100 if (!write_string(stdout,encoded_bytes, result))
102 werror ("Error writing file: %s\n", strerror(errno));