1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* BIRD Library -- Fletcher-16 Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "test/birdtest.h"
#include "lib/fletcher16.h"
static u16
straightforward_fletcher16_compute(const char *data)
{
int count = strlen(data);
u16 sum1 = 0;
u16 sum2 = 0;
int index;
for (index = 0; index < count; ++index)
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
static u16
straightforward_fletcher16_checksum(const char *data)
{
u16 csum;
u8 c0,c1,f0,f1;
csum = straightforward_fletcher16_compute(data);
f0 = csum & 0xff;
f1 = (csum >> 8) & 0xff;
c0 = 0xff - ((f0 + f1) % 0xff);
c1 = 0xff - ((f0 + c0) % 0xff);
return (c1 << 8) | c0;
}
static int
test_fletcher16(void *out_, const void *in_, const void *expected_out_)
{
u16 *out = out_;
const char *in = in_;
const u16 *expected_out = expected_out_;
struct fletcher16_context ctxt;
fletcher16_init(&ctxt);
fletcher16_update(&ctxt, in, strlen(in));
put_u16(out, fletcher16_compute(&ctxt));
return *out == *expected_out;
}
static int
test_fletcher16_checksum(void *out_, const void *in_, const void *expected_out_)
{
u16 *out = out_;
const char *in = in_;
const u16 *expected_out = expected_out_;
struct fletcher16_context ctxt;
int len = strlen(in);
fletcher16_init(&ctxt);
fletcher16_update(&ctxt, in, len);
put_u16(out, fletcher16_final(&ctxt, len, len));
return *out == *expected_out;
}
static int
t_fletcher16_compute(void)
{
struct bt_pair test_vectors[] = {
{
.in = "\001\002",
.out = & (const u16) { 0x0403 },
},
{
.in = "",
.out = & ((const u16) { straightforward_fletcher16_compute("") }),
},
{
.in = "a",
.out = & ((const u16) { straightforward_fletcher16_compute("a") }),
},
{
.in = "abcd",
.out = & ((const u16) { straightforward_fletcher16_compute("abcd") }),
},
{
.in = "message digest",
.out = & ((const u16) { straightforward_fletcher16_compute("message digest") }),
},
{
.in = "abcdefghijklmnopqrstuvwxyz",
.out = & ((const u16) { straightforward_fletcher16_compute("abcdefghijklmnopqrstuvwxyz") }),
},
{
.in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
.out = & ((const u16) { straightforward_fletcher16_compute("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
},
{
.in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
.out = & ((const u16) { straightforward_fletcher16_compute("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
},
};
return bt_assert_batch(test_vectors, test_fletcher16, bt_fmt_str, bt_fmt_unsigned);
}
static int
t_fletcher16_checksum(void)
{
struct bt_pair test_vectors[] = {
{
.in = "\001\002",
.out = & ((const u16) { straightforward_fletcher16_checksum("\001\002") }),
},
{
.in = "",
.out = & ((const u16) { straightforward_fletcher16_checksum("") }),
},
{
.in = "a",
.out = & ((const u16) { straightforward_fletcher16_checksum("a") }),
},
{
.in = "abcd",
.out = & ((const u16) { straightforward_fletcher16_checksum("abcd") }),
},
{
.in = "message digest",
.out = & ((const u16) { straightforward_fletcher16_checksum("message digest") }),
},
{
.in = "abcdefghijklmnopqrstuvwxyz",
.out = & ((const u16) { straightforward_fletcher16_checksum("abcdefghijklmnopqrstuvwxyz") }),
},
{
.in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
.out = & ((const u16) { straightforward_fletcher16_checksum("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
},
{
.in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
.out = & ((const u16) { straightforward_fletcher16_checksum("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
},
};
return bt_assert_batch(test_vectors, test_fletcher16_checksum, bt_fmt_str, bt_fmt_unsigned);
}
int
main(int argc, char *argv[])
{
bt_init(argc, argv);
bt_test_suite(t_fletcher16_compute, "Fletcher-16 Compute Tests");
bt_test_suite(t_fletcher16_checksum, "Fletcher-16 Checksum Tests");
return bt_exit_value();
}
|