| 1 |
/* read-file.c -- read file contents into a string |
| 2 |
Copyright (C) 2006, 2009-2026 Free Software Foundation, Inc. |
| 3 |
Written by Simon Josefsson and Bruno Haible. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#include <config.h> |
| 19 |
|
| 20 |
#include "read-file.h" |
| 21 |
|
| 22 |
/* Get fstat. */ |
| 23 |
#include <sys/stat.h> |
| 24 |
|
| 25 |
/* Get ftello. */ |
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
/* Get PTRDIFF_MAX. */ |
| 29 |
#include <stdint.h> |
| 30 |
|
| 31 |
/* Get malloc, realloc, free. */ |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
/* Get memcpy, memset_explicit. */ |
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
/* Get errno. */ |
| 38 |
#include <errno.h> |
| 39 |
|
| 40 |
/* Read a STREAM and return a newly allocated string with the content, |
| 41 |
and set *LENGTH to the length of the string. The string is |
| 42 |
zero-terminated, but the terminating zero byte is not counted in |
| 43 |
*LENGTH. On errors, *LENGTH is undefined, errno preserves the |
| 44 |
values set by system functions (if any), and NULL is returned. |
| 45 |
|
| 46 |
If the RF_SENSITIVE flag is set in FLAGS: |
| 47 |
- You should control the buffering of STREAM using 'setvbuf'. Either |
| 48 |
clear the buffer of STREAM after closing it, or disable buffering of |
| 49 |
STREAM before calling this function. |
| 50 |
- The memory buffer internally allocated will be cleared upon failure. */ |
| 51 |
char * |
| 52 |
fread_file (FILE *stream, int flags, size_t *length) |
| 53 |
{ |
| 54 |
size_t alloc = BUFSIZ; |
| 55 |
|
| 56 |
/* For a regular file, allocate a buffer that has exactly the right |
| 57 |
size. This avoids the need to do dynamic reallocations later. */ |
| 58 |
{ |
| 59 |
struct stat st; |
| 60 |
|
| 61 |
if (fstat (fileno (stream), &st) >= 0 && S_ISREG (st.st_mode)) |
| 62 |
{ |
| 63 |
off_t pos = ftello (stream); |
| 64 |
|
| 65 |
if (pos >= 0 && pos < st.st_size) |
| 66 |
{ |
| 67 |
off_t alloc_off = st.st_size - pos; |
| 68 |
|
| 69 |
/* '1' below, accounts for the trailing NUL. */ |
| 70 |
if (PTRDIFF_MAX - 1 < alloc_off) |
| 71 |
{ |
| 72 |
errno = ENOMEM; |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
alloc = alloc_off + 1; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
char *buf = malloc (alloc); |
| 82 |
if (!buf) |
| 83 |
return NULL; /* errno is ENOMEM. */ |
| 84 |
|
| 85 |
{ |
| 86 |
size_t size = 0; /* number of bytes read so far */ |
| 87 |
int saved_errno; |
| 88 |
|
| 89 |
for (;;) |
| 90 |
{ |
| 91 |
/* This reads 1 more than the size of a regular file |
| 92 |
so that we get eof immediately. */ |
| 93 |
size_t requested = alloc - size; |
| 94 |
size_t count = fread (buf + size, 1, requested, stream); |
| 95 |
size += count; |
| 96 |
|
| 97 |
if (count != requested) |
| 98 |
{ |
| 99 |
saved_errno = errno; |
| 100 |
if (ferror (stream)) |
| 101 |
break; |
| 102 |
|
| 103 |
/* Shrink the allocated memory if possible. */ |
| 104 |
if (size < alloc - 1) |
| 105 |
{ |
| 106 |
if (flags & RF_SENSITIVE) |
| 107 |
{ |
| 108 |
char *smaller_buf = malloc (size + 1); |
| 109 |
if (smaller_buf == NULL) |
| 110 |
memset_explicit (buf + size, 0, alloc - size); |
| 111 |
else |
| 112 |
{ |
| 113 |
memcpy (smaller_buf, buf, size); |
| 114 |
memset_explicit (buf, 0, alloc); |
| 115 |
free (buf); |
| 116 |
buf = smaller_buf; |
| 117 |
} |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
char *smaller_buf = realloc (buf, size + 1); |
| 122 |
if (smaller_buf != NULL) |
| 123 |
buf = smaller_buf; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
buf[size] = '\0'; |
| 128 |
*length = size; |
| 129 |
return buf; |
| 130 |
} |
| 131 |
|
| 132 |
{ |
| 133 |
char *new_buf; |
| 134 |
size_t saved_alloc = alloc; |
| 135 |
|
| 136 |
if (alloc == PTRDIFF_MAX) |
| 137 |
{ |
| 138 |
saved_errno = ENOMEM; |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
if (alloc < PTRDIFF_MAX - alloc / 2) |
| 143 |
alloc = alloc + alloc / 2; |
| 144 |
else |
| 145 |
alloc = PTRDIFF_MAX; |
| 146 |
|
| 147 |
if (flags & RF_SENSITIVE) |
| 148 |
{ |
| 149 |
new_buf = malloc (alloc); |
| 150 |
if (!new_buf) |
| 151 |
{ |
| 152 |
/* BUF should be cleared below after the loop. */ |
| 153 |
saved_errno = errno; |
| 154 |
break; |
| 155 |
} |
| 156 |
memcpy (new_buf, buf, saved_alloc); |
| 157 |
memset_explicit (buf, 0, saved_alloc); |
| 158 |
free (buf); |
| 159 |
} |
| 160 |
else if (!(new_buf = realloc (buf, alloc))) |
| 161 |
{ |
| 162 |
saved_errno = errno; |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
buf = new_buf; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (flags & RF_SENSITIVE) |
| 171 |
memset_explicit (buf, 0, alloc); |
| 172 |
|
| 173 |
free (buf); |
| 174 |
errno = saved_errno; |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/* Open and read the contents of FILENAME, and return a newly |
| 180 |
allocated string with the content, and set *LENGTH to the length of |
| 181 |
the string. The string is zero-terminated, but the terminating |
| 182 |
zero byte is not counted in *LENGTH. On errors, *LENGTH is |
| 183 |
undefined, errno preserves the values set by system functions (if |
| 184 |
any), and NULL is returned. |
| 185 |
|
| 186 |
If the RF_BINARY flag is set in FLAGS, the file is opened in binary |
| 187 |
mode. If the RF_SENSITIVE flag is set in FLAGS, the memory buffer |
| 188 |
internally allocated will be cleared upon failure. */ |
| 189 |
char * |
| 190 |
read_file (const char *filename, int flags, size_t *length) |
| 191 |
{ |
| 192 |
const char *mode = (flags & RF_BINARY) ? "rbe" : "re"; |
| 193 |
FILE *stream = fopen (filename, mode); |
| 194 |
|
| 195 |
if (!stream) |
| 196 |
return NULL; |
| 197 |
|
| 198 |
if (flags & RF_SENSITIVE) |
| 199 |
setvbuf (stream, NULL, _IONBF, 0); |
| 200 |
|
| 201 |
char *out = fread_file (stream, flags, length); |
| 202 |
|
| 203 |
if (fclose (stream) != 0) |
| 204 |
{ |
| 205 |
if (out) |
| 206 |
{ |
| 207 |
if (flags & RF_SENSITIVE) |
| 208 |
memset_explicit (out, 0, *length); |
| 209 |
free (out); |
| 210 |
} |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
return out; |
| 215 |
} |