src/test/SDL_test_crc32.c
author Ryan C. Gordon
Wed, 18 Jan 2017 02:11:56 -0500
changeset 10817 efc103e60c5b
parent 10737 3406a0f8b041
child 11465 9843b1895c45
permissions -rw-r--r--
audio: Several fixes to "simple" resampler (thanks, Vitaly!).

Fixes Bugzilla #3551.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /*
    23 
    24  Used by the test execution component.
    25  Original source code contributed by A. Schiffler for GSOC project.
    26 
    27 */
    28 
    29 #include "SDL_config.h"
    30 
    31 #include "SDL_test.h"
    32 
    33 
    34 int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
    35 {
    36   int i,j;
    37   CrcUint32 c;
    38 
    39   /* Sanity check context pointer */
    40   if (crcContext==NULL) {
    41    return -1;
    42   }
    43 
    44   /*
    45    * Build auxiliary table for parallel byte-at-a-time CRC-32
    46    */
    47 #ifdef ORIGINAL_METHOD
    48   for (i = 0; i < 256; ++i) {
    49     for (c = i << 24, j = 8; j > 0; --j) {
    50       c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
    51     }
    52     crcContext->crc32_table[i] = c;
    53   }
    54 #else
    55   for (i=0; i<256; i++) {
    56    c = i;
    57    for (j=8; j>0; j--) {
    58     if (c & 1) {
    59      c = (c >> 1) ^ CRC32_POLY;
    60     } else {
    61      c >>= 1;
    62     }
    63    }
    64    crcContext->crc32_table[i] = c;
    65   }
    66 #endif
    67 
    68   return 0;
    69 }
    70 
    71 /* Complete CRC32 calculation on a memory block */
    72 /* un-used
    73 int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
    74 {
    75   if (SDLTest_Crc32CalcStart(crcContext,crc32)) {
    76    return -1;
    77   }
    78 
    79   if (SDLTest_Crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
    80    return -1;
    81   }
    82 
    83   if (SDLTest_Crc32CalcEnd(crcContext, crc32)) {
    84    return -1;
    85   }
    86 
    87   return 0;
    88 }
    89 */
    90 
    91 /* Start crc calculation */
    92 
    93 int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
    94 {
    95   /* Sanity check pointers */
    96   if (crcContext==NULL) {
    97    *crc32=0;
    98    return -1;
    99   }
   100 
   101   /*
   102    * Preload shift register, per CRC-32 spec
   103    */
   104   *crc32 = 0xffffffff;
   105 
   106   return 0;
   107 }
   108 
   109 /* Finish crc calculation */
   110 
   111 int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
   112 {
   113   /* Sanity check pointers */
   114   if (crcContext==NULL) {
   115    *crc32=0;
   116    return -1;
   117   }
   118 
   119   /*
   120    * Return complement, per CRC-32 spec
   121    */
   122   *crc32 = (~(*crc32));
   123 
   124   return 0;
   125 }
   126 
   127 /* Include memory block in crc */
   128 
   129 int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
   130 {
   131   CrcUint8    *p;
   132   register CrcUint32    crc;
   133 
   134   if (crcContext==NULL) {
   135    *crc32=0;
   136    return -1;
   137   }
   138 
   139   if (inBuf==NULL) {
   140    return -1;
   141   }
   142 
   143   /*
   144    * Calculate CRC from data
   145    */
   146   crc = *crc32;
   147   for (p = inBuf; inLen > 0; ++p, --inLen) {
   148 #ifdef ORIGINAL_METHOD
   149     crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
   150 #else
   151     crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
   152 #endif
   153   }
   154   *crc32 = crc;
   155 
   156   return 0;
   157 }
   158 
   159 int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext)
   160 {
   161   if (crcContext==NULL) {
   162      return -1;
   163   }
   164 
   165   return 0;
   166 }