@matheusbsbs / matheus-bagliano / commits / eb950c192a
Fork 0

Convert unpack_entry_gently and friends to use offsets.

Change unpack_entry_gently and its helper functions to use offsets rather than addresses and left counts to supply pack position information. In most cases this makes the code easier to follow, and it reduces the number of local variables in a few functions. It also better prepares this code for mapping partial segments of packs and altering what regions of a pack are mapped while unpacking an entry. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed Aug 26, 2006 at 04:12 UTC eb950c192a0e0dfc069e16e857cbe1ae16d8ad82
1 file changed +15 -18
sha1_file.c
+15 -18
@@ -1041,9 +1041,9 @@ static int packed_object_info(struct pack_entry *entry,
1041 return 0;
1042 }
1043
1044 -static void *unpack_compressed_entry(unsigned char *data,
1045 - unsigned long size,
1046 - unsigned long left)
1044 +static void *unpack_compressed_entry(struct packed_git *p,
1045 + unsigned long offset,
1046 + unsigned long size)
1047 {
1048 int st;
1049 z_stream stream;
@@ -1052,8 +1052,8 @@ static void *unpack_compressed_entry(unsigned char *data,
1052 buffer = xmalloc(size + 1);
1053 buffer[size] = 0;
1054 memset(&stream, 0, sizeof(stream));
1055 - stream.next_in = data;
1056 - stream.avail_in = left;
1055 + stream.next_in = (unsigned char*)p->pack_base + offset;
1056 + stream.avail_in = p->pack_size - offset;
1057 stream.next_out = buffer;
1058 stream.avail_out = size;
1059
@@ -1068,21 +1068,22 @@ static void *unpack_compressed_entry(unsigned char *data,
1068 return buffer;
1069 }
1070
1071 -static void *unpack_delta_entry(unsigned char *base_sha1,
1071 +static void *unpack_delta_entry(struct packed_git *p,
1072 + unsigned long offset,
1073 unsigned long delta_size,
1073 - unsigned long left,
1074 char *type,
1075 - unsigned long *sizep,
1076 - struct packed_git *p)
1075 + unsigned long *sizep)
1076 {
1077 struct pack_entry base_ent;
1078 void *delta_data, *result, *base;
1079 unsigned long result_size, base_size;
1080 + unsigned char* base_sha1;
1081
1082 - if (left < 20)
1082 + if ((offset + 20) >= p->pack_size)
1083 die("truncated pack file");
1084
1085 /* The base entry _must_ be in the same pack */
1086 + base_sha1 = (unsigned char*)p->pack_base + offset;
1087 if (!find_pack_entry_one(base_sha1, &base_ent, p))
1088 die("failed to find delta-pack base object %s",
1089 sha1_to_hex(base_sha1));
@@ -1091,8 +1092,7 @@ static void *unpack_delta_entry(unsigned char *base_sha1,
1092 die("failed to read delta-pack base object %s",
1093 sha1_to_hex(base_sha1));
1094
1094 - delta_data = unpack_compressed_entry(base_sha1 + 20,
1095 - delta_size, left - 20);
1095 + delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
1096 result = patch_delta(base, base_size,
1097 delta_data, delta_size,
1098 &result_size);
@@ -1124,23 +1124,20 @@ void *unpack_entry_gently(struct pack_entry *entry,
1124 char *type, unsigned long *sizep)
1125 {
1126 struct packed_git *p = entry->p;
1127 - unsigned long offset, size, left;
1128 - unsigned char *pack;
1127 + unsigned long offset, size;
1128 enum object_type kind;
1129
1130 offset = unpack_object_header(p, entry->offset, &kind, &size);
1132 - pack = (unsigned char *) p->pack_base + offset;
1133 - left = p->pack_size - offset;
1131 switch (kind) {
1132 case OBJ_DELTA:
1136 - return unpack_delta_entry(pack, size, left, type, sizep, p);
1133 + return unpack_delta_entry(p, offset, size, type, sizep);
1134 case OBJ_COMMIT:
1135 case OBJ_TREE:
1136 case OBJ_BLOB:
1137 case OBJ_TAG:
1138 strcpy(type, type_names[kind]);
1139 *sizep = size;
1143 - return unpack_compressed_entry(pack, size, left);
1140 + return unpack_compressed_entry(p, offset, size);
1141 default:
1142 return NULL;
1143 }