3752df9e548cf34c979bf0c01b03cff3c762bfd9
[platform/upstream/nettle.git] / examples / base16enc.c
1 /* base16enc -- an encoder 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 /* The number of bytes read in each iteration, we do one line at a time: */
49 #define CHUNK_SIZE 36
50
51 /* The *maximum* size of an encoded chunk: */
52 #define ENCODED_SIZE BASE16_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
62 #ifdef WIN32
63   _setmode(0, O_BINARY);
64 #endif
65   
66   /* There is no context to initialize. */
67
68   for (;;)
69     {
70       /* "buffer" will hold the bytes from disk: */
71       uint8_t buffer[CHUNK_SIZE];
72       /* "result" will hold bytes before output: */
73       uint8_t result[ENCODED_SIZE + 1];
74       unsigned nbytes; /* Number of bytes read from stdin */
75       int encoded_bytes; /* Total number of bytes encoded per iteration */
76       
77       nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
78
79       /* We overwrite result with more data */
80       base16_encode_update(result, nbytes, buffer);
81       encoded_bytes = BASE16_ENCODE_LENGTH(nbytes);
82       result[encoded_bytes++] = '\n';
83       
84       if (nbytes < CHUNK_SIZE)
85         {
86           if (ferror(stdin))
87             {
88               werror ("Error reading file: %s\n", strerror(errno));
89               return EXIT_FAILURE;
90             }
91           if (!write_string (stdout, encoded_bytes, result)
92               || fflush (stdout) != 0)
93             {
94               werror ("Error writing file: %s\n", strerror(errno));
95               return EXIT_FAILURE;
96             }
97           return EXIT_SUCCESS;
98         }
99       /* The result vector is printed */
100       if (!write_string(stdout,encoded_bytes, result))
101         {
102           werror ("Error writing file: %s\n", strerror(errno));
103           return EXIT_FAILURE;
104         }
105     }
106 }
107