Panda3D
download_utils.cxx
1 // Filename: download_utils.cxx
2 // Created by: mike (18Jan99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pandabase.h"
16 
17 #ifdef HAVE_ZLIB
18 
19 #include "download_utils.h"
20 #include "config_downloader.h"
21 #include <zlib.h>
22 
23 unsigned long
24 check_crc(Filename name) {
25  pifstream read_stream;
26  name.set_binary();
27  if (!name.open_read(read_stream)) {
28  downloader_cat.error()
29  << "check_crc() - Failed to open input file: " << name << endl;
30  return 0;
31  }
32 
33  // Determine the length of the file and read it into the buffer
34  read_stream.seekg(0, ios::end);
35  int buffer_length = read_stream.tellg();
36  char *buffer = new char[buffer_length];
37  read_stream.seekg(0, ios::beg);
38  read_stream.read(buffer, buffer_length);
39 
40  // Compute the crc
41  unsigned long crc = crc32(0L, Z_NULL, 0);
42  crc = crc32(crc, (unsigned char *)buffer, buffer_length);
43 
44  delete[] buffer;
45 
46  return crc;
47 }
48 
49 unsigned long
50 check_adler(Filename name) {
51  pifstream read_stream;
52  name.set_binary();
53  if (!name.open_read(read_stream)) {
54  downloader_cat.error()
55  << "check_adler() - Failed to open input file: " << name << endl;
56  return 0;
57  }
58 
59  // Determine the length of the file and read it into the buffer
60  read_stream.seekg(0, ios::end);
61  int buffer_length = read_stream.tellg();
62  char *buffer = new char[buffer_length];
63  read_stream.seekg(0, ios::beg);
64  read_stream.read(buffer, buffer_length);
65 
66  // Compute the adler checksum
67  unsigned long adler = adler32(0L, Z_NULL, 0);
68  adler = adler32(adler, (unsigned char *)buffer, buffer_length);
69 
70  delete[] buffer;
71 
72  return adler;
73 }
74 
75 #endif // HAVE_ZLIB
void set_binary()
Indicates that the filename represents a binary file.
Definition: filename.I:494
bool open_read(ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
Definition: filename.cxx:2003
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44