Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
CRC32CheckSum.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef CRC32CheckSum_INCLUDE_ONCE
33 #define CRC32CheckSum_INCLUDE_ONCE
34 
35 #include <vlCore/VirtualFile.hpp>
36 
37 namespace vl
38 {
43  {
44  // Lower this if you need to limit the amount of data allocated to the stack, for example to 16K.
45  static const int CHUNK_SIZE = 128*1024;
46 
47  public:
50 
51  unsigned int compute(const void* buf, int length)
52  {
53  const unsigned char* buffer = (const unsigned char*)buf;
54  unsigned int mCRC32 = 0xffffffff;
55  while(length--)
56  mCRC32 = (mCRC32 >> 8) ^ crc32_table[(mCRC32 & 0xFF) ^ *buffer++];
57  return mCRC32 ^ 0xffffffff;
58  }
59 
60  unsigned int compute(VirtualFile* stream)
61  {
62  std::vector<unsigned char> buffer;
63  buffer.resize(CHUNK_SIZE);
64  unsigned char *ptr = &buffer.front();
65  startCRC32();
66 
67  for( int length = (int)stream->read(ptr, CHUNK_SIZE); length; length = (int)stream->read(ptr, CHUNK_SIZE) )
68  continueCRC32(ptr,length);
69 
70  return finalizeCRC32();
71  }
72 
73  void startCRC32() { mCRC32 = 0xffffffff; }
74  unsigned int finalizeCRC32() { return mCRC32 ^ 0xffffffff; }
75  void continueCRC32(unsigned char* ptr, int length)
76  {
77  while(length--)
78  mCRC32 = (mCRC32 >> 8) ^ crc32_table[(mCRC32 & 0xFF) ^ *ptr++];
79  }
80 
81  protected:
82  void crc32_init()
83  {
84  mCRC32 = 0;
85  unsigned int ulPolynomial = 0x04c11db7;
86  for(int i = 0; i <= 0xFF; i++)
87  {
88  crc32_table[i]=reflect(i, 8) << 24;
89  for (int j = 0; j < 8; j++)
90  crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
91  crc32_table[i] = reflect(crc32_table[i], 32);
92  }
93  }
94 
95  unsigned int reflect(unsigned int val, char ch)
96  {
97  unsigned int refl = 0;
98  for(int i = 1; i < (ch + 1); i++)
99  {
100  if(val & 1)
101  refl |= 1 << (ch - i);
102  val >>= 1;
103  }
104  return refl;
105  }
106 
107  protected:
108  unsigned int mCRC32;
109  unsigned int crc32_table[256];
110  };
111 }
112 
113 #endif
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
unsigned int compute(const void *buf, int length)
Computes the a CRC32 checksum of a given buffer or VirtualFile.
An abstract class representing a file.
Definition: VirtualFile.hpp:60
unsigned int crc32_table[256]
unsigned int finalizeCRC32()
unsigned int reflect(unsigned int val, char ch)
Visualization Library main namespace.
void continueCRC32(unsigned char *ptr, int length)
unsigned int mCRC32
unsigned int compute(VirtualFile *stream)
T length(T v)
Definition: glsl_math.hpp:1084
CRC32CheckSum()
Constructor.