# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/CRC32CheckSum.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.org                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef CRC32CheckSum_INCLUDE_ONCE
    00033 #define CRC32CheckSum_INCLUDE_ONCE
    00034
    00035 #include <vlCore/VirtualFile.hpp>
    00036
    00037 namespace vl
    00038 {
    00042   class CRC32CheckSum
    00043   {
    00044     // Lower this if you need to limit the amount of data allocated to the stack, for example to 16K.
    00045     static const int CHUNK_SIZE = 128*1024;
    00046
    00047   public:
    00049     CRC32CheckSum() { crc32_init(); }
    00050
    00051     unsigned int compute(const void* buf, int length)
    00052     {
    00053       const unsigned char* buffer = (const unsigned char*)buf;
    00054       unsigned int mCRC32 = 0xffffffff;
    00055       while(length--)
    00056         mCRC32 = (mCRC32 >> 8) ^ crc32_table[(mCRC32 & 0xFF) ^ *buffer++];
    00057       return mCRC32 ^ 0xffffffff;
    00058     }
    00059
    00060     unsigned int compute(VirtualFile* stream)
    00061     {
    00062       std::vector<unsigned char> buffer;
    00063       buffer.resize(CHUNK_SIZE);
    00064       unsigned char *ptr = &buffer.front();
    00065       startCRC32();
    00066
    00067       for( int length = (int)stream->read(ptr, CHUNK_SIZE); length; length = (int)stream->read(ptr, CHUNK_SIZE) )
    00068         continueCRC32(ptr,length);
    00069
    00070       return finalizeCRC32();
    00071     }
    00072
    00073     void startCRC32() { mCRC32 = 0xffffffff; }
    00074     unsigned int finalizeCRC32() { return mCRC32 ^ 0xffffffff; }
    00075     void continueCRC32(unsigned char* ptr, int length)
    00076     {
    00077       while(length--)
    00078         mCRC32 = (mCRC32 >> 8) ^ crc32_table[(mCRC32 & 0xFF) ^ *ptr++];
    00079     }
    00080
    00081   protected:
    00082     void crc32_init()
    00083     {
    00084       mCRC32 = 0;
    00085       unsigned int ulPolynomial = 0x04c11db7;
    00086       for(int i = 0; i <= 0xFF; i++)
    00087       {
    00088         crc32_table[i]=reflect(i, 8) << 24;
    00089         for (int j = 0; j < 8; j++)
    00090           crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
    00091         crc32_table[i] = reflect(crc32_table[i], 32);
    00092       }
    00093     }
    00094
    00095     unsigned int reflect(unsigned int val, char ch)
    00096     {
    00097       unsigned int refl = 0;
    00098       for(int i = 1; i < (ch + 1); i++)
    00099       {
    00100         if(val & 1)
    00101           refl |= 1 << (ch - i);
    00102         val >>= 1;
    00103       }
    00104       return refl;
    00105     }
    00106
    00107   protected:
    00108     unsigned int mCRC32;
    00109     unsigned int crc32_table[256];
    00110   };
    00111 }
    00112
    00113 #endif

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:26.
Permission is granted to use this page to write and publish articles regarding Visualization Library.