|
|
1.1 root 1: /*
2: * Hatari - utils.c
3: *
4: * This file is distributed under the GNU Public License, version 2 or at
5: * your option any later version. Read the file gpl.txt for details.
6: *
7: * Utils functions :
8: * - CRC32
9: *
10: * This file contains various utility functions used by different parts of Hatari.
11: */
1.1.1.3 root 12: const char Utils_fileid[] = "Hatari utils.c : " __DATE__ " " __TIME__;
1.1 root 13:
14: #include "utils.h"
15:
16:
17: /************************************************************************/
18: /* Functions used to compute the CRC32 of a stream of bytes. */
19: /* These functions require a pointer to an unsigned int (Uint32) to */
20: /* store the resulting CRC. */
21: /* crc32_reset : call this once to reset the CRC, before adding */
22: /* some bytes. */
23: /* crc32_add_byte : update the current CRC with a new byte. */
24: /************************************************************************/
25:
26: /*--------------------------------------------------------------*/
27: /* Reset the crc32 value. This should be done before calling */
28: /* crc32_add_byte(). */
29: /*--------------------------------------------------------------*/
30:
31: void crc32_reset ( Uint32 *crc )
32: {
33: *crc = 0xffffffff;
34: }
35:
36:
37: /*--------------------------------------------------------------*/
38: /* Update the current value of crc with a new byte. */
39: /* Call crc32_reset() first to init the crc value. */
40: /*--------------------------------------------------------------*/
41:
42: void crc32_add_byte ( Uint32 *crc , Uint8 c )
43: {
44: int bit;
45:
46: for ( bit=0 ; bit<8; bit++ )
47: {
48: if ( ( c & 0x80 ) ^ ( *crc & 0x80000000 ) )
49: *crc = ( *crc << 1 ) ^ CRC32_POLY;
50:
51: else
52: *crc = *crc << 1;
53:
54: c <<= 1;
55: }
56: }
57:
58:
59: /************************************************************************/
1.1.1.4 ! root 60: /* Functions used to compute the CRC16 of a stream of bytes. */
! 61: /* These functions require a pointer to an unsigned int (Uint16) to */
! 62: /* store the resulting CRC. */
! 63: /* crc16_reset : call this once to reset the CRC, before adding */
! 64: /* some bytes. */
! 65: /* crc16_add_byte : update the current CRC with a new byte. */
! 66: /************************************************************************/
1.1 root 67:
1.1.1.4 ! root 68: /*--------------------------------------------------------------*/
! 69: /* Reset the crc16 value. This should be done before calling */
! 70: /* crc16_add_byte(). */
! 71: /*--------------------------------------------------------------*/
! 72:
! 73: void crc16_reset ( Uint16 *crc )
! 74: {
! 75: *crc = 0xffff;
! 76: }
! 77:
! 78:
! 79: /*--------------------------------------------------------------*/
! 80: /* Update the current value of crc with a new byte. */
! 81: /* Call crc16_reset() first to init the crc value. */
! 82: /*--------------------------------------------------------------*/
! 83:
! 84: void crc16_add_byte ( Uint16 *crc , Uint8 c )
! 85: {
! 86: int bit;
! 87:
! 88: *crc ^= ( c << 8 );
! 89: for ( bit=0 ; bit<8; bit++ )
! 90: {
! 91: if ( *crc & 0x8000 )
! 92: *crc = ( *crc << 1 ) ^ CRC16_POLY;
! 93:
! 94: else
! 95: *crc = *crc << 1;
! 96:
! 97: c <<= 1;
! 98: }
! 99: }
1.1 root 100:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.