Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /* See Documentation/technical/pack-format.txt in Git. */
19 struct got_pack_obj_id {
20 u_int8_t sha1[SHA1_DIGEST_LENGTH];
21 } __attribute__((__packed__));
23 struct got_packidx_trailer {
24 u_int8_t pack_file_sha1[SHA1_DIGEST_LENGTH];
25 u_int8_t pack_idx_sha1[SHA1_DIGEST_LENGTH];
26 } __attribute__((__packed__));
28 /* Ignore pack index version 1 which is no longer written by Git. */
29 #define GOT_PACKIDX_VERSION 2
31 struct got_packidx_v2_hdr {
32 uint32_t magic; /* big endian */
33 #define GOT_PACKIDX_V2_MAGIC 0xff744f63 /* "\377t0c" */
34 uint32_t version;
36 /*
37 * Each entry N in the fanout table contains the number of objects in
38 * the packfile whose SHA1 begins with a byte less than or equal to N.
39 * The last entry (index 255) contains the number of objects in the
40 * pack file whose first SHA1 byte is <= 0xff, and thus records the
41 * total number of objects in the pack file. All pointer variables
42 * below point to tables with a corresponding number of entries.
43 */
44 uint32_t fanout_table[0xff + 1]; /* values are big endian */
46 /* Sorted SHA1 checksums for each object in the pack file. */
47 struct got_pack_obj_id *sorted_ids;
49 /* Offset into the pack file for each object. */
50 uint32_t *offsets; /* values are big endian */
51 #define GOT_PACKIDX_OFFSET_VAL_MASK 0x7fffffff
52 #define GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX 0x80000000
54 /* CRC32 of the packed representation of each object. */
55 uint32_t *crc32;
57 /* Large offsets table is empty for pack files < 2 GB. */
58 uint64_t *large_offsets; /* values are big endian */
60 struct got_packidx_trailer trailer;
61 };
63 struct got_packfile_hdr {
64 uint32_t signature;
65 #define GOT_PACKFILE_SIGNATURE 0x5041434b /* 'P' 'A' 'C' 'K' */
66 uint32_t version; /* big endian */
67 #define GOT_PACKFILE_VERSION 2
68 uint32_t nobjects; /* big endian */
69 };
71 struct got_packfile_obj_hdr {
72 /*
73 * The object size field uses a variable length encoding:
74 * size0...sizeN form a 4+7+7+...+7 bit integer, where size0 is the
75 * least significant part and sizeN is the most significant part.
76 * If the MSB of a size byte is set, an additional size byte follows.
77 * Of the 7 remaining bits of size0, the first 3 bits indicate the
78 * object's type, and the remaining 4 bits contribute to the size.
79 */
80 uint8_t *size; /* variable length */
81 #define GOT_PACK_OBJ_SIZE_MORE 0x80
82 #define GOT_PACK_OBJ_SIZE0_TYPE_MASK 0x70 /* See struct got_object->type */
83 #define GOT_PACK_OBJ_SIZE0_VAL_MASK 0x0f
84 #define GOT_PACK_OBJ_SIZEN_VAL_MASK 0x7f
85 };
87 /* If object is not a DELTA type. */
88 struct got_packfile_object_data {
89 uint8_t *data; /* compressed */
90 };
92 /* If object is of type GOT_OBJ_TYPE_REF_DELTA. */
93 struct got_packfile_object_data_ref_delta {
94 struct got_pack_obj_id id;
95 uint8_t *delta_data; /* compressed */
96 };
98 /* If object is of type GOT_OBJ_TYPE_OFFSET_DELTA. */
99 struct got_packfile_object_data_offset_delta {
100 /*
101 * This offset is interpreted as a negative offset from
102 * the got_packfile_obj_hdr corresponding to this object.
103 * The size provided in the header specifies the amount
104 * of compressed delta data that follows.
106 * This field uses a variable length encoding of N bytes,
107 * where the MSB is always set except for the last byte.
108 * The value is encoded as a series of N 7 bit integers,
109 * which are concatenated, and if N > 1 the value 2^7 +
110 * 2^14 + ... + 2^(7 * (n-1)) is added to the result.
111 */
112 uint8_t *offset; /* variable length */
113 };
115 struct got_packfile_obj_data {
116 union {
117 struct got_packfile_object_data data;
118 struct got_packfile_object_data_ref_delta ref_delta;
119 struct got_packfile_object_data_offset_delta offset_delta;
120 } __attribute__((__packed__));
121 } __attribute__((__packed__));
123 const struct got_error *got_packidx_open(struct got_packidx_v2_hdr **,
124 const char *);
125 void got_packidx_close(struct got_packidx_v2_hdr *);