| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // CRC32 implementation, compatible with OS/2 LVM data-structure checksums |
|---|
| 34 | // |
|---|
| 35 | // Author: J. van Wijk |
|---|
| 36 | // |
|---|
| 37 | // |
|---|
| 38 | |
|---|
| 39 | #include <txlib.h> // TxLib interface |
|---|
| 40 | |
|---|
| 41 | #define DFS_LVM_CRC_RANGE 256 |
|---|
| 42 | #define DFS_LVM_CRC_POLY 0xEDB88320L |
|---|
| 43 | #define DFS_LVM_CRC_INIT 0xFFFFFFFFL |
|---|
| 44 | |
|---|
| 45 | static ULONG *crcTable = NULL; // for LVM type CRC |
|---|
| 46 | |
|---|
| 47 | // Allocate and prepare the CRC-table needed for LVM CRC calculation |
|---|
| 48 | static void TxCreateLvmCrcTable |
|---|
| 49 | ( |
|---|
| 50 | void |
|---|
| 51 | ); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /*****************************************************************************/ |
|---|
| 55 | // Allocate and prepare the CRC-table needed for LVM CRC calculation |
|---|
| 56 | /*****************************************************************************/ |
|---|
| 57 | static void TxCreateLvmCrcTable |
|---|
| 58 | ( |
|---|
| 59 | void |
|---|
| 60 | ) |
|---|
| 61 | { |
|---|
| 62 | crcTable = TxAlloc(DFS_LVM_CRC_RANGE, sizeof(ULONG)); |
|---|
| 63 | if (crcTable != NULL) |
|---|
| 64 | { |
|---|
| 65 | ULONG i; |
|---|
| 66 | ULONG j; |
|---|
| 67 | ULONG value; |
|---|
| 68 | |
|---|
| 69 | for ( i = 0; i < DFS_LVM_CRC_RANGE ; i++ ) |
|---|
| 70 | { |
|---|
| 71 | value = i; |
|---|
| 72 | |
|---|
| 73 | for ( j = 8 ; j > 0; j-- ) |
|---|
| 74 | { |
|---|
| 75 | if (value & 1) |
|---|
| 76 | { |
|---|
| 77 | value = (value >> 1) ^ DFS_LVM_CRC_POLY; |
|---|
| 78 | } |
|---|
| 79 | else |
|---|
| 80 | { |
|---|
| 81 | value >>= 1; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | crcTable[i] = value; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } // end 'TxCreateLvmCrcTable' |
|---|
| 88 | /*---------------------------------------------------------------------------*/ |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /*****************************************************************************/ |
|---|
| 92 | // Calculate 32-bit CRC value using LVM compatible algorithm and polynom |
|---|
| 93 | /*****************************************************************************/ |
|---|
| 94 | ULONG TxCalculateLvmCrc |
|---|
| 95 | ( |
|---|
| 96 | char *area, // IN data area needing CRC |
|---|
| 97 | ULONG size // IN size of the data area |
|---|
| 98 | ) |
|---|
| 99 | { |
|---|
| 100 | ULONG rc = DFS_LVM_CRC_INIT; // function return, initial |
|---|
| 101 | |
|---|
| 102 | if (crcTable == NULL) |
|---|
| 103 | { |
|---|
| 104 | TxCreateLvmCrcTable(); |
|---|
| 105 | } |
|---|
| 106 | if (crcTable != NULL) |
|---|
| 107 | { |
|---|
| 108 | char *this = area; // current byte handled |
|---|
| 109 | ULONG t1; |
|---|
| 110 | ULONG t2; // intermediate values |
|---|
| 111 | ULONG i; |
|---|
| 112 | |
|---|
| 113 | for (this = area, i = 0; i < size; i++, this++) |
|---|
| 114 | { |
|---|
| 115 | t1 = (rc >> 8) & 0xffffff; |
|---|
| 116 | t2 = crcTable[ (rc ^ (ULONG) *this) & (ULONG) 0xff]; |
|---|
| 117 | rc = t1 ^ t2; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | return (rc); |
|---|
| 121 | } // end 'TxCalculateLvmCrc' |
|---|
| 122 | /*---------------------------------------------------------------------------*/ |
|---|