Initialize Tizen 2.3
[external/nettle.git] / 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., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, 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 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
70
71 unsigned
72 read_file(const char *name, unsigned max_size, char **contents)
73 {
74   unsigned size;
75   unsigned done;
76   char *buffer;
77   FILE *f;
78     
79   f = fopen(name, "rb");
80   if (!f)
81     {
82       werror("Opening `%s' failed: %s\n", name, strerror(errno));
83       return 0;
84     }
85   buffer = NULL;
86
87   if (max_size && max_size < 100)
88     size = max_size;
89   else
90     size = 100;
91   
92   for (size = 100, done = 0;
93        (!max_size || done < max_size) && !feof(f);
94        size *= 2)
95     {
96       char *p;
97
98       if (max_size && size > max_size)
99         size = max_size;
100
101       /* Space for terminating NUL */
102       p = realloc(buffer, size + 1);
103
104       if (!p)
105         {
106         fail:
107           fclose(f);
108           free(buffer);
109           *contents = NULL;
110           return 0;
111         }
112
113       buffer = p;
114       done += fread(buffer + done, 1, size - done, f);
115
116       if (ferror(f))
117         goto fail;
118     }
119   
120   fclose(f);
121
122   /* NUL-terminate the data. */
123   buffer[done] = '\0';
124   *contents = buffer;
125   
126   return done;
127 }
128
129 int
130 write_file(const char *name, unsigned size, const char *buffer)
131 {
132   FILE *f = fopen(name, "wb");
133   unsigned res;
134   
135   if (!f)
136     return 0;
137
138   res = fwrite(buffer, 1, size, f);
139   
140   if (res < size)
141     res = 0;
142
143   return fclose(f) == 0 && res > 0;
144 }
145
146 int
147 write_string(FILE *f, unsigned size, const char *buffer)
148 {
149   size_t res = fwrite(buffer, 1, size, f);
150
151   return res == size;
152 }
153
154 int
155 simple_random(struct yarrow256_ctx *ctx, const char *name)
156 {
157   unsigned length;
158   char *buffer;
159
160   if (name)
161     length = read_file(name, 0, &buffer);
162   else
163     length = read_file(RANDOM_DEVICE, 20, &buffer);
164   
165   if (!length)
166     return 0;
167
168   yarrow256_seed(ctx, length, buffer);
169
170   free(buffer);
171
172   return 1;
173 }
174
175 int
176 hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
177 {
178   for (;;)
179     {
180       char buffer[BUFSIZE];
181       size_t res = fread(buffer, 1, sizeof(buffer), f);
182       if (ferror(f))
183         return 0;
184       
185       hash->update(ctx, res, buffer);
186       if (feof(f))
187         return 1;
188     }
189 }