From b408094d1c4c9ed8b207276044433b569d38d075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 26 Apr 2022 16:58:05 +0800 Subject: [PATCH] init source code --- .gitignore | 3 ++ CMakeLists.txt | 16 +++++++++++ src/binary.h | 66 ++++++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 6 ++++ tests/binary_test.c | 12 ++++++++ 5 files changed, 103 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/binary.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/binary_test.c diff --git a/.gitignore b/.gitignore index c6127b3..800f757 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,6 @@ modules.order Module.symvers Mkfile.old dkms.conf + +build +.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..33c7109 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.0.0) +project(fumidb VERSION 1.0) +SET(CMAKE_BUILD_TYPE "Release") + +include(TestBigEndian) +test_big_endian(isBigEndian) +if(${isBigEndian}) + add_definitions(-DWORDS_BIGENDIAN) +endif() + +option(BUILD_TEST "Whether to build tests" OFF) +if(${BUILD_TEST}) + message(STATUS "Building tests...") + enable_testing() + add_subdirectory(tests) +endif() \ No newline at end of file diff --git a/src/binary.h b/src/binary.h new file mode 100644 index 0000000..d8f5078 --- /dev/null +++ b/src/binary.h @@ -0,0 +1,66 @@ +#include +#include +#include + +#ifdef __linux__ +# include +#endif +#ifdef __FreeBSD__ +# include +#endif +#ifdef __NetBSD__ +# include +#endif +#ifdef __OpenBSD__ +# include +# define be16toh(x) betoh16(x) +# define be32toh(x) betoh32(x) +# define be64toh(x) betoh64(x) +#endif +#ifdef __MAC_10_0 +# define be16toh(x) ntohs(x) +# define be32toh(x) ntohl(x) +# define be64toh(x) ntohll(x) +# define htobe16(x) ntohs(x) +# define htobe32(x) htonl(x) +# define htobe64(x) htonll(x) +#endif +#ifdef _WIN64 + #ifdef WORDS_BIGENDIAN + # define be16toh(x) (x) + # define be32toh(x) (x) + # define be64toh(x) (x) + # define htobe16(x) (x) + # define htobe32(x) (x) + # define htobe64(x) (x) + #else + # define be16toh(x) _byteswap_ushort(x) + # define be32toh(x) _byteswap_ulong(x) + # define be64toh(x) _byteswap_uint64(x) + # define htobe16(x) _byteswap_ushort(x) + # define htobe32(x) _byteswap_ulong(x) + # define htobe64(x) _byteswap_uint64(x) + #endif +#endif + +#ifdef _WIN64 + #ifdef WORDS_BIGENDIAN + #define putle16(buf, x) (*(uint16_t*)(buf) = _byteswap_ushort((uint16_t)(x))) + #define putle32(buf, x) (*(uint32_t*)(buf) = _byteswap_ulong((uint32_t)(x))) + #define putle64(buf, x) (*(uint64_t*)(buf) = _byteswap_uint64((uint64_t)(x))) + #else + #define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x)) + #define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x)) + #define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x)) + #endif +#else + #ifdef WORDS_BIGENDIAN + #define putle16(buf, x) (*(uint16_t*)(buf) = __builtin_bswap16((uint16_t)(x))) + #define putle32(buf, x) (*(uint32_t*)(buf) = __builtin_bswap32((uint32_t)(x))) + #define putle64(buf, x) (*(uint64_t*)(buf) = __builtin_bswap64((uint64_t)(x))) + #else + #define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x)) + #define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x)) + #define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x)) + #endif +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..ba0ee0a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.0.0) +project(fumidb_test VERSION 1.0) + +add_executable(binary_test binary_test.c) + +add_test(test_binary binary_test COMMAND binary_test) \ No newline at end of file diff --git a/tests/binary_test.c b/tests/binary_test.c new file mode 100644 index 0000000..1379f86 --- /dev/null +++ b/tests/binary_test.c @@ -0,0 +1,12 @@ +#include "../src/binary.h" + +int main() { + char buf[8]; + putle16(buf, 1); + if(buf[0] != 1) return 1; + putle32(buf, 2); + if(buf[0] != 2) return 2; + putle64(buf, 3); + if(buf[0] != 3) return 3; + return 0; +}