Signed-off-by: Jonathan Tan <jonathantanmy@xxxxxxxxxx> --- cache.h | 16 ---------------- packfile.c | 33 +++++++++++++++++++++++++++++++++ packfile.h | 16 ++++++++++++++++ sha1_file.c | 33 --------------------------------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/cache.h b/cache.h index 83aa3cc62..ee75a4949 100644 --- a/cache.h +++ b/cache.h @@ -1626,22 +1626,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern); */ extern int odb_pack_keep(const char *name); -/* - * Make sure that a pointer access into an mmap'd index file is within bounds, - * and can provide at least 8 bytes of data. - * - * Note that this is only necessary for variable-length segments of the file - * (like the 64-bit extended offset table), as we compare the size to the - * fixed-length parts when we open the file. - */ -extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr); - -/* - * Return the offset of the nth object within the specified packfile. - * The index must already be opened. - */ -extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n); - /* * If the object named sha1 is present in the specified packfile, * return its offset within the packfile; otherwise, return 0. diff --git a/packfile.c b/packfile.c index e9b16da94..e914422e9 100644 --- a/packfile.c +++ b/packfile.c @@ -1667,3 +1667,36 @@ const struct object_id *nth_packed_object_oid(struct object_id *oid, hashcpy(oid->hash, hash); return oid; } + +void check_pack_index_ptr(const struct packed_git *p, const void *vptr) +{ + const unsigned char *ptr = vptr; + const unsigned char *start = p->index_data; + const unsigned char *end = start + p->index_size; + if (ptr < start) + die(_("offset before start of pack index for %s (corrupt index?)"), + p->pack_name); + /* No need to check for underflow; .idx files must be at least 8 bytes */ + if (ptr >= end - 8) + die(_("offset beyond end of pack index for %s (truncated index?)"), + p->pack_name); +} + +off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) +{ + const unsigned char *index = p->index_data; + index += 4 * 256; + if (p->index_version == 1) { + return ntohl(*((uint32_t *)(index + 24 * n))); + } else { + uint32_t off; + index += 8 + p->num_objects * (20 + 4); + off = ntohl(*((uint32_t *)(index + 4 * n))); + if (!(off & 0x80000000)) + return off; + index += p->num_objects * 4 + (off & 0x7fffffff) * 8; + check_pack_index_ptr(p, index); + return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | + ntohl(*((uint32_t *)(index + 4))); + } +} diff --git a/packfile.h b/packfile.h index 56d70caa0..8deb84bd1 100644 --- a/packfile.h +++ b/packfile.h @@ -63,6 +63,16 @@ extern void unuse_pack(struct pack_window **); extern void clear_delta_base_cache(void); extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local); +/* + * Make sure that a pointer access into an mmap'd index file is within bounds, + * and can provide at least 8 bytes of data. + * + * Note that this is only necessary for variable-length segments of the file + * (like the 64-bit extended offset table), as we compare the size to the + * fixed-length parts when we open the file. + */ +extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr); + /* * Return the SHA-1 of the nth object within the specified packfile. * Open the index if it is not already open. The return value points @@ -77,6 +87,11 @@ extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t */ extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n); +/* + * Return the offset of the nth object within the specified packfile. + * The index must already be opened. + */ +extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n); extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *); extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep); @@ -94,4 +109,5 @@ extern int packed_object_info(struct packed_git *pack, off_t offset, struct obje extern void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1); extern const struct packed_git *has_packed_and_bad(const unsigned char *sha1); + #endif diff --git a/sha1_file.c b/sha1_file.c index 34fbe8e51..2d22bc228 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -1074,39 +1074,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep) return parse_sha1_header_extended(hdr, &oi, 0); } -void check_pack_index_ptr(const struct packed_git *p, const void *vptr) -{ - const unsigned char *ptr = vptr; - const unsigned char *start = p->index_data; - const unsigned char *end = start + p->index_size; - if (ptr < start) - die(_("offset before start of pack index for %s (corrupt index?)"), - p->pack_name); - /* No need to check for underflow; .idx files must be at least 8 bytes */ - if (ptr >= end - 8) - die(_("offset beyond end of pack index for %s (truncated index?)"), - p->pack_name); -} - -off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) -{ - const unsigned char *index = p->index_data; - index += 4 * 256; - if (p->index_version == 1) { - return ntohl(*((uint32_t *)(index + 24 * n))); - } else { - uint32_t off; - index += 8 + p->num_objects * (20 + 4); - off = ntohl(*((uint32_t *)(index + 4 * n))); - if (!(off & 0x80000000)) - return off; - index += p->num_objects * 4 + (off & 0x7fffffff) * 8; - check_pack_index_ptr(p, index); - return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | - ntohl(*((uint32_t *)(index + 4))); - } -} - off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *p) { -- 2.14.1.480.gb18f417b89-goog