summaryrefslogtreecommitdiff
path: root/packv4-parse.c
blob: 6abd62ee6988ee7d39352647820faaeedab7b40d (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
 * Code to parse pack v4 object encoding
 *
 * (C) Nicolas Pitre <nico@fluxnic.net>
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "cache.h"
#include "packv4-parse.h"
#include "tree-walk.h"
#include "varint.h"

const unsigned char *get_sha1ref(struct packed_git *p,
				 const unsigned char **bufp)
{
	const unsigned char *sha1;

	if (!**bufp) {
		sha1 = *bufp + 1;
		*bufp += 21;
	} else {
		unsigned int index = decode_varint(bufp);
		if (index < 1 || index - 1 > p->num_objects)
			die("bad index in %s", __func__);
		sha1 = p->sha1_table + (index - 1) * 20;
	}

	return sha1;
}

struct packv4_dict *pv4_create_dict(const unsigned char *data, int dict_size)
{
	struct packv4_dict *dict;
	int i;

	/* count number of entries */
	int nb_entries = 0;
	const unsigned char *cp = data;
	while (cp < data + dict_size - 3) {
		cp += 2;  /* prefix bytes */
		cp += strlen((const char *)cp);  /* entry string */
		cp += 1;  /* terminating NUL */
		nb_entries++;
	}
	if (cp - data != dict_size) {
		error("dict size mismatch");
		return NULL;
	}

	dict = xmalloc(sizeof(*dict) +
		       (nb_entries + 1) * sizeof(dict->offsets[0]));
	dict->data = data;
	dict->nb_entries = nb_entries;

	dict->offsets[0] = 0;
	cp = data;
	for (i = 0; i < nb_entries; i++) {
		cp += 2;
		cp += strlen((const char *)cp) + 1;
		dict->offsets[i + 1] = cp - data;
	}

	return dict;
}

void pv4_free_dict(struct packv4_dict *dict)
{
	if (dict) {
		free((void*)dict->data);
		free(dict);
	}
}

static struct packv4_dict *load_dict(struct packed_git *p, off_t *offset)
{
	struct pack_window *w_curs = NULL;
	off_t curpos = *offset;
	unsigned long dict_size, avail;
	unsigned char *src, *data;
	const unsigned char *cp;
	git_zstream stream;
	struct packv4_dict *dict;
	int st;

	/* get uncompressed dictionary data size */
	src = use_pack(p, &w_curs, curpos, &avail);
	cp = src;
	dict_size = decode_varint(&cp);
	curpos += cp - src;

	data = xmallocz(dict_size);
	memset(&stream, 0, sizeof(stream));
	stream.next_out = data;
	stream.avail_out = dict_size + 1;

	git_inflate_init(&stream);
	do {
		src = use_pack(p, &w_curs, curpos, &stream.avail_in);
		stream.next_in = src;
		st = git_inflate(&stream, Z_FINISH);
		curpos += stream.next_in - src;
	} while ((st == Z_OK || st == Z_BUF_ERROR) && stream.avail_out);
	git_inflate_end(&stream);
	unuse_pack(&w_curs);
	if (st != Z_STREAM_END || stream.total_out != dict_size) {
		error("pack dictionary bad");
		free(data);
		return NULL;
	}

	dict = pv4_create_dict(data, dict_size);
	if (!dict) {
		free(data);
		return NULL;
	}

	*offset = curpos;
	return dict;
}

static void load_ident_dict(struct packed_git *p)
{
	off_t offset = 12 + p->num_objects * 20;
	struct packv4_dict *names = load_dict(p, &offset);
	if (!names)
		die("bad pack name dictionary in %s", p->pack_name);
	p->ident_dict = names;
	p->ident_dict_end = offset;
}

const unsigned char *get_identref(struct packed_git *p, const unsigned char **srcp)
{
	unsigned int index;

	if (!p->ident_dict)
		load_ident_dict(p);

	index = decode_varint(srcp);
	if (index >= p->ident_dict->nb_entries) {
		error("%s: index overflow", __func__);
		return NULL;
	}
	return p->ident_dict->data + p->ident_dict->offsets[index];
}

static void load_path_dict(struct packed_git *p)
{
	off_t offset;
	struct packv4_dict *paths;

	/*
	 * For now we need to load the name dictionary to know where
	 * it ends and therefore where the path dictionary starts.
	 */
	if (!p->ident_dict)
		load_ident_dict(p);

	offset = p->ident_dict_end;
	paths = load_dict(p, &offset);
	if (!paths)
		die("bad pack path dictionary in %s", p->pack_name);
	p->path_dict = paths;
}

const unsigned char *get_pathref(struct packed_git *p, unsigned int index,
				 int *len)
{
	if (!p->path_dict)
		load_path_dict(p);

	if (index >= p->path_dict->nb_entries) {
		error("%s: index overflow", __func__);
		return NULL;
	}
	if (len)
		*len = p->path_dict->offsets[index + 1] -
			p->path_dict->offsets[index];
	return p->path_dict->data + p->path_dict->offsets[index];
}

static int tree_line(unsigned char *buf, unsigned long size,
		     const char *label, int label_len,
		     const unsigned char *sha1)
{
	static const char hex[] = "0123456789abcdef";
	int i;

	if (label_len + 1 + 40 + 1 > size)
		return 0;

	memcpy(buf, label, label_len);
	buf += label_len;
	*buf++ = ' ';

	for (i = 0; i < 20; i++) {
		unsigned int val = *sha1++;
		*buf++ = hex[val >> 4];
		*buf++ = hex[val & 0xf];
	}

	*buf = '\n';

	return label_len + 1 + 40 + 1;
}

static int ident_line(unsigned char *buf, unsigned long size,
		      const char *label, int label_len,
		      const unsigned char *ident, unsigned long time, int tz)
{
	int ident_len = strlen((const char *)ident);
	int len = label_len + 1 + ident_len + 1 + 1 + 5 + 1;
	int time_len = 0;
	unsigned char time_buf[16];

	do {
		time_buf[time_len++] = '0' + time % 10;
		time /= 10;
	} while (time);
	len += time_len;

	if (len > size)
		return 0;

	memcpy(buf, label, label_len);
	buf += label_len;
	*buf++ = ' ';

	memcpy(buf, ident, ident_len);
	buf += ident_len;
	*buf++ = ' ';

	do {
		*buf++ = time_buf[--time_len];
	} while (time_len);
	*buf++ = ' ';

	if (tz < 0) {
		tz = -tz;
		*buf++ = '-';
	} else
		*buf++ = '+';
	*buf++ = '0' + tz / 1000; tz %= 1000;
	*buf++ = '0' + tz / 100;  tz %= 100;
	*buf++ = '0' + tz / 10;   tz %= 10;
	*buf++ = '0' + tz;

	*buf = '\n';

	return len;
}

void *pv4_get_commit(struct packed_git *p, struct pack_window **w_curs,
		     off_t offset, unsigned long size)
{
	unsigned long avail;
	git_zstream stream;
	int len, st;
	unsigned int nb_parents;
	unsigned char *dst, *dcp;
	const unsigned char *src, *scp, *sha1, *ident, *author, *committer;
	unsigned long author_time, commit_time;
	int16_t author_tz, commit_tz;

	dst = xmallocz(size);
	dcp = dst;

	src = use_pack(p, w_curs, offset, &avail);
	scp = src;

	sha1 = get_sha1ref(p, &scp);
	len = tree_line(dcp, size, "tree", strlen("tree"), sha1);
	if (!len)
		die("overflow in %s", __func__);
	dcp += len;
	size -= len;

	nb_parents = decode_varint(&scp);
	while (nb_parents--) {
		sha1 = get_sha1ref(p, &scp);
		len = tree_line(dcp, size, "parent", strlen("parent"), sha1);
		if (!len)
			die("overflow in %s", __func__);
		dcp += len;
		size -= len;
	}

	commit_time = decode_varint(&scp);
	ident = get_identref(p, &scp);
	commit_tz = (ident[0] << 8) | ident[1];
	committer = &ident[2];

	author_time = decode_varint(&scp);
	ident = get_identref(p, &scp);
	author_tz = (ident[0] << 8) | ident[1];
	author = &ident[2];

	if (author_time & 1)
		author_time = commit_time + (author_time >> 1);
	else
		author_time = commit_time - (author_time >> 1);

	len = ident_line(dcp, size, "author", strlen("author"),
			 author, author_time, author_tz);
	if (!len)
		die("overflow in %s", __func__);
	dcp += len;
	size -= len;

	len = ident_line(dcp, size, "committer", strlen("committer"),
			 committer, commit_time, commit_tz);
	if (!len)
		die("overflow in %s", __func__);
	dcp += len;
	size -= len;

	if (scp - src > avail)
		die("overflow in %s", __func__);
	offset += scp - src;

	memset(&stream, 0, sizeof(stream));
	stream.next_out = dcp;
	stream.avail_out = size + 1;
	git_inflate_init(&stream);
	do {
		src = use_pack(p, w_curs, offset, &stream.avail_in);
		stream.next_in = (unsigned char *)src;
		st = git_inflate(&stream, Z_FINISH);
		offset += stream.next_in - src;
	} while ((st == Z_OK || st == Z_BUF_ERROR) && stream.avail_out);
	git_inflate_end(&stream);
	if (st != Z_STREAM_END || stream.total_out != size) {
		free(dst);
		return NULL;
	}

	return dst;
}

static int copy_canonical_tree_entries(struct packed_git *p, off_t offset,
				       unsigned int start, unsigned int count,
				       unsigned char **dstp, unsigned long *sizep)
{
	void *data;
	const unsigned char *from, *end;
	enum object_type type;
	unsigned long size;
	struct tree_desc desc;

	data = unpack_entry(p, offset, &type, &size);
	if (!data)
		return -1;
	if (type != OBJ_TREE) {
		free(data);
		return -1;
	}

	init_tree_desc(&desc, data, size);

	while (start--)
		update_tree_entry(&desc);

	from = desc.buffer;
	while (count--)
		update_tree_entry(&desc);
	end = desc.buffer;

	if (end - from > *sizep) {
		free(data);
		return -1;
	}
	memcpy(*dstp, from, end - from);
	*dstp += end - from;
	*sizep -= end - from;
	free(data);
	return 0;
}

/* ordering is so that member alignment takes the least amount of space */
struct pv4_tree_cache {
	off_t base_offset;
	off_t offset;
	off_t last_copy_base;
	struct packed_git *p;
	unsigned int pos;
	unsigned int nb_entries;
};

#define CACHE_SIZE 1024
static struct pv4_tree_cache pv4_tree_cache[CACHE_SIZE];

static struct pv4_tree_cache *get_tree_offset_cache(struct packed_git *p, off_t base_offset)
{
	struct pv4_tree_cache *c;
	unsigned long hash;

	hash = (unsigned long)p + (unsigned long)base_offset;
	hash += (hash >> 8) + (hash >> 16);
	hash %= CACHE_SIZE;

	c = &pv4_tree_cache[hash];
	if (c->p != p || c->base_offset != base_offset) {
		c->p = p;
		c->base_offset = base_offset;
		c->offset = 0;
		c->last_copy_base = 0;
		c->pos = 0;
		c->nb_entries = 0;
	}
	return c;
}

static int tree_entry_prefix(unsigned char *buf, unsigned long size,
			     const unsigned char *path, int path_len,
			     unsigned mode)
{
	int mode_len = 0;
	int len;
	unsigned char mode_buf[8];

	do {
		mode_buf[mode_len++] = '0' + (mode & 7);
		mode >>= 3;
	} while (mode);

	len = mode_len + 1 + path_len;
	if (len > size)
		return 0;

	do {
		*buf++ = mode_buf[--mode_len];
	} while (mode_len);
	*buf++ = ' ';
	memcpy(buf, path, path_len);

	return len;
}

static int decode_entries(struct packed_git *p, struct pack_window **w_curs,
			  off_t obj_offset, unsigned int start, unsigned int count,
			  unsigned char **dstp, unsigned long *sizep)
{
	unsigned long avail;
	const unsigned char *src, *scp;
	unsigned int curpos;
	off_t offset, copy_objoffset;
	struct pv4_tree_cache *c;

	c = get_tree_offset_cache(p, obj_offset);
	if (count && start < c->nb_entries && start >= c->pos &&
	    count <= c->nb_entries - start) {
		offset = c->offset;
		copy_objoffset = c->last_copy_base;
		curpos = c->pos;
		start -= curpos;
		src = NULL;
		avail = 0;
	} else {
		unsigned int nb_entries;

		src = use_pack(p, w_curs, obj_offset, &avail);
		scp = src;

		/* we need to skip over the object header */
		while (*scp & 128)
			if (++scp - src >= avail - 20)
				return -1;

		/* is this a canonical tree object? */
		if ((*scp & 0xf) == OBJ_TREE) {
			offset = obj_offset + (scp - src);
			return copy_canonical_tree_entries(p, offset,
							   start, count,
							   dstp, sizep);
		}

		/* let's still make sure this is actually a pv4 tree */
		if ((*scp++ & 0xf) != OBJ_PV4_TREE)
			return -1;

		nb_entries = decode_varint(&scp);
		if (!count)
			count = nb_entries;
		if (!nb_entries || start > nb_entries ||
		    count > nb_entries - start)
			return -1;

		curpos = 0;
		copy_objoffset = 0;
		offset = obj_offset + (scp - src);
		avail -= scp - src;
		src = scp;

		/*
		 * If this is a partial copy, let's (re)initialize a cache
		 * entry to speed things up if the remaining of this tree
		 * is needed in the future.
		 */
		if (start + count < nb_entries) {
			c->offset = offset;
			c->pos = 0;
			c->nb_entries = nb_entries;
			c->last_copy_base = 0;
		}
	}

	while (count) {
		unsigned int what;

		if (avail < 20) {
			src = use_pack(p, w_curs, offset, &avail);
			if (avail < 20)
				return -1;
		}
		scp = src;

		what = decode_varint(&scp);
		if (scp == src)
			return -1;

		if (!(what & 1) && start != 0) {
			/*
			 * This is a single entry and we have to skip it.
			 * The path index was parsed and is in 'what'.
			 * Skip over the SHA1 index.
			 */
			if (!*scp)
				scp += 1 + 20;
			else
				while (*scp++ & 128);
			start--;
			curpos++;
		} else if (!(what & 1) && start == 0) {
			/*
			 * This is an actual tree entry to recreate.
			 */
			const unsigned char *path, *sha1;
			unsigned mode;
			int len, pathlen;

			path = get_pathref(p, what >> 1, &pathlen);
			sha1 = get_sha1ref(p, &scp);
			if (!path || !sha1)
				return -1;
			mode = (path[0] << 8) | path[1];
			len = tree_entry_prefix(*dstp, *sizep,
						path + 2, pathlen - 2, mode);
			if (!len || len + 20 > *sizep)
				return -1;
			hashcpy(*dstp + len, sha1);
			*dstp += len + 20;
			*sizep -= len + 20;
			count--;
			curpos++;
		} else if (what & 1) {
			/*
			 * Copy from another tree object.
			 */
			unsigned int copy_start, copy_count;

			copy_start = what >> 1;
			copy_count = decode_varint(&scp);
			if (!copy_count)
				return -1;

			/*
			 * The LSB of copy_count is a flag indicating if
			 * a third value is provided to specify the source
			 * object.  This may be omitted when it doesn't
			 * change, but has to be specified at least for the
			 * first copy sequence.
			 */
			if (copy_count & 1) {
				unsigned index = decode_varint(&scp);
				if (!index) {
					/*
					 * SHA1 follows. We assume the
					 * object is in the same pack.
					 */
					copy_objoffset =
						find_pack_entry_one(scp, p);
					scp += 20;
				} else {
					/*
					 * From the SHA1 index we can get
					 * the object offset directly.
					 */
					copy_objoffset =
						nth_packed_object_offset(p, index - 1);
				}
			}
			copy_count >>= 1;
			if (!copy_count || !copy_objoffset)
				return -1;

			if (start >= copy_count) {
				start -= copy_count;
				curpos += copy_count;
			} else {
				int ret;

				copy_count -= start;
				copy_start += start;
				if (copy_count > count) {
					/*
					 * We won't consume the whole of
					 * this copy sequence and the main
					 * loop will be exited. Let's manage
					 * for offset and curpos to remain
					 * unchanged to update the cache.
					 */
					copy_count = count;
					count = 0;
					scp = src;
				} else {
					count -= copy_count;
					curpos += start + copy_count;
					start = 0;
				}

				ret = decode_entries(p, w_curs, copy_objoffset,
						     copy_start, copy_count,
						     dstp, sizep);
				if (ret)
					return ret;

				/* force pack window readjustment */
				avail = scp - src;
			}
		}

		offset += scp - src;
		avail -= scp - src;
		src = scp;
	}

	/*
	 * Update the cache if we didn't run through the entire tree.
	 * We have to "get" it again as a recursion into decode_entries()
	 * could have invalidated what we obtained initially.
	 */
	c = get_tree_offset_cache(p, obj_offset);
	if (curpos < c->nb_entries) {
		c->pos = curpos;
		c->offset = offset;
		c->last_copy_base = copy_objoffset;
	}
						
	return 0;
}

void *pv4_get_tree(struct packed_git *p, struct pack_window **w_curs,
		   off_t obj_offset, unsigned long size)
{
	unsigned char *dst, *dcp;
	int ret;

	dst = xmallocz(size);
	dcp = dst;
	ret = decode_entries(p, w_curs, obj_offset, 0, 0, &dcp, &size);
	if (ret < 0 || size != 0) {
		free(dst);
		return NULL;
	}
	return dst;
}

unsigned long pv4_unpack_object_header_buffer(const unsigned char *base,
					      unsigned long len,
					      enum object_type *type,
					      unsigned long *sizep)
{
	const unsigned char *cp = base;
	uintmax_t val = decode_varint(&cp);
	*type = val & 0xf;
	*sizep = val >> 4;
	return cp - base;
}