1 °³¿ä
Crc32Hashing°ú ¸¶Âù°¡Áö·Î ºü¸¥ ¼Óµµ¸¦ ¿°µÎ¿¡ µÐ ¸Þ¼¼Áö ´ÙÀÌÁ¦½ºÆ®¿ë ÇØ½¬ ¾Ë°í¸®Áò. ´©°¡ ´õ ºü¸¥Áö´Â Àß ¸ð¸£°Ú´Ù. Å×½ºÆ®ÇغÁ¾ß Çϳª...
2 ¼Ò½º
//////////////////////////////////////////////////////////////////////////////
/// \file Adler32Module.h
/// \author excel96
/// \date 2004.8.18
//////////////////////////////////////////////////////////////////////////////
#ifndef __ADLER32MODULE_H__
#define __ADLER32MODULE_H__
//////////////////////////////////////////////////////////////////////////////
/// \class Adler32Module
/// \brief
//////////////////////////////////////////////////////////////////////////////
class Adler32Module
{
private:
unsigned int m_Hash; ///< ÇØ½¬°ª
public:
/// \brief ±âº» »ý¼ºÀÚ
Adler32Module() { reset(); }
/// \brief ¹®ÀÚ¿À» ¿ä¾à
Adler32Module(unsigned char* str) { reset(); update(str, (unsigned int)strlen((const char*)str)); }
/// \brief ¹®ÀÚ¿À» ¿ä¾à
Adler32Module(const std::string& text) { reset(); update(text); }
/// \brief ÆÄÀÏÀÇ ³»¿ëÀ» ¿ä¾à
Adler32Module(FILE* file) { reset(); update(file); }
/// \brief ½ºÆ®¸²ÀÇ ³»¿ëÀ» ¿ä¾à
Adler32Module(std::istream& stream) { reset(); update(stream); }
/// \brief ÆÄÀÏ ½ºÆ®¸²ÀÇ ³»¿ëÀ» ¿ä¾à
Adler32Module(std::ifstream& stream) { reset(); update(stream); }
/// \brief ¼Ò¸êÀÚ
virtual ~Adler32Module() {}
public:
// ¹ÙÀÌÆ® ¹öÆÛ¸¦ ¿ä¾à
unsigned int update(const unsigned char* buffer, unsigned int len)
{
if (!len || !buffer) return 0;
unsigned int s1 = m_Hash & 0xFFFF;
unsigned int s2 = m_Hash >> 16;
unsigned int n = len;
while (n > 0)
{
// 5552 is the largest n such that
// 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
int k = (n < 5552 ? n : 5552);
n -= k;
// Modern processors will NOT benefit from
// coded loop optimisation techniques (unrolling)!
while (k--)
{
s1 += *buffer++;
s2 += s1;
}
s1 %= 65521; // 65521 is the largest prime smaller than 65536
s2 %= 65521;
}
return m_Hash = (s2 << 16) | s1;
}
/// \brief ¹®ÀÚ¿À» ¿ä¾à
void update(const std::string& text)
{
update((unsigned char*)text.c_str(), (unsigned int)text.size());
}
/// \brief ½ºÆ®¸²ÀÇ ³»¿ëÀ» ¿ä¾à
void update(std::istream& stream)
{
unsigned char buffer[1024];
while (stream.good())
{
// note that return value of read is unusable.
stream.read((char*)(&buffer[0]), 1024);
update(buffer, stream.gcount());
}
}
/// \brief ÆÄÀÏ ½ºÆ®¸²ÀÇ ³»¿ëÀ» ¿ä¾à
void update(std::ifstream& stream)
{
unsigned char buffer[1024];
while (stream.good())
{
// note that return value of read is unusable.
stream.read((char*)(&buffer[0]), 1024);
update(buffer, stream.gcount());
}
}
/// \brief ÆÄÀÏÀÇ ³»¿ëÀ» ¿ä¾à
void update(FILE* file)
{
unsigned char buffer[1024];
size_t len = 0;
while ((len = ::fread(buffer, 1, 1024, file)) > 0)
{
update(buffer, (unsigned int)len);
}
::fclose(file);
}
public:
// ÇØ½¬°ªÀ» ÃʱâÈÇÑ´Ù.
void reset() { m_Hash = 1; }
// ÇØ½¬°ªÀ» ¹ÝȯÇÑ´Ù.
unsigned int digest() const { return m_Hash; }
};
#endif //__ADLER32MODULE_H__
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)