Embedded Template Library 1.0
Loading...
Searching...
No Matches
checksum.h
Go to the documentation of this file.
1
3
4/******************************************************************************
5The MIT License(MIT)
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9Copyright(c) 2014 John Wellbelove
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25******************************************************************************/
26
27#ifndef ETL_CHECKSUM_INCLUDED
28#define ETL_CHECKSUM_INCLUDED
29
30#include "platform.h"
31#include "binary.h"
33
34#include <stdint.h>
35
38
39namespace etl
40{
41 //***************************************************************************
43 //***************************************************************************
44 template <typename T>
46 {
47 typedef T value_type;
48
49 T initial() const
50 {
51 return 0;
52 }
53
54 T add(T sum, uint8_t value) const
55 {
56 return sum + value;
57 }
58
59 T final(T sum) const
60 {
61 return sum;
62 }
63 };
64
65 //***************************************************************************
67 //***************************************************************************
68 template <typename T>
70 {
71 typedef T value_type;
72
73 T initial() const
74 {
75 return 0;
76 }
77
78 T add(T sum, uint8_t value) const
79 {
80 return etl::rotate_right(sum) + value;
81 }
82
83 T final(T sum) const
84 {
85 return sum;
86 }
87 };
88
89 //***************************************************************************
91 //***************************************************************************
92 template <typename T>
94 {
95 typedef T value_type;
96
97 T initial() const
98 {
99 return 0;
100 }
101
102 T add(T sum, uint8_t value) const
103 {
104 return sum ^ value;
105 }
106
107 T final(T sum) const
108 {
109 return sum;
110 }
111 };
112
113 //***************************************************************************
115 //***************************************************************************
116 template <typename T>
118 {
119 typedef T value_type;
120
121 T initial() const
122 {
123 return 0;
124 }
125
126 T add(T sum, uint8_t value) const
127 {
128 return etl::rotate_left(sum) ^ value;
129 }
130
131 T final(T sum) const
132 {
133 return sum;
134 }
135 };
136
137 //***************************************************************************
139 //***************************************************************************
140 template <typename T>
142 {
143 typedef T value_type;
144
145 T initial() const
146 {
147 return 0;
148 }
149
150 T add(T sum, uint8_t value) const
151 {
152 return sum ^ etl::parity(value);
153 }
154
155 T final(T sum) const
156 {
157 return sum;
158 }
159 };
160
161 //*************************************************************************
163 //*************************************************************************
164 template <typename T>
165 class checksum : public etl::frame_check_sequence<etl::checksum_policy_sum<T> >
166 {
167 public:
168
169 //*************************************************************************
171 //*************************************************************************
173 {
174 this->reset();
175 }
176
177 //*************************************************************************
181 //*************************************************************************
182 template<typename TIterator>
184 {
185 this->reset();
186 this->add(begin, end);
187 }
188 };
189
190 //*************************************************************************
192 //*************************************************************************
193 template <typename T>
194 class bsd_checksum : public etl::frame_check_sequence<etl::checksum_policy_bsd<T> >
195 {
196 public:
197
198 //*************************************************************************
200 //*************************************************************************
202 {
203 this->reset();
204 }
205
206 //*************************************************************************
210 //*************************************************************************
211 template<typename TIterator>
213 {
214 this->reset();
215 this->add(begin, end);
216 }
217 };
218
219 //*************************************************************************
221 //*************************************************************************
222 template <typename T>
223 class xor_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor<T> >
224 {
225 public:
226
227 //*************************************************************************
229 //*************************************************************************
231 {
232 this->reset();
233 }
234
235 //*************************************************************************
239 //*************************************************************************
240 template<typename TIterator>
242 {
243 this->reset();
244 this->add(begin, end);
245 }
246 };
247
248 //*************************************************************************
250 //*************************************************************************
251 template <typename T>
252 class xor_rotate_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor_rotate<T> >
253 {
254 public:
255
256 //*************************************************************************
258 //*************************************************************************
260 {
261 this->reset();
262 }
263
264 //*************************************************************************
268 //*************************************************************************
269 template<typename TIterator>
271 {
272 this->reset();
273 this->add(begin, end);
274 }
275 };
276
277 //*************************************************************************
279 //*************************************************************************
280 template <typename T>
281 class parity_checksum : public etl::frame_check_sequence<etl::checksum_policy_parity<T> >
282 {
283 public:
284
285 //*************************************************************************
287 //*************************************************************************
289 {
290 this->reset();
291 }
292
293 //*************************************************************************
297 //*************************************************************************
298 template<typename TIterator>
300 {
301 this->reset();
302 this->add(begin, end);
303 }
304 };
305}
306
307#endif
BSD Checksum.
Definition checksum.h:195
bsd_checksum()
Default constructor.
Definition checksum.h:201
bsd_checksum(TIterator begin, const TIterator end)
Definition checksum.h:212
Standard Checksum.
Definition checksum.h:166
checksum()
Default constructor.
Definition checksum.h:172
checksum(TIterator begin, const TIterator end)
Definition checksum.h:183
Parity Checksum.
Definition checksum.h:282
parity_checksum()
Default constructor.
Definition checksum.h:288
parity_checksum(TIterator begin, const TIterator end)
Definition checksum.h:299
XOR Checksum.
Definition checksum.h:224
xor_checksum(TIterator begin, const TIterator end)
Definition checksum.h:241
xor_checksum()
Default constructor.
Definition checksum.h:230
XOR-shift Checksum.
Definition checksum.h:253
xor_rotate_checksum()
Default constructor.
Definition checksum.h:259
xor_rotate_checksum(TIterator begin, const TIterator end)
Definition checksum.h:270
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type parity(T value)
Definition binary.h:1026
ETL_CONSTEXPR14 T rotate_left(T value)
Definition binary.h:116
ETL_CONSTEXPR14 T rotate_right(T value)
Definition binary.h:161
ETL_CONSTEXPR14 void add(TIterator begin, const TIterator end)
Definition frame_check_sequence.h:144
ETL_CONSTEXPR14 void reset()
Resets the FCS to the initial state.
Definition frame_check_sequence.h:133
Definition frame_check_sequence.h:98
bitset_ext
Definition absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition iterator.h:962
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition iterator.h:992
BSD checksum policy.
Definition checksum.h:70
Parity checksum policy.
Definition checksum.h:142
Standard addition checksum policy.
Definition checksum.h:46
XOR-rotate checksum policy.
Definition checksum.h:118
Standard XOR checksum policy.
Definition checksum.h:94
pair holds two objects of arbitrary type
Definition utility.h:164