Embedded Template Library  1.0
checksum.h
Go to the documentation of this file.
1 
3 
4 /******************************************************************************
5 The MIT License(MIT)
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 Copyright(c) 2014 jwellbelove
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 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 ******************************************************************************/
26 
27 #ifndef ETL_CHECKSUM_INCLUDED
28 #define ETL_CHECKSUM_INCLUDED
29 
30 #include <stdint.h>
31 
32 #include "platform.h"
33 #include "binary.h"
34 #include "frame_check_sequence.h"
35 
38 
39 namespace etl
40 {
41  //***************************************************************************
43  //***************************************************************************
44  template <typename T>
46  {
47  typedef T value_type;
48 
49  inline T initial() const
50  {
51  return 0;
52  }
53 
54  inline T add(T sum, uint8_t value) const
55  {
56  return sum + value;
57  }
58 
59  inline 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  inline T initial() const
74  {
75  return 0;
76  }
77 
78  inline T add(T sum, uint8_t value) const
79  {
80  return etl::rotate_right(sum) + value;
81  }
82 
83  inline 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  inline T initial() const
98  {
99  return 0;
100  }
101 
102  inline T add(T sum, uint8_t value) const
103  {
104  return sum ^ value;
105  }
106 
107  inline 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  inline T initial() const
122  {
123  return 0;
124  }
125 
126  inline T add(T sum, uint8_t value) const
127  {
128  return etl::rotate_left(sum) ^ value;
129  }
130 
131  inline 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  inline T initial() const
146  {
147  return 0;
148  }
149 
150  inline T add(T sum, uint8_t value) const
151  {
152  return sum ^ etl::parity(value);
153  }
154 
155  inline 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>
183  checksum(TIterator begin, const TIterator end)
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>
212  bsd_checksum(TIterator begin, const TIterator end)
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>
241  xor_checksum(TIterator begin, const TIterator end)
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>
270  xor_rotate_checksum(TIterator begin, const TIterator end)
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>
299  parity_checksum(TIterator begin, const TIterator end)
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 T rotate_left(T value)
Definition: binary.h:115
ETL_CONSTEXPR14 uint_least8_t parity(uint16_t value)
Definition: binary.h:822
ETL_CONSTEXPR14 T rotate_right(T value)
Definition: binary.h:145
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: container.h:49
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: container.h:99
void reset()
Resets the FCS to the initial state.
Definition: frame_check_sequence.h:135
void add(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:146
Definition: frame_check_sequence.h:101
Definition: absolute.h:37
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