Skip to content

Commit c65a234

Browse files
committed
new no-malloc api
1 parent 8d49009 commit c65a234

File tree

11 files changed

+225
-212
lines changed

11 files changed

+225
-212
lines changed

32/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

32/base14.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

64/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

64/base14.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.0.0)
22
project(base16384 VERSION 2.0)
33
SET(CMAKE_BUILD_TYPE "Release")
4+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
45

56
add_executable(base16384 base16384.c)
67

@@ -11,13 +12,15 @@ if (${isBigEndian})
1112
endif()
1213

1314
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
14-
add_definitions("-DCPUBIT64")
15-
add_subdirectory("./64")
16-
target_link_libraries(base16384 base1464)
15+
add_library(b1464 STATIC base1464.c base14.c)
16+
add_library(b14 SHARED base1464.c base14.c)
17+
target_link_libraries(base16384 b1464)
1718
ELSE()
18-
add_definitions("-DCPUBIT32")
19-
add_subdirectory("./32")
20-
target_link_libraries(base16384 base1432)
19+
add_library(b1432 STATIC base1432.c base14.c)
20+
add_library(b14 SHARED base1432.c base14.c)
21+
target_link_libraries(base16384 b1432)
2122
ENDIF()
2223

23-
INSTALL(TARGETS base16384 RUNTIME DESTINATION bin)
24+
INSTALL(TARGETS base16384 RUNTIME DESTINATION bin)
25+
INSTALL(TARGETS b14 LIBRARY DESTINATION lib)
26+
INSTALL(FILES base14.h DESTINATION include)

base14.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "base14.h"
2+
3+
int encode_len(int dlen) {
4+
int outlen = dlen / 7 * 8;
5+
int offset = dlen % 7;
6+
switch(offset) { // 算上偏移标志字符占用的2字节
7+
case 0: break;
8+
case 1: outlen += 4; break;
9+
case 2:
10+
case 3: outlen += 6; break;
11+
case 4:
12+
case 5: outlen += 8; break;
13+
case 6: outlen += 10; break;
14+
default: break;
15+
}
16+
return outlen + 8; // 冗余的8B用于可能的结尾的覆盖
17+
}
18+
19+
int decode_len(int dlen, int offset) {
20+
int outlen = dlen;
21+
switch(offset) { // 算上偏移标志字符占用的2字节
22+
case 0: break;
23+
case 1: outlen -= 4; break;
24+
case 2:
25+
case 3: outlen -= 6; break;
26+
case 4:
27+
case 5: outlen -= 8; break;
28+
case 6: outlen -= 10; break;
29+
default: break;
30+
}
31+
return outlen / 8 * 7 + offset + 1; // 多出1字节用于循环覆盖
32+
}

base14.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// base14.h
2+
// fumiama 20220319
3+
4+
// encode_len calc min buf size to fill encode result
5+
int encode_len(int dlen);
6+
// decode_len calc min buf size to fill decode result
7+
int decode_len(int dlen, int offset);
8+
9+
// encode data and write result into buf
10+
int encode(const char* data, int dlen, char* buf, int blen);
11+
// decode data and write result into buf
12+
int decode(const char* data, int dlen, char* buf, int blen);

32/base14.c renamed to base1432.c

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//base1432le.c
2-
//fumiama 20210408
1+
// base1432.c
2+
// fumiama 20220319
33
#include <stdio.h>
44
#include <stdlib.h>
55
#ifdef __linux__
@@ -37,17 +37,12 @@
3737
#endif
3838
#include "base14.h"
3939

40-
//#define DEBUG
40+
// #define DEBUG
4141

