Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / examples / io.c
blob2eab7e0c28211109e3e385a12542b4c9992d602c
1 /* io.c
3 * Miscellaneous functions used by the example programs.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
9 *
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <stdarg.h>
31 #include <stdlib.h>
33 /* For errno and strerror */
34 #include <errno.h>
35 #include <string.h>
37 #include "io.h"
39 #define RANDOM_DEVICE "/dev/urandom"
40 #define BUFSIZE 1000
42 int quiet_flag = 0;
44 void *
45 xalloc(size_t size)
47 void *p = malloc(size);
48 if (!p)
50 fprintf(stderr, "Virtual memory exhausted.\n");
51 abort();
54 return p;
57 void
58 werror(const char *format, ...)
60 if (!quiet_flag)
62 va_list args;
63 va_start(args, format);
64 vfprintf(stderr, format, args);
65 va_end(args);
69 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
71 unsigned
72 read_file(const char *name, unsigned max_size, char **contents)
74 unsigned size, done;
75 char *buffer;
76 FILE *f;
78 f = fopen(name, "rb");
79 if (!f)
81 werror("Opening `%s' failed: %s\n", name, strerror(errno));
82 return 0;
85 size = 100;
87 for (buffer = NULL, done = 0;; size *= 2)
89 char *p;
91 if (max_size && size > max_size)
92 size = max_size;
94 /* Space for terminating NUL */
95 p = realloc(buffer, size + 1);
97 if (!p)
99 fail:
100 fclose(f);
101 free(buffer);
102 *contents = NULL;
103 return 0;
106 buffer = p;
107 done += fread(buffer + done, 1, size - done, f);
109 if (done < size)
111 /* Short count means EOF or read error */
112 if (ferror(f))
114 fprintf (stderr, "Reading `%s' failed: %s\n",
115 name, strerror(errno));
117 goto fail;
119 if (done == 0)
120 /* Treat empty file as error */
121 goto fail;
123 break;
126 if (size == max_size)
127 break;
130 fclose(f);
132 /* NUL-terminate the data. */
133 buffer[done] = '\0';
134 *contents = buffer;
136 return done;
140 write_string(FILE *f, unsigned size, const char *buffer)
142 size_t res = fwrite(buffer, 1, size, f);
144 return res == size;
148 write_file(const char *name, unsigned size, const char *buffer)
150 FILE *f = fopen(name, "wb");
151 int res;
153 if (!f)
154 return 0;
156 res = write_string(f, size, buffer);
157 return fclose(f) == 0 && res;
161 simple_random(struct yarrow256_ctx *ctx, const char *name)
163 unsigned length;
164 char *buffer;
166 if (name)
167 length = read_file(name, 0, &buffer);
168 else
169 length = read_file(RANDOM_DEVICE, 20, &buffer);
171 if (!length)
172 return 0;
174 yarrow256_seed(ctx, length, buffer);
176 free(buffer);
178 return 1;
182 hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
184 for (;;)
186 char buffer[BUFSIZE];
187 size_t res = fread(buffer, 1, sizeof(buffer), f);
188 if (ferror(f))
189 return 0;
191 hash->update(ctx, res, buffer);
192 if (feof(f))
193 return 1;