1d339a16e47c0096324b360a1c2662edbccef9e1
[platform/upstream/nettle.git] / examples / base64dec.c
1 /* base64dec -- an decoder for base64
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 "base64.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 BASE64_DECODE_LENGTH(CHUNK_SIZE)
52
53
54 /*
55  * Reads base-64 encoded from stdin, writes decoded to stdout.
56  */
57 int
58 main(int argc UNUSED, char **argv UNUSED)
59 {
60   /* "buffer" will hold the bytes from disk: */
61   uint8_t * buffer = xalloc (CHUNK_SIZE);
62
63   /* "result" will hold bytes before output: */
64   uint8_t * result = xalloc (DECODED_SIZE);
65
66   /* We need a Base64 context for decoding: */
67   struct base64_decode_ctx b64_ctx;
68
69   /* Init the context: */
70   base64_decode_init(&b64_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 (!base64_decode_update(&b64_ctx, &decoded_bytes, result, nbytes, buffer))
91         {
92           werror ("Error decoding input (not base64?)\n");
93           return EXIT_FAILURE;
94         }
95
96       if (!write_string (stdout, decoded_bytes, result))
97         {
98           werror ("Error writing file: %s\n", strerror(errno));
99           return EXIT_FAILURE;
100         }
101
102       if (nbytes < CHUNK_SIZE)
103         {
104           /* Check if decoding finalized OK: */
105           if (!base64_decode_final(&b64_ctx))
106             {
107               werror ("Decoding did not finish properly.\n");
108               return EXIT_FAILURE;
109             }
110           break;
111         }
112     }
113
114   if (fflush (stdout) != 0)
115     {
116       werror ("Error writing file: %s\n", strerror(errno));
117       return EXIT_FAILURE;
118     }
119
120   free (buffer);
121   free (result);
122
123   return EXIT_SUCCESS;
124 }