-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar.hpp
363 lines (326 loc) · 12.3 KB
/
tar.hpp
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
#pragma once
#include "Filter.hpp"
#include "../file/File.hpp"
#include "../CharacterNames.hpp"
#include <cstdint>
/**
* UStar (Unix Standard TAR) detection and transformation
*
*/
class TarFilter : public Filter {
private:
struct TARheader { // 512 bytes
char name[100]; // 0 | file name
char mode[8]; // 100 | file mode (permissions)
char uid[8]; // 108 | owner user id (octal)
char gid[8]; // 116 | owner group id (octal)
char size[12]; // 124 | file size in bytes (octal)
char mtime[12]; // 136 | last modification time in numeric Unix time format (octal)
char chksum[8]; // 148 | sum of unsigned characters in header block, filled with spaces while calculating (octal)
char typeflag; // 156 | link flag / file type
char linkname[100]; // 157 | name of linked file
char magic[8]; // 257 | "ustar\000" in calgary.tar created by windows 10 and gnu tar
// "ustar \0" in mozilla, samba, xml or in calgary.tar created by total commander or gnu tar
char uname[32]; // 265 | owner user name (string)
char gname[32]; // 297 | owner group name (string)
char devmajor[8]; // 329 | device major number
char devminor[8]; // 337 | device minor number
char prefix[155]; // 345 | filename prefix
char padding[12]; // 500 | padding
int oct2bin(const char* p, int size) {
while (*p == SPACE) { //skip leading spaces
++p;
--size;
}
int i = 0;
while (size > 0) {
if (*p == 0/* && size == 1*/) //the last char must be a \0
break;
if (*p == SPACE)
break;
if (*p < '0' || *p > '7')
return -1; //fail
i *= 8;
i += *p - '0';
++p;
--size;
}
return i;
}
int calculateChecksum() {
const char* p = &name[0];
constexpr int chksumOffset = offsetof(TARheader, chksum);
constexpr int chksumSize = 8;
int u = 0;
for (int n = 0; n < sizeof(TARheader); ++n) {
if (n < chksumOffset || n >= chksumOffset + chksumSize) //exluding the checksum bytes
u += ((uint8_t*)p)[n];
else
u += SPACE; // emulate spaces in place of the checksum bytes
}
return u;
}
// checksum format examples:
// "0012201\0" (as in calgary.tar as created by total commander)
// "012201\0 " (as in samba or calgary.tar created by windows 10 or gnu tar)
// " 12201\0 " (as in mozilla and xml)
void clearChecksum() {
//look up the terminating \0 and fill it's left side with either spaces or '0's to preserve
//information about the format (see the checksum format examples above).
char filler = chksum[0] == SPACE ? SPACE : '0';
int i = 0;
for (; i < sizeof(tarh.chksum); i++)
if (chksum[i + 1] == 0) break;
//now i points to the last digit of the checksum
for (; i >= 0; i--)
chksum[i] = filler;
}
void generateChecksum() {
//look up the terminating \0 and fill it's left side with the checksum (octal)
int checksum = calculateChecksum();
int i = 0;
for (; i < sizeof(tarh.chksum); i++)
if (chksum[i + 1] == 0) break;
//now i points to the last digit of the checksum
for (; i >= 0; i--) {
chksum[i] = (checksum & 7) + '0';
checksum >>= 3;
if (checksum == 0)
break;
}
}
bool verifyChecksum() {
return calculateChecksum() == oct2bin(&chksum[0], 8);
}
bool isEmptySector() {
const char* p = &name[0];
for (int n = 511; n >= 0; --n)
if (p[n] != 0)
return false;
return true;
}
} tarh;
Array<uint64_t> detectedSectorStartPositions{ 0 };
Array<uint64_t> detectedFileStartPositions{ 0 };
Array<uint64_t> detectedFileLengths{ 0 };
uint64_t detectedEmptySectorCount{ 0 };
//detect tar content
//a tar file is: hdr+filecontent + hdr+filecontent + etc...
//this function figures out where each file starts and how long they are
//we ignore files in tar having garbage in the padding area
bool process(File* in, uint64_t maxFilePos) {
uint64_t sectorStartPos = this->detectedStartPos;
while (true) {
if (sectorStartPos == maxFilePos) {
//no empty sectors at the end - that'll be ok
//this usually happens when we ignore files in a tar with garbage in the padding area
this->detectedEndPos = sectorStartPos;
return true;
}
else if (sectorStartPos > maxFilePos)
return false; //fail
in->setpos(sectorStartPos);
int bytesRead = in->blockRead((uint8_t*)&tarh, sizeof(tarh));
if (bytesRead != sizeof(tarh)) {
return false; //fail
}
if (tarh.isEmptySector()) {
do {
detectedEmptySectorCount++;
sectorStartPos += sizeof(tarh);
int bytesRead = in->blockRead((uint8_t*)&tarh, sizeof(tarh));
if (bytesRead != sizeof(tarh))
break;
} while (tarh.isEmptySector());
this->detectedEndPos = sectorStartPos;
return true;
}
//verify if all fields look octal that should look octal
if (!tarh.verifyChecksum())
return false; //fail
if (tarh.oct2bin(tarh.mode, sizeof(tarh.mode)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.uid, sizeof(tarh.uid)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.gid, sizeof(tarh.gid)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.size, sizeof(tarh.size)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.mtime, sizeof(tarh.mtime)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.devmajor, sizeof(tarh.devmajor)) < 0)
return false; //fail
if (tarh.oct2bin(tarh.devminor, sizeof(tarh.devminor)) < 0)
return false; //fail
detectedSectorStartPositions.pushBack(sectorStartPos);
int fileSize = tarh.oct2bin(tarh.size, sizeof(tarh.size));
if (fileSize != 0) {
//detect if file is properly padded
int filePaddingSize = (512 - (fileSize & 511)) & 511;
in->setpos(sectorStartPos + sizeof(TARheader) + fileSize);
for (int i = 0; i < filePaddingSize; i++) {
int c = in->getchar();
if (c != 0) {
if (detectedFileStartPositions.size() > 0) {
this->detectedEndPos = sectorStartPos;
return true; //accept what we have so far (files with proper padding)
}
return false; //fail, there is not properly padded files so far
}
}
detectedFileStartPositions.pushBack(sectorStartPos + sizeof(TARheader));
detectedFileLengths.pushBack(fileSize);
}
int sectorsToJump = (fileSize + 511) >> 9;
sectorStartPos += sizeof(TARheader) * (sectorsToJump + 1);
}
}
void Print() {
for (size_t i = 0; i < detectedSectorStartPositions.size(); i++) {
printf("tar sector position: %d\n", (int)detectedSectorStartPositions[i]);
}
for (size_t i = 0; i < detectedFileStartPositions.size(); i++) {
printf("file position: %d, length: %d\n", (int)detectedFileStartPositions[i], (int)detectedFileLengths[i]);
}
printf("empty sectors: %d, length: %d\n", (int)detectedEmptySectorCount, (int)(detectedEmptySectorCount * sizeof(tarh)));
}
public:
uint64_t detectedStartPos{};
uint64_t detectedEndPos{};
bool detect(File* in, uint64_t maxFilePos) {
uint64_t userNamePos = in->curPos();
this->detectedStartPos = userNamePos - offsetof(TARheader, uname);
return process(in, maxFilePos);
}
void encode(File *in, File *out, uint64_t size, int width, int & headerSize) override {
this->detectedStartPos = in->curPos();
uint64_t maxFilePos = this->detectedStartPos + size;
bool success = process(in, maxFilePos);
if (!success)
quit("Internal error in TAR detection.");
//for debugging
//Print();
out->putVLI(detectedSectorStartPositions.size());
out->putVLI(detectedEmptySectorCount);
for (size_t i = 0; i < detectedSectorStartPositions.size(); i++) {
in->setpos(detectedSectorStartPositions[i]);
int bytesRead = in->blockRead((uint8_t*)&tarh, sizeof(tarh));
if (bytesRead != sizeof(tarh)) {
quit("Internal error in TAR transformation.");
}
tarh.clearChecksum();
out->blockWrite((uint8_t*)&tarh, sizeof(tarh));
}
Array<uint8_t, 1> fileData{0};
for (size_t i = 0; i < detectedFileStartPositions.size(); i++) {
fileData.resize(detectedFileLengths[i]);
in->setpos(detectedFileStartPositions[i]);
in->blockRead(&fileData[0], detectedFileLengths[i]);
out->blockWrite(&fileData[0], detectedFileLengths[i]);
}
return;
}
uint64_t decode(File* in, File* out, FMode fMode, uint64_t size, uint64_t& diffFound) override {
size_t sectorCount = in->getVLI();
size_t emptySectorCount = in->getVLI();
size_t curPos = in->curPos();
Array<TARheader, 1> headerData{ sectorCount };
Array<uint8_t, 1> fileData{ 0 };
uint64_t p = 0;
uint64_t fileDataStartPos = curPos + sectorCount * sizeof(tarh);
for (size_t i = 0; i < sectorCount; i++) {
in->setpos(curPos + i * sizeof(tarh));
int bytesRead = in->blockRead((uint8_t*)&headerData[i], sizeof(tarh));
if (bytesRead != sizeof(tarh)) {
if (fMode == FMode::FCOMPARE)
diffFound = p + 1;
return 0;
}
headerData[i].generateChecksum();
if (fMode == FMode::FDECOMPRESS) {
p += sizeof(tarh);
out->blockWrite((uint8_t*)&headerData[i], sizeof(tarh));
}
else if (fMode == FMode::FCOMPARE) {
for (int j = 0; j < sizeof(tarh); j++) {
p++;
int c1 = out->getchar();
int c2 = ((uint8_t*)&headerData[i])[j];
if (c1 != c2 && (diffFound == 0)) {
diffFound = p;
}
}
}
int fileSize = tarh.oct2bin(headerData[i].size, 12);
if (fileSize != 0) {
in->setpos(fileDataStartPos);
fileData.resize(fileSize);
int bytesRead = in->blockRead(&fileData[0], fileSize);
if (bytesRead != fileSize) {
if (fMode == FMode::FCOMPARE)
diffFound = p;
return p;
}
if (fMode == FMode::FDECOMPRESS) {
p += fileSize;
out->blockWrite(&fileData[0], fileSize);
}
else if (fMode == FMode::FCOMPARE) {
for (int j = 0; j < fileSize; j++) {
p++;
if (fileData[j] != out->getchar() && (diffFound == 0)) {
diffFound = p;
}
}
}
fileDataStartPos += fileSize;
//printf("filesize (%d): %d\n",int(i), int(fileSize));
//padding sector with 0 when needed
while ((fileSize & 511) != 0) {
p++;
fileSize++;
if (fMode == FMode::FDECOMPRESS) {
out->putChar(0);
}
else if (fMode == FMode::FCOMPARE) {
if (out->getchar() != 0 && (diffFound == 0)) {
diffFound = p;
}
}
}
}
}
//write empty sectors at the end
for (size_t i = 0; i < emptySectorCount * sizeof(tarh); i++) {
p++;
if (fMode == FMode::FDECOMPRESS) {
out->putChar(0);
}
else if (fMode == FMode::FCOMPARE) {
if (out->getchar() != 0 && (diffFound == 0)) {
diffFound = p;
}
}
}
in->setpos(fileDataStartPos);
return p;
}
void getFilePositions(File* in, Array<uint64_t,1> &filePositions) {
assert(filePositions.size() == 0);
size_t sectorCount = in->getVLI();
size_t emptySectorCount = in->getVLI();
size_t curPos = in->curPos();
uint64_t fileDataStartPos = curPos + sectorCount * sizeof(tarh);
filePositions.pushBack(fileDataStartPos); //the first entry is the first file position
for (size_t i = 0; i < sectorCount; i++) {
int bytesRead = in->blockRead((uint8_t*)&tarh, sizeof(tarh));
assert(bytesRead == sizeof(tarh));
int fileSize = tarh.oct2bin(tarh.size, 12);
assert(fileSize >= 0);
if (fileSize != 0) {
fileDataStartPos += fileSize;
filePositions.pushBack(fileDataStartPos); //the last entry is exactly the tempfile size (it points past to the last file)
}
}
}
};