Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
crc16.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_CRC16__
30 #define __ETL_CRC16__
31 
32 #include <stdint.h>
33 
34 #include "static_assert.h"
35 #include "type_traits.h"
36 #include "endian.h"
37 
40 
41 namespace etl
42 {
43  //***************************************************************************
46  //***************************************************************************
47  extern const uint16_t CRC16[];
48 
49  //***************************************************************************
53  //***************************************************************************
54  template <const int ENDIANNESS = endian::little>
55  class crc16
56  {
57  public:
58 
59  typedef uint16_t value_type;
60 
61  //*************************************************************************
63  //*************************************************************************
65  {
66  reset();
67  }
68 
69  //*************************************************************************
73  //*************************************************************************
74  template<typename TIterator>
75  crc16(TIterator begin, const TIterator end)
76  {
77  reset();
78  add(begin, end);
79  }
80 
81  //*************************************************************************
83  //*************************************************************************
84  void reset()
85  {
86  crc = 0;
87  }
88 
89  //*************************************************************************
91  //*************************************************************************
92  template<typename TValue>
93  void add(TValue value)
94  {
95  STATIC_ASSERT(is_integral<TValue>::value, "Non-integral parameter");
96 
97  if (ENDIANNESS == endian::little)
98  {
99  for (int i = 0; i < sizeof(TValue); ++i)
100  {
101  add(uint8_t((value >> (i * 8)) & 0xFF));
102  }
103  }
104  else
105  {
106  for (int i = sizeof(TValue) - 1; i >= 0; --i)
107  {
108  add(uint8_t((value >> (i * 8)) & 0xFF));
109  }
110  }
111  }
112 
113  //*************************************************************************
116  //*************************************************************************
117  void add(uint8_t value)
118  {
119  crc = (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF];
120  }
121 
122  //*************************************************************************
125  //*************************************************************************
126  template<typename TIterator>
127  void add(TIterator begin, const TIterator end)
128  {
129  while (begin != end)
130  {
131  add(*begin);
132  ++begin;
133  }
134  }
135 
136  //*************************************************************************
138  //*************************************************************************
139  value_type value() const
140  {
141  return crc;
142  }
143 
144  //*************************************************************************
146  //*************************************************************************
147  template<typename TValue>
149  {
150  add(value);
151 
152  return *this;
153  }
154 
155  //*************************************************************************
157  //*************************************************************************
158  operator value_type () const
159  {
160  return crc;
161  }
162 
163  private:
164 
165  value_type crc;
166  };
167 }
168 
169 #endif
crc16< ENDIANNESS > & operator+=(TValue value)
Definition: crc16.h:148
void add(uint8_t value)
Definition: crc16.h:117
void add(TValue value)
Definition: crc16.h:93
Definition: type_traits.h:130
const uint16_t CRC16[]
Definition: crc16.h:47
crc16()
Default constructor.
Definition: crc16.h:64
Definition: algorithm.h:43
void add(TIterator begin, const TIterator end)
Definition: crc16.h:127
crc16(TIterator begin, const TIterator end)
Definition: crc16.h:75
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45
void reset()
Resets the CRC to the initial state.
Definition: crc16.h:84
value_type value() const
Gets the CRC value.
Definition: crc16.h:139
Definition: crc16.h:55