Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / examples / base64enc.c
blob0b5f3587501db43ad0ccec21928a22526b3e16b4
1 /* base64enc -- an encoder for base64
3 * Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
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.
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 "base64.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 54
40 /* The *maximum* size of an encoded chunk: */
41 #define ENCODED_SIZE BASE64_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)
50 struct base64_encode_ctx b64_ctx;
52 /* Init the context: */
53 base64_encode_init(&b64_ctx);
55 #ifdef WIN32
56 _setmode(0, O_BINARY);
57 #endif
59 for (;;)
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);
69 /* We overwrite result with more data */
70 encoded_bytes = base64_encode_update(&b64_ctx, result, nbytes, buffer);
72 if (nbytes < CHUNK_SIZE)
74 if (ferror(stdin))
76 werror ("Error reading file: %s\n", strerror(errno));
77 return EXIT_FAILURE;
79 encoded_bytes += base64_encode_final(&b64_ctx,result + encoded_bytes);
81 result[encoded_bytes++] = '\n';
82 if (!write_string (stdout, encoded_bytes, result)
83 || fflush (stdout) != 0)
85 werror ("Error writing file: %s\n", strerror(errno));
86 return EXIT_FAILURE;
88 return EXIT_SUCCESS;
91 /* The result vector is written */
92 result[encoded_bytes++] = '\n';
93 if (!write_string (stdout, encoded_bytes, result))
95 werror ("Error writing file: %s\n", strerror(errno));
96 return EXIT_FAILURE;