Merged aes-reorg branch.
[gd/nettle] / examples / io.c
1 /* io.c
2  *
3  * Miscellaneous functions used by the example programs.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
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.
14  * 
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.
19  * 
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.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32
33 /* For errno and strerror */
34 #include <errno.h>
35 #include <string.h>
36
37 #include "io.h"
38
39 #define RANDOM_DEVICE "/dev/urandom"
40 #define BUFSIZE 1000
41
42 int quiet_flag = 0;
43
44 void *
45 xalloc(size_t size)
46 {
47   void *p = malloc(size);
48   if (!p)
49     {
50       fprintf(stderr, "Virtual memory exhausted.\n");
51       abort();
52     }
53
54   return p;
55 }
56
57 void
58 werror(const char *format, ...)
59 {
60   if (!quiet_flag)
61     {
62       va_list args;
63       va_start(args, format);
64       vfprintf(stderr, format, args);
65       va_end(args);
66     }
67 }
68
69 unsigned
70 read_file(const char *name, unsigned max_size, char **contents)
71 {
72   unsigned size, done;
73   char *buffer;
74   FILE *f;
75     
76   f = fopen(name, "rb");
77   if (!f)
78     {
79       werror("Opening `%s' failed: %s\n", name, strerror(errno));
80       return 0;
81     }
82
83   size = 100;
84
85   for (buffer = NULL, done = 0;; size *= 2)
86     {
87       char *p;
88
89       if (max_size && size > max_size)
90         size = max_size;
91
92       /* Space for terminating NUL */
93       p = realloc(buffer, size + 1);
94
95       if (!p)
96         {
97         fail:
98           fclose(f);
99           free(buffer);
100           *contents = NULL;
101           return 0;
102         }
103
104       buffer = p;
105       done += fread(buffer + done, 1, size - done, f);
106
107       if (done < size)
108         {
109           /* Short count means EOF or read error */
110           if (ferror(f))
111             {
112               fprintf (stderr, "Reading `%s' failed: %s\n",
113                        name, strerror(errno));
114
115               goto fail;
116             }
117           if (done == 0)
118             /* Treat empty file as error */
119             goto fail;
120
121           break;
122         }
123
124       if (size == max_size)
125         break;
126     }
127   
128   fclose(f);
129
130   /* NUL-terminate the data. */
131   buffer[done] = '\0';
132   *contents = buffer;
133   
134   return done;
135 }
136
137 int
138 write_string(FILE *f, unsigned size, const char *buffer)
139 {
140   size_t res = fwrite(buffer, 1, size, f);
141
142   return res == size;
143 }
144
145 int
146 write_file(const char *name, unsigned size, const char *buffer)
147 {
148   FILE *f = fopen(name, "wb");
149   int res;
150   
151   if (!f)
152     return 0;
153
154   res = write_string(f, size, buffer);
155   return fclose(f) == 0 && res;
156 }
157
158 int
159 simple_random(struct yarrow256_ctx *ctx, const char *name)
160 {
161   unsigned length;
162   char *buffer;
163
164   if (name)
165     length = read_file(name, 0, &buffer);
166   else
167     length = read_file(RANDOM_DEVICE, 20, &buffer);
168   
169   if (!length)
170     return 0;
171
172   yarrow256_seed(ctx, length, buffer);
173
174   free(buffer);
175
176   return 1;
177 }
178
179 int
180 hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
181 {
182   for (;;)
183     {
184       char buffer[BUFSIZE];
185       size_t res = fread(buffer, 1, sizeof(buffer), f);
186       if (ferror(f))
187         return 0;
188       
189       hash->update(ctx, res, buffer);
190       if (feof(f))
191         return 1;
192     }
193 }