-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdlbreader.cpp
134 lines (101 loc) · 2.21 KB
/
dlbreader.cpp
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
#include "dlbreader.h"
dlbreader::dlbreader(char *file,char *outpath,int verb)
{
okay=false;
verbose=verb;
if((infile = open(file, O_RDWR)) == ERR)
{
printf("couldn't open %s\n", file);
return;
}
if(!read_header())
{
printf("can't read header");
return;
}
strncpy(path,outpath,PATH_MAX-1);
okay=true;
}
dlbreader::~dlbreader()
{
if(infile) close(infile);
}
bool dlbreader::read_header()
{
char mg[8];
read(infile,&mg,8);
if(strcmp(mg,AR_MAGIC))
return false;
struct ar_header head;
read(infile,&head,sizeof(ar_header));
uint16_t hdrmg;
hdrmg=(uint16_t)(head.ar_fmag[0]|((uint16_t)(head.ar_fmag[1]))<<8);
if(hdrmg!=AR_OFFSET_MAGIC)
return false;
uint32_t cnt=0;
read(infile,&cnt,sizeof(cnt));
if(!cnt)
return false;
uint32_t old=0;
while(cnt--)
{
uint32_t offset=0;
read(infile,&offset,sizeof(offset));
if(!offset)
return false;
if(old==offset)
continue;
old=offset;
offsets.push_back(offset);
}
current=offsets.begin();
return true;
}
size_t dlbreader::write_next_file(char *name,size_t slen)
{
uint32_t start=*current;
current++;
uint32_t end=*current;
uint32_t len=end-start-sizeof(ar_header);
if(!okay)
return 0;
lseek(infile,start,SEEK_SET);
struct ar_header head;
read(infile,&head,sizeof(ar_header));
char filename[256];
strncpy(filename,head.ar_name,256);
for(int i=0;i<255;i++)
{
if(filename[i]=='/')
{
filename[i]=0;
break;
}
}
filename[255]=0;
char filepath[256+PATH_MAX];
strncpy(filepath,path,PATH_MAX);
strcat(filepath,filename);
int fd=0;
if((fd=open(filepath,O_WRONLY|O_CREAT,0666))==ERR)
{
printf("couldn't open %s\n", filepath);
return 0;
}
size_t size=0;
while(len--)
{
char c=0;
if(!read(infile,&c,1))
break;
write(fd,&c,1);
size++;
}
close(fd);
strncpy(name,filename,slen);
return size;
}
bool dlbreader::remove_file()
{
return true;
}