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