corrected build instr + mmap check
[my-code/pnx.git] / efp_extract.c
1 /*
2  * extract elecard efp files
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  * build: gcc -Wall efp_extract.c -o efp_extract
7  * usage: ./efp_extract file.efp
8  *        chmod 640 file_??
9  *
10  * based on info from Muart232
11  * http://www.mikrocontroller.net/topic/210759
12  *
13  * elecard:
14  * http://www.elecard.com/forum/index.php?topic=4174.0
15  *
16  */
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/mman.h>
24
25 int getbytes(int fd,char *buf,int len) {
26
27         int cnt,ret;
28
29         cnt=len;
30         while(cnt) {
31                 ret=read(fd,buf+len-cnt,cnt);
32                 cnt-=ret;
33         }
34
35         return 0;
36 }
37
38 int hexprint(char *buf,int len) {
39
40         int i;
41
42         for(i=0;i<len;i++)
43                 printf("%02x ",*(buf+i));
44         printf("\n");
45
46         return 0;
47
48 }
49
50 int main(int argc,char **argv) {
51
52         int ret,cnt,i,fd,wfd;
53         char buf[128];
54         int nof;
55         unsigned int index[16],offset[16],len[16],wtf[16];
56         char filename[128];
57         char *map;
58
59         fd=open(argv[1],O_RDONLY);
60         if(fd<=0) {
61                 perror("fd open");
62                 return fd;
63         }
64
65         lseek(fd,12,SEEK_CUR);
66         // ignore first 12 bytes
67
68         getbytes(fd,buf,4);
69         nof=*((unsigned int *)buf);
70
71         printf("number of files: %d\n",nof);
72
73         lseek(fd,6,SEEK_CUR);
74
75         for(i=0;i<nof;i++) {
76
77                 getbytes(fd,buf,4);
78                 index[i]=*((unsigned int *)buf);
79
80                 getbytes(fd,buf,4);
81                 wtf[i]=*((unsigned int *)buf);
82
83                 getbytes(fd,buf,4);
84                 len[i]=*((unsigned int *)buf);
85
86                 getbytes(fd,buf,4);
87                 offset[i]=*((unsigned int *)buf);
88
89                 lseek(fd,8,SEEK_CUR);
90                 // ignore
91
92                 printf("file %u:\n",index[i]);
93                 printf(" - wtf: %u\n",wtf[i]);
94                 printf(" - len: %u\n",len[i]);
95                 printf(" - offset: %u\n",offset[i]);
96                 printf(" - next estimated offset: %u\n",len[i]+offset[i]);
97
98         }
99
100         printf("mapping file into memory ...\n");
101         map=mmap(0,offset[nof-1]+len[nof-1],PROT_READ,MAP_SHARED,fd,0);
102         if(map==MAP_FAILED) {
103                 perror("mmap");
104                 return -1;
105         }
106
107         for(i=0;i<nof;i++) {
108
109                 snprintf(filename,128,"file_%02d",index[i]);
110
111                 wfd=open(filename,O_WRONLY|O_CREAT);
112                 if(wfd<=0) {
113                         perror("wfd open");
114                         return wfd;
115                 }
116
117                 cnt=len[i];
118                 while(cnt) {
119                         ret=write(wfd,map+offset[i]+len[i]-cnt,cnt);
120                         cnt-=ret;
121                 }
122
123                 close(wfd);
124
125         }
126
127         close(fd);
128
129         return 0;
130
131 }