int pt_sec_file_read(const struct pt_section *section, uint8_t *buffer, uint16_t size, uint64_t offset) { struct pt_sec_file_mapping *mapping; FILE *file; long begin, end, fbegin, fend; size_t read; int errcode; if (!buffer || !section) return -pte_invalid; mapping = section->mapping; if (!mapping) return -pte_internal; file = mapping->file; begin = mapping->begin; end = mapping->end; fbegin = (long) offset; if (((uint64_t) fbegin) != offset) return -pte_nomap; fbegin += begin; fend = fbegin + size; if (fend < fbegin) return -pte_nomap; if (end <= fbegin) return -pte_nomap; if (fbegin < begin) return -pte_nomap; errcode = fmap_lock(mapping); if (errcode < 0) return errcode; errcode = fseek(file, fbegin, SEEK_SET); if (errcode) goto out_unlock; read = fread(buffer, 1, size, file); errcode = fmap_unlock(mapping); if (errcode < 0) return errcode; return (int) read; out_unlock: (void) fmap_unlock(mapping); return -pte_nomap; }
int pt_sec_file_read(const struct pt_section *section, uint8_t *buffer, uint16_t size, uint64_t offset) { struct pt_sec_file_mapping *mapping; FILE *file; long begin; size_t read; int errcode; if (!buffer || !section) return -pte_internal; mapping = section->mapping; if (!mapping) return -pte_internal; file = mapping->file; /* We already checked in pt_section_read() that the requested memory * lies within the section's boundaries. * * And we checked that the file covers the entire section in * pt_sec_file_map(). There's no need to check for overflows, again. */ begin = mapping->begin + (long) offset; errcode = fmap_lock(mapping); if (errcode < 0) return errcode; errcode = fseek(file, begin, SEEK_SET); if (errcode) goto out_unlock; read = fread(buffer, 1, size, file); errcode = fmap_unlock(mapping); if (errcode < 0) return errcode; return (int) read; out_unlock: (void) fmap_unlock(mapping); return -pte_nomap; }