-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.js
More file actions
248 lines (200 loc) · 6.08 KB
/
Copy pathpack.js
File metadata and controls
248 lines (200 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const zlib = require("zlib");
const { fatal } = require("./errors");
const SIGNATURE = "PACK";
const SUPPORTED_VERSION = 2;
const HEADER_BYTES = 12;
const SHA_BYTES = 20;
const OBJ_COMMIT = 1;
const OBJ_TREE = 2;
const OBJ_BLOB = 3;
const OBJ_TAG = 4;
const OBJ_OFS_DELTA = 6;
const OBJ_REF_DELTA = 7;
const TYPE_NAMES = {
[OBJ_COMMIT]: "commit",
[OBJ_TREE]: "tree",
[OBJ_BLOB]: "blob",
[OBJ_TAG]: "tag",
};
const COPY_INSTRUCTION = 0x80;
const CONTINUATION = 0x80;
const LOW_SEVEN = 0x7f;
const DEFAULT_COPY_SIZE = 0x10000;
function readHeader(buffer) {
if (buffer.length < HEADER_BYTES || buffer.subarray(0, 4).toString("ascii") !== SIGNATURE) {
throw fatal("invalid packfile signature");
}
const version = buffer.readUInt32BE(4);
if (version !== SUPPORTED_VERSION) {
throw fatal(`unsupported packfile version ${version}`);
}
return { count: buffer.readUInt32BE(8), offset: HEADER_BYTES };
}
function readTypeAndSize(buffer, start) {
let offset = start;
let byte = buffer[offset];
offset += 1;
const type = (byte >> 4) & 0x07;
let size = byte & 0x0f;
let shift = 4;
while (byte & CONTINUATION) {
byte = buffer[offset];
offset += 1;
size += (byte & LOW_SEVEN) * 2 ** shift;
shift += 7;
}
return { type, size, offset };
}
function readVariableLength(buffer, start) {
let offset = start;
let value = 0;
let shift = 0;
let byte;
do {
byte = buffer[offset];
offset += 1;
value += (byte & LOW_SEVEN) * 2 ** shift;
shift += 7;
} while (byte & CONTINUATION);
return { value, offset };
}
function readNegativeOffset(buffer, start) {
let offset = start;
let byte = buffer[offset];
offset += 1;
let value = byte & LOW_SEVEN;
while (byte & CONTINUATION) {
byte = buffer[offset];
offset += 1;
value = (value + 1) * 128 + (byte & LOW_SEVEN);
}
return { value, offset };
}
function inflateAt(buffer, offset) {
let result;
try {
result = zlib.inflateSync(buffer.subarray(offset), { info: true });
} catch {
throw fatal("failed to inflate packfile entry");
}
return { data: result.buffer, next: offset + result.engine.bytesWritten };
}
function applyDelta(base, delta) {
const { value: baseSize, offset: afterBase } = readVariableLength(delta, 0);
const { value: resultSize, offset: afterResult } = readVariableLength(delta, afterBase);
if (baseSize !== base.length) {
throw fatal("delta base size mismatch");
}
const parts = [];
let offset = afterResult;
let produced = 0;
while (offset < delta.length) {
const instruction = delta[offset];
offset += 1;
if (instruction & COPY_INSTRUCTION) {
let copyOffset = 0;
let copySize = 0;
for (let bit = 0; bit < 4; bit += 1) {
if (instruction & (1 << bit)) {
copyOffset |= delta[offset] << (bit * 8);
offset += 1;
}
}
for (let bit = 0; bit < 3; bit += 1) {
if (instruction & (1 << (bit + 4))) {
copySize |= delta[offset] << (bit * 8);
offset += 1;
}
}
copyOffset >>>= 0;
if (copySize === 0) {
copySize = DEFAULT_COPY_SIZE;
}
parts.push(base.subarray(copyOffset, copyOffset + copySize));
produced += copySize;
} else if (instruction !== 0) {
parts.push(delta.subarray(offset, offset + instruction));
offset += instruction;
produced += instruction;
} else {
throw fatal("invalid delta instruction");
}
}
if (produced !== resultSize) {
throw fatal("delta produced the wrong result size");
}
return Buffer.concat(parts, resultSize);
}
function readEntries(buffer) {
const { count, offset: start } = readHeader(buffer);
const entries = [];
let offset = start;
for (let index = 0; index < count; index += 1) {
const entryOffset = offset;
const { type, offset: afterType } = readTypeAndSize(buffer, offset);
offset = afterType;
let baseOffset = null;
let baseSha = null;
if (type === OBJ_OFS_DELTA) {
const negative = readNegativeOffset(buffer, offset);
baseOffset = entryOffset - negative.value;
offset = negative.offset;
} else if (type === OBJ_REF_DELTA) {
baseSha = buffer.subarray(offset, offset + SHA_BYTES).toString("hex");
offset += SHA_BYTES;
}
const { data, next } = inflateAt(buffer, offset);
offset = next;
entries.push({ offset: entryOffset, type, baseOffset, baseSha, data });
}
return entries;
}
function unpack(buffer, shaOf, lookup = null) {
const entries = readEntries(buffer);
const byOffset = new Map(entries.map((entry) => [entry.offset, entry]));
const resolved = new Map();
const offsetBySha = new Map();
const objects = [];
const store = (entry, type, data) => {
resolved.set(entry.offset, { type, data });
const sha = shaOf(type, data);
offsetBySha.set(sha, entry.offset);
objects.push({ sha, type, data });
};
let remaining = entries.filter((entry) => {
if (entry.type === OBJ_OFS_DELTA || entry.type === OBJ_REF_DELTA) {
return true;
}
const type = TYPE_NAMES[entry.type];
if (!type) {
throw fatal(`unsupported packfile object type ${entry.type}`);
}
store(entry, type, entry.data);
return false;
});
while (remaining.length > 0) {
const pending = [];
for (const entry of remaining) {
const baseOffset =
entry.baseOffset !== null ? entry.baseOffset : offsetBySha.get(entry.baseSha);
let base = baseOffset === undefined ? undefined : resolved.get(baseOffset);
if (!base && entry.baseSha && lookup) {
base = lookup(entry.baseSha);
}
if (!base) {
if (baseOffset !== undefined && !byOffset.has(baseOffset)) {
throw fatal(`packfile refers to a missing base object`);
}
pending.push(entry);
continue;
}
store(entry, base.type, applyDelta(base.data, entry.data));
}
if (pending.length === remaining.length) {
throw fatal("packfile contains deltas whose base objects are missing");
}
remaining = pending;
}
return objects;
}
module.exports = { unpack, applyDelta, readEntries };