summaryrefslogtreecommitdiffstats
path: root/src/ustar.c
blob: d8bd98040a4a112eb6c5a5c8b945ca72fef3a698 (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opk is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "gzip.h"
#include "ustar.h"

#define OPKG_OPK_USTAR_NUM_BASE_ 8

struct _opkg_opk_ustar_header {
	char name     [100];
	char mode       [8];
	char uid        [8];
	char gid        [8];
	char size      [12];
	char mtime     [12];
	char chksum     [8];
	char typeflag   [1];
	char linkname [100];
	char magic      [6];
	char version    [2];
	char uname     [32];
	char gname     [32];
	char devmajor   [8];
	char devminor   [8];
	char prefix   [155];
	char padding   [12];
} __attribute__((__packed__));

struct opkg_opk_ustar {
	struct opkg_opk_gzip *gzip;
	uint64_t              data_size_remaining;
	char                  read_record[OPKG_OPK_USTAR_RECORD_SIZE];
};

struct opkg_opk_ustar_seek_name {
	const char                      *name;
	int                              found;
	struct opkg_opk_ustar_seek_name *next;
};

struct opkg_opk_ustar *
opkg_opk_ustar_init(struct opkg_opk_gzip *gzip)
{
	struct opkg_opk_ustar *ustar;

	ustar = malloc(sizeof(*ustar));
	if (ustar == NULL) {
		return NULL;
	}

	ustar->gzip                = gzip;
	ustar->data_size_remaining = 0;

	return ustar;
}

static int
_opkg_opk_ustar_next(struct opkg_opk_ustar *ustar,
		struct _opkg_opk_ustar_header *header)
{
	static char      record[OPKG_OPK_USTAR_RECORD_SIZE];
	       char     *end;
	       uint32_t  chksum_got;
	       uint32_t  chksum_exp;
	       size_t    i;
	       char     *header_uc;

	/* Decompress next (hopefully valid header) record. */
	switch (opkg_opk_gzip_read(ustar->gzip, header)) {
		case OPKG_OPK_OK:
			break;
		case OPKG_OPK_END:
			/* End of gzip stream before end of ustar archive */
		case OPKG_OPK_ERROR:
			return OPKG_OPK_ERROR;
	}

	/* Check for end of archive. */
	memset(record, 0, OPKG_OPK_USTAR_RECORD_SIZE);
	if (memcmp(header, record, OPKG_OPK_USTAR_RECORD_SIZE) == 0) {
		return OPKG_OPK_END;
	}

	/* Verify magic. */
	if (memcmp(header->magic, "ustar", strlen("ustar")) != 0) {
		return OPKG_OPK_ERROR;
	}

	/* Verify checksum. */
	chksum_got = strtol(header->chksum, &end,
			OPKG_OPK_USTAR_NUM_BASE_);
	chksum_exp = 0;
	if (*end != '\0') {
		return OPKG_OPK_ERROR;
	}
	for (i = 0; i < sizeof(header->chksum); ++i) {
		header->chksum[i] = ' ';
	}
	header_uc = (char *) header;
	for (i = 0; i < OPKG_OPK_USTAR_RECORD_SIZE; ++i) {
		chksum_exp += header_uc[i];
	}
	if (chksum_got != chksum_exp) {
		return OPKG_OPK_ERROR;
	}

	/* Depending on type, get size. */
	switch (*header->typeflag) {
		case '0':  /* Regular file */
		case '7':  /* High-performance or regular file */
			ustar->data_size_remaining = strtol(
					header->size, &end,
					OPKG_OPK_USTAR_NUM_BASE_);
			if (*end != '\0') {
				return OPKG_OPK_ERROR;
			}
			break;
		case '2':  /* Symbolic link */
		case '3':  /* Character special file */
		case '4':  /* Block special file */
		case '5':  /* Directory */
		case '6':  /* FIFO special file */
			ustar->data_size_remaining = 0;
			break;
		case '1':  /* Link */
		default:   /* Reserved */
			return OPKG_OPK_ERROR;  /* Unsupported */
	}

	return OPKG_OPK_OK;
}

int
opkg_opk_ustar_list(struct opkg_opk_ustar *ustar,
		struct opkg_opk_ustar_member **member)
{
	static struct _opkg_opk_ustar_header  header;
	       int                            ret;
	       char                          *end;

	/* Get next header record, if any. */
	if ((ret =_opkg_opk_ustar_next(ustar, &header)) != OPKG_OPK_OK) {
		return ret;  /* Error or end of archive */
	}

	/* Allocate outward-facing member information structure. */
	*member = malloc(sizeof(**member));
	if (*member == NULL) {
		return OPKG_OPK_ERROR;
	}

	/* Set name, mode, size, mtime, type, linkname, uname, and gname. */
	if (header.prefix[0] != '\0') {
		sprintf((*member)->name, "%s/%s", header.prefix, header.name);
	} else {
		/* Use memcpy() because header.name may not be NUL-terminated.
		 */
		memcpy((*member)->name, header.name, sizeof(header.name));
		(*member)->name[sizeof(header.name)] = '\0';
	}
	(*member)->mode = strtol(header.mode, &end,
			OPKG_OPK_USTAR_NUM_BASE_);
	if (*end != '\0') {
		free(*member);
		return OPKG_OPK_ERROR;
	}
	(*member)->size = ustar->data_size_remaining;
	(*member)->mtime = strtol(header.mtime, &end,
			OPKG_OPK_USTAR_NUM_BASE_);
	if (*end != '\0') {
		free(*member);
		return OPKG_OPK_ERROR;
	}
	switch (*header.typeflag) {
		case '0':  /* Regular file */
		case '7':  /* High-performance or regular file */
			(*member)->type = '-';
			break;
		case '2':  /* Symbolic link */
			(*member)->type = 'l';
			strncpy((*member)->linkname, header.linkname,
					sizeof((*member)->linkname));
			break;
		case '3':  /* Character special file */
			(*member)->type = 'c';
			break;
		case '4':  /* Block special file */
			(*member)->type = 'b';
			break;
		case '5':  /* Directory */
			(*member)->type = 'd';
			break;
		case '6':  /* FIFO special file */
			(*member)->type = 'p';
			break;
		case '1':  /* Link */
		default:   /* Reserved */
			free(*member);
			return OPKG_OPK_ERROR;  /* Unsupported */
	}
	strncpy((*member)->uname, header.uname, sizeof((*member)->uname));
	strncpy((*member)->gname, header.gname, sizeof((*member)->gname));

	/* Seek through data records until next header record. */
	while (ustar->data_size_remaining > 0) {
		if (opkg_opk_ustar_read(ustar, NULL, NULL) == OPKG_OPK_ERROR) {
			free(*member);
			return OPKG_OPK_ERROR;
		}
	}

	return OPKG_OPK_OK;  /* Possibly more members in archive */
}

int
opkg_opk_ustar_add_seek_name(struct opkg_opk_ustar_seek_name **names,
		const char *name)
{
	struct opkg_opk_ustar_seek_name *seek_name;

	seek_name = malloc(sizeof(*seek_name));
	if (seek_name == NULL) {
		return OPKG_OPK_ERROR;
	}
	seek_name->name  = name;
	seek_name->found = 0;
	seek_name->next  = *names;

	*names = seek_name;
	return OPKG_OPK_OK;
}

void
opkg_opk_ustar_free_seek_names(struct opkg_opk_ustar_seek_name *names)
{
	struct opkg_opk_ustar_seek_name *name;

	while (names != NULL) {
		name  = names;
		names = names->next;
		free(name);
	}
}

int
opkg_opk_ustar_seek(struct opkg_opk_ustar *ustar,
		struct opkg_opk_ustar_seek_name *names)
{
	static struct _opkg_opk_ustar_header    header;
	static char                             name[OPKG_OPK_USTAR_NAME_SIZE];
	       int                              found;
	       int                              found_all;
	       struct opkg_opk_ustar_seek_name *seek_name;

	for (;;) {
		/* Get next header record. */
		if (_opkg_opk_ustar_next(ustar, &header) != OPKG_OPK_OK) {
			return OPKG_OPK_ERROR;  /* Error or end (not found) */
		}

		/* Prepare name (with prefix if any) for check. */
		if (header.prefix[0] != '\0') {
			sprintf(name, "%s/%s", header.prefix,
					header.name);
		} else {
			/* Use memcpy() because header.name may not be NUL-
			 * terminated. */
			memcpy(name, header.name, sizeof(header.name));
			name[sizeof(header.name)] = '\0';
		}

		/* Check each requested name. */
		found     = 0;
		found_all = 1;
		for (seek_name = names; seek_name != NULL;
				seek_name = seek_name->next) {
			if (seek_name->found == 1) {
				continue;  /* Previously found this member */
			}
			if (strcmp(name, seek_name->name) == 0) {
				if (found == 0) {
					seek_name->found = 1;
					found = 1;
					continue;
				}
			}
			if (name[0] == '.' && name[1] == '/' &&
					strcmp(name + 2, seek_name->name) == 0)
			{
				if (found == 0) {
					seek_name->found = 1;
					found = 1;
					continue;
				}
			}
			found_all = 0;
		}
		if (found == 1) {
			if (found_all == 1) {
				/* All requested members found */
				return OPKG_OPK_END;
			}
			return OPKG_OPK_OK;  /* Member found, but more remain */
		}

		/* Seek through data records until next header record. */
		while (ustar->data_size_remaining > 0) {
			if (opkg_opk_ustar_read(ustar, NULL, NULL) ==
					OPKG_OPK_ERROR) {
				return OPKG_OPK_ERROR;
			}
		}
	}

	return OPKG_OPK_ERROR;  /* Member not found */
}

int
opkg_opk_ustar_seek_one(struct opkg_opk_ustar *ustar, const char *name)
{
	struct opkg_opk_ustar_seek_name  seek_name;

	seek_name.name  = name;
	seek_name.found = 0;
	seek_name.next  = NULL;

	if (opkg_opk_ustar_seek(ustar, &seek_name) == OPKG_OPK_END) {
		return OPKG_OPK_OK;
	} else {
		return OPKG_OPK_ERROR;
	}
}

int
opkg_opk_ustar_read(struct opkg_opk_ustar *ustar, char **buffer, size_t *size)
{
	if (ustar->data_size_remaining == 0) {
		return OPKG_OPK_END;
	}

	/* Decompress next data record. */
	switch (opkg_opk_gzip_read(ustar->gzip, ustar->read_record)) {
		case OPKG_OPK_OK:
			break;
		case OPKG_OPK_END:
		case OPKG_OPK_ERROR:
			return OPKG_OPK_ERROR;
	}

	/* Store buffer and size in caller's memory and update remaining size.
	 */
	if (buffer != NULL) {
		*buffer = ustar->read_record;
	}
	if (ustar->data_size_remaining >= OPKG_OPK_USTAR_RECORD_SIZE) {
		if (size != NULL) {
			*size = OPKG_OPK_USTAR_RECORD_SIZE;
		}
		ustar->data_size_remaining -= OPKG_OPK_USTAR_RECORD_SIZE;
	} else {
		if (size != NULL) {
			*size = ustar->data_size_remaining;
		}
		ustar->data_size_remaining = 0;
	}
	return OPKG_OPK_OK;
}

void
opkg_opk_ustar_free(struct opkg_opk_ustar *ustar)
{
	free(ustar);
}