src/test/SDL_test_crc32.c
changeset 6713 fa17a97389ed
child 6885 700f1b25f77f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_crc32.c	Wed Nov 28 19:19:57 2012 -0800
     1.3 @@ -0,0 +1,165 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +
    1.27 + Used by the test execution component. 
    1.28 + Original source code contributed by A. Schiffler for GSOC project.
    1.29 +
    1.30 +*/
    1.31 +
    1.32 +#include "SDL_config.h"
    1.33 +
    1.34 +#include "SDL_test.h"
    1.35 +
    1.36 +
    1.37 +int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
    1.38 +{
    1.39 +  int i,j;
    1.40 +  CrcUint32 c;
    1.41 +
    1.42 +  /* Sanity check context pointer */
    1.43 +  if (crcContext==NULL) {
    1.44 +   return -1;
    1.45 +  }  
    1.46 + 
    1.47 +  /*
    1.48 +   * Build auxiliary table for parallel byte-at-a-time CRC-32 
    1.49 +   */
    1.50 +#ifdef ORIGINAL_METHOD
    1.51 +  for (i = 0; i < 256; ++i) {
    1.52 +    for (c = i << 24, j = 8; j > 0; --j) {
    1.53 +      c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
    1.54 +    }
    1.55 +    crcContext->crc32_table[i] = c;
    1.56 +  }
    1.57 +#else
    1.58 +  for (i=0; i<256; i++) {
    1.59 +   c = i;
    1.60 +   for (j=8; j>0; j--) {
    1.61 +    if (c & 1) {
    1.62 +     c = (c >> 1) ^ CRC32_POLY;
    1.63 +    } else {
    1.64 +     c >>= 1;
    1.65 +    }
    1.66 +   }
    1.67 +   crcContext->crc32_table[i] = c;
    1.68 +  }
    1.69 +#endif
    1.70 +  
    1.71 +  return 0;
    1.72 +}
    1.73 +
    1.74 +/* Complete CRC32 calculation on a memory block */
    1.75 +
    1.76 +int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
    1.77 +{
    1.78 +  if (SDLTest_Crc32CalcStart(crcContext,crc32)) {
    1.79 +   return -1;
    1.80 +  }
    1.81 +  
    1.82 +  if (SDLTest_Crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
    1.83 +   return -1;
    1.84 +  }
    1.85 +  
    1.86 +  if (SDLTest_Crc32CalcEnd(crcContext, crc32)) {
    1.87 +   return -1;
    1.88 +  }
    1.89 +   
    1.90 +  return 0;
    1.91 +}
    1.92 +
    1.93 +/* Start crc calculation */
    1.94 +
    1.95 +int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
    1.96 +{
    1.97 +  /* Sanity check pointers */
    1.98 +  if (crcContext==NULL) {
    1.99 +   *crc32=0;
   1.100 +   return -1;
   1.101 +  }  
   1.102 +
   1.103 +  /*
   1.104 +   * Preload shift register, per CRC-32 spec 
   1.105 +   */
   1.106 +  *crc32 = 0xffffffff;
   1.107 +
   1.108 +  return 0;
   1.109 +}
   1.110 +
   1.111 +/* Finish crc calculation */
   1.112 +
   1.113 +int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
   1.114 +{
   1.115 +  /* Sanity check pointers */
   1.116 +  if (crcContext==NULL) {
   1.117 +   *crc32=0;
   1.118 +   return -1;
   1.119 +  }  
   1.120 +  
   1.121 +  /*
   1.122 +   * Return complement, per CRC-32 spec 
   1.123 +   */
   1.124 +  *crc32 = (~(*crc32));
   1.125 +
   1.126 +  return 0;
   1.127 +}
   1.128 +
   1.129 +/* Include memory block in crc */
   1.130 +
   1.131 +int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
   1.132 +{
   1.133 +  CrcUint8    *p;
   1.134 +  register CrcUint32    crc;
   1.135 +
   1.136 +  if (crcContext==NULL) {
   1.137 +   *crc32=0;
   1.138 +   return -1;
   1.139 +  }
   1.140 +  
   1.141 +  if (inBuf==NULL) {
   1.142 +   return -1;
   1.143 +  }
   1.144 +
   1.145 +  /*
   1.146 +   * Calculate CRC from data 
   1.147 +   */
   1.148 +  crc = *crc32;
   1.149 +  for (p = inBuf; inLen > 0; ++p, --inLen) {
   1.150 +#ifdef ORIGINAL_METHOD  
   1.151 +    crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
   1.152 +#else
   1.153 +    crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
   1.154 +#endif         
   1.155 +  }  
   1.156 +  *crc32 = crc;
   1.157 +  
   1.158 +  return 0;
   1.159 +}
   1.160 +
   1.161 +int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext)
   1.162 +{
   1.163 +  if (crcContext==NULL) {
   1.164 +     return -1;
   1.165 +  }  
   1.166 +
   1.167 +  return 0;
   1.168 +}