gitweb: Use --git-dir parameter instead of setting $ENV{'GIT_DIR'}
[gitweb.git] / sha1_file.c
index 3d7358fe727453b6912098940dd139919f2e73c5..46272b591645e444396ae683673620594f336a7c 100644 (file)
@@ -917,23 +917,19 @@ static unsigned long unpack_object_header(struct packed_git *p, unsigned long of
        enum object_type *type, unsigned long *sizep)
 {
        unsigned shift;
-       unsigned char *pack, c;
+       unsigned char c;
        unsigned long size;
 
        if (offset >= p->pack_size)
                die("object offset outside of pack file");
-
-       pack =  (unsigned char *) p->pack_base + offset;
-       c = *pack++;
-       offset++;
+       c = *((unsigned char *)p->pack_base + offset++);
        *type = (c >> 4) & 7;
        size = c & 15;
        shift = 4;
        while (c & 0x80) {
                if (offset >= p->pack_size)
                        die("object offset outside of pack file");
-               c = *pack++;
-               offset++;
+               c = *((unsigned char *)p->pack_base + offset++);
                size += (c & 0x7f) << shift;
                shift += 7;
        }
@@ -996,16 +992,10 @@ void packed_object_info_detail(struct pack_entry *e,
        }
        switch (kind) {
        case OBJ_COMMIT:
-               strcpy(type, commit_type);
-               break;
        case OBJ_TREE:
-               strcpy(type, tree_type);
-               break;
        case OBJ_BLOB:
-               strcpy(type, blob_type);
-               break;
        case OBJ_TAG:
-               strcpy(type, tag_type);
+               strcpy(type, type_names[kind]);
                break;
        default:
                die("corrupted pack file %s containing object of kind %d",
@@ -1036,16 +1026,10 @@ static int packed_object_info(struct pack_entry *entry,
                unuse_packed_git(p);
                return retval;
        case OBJ_COMMIT:
-               strcpy(type, commit_type);
-               break;
        case OBJ_TREE:
-               strcpy(type, tree_type);
-               break;
        case OBJ_BLOB:
-               strcpy(type, blob_type);
-               break;
        case OBJ_TAG:
-               strcpy(type, tag_type);
+               strcpy(type, type_names[kind]);
                break;
        default:
                die("corrupted pack file %s containing object of kind %d",
@@ -1057,9 +1041,9 @@ static int packed_object_info(struct pack_entry *entry,
        return 0;
 }
 
-static void *unpack_compressed_entry(unsigned char *data,
-                                   unsigned long size,
-                                   unsigned long left)
+static void *unpack_compressed_entry(struct packed_git *p,
+                                   unsigned long offset,
+                                   unsigned long size)
 {
        int st;
        z_stream stream;
@@ -1068,8 +1052,8 @@ static void *unpack_compressed_entry(unsigned char *data,
        buffer = xmalloc(size + 1);
        buffer[size] = 0;
        memset(&stream, 0, sizeof(stream));
-       stream.next_in = data;
-       stream.avail_in = left;
+       stream.next_in = (unsigned char*)p->pack_base + offset;
+       stream.avail_in = p->pack_size - offset;
        stream.next_out = buffer;
        stream.avail_out = size;
 
@@ -1084,21 +1068,22 @@ static void *unpack_compressed_entry(unsigned char *data,
        return buffer;
 }
 
-static void *unpack_delta_entry(unsigned char *base_sha1,
+static void *unpack_delta_entry(struct packed_git *p,
+                               unsigned long offset,
                                unsigned long delta_size,
-                               unsigned long left,
                                char *type,
-                               unsigned long *sizep,
-                               struct packed_git *p)
+                               unsigned long *sizep)
 {
        struct pack_entry base_ent;
        void *delta_data, *result, *base;
        unsigned long result_size, base_size;
+       unsigned char* base_sha1;
 
-       if (left < 20)
+       if ((offset + 20) >= p->pack_size)
                die("truncated pack file");
 
        /* The base entry _must_ be in the same pack */
+       base_sha1 = (unsigned char*)p->pack_base + offset;
        if (!find_pack_entry_one(base_sha1, &base_ent, p))
                die("failed to find delta-pack base object %s",
                    sha1_to_hex(base_sha1));
@@ -1107,8 +1092,7 @@ static void *unpack_delta_entry(unsigned char *base_sha1,
                die("failed to read delta-pack base object %s",
                    sha1_to_hex(base_sha1));
 
-       delta_data = unpack_compressed_entry(base_sha1 + 20,
-                            delta_size, left - 20);
+       delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
        result = patch_delta(base, base_size,
                             delta_data, delta_size,
                             &result_size);
@@ -1140,36 +1124,23 @@ void *unpack_entry_gently(struct pack_entry *entry,
                          char *type, unsigned long *sizep)
 {
        struct packed_git *p = entry->p;
-       unsigned long offset, size, left;
-       unsigned char *pack;
+       unsigned long offset, size;
        enum object_type kind;
-       void *retval;
 
        offset = unpack_object_header(p, entry->offset, &kind, &size);
-       pack = (unsigned char *) p->pack_base + offset;
-       left = p->pack_size - offset;
        switch (kind) {
        case OBJ_DELTA:
-               retval = unpack_delta_entry(pack, size, left, type, sizep, p);
-               return retval;
+               return unpack_delta_entry(p, offset, size, type, sizep);
        case OBJ_COMMIT:
-               strcpy(type, commit_type);
-               break;
        case OBJ_TREE:
-               strcpy(type, tree_type);
-               break;
        case OBJ_BLOB:
-               strcpy(type, blob_type);
-               break;
        case OBJ_TAG:
-               strcpy(type, tag_type);
-               break;
+               strcpy(type, type_names[kind]);
+               *sizep = size;
+               return unpack_compressed_entry(p, offset, size);
        default:
                return NULL;
        }
-       *sizep = size;
-       retval = unpack_compressed_entry(pack, size, left);
-       return retval;
 }
 
 int num_packed_objects(const struct packed_git *p)
@@ -1769,7 +1740,7 @@ int read_pipe(int fd, char** return_buf, unsigned long* return_size)
                        off += iret;
                        if (off == size) {
                                size *= 2;
-                               buf = realloc(buf, size);
+                               buf = xrealloc(buf, size);
                        }
                }
        } while (iret > 0);