Blame


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