From 6d220dd5759c6a7fdf6b7f535343803d42763a7e 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: Thu, 17 Mar 2022 19:55:59 +0800 Subject: [PATCH] fix #1: fix on msvc & define malloc as new --- 32/base14.c | 42 ++++++++++++++++++++++-------------------- 64/base14.c | 39 ++++++++++++++++++++++++--------------- base16384.c | 2 +- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/32/base14.c b/32/base14.c index 7c88164..31905d9 100644 --- a/32/base14.c +++ b/32/base14.c @@ -2,45 +2,47 @@ //fumiama 20210408 #include #include -#if defined(__linux__) +#ifdef __linux__ # include -#elif defined(__FreeBSD__) || defined(__NetBSD__) +#endif +#ifdef __FreeBSD__ # include -#elif defined(__OpenBSD__) +#endif +#ifdef __NetBSD__ +# include +#endif +#ifdef __OpenBSD__ # include # define be16toh(x) betoh16(x) # define be32toh(x) betoh32(x) -# define be64toh(x) betoh64(x) -#elif defined(__MAC_10_0) +#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) -#elif defined(__WIN32__) +#endif +#ifdef _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) + # define be16toh(x) _byteswap_ushort(x) + # define be32toh(x) _byteswap_ulong(x) + # define htobe16(x) _byteswap_ushort(x) + # define htobe32(x) _byteswap_ulong(x) #endif #endif #include "base14.h" //#define DEBUG +#define new malloc + LENDAT* encode(const uint8_t* data, const int32_t len) { - LENDAT* encd = (LENDAT*)malloc(sizeof(LENDAT)); + LENDAT* encd = (LENDAT*)new(sizeof(LENDAT)); int32_t outlen = len / 7 * 8; uint8_t offset = len % 7; switch(offset) { //算上偏移标志字符占用的2字节 @@ -56,7 +58,7 @@ LENDAT* encode(const uint8_t* data, const int32_t len) { #ifdef DEBUG printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); #endif - encd->data = (uint8_t*)malloc(outlen + 8); //冗余的8B用于可能的结尾的覆盖 + encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖 encd->len = outlen; uint32_t* vals = (uint32_t*)(encd->data); uint32_t n = 0; @@ -121,7 +123,7 @@ LENDAT* encode(const uint8_t* data, const int32_t len) { } LENDAT* decode(const uint8_t* data, const int32_t len) { - LENDAT* decd = (LENDAT*)malloc(sizeof(LENDAT)); + LENDAT* decd = (LENDAT*)new(sizeof(LENDAT)); int32_t outlen = len; uint8_t offset = 0; if(data[len-2] == '=') { @@ -138,7 +140,7 @@ LENDAT* decode(const uint8_t* data, const int32_t len) { } } outlen = outlen / 8 * 7 + offset; - decd->data = (uint8_t*)malloc(outlen+1); //多出1字节用于循环覆盖 + decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖 decd->len = outlen; uint32_t* vals = (uint32_t*)data; uint32_t n = 0; diff --git a/64/base14.c b/64/base14.c index 0dd0d48..eb7ad70 100644 --- a/64/base14.c +++ b/64/base14.c @@ -2,23 +2,30 @@ //fumiama 20211029 #include #include -#if defined(__linux__) +#ifdef __linux__ # include -#elif defined(__FreeBSD__) || defined(__NetBSD__) +#endif +#ifdef __FreeBSD__ # include -#elif defined(__OpenBSD__) +#endif +#ifdef __NetBSD__ +# include +#endif +#ifdef __OpenBSD__ # include # define be16toh(x) betoh16(x) # define be32toh(x) betoh32(x) # define be64toh(x) betoh64(x) -#elif defined(__MAC_10_0) +#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) -#elif defined(__WIN64__) +#endif +#ifdef _WIN64 #ifdef WORDS_BIGENDIAN # define be16toh(x) (x) # define be32toh(x) (x) @@ -27,20 +34,22 @@ # 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) + # 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 #include "base14.h" //#define DEBUG +#define new malloc + LENDAT* encode(const uint8_t* data, const int64_t len) { - LENDAT* encd = (LENDAT*)malloc(sizeof(LENDAT)); + LENDAT* encd = (LENDAT*)new(sizeof(LENDAT)); int64_t outlen = len / 7 * 8; uint8_t offset = len % 7; switch(offset) { //算上偏移标志字符占用的2字节 @@ -56,7 +65,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) { #ifdef DEBUG printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); #endif - encd->data = (uint8_t*)malloc(outlen + 8); //冗余的8B用于可能的结尾的覆盖 + encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖 encd->len = outlen; uint64_t* vals = (uint64_t*)(encd->data); uint64_t n = 0; @@ -117,7 +126,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) { } LENDAT* decode(const uint8_t* data, const int64_t len) { - LENDAT* decd = (LENDAT*)malloc(sizeof(LENDAT)); + LENDAT* decd = (LENDAT*)new(sizeof(LENDAT)); int64_t outlen = len; uint8_t offset = 0; if(data[len-2] == '=') { @@ -134,7 +143,7 @@ LENDAT* decode(const uint8_t* data, const int64_t len) { } } outlen = outlen / 8 * 7 + offset; - decd->data = (uint8_t*)malloc(outlen+1); //多出1字节用于循环覆盖 + decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖 decd->len = outlen; uint64_t* vals = (uint64_t*)data; uint64_t n = 0; diff --git a/base16384.c b/base16384.c index 4353301..03c4e98 100644 --- a/base16384.c +++ b/base16384.c @@ -1,7 +1,7 @@ #include #include #include -#if defined(__WINNT__) +#ifdef __WINNT__ #include #endif #include "base16384.h"