Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

fxcrc.h
1 /********************************************************************************
2 * *
3 * C R C 3 2 S u p p o r t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXCRC_H
22 #define FXCRC_H
23 
24 namespace FX {
25 
26 namespace CRC32 {
27 
29 extern FXAPI const FXuint crctab[256];
30 
54 static inline FXuint CRC(FXuint crc,FXuchar byte){
56  return crctab[(crc^byte)&0xff] ^ (crc>>8);
57  }
58 
59 
61 static inline FXuint CRC(FXuint crc,FXushort x){
62  union{ FXushort s; FXuchar b[2]; } z={x};
63 #if FOX_BIGENDIAN == 1
64  crc=CRC(crc,z.b[0]);
65  crc=CRC(crc,z.b[1]);
66 #else
67  crc=CRC(crc,z.b[1]);
68  crc=CRC(crc,z.b[0]);
69 #endif
70  return crc;
71  }
72 
73 
75 static inline FXuint CRC(FXuint crc,FXuint x){
76  union{ FXuint i; FXuchar b[4]; } z={x};
77 #if FOX_BIGENDIAN == 1
78  crc=CRC(crc,z.b[0]);
79  crc=CRC(crc,z.b[1]);
80  crc=CRC(crc,z.b[2]);
81  crc=CRC(crc,z.b[3]);
82 #else
83  crc=CRC(crc,z.b[3]);
84  crc=CRC(crc,z.b[2]);
85  crc=CRC(crc,z.b[1]);
86  crc=CRC(crc,z.b[0]);
87 #endif
88  return crc;
89  }
90 
91 
93 static inline FXuint CRC(FXuint crc,FXulong x){
94  union{ FXulong l; FXuchar b[8]; } z={x};
95 #if FOX_BIGENDIAN == 1
96  crc=CRC(crc,z.b[0]);
97  crc=CRC(crc,z.b[1]);
98  crc=CRC(crc,z.b[2]);
99  crc=CRC(crc,z.b[3]);
100  crc=CRC(crc,z.b[4]);
101  crc=CRC(crc,z.b[5]);
102  crc=CRC(crc,z.b[6]);
103  crc=CRC(crc,z.b[7]);
104 #else
105  crc=CRC(crc,z.b[7]);
106  crc=CRC(crc,z.b[6]);
107  crc=CRC(crc,z.b[5]);
108  crc=CRC(crc,z.b[4]);
109  crc=CRC(crc,z.b[3]);
110  crc=CRC(crc,z.b[2]);
111  crc=CRC(crc,z.b[1]);
112  crc=CRC(crc,z.b[0]);
113 #endif
114  return crc;
115  }
116 
117 
119 static inline FXuint CRC(FXuint crc,const FXuchar *buf,FXival len){
120  const FXuchar* end=buf+len;
121  while(buf!=end){
122  crc=CRC(crc,*buf++);
123  }
124  return crc;
125  }
126 
127 }
128 
129 }
130 
131 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp