Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
crc64_ecma.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_CRC64_ECMA__
30 #define __ETL_CRC64_ECMA__
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 uint64_t CRC64_ECMA[];
48 
49  //***************************************************************************
53  //***************************************************************************
54  template <const int ENDIANNESS = endian::little>
55  class crc64_ecma
56  {
57  public:
58 
59  typedef uint64_t value_type;
60 
61  //*************************************************************************
63  //*************************************************************************
65  {
66  reset();
67  }
68 
69  //*************************************************************************
73  //*************************************************************************
74  template<typename TIterator>
75  crc64_ecma(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  //*************************************************************************
115  //*************************************************************************
116  void add(uint8_t value)
117  {
118  crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ value) & 0xFF];
119  }
120 
121  //*************************************************************************
124  //*************************************************************************
125  template<typename TIterator>
126  void add(TIterator begin, const TIterator end)
127  {
128  while (begin != end)
129  {
130  add(*begin);
131  ++begin;
132  }
133  }
134 
135  //*************************************************************************
137  //*************************************************************************
138  value_type value() const
139  {
140  return crc;
141  }
142 
143  //*************************************************************************
145  //*************************************************************************
146  template<typename TValue>
148  {
149  add(value);
150 
151  return *this;
152  }
153 
154  //*************************************************************************
156  //*************************************************************************
157  operator value_type () const
158  {
159  return crc;
160  }
161 
162  private:
163 
164  value_type crc;
165  };
166 }
167 
168 #endif
const uint64_t CRC64_ECMA[]
Definition: crc64_ecma.h:47
value_type value() const
Gets the CRC value.
Definition: crc64_ecma.h:138
void add(TValue value)
Definition: crc64_ecma.h:93
Definition: type_traits.h:130
void add(uint8_t value)
Definition: crc64_ecma.h:116
Definition: algorithm.h:43
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45
crc64_ecma(TIterator begin, const TIterator end)
Definition: crc64_ecma.h:75
Definition: crc64_ecma.h:55
crc64_ecma()
Default constructor.
Definition: crc64_ecma.h:64
crc64_ecma< ENDIANNESS > & operator+=(TValue value)
Definition: crc64_ecma.h:147
void add(TIterator begin, const TIterator end)
Definition: crc64_ecma.h:126
void reset()
Resets the CRC to the initial state.
Definition: crc64_ecma.h:84