Whole-file byte snapshot. Returns malloc'd buffer (caller frees) and * writes the length to *out_len. Returns NULL on failure. */
| 20952 | /* Whole-file byte snapshot. Returns malloc'd buffer (caller frees) and |
| 20953 | * writes the length to *out_len. Returns NULL on failure. */ |
| 20954 | static unsigned char *roq_read_file_bytes(const char *path, long *out_len) { |
| 20955 | *out_len = 0; |
| 20956 | FILE *fp = fopen(path, "rb"); |
| 20957 | if (!fp) { |
| 20958 | return NULL; |
| 20959 | } |
| 20960 | if (fseek(fp, 0, SEEK_END) != 0) { |
| 20961 | fclose(fp); |
| 20962 | return NULL; |
| 20963 | } |
| 20964 | long sz = ftell(fp); |
| 20965 | if (sz < 0) { |
| 20966 | fclose(fp); |
| 20967 | return NULL; |
| 20968 | } |
| 20969 | rewind(fp); |
| 20970 | unsigned char *buf = malloc((size_t)sz > 0 ? (size_t)sz : 1); |
| 20971 | if (!buf) { |
| 20972 | fclose(fp); |
| 20973 | return NULL; |
| 20974 | } |
| 20975 | size_t got = fread(buf, 1, (size_t)sz, fp); |
| 20976 | fclose(fp); |
| 20977 | if (got != (size_t)sz) { |
| 20978 | free(buf); |
| 20979 | return NULL; |
| 20980 | } |
| 20981 | *out_len = sz; |
| 20982 | return buf; |
| 20983 | } |
| 20984 | |
| 20985 | static int roq_file_exists(const char *path) { |
| 20986 | struct stat st; |
no test coverage detected