From 9e0f45f5a61d9d0556e6004198dd6a650be14bd9 Mon Sep 17 00:00:00 2001 From: Jonathan Tan Date: Fri, 18 Aug 2017 15:20:32 -0700 Subject: [PATCH] pack: move check_pack_index_ptr(), nth_packed_object_offset() Signed-off-by: Jonathan Tan Signed-off-by: Junio C Hamano --- 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 42a828336..1a8b22779 100644 --- a/cache.h +++ b/cache.h @@ -1621,22 +1621,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 cc8d0d7db..f251e3878 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 790b174cb..f4f5b936f 100644 --- a/packfile.h +++ b/packfile.h @@ -64,6 +64,16 @@ 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 * at the SHA-1 within the mmapped index. Return NULL if there is an @@ -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 20cb30b87..52e9292ff 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -1075,39 +1075,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.11.0