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 /* An open pack file. */
18 struct got_pack {
19 char *path_packfile;
20 int fd;
21 size_t filesize;
22 };
24 void got_pack_close(struct got_pack *);
26 /* See Documentation/technical/pack-format.txt in Git. */
28 struct got_packidx_trailer {
29 u_int8_t packfile_sha1[SHA1_DIGEST_LENGTH];
30 u_int8_t packidx_sha1[SHA1_DIGEST_LENGTH];
31 } __attribute__((__packed__));
33 struct got_packidx_object_id {
34 u_int8_t sha1[SHA1_DIGEST_LENGTH];
35 } __attribute__((__packed__));
37 /* Ignore pack index version 1 which is no longer written by Git. */
38 #define GOT_PACKIDX_VERSION 2
40 struct got_packidx_v2_hdr {
41 uint32_t *magic; /* big endian */
42 #define GOT_PACKIDX_V2_MAGIC 0xff744f63 /* "\377t0c" */
43 uint32_t *version;
45 /*
46 * Each entry N in the fanout table contains the number of objects in
47 * the packfile whose SHA1 begins with a byte less than or equal to N.
48 * The last entry (index 255) contains the number of objects in the
49 * pack file whose first SHA1 byte is <= 0xff, and thus records the
50 * total number of objects in the pack file. All pointer variables
51 * below point to tables with a corresponding number of entries.
52 */
53 uint32_t *fanout_table; /* values are big endian */
54 #define GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS (0xff + 1)
56 /* Sorted SHA1 checksums for each object in the pack file. */
57 struct got_packidx_object_id *sorted_ids;
59 /* CRC32 of the packed representation of each object. */
60 uint32_t *crc32;
62 /* Offset into the pack file for each object. */
63 uint32_t *offsets; /* values are big endian */
64 #define GOT_PACKIDX_OFFSET_VAL_MASK 0x7fffffff
65 #define GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX 0x80000000
67 /* Large offsets table is empty for pack files < 2 GB. */
68 uint64_t *large_offsets; /* values are big endian */
70 struct got_packidx_trailer *trailer;
71 };
73 /* An open pack index file. */
74 struct got_packidx {
75 char *path_packidx; /* actual on-disk path */
76 int fd;
77 uint8_t *map;
78 size_t len;
79 struct got_packidx_v2_hdr hdr; /* convenient pointers into map */
80 };
82 struct got_packfile_hdr {
83 uint32_t signature;
84 #define GOT_PACKFILE_SIGNATURE 0x5041434b /* 'P' 'A' 'C' 'K' */
85 uint32_t version; /* big endian */
86 #define GOT_PACKFILE_VERSION 2
87 uint32_t nobjects; /* big endian */
88 };
90 struct got_packfile_obj_hdr {
91 /*
92 * The object size field uses a variable length encoding:
93 * size0...sizeN form a 4+7+7+...+7 bit integer, where size0 is the
94 * least significant part and sizeN is the most significant part.
95 * If the MSB of a size byte is set, an additional size byte follows.
96 * Of the 7 remaining bits of size0, the first 3 bits indicate the
97 * object's type, and the remaining 4 bits contribute to the size.
98 */
99 uint8_t *size; /* variable length */
100 #define GOT_PACK_OBJ_SIZE_MORE 0x80
101 #define GOT_PACK_OBJ_SIZE0_TYPE_MASK 0x70 /* See struct got_object->type */
102 #define GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT 4
103 #define GOT_PACK_OBJ_SIZE0_VAL_MASK 0x0f
104 #define GOT_PACK_OBJ_SIZE_VAL_MASK 0x7f
105 };
107 /* If object is not a DELTA type. */
108 struct got_packfile_object_data {
109 uint8_t *data; /* compressed */
110 };
112 /* If object is of type GOT_OBJ_TYPE_REF_DELTA. */
113 struct got_packfile_object_data_ref_delta {
114 uint8_t sha1[SHA1_DIGEST_LENGTH];
115 uint8_t *delta_data; /* compressed */
116 };
118 /* If object is of type GOT_OBJ_TYPE_OFFSET_DELTA. */
119 struct got_packfile_object_data_offset_delta {
120 /*
121 * This offset is interpreted as a negative offset from
122 * the got_packfile_obj_hdr corresponding to this object.
123 * The size provided in the header specifies the amount
124 * of compressed delta data that follows.
126 * This field uses a variable length encoding of N bytes,
127 * where the MSB is always set except for the last byte.
128 * The value is encoded as a series of N 7 bit integers,
129 * which are concatenated, and if N > 1 the value 2^7 +
130 * 2^14 + ... + 2^(7 * (n-1)) is added to the result.
131 */
132 uint8_t *offset; /* variable length */
133 #define GOT_PACK_OBJ_DELTA_OFF_MORE 0x80
134 #define GOT_PACK_OBJ_DELTA_OFF_VAL_MASK 0x7f
135 uint8_t *delta_data; /* compressed */
136 };
138 struct got_packfile_obj_data {
139 union {
140 struct got_packfile_object_data data;
141 struct got_packfile_object_data_ref_delta ref_delta;
142 struct got_packfile_object_data_offset_delta offset_delta;
143 } __attribute__((__packed__));
144 } __attribute__((__packed__));
146 const struct got_error *got_packidx_open(struct got_packidx **,
147 const char *);
148 const struct got_error* got_packidx_close(struct got_packidx *);
150 const struct got_error *got_packfile_open_object(struct got_object **,
151 struct got_object_id *, struct got_repository *);
152 const struct got_error *got_packfile_extract_object(FILE **,
153 struct got_object *, struct got_repository *);
154 const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
155 struct got_object *, struct got_repository *);