include/SDL_test_crc32.h
author Ryan C. Gordon <icculus@icculus.org>
Thu, 28 May 2015 01:54:52 -0400
changeset 9671 0e1f57b051f4
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Windows GetBasePath should use GetModuleFileNameExW() and check for overflows.

Apparently you might get strange paths from GetModuleFileName(), such as
short path names or UNC filenames, so this avoids that problem. Since you have
to tapdance with linking different libraries and defining macros depending on
what Windows you plan to target, we dynamically load the API we need, which
works on all versions of Windows (on Win7, it'll load a compatibility wrapper
for the newer API location).

What a mess.

This also now does the right thing if there isn't enough space to store the
path, looping with a larger allocated buffer each try.

Fixes Bugzilla #2435.
aschiffler@6713
     1
/*
aschiffler@6713
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
aschiffler@6713
     4
aschiffler@6713
     5
  This software is provided 'as-is', without any express or implied
aschiffler@6713
     6
  warranty.  In no event will the authors be held liable for any damages
aschiffler@6713
     7
  arising from the use of this software.
aschiffler@6713
     8
aschiffler@6713
     9
  Permission is granted to anyone to use this software for any purpose,
aschiffler@6713
    10
  including commercial applications, and to alter it and redistribute it
aschiffler@6713
    11
  freely, subject to the following restrictions:
aschiffler@6713
    12
aschiffler@6713
    13
  1. The origin of this software must not be misrepresented; you must not
aschiffler@6713
    14
     claim that you wrote the original software. If you use this software
aschiffler@6713
    15
     in a product, an acknowledgment in the product documentation would be
aschiffler@6713
    16
     appreciated but is not required.
aschiffler@6713
    17
  2. Altered source versions must be plainly marked as such, and must not be
aschiffler@6713
    18
     misrepresented as being the original software.
aschiffler@6713
    19
  3. This notice may not be removed or altered from any source distribution.
aschiffler@6713
    20
*/
aschiffler@6713
    21
aschiffler@6713
    22
/**
aschiffler@6713
    23
 *  \file SDL_test_crc32.h
slouken@7191
    24
 *
aschiffler@6713
    25
 *  Include file for SDL test framework.
aschiffler@6713
    26
 *
aschiffler@6713
    27
 *  This code is a part of the SDL2_test library, not the main SDL library.
aschiffler@6713
    28
 */
aschiffler@6713
    29
slouken@7191
    30
/*
aschiffler@6713
    31
aschiffler@6713
    32
 Implements CRC32 calculations (default output is Perl String::CRC32 compatible).
slouken@7191
    33
aschiffler@6713
    34
*/
aschiffler@6713
    35
aschiffler@6713
    36
#ifndef _SDL_test_crc32_h
aschiffler@6713
    37
#define _SDL_test_crc32_h
aschiffler@6713
    38
aschiffler@6713
    39
#include "begin_code.h"
aschiffler@6713
    40
/* Set up for C function definitions, even when using C++ */
aschiffler@6713
    41
#ifdef __cplusplus
aschiffler@6713
    42
extern "C" {
aschiffler@6713
    43
#endif
aschiffler@6713
    44
aschiffler@6713
    45
aschiffler@6713
    46
/* ------------ Definitions --------- */
aschiffler@6713
    47
aschiffler@6713
    48
/* Definition shared by all CRC routines */
aschiffler@6713
    49
aschiffler@6713
    50
#ifndef CrcUint32
slouken@7191
    51
 #define CrcUint32  unsigned int
aschiffler@6713
    52
#endif
aschiffler@6713
    53
#ifndef CrcUint8
slouken@7191
    54
 #define CrcUint8   unsigned char
aschiffler@6713
    55
#endif
aschiffler@6713
    56
aschiffler@6713
    57
#ifdef ORIGINAL_METHOD
slouken@7191
    58
 #define CRC32_POLY 0x04c11db7   /* AUTODIN II, Ethernet, & FDDI */
aschiffler@6713
    59
#else
aschiffler@6713
    60
 #define CRC32_POLY 0xEDB88320   /* Perl String::CRC32 compatible */
aschiffler@6713
    61
#endif
aschiffler@6713
    62
slouken@7191
    63
/**
slouken@7191
    64
 * Data structure for CRC32 (checksum) computation
aschiffler@6713
    65
 */
aschiffler@6713
    66
  typedef struct {
aschiffler@6713
    67
    CrcUint32    crc32_table[256]; /* CRC table */
aschiffler@6713
    68
  } SDLTest_Crc32Context;
aschiffler@6713
    69
aschiffler@6713
    70
/* ---------- Function Prototypes ------------- */
aschiffler@6713
    71
slouken@7191
    72
/**
philipp@9233
    73
 * \brief Initialize the CRC context
aschiffler@6713
    74
 *
aschiffler@6713
    75
 * Note: The function initializes the crc table required for all crc calculations.
aschiffler@6713
    76
 *
philipp@9233
    77
 * \param crcContext        pointer to context variable
aschiffler@6713
    78
 *
philipp@9233
    79
 * \returns 0 for OK, -1 on error
aschiffler@6713
    80
 *
aschiffler@6713
    81
 */
aschiffler@6713
    82
 int SDLTest_Crc32Init(SDLTest_Crc32Context * crcContext);
aschiffler@6713
    83
aschiffler@6713
    84
aschiffler@6713
    85
/**
philipp@9233
    86
 * \brief calculate a crc32 from a data block
slouken@7191
    87
 *
philipp@9233
    88
 * \param crcContext         pointer to context variable
philipp@9233
    89
 * \param inBuf              input buffer to checksum
philipp@9233
    90
 * \param inLen              length of input buffer
philipp@9233
    91
 * \param crc32              pointer to Uint32 to store the final CRC into
aschiffler@6713
    92
 *
philipp@9233
    93
 * \returns 0 for OK, -1 on error
aschiffler@6713
    94
 *
aschiffler@6713
    95
 */
aschiffler@6713
    96
int SDLTest_crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32);
aschiffler@6713
    97
aschiffler@6713
    98
/* Same routine broken down into three steps */
aschiffler@6713
    99
int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32);
aschiffler@6713
   100
int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32);
aschiffler@6713
   101
int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32);
aschiffler@6713
   102
aschiffler@6713
   103
aschiffler@6713
   104
/**
philipp@9233
   105
 * \brief clean up CRC context
aschiffler@6713
   106
 *
philipp@9233
   107
 * \param crcContext        pointer to context variable
aschiffler@6713
   108
 *
philipp@9233
   109
 * \returns 0 for OK, -1 on error
aschiffler@6713
   110
 *
aschiffler@6713
   111
*/
aschiffler@6713
   112
aschiffler@6713
   113
int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext);
aschiffler@6713
   114
aschiffler@6713
   115
aschiffler@6713
   116
/* Ends C function definitions when using C++ */
aschiffler@6713
   117
#ifdef __cplusplus
aschiffler@6713
   118
}
aschiffler@6713
   119
#endif
aschiffler@6713
   120
#include "close_code.h"
aschiffler@6713
   121
aschiffler@6713
   122
#endif /* _SDL_test_crc32_h */
aschiffler@6713
   123
aschiffler@6713
   124
/* vi: set ts=4 sw=4 expandtab: */