Create fileobject from file */
| 368 | |
| 369 | /* Create fileobject from file */ |
| 370 | static struct fileobject *read_file(const char *filename) |
| 371 | { |
| 372 | FILE *fd = fopen(filename, "rb"); |
| 373 | off_t read_size = DEF_ALLOC; |
| 374 | |
| 375 | if (!fd) { |
| 376 | printerr("%s open failed: %s\n", filename, strerror(errno)); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | struct fileobject *fo = malloc_fo(read_size); |
| 381 | if (!fo) { |
| 382 | printerr("malloc failed\n"); |
| 383 | fclose(fd); |
| 384 | return NULL; |
| 385 | } |
| 386 | |
| 387 | off_t total_bytes_read = 0, bytes_read; |
| 388 | while ((bytes_read = fread(fo->data + total_bytes_read, 1, read_size, fd)) > 0) { |
| 389 | total_bytes_read += bytes_read; |
| 390 | struct fileobject *newfo = remalloc_fo(fo, fo->size + read_size); |
| 391 | if (!newfo) { |
| 392 | fclose(fd); |
| 393 | free_fo(fo); |
| 394 | return NULL; |
| 395 | } |
| 396 | fo = newfo; |
| 397 | } |
| 398 | |
| 399 | if (!total_bytes_read) { |
| 400 | fclose(fd); |
| 401 | free_fo(fo); |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | if (fclose(fd)) { |
| 406 | printerr("%s close failed: %s\n", filename, strerror(errno)); |
| 407 | free_fo(fo); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | fo->size = total_bytes_read; |
| 412 | |
| 413 | return fo; |
| 414 | } |
| 415 | |
| 416 | /* Create fileobject from physical memory at given address of size 64 KiB */ |
| 417 | static struct fileobject *read_physmem(size_t addr) |