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