1 /* base16dec -- an decoder for base16
2
3 Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
4
5 This file is part of GNU Nettle.
6
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
9
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.
13
14 or
15
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.
19
20 or both in parallel, as here.
21
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.
26
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/.
30 */
31
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #ifdef WIN32
41 #include <fcntl.h>
42 #endif
43
44 #include "base16.h"
45
46 #include "io.h"
47
48 #define CHUNK_SIZE 16392
49
50 /* The maximum number of bytes generated for each chunk: */
51 #define DECODED_SIZE BASE16_DECODE_LENGTH(CHUNK_SIZE)
52
53
54 /*
55 * Reads base-16 encoded from stdin, writes decoded to stdout.
56 */
57 int
main(int argc UNUSED,char ** argv UNUSED)58 main(int argc UNUSED, char **argv UNUSED)
59 {
60 /* "buffer" will hold the bytes from disk: */
61 char * buffer = xalloc (CHUNK_SIZE);
62
63 /* "result" will hold bytes before output: */
64 uint8_t * result = xalloc (DECODED_SIZE);
65
66 /* We need a Base16 context for decoding: */
67 struct base16_decode_ctx b16_ctx;
68
69 /* Init the context: */
70 base16_decode_init (&b16_ctx);
71
72 #ifdef WIN32
73 _setmode(1, O_BINARY);
74 #endif
75
76 for (;;)
77 {
78 int nbytes; /* Number of bytes read frmo disk at each iteration */
79 size_t decoded_bytes; /* Bytes actually generated at each iteration */
80
81 nbytes = fread(buffer, 1, CHUNK_SIZE, stdin);
82
83 if (nbytes < CHUNK_SIZE && ferror(stdin))
84 {
85 werror ("Error reading file: %s\n", strerror(errno));
86 return EXIT_FAILURE;
87 }
88
89 /* Decodes one chunk: */
90 if (!base16_decode_update(&b16_ctx, &decoded_bytes, result, nbytes, buffer))
91 {
92 werror ("Error decoding input (not base16?)\n");
93 return EXIT_FAILURE;
94 }
95
96 if (!write_data (stdout, decoded_bytes, result))
97 {
98 werror ("Error writing file: %s\n", strerror(errno));
99 return EXIT_FAILURE;
100 }
101 if (nbytes < CHUNK_SIZE)
102 {
103 /* Check if decoding finalized OK: */
104 if (!base16_decode_final(&b16_ctx))
105 {
106 werror("Decoding did not finish properly.\n");
107 return EXIT_FAILURE;
108 }
109 break;
110 }
111 }
112
113 if (fflush (stdout) != 0)
114 {
115 werror ("Error writing file: %s\n", strerror(errno));
116 return EXIT_FAILURE;
117 }
118
119 free (buffer);
120 free (result);
121
122 return EXIT_SUCCESS;
123 }
124
125