~ubuntu-branches/ubuntu/quantal/nettle/quantal

« back to all changes in this revision

Viewing changes to examples/io.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "io.h"
 
34
 
 
35
#define RANDOM_DEVICE "/dev/urandom"
 
36
#define BUFSIZE 1000
 
37
 
 
38
int quiet_flag = 0;
 
39
 
 
40
void *
 
41
xalloc(size_t size)
 
42
{
 
43
  void *p = malloc(size);
 
44
  if (!p)
 
45
    {
 
46
      fprintf(stderr, "Virtual memory exhausted.\n");
 
47
      abort();
 
48
    }
 
49
 
 
50
  return p;
 
51
}
 
52
 
 
53
void
 
54
werror(const char *format, ...)
 
55
{
 
56
  if (!quiet_flag)
 
57
    {
 
58
      va_list args;
 
59
      va_start(args, format);
 
60
      vfprintf(stderr, format, args);
 
61
      va_end(args);
 
62
    }
 
63
}
 
64
 
 
65
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
 
66
 
 
67
unsigned
 
68
read_file(const char *name, unsigned max_size, char **contents)
 
69
{
 
70
  unsigned size;
 
71
  unsigned done;
 
72
  char *buffer;
 
73
  FILE *f;
 
74
    
 
75
  f = fopen(name, "rb");
 
76
  if (!f)
 
77
    return 0;
 
78
 
 
79
  buffer = NULL;
 
80
 
 
81
  if (max_size && max_size < 100)
 
82
    size = max_size;
 
83
  else
 
84
    size = 100;
 
85
  
 
86
  for (size = 100, done = 0;
 
87
       (!max_size || done < max_size) && !feof(f);
 
88
       size *= 2)
 
89
    {
 
90
      char *p;
 
91
 
 
92
      if (max_size && size > max_size)
 
93
        size = max_size;
 
94
 
 
95
      /* Space for terminating NUL */
 
96
      p = realloc(buffer, size + 1);
 
97
 
 
98
      if (!p)
 
99
        {
 
100
        fail:
 
101
          fclose(f);
 
102
          free(buffer);
 
103
          *contents = NULL;
 
104
          return 0;
 
105
        }
 
106
 
 
107
      buffer = p;
 
108
      done += fread(buffer + done, 1, size - done, f);
 
109
 
 
110
      if (ferror(f))
 
111
        goto fail;
 
112
    }
 
113
  
 
114
  fclose(f);
 
115
 
 
116
  /* NUL-terminate the data. */
 
117
  buffer[done] = '\0';
 
118
  *contents = buffer;
 
119
  
 
120
  return done;
 
121
}
 
122
 
 
123
int
 
124
write_file(const char *name, unsigned size, const char *buffer)
 
125
{
 
126
  FILE *f = fopen(name, "wb");
 
127
  unsigned res;
 
128
  
 
129
  if (!f)
 
130
    return 0;
 
131
 
 
132
  res = fwrite(buffer, 1, size, f);
 
133
  
 
134
  if (res < size)
 
135
    res = 0;
 
136
 
 
137
  return fclose(f) == 0 && res > 0;
 
138
}
 
139
 
 
140
int
 
141
write_string(FILE *f, unsigned size, const char *buffer)
 
142
{
 
143
  size_t res = fwrite(buffer, 1, size, f);
 
144
 
 
145
  return res == size;
 
146
}
 
147
 
 
148
int
 
149
simple_random(struct yarrow256_ctx *ctx, const char *name)
 
150
{
 
151
  unsigned length;
 
152
  char *buffer;
 
153
 
 
154
  if (name)
 
155
    length = read_file(name, 0, &buffer);
 
156
  else
 
157
    length = read_file(RANDOM_DEVICE, 20, &buffer);
 
158
  
 
159
  if (!length)
 
160
    return 0;
 
161
 
 
162
  yarrow256_seed(ctx, length, buffer);
 
163
 
 
164
  return 1;
 
165
}
 
166
 
 
167
int
 
168
hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
 
169
{
 
170
  for (;;)
 
171
    {
 
172
      char buffer[BUFSIZE];
 
173
      size_t res = fread(buffer, 1, sizeof(buffer), f);
 
174
      if (ferror(f))
 
175
        return 0;
 
176
      
 
177
      hash->update(ctx, res, buffer);
 
178
      if (feof(f))
 
179
        return 1;
 
180
    }
 
181
}
 
182
 
 
183
#if WITH_PUBLIC_KEY
 
184
int
 
185
read_rsa_key(const char *name,
 
186
             struct rsa_public_key *pub,
 
187
             struct rsa_private_key *priv)
 
188
{
 
189
  unsigned length;
 
190
  char *buffer;
 
191
  int res;
 
192
  
 
193
  length = read_file(name, 0, &buffer);
 
194
  if (!length)
 
195
    return 0;
 
196
 
 
197
  res = rsa_keypair_from_sexp(pub, priv, 0, length, buffer);
 
198
  free(buffer);
 
199
 
 
200
  return res;
 
201
}
 
202
#endif /* WITH_PUBLIC_KEY */