Skip to content

Commit

Permalink
chore: adapt to MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Apr 7, 2024
1 parent 3b4925e commit 3ee1239
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
24 changes: 12 additions & 12 deletions base1464.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,20 @@ int base16384_decode_safe(const char* data, int dlen, char* buf) {
#else
register uint64_t sum = valbuf.val - 0x000000000000004e;
#endif
buf[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14);
buf[i++] = (char)(((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14));
if(offset--) {
sum -= 0x00000000004e0000;
buf[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20);
buf[i++] = (char)(((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20));
if(offset--) {
buf[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28);
buf[i++] = (char)(((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28));
if(offset--) {
sum -= 0x0000004e00000000;
buf[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34);
buf[i++] = (char)(((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34));
if(offset--) {
buf[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42);
buf[i++] = (char)(((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42));
if(offset--) {
sum -= 0x004e000000000000;
buf[i] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48);
buf[i] = (char)(((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48));
}
}
}
Expand Down Expand Up @@ -328,20 +328,20 @@ int base16384_decode(const char* data, int dlen, char* buf) {
#else
register uint64_t sum = vals[n] - 0x000000000000004e;
#endif
buf[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14);
buf[i++] = (char)(((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14));
if(offset--) {
sum -= 0x00000000004e0000;
buf[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20);
buf[i++] = (char)(((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20));
if(offset--) {
buf[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28);
buf[i++] = (char)(((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28));
if(offset--) {
sum -= 0x0000004e00000000;
buf[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34);
buf[i++] = (char)(((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34));
if(offset--) {
buf[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42);
buf[i++] = (char)(((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42));
if(offset--) {
sum -= 0x004e000000000000;
buf[i] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48);
buf[i] = (char)(((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48));
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions base16384.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
#include <stdint.h>
#include <stdio.h>
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

enum base16384_err_t {
base16384_err_ok,
Expand Down Expand Up @@ -63,6 +59,13 @@ typedef enum base16384_err_t base16384_err_t;
// forcely do sumcheck without checking data length
#define BASE16384_FLAG_DO_SUM_CHECK_FORCELY (1<<2)

#ifdef _MSC_VER
#include <BaseTsd.h>
#ifndef ssize_t
#define SSIZE_T ssize_t;
#endif
#endif

/**
* @brief custom reader function interface
* @param client_data the data pointer defined by the client
Expand All @@ -81,6 +84,12 @@ typedef ssize_t (*base16384_reader_t)(const void *client_data, void *buffer, siz
*/
typedef ssize_t (*base16384_writer_t)(const void *client_data, const void *buffer, size_t count);

#ifdef _MSC_VER
#ifdef ssize_t
#undef ssize_t
#endif
#endif

union base16384_io_function_t {
base16384_reader_t reader;
base16384_writer_t writer;
Expand Down
4 changes: 4 additions & 0 deletions binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
// initial sum value used in BASE16384_FLAG_SUM_CHECK_ON_REMAIN
#define BASE16384_SIMPLE_SUM_INIT_VALUE (0x8e29c213)

#ifdef _MSC_VER
static inline uint32_t calc_sum(uint32_t sum, int cnt, const char* encbuf) {
#else
static inline uint32_t calc_sum(uint32_t sum, size_t cnt, const char* encbuf) {
#endif
size_t i;
uint32_t buf;
for(i = 0; i < cnt; i++) {
Expand Down
20 changes: 17 additions & 3 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif

#ifndef __cosmopolitan
#include <stdio.h>
#include <stdint.h>
Expand Down Expand Up @@ -91,7 +97,11 @@ base16384_err_t base16384_encode_file_detailed(const char* input, const char* ou
fputc(0xFE, fpo);
fputc(0xFF, fpo);
}
size_t cnt;
#ifdef _MSC_VER
int cnt;
#else
size_t cnt;
#endif
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) {
int n;
Expand Down Expand Up @@ -159,7 +169,11 @@ base16384_err_t base16384_encode_fp_detailed(FILE* input, FILE* output, char* en
}
off_t inputsize = _BASE16384_ENCBUFSZ;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
size_t cnt;
#ifdef _MSC_VER
int cnt;
#else
size_t cnt;
#endif
while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) {
int n;
while(cnt%7) {
Expand Down Expand Up @@ -228,7 +242,7 @@ base16384_err_t base16384_encode_stream_detailed(base16384_stream_t* input, base
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
if(!(flag&BASE16384_FLAG_NOHEADER)) call_writer(output, "\xfe\xff", 2);
while((cnt = call_reader(input, encbuf, inputsize)) > 0) {
int n;
ssize_t n;
while(cnt%7) {
n = call_reader(input, encbuf+cnt, sizeof(char));
if(n > 0) cnt++;
Expand Down
6 changes: 6 additions & 0 deletions test/file_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif

#include <stdint.h>

#define ok(has_failed, reason) \
Expand Down

0 comments on commit 3ee1239

Please sign in to comment.