-
Notifications
You must be signed in to change notification settings - Fork 18
/
base16384.h
349 lines (312 loc) · 11.6 KB
/
base16384.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifndef _BASE16384_H_
#define _BASE16384_H_
/* base16384.h
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022-2024 Fumiama Minamoto.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __cosmopolitan
#include <stdint.h>
#include <stdio.h>
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
#ifndef ssize_t
typedef SSIZE_T ssize_t;
#endif
#endif
enum base16384_err_t {
base16384_err_ok,
base16384_err_get_file_size,
base16384_err_fopen_output_file,
base16384_err_fopen_input_file,
base16384_err_write_file,
base16384_err_open_input_file,
base16384_err_map_input_file,
base16384_err_read_file,
base16384_err_invalid_file_name,
base16384_err_invalid_commandline_parameter,
base16384_err_invalid_decoding_checksum,
};
/**
* @brief return value of base16384_en/decode_file
*/
typedef enum base16384_err_t base16384_err_t;
#ifndef BASE16384_BUFSZ_FACTOR
#define BASE16384_BUFSZ_FACTOR (8)
#endif
#define _BASE16384_ENCBUFSZ ((BUFSIZ*BASE16384_BUFSZ_FACTOR)/7*7)
#define _BASE16384_DECBUFSZ ((BUFSIZ*BASE16384_BUFSZ_FACTOR)/8*8)
#define BASE16384_ENCBUFSZ (_BASE16384_ENCBUFSZ+16)
#define BASE16384_DECBUFSZ (_BASE16384_DECBUFSZ+16)
// disable 0xFEFF file header in encode
#define BASE16384_FLAG_NOHEADER (1<<0)
// enable sum check when using stdin or stdout or inputsize > _BASE16384_ENCBUFSZ
#define BASE16384_FLAG_SUM_CHECK_ON_REMAIN (1<<1)
// forcely do sumcheck without checking data length
#define BASE16384_FLAG_DO_SUM_CHECK_FORCELY (1<<2)
/**
* @brief custom reader function interface
* @param client_data the data pointer defined by the client
* @param buffer to where put data
* @param count read bytes count
* @return the size read
*/
typedef ssize_t (*base16384_reader_t)(const void *client_data, void *buffer, size_t count);
/**
* @brief custom writer function interface
* @param client_data the data pointer defined by the client
* @param buffer from where read data
* @param count write bytes count
* @return the size written
*/
typedef ssize_t (*base16384_writer_t)(const void *client_data, const void *buffer, size_t count);
union base16384_io_function_t {
base16384_reader_t reader;
base16384_writer_t writer;
};
typedef union base16384_io_function_t base16384_io_function_t;
struct base16384_stream_t {
base16384_io_function_t f;
void *client_data;
};
/**
* @brief for stream encode/decode
*/
typedef struct base16384_stream_t base16384_stream_t;
/**
* @brief calculate the exact encoded size
* @param dlen the data length to encode
* @return the size
*/
static inline int _base16384_encode_len(int dlen) {
int outlen = dlen / 7 * 8;
int offset = dlen % 7;
switch(offset) { // also count 0x3dxx
case 0: break;
case 1: outlen += 4; break;
case 2:
case 3: outlen += 6; break;
case 4:
case 5: outlen += 8; break;
case 6: outlen += 10; break;
default: break;
}
return outlen;
}
/**
* @brief calculate minimum encoding buffer size (16 bits larger than the real encoded size)
* @param dlen the data length to encode
* @return the minimum encoding buffer size
*/
static inline int base16384_encode_len(int dlen) {
return _base16384_encode_len(dlen) + 16; // 冗余的 16 字节用于可能的结尾的 unsafe 覆盖
}
/**
* @brief calculate the exact decoded size
* @param dlen the data length to decode
* @param offset the last char `xx` of the underfilled coding (0x3Dxx) or 0 for the full coding
* @return the size
*/
static inline int _base16384_decode_len(int dlen, int offset) {
int outlen = dlen;
switch(offset) { // also count 0x3dxx
case 0: break;
case 1: outlen -= 4; break;
case 2:
case 3: outlen -= 6; break;
case 4:
case 5: outlen -= 8; break;
case 6: outlen -= 10; break;
default: break;
}
return outlen / 8 * 7 + offset;
}
/**
* @brief calculate minimum decoding buffer size (16 bits larger than the real decoded size)
* @param dlen the data length to decode
* @param offset the last char `xx` of the underfilled coding (0x3Dxx) or 0 for the full coding
* @return the minimum decoding buffer size
*/
static inline int base16384_decode_len(int dlen, int offset) {
return _base16384_decode_len(dlen, offset) + 16; // 多出 16 字节用于 unsafe 循环覆盖
}
/**
* @brief safely encode data and write result into buf
* @param data data to encode, no data overread
* @param dlen the data length
* @param buf the output buffer, whose size can be exactly `_base16384_encode_len`
* @return the total length written
*/
int base16384_encode_safe(const char* data, int dlen, char* buf);
/**
* @brief encode data and write result into buf
* @param data data to encode
* @param dlen the data length
* @param buf the output buffer, whose size must greater than `base16384_encode_len`
* @return the total length written
*/
int base16384_encode(const char* data, int dlen, char* buf);
/**
* @brief encode data and write result into buf without considering border condition
* @param data data to encode
* @param dlen the data length
* @param buf the output buffer, whose size must greater than `base16384_encode_len`
* @return the total length written
*/
int base16384_encode_unsafe(const char* data, int dlen, char* buf);
/**
* @brief safely decode data and write result into buf
* @param data data to decode, no data overread
* @param dlen the data length
* @param buf the output buffer, whose size can be exactly `_base16384_decode_len`
* @return the total length written
*/
int base16384_decode_safe(const char* data, int dlen, char* buf);
/**
* @brief decode data and write result into buf
* @param data data to decode
* @param dlen the data length
* @param buf the output buffer, whose size must greater than `base16384_decode_len`
* @return the total length written
*/
int base16384_decode(const char* data, int dlen, char* buf);
/**
* @brief decode data and write result into buf without considering border condition
* @param data data to decode
* @param dlen the data length
* @param buf the output buffer, whose size must greater than `base16384_decode_len`
* @return the total length written
*/
int base16384_decode_unsafe(const char* data, int dlen, char* buf);
#define base16384_typed_params(type) type input, type output, char* encbuf, char* decbuf
#define base16384_typed_flag_params(type) base16384_typed_params(type), int flag
/**
* @brief encode input file to output file
* @param input filename or `-` to specify stdin
* @param output filename or `-` to specify stdout
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_encode_file_detailed(base16384_typed_flag_params(const char*));
/**
* @brief encode input `FILE*` to output `FILE*`
* @param input `FILE*` pointer
* @param output `FILE*` pointer
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_encode_fp_detailed(base16384_typed_flag_params(FILE*));
/**
* @brief encode input stream to output stream
* @param input file descripter
* @param output file descripter
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_encode_fd_detailed(base16384_typed_flag_params(int));
/**
* @brief encode custom input reader to custom output writer
* @param input custom input reader
* @param output custom output writer
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_encode_stream_detailed(base16384_typed_flag_params(base16384_stream_t*));
/**
* @brief decode input file to output file
* @param input filename or `-` to specify stdin
* @param output filename or `-` to specify stdout
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_decode_file_detailed(base16384_typed_flag_params(const char*));
/**
* @brief decode input `FILE*` to output `FILE*`
* @param input `FILE*` pointer
* @param output `FILE*` pointer
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_decode_fp_detailed(base16384_typed_flag_params(FILE*));
/**
* @brief decode input stream to output stream
* @param input file descripter
* @param output file descripter
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_decode_fd_detailed(base16384_typed_flag_params(int));
/**
* @brief decode custom input reader to custom output writer
* @param input custom input reader
* @param output custom output writer
* @param encbuf must be no less than BASE16384_ENCBUFSZ
* @param decbuf must be no less than BASE16384_DECBUFSZ
* @param flag BASE16384_FLAG_xxx value, add multiple flags by `|`
* @return the error code
*/
base16384_err_t base16384_decode_stream_detailed(base16384_typed_flag_params(base16384_stream_t*));
#define BASE16384_WRAP_DECL(method, name, type) \
base16384_err_t base16384_##method##_##name(base16384_typed_params(type));
BASE16384_WRAP_DECL(encode, file, const char*);
BASE16384_WRAP_DECL(encode, fp, FILE*);
BASE16384_WRAP_DECL(encode, fd, int);
BASE16384_WRAP_DECL(encode, stream, base16384_stream_t*);
BASE16384_WRAP_DECL(decode, file, const char*);
BASE16384_WRAP_DECL(decode, fp, FILE*);
BASE16384_WRAP_DECL(decode, fd, int);
BASE16384_WRAP_DECL(decode, stream, base16384_stream_t*);
#undef BASE16384_WRAP_DECL
#undef base16384_typed_flag_params
#undef base16384_typed_params
/**
* @brief call perror on error
* @param err the error
* @return the input parameter `err`
*/
static inline base16384_err_t base16384_perror(base16384_err_t err) {
#define base16384_perror_case(n) case base16384_err_##n: perror("base16384_err_"#n)
if(err) switch(err) {
base16384_perror_case(get_file_size); break;
base16384_perror_case(fopen_output_file); break;
base16384_perror_case(fopen_input_file); break;
base16384_perror_case(write_file); break;
base16384_perror_case(open_input_file); break;
base16384_perror_case(map_input_file); break;
base16384_perror_case(read_file); break;
base16384_perror_case(invalid_file_name); break;
base16384_perror_case(invalid_commandline_parameter); break;
base16384_perror_case(invalid_decoding_checksum); break;
default: perror("base16384"); break;
}
#undef base16384_perror_case
return err;
}
#endif