consolidate two copies of new style object header parsing code.
Also while we are at it, remove redundant typename[] array from unpack_sha1_header. The only reason it is different from the type_names[] array in object.c module is that this code cares about the subset of object types that are valid in a loose object, so prepare a separate array of boolean that tells us which types are valid, and share the name translation with the others. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 1, 2006 at 15:17 UTC
ad1ed5ee896ba5d7f89bc04c7441b1532efb9853
1 file changed
+45
-40
sha1_file.c
+45
-40
@@ -711,17 +711,39 @@ int legacy_loose_object(unsigned char *map)
711
return 0;
712
}
713
714
-static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
714
+static unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep)
715
{
716
+ unsigned shift;
717
unsigned char c;
717
- unsigned int bits;
718
unsigned long size;
719
- static const char *typename[8] = {
720
- NULL, /* OBJ_EXT */
721
- "commit", "tree", "blob", "tag",
722
- NULL, NULL, NULL
719
+ unsigned long used = 0;
720
+
721
+ c = buf[used++];
722
+ *type = (c >> 4) & 7;
723
+ size = c & 15;
724
+ shift = 4;
725
+ while (c & 0x80) {
726
+ if (len <= used)
727
+ return 0;
728
+ if (sizeof(long) * 8 <= shift)
729
+ return 0;
730
+ c = buf[used++];
731
+ size += (c & 0x7f) << shift;
732
+ shift += 7;
733
+ }
734
+ *sizep = size;
735
+ return used;
736
+}
737
+
738
+static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
739
+{
740
+ unsigned long size, used;
741
+ static const char valid_loose_object_type[8] = {
742
+ 0, /* OBJ_EXT */
743
+ 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
744
+ 0, /* "delta" and others are invalid in a loose object */
745
};
724
- const char *type;
746
+ enum object_type type;
747
748
/* Get the data stream */
749
memset(stream, 0, sizeof(*stream));
@@ -735,22 +757,11 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
757
return inflate(stream, 0);
758
}
759
738
- c = *map++;
739
- mapsize--;
740
- type = typename[(c >> 4) & 7];
741
- if (!type)
760
+ used = unpack_object_header_gently(map, mapsize, &type, &size);
761
+ if (!used || !valid_loose_object_type[type])
762
return -1;
743
-
744
- bits = 4;
745
- size = c & 0xf;
746
- while ((c & 0x80)) {
747
- if (bits >= 8*sizeof(long))
748
- return -1;
749
- c = *map++;
750
- size += (c & 0x7f) << bits;
751
- bits += 7;
752
- mapsize--;
753
- }
763
+ map += used;
764
+ mapsize -= used;
765
766
/* Set up the stream for the rest.. */
767
stream->next_in = map;
@@ -758,7 +769,8 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
769
inflateInit(stream);
770
771
/* And generate the fake traditional header */
761
- stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu", type, size);
772
+ stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
773
+ type_names[type], size);
774
return 0;
775
}
776
@@ -916,25 +928,18 @@ static int packed_delta_info(unsigned char *base_sha1,
928
static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
929
enum object_type *type, unsigned long *sizep)
930
{
919
- unsigned shift;
920
- unsigned char c;
921
- unsigned long size;
931
+ unsigned long used;
932
923
- if (offset >= p->pack_size)
933
+ if (p->pack_size <= offset)
934
die("object offset outside of pack file");
925
- c = *((unsigned char *)p->pack_base + offset++);
926
- *type = (c >> 4) & 7;
927
- size = c & 15;
928
- shift = 4;
929
- while (c & 0x80) {
930
- if (offset >= p->pack_size)
931
- die("object offset outside of pack file");
932
- c = *((unsigned char *)p->pack_base + offset++);
933
- size += (c & 0x7f) << shift;
934
- shift += 7;
935
- }
936
- *sizep = size;
937
- return offset;
935
+
936
+ used = unpack_object_header_gently((unsigned char *)p->pack_base +
937
+ offset,
938
+ p->pack_size - offset, type, sizep);
939
+ if (!used)
940
+ die("object offset outside of pack file");
941
+
942
+ return offset + used;
943
}
944
945
int check_reuse_pack_delta(struct packed_git *p, unsigned long offset,