6be5ad45e1f1a5d00b58d4726b3e9630cdf7000a
[platform/upstream/nettle.git] / examples / base64enc.c
1 /* base64enc -- an encoder 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 /* The number of bytes read in each iteration, we do one line at a time: */
49 #define CHUNK_SIZE 54
50
51 /* The *maximum* size of an encoded chunk: */
52 #define ENCODED_SIZE BASE64_ENCODE_LENGTH(CHUNK_SIZE)
53
54 /*
55  * Reads bytes from standard input and writes base64-encoded
56  * on standard output.
57  */
58 int
59 main(int argc UNUSED, char **argv UNUSED)
60 {
61   struct base64_encode_ctx b64_ctx;
62
63   /* Init the context: */
64   base64_encode_init(&b64_ctx);
65
66 #ifdef WIN32
67   _setmode(0, O_BINARY);
68 #endif
69
70   for (;;)
71     {
72       /* "buffer" will hold the bytes from disk: */
73       uint8_t buffer[CHUNK_SIZE];
74       /* "result" is the result vector: */
75       uint8_t result[ENCODED_SIZE + BASE64_ENCODE_FINAL_LENGTH + 1];
76       unsigned nbytes; /* Number of bytes read from stdin */
77       int encoded_bytes; /* total number of bytes encoded per iteration */
78       nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
79
80       /* We overwrite result with more data */
81       encoded_bytes = base64_encode_update(&b64_ctx, result, nbytes, buffer);
82
83       if (nbytes < CHUNK_SIZE)
84         {
85           if (ferror(stdin))
86             {
87               werror ("Error reading file: %s\n", strerror(errno));
88               return EXIT_FAILURE;
89             }
90           encoded_bytes += base64_encode_final(&b64_ctx,result + encoded_bytes);
91
92           result[encoded_bytes++] = '\n';
93           if (!write_string (stdout, encoded_bytes, result)
94               || fflush (stdout) != 0)
95             {
96               werror ("Error writing file: %s\n", strerror(errno));
97               return EXIT_FAILURE;
98             }
99           return EXIT_SUCCESS;
100         }
101
102       /* The result vector is written */
103       result[encoded_bytes++] = '\n';
104       if (!write_string (stdout, encoded_bytes, result))
105         {
106           werror ("Error writing file: %s\n", strerror(errno));
107           return EXIT_FAILURE;
108         }
109     }
110 }