1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-27 12:10:24 +08:00

fix #1: fix on msvc & define malloc as new

This commit is contained in:
源文雨
2022-03-17 19:55:59 +08:00
parent 9589f8d578
commit 6d220dd575
3 changed files with 47 additions and 36 deletions

View File

@@ -2,45 +2,47 @@
//fumiama 20210408 //fumiama 20210408
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#if defined(__linux__) #ifdef __linux__
# include <endian.h> # include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__) #endif
#ifdef __FreeBSD__
# include <sys/endian.h> # include <sys/endian.h>
#elif defined(__OpenBSD__) #endif
#ifdef __NetBSD__
# include <sys/endian.h>
#endif
#ifdef __OpenBSD__
# include <sys/types.h> # include <sys/types.h>
# define be16toh(x) betoh16(x) # define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x) # define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x) #endif
#elif defined(__MAC_10_0) #ifdef __MAC_10_0
# define be16toh(x) ntohs(x) # define be16toh(x) ntohs(x)
# define be32toh(x) ntohl(x) # define be32toh(x) ntohl(x)
# define be64toh(x) ntohll(x)
# define htobe16(x) ntohs(x) # define htobe16(x) ntohs(x)
# define htobe32(x) htonl(x) # define htobe32(x) htonl(x)
# define htobe64(x) htonll(x) #endif
#elif defined(__WIN32__) #ifdef _WIN32
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
# define be16toh(x) (x) # define be16toh(x) (x)
# define be32toh(x) (x) # define be32toh(x) (x)
# define be64toh(x) (x)
# define htobe16(x) (x) # define htobe16(x) (x)
# define htobe32(x) (x) # define htobe32(x) (x)
# define htobe64(x) (x)
#else #else
# define be16toh(x) __builtin_bswap16(x) # define be16toh(x) _byteswap_ushort(x)
# define be32toh(x) __builtin_bswap32(x) # define be32toh(x) _byteswap_ulong(x)
# define be64toh(x) __builtin_bswap64(x) # define htobe16(x) _byteswap_ushort(x)
# define htobe16(x) __builtin_bswap16(x) # define htobe32(x) _byteswap_ulong(x)
# define htobe32(x) __builtin_bswap32(x)
# define htobe64(x) __builtin_bswap64(x)
#endif #endif
#endif #endif
#include "base14.h" #include "base14.h"
//#define DEBUG //#define DEBUG
#define new malloc
LENDAT* encode(const uint8_t* data, const int32_t len) { 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; int32_t outlen = len / 7 * 8;
uint8_t offset = len % 7; uint8_t offset = len % 7;
switch(offset) { //算上偏移标志字符占用的2字节 switch(offset) { //算上偏移标志字符占用的2字节
@@ -56,7 +58,7 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
#ifdef DEBUG #ifdef DEBUG
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
#endif #endif
encd->data = (uint8_t*)malloc(outlen + 8); //冗余的8B用于可能的结尾的覆盖 encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖
encd->len = outlen; encd->len = outlen;
uint32_t* vals = (uint32_t*)(encd->data); uint32_t* vals = (uint32_t*)(encd->data);
uint32_t n = 0; 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* 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; int32_t outlen = len;
uint8_t offset = 0; uint8_t offset = 0;
if(data[len-2] == '=') { if(data[len-2] == '=') {
@@ -138,7 +140,7 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
} }
} }
outlen = outlen / 8 * 7 + offset; outlen = outlen / 8 * 7 + offset;
decd->data = (uint8_t*)malloc(outlen+1); //多出1字节用于循环覆盖 decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖
decd->len = outlen; decd->len = outlen;
uint32_t* vals = (uint32_t*)data; uint32_t* vals = (uint32_t*)data;
uint32_t n = 0; uint32_t n = 0;

View File

@@ -2,23 +2,30 @@
//fumiama 20211029 //fumiama 20211029
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#if defined(__linux__) #ifdef __linux__
# include <endian.h> # include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__) #endif
#ifdef __FreeBSD__
# include <sys/endian.h> # include <sys/endian.h>
#elif defined(__OpenBSD__) #endif
#ifdef __NetBSD__
# include <sys/endian.h>
#endif
#ifdef __OpenBSD__
# include <sys/types.h> # include <sys/types.h>
# define be16toh(x) betoh16(x) # define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x) # define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x) # define be64toh(x) betoh64(x)
#elif defined(__MAC_10_0) #endif
#ifdef __MAC_10_0
# define be16toh(x) ntohs(x) # define be16toh(x) ntohs(x)
# define be32toh(x) ntohl(x) # define be32toh(x) ntohl(x)
# define be64toh(x) ntohll(x) # define be64toh(x) ntohll(x)
# define htobe16(x) ntohs(x) # define htobe16(x) ntohs(x)
# define htobe32(x) htonl(x) # define htobe32(x) htonl(x)
# define htobe64(x) htonll(x) # define htobe64(x) htonll(x)
#elif defined(__WIN64__) #endif
#ifdef _WIN64
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
# define be16toh(x) (x) # define be16toh(x) (x)
# define be32toh(x) (x) # define be32toh(x) (x)
@@ -27,20 +34,22 @@
# define htobe32(x) (x) # define htobe32(x) (x)
# define htobe64(x) (x) # define htobe64(x) (x)
#else #else
# define be16toh(x) __builtin_bswap16(x) # define be16toh(x) _byteswap_ushort(x)
# define be32toh(x) __builtin_bswap32(x) # define be32toh(x) _byteswap_ulong(x)
# define be64toh(x) __builtin_bswap64(x) # define be64toh(x) _byteswap_uint64(x)
# define htobe16(x) __builtin_bswap16(x) # define htobe16(x) _byteswap_ushort(x)
# define htobe32(x) __builtin_bswap32(x) # define htobe32(x) _byteswap_ulong(x)
# define htobe64(x) __builtin_bswap64(x) # define htobe64(x) _byteswap_uint64(x)
#endif #endif
#endif #endif
#include "base14.h" #include "base14.h"
//#define DEBUG //#define DEBUG
#define new malloc
LENDAT* encode(const uint8_t* data, const int64_t len) { 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; int64_t outlen = len / 7 * 8;
uint8_t offset = len % 7; uint8_t offset = len % 7;
switch(offset) { //算上偏移标志字符占用的2字节 switch(offset) { //算上偏移标志字符占用的2字节
@@ -56,7 +65,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
#ifdef DEBUG #ifdef DEBUG
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
#endif #endif
encd->data = (uint8_t*)malloc(outlen + 8); //冗余的8B用于可能的结尾的覆盖 encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖
encd->len = outlen; encd->len = outlen;
uint64_t* vals = (uint64_t*)(encd->data); uint64_t* vals = (uint64_t*)(encd->data);
uint64_t n = 0; 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* 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; int64_t outlen = len;
uint8_t offset = 0; uint8_t offset = 0;
if(data[len-2] == '=') { if(data[len-2] == '=') {
@@ -134,7 +143,7 @@ LENDAT* decode(const uint8_t* data, const int64_t len) {
} }
} }
outlen = outlen / 8 * 7 + offset; outlen = outlen / 8 * 7 + offset;
decd->data = (uint8_t*)malloc(outlen+1); //多出1字节用于循环覆盖 decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖
decd->len = outlen; decd->len = outlen;
uint64_t* vals = (uint64_t*)data; uint64_t* vals = (uint64_t*)data;
uint64_t n = 0; uint64_t n = 0;

View File

@@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#if defined(__WINNT__) #ifdef __WINNT__
#include <windows.h> #include <windows.h>
#endif #endif
#include "base16384.h" #include "base16384.h"