1 /* base16enc -- an encoder for base16
3 * Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 * License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with the nettle library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
37 /* The number of bytes read in each iteration, we do one line at a time: */
40 /* The *maximum* size of an encoded chunk: */
41 #define ENCODED_SIZE BASE16_ENCODE_LENGTH(CHUNK_SIZE)
44 * Reads bytes from standard input and writes base64-encoded
48 main(int argc UNUSED
, char **argv UNUSED
)
52 _setmode(0, O_BINARY
);
55 /* There is no context to initialize. */
59 /* "buffer" will hold the bytes from disk: */
60 uint8_t buffer
[CHUNK_SIZE
];
61 /* "result" will hold bytes before output: */
62 uint8_t result
[ENCODED_SIZE
+ 1];
63 unsigned nbytes
; /* Number of bytes read from stdin */
64 int encoded_bytes
; /* Total number of bytes encoded per iteration */
66 nbytes
= fread(buffer
,1,CHUNK_SIZE
,stdin
);
68 /* We overwrite result with more data */
69 base16_encode_update(result
, nbytes
, buffer
);
70 encoded_bytes
= BASE16_ENCODE_LENGTH(nbytes
);
71 result
[encoded_bytes
++] = '\n';
73 if (nbytes
< CHUNK_SIZE
)
77 werror ("Error reading file: %s\n", strerror(errno
));
80 if (!write_string (stdout
, encoded_bytes
, result
)
81 || fflush (stdout
) != 0)
83 werror ("Error writing file: %s\n", strerror(errno
));
88 /* The result vector is printed */
89 if (!write_string(stdout
,encoded_bytes
, result
))
91 werror ("Error writing file: %s\n", strerror(errno
));