mirror of
https://github.com/oatpp/oatpp.git
synced 2024-11-21 03:14:51 +08:00
algorithm CRC32 added
This commit is contained in:
parent
3fd6a2cf0c
commit
ca16919063
77
algorithm/CRC32.cpp
Normal file
77
algorithm/CRC32.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#include "CRC32.hpp"
|
||||
|
||||
namespace oatpp { namespace algorithm {
|
||||
|
||||
const p_word32 CRC32::TABLE_04C11DB7 = generateTable(0x04C11DB7);
|
||||
|
||||
v_word32 CRC32::bitReverse(v_word32 poly) {
|
||||
v_word32 result = 0;
|
||||
for(v_int32 i = 0; i < 32; i ++) {
|
||||
if((poly & (1 << i)) > 0) {
|
||||
result |= 1 << (31 - i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
p_word32 CRC32::generateTable(v_word32 poly) {
|
||||
|
||||
p_word32 result = new v_word32[256];
|
||||
v_word32 polyReverse = bitReverse(poly);
|
||||
v_word32 value;
|
||||
|
||||
for(v_int32 i = 0; i < 256; i++) {
|
||||
value = i;
|
||||
for (v_int32 bit = 0; bit < 8; bit++) {
|
||||
if (value & 1) {
|
||||
value = (value >> 1) ^ polyReverse;
|
||||
} else {
|
||||
value = (value >> 1);
|
||||
}
|
||||
}
|
||||
|
||||
result[i] = value;
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
v_word32 CRC32::calc(const void *buffer, v_int32 size, v_word32 crc, v_word32 initValue, v_word32 xorOut, p_word32 table) {
|
||||
|
||||
p_word8 data = (p_word8) buffer;
|
||||
crc = crc ^ initValue;
|
||||
|
||||
for(v_int32 i = 0; i < size; i++) {
|
||||
crc = table[(crc & 0xFF) ^ data[i]] ^ (crc >> 8);
|
||||
}
|
||||
|
||||
return crc ^ xorOut;
|
||||
}
|
||||
|
||||
}}
|
52
algorithm/CRC32.hpp
Normal file
52
algorithm/CRC32.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_algorithm_CRC32_hpp
|
||||
#define oatpp_algorithm_CRC32_hpp
|
||||
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
|
||||
#include "oatpp/encoding/Hex.hpp"
|
||||
|
||||
namespace oatpp { namespace algorithm {
|
||||
|
||||
class CRC32 {
|
||||
public:
|
||||
static const p_word32 TABLE_04C11DB7;
|
||||
public:
|
||||
|
||||
static v_word32 bitReverse(v_word32 poly);
|
||||
|
||||
/**
|
||||
* Generates v_word32 table[256] for polynomial
|
||||
*/
|
||||
static p_word32 generateTable(v_word32 poly);
|
||||
|
||||
static v_word32 calc(const void *buffer, v_int32 size, v_word32 crc = 0, v_word32 initValue = 0xFFFFFFFF, v_word32 xorOut = 0xFFFFFFFF, p_word32 table = TABLE_04C11DB7);
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif /* oatpp_algorithm_CRC32_hpp */
|
Loading…
Reference in New Issue
Block a user