Skip to content

Commit

Permalink
C API. (google#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall authored and cmumford committed Sep 27, 2017
1 parent 53ecc2f commit 9a58163
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ build_script:

test_script:
- out\%CONFIGURATION%\crc32c_tests.exe
- out\%CONFIGURATION%\crc32c_capi_tests.exe
- out\%CONFIGURATION%\crc32c_bench.exe
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ before_script:

script:
- out/crc32c_tests
- out/crc32c_capi_tests
- out/crc32c_bench
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
cmake_minimum_required(VERSION 3.1)
project(Crc32c VERSION 0.1.0 LANGUAGES C CXX)

# This project can use C11, but will gracefully decay down to C89.
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED OFF)
set(CMAKE_C_EXTENSIONS OFF)

# This project requires C++11.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down Expand Up @@ -308,6 +313,20 @@ if(CRC32C_BUILD_TESTS)
endif(CRC32C_USE_GLOG)

add_test(NAME crc32c_tests COMMAND crc32c_tests)

add_executable(crc32c_capi_tests "")
target_sources(crc32c_capi_tests
PRIVATE
"${PROJECT_SOURCE_DIR}/src/crc32c_capi_unittest.c"
)
target_link_libraries(crc32c_capi_tests crc32c)

# Warnings as errors in Visual Studio for this project's targets.
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_property(TARGET crc32c_capi_tests APPEND PROPERTY COMPILE_OPTIONS "/WX")
endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")

add_test(NAME crc32c_capi_tests COMMAND crc32c_capi_tests)
endif(CRC32C_BUILD_TESTS)

if(CRC32C_BUILD_BENCHMARKS)
Expand Down
41 changes: 37 additions & 4 deletions include/crc32c/crc32c.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
// Copyright 2017 The CRC32C Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
/* Copyright 2017 The CRC32C Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. See the AUTHORS file for names of contributors. */

#ifndef CRC32C_CRC32C_H_
#define CRC32C_CRC32C_H_

// The API exported by the CRC32C project.
/* The API exported by the CRC32C project. */

#if defined(__cplusplus)

#include <cstddef>
#include <cstdint>
#include <string>

#else /* !defined(__cplusplus) */

#include <stddef.h>
#include <stdint.h>

#endif /* !defined(__cplusplus) */


/* The C API. */

#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */

/* Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by
"data" */
uint32_t crc32c_extend(uint32_t crc, const uint8_t* data, size_t count);

/* Computes the CRC32C of "count" bytes in the buffer pointed by "data". */
uint32_t crc32c_value(const uint8_t* data, size_t count);

#ifdef __cplusplus
} /* end extern "C" */
#endif /* defined(__cplusplus) */


/* The C++ API. */

#if defined(__cplusplus)

namespace crc32c {

// Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by
Expand Down Expand Up @@ -48,5 +80,6 @@ inline uint32_t Crc32c(const std::string_view& string_view) {

} // namespace crc32c

#endif /* defined(__cplusplus) */

#endif // CRC32C_CRC32C_H_
9 changes: 9 additions & 0 deletions src/crc32c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) {
return ExtendPortable(crc, data, count);
}

extern "C" uint32_t crc32c_extend(uint32_t crc, const uint8_t* data,
size_t count) {
return crc32c::Extend(crc, data, count);
}

extern "C" uint32_t crc32c_value(const uint8_t* data, size_t count) {
return crc32c::Crc32c(data, count);
}

} // namespace crc32c
66 changes: 66 additions & 0 deletions src/crc32c_capi_unittest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2017 The CRC32C Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "crc32c/crc32c.h"

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
/* From rfc3720 section B.4. */
uint8_t buf[32];

memset(buf, 0, sizeof(buf));
if ((uint32_t)0x8a9136aa != crc32c_value(buf, sizeof(buf))) {
printf("crc32c_value(zeros) test failed\n");
return 1;
}

memset(buf, 0xff, sizeof(buf));
if ((uint32_t)0x62a8ab43 != crc32c_value(buf, sizeof(buf))) {
printf("crc32c_value(0xff) test failed\n");
return 1;
}

for (size_t i = 0; i < 32; ++i)
buf[i] = (uint8_t)i;
if ((uint32_t)0x46dd794e != crc32c_value(buf, sizeof(buf))) {
printf("crc32c_value(0..31) test failed\n");
return 1;
}

for (size_t i = 0; i < 32; ++i)
buf[i] = (uint8_t)(31 - i);
if ((uint32_t)0x113fdb5c != crc32c_value(buf, sizeof(buf))) {
printf("crc32c_value(31..0) test failed\n");
return 1;
}

uint8_t data[48] = {
0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
if ((uint32_t)0xd9963a56 != crc32c_value(data, sizeof(data))) {
printf("crc32c_value(31..0) test failed\n");
return 1;
}

const uint8_t* hello_space_world = (const uint8_t*)"hello world";
const uint8_t* hello_space = (const uint8_t*)"hello ";
const uint8_t* world = (const uint8_t*)"world";

if (crc32c_value(hello_space_world, 11) !=
crc32c_extend(crc32c_value(hello_space, 6), world, 5)) {
printf("crc32c_extend test failed\n");
return 1;
}

printf("All tests passed\n");
return 0;
}

0 comments on commit 9a58163

Please sign in to comment.