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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <klocale.h>
#include <util/file.h>
#include <util/error.h>
#include <util/fileops.h>
#include <util/sha1hash.h>
#include "dndfile.h"
namespace bt
{
const Uint32 DND_FILE_HDR_MAGIC = 0xD1234567;
struct DNDFileHeader
{
Uint32 magic;
Uint32 first_size;
Uint32 last_size;
Uint8 data_sha1[20];
};
DNDFile::DNDFile(const QString & path) : path(path)
{}
DNDFile::~DNDFile()
{}
void DNDFile::changePath(const QString & npath)
{
path = npath;
}
void DNDFile::checkIntegrity()
{
File fptr;
if (!fptr.open(path,"rb"))
{
create();
return;
}
DNDFileHeader hdr;
if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader))
{
create();
return;
}
if (hdr.magic != DND_FILE_HDR_MAGIC && bt::FileSize(path) != sizeof(DNDFileHeader) + hdr.first_size + hdr.last_size)
{
create();
return;
}
#if 0
if (hdr.first_size > 0 || hdr.last_size > 0)
{
// check hash
Uint32 data_size = hdr.first_size + hdr.last_size;
Uint8* buf = new Uint8[data_size];
if (fptr.read(buf,data_size) != data_size)
{
delete [] buf;
create();
return;
}
if (SHA1Hash::generate(buf,data_size) != SHA1Hash(hdr.data_sha1))
{
delete [] buf;
create();
return;
}
delete [] buf;
}
#endif
}
void DNDFile::create()
{
DNDFileHeader hdr;
hdr.magic = DND_FILE_HDR_MAGIC;
hdr.first_size = 0;
hdr.last_size = 0;
memset(hdr.data_sha1,0,20);
File fptr;
if (!fptr.open(path,"wb"))
throw Error(i18n("Cannot create file %1 : %2").arg(path).arg(fptr.errorString()));
fptr.write(&hdr,sizeof(DNDFileHeader));
fptr.close();
}
Uint32 DNDFile::readFirstChunk(Uint8* buf,Uint32 off,Uint32 buf_size)
{
File fptr;
if (!fptr.open(path,"rb"))
{
create();
return 0;
}
DNDFileHeader hdr;
if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader))
{
create();
return 0;
}
if (hdr.first_size == 0)
return 0;
if (hdr.first_size + off > buf_size)
return 0;
return fptr.read(buf + off,hdr.first_size);
}
Uint32 DNDFile::readLastChunk(Uint8* buf,Uint32 off,Uint32 buf_size)
{
File fptr;
if (!fptr.open(path,"rb"))
{
create();
return 0;
}
DNDFileHeader hdr;
if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader))
{
create();
return 0;
}
if (hdr.last_size == 0)
return 0;
if (hdr.last_size + off > buf_size)
return 0;
fptr.seek(File::BEGIN,sizeof(DNDFileHeader) + hdr.first_size);
return fptr.read(buf + off,hdr.last_size);
}
void DNDFile::writeFirstChunk(const Uint8* buf,Uint32 fc_size)
{
File fptr;
if (!fptr.open(path,"r+b"))
{
create();
if (!fptr.open(path,"r+b"))
{
throw Error(i18n("Failed to write first chunk to DND file : %1").arg(fptr.errorString()));
}
}
DNDFileHeader hdr;
fptr.read(&hdr,sizeof(DNDFileHeader));
if (hdr.last_size == 0)
{
hdr.first_size = fc_size;
fptr.seek(File::BEGIN,0);
// update hash first
// SHA1Hash h = SHA1Hash::generate(buf,fc_size);
// memcpy(hdr.data_sha1,h.getData(),20);
// write header
fptr.write(&hdr,sizeof(DNDFileHeader));
// write data
fptr.write(buf,fc_size);
}
else
{
hdr.first_size = fc_size;
Uint8* tmp = new Uint8[hdr.first_size + hdr.last_size];
try
{
// put everything in tmp buf
memcpy(tmp,buf,hdr.first_size);
fptr.seek(File::BEGIN,sizeof(DNDFileHeader) + hdr.first_size);
fptr.read(tmp + hdr.first_size,hdr.last_size);
// update the hash of the header
// SHA1Hash h = SHA1Hash::generate(tmp,hdr.first_size + hdr.last_size);
// memcpy(hdr.data_sha1,h.getData(),20);
// write header + data
fptr.seek(File::BEGIN,0);
fptr.write(&hdr,sizeof(DNDFileHeader));
fptr.write(tmp,hdr.first_size + hdr.last_size);
delete [] tmp;
}
catch (...)
{
delete [] tmp;
throw;
}
}
}
void DNDFile::writeLastChunk(const Uint8* buf,Uint32 lc_size)
{
File fptr;
if (!fptr.open(path,"r+b"))
{
create();
if (!fptr.open(path,"r+b"))
{
throw Error(i18n("Failed to write last chunk to DND file : %1").arg(fptr.errorString()));
}
}
DNDFileHeader hdr;
fptr.read(&hdr,sizeof(DNDFileHeader));
hdr.last_size = lc_size;
Uint8* tmp = new Uint8[hdr.first_size + hdr.last_size];
try
{
// put everything in tmp buf
memcpy(tmp + hdr.first_size,buf,lc_size);
if (hdr.first_size > 0)
{
fptr.seek(File::BEGIN,sizeof(DNDFileHeader));
fptr.read(tmp,hdr.first_size);
}
// update the hash of the header
// SHA1Hash h = SHA1Hash::generate(tmp,hdr.first_size + hdr.last_size);
// memcpy(hdr.data_sha1,h.getData(),20);
// write header + data
fptr.seek(File::BEGIN,0);
fptr.write(&hdr,sizeof(DNDFileHeader));
fptr.write(tmp,hdr.first_size + hdr.last_size);
delete [] tmp;
}
catch (...)
{
delete [] tmp;
throw;
}
}
}
|