-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_crc.cc
95 lines (73 loc) · 2.36 KB
/
lib_crc.cc
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
//------------------------------------------------------------------------
// CRC : Cyclic Rendundancy Check
//------------------------------------------------------------------------
//
// OBSIDIAN Level Maker
//
// Copyright (C) 2021-2022 The OBSIDIAN Team
// Copyright (C) 2006-2017 Andrew Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
//
// This is the Adler-32 algorithm as described in RFC-1950.
//
//------------------------------------------------------------------------
#include "lib_crc.h"
#include "headers.h"
// ---- Primitive routines ----
crc32_c &crc32_c::operator+=(u8_t data) {
u32_t s1 = raw & 0xFFFF;
u32_t s2 = (raw >> 16) & 0xFFFF;
s1 = (s1 + data) % 65521;
s2 = (s2 + s1) % 65521;
raw = (s2 << 16) | s1;
return *this;
}
crc32_c &crc32_c::AddBlock(const u8_t *data, int len) {
u32_t s1 = raw & 0xFFFF;
u32_t s2 = (raw >> 16) & 0xFFFF;
for (; len > 0; data++, len--) {
s1 = (s1 + *data) % 65521;
s2 = (s2 + s1) % 65521;
}
raw = (s2 << 16) | s1;
return *this;
}
// ---- non-primitive routines ----
crc32_c &crc32_c::operator+=(u16_t value) {
*this += (u8_t)(value >> 8);
*this += (u8_t)(value);
return *this;
}
crc32_c &crc32_c::operator+=(u32_t value) {
*this += (u8_t)(value >> 24);
*this += (u8_t)(value >> 16);
*this += (u8_t)(value >> 8);
*this += (u8_t)(value);
return *this;
}
crc32_c &crc32_c::operator+=(float value) {
bool neg = (value < 0.0f);
value = (float)fabs(value);
int exp;
u32_t mant = (u32_t)(ldexp(frexp(value, &exp), 30));
*this += (u8_t)(neg ? '-' : '+');
*this += (u32_t)exp;
*this += mant;
return *this;
}
crc32_c &crc32_c::AddCStr(const char *str) {
return AddBlock((const u8_t *)str, (int)strlen(str));
}
//--- editor settings ---
// vi:ts=4:sw=4:noexpandtab