pack: move check_pack_index_ptr(), nth_packed_object_offset()

Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 18, 2017 at 15:20 UTC 9e0f45f5a61d9d0556e6004198dd6a650be14bd9
4 files changed +49 -49
cache.h
-16
@@ -1620,22 +1620,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
1620 */
1621 extern int odb_pack_keep(const char *name);
1622
1623 -/*
1624 - * Make sure that a pointer access into an mmap'd index file is within bounds,
1625 - * and can provide at least 8 bytes of data.
1626 - *
1627 - * Note that this is only necessary for variable-length segments of the file
1628 - * (like the 64-bit extended offset table), as we compare the size to the
1629 - * fixed-length parts when we open the file.
1630 - */
1631 -extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
1632 -
1633 -/*
1634 - * Return the offset of the nth object within the specified packfile.
1635 - * The index must already be opened.
1636 - */
1637 -extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
1638 -
1623 /*
1624 * If the object named sha1 is present in the specified packfile,
1625 * return its offset within the packfile; otherwise, return 0.
packfile.c
+33
@@ -1667,3 +1667,36 @@ const struct object_id *nth_packed_object_oid(struct object_id *oid,
1667 hashcpy(oid->hash, hash);
1668 return oid;
1669 }
1670 +
1671 +void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1672 +{
1673 + const unsigned char *ptr = vptr;
1674 + const unsigned char *start = p->index_data;
1675 + const unsigned char *end = start + p->index_size;
1676 + if (ptr < start)
1677 + die(_("offset before start of pack index for %s (corrupt index?)"),
1678 + p->pack_name);
1679 + /* No need to check for underflow; .idx files must be at least 8 bytes */
1680 + if (ptr >= end - 8)
1681 + die(_("offset beyond end of pack index for %s (truncated index?)"),
1682 + p->pack_name);
1683 +}
1684 +
1685 +off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1686 +{
1687 + const unsigned char *index = p->index_data;
1688 + index += 4 * 256;
1689 + if (p->index_version == 1) {
1690 + return ntohl(*((uint32_t *)(index + 24 * n)));
1691 + } else {
1692 + uint32_t off;
1693 + index += 8 + p->num_objects * (20 + 4);
1694 + off = ntohl(*((uint32_t *)(index + 4 * n)));
1695 + if (!(off & 0x80000000))
1696 + return off;
1697 + index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1698 + check_pack_index_ptr(p, index);
1699 + return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
1700 + ntohl(*((uint32_t *)(index + 4)));
1701 + }
1702 +}
packfile.h
+16
@@ -63,6 +63,16 @@ extern void unuse_pack(struct pack_window **);
63 extern void clear_delta_base_cache(void);
64 extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
65
66 +/*
67 + * Make sure that a pointer access into an mmap'd index file is within bounds,
68 + * and can provide at least 8 bytes of data.
69 + *
70 + * Note that this is only necessary for variable-length segments of the file
71 + * (like the 64-bit extended offset table), as we compare the size to the
72 + * fixed-length parts when we open the file.
73 + */
74 +extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
75 +
76 /*
77 * Return the SHA-1 of the nth object within the specified packfile.
78 * 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
87 */
88 extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
89
90 +/*
91 + * Return the offset of the nth object within the specified packfile.
92 + * The index must already be opened.
93 + */
94 +extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
95
96 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
97 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
109
110 extern void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1);
111 extern const struct packed_git *has_packed_and_bad(const unsigned char *sha1);
112 +
113 #endif
sha1_file.c
-33
@@ -1075,39 +1075,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
1075 return parse_sha1_header_extended(hdr, &oi, 0);
1076 }
1077
1078 -void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1079 -{
1080 - const unsigned char *ptr = vptr;
1081 - const unsigned char *start = p->index_data;
1082 - const unsigned char *end = start + p->index_size;
1083 - if (ptr < start)
1084 - die(_("offset before start of pack index for %s (corrupt index?)"),
1085 - p->pack_name);
1086 - /* No need to check for underflow; .idx files must be at least 8 bytes */
1087 - if (ptr >= end - 8)
1088 - die(_("offset beyond end of pack index for %s (truncated index?)"),
1089 - p->pack_name);
1090 -}
1091 -
1092 -off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1093 -{
1094 - const unsigned char *index = p->index_data;
1095 - index += 4 * 256;
1096 - if (p->index_version == 1) {
1097 - return ntohl(*((uint32_t *)(index + 24 * n)));
1098 - } else {
1099 - uint32_t off;
1100 - index += 8 + p->num_objects * (20 + 4);
1101 - off = ntohl(*((uint32_t *)(index + 4 * n)));
1102 - if (!(off & 0x80000000))
1103 - return off;
1104 - index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1105 - check_pack_index_ptr(p, index);
1106 - return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
1107 - ntohl(*((uint32_t *)(index + 4)));
1108 - }
1109 -}
1110 -
1078 off_t find_pack_entry_one(const unsigned char *sha1,
1079 struct packed_git *p)
1080 {