[516d32]: Bif.cpp Maximize Restore History

Download this file

Bif.cpp    195 lines (178 with data), 4.2 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
 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
// Bif.cpp: implementation of the CBif class.
// This is a class for bif manipulations
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <fcntl.h>
#include <sys/stat.h>
#include "chitem.h"
#include "Bif.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBif::CBif()
{
entries=NULL;
tisentries=NULL;
}
CBif::~CBif()
{
if(entries) delete [] entries;
if(tisentries) delete [] tisentries;
}
//returns 1 if ordering is required
int CBif::ReadBifHeader(CString filename)
{
bif_entry *tmpentries=NULL; // unsorted entries
int i, idx;
int esize;
int fhandle;
int ret;
fhandle=open(filename,O_RDONLY|O_BINARY);
if(fhandle<1) return -2;
ret=0;
if(read(fhandle,&header,sizeof(header))!=sizeof(header))
{
ret=-2;
goto endofquest;
}
if(memcmp(header.signature,"BIFF",4))
{
if(memcmp(header.signature,"BIFC",4))
{
ret=-2; //corrupted file
goto endofquest;
}
return -1; //BIFC not supported
}
if(header.file_offset!=sizeof(header))
{
ret=-2; //corrupted file
goto endofquest;
}
esize=header.file_entries*sizeof(bif_entry);
startpos=esize+header.tile_entries*sizeof(tis_entry)+header.file_offset;
if(filelength(fhandle)<startpos)
{
ret=-2; //corrupted file
goto endofquest;
}
tmpentries=new bif_entry[header.file_entries];
if(!tmpentries)
{
ret=-3; //out of memory
goto endofquest;
}
if(entries) delete [] entries;
entries=new bif_entry[header.file_entries]; //
memset(entries,-1,esize); //fill memory with invalidated entries
if(!entries)
{
ret=-3; //out of memory
goto endofquest;
}
if(read(fhandle,tmpentries,esize)!=esize)
{
ret=-2; //corrupted file
goto endofquest;
}
esize=header.tile_entries*sizeof(tis_entry);
if(tisentries) delete [] tisentries;
tisentries=new tis_entry[header.tile_entries];
if(!tisentries)
{
ret=-3; //out of memory
goto endofquest;
}
if(read(fhandle,tisentries,esize)!=esize)
{
ret=-2; //corrupted file
goto endofquest;
}
//reordering bif entries according to res_loc
for(i=0;i<header.file_entries;i++)
{
idx=tmpentries[i].res_loc&FILE_IDX_MASK;
if(idx!=i) ret=1;
if(tmpentries[i].res_loc&TIS_IDX_MASK)
{
ret=-2;
goto endofquest;
}
if(entries[idx].size!=-1)
{
ret=-2;
goto endofquest;
}
entries[idx]=tmpentries[i];
}
endofquest:
if(tmpentries) delete [] tmpentries;
close(fhandle);
return ret;
}
int CBif::WriteBifHeader(CString filename)
{
int esize;
int fhandle;
int ret;
fhandle=open(filename,O_RDWR|O_BINARY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE);
if(fhandle<1) return -2;
if(write(fhandle,&header,sizeof(header))!=sizeof(header))
{
ret=-2;
goto endofquest;
}
esize=header.file_entries*sizeof(bif_entry);
if(write(fhandle,entries,esize)!=esize)
{
ret=-2;
goto endofquest;
}
esize=header.tile_entries*sizeof(tis_entry);
if(write(fhandle,tisentries,esize)!=esize)
{
ret=-2;
goto endofquest;
}
endofquest:
close(fhandle);
return ret;
}
//copying BIF data from a complete bif to a bif stub
int CBif::CopyData(CString infile, CString outfile)
{
int fin, fout;
int ret;
fin=open(infile,O_RDONLY|O_BINARY);
if(fin<1) return -2;
fout=open(outfile,O_RDWR|O_BINARY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE);
if(fout<1)
{
close(fin);
return -2;
}
ret=0;
if(lseek(fin,startpos,SEEK_SET)!=startpos)
{
ret=-2;
goto endofquest;
}
if(lseek(fout,startpos,SEEK_SET)!=startpos)
{
ret=-2;
goto endofquest;
}
//the remaining data is transferred by the original copy_file
//remaining filesize is whole length-startpos,
//decryption is set to 0
ret=copy_file(fin, fout, filelength(fin)-startpos,0);
endofquest:
close(fin);
close(fout);
return ret;
}