883
return unpack_sha1_rest(&stream, hdr, *size);
884
}
885
886
+static unsigned long get_delta_base(struct packed_git *p,
887
+ unsigned long offset,
888
+ enum object_type kind,
889
+ unsigned long delta_obj_offset,
890
+ unsigned long *base_obj_offset)
891
+{
892
+ unsigned char *base_info = (unsigned char *) p->pack_base + offset;
893
+ unsigned long base_offset;
894
+
895
+ /* there must be at least 20 bytes left regardless of delta type */
896
+ if (p->pack_size <= offset + 20)
897
+ die("truncated pack file");
898
+
899
+ if (kind == OBJ_OFS_DELTA) {
900
+ unsigned used = 0;
901
+ unsigned char c = base_info[used++];
902
+ base_offset = c & 127;
903
+ while (c & 128) {
904
+ base_offset += 1;
905
+ if (!base_offset || base_offset & ~(~0UL >> 7))
906
+ die("offset value overflow for delta base object");
907
+ c = base_info[used++];
908
+ base_offset = (base_offset << 7) + (c & 127);
909
+ }
910
+ base_offset = delta_obj_offset - base_offset;
911
+ if (base_offset >= delta_obj_offset)
912
+ die("delta base offset out of bound");
913
+ offset += used;
914
+ } else if (kind == OBJ_REF_DELTA) {
915
+ /* The base entry _must_ be in the same pack */
916
+ base_offset = find_pack_entry_one(base_info, p);
917
+ if (!base_offset)
918
+ die("failed to find delta-pack base object %s",
919
+ sha1_to_hex(base_info));
920
+ offset += 20;
921
+ } else
922
+ die("I am totally screwed");
923
+ *base_obj_offset = base_offset;
924
+ return offset;
925
+}
926
+
927
/* forward declaration for a mutually recursive function */
928
static int packed_object_info(struct packed_git *p, unsigned long offset,
929
char *type, unsigned long *sizep);
930
931
static int packed_delta_info(struct packed_git *p,
932
unsigned long offset,
933
+ enum object_type kind,
934
+ unsigned long obj_offset,
935
char *type,
936
unsigned long *sizep)
937
{
938
unsigned long base_offset;
896
- unsigned char *base_sha1 = (unsigned char *) p->pack_base + offset;
939
898
- if (p->pack_size < offset + 20)
899
- die("truncated pack file");
900
- /* The base entry _must_ be in the same pack */
901
- base_offset = find_pack_entry_one(base_sha1, p);
902
- if (!base_offset)
903
- die("failed to find delta-pack base object %s",
904
- sha1_to_hex(base_sha1));
905
- offset += 20;
940
+ offset = get_delta_base(p, offset, kind, obj_offset, &base_offset);
941
942
/* We choose to only get the type of the base object and
943
* ignore potentially corrupt pack file that expects the delta
1010
use_packed_git(p);
1011
ptr = offset;
1012
ptr = unpack_object_header(p, ptr, kindp, sizep);
978
- if (*kindp != OBJ_DELTA)
1013
+ if (*kindp != OBJ_REF_DELTA)
1014
goto done;
1015
hashcpy(base, (unsigned char *) p->pack_base + ptr);
1016
status = 0;
1027
unsigned int *delta_chain_length,
1028
unsigned char *base_sha1)
1029
{
995
- unsigned long val;
1030
+ unsigned long obj_offset, val;
1031
unsigned char *next_sha1;
1032
enum object_type kind;
1033
1034
*delta_chain_length = 0;
1035
+ obj_offset = offset;
1036
offset = unpack_object_header(p, offset, &kind, size);
1037
1038
for (;;) {
1047
strcpy(type, type_names[kind]);
1048
*store_size = 0; /* notyet */
1049
return;
1014
- case OBJ_DELTA:
1050
+ case OBJ_OFS_DELTA:
1051
+ get_delta_base(p, offset, kind, obj_offset, &offset);
1052
+ if (*delta_chain_length == 0) {
1053
+ /* TODO: find base_sha1 as pointed by offset */
1054
+ }
1055
+ break;
1056
+ case OBJ_REF_DELTA:
1057
if (p->pack_size <= offset + 20)
1058
die("pack file %s records an incomplete delta base",
1059
p->pack_name);
1063
offset = find_pack_entry_one(next_sha1, p);
1064
break;
1065
}
1066
+ obj_offset = offset;
1067
offset = unpack_object_header(p, offset, &kind, &val);
1068
(*delta_chain_length)++;
1069
}
1072
static int packed_object_info(struct packed_git *p, unsigned long offset,
1073
char *type, unsigned long *sizep)
1074
{
1032
- unsigned long size;
1075
+ unsigned long size, obj_offset = offset;
1076
enum object_type kind;
1077
1078
offset = unpack_object_header(p, offset, &kind, &size);
1079
1037
- if (kind == OBJ_DELTA)
1038
- return packed_delta_info(p, offset, type, sizep);
1039
-
1080
switch (kind) {
1081
+ case OBJ_OFS_DELTA:
1082
+ case OBJ_REF_DELTA:
1083
+ return packed_delta_info(p, offset, kind, obj_offset, type, sizep);
1084
case OBJ_COMMIT:
1085
case OBJ_TREE:
1086
case OBJ_BLOB:
1126
static void *unpack_delta_entry(struct packed_git *p,
1127
unsigned long offset,
1128
unsigned long delta_size,
1129
+ enum object_type kind,
1130
+ unsigned long obj_offset,
1131
char *type,
1132
unsigned long *sizep)
1133
{
1134
void *delta_data, *result, *base;
1135
unsigned long result_size, base_size, base_offset;
1091
- unsigned char *base_sha1;
1092
-
1093
- if (p->pack_size < offset + 20)
1094
- die("truncated pack file");
1095
- /* The base entry _must_ be in the same pack */
1096
- base_sha1 = (unsigned char*)p->pack_base + offset;
1097
- base_offset = find_pack_entry_one(base_sha1, p);
1098
- if (!base_offset)
1099
- die("failed to find delta-pack base object %s",
1100
- sha1_to_hex(base_sha1));
1101
- offset += 20;
1136
1137
+ offset = get_delta_base(p, offset, kind, obj_offset, &base_offset);
1138
base = unpack_entry_gently(p, base_offset, type, &base_size);
1139
if (!base)
1140
die("failed to read delta base object at %lu from %s",
1171
void *unpack_entry_gently(struct packed_git *p, unsigned long offset,
1172
char *type, unsigned long *sizep)
1173
{
1139
- unsigned long size;
1174
+ unsigned long size, obj_offset = offset;
1175
enum object_type kind;
1176
1177
offset = unpack_object_header(p, offset, &kind, &size);
1178
switch (kind) {
1144
- case OBJ_DELTA:
1145
- return unpack_delta_entry(p, offset, size, type, sizep);
1179
+ case OBJ_OFS_DELTA:
1180
+ case OBJ_REF_DELTA:
1181
+ return unpack_delta_entry(p, offset, size, kind, obj_offset, type, sizep);
1182
case OBJ_COMMIT:
1183
case OBJ_TREE:
1184
case OBJ_BLOB: