Add crc32 and md5 to test lib
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Wed, 28 Nov 2012 19:19:57 -0800
changeset 6713fa17a97389ed
parent 6712 05f046f5886b
child 6714 146d1d7ecdc4
Add crc32 and md5 to test lib
include/SDL_test.h
include/SDL_test_crc32.h
include/SDL_test_md5.h
include/SDL_test_random.h
src/test/SDL_test_crc32.c
src/test/SDL_test_md5.c
     1.1 --- a/include/SDL_test.h	Wed Nov 28 11:52:38 2012 -0800
     1.2 +++ b/include/SDL_test.h	Wed Nov 28 19:19:57 2012 -0800
     1.3 @@ -20,20 +20,22 @@
     1.4  */
     1.5  
     1.6  /**
     1.7 - *  \file SDLTest.h
     1.8 + *  \file SDL_test.h
     1.9   *  
    1.10   *  Include file for SDL test framework.
    1.11   *
    1.12   *  This code is a part of the SDL2_test library, not the main SDL library.
    1.13   */
    1.14  
    1.15 -#ifndef _SDLTest_h
    1.16 -#define _SDLTest_h
    1.17 +#ifndef _SDL_test_h
    1.18 +#define _SDL_test_h
    1.19  
    1.20  #include "SDL.h"
    1.21  #include "SDL_test_font.h"
    1.22  #include "SDL_test_random.h"
    1.23  #include "SDL_test_fuzzer.h"
    1.24 +#include "SDL_test_crc32.h"
    1.25 +#include "SDL_test_md5.h"
    1.26  
    1.27  #include "begin_code.h"
    1.28  /* Set up for C function definitions, even when using C++ */
    1.29 @@ -55,6 +57,6 @@
    1.30  #endif
    1.31  #include "close_code.h"
    1.32  
    1.33 -#endif /* _SDLTest_h */
    1.34 +#endif /* _SDL_test_h */
    1.35  
    1.36  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/include/SDL_test_crc32.h	Wed Nov 28 19:19:57 2012 -0800
     2.3 @@ -0,0 +1,128 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +
    2.25 +/**
    2.26 + *  \file SDL_test_crc32.h
    2.27 + *  
    2.28 + *  Include file for SDL test framework.
    2.29 + *
    2.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    2.31 + */
    2.32 +
    2.33 +/* 
    2.34 +
    2.35 + Implements CRC32 calculations (default output is Perl String::CRC32 compatible).
    2.36 +  
    2.37 +*/
    2.38 +
    2.39 +#ifndef _SDL_test_crc32_h
    2.40 +#define _SDL_test_crc32_h
    2.41 +
    2.42 +#include "begin_code.h"
    2.43 +/* Set up for C function definitions, even when using C++ */
    2.44 +#ifdef __cplusplus
    2.45 +/* *INDENT-OFF* */
    2.46 +extern "C" {
    2.47 +/* *INDENT-ON* */
    2.48 +#endif
    2.49 +
    2.50 +
    2.51 +/* ------------ Definitions --------- */
    2.52 +
    2.53 +/* Definition shared by all CRC routines */
    2.54 +
    2.55 +#ifndef CrcUint32
    2.56 + #define CrcUint32	unsigned int
    2.57 +#endif
    2.58 +#ifndef CrcUint8
    2.59 + #define CrcUint8	unsigned char
    2.60 +#endif
    2.61 +
    2.62 +#ifdef ORIGINAL_METHOD
    2.63 + #define CRC32_POLY 0x04c11db7 	 /* AUTODIN II, Ethernet, & FDDI */
    2.64 +#else
    2.65 + #define CRC32_POLY 0xEDB88320   /* Perl String::CRC32 compatible */
    2.66 +#endif
    2.67 +
    2.68 +/** 
    2.69 + * Data structure for CRC32 (checksum) computation 
    2.70 + */
    2.71 +  typedef struct {
    2.72 +    CrcUint32    crc32_table[256]; /* CRC table */
    2.73 +  } SDLTest_Crc32Context;
    2.74 +
    2.75 +/* ---------- Function Prototypes ------------- */
    2.76 +
    2.77 +/** 
    2.78 + * /brief Initialize the CRC context
    2.79 + *
    2.80 + * Note: The function initializes the crc table required for all crc calculations.
    2.81 + *
    2.82 + * /param crcContext		pointer to context variable
    2.83 + *
    2.84 + * /returns 0 for OK, -1 on error
    2.85 + *
    2.86 + */
    2.87 + int SDLTest_Crc32Init(SDLTest_Crc32Context * crcContext);
    2.88 +
    2.89 +
    2.90 +/**
    2.91 + * /brief calculate a crc32 from a data block
    2.92 + * 
    2.93 + * /param crcContext	     pointer to context variable
    2.94 + * /param inBuf              input buffer to checksum
    2.95 + * /param inLen              length of input buffer
    2.96 + * /param crc32              pointer to Uint32 to store the final CRC into
    2.97 + *
    2.98 + * /returns 0 for OK, -1 on error
    2.99 + *
   2.100 + */
   2.101 +int SDLTest_crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32);
   2.102 +
   2.103 +/* Same routine broken down into three steps */
   2.104 +int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32);
   2.105 +int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32);
   2.106 +int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32);
   2.107 +
   2.108 +
   2.109 +/**
   2.110 + * /brief clean up CRC context
   2.111 + *
   2.112 + * /param crcContext		pointer to context variable
   2.113 + *
   2.114 + * /returns 0 for OK, -1 on error
   2.115 + *
   2.116 +*/
   2.117 +
   2.118 +int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext);
   2.119 +
   2.120 +
   2.121 +/* Ends C function definitions when using C++ */
   2.122 +#ifdef __cplusplus
   2.123 +/* *INDENT-OFF* */
   2.124 +}
   2.125 +/* *INDENT-ON* */
   2.126 +#endif
   2.127 +#include "close_code.h"
   2.128 +
   2.129 +#endif /* _SDL_test_crc32_h */
   2.130 +
   2.131 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/SDL_test_md5.h	Wed Nov 28 19:19:57 2012 -0800
     3.3 @@ -0,0 +1,133 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +
    3.25 +/**
    3.26 + *  \file SDL_test_md5.h
    3.27 + *  
    3.28 + *  Include file for SDL test framework.
    3.29 + *
    3.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    3.31 + */
    3.32 +
    3.33 +/*
    3.34 + ***********************************************************************
    3.35 + ** Header file for implementation of MD5                             **
    3.36 + ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
    3.37 + ** Created: 2/17/90 RLR                                              **
    3.38 + ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version               **
    3.39 + ** Revised (for MD5): RLR 4/27/91                                    **
    3.40 + **   -- G modified to have y&~z instead of y&z                       **
    3.41 + **   -- FF, GG, HH modified to add in last register done             **
    3.42 + **   -- Access pattern: round 2 works mod 5, round 3 works mod 3     **
    3.43 + **   -- distinct additive constant for each step                     **
    3.44 + **   -- round 4 added, working mod 7                                 **
    3.45 + ***********************************************************************
    3.46 +*/
    3.47 +
    3.48 +/*
    3.49 + ***********************************************************************
    3.50 + **  Message-digest routines:                                         **
    3.51 + **  To form the message digest for a message M                       **
    3.52 + **    (1) Initialize a context buffer mdContext using MD5Init        **
    3.53 + **    (2) Call MD5Update on mdContext and M                          **
    3.54 + **    (3) Call MD5Final on mdContext                                 **
    3.55 + **  The message digest is now in mdContext->digest[0...15]           **
    3.56 + ***********************************************************************
    3.57 +*/
    3.58 +
    3.59 +#ifndef _SDL_test_md5_h
    3.60 +#define _SDL_test_md5_h
    3.61 +
    3.62 +#include "begin_code.h"
    3.63 +/* Set up for C function definitions, even when using C++ */
    3.64 +#ifdef __cplusplus
    3.65 +/* *INDENT-OFF* */
    3.66 +extern "C" {
    3.67 +/* *INDENT-ON* */
    3.68 +#endif
    3.69 +
    3.70 +/* ------------ Definitions --------- */
    3.71 +
    3.72 +/* typedef a 32-bit type */
    3.73 +  typedef unsigned long int MD5UINT4;
    3.74 +
    3.75 +/* Data structure for MD5 (Message-Digest) computation */
    3.76 +  typedef struct {
    3.77 +    MD5UINT4  i[2];		/* number of _bits_ handled mod 2^64 */
    3.78 +    MD5UINT4  buf[4];		/* scratch buffer */
    3.79 +    unsigned char in[64];	/* input buffer */
    3.80 +    unsigned char digest[16];	/* actual digest after Md5Final call */
    3.81 +  } SDLTest_Md5Context;
    3.82 +
    3.83 +/* ---------- Function Prototypes ------------- */
    3.84 +
    3.85 +/** 
    3.86 + * /brief initialize the context
    3.87 + *
    3.88 + * /param  mdContext		pointer to context variable
    3.89 + *
    3.90 + * Note: The function initializes the message-digest context
    3.91 + *       mdContext. Call before each new use of the context - 
    3.92 + *       all fields are set to zero.
    3.93 + */
    3.94 + void SDLTest_Md5Init(SDLTest_Md5Context * mdContext);
    3.95 +
    3.96 +
    3.97 +/**
    3.98 + * /brief update digest from variable length data
    3.99 + * 
   3.100 + * /param  mdContext       pointer to context variable
   3.101 + * /param  inBuf           pointer to data array/string
   3.102 + * /param  inLen           length of data array/string
   3.103 + *
   3.104 + * Note: The function updates the message-digest context to account 
   3.105 + *       for the presence of each of the characters inBuf[0..inLen-1]
   3.106 + *       in the message whose digest is being computed.
   3.107 +*/
   3.108 +
   3.109 + void SDLTest_Md5Update(SDLTest_Md5Context * mdContext, unsigned char *inBuf,
   3.110 +				 unsigned int inLen);
   3.111 +
   3.112 +
   3.113 +/*
   3.114 + * /brief complete digest computation
   3.115 + *
   3.116 + * /param mdContext		pointer to context variable
   3.117 + *
   3.118 + * Note: The function terminates the message-digest computation and
   3.119 + *       ends with the desired message digest in mdContext.digest[0..15].
   3.120 + *       Always call before using the digest[] variable.
   3.121 +*/
   3.122 +
   3.123 + void SDLTest_Md5Final(SDLTest_Md5Context * mdContext);
   3.124 +
   3.125 +
   3.126 +/* Ends C function definitions when using C++ */
   3.127 +#ifdef __cplusplus
   3.128 +/* *INDENT-OFF* */
   3.129 +}
   3.130 +/* *INDENT-ON* */
   3.131 +#endif
   3.132 +#include "close_code.h"
   3.133 +
   3.134 +#endif /* _SDL_test_md5_h */
   3.135 +
   3.136 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/include/SDL_test_random.h	Wed Nov 28 11:52:38 2012 -0800
     4.2 +++ b/include/SDL_test_random.h	Wed Nov 28 19:19:57 2012 -0800
     4.3 @@ -48,10 +48,7 @@
     4.4  /* *INDENT-ON* */
     4.5  #endif
     4.6  
     4.7 -/* Function prototypes */
     4.8 -
     4.9 -
    4.10 -/* ------- Definitions ------- */
    4.11 +/* --- Definitions */
    4.12  
    4.13  /*
    4.14   * Macros that return a random number in a specific format.
    4.15 @@ -69,13 +66,14 @@
    4.16      unsigned int al;
    4.17    } SDLTest_RandomContext;
    4.18  
    4.19 -/* ---- Function Prototypes -------- */
    4.20 +
    4.21 +/* --- Function prototypes */
    4.22  
    4.23  /**
    4.24   *  \brief Initialize random number generator with two integers. 
    4.25   *
    4.26 - * Note: The random sequence of numbers returned by ...Random() is the
    4.27 - *       same for the same two integers and has a period of 2^31.
    4.28 + *  Note: The random sequence of numbers returned by ...Random() is the
    4.29 + *  same for the same two integers and has a period of 2^31.
    4.30   *
    4.31   *  \param rndContext     pointer to context structure
    4.32   *  \param xi         integer that defines the random sequence
    4.33 @@ -97,8 +95,8 @@
    4.34  /**
    4.35   *  \brief Initialize random number generator based on current system time. 
    4.36   *
    4.37 - * Note: ...RandomInit() or ...RandomInitTime() must have been called
    4.38 - *       before using this function.
    4.39 + *  Note: ...RandomInit() or ...RandomInitTime() must have been called
    4.40 + *  before using this function.
    4.41   *
    4.42   *  \param rndContext     pointer to context structure
    4.43   *
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/test/SDL_test_crc32.c	Wed Nov 28 19:19:57 2012 -0800
     5.3 @@ -0,0 +1,165 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +
    5.25 +/*
    5.26 +
    5.27 + Used by the test execution component. 
    5.28 + Original source code contributed by A. Schiffler for GSOC project.
    5.29 +
    5.30 +*/
    5.31 +
    5.32 +#include "SDL_config.h"
    5.33 +
    5.34 +#include "SDL_test.h"
    5.35 +
    5.36 +
    5.37 +int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
    5.38 +{
    5.39 +  int i,j;
    5.40 +  CrcUint32 c;
    5.41 +
    5.42 +  /* Sanity check context pointer */
    5.43 +  if (crcContext==NULL) {
    5.44 +   return -1;
    5.45 +  }  
    5.46 + 
    5.47 +  /*
    5.48 +   * Build auxiliary table for parallel byte-at-a-time CRC-32 
    5.49 +   */
    5.50 +#ifdef ORIGINAL_METHOD
    5.51 +  for (i = 0; i < 256; ++i) {
    5.52 +    for (c = i << 24, j = 8; j > 0; --j) {
    5.53 +      c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
    5.54 +    }
    5.55 +    crcContext->crc32_table[i] = c;
    5.56 +  }
    5.57 +#else
    5.58 +  for (i=0; i<256; i++) {
    5.59 +   c = i;
    5.60 +   for (j=8; j>0; j--) {
    5.61 +    if (c & 1) {
    5.62 +     c = (c >> 1) ^ CRC32_POLY;
    5.63 +    } else {
    5.64 +     c >>= 1;
    5.65 +    }
    5.66 +   }
    5.67 +   crcContext->crc32_table[i] = c;
    5.68 +  }
    5.69 +#endif
    5.70 +  
    5.71 +  return 0;
    5.72 +}
    5.73 +
    5.74 +/* Complete CRC32 calculation on a memory block */
    5.75 +
    5.76 +int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
    5.77 +{
    5.78 +  if (SDLTest_Crc32CalcStart(crcContext,crc32)) {
    5.79 +   return -1;
    5.80 +  }
    5.81 +  
    5.82 +  if (SDLTest_Crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
    5.83 +   return -1;
    5.84 +  }
    5.85 +  
    5.86 +  if (SDLTest_Crc32CalcEnd(crcContext, crc32)) {
    5.87 +   return -1;
    5.88 +  }
    5.89 +   
    5.90 +  return 0;
    5.91 +}
    5.92 +
    5.93 +/* Start crc calculation */
    5.94 +
    5.95 +int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
    5.96 +{
    5.97 +  /* Sanity check pointers */
    5.98 +  if (crcContext==NULL) {
    5.99 +   *crc32=0;
   5.100 +   return -1;
   5.101 +  }  
   5.102 +
   5.103 +  /*
   5.104 +   * Preload shift register, per CRC-32 spec 
   5.105 +   */
   5.106 +  *crc32 = 0xffffffff;
   5.107 +
   5.108 +  return 0;
   5.109 +}
   5.110 +
   5.111 +/* Finish crc calculation */
   5.112 +
   5.113 +int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
   5.114 +{
   5.115 +  /* Sanity check pointers */
   5.116 +  if (crcContext==NULL) {
   5.117 +   *crc32=0;
   5.118 +   return -1;
   5.119 +  }  
   5.120 +  
   5.121 +  /*
   5.122 +   * Return complement, per CRC-32 spec 
   5.123 +   */
   5.124 +  *crc32 = (~(*crc32));
   5.125 +
   5.126 +  return 0;
   5.127 +}
   5.128 +
   5.129 +/* Include memory block in crc */
   5.130 +
   5.131 +int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
   5.132 +{
   5.133 +  CrcUint8    *p;
   5.134 +  register CrcUint32    crc;
   5.135 +
   5.136 +  if (crcContext==NULL) {
   5.137 +   *crc32=0;
   5.138 +   return -1;
   5.139 +  }
   5.140 +  
   5.141 +  if (inBuf==NULL) {
   5.142 +   return -1;
   5.143 +  }
   5.144 +
   5.145 +  /*
   5.146 +   * Calculate CRC from data 
   5.147 +   */
   5.148 +  crc = *crc32;
   5.149 +  for (p = inBuf; inLen > 0; ++p, --inLen) {
   5.150 +#ifdef ORIGINAL_METHOD  
   5.151 +    crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
   5.152 +#else
   5.153 +    crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
   5.154 +#endif         
   5.155 +  }  
   5.156 +  *crc32 = crc;
   5.157 +  
   5.158 +  return 0;
   5.159 +}
   5.160 +
   5.161 +int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext)
   5.162 +{
   5.163 +  if (crcContext==NULL) {
   5.164 +     return -1;
   5.165 +  }  
   5.166 +
   5.167 +  return 0;
   5.168 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/test/SDL_test_md5.c	Wed Nov 28 19:19:57 2012 -0800
     6.3 @@ -0,0 +1,336 @@
     6.4 +/*
     6.5 +  Simple DirectMedia Layer
     6.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     6.7 +
     6.8 +  This software is provided 'as-is', without any express or implied
     6.9 +  warranty.  In no event will the authors be held liable for any damages
    6.10 +  arising from the use of this software.
    6.11 +
    6.12 +  Permission is granted to anyone to use this software for any purpose,
    6.13 +  including commercial applications, and to alter it and redistribute it
    6.14 +  freely, subject to the following restrictions:
    6.15 +
    6.16 +  1. The origin of this software must not be misrepresented; you must not
    6.17 +     claim that you wrote the original software. If you use this software
    6.18 +     in a product, an acknowledgment in the product documentation would be
    6.19 +     appreciated but is not required.
    6.20 +  2. Altered source versions must be plainly marked as such, and must not be
    6.21 +     misrepresented as being the original software.
    6.22 +  3. This notice may not be removed or altered from any source distribution.
    6.23 +*/
    6.24 +
    6.25 +/*
    6.26 + ***********************************************************************
    6.27 + ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
    6.28 + ** Created: 2/17/90 RLR                                              **
    6.29 + ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
    6.30 + ***********************************************************************
    6.31 + */
    6.32 +
    6.33 +/*
    6.34 + ***********************************************************************
    6.35 + ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
    6.36 + **                                                                   **
    6.37 + ** License to copy and use this software is granted provided that    **
    6.38 + ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
    6.39 + ** Digest Algorithm" in all material mentioning or referencing this  **
    6.40 + ** software or this function.                                        **
    6.41 + **                                                                   **
    6.42 + ** License is also granted to make and use derivative works          **
    6.43 + ** provided that such works are identified as "derived from the RSA  **
    6.44 + ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
    6.45 + ** material mentioning or referencing the derived work.              **
    6.46 + **                                                                   **
    6.47 + ** RSA Data Security, Inc. makes no representations concerning       **
    6.48 + ** either the merchantability of this software or the suitability    **
    6.49 + ** of this software for any particular purpose.  It is provided "as  **
    6.50 + ** is" without express or implied warranty of any kind.              **
    6.51 + **                                                                   **
    6.52 + ** These notices must be retained in any copies of any part of this  **
    6.53 + ** documentation and/or software.                                    **
    6.54 + ***********************************************************************
    6.55 + */
    6.56 +
    6.57 +#include "SDL_config.h"
    6.58 +
    6.59 +#include "SDL_test.h"
    6.60 +
    6.61 +/* Forward declaration of static helper function */
    6.62 +static void SDLTest_Md5Transform(MD5UINT4 * buf, MD5UINT4 * in);
    6.63 +
    6.64 +static unsigned char MD5PADDING[64] = {
    6.65 +  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.66 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.67 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.68 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.69 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.70 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.71 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.72 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    6.73 +};
    6.74 +
    6.75 +/* F, G, H and I are basic MD5 functions */
    6.76 +#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
    6.77 +#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
    6.78 +#define H(x, y, z) ((x) ^ (y) ^ (z))
    6.79 +#define I(x, y, z) ((y) ^ ((x) | (~z)))
    6.80 +
    6.81 +/* ROTATE_LEFT rotates x left n bits */
    6.82 +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
    6.83 +
    6.84 +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
    6.85 +
    6.86 +/* Rotation is separate from addition to prevent recomputation */
    6.87 +#define FF(a, b, c, d, x, s, ac) \
    6.88 +  {(a) += F ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    6.89 +   (a) = ROTATE_LEFT ((a), (s)); \
    6.90 +   (a) += (b); \
    6.91 +  }
    6.92 +#define GG(a, b, c, d, x, s, ac) \
    6.93 +  {(a) += G ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    6.94 +   (a) = ROTATE_LEFT ((a), (s)); \
    6.95 +   (a) += (b); \
    6.96 +  }
    6.97 +#define HH(a, b, c, d, x, s, ac) \
    6.98 +  {(a) += H ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    6.99 +   (a) = ROTATE_LEFT ((a), (s)); \
   6.100 +   (a) += (b); \
   6.101 +  }
   6.102 +#define II(a, b, c, d, x, s, ac) \
   6.103 +  {(a) += I ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
   6.104 +   (a) = ROTATE_LEFT ((a), (s)); \
   6.105 +   (a) += (b); \
   6.106 +  }
   6.107 +
   6.108 +/* 
   6.109 +  The routine MD5Init initializes the message-digest context
   6.110 +  mdContext. All fields are set to zero.
   6.111 +*/
   6.112 +
   6.113 +void SDLTest_Md5Init(SDLTest_Md5Context * mdContext)
   6.114 +{
   6.115 +  if (mdContext==NULL) return;
   6.116 +
   6.117 +  mdContext->i[0] = mdContext->i[1] = (MD5UINT4) 0;
   6.118 +
   6.119 +  /*
   6.120 +   * Load magic initialization constants.
   6.121 +   */
   6.122 +  mdContext->buf[0] = (MD5UINT4) 0x67452301;
   6.123 +  mdContext->buf[1] = (MD5UINT4) 0xefcdab89;
   6.124 +  mdContext->buf[2] = (MD5UINT4) 0x98badcfe;
   6.125 +  mdContext->buf[3] = (MD5UINT4) 0x10325476;
   6.126 +}
   6.127 +
   6.128 +/* 
   6.129 + The routine MD5Update updates the message-digest context to
   6.130 + account for the presence of each of the characters inBuf[0..inLen-1]
   6.131 + in the message whose digest is being computed.
   6.132 +*/
   6.133 +
   6.134 +void SDLTest_Md5Update(SDLTest_Md5Context * mdContext, unsigned char *inBuf,
   6.135 +		  unsigned int inLen)
   6.136 +{
   6.137 +  MD5UINT4  in[16];
   6.138 +  int       mdi;
   6.139 +  unsigned int i, ii;
   6.140 +
   6.141 +  if (mdContext == NULL) return;
   6.142 +  if (inBuf == NULL || inLen < 1) return;
   6.143 +
   6.144 +  /*
   6.145 +   * compute number of bytes mod 64 
   6.146 +   */
   6.147 +  mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
   6.148 +
   6.149 +  /*
   6.150 +   * update number of bits 
   6.151 +   */
   6.152 +  if ((mdContext->i[0] + ((MD5UINT4) inLen << 3)) < mdContext->i[0])
   6.153 +    mdContext->i[1]++;
   6.154 +  mdContext->i[0] += ((MD5UINT4) inLen << 3);
   6.155 +  mdContext->i[1] += ((MD5UINT4) inLen >> 29);
   6.156 +
   6.157 +  while (inLen--) {
   6.158 +    /*
   6.159 +     * add new character to buffer, increment mdi 
   6.160 +     */
   6.161 +    mdContext->in[mdi++] = *inBuf++;
   6.162 +
   6.163 +    /*
   6.164 +     * transform if necessary 
   6.165 +     */
   6.166 +    if (mdi == 0x40) {
   6.167 +      for (i = 0, ii = 0; i < 16; i++, ii += 4)
   6.168 +	in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
   6.169 +	  (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
   6.170 +	  (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
   6.171 +	  ((MD5UINT4) mdContext->in[ii]);
   6.172 +      SDLTest_Md5Transform(mdContext->buf, in);
   6.173 +      mdi = 0;
   6.174 +    }
   6.175 +  }
   6.176 +}
   6.177 +
   6.178 +/* 
   6.179 + The routine MD5Final terminates the message-digest computation and
   6.180 + ends with the desired message digest in mdContext->digest[0...15].
   6.181 +*/
   6.182 +
   6.183 +void SDLTest_Md5Final(SDLTest_Md5Context * mdContext)
   6.184 +{
   6.185 +  MD5UINT4  in[16];
   6.186 +  int       mdi;
   6.187 +  unsigned int i, ii;
   6.188 +  unsigned int padLen;
   6.189 +
   6.190 +  if (mdContext == NULL) return;
   6.191 +
   6.192 +  /*
   6.193 +   * save number of bits 
   6.194 +   */
   6.195 +  in[14] = mdContext->i[0];
   6.196 +  in[15] = mdContext->i[1];
   6.197 +
   6.198 +  /*
   6.199 +   * compute number of bytes mod 64 
   6.200 +   */
   6.201 +  mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
   6.202 +
   6.203 +  /*
   6.204 +   * pad out to 56 mod 64 
   6.205 +   */
   6.206 +  padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
   6.207 +  SDLTest_Md5Update(mdContext, MD5PADDING, padLen);
   6.208 +
   6.209 +  /*
   6.210 +   * append length in bits and transform 
   6.211 +   */
   6.212 +  for (i = 0, ii = 0; i < 14; i++, ii += 4)
   6.213 +    in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
   6.214 +      (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
   6.215 +      (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
   6.216 +      ((MD5UINT4) mdContext->in[ii]);
   6.217 +  SDLTest_Md5Transform(mdContext->buf, in);
   6.218 +
   6.219 +  /*
   6.220 +   * store buffer in digest 
   6.221 +   */
   6.222 +  for (i = 0, ii = 0; i < 4; i++, ii += 4) {
   6.223 +    mdContext->digest[ii] = (unsigned char) (mdContext->buf[i] & 0xFF);
   6.224 +    mdContext->digest[ii + 1] =
   6.225 +      (unsigned char) ((mdContext->buf[i] >> 8) & 0xFF);
   6.226 +    mdContext->digest[ii + 2] =
   6.227 +      (unsigned char) ((mdContext->buf[i] >> 16) & 0xFF);
   6.228 +    mdContext->digest[ii + 3] =
   6.229 +      (unsigned char) ((mdContext->buf[i] >> 24) & 0xFF);
   6.230 +  }
   6.231 +}
   6.232 +
   6.233 +/* Basic MD5 step. Transforms buf based on in.
   6.234 + */
   6.235 +static void SDLTest_Md5Transform(MD5UINT4 * buf, MD5UINT4 * in)
   6.236 +{
   6.237 +  MD5UINT4  a = buf[0], b = buf[1], c = buf[2], d = buf[3];
   6.238 +
   6.239 +  /*
   6.240 +   * Round 1 
   6.241 +   */
   6.242 +#define S11 7
   6.243 +#define S12 12
   6.244 +#define S13 17
   6.245 +#define S14 22
   6.246 +  FF(a, b, c, d, in[0], S11, 3614090360u);	/* 1 */
   6.247 +  FF(d, a, b, c, in[1], S12, 3905402710u);	/* 2 */
   6.248 +  FF(c, d, a, b, in[2], S13, 606105819u);	/* 3 */
   6.249 +  FF(b, c, d, a, in[3], S14, 3250441966u);	/* 4 */
   6.250 +  FF(a, b, c, d, in[4], S11, 4118548399u);	/* 5 */
   6.251 +  FF(d, a, b, c, in[5], S12, 1200080426u);	/* 6 */
   6.252 +  FF(c, d, a, b, in[6], S13, 2821735955u);	/* 7 */
   6.253 +  FF(b, c, d, a, in[7], S14, 4249261313u);	/* 8 */
   6.254 +  FF(a, b, c, d, in[8], S11, 1770035416u);	/* 9 */
   6.255 +  FF(d, a, b, c, in[9], S12, 2336552879u);	/* 10 */
   6.256 +  FF(c, d, a, b, in[10], S13, 4294925233u);	/* 11 */
   6.257 +  FF(b, c, d, a, in[11], S14, 2304563134u);	/* 12 */
   6.258 +  FF(a, b, c, d, in[12], S11, 1804603682u);	/* 13 */
   6.259 +  FF(d, a, b, c, in[13], S12, 4254626195u);	/* 14 */
   6.260 +  FF(c, d, a, b, in[14], S13, 2792965006u);	/* 15 */
   6.261 +  FF(b, c, d, a, in[15], S14, 1236535329u);	/* 16 */
   6.262 +
   6.263 +  /*
   6.264 +   * Round 2 
   6.265 +   */
   6.266 +#define S21 5
   6.267 +#define S22 9
   6.268 +#define S23 14
   6.269 +#define S24 20
   6.270 +  GG(a, b, c, d, in[1], S21, 4129170786u);	/* 17 */
   6.271 +  GG(d, a, b, c, in[6], S22, 3225465664u);	/* 18 */
   6.272 +  GG(c, d, a, b, in[11], S23, 643717713u);	/* 19 */
   6.273 +  GG(b, c, d, a, in[0], S24, 3921069994u);	/* 20 */
   6.274 +  GG(a, b, c, d, in[5], S21, 3593408605u);	/* 21 */
   6.275 +  GG(d, a, b, c, in[10], S22, 38016083u);	/* 22 */
   6.276 +  GG(c, d, a, b, in[15], S23, 3634488961u);	/* 23 */
   6.277 +  GG(b, c, d, a, in[4], S24, 3889429448u);	/* 24 */
   6.278 +  GG(a, b, c, d, in[9], S21, 568446438u);	/* 25 */
   6.279 +  GG(d, a, b, c, in[14], S22, 3275163606u);	/* 26 */
   6.280 +  GG(c, d, a, b, in[3], S23, 4107603335u);	/* 27 */
   6.281 +  GG(b, c, d, a, in[8], S24, 1163531501u);	/* 28 */
   6.282 +  GG(a, b, c, d, in[13], S21, 2850285829u);	/* 29 */
   6.283 +  GG(d, a, b, c, in[2], S22, 4243563512u);	/* 30 */
   6.284 +  GG(c, d, a, b, in[7], S23, 1735328473u);	/* 31 */
   6.285 +  GG(b, c, d, a, in[12], S24, 2368359562u);	/* 32 */
   6.286 +
   6.287 +  /*
   6.288 +   * Round 3 
   6.289 +   */
   6.290 +#define S31 4
   6.291 +#define S32 11
   6.292 +#define S33 16
   6.293 +#define S34 23
   6.294 +  HH(a, b, c, d, in[5], S31, 4294588738u);	/* 33 */
   6.295 +  HH(d, a, b, c, in[8], S32, 2272392833u);	/* 34 */
   6.296 +  HH(c, d, a, b, in[11], S33, 1839030562u);	/* 35 */
   6.297 +  HH(b, c, d, a, in[14], S34, 4259657740u);	/* 36 */
   6.298 +  HH(a, b, c, d, in[1], S31, 2763975236u);	/* 37 */
   6.299 +  HH(d, a, b, c, in[4], S32, 1272893353u);	/* 38 */
   6.300 +  HH(c, d, a, b, in[7], S33, 4139469664u);	/* 39 */
   6.301 +  HH(b, c, d, a, in[10], S34, 3200236656u);	/* 40 */
   6.302 +  HH(a, b, c, d, in[13], S31, 681279174u);	/* 41 */
   6.303 +  HH(d, a, b, c, in[0], S32, 3936430074u);	/* 42 */
   6.304 +  HH(c, d, a, b, in[3], S33, 3572445317u);	/* 43 */
   6.305 +  HH(b, c, d, a, in[6], S34, 76029189u);	/* 44 */
   6.306 +  HH(a, b, c, d, in[9], S31, 3654602809u);	/* 45 */
   6.307 +  HH(d, a, b, c, in[12], S32, 3873151461u);	/* 46 */
   6.308 +  HH(c, d, a, b, in[15], S33, 530742520u);	/* 47 */
   6.309 +  HH(b, c, d, a, in[2], S34, 3299628645u);	/* 48 */
   6.310 +
   6.311 +  /*
   6.312 +   * Round 4 
   6.313 +   */
   6.314 +#define S41 6
   6.315 +#define S42 10
   6.316 +#define S43 15
   6.317 +#define S44 21
   6.318 +  II(a, b, c, d, in[0], S41, 4096336452u);	/* 49 */
   6.319 +  II(d, a, b, c, in[7], S42, 1126891415u);	/* 50 */
   6.320 +  II(c, d, a, b, in[14], S43, 2878612391u);	/* 51 */
   6.321 +  II(b, c, d, a, in[5], S44, 4237533241u);	/* 52 */
   6.322 +  II(a, b, c, d, in[12], S41, 1700485571u);	/* 53 */
   6.323 +  II(d, a, b, c, in[3], S42, 2399980690u);	/* 54 */
   6.324 +  II(c, d, a, b, in[10], S43, 4293915773u);	/* 55 */
   6.325 +  II(b, c, d, a, in[1], S44, 2240044497u);	/* 56 */
   6.326 +  II(a, b, c, d, in[8], S41, 1873313359u);	/* 57 */
   6.327 +  II(d, a, b, c, in[15], S42, 4264355552u);	/* 58 */
   6.328 +  II(c, d, a, b, in[6], S43, 2734768916u);	/* 59 */
   6.329 +  II(b, c, d, a, in[13], S44, 1309151649u);	/* 60 */
   6.330 +  II(a, b, c, d, in[4], S41, 4149444226u);	/* 61 */
   6.331 +  II(d, a, b, c, in[11], S42, 3174756917u);	/* 62 */
   6.332 +  II(c, d, a, b, in[2], S43, 718787259u);	/* 63 */
   6.333 +  II(b, c, d, a, in[9], S44, 3951481745u);	/* 64 */
   6.334 +
   6.335 +  buf[0] += a;
   6.336 +  buf[1] += b;
   6.337 +  buf[2] += c;
   6.338 +  buf[3] += d;
   6.339 +}