aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/src/read_sparse_map_new.c
blob: e42466c19073c66c35d6da6c007d6c2b78f39a9f (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * read_sparse_map_new.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "internal.h"

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

static int decode(const char *str, size_t len, size_t *out)
{
	size_t count = 0;

	*out = 0;

	while (count < len && isdigit(*str)) {
		if (SZ_MUL_OV(*out, 10, out))
			return -1;
		if (SZ_ADD_OV(*out, (*(str++) - '0'), out))
			return -1;
		++count;
	}

	if (count == 0 || count == len)
		return 0;

	return (*str == '\n') ? ((int)count + 1) : -1;
}

sparse_map_t *read_gnu_new_sparse(sqfs_istream_t *fp, tar_header_decoded_t *out)
{
	sparse_map_t *last = NULL, *list = NULL, *ent = NULL;
	size_t i, count, value;
	char buffer[1024];
	int diff, ret;

	if (out->record_size < 512)
		goto fail_format;

	ret = sqfs_istream_read(fp, buffer, 512);
	if (ret < 0) {
		sqfs_perror(fp->get_filename(fp), "reading sparse list", ret);
		goto fail;
	}

	if (ret < 512)
		goto fail_format;

	diff = decode(buffer, 512, &count);
	if (diff <= 0)
		goto fail_format;

	out->record_size -= 512;

	if (count == 0 || count > TAR_MAX_SPARSE_ENT)
		goto fail_format;

	for (i = 0; i < (count * 2); ++i) {
		ret = decode(buffer + diff, 512 - diff, &value);
		if (ret < 0)
			goto fail_format;

		if (ret > 0) {
			diff += ret;
		} else {
			if (out->record_size < 512)
				goto fail_format;

			ret = sqfs_istream_read(fp, buffer + 512, 512);
			if (ret < 0) {
				sqfs_perror(fp->get_filename(fp),
					    "reading sparse list", ret);
				goto fail;
			}

			if (ret < 512)
				goto fail_format;

			ret = decode(buffer + diff, 1024 - diff, &value);
			if (ret <= 0)
				goto fail_format;

			memcpy(buffer, buffer + 512, 512);
			diff = diff + ret - 512;
			out->record_size -= 512;
		}

		if ((i & 0x01) == 0) {
			ent = calloc(1, sizeof(*ent));
			if (ent == NULL)
				goto fail_errno;

			if (list == NULL) {
				list = last = ent;
			} else {
				last->next = ent;
				last = ent;
			}

			ent->offset = value;
		} else {
			ent->count = value;
		}
	}

	return list;
fail_errno:
	perror("parsing GNU 1.0 style sparse file record");
	goto fail;
fail_format:
	fputs("Malformed GNU 1.0 style sparse file map.\n", stderr);
	goto fail;
fail:
	free_sparse_list(list);
	return NULL;
}