-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc529.h
71 lines (59 loc) · 1.48 KB
/
crc529.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// (C) Copyright 2009-2011 Vit Kasal
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <boost/type_traits.hpp>
#include <boost/cstdint.hpp>
namespace si
{
// unsigned int crc(unsigned int uiCount,unsigned char *pucDat);
BOOST_STATIC_CONSTANT( boost::uint16_t, POLYNOM=0x8005);
template<typename iterator> boost::uint16_t crc(std::size_t uiCount,iterator it)
{
short int iTmp;
unsigned short int uiTmp,uiTmp1,uiVal;
iterator pucTmpDat;
if (uiCount < 2) return(0); // response value is "0" for none or one data boost::uint8_t
pucTmpDat = it;
uiTmp1 = *pucTmpDat++;
uiTmp1 = (uiTmp1<<8) + *pucTmpDat++;
if (uiCount == 2) return(uiTmp1); // response value is CRC for two data boost::uint8_ts
for (iTmp=(int)(uiCount>>1);iTmp>0;iTmp--)
{
if (iTmp>1)
{
uiVal = *pucTmpDat++;
uiVal= (uiVal<<8) + *pucTmpDat++;
}
else
{
if (uiCount&1) // odd number of data boost::uint8_ts, complete with "0"
{
uiVal = *pucTmpDat;
uiVal= (uiVal<<8);
}
else
{
uiVal=0; //letzte Werte mit 0
}
}
for (uiTmp=0;uiTmp<16;uiTmp++)
{
if (uiTmp1 & 0x8000)
{
uiTmp1 <<= 1;
if (uiVal & 0x8000)uiTmp1++;
uiTmp1 ^= POLYNOM;
}
else
{
uiTmp1 <<= 1;
if (uiVal & 0x8000)uiTmp1++;
}
uiVal <<= 1;
}
}
return(uiTmp1);
}
}//namespace si