/src/opensc/src/tools/fread_to_eof.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2010 Frank Morgner |
3 | | * |
4 | | * This file is part of OpenSC. |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | #ifdef HAVE_CONFIG_H |
21 | | #include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | int fread_to_eof(const char *file, unsigned char **buf, size_t *buflen) |
29 | 0 | { |
30 | 0 | FILE *input = NULL; |
31 | 0 | int r = 0; |
32 | 0 | unsigned char *p; |
33 | |
|
34 | 0 | if (!buflen || !buf || !file) |
35 | 0 | goto err; |
36 | | |
37 | 0 | #define MAX_READ_LEN 0xfff |
38 | 0 | p = realloc(*buf, MAX_READ_LEN); |
39 | 0 | if (!p) |
40 | 0 | goto err; |
41 | 0 | *buf = p; |
42 | |
|
43 | 0 | input = fopen(file, "rb"); |
44 | 0 | if (!input) { |
45 | 0 | goto err; |
46 | 0 | } |
47 | | |
48 | 0 | *buflen = 0; |
49 | 0 | while (feof(input) == 0 && *buflen < MAX_READ_LEN) { |
50 | 0 | *buflen += fread(*buf+*buflen, 1, MAX_READ_LEN-*buflen, input); |
51 | 0 | if (ferror(input)) { |
52 | 0 | goto err; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 0 | r = 1; |
57 | 0 | err: |
58 | 0 | if (input) |
59 | 0 | fclose(input); |
60 | |
|
61 | 0 | return r; |
62 | 0 | } |