forked from omgneeq/ps3utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_tar.c
104 lines (84 loc) · 2.33 KB
/
fix_tar.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
/*
* fix_tar.c -- TAR fixer for PS3 packages
*
* Copyright (C) Youness Alaoui (KaKaRoTo)
*
* This software is distributed under the terms of the GNU General Public
* License ("GPL") version 3, as published by the Free Software Foundation.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char filename[100];
char filemode[8];
char owner_id[8];
char group_id[8];
char filesize[12];
char atime[12];
char checksum[8];
char file_type;
char link[100];
char ustar[6];
char ustar_version[2];
char owner[32];
char group[32];
char device_major[8];
char device_minor[8];
char filename_prefix[155];
} TARHeader;
int main (int argc, char *argv[])
{
FILE *fd = NULL;
TARHeader block;
size_t pos = 0;
fprintf (stderr, "TAR Fixer for PS3 packages\n");
fprintf (stderr, "By KaKaRoTo\n\n");
if (argc != 2) {
fprintf (stderr, "Usage: %s <file.tar>", argv[0]);
exit (-1);
}
fd = fopen (argv[1], "rb+");
if (fd == NULL) {
perror ("Error opening input file");
exit (-2);
}
do {
unsigned int size = 0;
unsigned int checksum = 0;
unsigned int i;
fseek (fd, pos, SEEK_SET);
if (fread (&block, sizeof(TARHeader), 1, fd) != 1)
break;
// Found end of file block
if (block.filename[0] == 0)
break;
printf ("Fixing file : %s\n", block.filename);
printf ("\tOwner/group: %s(%s):%s(%s)\n", block.owner, block.owner_id, block.group, block.group_id);
sscanf (block.filesize, "%o", &size);
strncpy (block.owner_id, "0001752", 7);
strncpy (block.group_id, "0001274", 7);
strncpy (block.owner, "pup_tool", 32);
strncpy (block.group, "psnes", 32);
strncpy (block.ustar, "ustar ", 6);
block.ustar_version[0] = ' ';
block.ustar_version[1] = 0;
memset (block.device_major, 0, 8);
memset (block.device_minor, 0, 8);
// Rebuild checksum
memset (block.checksum, ' ', 8);
for (i = 0; i < sizeof(TARHeader); i++)
checksum += ((unsigned char *) &block)[i];
snprintf (block.checksum, 8, "0%o", checksum);
fseek (fd, pos, SEEK_SET);
if (fwrite (&block, sizeof(TARHeader), 1, fd) != 1)
break;
pos += 512;
pos += size;
// padding to 512 block boundary
if (size % 512 != 0)
pos += 512 - (size % 512);
} while (!feof (fd) && block.filename[0] != 0);
return 0;
}