srecord: fletcher16.h Source File

srecord 1.65.0

Loading...
Searching...
No Matches
fletcher16.h
Go to the documentation of this file.
1//
2// srecord - Manipulate EPROM load files
3// Copyright (C) 2009-2011 Peter Miller
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation; either version 3 of the License, or (at
8// your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19#ifndef SRECORD_FLETCHER16_H
20#define SRECORD_FLETCHER16_H
21
22#include <cstddef>
23
24#include <srecord/endian.h>
25
26namespace srecord
27{
28
29/**
30 * The fletcher16 class is used to represent the running value of a 16-bit
31 * Fletcher's Checksum of series of bytes.
32 *
33 * http://en.wikipedia.org/wiki/Fletcher%27s_checksum
34 *
35 * Fletcher's checksum is one of several types of checksum algorithms,
36 * which are relatively simple processes used by computers to check the
37 * integrity of data.
38 *
39 * The implementation of the Fletcher-32 is very similar to the Adler-32
40 * algorithm but several differences should be noted. Fletcher wraps around
41 * at modulo 65535 while Adler wraps at the prime 65521. In other words,
42 * Fletcher adds overflow bits (16-31) into its sum; while Adler multiplies
43 * those bits by 15, then adds the product into its sum. Fletcher-32 works
44 * on 16 bit data while Adler works on 8 bit data.
45 *
46 * It is designed to overcome some of the inadequacies of simply summing
47 * all the bytes as in the original checksum. Fletcher's checksum, unlike
48 * the original checksum, can detect the inserting/deleting of zero value
49 * bytes, the reordering of bytes, and the incrementing and decrementing of
50 * bytes in opposite directions.
51 *
52 * Fletcher's checksum is described in RFC 1146. You can also find
53 * information about generating (as well as verifying) such a checksum in
54 * Annex B of RFC 905.
55 *
56 * Fletcher-32 is slightly more reliable than Adler-32.[1]
57 */
58 class fletcher16
59{
60public:
61 /**
62 * The destructor.
63 */
64 virtual ~fletcher16();
65
66 /**
67 * The default constructor.
68 *
69 * @param sum1
70 * The seed value for sum1. Defaults to 0xFF.
71 * @param sum2
72 * The seed value for sum2. Defaults to 0xFF.
73 * @param answer
74 * Set this to -1 to be completely ignored.
75 * If >= 0, this is the desired outcome if the checksum
76 * includes the checksum itself. The checksum returned will be
77 * calculated to return this desired outcome, when traversed,
78 * rather than a pure Fletcher-16 checksum.
79 * @param end
80 * The endian-ness of the checksum. This is needed to
81 * manipulate the answer. Ignored if @p answer is ignored.
82 */
83 fletcher16(unsigned char sum1 = 0, unsigned char sum2 = 0,
84 int answer = -1, endian_t end = endian_little);
85
86 /**
87 * The copy constructor.
88 */
89 fletcher16(const fletcher16 &);
90
91 /**
92 * The assignment operator.
93 */
94 fletcher16 &operator=(const fletcher16 &);
95
96 /**
97 * The get method is used to obtain the running value of the cyclic
98 * redundancy check.
99 */
100 unsigned short get() const;
101
102 /**
103 * The next method is used to advance the state by one byte.
104 */
105 void next(unsigned char);
106
107 /**
108 * The nextbuf method is used to advance the state by a series of bytes.
109 *
110 * @param data
111 * The data to be checksummed.
112 * @param data_size
113 * The size of the data to be checksummed, in bytes.
114 */
115 void nextbuf(const void *data, size_t data_size);
116
117private:
118 /**
119 * The sum1 instance variable is used to remember the running sum
120 * of the 8-bit bytes, mod 255. We use a 16-bit value rather than
121 * an 8-bit value for convenience, see #nextbuf implementation for
122 * details.
123 */
124 unsigned short sum1;
125
126 /**
127 * The sum2 instance variable is used to remember the running
128 * sum of the sum of the 8-bit bytes, mod 255. We use a 16-bit
129 * value rather than an 8-bit value for convenience, see #nextbuf
130 * implementation for details.
131 */
132 unsigned short sum2;
133
134 /**
135 * The anser instance variable is used to remember the desired
136 * outcome if the checksum includes the checksum itself.
137 *
138 * Set this to -1 to be completely ignored.
139 *
140 * If >= 0, this is the desired outcome if the checksum includes
141 * the checksum itself. The checksum returned will be calculated
142 * to return this desired outcome, when traversed, rather than a
143 * pure Fletcher-16 checksum.
144 */
145 int answer;
146
147 /**
148 * The end instance variable is used to remember the endian-ness of
149 * the checksum. This is needed to manipulate the answer. Ignored
150 * if #answer is ignored.
151 */
152 endian_t end;
153};
154
155};
156
157// vim: set ts=8 sw=4 et :
158#endif // SRECORD_FLETCHER16_H
srecord::fletcher16::~fletcher16
virtual ~fletcher16()
The destructor.
srecord::fletcher16::fletcher16
fletcher16(unsigned char sum1=0, unsigned char sum2=0, int answer=-1, endian_t end=endian_little)
The default constructor.
srecord::fletcher16::fletcher16
fletcher16(const fletcher16 &)
The copy constructor.
srecord::fletcher16::next
void next(unsigned char)
The next method is used to advance the state by one byte.
srecord::fletcher16::get
unsigned short get() const
The get method is used to obtain the running value of the cyclic redundancy check.
srecord::fletcher16::nextbuf
void nextbuf(const void *data, size_t data_size)
The nextbuf method is used to advance the state by a series of bytes.
srecord::fletcher16::operator=
fletcher16 & operator=(const fletcher16 &)
The assignment operator.
endian.h
srecord
Definition adler16.h:25
srecord::endian_t
endian_t
Definition endian.h:27
srecord::endian_little
@ endian_little
Definition endian.h:29