forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSpec.cxx
More file actions
executable file
·537 lines (476 loc) · 15.9 KB
/
fileSpec.cxx
File metadata and controls
executable file
·537 lines (476 loc) · 15.9 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Filename: fileSpec.cxx
// Created by: drose (29Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#include "fileSpec.h"
#include "wstring_encode.h"
#include "openssl/md5.h"
#include <fstream>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#ifdef _WIN32
#include <sys/utime.h>
#include <direct.h>
#define utimbuf _utimbuf
#else
#include <utime.h>
#endif
////////////////////////////////////////////////////////////////////
// Function: FileSpec::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
FileSpec::
FileSpec() {
_size = 0;
_timestamp = 0;
memset(_hash, 0, hash_size);
_got_hash = false;
_actual_file = NULL;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
FileSpec::
FileSpec(const FileSpec ©) :
_filename(copy._filename),
_size(copy._size),
_timestamp(copy._timestamp),
_got_hash(copy._got_hash)
{
memcpy(_hash, copy._hash, hash_size);
_actual_file = NULL;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void FileSpec::
operator = (const FileSpec ©) {
_filename = copy._filename;
_size = copy._size;
_timestamp = copy._timestamp;
memcpy(_hash, copy._hash, hash_size);
_got_hash = copy._got_hash;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
FileSpec::
~FileSpec() {
if (_actual_file != NULL) {
delete _actual_file;
}
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::load_xml
// Access: Public
// Description: Reads the data from the indicated XML file.
////////////////////////////////////////////////////////////////////
void FileSpec::
load_xml(TiXmlElement *xelement) {
const char *filename = xelement->Attribute("filename");
if (filename != NULL) {
_filename = filename;
}
const char *size = xelement->Attribute("size");
if (size != NULL) {
char *endptr;
_size = strtoul(size, &endptr, 10);
}
const char *timestamp = xelement->Attribute("timestamp");
if (timestamp != NULL) {
char *endptr;
_timestamp = strtoul(timestamp, &endptr, 10);
}
_got_hash = false;
const char *hash = xelement->Attribute("hash");
if (hash != NULL && strlen(hash) == (hash_size * 2)) {
// Decode the hex hash string.
_got_hash = decode_hex(_hash, hash, hash_size);
}
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::store_xml
// Access: Public
// Description: Stores the data to the indicated XML file.
////////////////////////////////////////////////////////////////////
void FileSpec::
store_xml(TiXmlElement *xelement) {
if (!_filename.empty()) {
xelement->SetAttribute("filename", _filename);
}
if (_size != 0) {
xelement->SetAttribute("size", _size);
}
if (_timestamp != 0) {
xelement->SetAttribute("timestamp", (int)_timestamp);
}
if (_got_hash) {
char hash[hash_size * 2 + 1];
encode_hex(hash, _hash, hash_size);
hash[hash_size * 2] = '\0';
xelement->SetAttribute("hash", hash);
}
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::quick_verify
// Access: Public
// Description: Performs a quick test to ensure the file has not been
// modified. This test is vulnerable to people
// maliciously attempting to fool the program (by
// setting datestamps etc.).
//
// Returns true if it is intact, false if it needs to be
// redownloaded.
////////////////////////////////////////////////////////////////////
bool FileSpec::
quick_verify(const string &package_dir) {
return quick_verify_pathname(get_pathname(package_dir));
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::quick_verify_pathname
// Access: Public
// Description: Works like quick_verify(), above, with an explicit
// pathname. Useful for verifying the copy of a file in
// a temporary location.
////////////////////////////////////////////////////////////////////
bool FileSpec::
quick_verify_pathname(const string &pathname) {
if (_actual_file != NULL) {
delete _actual_file;
_actual_file = NULL;
}
int result = 1;
#ifdef _WIN32
struct _stat st;
wstring pathname_w;
if (string_to_wstring(pathname_w, pathname)) {
result = _wstat(pathname_w.c_str(), &st);
}
#else // _WIN32
struct stat st;
result = stat(pathname.c_str(), &st);
#endif // _WIN32
if (result != 0) {
//cerr << "file not found: " << _filename << "\n";
return false;
}
if (st.st_size != _size) {
// If the size is wrong, the file fails.
//cerr << "size wrong: " << _filename << "\n";
return false;
}
if (st.st_mtime == _timestamp) {
// If the size is right and the timestamp is right, the file passes.
//cerr << "file ok: " << _filename << "\n";
return true;
}
//cerr << "modification time wrong: " << _filename << "\n";
// If the size is right but the timestamp is wrong, the file
// soft-fails. We follow this up with a hash check.
if (!priv_check_hash(pathname, &st)) {
// Hard fail, the hash is wrong.
//cerr << "hash check wrong: " << _filename << "\n";
return false;
}
//cerr << "hash check ok: " << _filename << "\n";
// The hash is OK after all. Change the file's timestamp back to
// what we expect it to be, so we can quick-verify it successfully
// next time.
utimbuf utb;
utb.actime = st.st_atime;
utb.modtime = _timestamp;
#ifdef _WIN32
_wutime(pathname_w.c_str(), &utb);
#else // _WIN32
utime(pathname.c_str(), &utb);
#endif // _WIN32
return true;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::full_verify
// Access: Public
// Description: Performs a more thorough test to ensure the file has
// not been modified. This test is less vulnerable to
// malicious attacks, since it reads and verifies the
// entire file.
//
// Returns true if it is intact, false if it needs to be
// redownloaded.
////////////////////////////////////////////////////////////////////
bool FileSpec::
full_verify(const string &package_dir) {
if (_actual_file != NULL) {
delete _actual_file;
_actual_file = NULL;
}
string pathname = get_pathname(package_dir);
int result = 1;
#ifdef _WIN32
struct _stat st;
wstring pathname_w;
if (string_to_wstring(pathname_w, pathname)) {
result = _wstat(pathname_w.c_str(), &st);
}
#else // _WIN32
struct stat st;
result = stat(pathname.c_str(), &st);
#endif // _WIN32
if (result != 0) {
//cerr << "file not found: " << _filename << "\n";
return false;
}
if (st.st_size != _size) {
// If the size is wrong, the file fails.
//cerr << "size wrong: " << _filename << "\n";
return false;
}
if (!priv_check_hash(pathname, &st)) {
// Hard fail, the hash is wrong.
//cerr << "hash check wrong: " << _filename << "\n";
return false;
}
//cerr << "hash check ok: " << _filename << "\n";
// The hash is OK. If the timestamp is wrong, change it back to
// what we expect it to be, so we can quick-verify it successfully
// next time.
if (st.st_mtime != _timestamp) {
utimbuf utb;
utb.actime = st.st_atime;
utb.modtime = _timestamp;
#ifdef _WIN32
_wutime(pathname_w.c_str(), &utb);
#else // _WIN32
utime(pathname.c_str(), &utb);
#endif // _WIN32
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::force_get_actual_file
// Access: Public
// Description: Returns a FileSpec that represents the actual data
// read on disk. This will read the disk to determine
// the data if necessary.
////////////////////////////////////////////////////////////////////
const FileSpec *FileSpec::
force_get_actual_file(const string &pathname) {
if (_actual_file == NULL) {
#ifdef _WIN32
struct _stat st;
wstring pathname_w;
if (string_to_wstring(pathname_w, pathname)) {
_wstat(pathname_w.c_str(), &st);
}
#else // _WIN32
struct stat st;
stat(pathname.c_str(), &st);
#endif // _WIN32
priv_check_hash(pathname, &st);
}
return _actual_file;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::check_hash
// Access: Public
// Description: Returns true if the file has the expected md5 hash,
// false otherwise.
////////////////////////////////////////////////////////////////////
bool FileSpec::
check_hash(const string &pathname) const {
FileSpec other;
if (!other.read_hash(pathname)) {
return false;
}
return (memcmp(_hash, other._hash, hash_size) == 0);
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::read_hash
// Access: Public
// Description: Computes the hash from the indicated pathname and
// stores it within the FileSpec.
////////////////////////////////////////////////////////////////////
bool FileSpec::
read_hash(const string &pathname) {
memset(_hash, 0, hash_size);
_got_hash = false;
ifstream stream;
#ifdef _WIN32
wstring pathname_w;
if (string_to_wstring(pathname_w, pathname)) {
stream.open(pathname_w.c_str(), ios::in | ios::binary);
}
#else // _WIN32
stream.open(pathname.c_str(), ios::in | ios::binary);
#endif // _WIN32
if (!stream) {
//cerr << "unable to read " << pathname << "\n";
return false;
}
MD5_CTX ctx;
MD5_Init(&ctx);
static const int buffer_size = 4096;
char buffer[buffer_size];
stream.read(buffer, buffer_size);
size_t count = stream.gcount();
while (count != 0) {
MD5_Update(&ctx, buffer, count);
stream.read(buffer, buffer_size);
count = stream.gcount();
}
MD5_Final(_hash, &ctx);
_got_hash = true;
return true;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::read_hash_stream
// Access: Public
// Description: Reads the hash from the next 16 bytes on the
// indicated istream, in the same unusual order observed
// by Panda's HashVal::read_stream() method.
////////////////////////////////////////////////////////////////////
bool FileSpec::
read_hash_stream(istream &in) {
for (int i = 0; i < hash_size; i += 4) {
unsigned int a = in.get();
unsigned int b = in.get();
unsigned int c = in.get();
unsigned int d = in.get();
_hash[i + 0] = d;
_hash[i + 1] = c;
_hash[i + 2] = b;
_hash[i + 3] = a;
}
return !in.fail();
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::compare_hash
// Access: Public
// Description: Returns < 0 if this hash sorts before the other
// hash, > 0 if it sorts after, 0 if they are the same.
////////////////////////////////////////////////////////////////////
int FileSpec::
compare_hash(const FileSpec &other) const {
return memcmp(_hash, other._hash, hash_size);
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::write
// Access: Public
// Description: Describes the data in the FileSpec.
////////////////////////////////////////////////////////////////////
void FileSpec::
write(ostream &out) const {
out << "filename: " << _filename << ", " << _size << " bytes, "
<< asctime(localtime(&_timestamp));
// asctime includes a newline.
out << "hash: ";
stream_hex(out, _hash, hash_size);
out << "\n";
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::output_hash
// Access: Public
// Description: Writes just the hash code.
////////////////////////////////////////////////////////////////////
void FileSpec::
output_hash(ostream &out) const {
stream_hex(out, _hash, hash_size);
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::priv_check_hash
// Access: Private
// Description: Returns true if the file has the expected md5 hash,
// false otherwise. Updates _actual_file with the data
// read from disk, including the hash, for future
// reference.
//
// The parameter stp is a pointer to a stat structure.
// It's declared as a void * to get around issues with
// the nonstandard declaration of this structure in
// Windows.
////////////////////////////////////////////////////////////////////
bool FileSpec::
priv_check_hash(const string &pathname, void *stp) {
const struct stat &st = *(const struct stat *)stp;
assert(_actual_file == NULL);
_actual_file = new FileSpec;
_actual_file->_filename = pathname;
_actual_file->_size = st.st_size;
_actual_file->_timestamp = st.st_mtime;
if (!_actual_file->read_hash(pathname)) {
return false;
}
return (memcmp(_hash, _actual_file->_hash, hash_size) == 0);
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::decode_hex
// Access: Private, Static
// Description: Decodes the hex string in source into the character
// array in dest. dest must have has least size bytes;
// source must have size * 2 bytes.
//
// Returns true on success, false if there was a non-hex
// digit in the string.
////////////////////////////////////////////////////////////////////
bool FileSpec::
decode_hex(unsigned char *dest, const char *source, size_t size) {
for (size_t i = 0; i < size; ++i) {
int high = decode_hexdigit(source[i * 2]);
int low = decode_hexdigit(source[i * 2 + 1]);
if (high < 0 || low < 0) {
return false;
}
dest[i] = (high << 4) | low;
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::encode_hex
// Access: Private, Static
// Description: Encodes a character array into a hex string for
// output. dest must have at least size * 2 bytes;
// source must have size bytes. The result is not
// null-terminated.
////////////////////////////////////////////////////////////////////
void FileSpec::
encode_hex(char *dest, const unsigned char *source, size_t size) {
for (size_t i = 0; i < size; ++i) {
int high = (source[i] >> 4) & 0xf;
int low = source[i] & 0xf;
dest[2 * i] = encode_hexdigit(high);
dest[2 * i + 1] = encode_hexdigit(low);
}
}
////////////////////////////////////////////////////////////////////
// Function: FileSpec::stream_hex
// Access: Private, Static
// Description: Writes the indicated buffer as a string of hex
// characters to the given ostream.
////////////////////////////////////////////////////////////////////
void FileSpec::
stream_hex(ostream &out, const unsigned char *source, size_t size) {
for (size_t i = 0; i < size; ++i) {
int high = (source[i] >> 4) & 0xf;
int low = source[i] & 0xf;
out.put(encode_hexdigit(high));
out.put(encode_hexdigit(low));
}
}