1 /* IPSec VPN client compatible with Cisco equipment.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 void crypto_error_set(crypto_error
**error
,
43 fprintf(stderr
, "%s: called with non-NULL *error\n", __func__
);
47 *error
= calloc(1, sizeof(crypto_error
));
51 (*error
)->code
= code
;
52 (*error
)->err
= in_errno
;
54 (*error
)->msg
= malloc(MSG_SIZE
);
56 fprintf(stderr
, "%s: not enough memory for error message\n", __func__
);
57 crypto_error_clear(error
);
62 if (vsnprintf((*error
)->msg
, MSG_SIZE
, fmt
, args
) == -1)
63 (*error
)->msg
[0] = '\0';
67 void crypto_error_free(crypto_error
*error
)
72 memset(error
, 0, sizeof(crypto_error
));
77 void crypto_error_clear(crypto_error
**error
)
79 if (error
&& *error
) {
80 crypto_error_free(*error
);
85 void crypto_call_error(crypto_error
*err
)
88 error(err
->code
, err
->err
, "%s\n", err
->msg
);
90 error(1, 0, "unknown error");
94 crypto_read_file(const char *path
, size_t *out_len
, crypto_error
**error
)
100 unsigned char *data
= NULL
;
104 fd
= open(path
, O_RDONLY
);
106 crypto_error_set(error
, 1, errno
, "failed to open file '%s'", path
);
110 if (fstat(fd
, &st
) < 0) {
111 crypto_error_set(error
, 1, errno
, "failed to stat file '%s'", path
);
115 if (st
.st_size
<= 0 || st
.st_size
> INT_MAX
) {
116 crypto_error_set(error
, 1, errno
, "invalid file '%s' length %ld", path
, st
.st_size
);
120 file_size
= st
.st_size
;
121 data
= malloc(file_size
);
123 crypto_error_set(error
, 1, ENOMEM
, "not enough memory to read file '%s'", path
);
128 bytes_read
= read(fd
, &(data
[*out_len
]), (st
.st_size
- *out_len
));
129 if (bytes_read
< 0) {
133 crypto_error_set(error
, 1, errno
, "failed to read file '%s'", path
);
136 *out_len
+= bytes_read
;
137 } while ((bytes_read
> 0) && (*out_len
<= file_size
));