config.tests/mac/crc/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the config.tests of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <iostream>
       
    43 #include <cstdlib>
       
    44 #include <cstring>
       
    45 
       
    46 
       
    47 class CCRC32
       
    48 {
       
    49 public:
       
    50     CCRC32() { initialize(); }
       
    51 
       
    52     unsigned long FullCRC(const unsigned char *sData, unsigned long ulDataLength)
       
    53     {
       
    54         unsigned long ulCRC = 0xffffffff;
       
    55         PartialCRC(&ulCRC, sData, ulDataLength);
       
    56         return(ulCRC ^ 0xffffffff);
       
    57     }
       
    58 
       
    59     void PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength)
       
    60     {
       
    61         while(ulDataLength--) {
       
    62             *ulCRC = (*ulCRC >> 8) ^ ulTable[(*ulCRC & 0xFF) ^ *sData++];
       
    63         }
       
    64     }
       
    65 
       
    66 private:
       
    67     void initialize(void)
       
    68     {
       
    69         unsigned long ulPolynomial = 0x04C11DB7;
       
    70         memset(&ulTable, 0, sizeof(ulTable));
       
    71         for(int iCodes = 0; iCodes <= 0xFF; iCodes++) {
       
    72             ulTable[iCodes] = Reflect(iCodes, 8) << 24;
       
    73             for(int iPos = 0; iPos < 8; iPos++) {
       
    74                 ulTable[iCodes] = (ulTable[iCodes] << 1)
       
    75                     ^ ((ulTable[iCodes] & (1 << 31)) ? ulPolynomial : 0);
       
    76             }
       
    77 
       
    78             ulTable[iCodes] = Reflect(ulTable[iCodes], 32);
       
    79         }
       
    80     }
       
    81     unsigned long Reflect(unsigned long ulReflect, const char cChar)
       
    82     {
       
    83         unsigned long ulValue = 0;
       
    84         // Swap bit 0 for bit 7, bit 1 For bit 6, etc....
       
    85         for(int iPos = 1; iPos < (cChar + 1); iPos++) {
       
    86             if(ulReflect & 1) {
       
    87                 ulValue |= (1 << (cChar - iPos));
       
    88             }
       
    89             ulReflect >>= 1;
       
    90         }
       
    91         return ulValue;
       
    92     }
       
    93     unsigned long ulTable[256]; // CRC lookup table array.
       
    94 };
       
    95 
       
    96 
       
    97 int main(int argc, char **argv)
       
    98 {
       
    99     CCRC32 crc;
       
   100     char *name;
       
   101     if (argc < 2) {
       
   102         std::cerr << "usage: crc <string>\n";
       
   103         return 0;
       
   104     } else {
       
   105         name = argv[1];
       
   106     }
       
   107     std::cout << crc.FullCRC((unsigned char *)name, strlen(name)) << std::endl;
       
   108 }