| 478 | } |
| 479 | |
| 480 | static int loadMem(const char *filename, const char **mem, int *size) { |
| 481 | int fd, res; |
| 482 | struct stat info; |
| 483 | char *base; |
| 484 | int siz = 0; |
| 485 | if (stat(filename, &info) < 0) |
| 486 | return(-1); |
| 487 | base = malloc(info.st_size + 1); |
| 488 | if (base == NULL) |
| 489 | return(-1); |
| 490 | if ((fd = open(filename, RD_FLAGS)) < 0) { |
| 491 | free(base); |
| 492 | return(-1); |
| 493 | } |
| 494 | while ((res = read(fd, &base[siz], info.st_size - siz)) > 0) { |
| 495 | siz += res; |
| 496 | } |
| 497 | close(fd); |
| 498 | #if !defined(_WIN32) |
| 499 | if (siz != info.st_size) { |
| 500 | free(base); |
| 501 | return(-1); |
| 502 | } |
| 503 | #endif |
| 504 | base[siz] = 0; |
| 505 | *mem = base; |
| 506 | *size = siz; |
| 507 | return(0); |
| 508 | } |
| 509 | |
| 510 | static int unloadMem(const char *mem) { |
| 511 | free((char *)mem); |
no test coverage detected