16 #include "virtualFileSystem.h"
20 #include "openSSLWrapper.h"
21 #include "openssl/md5.h"
22 #endif // HAVE_OPENSSL
33 encode_hex(_hv[0], buffer);
34 encode_hex(_hv[1], buffer + 8);
35 encode_hex(_hv[2], buffer + 16);
36 encode_hex(_hv[3], buffer + 24);
37 out.write(buffer, 32);
52 while (!in.eof() && !in.fail() && isxdigit(ch)) {
61 in.clear(ios::failbit|in.rdstate());
71 decode_hex(buffer, _hv[0]);
72 decode_hex(buffer + 8, _hv[1]);
73 decode_hex(buffer + 16, _hv[2]);
74 decode_hex(buffer + 24, _hv[3]);
143 encode_hex(_hv[0], buffer);
144 encode_hex(_hv[1], buffer + 8);
145 encode_hex(_hv[2], buffer + 16);
146 encode_hex(_hv[3], buffer + 24);
147 return string(buffer, 32);
183 nassertr(text.size() == 16,
false);
201 hash_file(
const Filename &filename) {
202 Filename bin_filename = Filename::binary_filename(filename);
205 if (istr == (istream *)NULL) {
210 bool result = hash_stream(*istr);
215 #endif // HAVE_OPENSSL
228 hash_stream(istream &stream) {
229 unsigned char md[16];
234 static const int buffer_size = 1024;
235 char buffer[buffer_size];
238 stream.seekg(0, ios::beg);
240 stream.read(buffer, buffer_size);
241 size_t count = stream.gcount();
243 MD5_Update(&ctx, buffer, count);
244 stream.read(buffer, buffer_size);
245 count = stream.gcount();
256 _hv[0] = (md[0] << 24) | (md[1] << 16) | (md[2] << 8) | (md[3]);
257 _hv[1] = (md[4] << 24) | (md[5] << 16) | (md[6] << 8) | (md[7]);
258 _hv[2] = (md[8] << 24) | (md[9] << 16) | (md[10] << 8) | (md[11]);
259 _hv[3] = (md[12] << 24) | (md[13] << 16) | (md[14] << 8) | (md[15]);
263 #endif // HAVE_OPENSSL
276 hash_buffer(
const char *buffer,
int length) {
277 unsigned char md[16];
278 MD5((
const unsigned char *)buffer, length, md);
282 _hv[0] = (md[0] << 24) | (md[1] << 16) | (md[2] << 8) | (md[3]);
283 _hv[1] = (md[4] << 24) | (md[5] << 16) | (md[6] << 8) | (md[7]);
284 _hv[2] = (md[8] << 24) | (md[9] << 16) | (md[10] << 8) | (md[11]);
285 _hv[3] = (md[12] << 24) | (md[13] << 16) | (md[14] << 8) | (md[15]);
288 #endif // HAVE_OPENSSL
299 encode_hex(PN_uint32 val,
char *buffer) {
300 buffer[0] = tohex(val >> 28);
301 buffer[1] = tohex(val >> 24);
302 buffer[2] = tohex(val >> 20);
303 buffer[3] = tohex(val >> 16);
304 buffer[4] = tohex(val >> 12);
305 buffer[5] = tohex(val >> 8);
306 buffer[6] = tohex(val >> 4);
307 buffer[7] = tohex(val);
317 decode_hex(
const char *buffer, PN_uint32 &val) {
318 unsigned int bytes[8];
319 for (
int i = 0; i < 8; i++) {
320 bytes[i] = fromhex(buffer[i]);
323 val = ((bytes[0] << 28) |
A StreamWriter object is used to write sequential binary data directly to an ostream.
string get_message() const
Returns the datagram's data as a string.
void input_dec(istream &in)
Inputs the HashVal as four unsigned decimal integers.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
string as_dec() const
Returns the HashVal as a string with four decimal numbers.
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer...
bool set_from_hex(const string &text)
Sets the HashVal from a 32-byte hexademical string.
void input_hex(istream &in)
Inputs the HashVal as a 32-digit hexadecimal number.
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
bool set_from_dec(const string &text)
Sets the HashVal from a string with four decimal numbers.
The name of a file, such as a texture file or an Egg file.
void add_be_uint32(PN_uint32 value)
Adds an unsigned 32-bit big-endian integer to the streamWriter.
void output_hex(ostream &out) const
Outputs the HashVal as a 32-digit hexadecimal number.
bool set_from_bin(const string &text)
Sets the HashVal from a 16-byte binary string.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
void output_dec(ostream &out) const
Outputs the HashVal as four unsigned decimal integers.
void output_binary(ostream &out) const
Outputs the HashVal as a binary stream of bytes in order.
istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read...
string as_bin() const
Returns the HashVal as a 16-byte binary string.
A class to retrieve the individual data elements previously stored in a Datagram. ...
string as_hex() const
Returns the HashVal as a 32-byte hexadecimal string.
A class to read sequential binary data directly from an istream.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
PN_uint32 get_be_uint32()
Extracts an unsigned big-endian 32-bit integer.
void input_binary(istream &in)
Inputs the HashVal as a binary stream of bytes in order.