Blame


1 32cb896c 2018-03-11 stsp /*
2 32cb896c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 32cb896c 2018-03-11 stsp *
4 32cb896c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 32cb896c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 32cb896c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 32cb896c 2018-03-11 stsp *
8 32cb896c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 32cb896c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 32cb896c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 32cb896c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 32cb896c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 32cb896c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 32cb896c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 32cb896c 2018-03-11 stsp */
16 32cb896c 2018-03-11 stsp
17 f7e127f3 2018-03-17 stsp struct got_delta_cache_entry {
18 f7e127f3 2018-03-17 stsp off_t data_offset;
19 f7e127f3 2018-03-17 stsp uint8_t *delta_buf;
20 f7e127f3 2018-03-17 stsp size_t delta_len;
21 f7e127f3 2018-03-17 stsp };
22 f7e127f3 2018-03-17 stsp
23 f7e127f3 2018-03-17 stsp #define GOT_DELTA_CACHE_SIZE 1024
24 f7e127f3 2018-03-17 stsp
25 f7e127f3 2018-03-17 stsp struct got_delta_cache {
26 e1ad6ebc 2018-03-17 stsp int nentries;
27 f7e127f3 2018-03-17 stsp struct got_delta_cache_entry deltas[GOT_DELTA_CACHE_SIZE];
28 f7e127f3 2018-03-17 stsp };
29 f7e127f3 2018-03-17 stsp
30 7e656b93 2018-03-17 stsp /* A pack file segment mapped with mmap(2). */
31 7e656b93 2018-03-17 stsp struct got_pack_mapping {
32 7e656b93 2018-03-17 stsp TAILQ_ENTRY(got_pack_mapping) entry;
33 7e656b93 2018-03-17 stsp int fd;
34 7e656b93 2018-03-17 stsp uint8_t *addr;
35 7e656b93 2018-03-17 stsp off_t offset;
36 7e656b93 2018-03-17 stsp size_t len;
37 7e656b93 2018-03-17 stsp };
38 7e656b93 2018-03-17 stsp
39 7e656b93 2018-03-17 stsp #define GOT_PACK_MAX_OPEN_MAPPINGS 512
40 7e656b93 2018-03-17 stsp #define GOT_PACK_MAPPING_MIN_SIZE 8192
41 7e656b93 2018-03-17 stsp #define GOT_PACK_MAPPING_MAX_SIZE (2048 * GOT_PACK_MAPPING_MIN_SIZE)
42 7e656b93 2018-03-17 stsp
43 7e656b93 2018-03-17 stsp /* An open pack file. */
44 7e656b93 2018-03-17 stsp struct got_pack {
45 7e656b93 2018-03-17 stsp char *path_packfile;
46 7e656b93 2018-03-17 stsp FILE *packfile;
47 7e656b93 2018-03-17 stsp size_t filesize;
48 7e656b93 2018-03-17 stsp int nmappings;
49 f7e127f3 2018-03-17 stsp
50 7e656b93 2018-03-17 stsp TAILQ_HEAD(, got_pack_mapping) mappings;
51 f7e127f3 2018-03-17 stsp
52 f7e127f3 2018-03-17 stsp struct got_delta_cache delta_cache;
53 7e656b93 2018-03-17 stsp };
54 7e656b93 2018-03-17 stsp
55 7e656b93 2018-03-17 stsp const struct got_error *got_pack_close(struct got_pack *);
56 7e656b93 2018-03-17 stsp
57 32cb896c 2018-03-11 stsp /* See Documentation/technical/pack-format.txt in Git. */
58 32cb896c 2018-03-11 stsp
59 32cb896c 2018-03-11 stsp struct got_packidx_trailer {
60 32cb896c 2018-03-11 stsp u_int8_t packfile_sha1[SHA1_DIGEST_LENGTH];
61 32cb896c 2018-03-11 stsp u_int8_t packidx_sha1[SHA1_DIGEST_LENGTH];
62 32cb896c 2018-03-11 stsp } __attribute__((__packed__));
63 32cb896c 2018-03-11 stsp
64 32cb896c 2018-03-11 stsp /* Ignore pack index version 1 which is no longer written by Git. */
65 32cb896c 2018-03-11 stsp #define GOT_PACKIDX_VERSION 2
66 32cb896c 2018-03-11 stsp
67 32cb896c 2018-03-11 stsp struct got_packidx_v2_hdr {
68 32cb896c 2018-03-11 stsp uint32_t magic; /* big endian */
69 32cb896c 2018-03-11 stsp #define GOT_PACKIDX_V2_MAGIC 0xff744f63 /* "\377t0c" */
70 32cb896c 2018-03-11 stsp uint32_t version;
71 32cb896c 2018-03-11 stsp
72 32cb896c 2018-03-11 stsp /*
73 32cb896c 2018-03-11 stsp * Each entry N in the fanout table contains the number of objects in
74 32cb896c 2018-03-11 stsp * the packfile whose SHA1 begins with a byte less than or equal to N.
75 32cb896c 2018-03-11 stsp * The last entry (index 255) contains the number of objects in the
76 32cb896c 2018-03-11 stsp * pack file whose first SHA1 byte is <= 0xff, and thus records the
77 32cb896c 2018-03-11 stsp * total number of objects in the pack file. All pointer variables
78 32cb896c 2018-03-11 stsp * below point to tables with a corresponding number of entries.
79 32cb896c 2018-03-11 stsp */
80 32cb896c 2018-03-11 stsp uint32_t fanout_table[0xff + 1]; /* values are big endian */
81 32cb896c 2018-03-11 stsp
82 32cb896c 2018-03-11 stsp /* Sorted SHA1 checksums for each object in the pack file. */
83 32cb896c 2018-03-11 stsp struct got_object_id *sorted_ids;
84 32cb896c 2018-03-11 stsp
85 32cb896c 2018-03-11 stsp /* CRC32 of the packed representation of each object. */
86 32cb896c 2018-03-11 stsp uint32_t *crc32;
87 32cb896c 2018-03-11 stsp
88 32cb896c 2018-03-11 stsp /* Offset into the pack file for each object. */
89 32cb896c 2018-03-11 stsp uint32_t *offsets; /* values are big endian */
90 32cb896c 2018-03-11 stsp #define GOT_PACKIDX_OFFSET_VAL_MASK 0x7fffffff
91 32cb896c 2018-03-11 stsp #define GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX 0x80000000
92 32cb896c 2018-03-11 stsp
93 32cb896c 2018-03-11 stsp /* Large offsets table is empty for pack files < 2 GB. */
94 32cb896c 2018-03-11 stsp uint64_t *large_offsets; /* values are big endian */
95 32cb896c 2018-03-11 stsp
96 32cb896c 2018-03-11 stsp struct got_packidx_trailer trailer;
97 32cb896c 2018-03-11 stsp };
98 32cb896c 2018-03-11 stsp
99 32cb896c 2018-03-11 stsp struct got_packfile_hdr {
100 32cb896c 2018-03-11 stsp uint32_t signature;
101 32cb896c 2018-03-11 stsp #define GOT_PACKFILE_SIGNATURE 0x5041434b /* 'P' 'A' 'C' 'K' */
102 32cb896c 2018-03-11 stsp uint32_t version; /* big endian */
103 32cb896c 2018-03-11 stsp #define GOT_PACKFILE_VERSION 2
104 32cb896c 2018-03-11 stsp uint32_t nobjects; /* big endian */
105 32cb896c 2018-03-11 stsp };
106 32cb896c 2018-03-11 stsp
107 32cb896c 2018-03-11 stsp struct got_packfile_obj_hdr {
108 32cb896c 2018-03-11 stsp /*
109 32cb896c 2018-03-11 stsp * The object size field uses a variable length encoding:
110 32cb896c 2018-03-11 stsp * size0...sizeN form a 4+7+7+...+7 bit integer, where size0 is the
111 32cb896c 2018-03-11 stsp * least significant part and sizeN is the most significant part.
112 32cb896c 2018-03-11 stsp * If the MSB of a size byte is set, an additional size byte follows.
113 32cb896c 2018-03-11 stsp * Of the 7 remaining bits of size0, the first 3 bits indicate the
114 32cb896c 2018-03-11 stsp * object's type, and the remaining 4 bits contribute to the size.
115 32cb896c 2018-03-11 stsp */
116 32cb896c 2018-03-11 stsp uint8_t *size; /* variable length */
117 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_SIZE_MORE 0x80
118 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_SIZE0_TYPE_MASK 0x70 /* See struct got_object->type */
119 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT 4
120 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_SIZE0_VAL_MASK 0x0f
121 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_SIZE_VAL_MASK 0x7f
122 32cb896c 2018-03-11 stsp };
123 32cb896c 2018-03-11 stsp
124 32cb896c 2018-03-11 stsp /* If object is not a DELTA type. */
125 32cb896c 2018-03-11 stsp struct got_packfile_object_data {
126 32cb896c 2018-03-11 stsp uint8_t *data; /* compressed */
127 32cb896c 2018-03-11 stsp };
128 32cb896c 2018-03-11 stsp
129 32cb896c 2018-03-11 stsp /* If object is of type GOT_OBJ_TYPE_REF_DELTA. */
130 32cb896c 2018-03-11 stsp struct got_packfile_object_data_ref_delta {
131 32cb896c 2018-03-11 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
132 32cb896c 2018-03-11 stsp uint8_t *delta_data; /* compressed */
133 32cb896c 2018-03-11 stsp };
134 32cb896c 2018-03-11 stsp
135 32cb896c 2018-03-11 stsp /* If object is of type GOT_OBJ_TYPE_OFFSET_DELTA. */
136 32cb896c 2018-03-11 stsp struct got_packfile_object_data_offset_delta {
137 32cb896c 2018-03-11 stsp /*
138 32cb896c 2018-03-11 stsp * This offset is interpreted as a negative offset from
139 32cb896c 2018-03-11 stsp * the got_packfile_obj_hdr corresponding to this object.
140 32cb896c 2018-03-11 stsp * The size provided in the header specifies the amount
141 32cb896c 2018-03-11 stsp * of compressed delta data that follows.
142 32cb896c 2018-03-11 stsp *
143 32cb896c 2018-03-11 stsp * This field uses a variable length encoding of N bytes,
144 32cb896c 2018-03-11 stsp * where the MSB is always set except for the last byte.
145 32cb896c 2018-03-11 stsp * The value is encoded as a series of N 7 bit integers,
146 32cb896c 2018-03-11 stsp * which are concatenated, and if N > 1 the value 2^7 +
147 32cb896c 2018-03-11 stsp * 2^14 + ... + 2^(7 * (n-1)) is added to the result.
148 32cb896c 2018-03-11 stsp */
149 32cb896c 2018-03-11 stsp uint8_t *offset; /* variable length */
150 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_DELTA_OFF_MORE 0x80
151 32cb896c 2018-03-11 stsp #define GOT_PACK_OBJ_DELTA_OFF_VAL_MASK 0x7f
152 32cb896c 2018-03-11 stsp uint8_t *delta_data; /* compressed */
153 32cb896c 2018-03-11 stsp };
154 32cb896c 2018-03-11 stsp
155 32cb896c 2018-03-11 stsp struct got_packfile_obj_data {
156 32cb896c 2018-03-11 stsp union {
157 32cb896c 2018-03-11 stsp struct got_packfile_object_data data;
158 32cb896c 2018-03-11 stsp struct got_packfile_object_data_ref_delta ref_delta;
159 32cb896c 2018-03-11 stsp struct got_packfile_object_data_offset_delta offset_delta;
160 32cb896c 2018-03-11 stsp } __attribute__((__packed__));
161 32cb896c 2018-03-11 stsp } __attribute__((__packed__));
162 32cb896c 2018-03-11 stsp
163 32cb896c 2018-03-11 stsp const struct got_error *got_packidx_open(struct got_packidx_v2_hdr **,
164 32cb896c 2018-03-11 stsp const char *);
165 32cb896c 2018-03-11 stsp void got_packidx_close(struct got_packidx_v2_hdr *);
166 32cb896c 2018-03-11 stsp
167 32cb896c 2018-03-11 stsp const struct got_error *got_packfile_open_object(struct got_object **,
168 32cb896c 2018-03-11 stsp struct got_object_id *, struct got_repository *);
169 32cb896c 2018-03-11 stsp const struct got_error *got_packfile_extract_object(FILE **,
170 32cb896c 2018-03-11 stsp struct got_object *, struct got_repository *);
171 e0ab43e7 2018-03-16 stsp const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
172 e0ab43e7 2018-03-16 stsp struct got_object *, struct got_repository *);