Skip to content

Commit

Permalink
Add CRC64 (ECMA-182, reflected form polynomial) calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 3, 2023
1 parent 373e720 commit ac363dc
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
50 changes: 50 additions & 0 deletions util/oc_crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******************************************************************
*
* Copyright (c) 2023 plgd.dev s.r.o.
*
* 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 "util/oc_crc_internal.h"

#ifdef OC_HAS_FEATURE_CRC64

// ECMA-182, reflected form polynomial
#define CRC64_POLYNOMIAL (0xC96C5795D7870F42ULL)

static uint64_t
crc64_update(uint64_t crc, uint8_t byte)
{
crc ^= (uint64_t)byte;
for (int i = 0; i < 8; ++i) {
if (crc & 1) {
crc = (crc >> 1) ^ CRC64_POLYNOMIAL;
} else {
crc >>= 1;
}
}
return crc;
}

uint64_t
oc_crc64(const uint8_t *buffer, size_t size)
{
uint64_t crc = 0;
for (size_t i = 0; i < size; i++) {
crc = crc64_update(crc, buffer[i]);
}
return crc;
}

#endif /* OC_HAS_FEATURE_CRC64 */
49 changes: 49 additions & 0 deletions util/oc_crc_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/******************************************************************
*
* Copyright (c) 2023 plgd.dev s.r.o.
*
* 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 OC_CRC_INTERNAL_H
#define OC_CRC_INTERNAL_H

#include "util/oc_compiler.h"
#include "util/oc_features.h"
#include <stddef.h>
#include <stdint.h>

#ifdef OC_HAS_FEATURE_CRC64

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Calculate CRC64 for a buffer of data.
*
* @param buffer The buffer of data to calculate CRC64 for (cannot be NULL);
* @param size The size of the buffer of data to calculate CRC64 for.
*
* @return The CRC64 value.
*/
uint64_t oc_crc64(const uint8_t *buffer, size_t size) OC_NONNULL();

#ifdef __cplusplus
}
#endif

#endif /* OC_HAS_FEATURE_CRC64 */

#endif /* OC_CRC_INTERNAL_H */
4 changes: 4 additions & 0 deletions util/oc_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@

#endif /* OC_ETAG && OC_SERVER */

#ifdef OC_HAS_FEATURE_ETAG
#define OC_HAS_FEATURE_CRC64
#endif /* OC_HAS_FEATURE_ETAG */

#endif /* OC_FEATURES_H */
51 changes: 51 additions & 0 deletions util/unittest/crc64test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/****************************************************************************
*
* Copyright (c) 2023 plgd.dev s.r.o.
*
* 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 "util/oc_features.h"

#ifdef OC_HAS_FEATURE_CRC64

#include "util/oc_crc_internal.h"

#include <gtest/gtest.h>

TEST(TestCRC, CRC64)
{
struct test
{
std::string input;
uint64_t crc;
};

std::vector<test> inputs{
{ "", 0x0 },
{ "Hello, World!", 0xA885B0FA12A6B582 },
{ "1234567890", 0x4CCE99FD976EC1A8 },
{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
0x9A076C1F9CFD879C },
{ "IoTivity-Lite is the best IoT C library on the flat plane!",
0xB54B2DCA2CFA4D9E },
};

for (auto &input : inputs) {
EXPECT_EQ(input.crc,
oc_crc64((uint8_t *)&input.input[0], input.input.size()));
}
}

#endif /* OC_HAS_FEATURE_CRC64 */

0 comments on commit ac363dc

Please sign in to comment.