-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCArExtract.hpp
More file actions
77 lines (51 loc) · 1.71 KB
/
CArExtract.hpp
File metadata and controls
77 lines (51 loc) · 1.71 KB
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
#ifndef CAREXTRACT_H
#define CAREXTRACT_H
#include <stdio.h>
int ExtractArchive(char* cpArchiveName, char* cpExtractionDirectory)
{
FILE* filep = fopen(cpArchiveName, "rb");
if(filep == NULL)
{
printf("\n%s does not exist!", cpArchiveName);
return -1;
}
fseek(filep , 0 , SEEK_END);
unsigned int unFileSize = ftell (filep);
rewind(filep);
char* cpFileBuffer = new char[unFileSize];
fread(cpFileBuffer, 1, unFileSize, filep);
fclose(filep);
int nObjectCount = 0;
int nHeadIndex = -1;
int nTailIndex = -1;
char cpObjectName[200];
for(int nIndex = 0; nIndex < unFileSize; nIndex++)
{
if(cpFileBuffer[nIndex] == 0x7F)
if(cpFileBuffer[nIndex + 1] == 'E' && cpFileBuffer[nIndex + 2] == 'L' && cpFileBuffer[nIndex + 3] == 'F')
{
printf("\nFound ELF at %d", nIndex);
if(nObjectCount == 0) //First encounter
{
nHeadIndex = nIndex;
}
else
{
nTailIndex = nIndex; //Note tail points to one char after obj ends
sprintf(cpObjectName, "%sobj_%d",cpExtractionDirectory, nObjectCount);
filep = fopen(cpObjectName,"wb");
fwrite(cpFileBuffer + nHeadIndex, 1, nTailIndex - nHeadIndex, filep);
fclose(filep);
nHeadIndex = nTailIndex;
}
nObjectCount++;
}
}
nTailIndex = unFileSize; //Note tail points to one char after obj ends
sprintf(cpObjectName, "%sobj_%d",cpExtractionDirectory, nObjectCount);
filep = fopen(cpObjectName,"wb");
fwrite(cpFileBuffer + nHeadIndex, 1, nTailIndex - nHeadIndex, filep);
fclose(filep);
return nObjectCount;
}
#endif