From 9589f8d578df0411e6ff2fe6b2e1f8ecace45ad3 Mon Sep 17 00:00:00 2001 From: fumiama Date: Sun, 12 Dec 2021 00:49:12 +0800 Subject: [PATCH] fix on windows --- 32/base14.c | 22 +++++++++++++++++++--- 64/base14.c | 20 ++++++++++++++++++-- CMakeLists.txt | 6 ++++++ base16384.c | 17 +++++++++++++++-- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/32/base14.c b/32/base14.c index eda7bec..7c88164 100644 --- a/32/base14.c +++ b/32/base14.c @@ -18,6 +18,22 @@ # define htobe16(x) ntohs(x) # define htobe32(x) htonl(x) # define htobe64(x) htonll(x) +#elif defined(__WIN32__) + #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) __builtin_bswap16(x) + # define be32toh(x) __builtin_bswap32(x) + # define be64toh(x) __builtin_bswap64(x) + # define htobe16(x) __builtin_bswap16(x) + # define htobe32(x) __builtin_bswap32(x) + # define htobe64(x) __builtin_bswap64(x) + #endif #endif #include "base14.h" @@ -75,7 +91,7 @@ LENDAT* encode(const uint8_t* data, const int32_t len) { if(o--) { sum |= ((uint32_t)data[i + 3] << 20) & 0x0f000000; sum += 0x004e004e; - #if BYTE_ORDER == BIG_ENDIAN + #ifdef WORDS_BIGENDIAN vals[n++] = __builtin_bswap32(sum); #else vals[n++] = sum; @@ -93,7 +109,7 @@ LENDAT* encode(const uint8_t* data, const int32_t len) { } } sum += 0x004e004e; - #if BYTE_ORDER == BIG_ENDIAN + #ifdef WORDS_BIGENDIAN vals[n] = __builtin_bswap32(sum); #else vals[n] = sum; @@ -146,7 +162,7 @@ LENDAT* decode(const uint8_t* data, const int32_t len) { } if(offset--) { //这里有读取越界 - #if BYTE_ORDER == BIG_ENDIAN + #ifdef WORDS_BIGENDIAN register uint32_t sum = __builtin_bswap32(vals[n++]); #else register uint32_t sum = vals[n++]; diff --git a/64/base14.c b/64/base14.c index cb617dd..0dd0d48 100644 --- a/64/base14.c +++ b/64/base14.c @@ -18,6 +18,22 @@ # define htobe16(x) ntohs(x) # define htobe32(x) htonl(x) # define htobe64(x) htonll(x) +#elif defined(__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) __builtin_bswap16(x) + # define be32toh(x) __builtin_bswap32(x) + # define be64toh(x) __builtin_bswap64(x) + # define htobe16(x) __builtin_bswap16(x) + # define htobe32(x) __builtin_bswap32(x) + # define htobe64(x) __builtin_bswap64(x) + #endif #endif #include "base14.h" @@ -86,7 +102,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) { } } sum += 0x004e004e004e004e; - #if BYTE_ORDER == BIG_ENDIAN + #ifdef WORDS_BIGENDIAN vals[n] = __builtin_bswap64(sum); #else vals[n] = sum; @@ -141,7 +157,7 @@ LENDAT* decode(const uint8_t* data, const int64_t len) { } if(offset--) { //这里有读取越界 - #if BYTE_ORDER == BIG_ENDIAN + #ifdef WORDS_BIGENDIAN register uint64_t sum = __builtin_bswap64(vals[n]) - 0x000000000000004e; #else register uint64_t sum = vals[n] - 0x000000000000004e; diff --git a/CMakeLists.txt b/CMakeLists.txt index 18e15bf..9384dad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,12 @@ SET(CMAKE_BUILD_TYPE "Release") add_executable(base16384 base16384.c) +include(TestBigEndian) +test_big_endian(isBigEndian) +if (${isBigEndian}) + add_definitions(-DWORDS_BIGENDIAN) +endif() + IF(CMAKE_SIZEOF_VOID_P EQUAL 8) add_definitions("-DCPUBIT64") add_subdirectory("./64") diff --git a/base16384.c b/base16384.c index befdd66..4353301 100644 --- a/base16384.c +++ b/base16384.c @@ -1,6 +1,9 @@ #include #include #include +#if defined(__WINNT__) + #include +#endif #include "base16384.h" void encode_file(const char* input, const char* output) { @@ -80,22 +83,32 @@ void decode_file(const char* input, const char* output) { } else puts("Open input file error!"); } +#ifndef __WINNT__ unsigned long get_start_ms() { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000); } +#endif #define CHOICE argv[1][1] int main(int argc, char** argv) { if(argc == 4) { - unsigned long t = get_start_ms(); + #if defined(__WINNT__) + clock_t t = clock(); + #else + unsigned long t = get_start_ms(); + #endif switch(CHOICE){ case 'e': encode_file(argv[2], argv[3]); break; case 'd': decode_file(argv[2], argv[3]); break; default: break; } - printf("spend time: %lums\n", get_start_ms() - t); + #if defined(__WINNT__) + printf("spend time: %lums\n", clock() - t); + #else + printf("spend time: %lums\n", get_start_ms() - t); + #endif } else { fputs("Usage: -[e|d] \n", stderr); fputs("\t-e encode\n", stderr);