42-
#ifndef new
43-
#define new malloc
44-
#endif
45-
46-
LENDAT* encode(const uint8_t* data, const int32_t len) {
47-
LENDAT* encd = (LENDAT*)new(sizeof(LENDAT));
48-
int32_t outlen = len / 7 * 8;
49-
uint8_t offset = len % 7;
50-
switch(offset) { //算上偏移标志字符占用的2字节
42+
int encode(const char* data, int dlen, char* buf, int blen) {
43+
int outlen = dlen / 7 * 8;
44+
int offset = dlen % 7;
45+
switch(offset) { // 算上偏移标志字符占用的2字节
5146
case 0: break;
5247
case 1: outlen += 4; break;
5348
case 2:
@@ -60,12 +55,10 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
6055
#ifdef DEBUG
6156
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
6257
#endif
63-
encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖
64-
encd->len = outlen;
65-
uint32_t* vals = (uint32_t*)(encd->data);
58+
uint32_t* vals = (uint32_t*)buf;
6659
uint32_t n = 0;
6760
int32_t i = 0;
68-
for(; i <= len - 7; i += 7) {
61+
for(; i <= dlen - 7; i += 7) {
6962
register uint32_t sum = 0;
7063
register uint32_t shift = htobe32(*(uint32_t*)(data+i));
7164
sum |= (shift>>2) & 0x3fff0000;
@@ -118,19 +111,18 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
118111
#else
119112
vals[n] = sum;
120113
#endif
121-
encd->data[outlen - 2] = '=';
122-
encd->data[outlen - 1] = offset;
114+
buf[outlen - 2] = '=';
115+
buf[outlen - 1] = offset;
123116
}
124-
return encd;
117+
return outlen;
125118
}
126119

127-
LENDAT* decode(const uint8_t* data, const int32_t len) {
128-
LENDAT* decd = (LENDAT*)new(sizeof(LENDAT));
129-
int32_t outlen = len;
130-
uint8_t offset = 0;
131-
if(data[len-2] == '=') {
132-
offset = data[len-1];
133-
switch(offset) { //算上偏移标志字符占用的2字节
120+
int decode(const char* data, int dlen, char* buf, int blen) {
121+
int outlen = dlen;
122+
int offset = 0;
123+
if(data[dlen-2] == '=') {
124+
offset = data[dlen-1];
125+
switch(offset) { // 算上偏移标志字符占用的2字节
134126
case 0: break;
135127
case 1: outlen -= 4; break;
136128
case 2:
@@ -142,12 +134,10 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
142134
}
143135
}
144136
outlen = outlen / 8 * 7 + offset;
145-
decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖
146-
decd->len = outlen;
147137
uint32_t* vals = (uint32_t*)data;
148138
uint32_t n = 0;
149139
int32_t i = 0;
150-
for(; i <= outlen - 7; i+=7) { //n实际每次自增2
140+
for(; i <= outlen - 7; i+=7) { // n实际每次自增2
151141
register uint32_t sum = 0;
152142
register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00;
153143
shift <<= 2;
@@ -156,44 +146,44 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
156146
sum |= shift & 0x0003fff0;
157147
shift = htobe32(vals[n++]) - 0x4e004e00;
158148
sum |= shift >> 26;
159-
*(uint32_t*)(decd->data+i) = be32toh(sum);
149+
*(uint32_t*)(buf+i) = be32toh(sum);
160150
sum = 0;
161151
shift <<= 6;
162152
sum |= shift & 0xffc00000;
163153
shift <<= 2;
164154
sum |= shift & 0x003fff00;
165-
*(uint32_t*)(decd->data+i+4) = be32toh(sum);
155+
*(uint32_t*)(buf+i+4) = be32toh(sum);
166156
}
167157
if(offset--) {
168-
//这里有读取越界
158+
// 这里有读取越界
169159
#ifdef WORDS_BIGENDIAN
170160
register uint32_t sum = __builtin_bswap32(vals[n++]);
171161
#else
172162
register uint32_t sum = vals[n++];
173163
#endif
174164
sum -= 0x0000004e;
175-
decd->data[i++] = ((sum & 0x0000003f) << 2) | ((sum & 0x0000c000) >> 14);
165+
buf[i++] = ((sum & 0x0000003f) << 2) | ((sum & 0x0000c000) >> 14);
176166
if(offset--) {
177167
sum -= 0x004e0000;
178-
decd->data[i++] = ((sum & 0x00003f00) >> 6) | ((sum & 0x00300000) >> 20);
168+
buf[i++] = ((sum & 0x00003f00) >> 6) | ((sum & 0x00300000) >> 20);
179169
if(offset--) {
180-
decd->data[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28);
170+
buf[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28);
181171
if(offset--) {
182-
decd->data[i++] = (sum & 0x0f000000) >> 20;
183-
//这里有读取越界
172+
buf[i++] = (sum & 0x0f000000) >> 20;
173+
// 这里有读取越界
184174
sum = vals[n];
185175
sum -= 0x0000004e;
186-
decd->data[i++] |= (sum & 0x0000003c) >> 2;
176+
buf[i++] |= (sum & 0x0000003c) >> 2;
187177
if(offset--) {
188-
decd->data[i++] = ((sum & 0x00000003) << 6) | ((sum & 0x0000fc00) >> 10);
178+
buf[i++] = ((sum & 0x00000003) << 6) | ((sum & 0x0000fc00) >> 10);
189179
if(offset--) {
190180
sum -= 0x004e0000;
191-
decd->data[i] = ((sum & 0x00000300) >> 2) | ((sum & 0x003f0000) >> 16);
181+
buf[i] = ((sum & 0x00000300) >> 2) | ((sum & 0x003f0000) >> 16);
192182
}
193183
}
194184
}
195185
}
196186
}
197187
}
198-
return decd;
188+
return outlen;
199189
}

0 commit comments

Comments
 (0)