-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharchive.c
307 lines (280 loc) · 7.56 KB
/
archive.c
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
/*
* multihash - compute hashes on collections of files
* Copyright (c) 2017 Nicolas George <[email protected]>
*
* This program 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.
*
* This program 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "archive.h"
#define OFF_PATH 0x000
#define OFF_MODE 0x064
#define OFF_SIZE 0x07c
#define OFF_MTIME 0x088
#define OFF_TYPE 0x09c
#define OFF_TARGET 0x09d
#define OFF_MAGIC 0x101
#define LEN_PATH 100
int
archive_open(Archive_reader **rar, FILE *in)
{
Archive_reader *ar;
ar = malloc(sizeof(*ar));
if (ar == NULL) {
perror("malloc");
return -1;
}
ar->in = in;
ar->toread = 0;
ar->long_filename_buf = NULL;
ar->long_filename_buf_size = 0;
*rar = ar;
return 0;
}
void
archive_free(Archive_reader **rar)
{
free((*rar)->long_filename_buf);
free(*rar);
*rar = NULL;
}
static intmax_t
get_oct(const uint8_t *p, unsigned len)
{
const uint8_t *end = p + len;
intmax_t r = 0;
while (p < end && *p == ' ')
p++;
while (p < end && (unsigned)(*p - '0') < 8)
r = (r << 3) | (*(p++) - '0');
return r;
}
static int
is_all_zero(uint8_t *data, size_t size)
{
while (size-- > 0)
if (*(data++) != 0)
return 0;
return 1;
}
static unsigned
get_mode(const uint8_t *head)
{
return get_oct(head + OFF_MODE, 8);
}
static uint64_t
get_size(const uint8_t *head)
{
return get_oct(head + OFF_SIZE, 12);
}
static uint64_t
get_mtime(const uint8_t *head)
{
return get_oct(head + OFF_MTIME, 12);
}
#define ATOFF " at offset 0x%jx\n"
#define OFFAT(d) (uintmax_t)(ftello(ar->in) - (d))
#define ATOFFSET(d) ATOFF, OFFAT(d)
static int
read_long_something(Archive_reader *ar, uint8_t *head,
char **val, char **buf, unsigned *buf_size)
{
static const uint8_t magic[LEN_PATH] = "././@LongLink";
uint64_t size, bsize;
int ret;
if (memcmp(head + OFF_PATH, magic, sizeof(magic)) != 0) {
fprintf(stderr, "Invalid long entry pseudo-path"
ATOFFSET(sizeof(head)));
return -1;
}
size = get_size(head);
if (size >= 65536) {
fprintf(stderr, "Long file name really too long"
ATOFFSET(sizeof(head)));
return -1;
}
bsize = (size + 0x1FF) &~ 0x1FF;
if (bsize > *buf_size) {
char *n = realloc(*buf, bsize + 1);
if (n == NULL) {
perror("malloc");
return -1;
}
*buf = n;
*buf_size = bsize;
}
ret = fread(*buf, 1, bsize, ar->in);
if (ret != (int)bsize) {
if (ret < 0)
fprintf(stderr, "Read error in tar file" ATOFFSET(0));
else
fprintf(stderr, "Truncated tar file" ATOFFSET(0));
return -1;
}
(*buf)[size] = 0;
(*val) = *buf;
return 0;
}
static int
read_long_file_name(Archive_reader *ar, uint8_t *head)
{
return read_long_something(ar, head, &ar->filename,
&ar->long_filename_buf, &ar->long_filename_buf_size);
}
static int
read_long_link(Archive_reader *ar, uint8_t *head)
{
return read_long_something(ar, head, &ar->target,
&ar->long_target_buf, &ar->long_target_buf_size);
}
int
archive_next(Archive_reader *ar)
{
uint8_t head[512];
static const uint8_t magic[8] = "ustar ";
int ret, zblocks = 0, type, len;
assert(ar->toread == 0);
ar->filename = ar->filename_buf;
ar->target = ar->target_buf;
while (1) {
ret = fread(head, 1, sizeof(head), ar->in);
if (ret == 0) {
if (ferror(ar->in) || !feof(ar->in)) {
fprintf(stderr, "Read error in tar file" ATOFFSET(0));
return -1;
}
return 0;
} else if (ret < (int)sizeof(head)) {
fprintf(stderr, "Truncated tar file" ATOFFSET(0));
return -1;
} else {
type = head[OFF_TYPE];
if (type == 'L') {
ret = read_long_file_name(ar, head);
if (ret < 0)
return -1;
} else if (type == 'K') {
ret = read_long_link(ar, head);
if (ret < 0)
return -1;
} else if (!is_all_zero(head, sizeof(head))) {
break;
} else {
zblocks++;
}
}
}
if (zblocks == 1) {
fprintf(stderr, "Strange zero blocks" ATOFFSET(zblocks * sizeof(head)));
return -1;
}
if (memcmp(head + OFF_MAGIC, magic, sizeof(magic)) != 0) {
fprintf(stderr, "Invalid or unsupported tar file header"
ATOFFSET(sizeof(head)));
return -1;
}
memcpy(ar->filename_buf, head + OFF_PATH, LEN_PATH);
ar->filename_buf[LEN_PATH] = 0;
memcpy(ar->target_buf, head + OFF_TARGET, LEN_PATH);
ar->target_buf[LEN_PATH] = 0;
ar->mode = get_mode(head);
ar->size = get_size(head);
ar->mtime = get_mtime(head);
len = strlen(ar->filename_buf);
if (len > 0 && ar->filename_buf[len - 1] == '/')
ar->filename_buf[--len] = 0;
switch (type) {
case 0:
case '0':
case '7': ar->type = 'F'; break;
case '2': ar->type = 'L'; break;
case '3': ar->type = 'c'; break;
case '4': ar->type = 'b'; break;
case '5': ar->type = 'D'; break;
case '6': ar->type = 'p'; break;
case '1':
fprintf(stderr, "Hard links not supported" ATOFFSET(0));
return -1;
default:
fprintf(stderr, "Unsupported file type '%c'" ATOFF,
type, OFFAT(sizeof(head)));
return -1;
}
ar->toread = ar->size;
if (ar->type != 'F' && ar->toread != 0) {
fprintf(stderr, "Special file with size\n");
return -1;
}
return 1;
}
int
archive_read(Archive_reader *ar, uint8_t *buf, int size)
{
unsigned pad;
uint8_t padbuf[512];
int ret;
if (ar->toread == 0)
return 0;
if ((uint64_t)size > ar->toread)
size = ar->toread;
ret = fread(buf, 1, size, ar->in);
/* TODO read errors */
if (ret < size)
return -1;
ar->toread -= size;
if (ar->toread == 0) {
pad = 512 - (ar->size & 511);
if (pad < 512) {
ret = fread(padbuf, 1, pad, ar->in);
if (ret < (int)pad)
return -1;
}
}
return size;
}
#if 0
int
main(void)
{
Archive_reader *ar;
int ret;
if (archive_open(&ar, stdin) < 0)
exit(1);
while (1) {
printf("\n");
ret = archive_next(ar);
printf("ret = %d\n", ret);
if (ret <= 0)
break;
printf("file: '%s'\n", ar->filename);
printf("type: %c\n", ar->type);
printf("mode: %o\n", ar->mode);
printf("size: %d\n", (int)ar->size);
if (ar->type == 'F') {
uint8_t buf[100];
while (1) {
ret = archive_read(ar, buf, sizeof(buf));
if (ret < 0)
exit(2);
if (ret == 0)
break;
printf("read %d\n", ret);
//fwrite(buf, 1, ret, stdout);
}
}
printf("\n");
}
archive_free(&ar);
return 0;
}
#endif