Revert "Merge branch 'upstream' into tizen"
[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 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.
9  *
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.
14  *
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,
18  * MA 02111-1301, USA.
19  */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #ifdef WIN32
30 #include <fcntl.h>
31 #endif
32
33 #include "base64.h"
34
35 #include "io.h"
36
37 /* The number of bytes read in each iteration, we do one line at a time: */
38 #define CHUNK_SIZE 54
39
40 /* The *maximum* size of an encoded chunk: */
41 #define ENCODED_SIZE BASE64_ENCODE_LENGTH(CHUNK_SIZE)
42
43 /*
44  * Reads bytes from standard input and writes base64-encoded
45  * on standard output.
46  */
47 int
48 main(int argc UNUSED, char **argv UNUSED)
49 {
50   struct base64_encode_ctx b64_ctx;
51
52   /* Init the context: */
53   base64_encode_init(&b64_ctx);
54
55 #ifdef WIN32
56   _setmode(0, O_BINARY);
57 #endif
58
59   for (;;)
60     {
61       /* "buffer" will hold the bytes from disk: */
62       uint8_t buffer[CHUNK_SIZE];
63       /* "result" is the result vector: */
64       uint8_t result[ENCODED_SIZE + BASE64_ENCODE_FINAL_LENGTH + 1];
65       unsigned nbytes; /* Number of bytes read from stdin */
66       int encoded_bytes; /* total number of bytes encoded per iteration */
67       nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
68
69       /* We overwrite result with more data */
70       encoded_bytes = base64_encode_update(&b64_ctx, result, nbytes, buffer);
71
72       if (nbytes < CHUNK_SIZE)
73         {
74           if (ferror(stdin))
75             {
76               werror ("Error reading file: %s\n", strerror(errno));
77               return EXIT_FAILURE;
78             }
79           encoded_bytes += base64_encode_final(&b64_ctx,result + encoded_bytes);
80
81           result[encoded_bytes++] = '\n';
82           if (!write_string (stdout, encoded_bytes, result)
83               || fflush (stdout) != 0)
84             {
85               werror ("Error writing file: %s\n", strerror(errno));
86               return EXIT_FAILURE;
87             }
88           return EXIT_SUCCESS;
89         }
90
91       /* The result vector is written */
92       result[encoded_bytes++] = '\n';
93       if (!write_string (stdout, encoded_bytes, result))
94         {
95           werror ("Error writing file: %s\n", strerror(errno));
96           return EXIT_FAILURE;
97         }
98     }
99 }