Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / examples / base16enc.c
blobc1a302d1ac8614028dc8b89de302b54d94d8bb00
1 /* base16enc -- an encoder for base16
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.
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.
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
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
33 #include "base16.h"
35 #include "io.h"
37 /* The number of bytes read in each iteration, we do one line at a time: */
38 #define CHUNK_SIZE 36
40 /* The *maximum* size of an encoded chunk: */
41 #define ENCODED_SIZE BASE16_ENCODE_LENGTH(CHUNK_SIZE)
44 * Reads bytes from standard input and writes base64-encoded
45 * on standard output.
47 int
48 main(int argc UNUSED, char **argv UNUSED)
51 #ifdef WIN32
52 _setmode(0, O_BINARY);
53 #endif
55 /* There is no context to initialize. */
57 for (;;)
59 /* "buffer" will hold the bytes from disk: */
60 uint8_t buffer[CHUNK_SIZE];
61 /* "result" will hold bytes before output: */
62 uint8_t result[ENCODED_SIZE + 1];
63 unsigned nbytes; /* Number of bytes read from stdin */
64 int encoded_bytes; /* Total number of bytes encoded per iteration */
66 nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
68 /* We overwrite result with more data */
69 base16_encode_update(result, nbytes, buffer);
70 encoded_bytes = BASE16_ENCODE_LENGTH(nbytes);
71 result[encoded_bytes++] = '\n';
73 if (nbytes < CHUNK_SIZE)
75 if (ferror(stdin))
77 werror ("Error reading file: %s\n", strerror(errno));
78 return EXIT_FAILURE;
80 if (!write_string (stdout, encoded_bytes, result)
81 || fflush (stdout) != 0)
83 werror ("Error writing file: %s\n", strerror(errno));
84 return EXIT_FAILURE;
86 return EXIT_SUCCESS;
88 /* The result vector is printed */
89 if (!write_string(stdout,encoded_bytes, result))
91 werror ("Error writing file: %s\n", strerror(errno));
92 return EXIT_FAILURE;