Blame


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