884
}
885
886
/* forward declaration for a mutually recursive function */
887
-static int packed_object_info(struct pack_entry *entry,
887
+static int packed_object_info(struct packed_git *p, unsigned long offset,
888
char *type, unsigned long *sizep);
889
890
-static int packed_delta_info(unsigned char *base_sha1,
891
- unsigned long delta_size,
892
- unsigned long left,
890
+static int packed_delta_info(struct packed_git *p,
891
+ unsigned long offset,
892
char *type,
894
- unsigned long *sizep,
895
- struct packed_git *p)
893
+ unsigned long *sizep)
894
{
897
- struct pack_entry base_ent;
895
+ unsigned long base_offset;
896
+ unsigned char *base_sha1 = (unsigned char *) p->pack_base + offset;
897
899
- if (left < 20)
898
+ if (p->pack_size < offset + 20)
899
die("truncated pack file");
901
-
900
/* The base entry _must_ be in the same pack */
903
- if (!find_pack_entry_one(base_sha1, &base_ent, p))
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;
906
907
/* We choose to only get the type of the base object and
908
* ignore potentially corrupt pack file that expects the delta
909
* based on a base with a wrong size. This saves tons of
910
* inflate() calls.
911
*/
912
-
913
- if (packed_object_info(&base_ent, type, NULL))
912
+ if (packed_object_info(p, base_offset, type, NULL))
913
die("cannot get info for delta-pack base");
914
915
if (sizep) {
921
922
memset(&stream, 0, sizeof(stream));
923
925
- data = stream.next_in = base_sha1 + 20;
926
- stream.avail_in = left - 20;
924
+ stream.next_in = (unsigned char *) p->pack_base + offset;
925
+ stream.avail_in = p->pack_size - offset;
926
stream.next_out = delta_head;
927
stream.avail_out = sizeof(delta_head);
928
984
return status;
985
}
986
988
-void packed_object_info_detail(struct pack_entry *e,
987
+void packed_object_info_detail(struct packed_git *p,
988
+ unsigned long offset,
989
char *type,
990
unsigned long *size,
991
unsigned long *store_size,
992
unsigned int *delta_chain_length,
993
unsigned char *base_sha1)
994
{
995
- struct packed_git *p = e->p;
996
- unsigned long offset;
997
- unsigned char *pack;
995
+ unsigned long val;
996
+ unsigned char *next_sha1;
997
enum object_type kind;
998
1000
- offset = unpack_object_header(p, e->offset, &kind, size);
1001
- pack = (unsigned char *) p->pack_base + offset;
1002
- if (kind != OBJ_DELTA)
1003
- *delta_chain_length = 0;
1004
- else {
1005
- unsigned int chain_length = 0;
1006
- if (p->pack_size <= offset + 20)
1007
- die("pack file %s records an incomplete delta base",
1008
- p->pack_name);
1009
- hashcpy(base_sha1, pack);
1010
- do {
1011
- struct pack_entry base_ent;
1012
- unsigned long junk;
1013
-
1014
- find_pack_entry_one(pack, &base_ent, p);
1015
- offset = unpack_object_header(p, base_ent.offset,
1016
- &kind, &junk);
1017
- pack = (unsigned char *) p->pack_base + offset;
1018
- chain_length++;
1019
- } while (kind == OBJ_DELTA);
1020
- *delta_chain_length = chain_length;
1021
- }
1022
- switch (kind) {
1023
- case OBJ_COMMIT:
1024
- case OBJ_TREE:
1025
- case OBJ_BLOB:
1026
- case OBJ_TAG:
1027
- strcpy(type, type_names[kind]);
1028
- break;
1029
- default:
1030
- die("corrupted pack file %s containing object of kind %d",
1031
- p->pack_name, kind);
999
+ *delta_chain_length = 0;
1000
+ offset = unpack_object_header(p, offset, &kind, size);
1001
+
1002
+ for (;;) {
1003
+ switch (kind) {
1004
+ default:
1005
+ die("corrupted pack file %s containing object of kind %d",
1006
+ p->pack_name, kind);
1007
+ case OBJ_COMMIT:
1008
+ case OBJ_TREE:
1009
+ case OBJ_BLOB:
1010
+ case OBJ_TAG:
1011
+ strcpy(type, type_names[kind]);
1012
+ *store_size = 0; /* notyet */
1013
+ return;
1014
+ case OBJ_DELTA:
1015
+ if (p->pack_size <= offset + 20)
1016
+ die("pack file %s records an incomplete delta base",
1017
+ p->pack_name);
1018
+ next_sha1 = (unsigned char *) p->pack_base + offset;
1019
+ if (*delta_chain_length == 0)
1020
+ hashcpy(base_sha1, next_sha1);
1021
+ offset = find_pack_entry_one(next_sha1, p);
1022
+ break;
1023
+ }
1024
+ offset = unpack_object_header(p, offset, &kind, &val);
1025
+ (*delta_chain_length)++;
1026
}
1033
- *store_size = 0; /* notyet */
1027
}
1028
1036
-static int packed_object_info(struct pack_entry *entry,
1029
+static int packed_object_info(struct packed_git *p, unsigned long offset,
1030
char *type, unsigned long *sizep)
1031
{
1039
- struct packed_git *p = entry->p;
1040
- unsigned long offset, size, left;
1041
- unsigned char *pack;
1032
+ unsigned long size;
1033
enum object_type kind;
1043
- int retval;
1034
1045
- if (use_packed_git(p))
1046
- die("cannot map packed file");
1035
+ offset = unpack_object_header(p, offset, &kind, &size);
1036
1048
- offset = unpack_object_header(p, entry->offset, &kind, &size);
1049
- pack = (unsigned char *) p->pack_base + offset;
1050
- left = p->pack_size - offset;
1037
+ if (kind == OBJ_DELTA)
1038
+ return packed_delta_info(p, offset, type, sizep);
1039
1040
switch (kind) {
1053
- case OBJ_DELTA:
1054
- retval = packed_delta_info(pack, size, left, type, sizep, p);
1055
- unuse_packed_git(p);
1056
- return retval;
1041
case OBJ_COMMIT:
1042
case OBJ_TREE:
1043
case OBJ_BLOB:
1050
}
1051
if (sizep)
1052
*sizep = size;
1069
- unuse_packed_git(p);
1053
return 0;
1054
}
1055
1086
char *type,
1087
unsigned long *sizep)
1088
{
1106
- struct pack_entry base_ent;
1089
void *delta_data, *result, *base;
1108
- unsigned long result_size, base_size;
1109
- unsigned char* base_sha1;
1090
+ unsigned long result_size, base_size, base_offset;
1091
+ unsigned char *base_sha1;
1092
1111
- if ((offset + 20) >= p->pack_size)
1093
+ if (p->pack_size < offset + 20)
1094
die("truncated pack file");
1113
-
1095
/* The base entry _must_ be in the same pack */
1096
base_sha1 = (unsigned char*)p->pack_base + offset;
1116
- if (!find_pack_entry_one(base_sha1, &base_ent, p))
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));
1119
- base = unpack_entry_gently(&base_ent, type, &base_size);
1101
+ offset += 20;
1102
+
1103
+ base = unpack_entry_gently(p, base_offset, type, &base_size);
1104
if (!base)
1121
- die("failed to read delta-pack base object %s",
1122
- sha1_to_hex(base_sha1));
1105
+ die("failed to read delta base object at %lu from %s",
1106
+ base_offset, p->pack_name);
1107
1124
- delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
1108
+ delta_data = unpack_compressed_entry(p, offset, delta_size);
1109
result = patch_delta(base, base_size,
1110
delta_data, delta_size,
1111
&result_size);
1125
1126
if (use_packed_git(p))
1127
die("cannot map packed file");
1144
- retval = unpack_entry_gently(entry, type, sizep);
1128
+ retval = unpack_entry_gently(p, entry->offset, type, sizep);
1129
unuse_packed_git(p);
1130
if (!retval)
1131
die("corrupted pack file %s", p->pack_name);
1133
}
1134
1135
/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1152
-void *unpack_entry_gently(struct pack_entry *entry,
1136
+void *unpack_entry_gently(struct packed_git *p, unsigned long offset,
1137
char *type, unsigned long *sizep)
1138
{
1155
- struct packed_git *p = entry->p;
1156
- unsigned long offset, size;
1139
+ unsigned long size;
1140
enum object_type kind;
1141
1159
- offset = unpack_object_header(p, entry->offset, &kind, &size);
1142
+ offset = unpack_object_header(p, offset, &kind, &size);
1143
switch (kind) {
1144
case OBJ_DELTA:
1145
return unpack_delta_entry(p, offset, size, type, sizep);
1171
return 0;
1172
}
1173
1191
-int find_pack_entry_one(const unsigned char *sha1,
1192
- struct pack_entry *e, struct packed_git *p)
1174
+unsigned long find_pack_entry_one(const unsigned char *sha1,
1175
+ struct packed_git *p)
1176
{
1177
unsigned int *level1_ofs = p->index_base;
1178
int hi = ntohl(level1_ofs[*sha1]);
1182
do {
1183
int mi = (lo + hi) / 2;
1184
int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1202
- if (!cmp) {
1203
- e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1204
- hashcpy(e->sha1, sha1);
1205
- e->p = p;
1206
- return 1;
1207
- }
1185
+ if (!cmp)
1186
+ return ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1187
if (cmp > 0)
1188
hi = mi;
1189
else
1195
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)
1196
{
1197
struct packed_git *p;
1198
+ unsigned long offset;
1199
+
1200
prepare_packed_git();
1201
1202
for (p = packed_git; p; p = p->next) {
1208
if (*ig)
1209
continue;
1210
}
1230
- if (find_pack_entry_one(sha1, e, p))
1211
+ offset = find_pack_entry_one(sha1, p);
1212
+ if (offset) {
1213
+ e->offset = offset;
1214
+ e->p = p;
1215
+ hashcpy(e->sha1, sha1);
1216
return 1;
1217
+ }
1218
}
1219
return 0;
1220
}
1223
struct packed_git *packs)
1224
{
1225
struct packed_git *p;
1240
- struct pack_entry e;
1226
1227
for (p = packs; p; p = p->next) {
1243
- if (find_pack_entry_one(sha1, &e, p))
1228
+ if (find_pack_entry_one(sha1, p))
1229
return p;
1230
}
1231
return NULL;
1244
if (!map) {
1245
struct pack_entry e;
1246
1262
- if (find_pack_entry(sha1, &e, NULL))
1263
- return packed_object_info(&e, type, sizep);
1264
- reprepare_packed_git();
1265
- if (find_pack_entry(sha1, &e, NULL))
1266
- return packed_object_info(&e, type, sizep);
1267
- return error("unable to find %s", sha1_to_hex(sha1));
1247
+ if (!find_pack_entry(sha1, &e, NULL)) {
1248
+ reprepare_packed_git();
1249
+ if (!find_pack_entry(sha1, &e, NULL))
1250
+ return error("unable to find %s", sha1_to_hex(sha1));
1251
+ }
1252
+ if (use_packed_git(e.p))
1253
+ die("cannot map packed file");
1254
+ status = packed_object_info(e.p, e.offset, type, sizep);
1255
+ unuse_packed_git(e.p);
1256
+ return status;
1257
}
1258
if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1259
status = error("unable to unpack %s header",