1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-12 15:51:11 +08:00

添加平台适配

This commit is contained in:
fumiama
2021-10-29 22:03:53 +08:00
parent 60c8911bd8
commit eeea555ad9
2 changed files with 46 additions and 14 deletions

View File

@@ -2,7 +2,23 @@
//fumiama 20210408 //fumiama 20210408
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <arpa/inet.h> #if defined(__linux__)
# include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/endian.h>
#elif defined(__OpenBSD__)
# include <sys/types.h>
# define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x)
#elif defined(__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
#include "base14.h" #include "base14.h"
//#define DEBUG //#define DEBUG
@@ -31,20 +47,20 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
int32_t i = 0; int32_t i = 0;
for(; i <= len - 7; i += 7) { for(; i <= len - 7; i += 7) {
register uint32_t sum = 0; register uint32_t sum = 0;
register uint32_t shift = htonl(*(uint32_t*)(data+i)); register uint32_t shift = htobe32(*(uint32_t*)(data+i));
sum |= (shift>>2) & 0x3fff0000; sum |= (shift>>2) & 0x3fff0000;
sum |= (shift>>4) & 0x00003fff; sum |= (shift>>4) & 0x00003fff;
sum += 0x4e004e00; sum += 0x4e004e00;
vals[n++] = ntohl(sum); vals[n++] = be32toh(sum);
shift <<= 26; shift <<= 26;
shift &= 0x3c000000; shift &= 0x3c000000;
sum = 0; sum = 0;
shift |= (htonl(*(uint32_t*)(data+i+4))>>6)&0x03fffffc; shift |= (htobe32(*(uint32_t*)(data+i+4))>>6)&0x03fffffc;
sum |= shift & 0x3fff0000; sum |= shift & 0x3fff0000;
shift >>= 2; shift >>= 2;
sum |= shift & 0x00003fff; sum |= shift & 0x00003fff;
sum += 0x4e004e00; sum += 0x4e004e00;
vals[n++] = ntohl(sum); vals[n++] = be32toh(sum);
} }
uint8_t o = offset; uint8_t o = offset;
if(o--) { if(o--) {
@@ -113,20 +129,20 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
int32_t i = 0; int32_t i = 0;
for(; i <= outlen - 7; i+=7) { //n实际每次自增2 for(; i <= outlen - 7; i+=7) { //n实际每次自增2
register uint32_t sum = 0; register uint32_t sum = 0;
register uint32_t shift = htonl(vals[n++]) - 0x4e004e00; register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00;
shift <<= 2; shift <<= 2;
sum |= shift & 0xfffc0000; sum |= shift & 0xfffc0000;
shift <<= 2; shift <<= 2;
sum |= shift & 0x0003fff0; sum |= shift & 0x0003fff0;
shift = htonl(vals[n++]) - 0x4e004e00; shift = htobe32(vals[n++]) - 0x4e004e00;
sum |= shift >> 26; sum |= shift >> 26;
*(uint32_t*)(decd->data+i) = ntohl(sum); *(uint32_t*)(decd->data+i) = be32toh(sum);
sum = 0; sum = 0;
shift <<= 6; shift <<= 6;
sum |= shift & 0xffc00000; sum |= shift & 0xffc00000;
shift <<= 2; shift <<= 2;
sum |= shift & 0x003fff00; sum |= shift & 0x003fff00;
*(uint32_t*)(decd->data+i+4) = ntohl(sum); *(uint32_t*)(decd->data+i+4) = be32toh(sum);
} }
if(offset--) { if(offset--) {
//这里有读取越界 //这里有读取越界

View File

@@ -2,7 +2,23 @@
//fumiama 20211029 //fumiama 20211029
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <arpa/inet.h> #if defined(__linux__)
# include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/endian.h>
#elif defined(__OpenBSD__)
# include <sys/types.h>
# define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x)
#elif defined(__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
#include "base14.h" #include "base14.h"
//#define DEBUG //#define DEBUG
@@ -31,7 +47,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
int64_t i = 0; int64_t i = 0;
for(; i <= len - 7; i += 7) { for(; i <= len - 7; i += 7) {
register uint64_t sum = 0; register uint64_t sum = 0;
register uint64_t shift = htonll(*(uint64_t*)(data+i))>>2; //这里有读取越界 register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; //这里有读取越界
sum |= shift & 0x3fff000000000000; sum |= shift & 0x3fff000000000000;
shift >>= 2; shift >>= 2;
sum |= shift & 0x00003fff00000000; sum |= shift & 0x00003fff00000000;
@@ -40,7 +56,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
shift >>= 2; shift >>= 2;
sum |= shift & 0x0000000000003fff; sum |= shift & 0x0000000000003fff;
sum += 0x4e004e004e004e00; sum += 0x4e004e004e004e00;
vals[n++] = ntohll(sum); vals[n++] = be64toh(sum);
#ifdef DEBUG #ifdef DEBUG
printf("i: %llu, add sum: %016llx\n", i, sum); printf("i: %llu, add sum: %016llx\n", i, sum);
#endif #endif
@@ -109,7 +125,7 @@ LENDAT* decode(const uint8_t* data, const int64_t len) {
int64_t i = 0; int64_t i = 0;
for(; i <= outlen - 7; n++, i+=7) { for(; i <= outlen - 7; n++, i+=7) {
register uint64_t sum = 0; register uint64_t sum = 0;
register uint64_t shift = htonll(vals[n]) - 0x4e004e004e004e00; register uint64_t shift = htobe64(vals[n]) - 0x4e004e004e004e00;
shift <<= 2; shift <<= 2;
sum |= shift & 0xfffc000000000000; sum |= shift & 0xfffc000000000000;
shift <<= 2; shift <<= 2;
@@ -118,7 +134,7 @@ LENDAT* decode(const uint8_t* data, const int64_t len) {
sum |= shift & 0x0000000fffc00000; sum |= shift & 0x0000000fffc00000;
shift <<= 2; shift <<= 2;
sum |= shift & 0x00000000003fff00; sum |= shift & 0x00000000003fff00;
*(uint64_t*)(decd->data+i) = ntohll(sum); *(uint64_t*)(decd->data+i) = be64toh(sum);
#ifdef DEBUG #ifdef DEBUG
printf("i: %llu, add sum: %016llx\n", i, sum); printf("i: %llu, add sum: %016llx\n", i, sum);
#endif #endif