Skip to content

Commit

Permalink
fix base16384_decode_fd
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 17, 2022
1 parent 9cfa8ad commit b9fcd2e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions base16384.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ int main(int argc, char** argv) {
case base16384_err_write_file: perror("base16384_err_write_file"); break;
case base16384_err_open_input_file: perror("base16384_err_open_input_file"); break;
case base16384_err_map_input_file: perror("base16384_err_map_input_file"); break;
case base16384_err_read_file: perror("base16384_err_read_file"); break;
default: perror("base16384"); break;
}
return exitstat;
Expand Down
1 change: 1 addition & 0 deletions base16384.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum base16384_err_t {
base16384_err_write_file,
base16384_err_open_input_file,
base16384_err_map_input_file,
base16384_err_read_file,
};
// base16384_err_t is the return value of base16384_en/decode_file
typedef enum base16384_err_t base16384_err_t;
Expand Down
12 changes: 7 additions & 5 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#ifdef __cosmopolitan
#define get_file_size(filepath) ((off_t)GetFileSize(filepath))
#else
static off_t get_file_size(const char* filepath) {
static inline off_t get_file_size(const char* filepath) {
struct stat statbuf;
return stat(filepath, &statbuf)?-1:statbuf.st_size;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d

#define skip_offset(input_file) ((input_file[0]==(char)0xFE)?2:0)

static int is_next_end(FILE* fp) {
static inline int is_next_end(FILE* fp) {
int ch = fgetc(fp);
if(ch == EOF) return 0;
if(ch == '=') return fgetc(fp);
Expand Down Expand Up @@ -246,7 +246,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha
return base16384_err_ok;
}

static int is_next_end_fd(int fd) {
static inline int is_next_end_fd(int fd) {
char ch = 0;
read(fd, &ch, 1);
if(ch == '=') {
Expand All @@ -267,9 +267,11 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
int cnt = 0;
int end = 0;
decbuf[0] = 0;
read(input, decbuf, 2);
if(read(input, decbuf, 2) < 2) {
return base16384_err_read_file;
}
if(decbuf[0] != (char)(0xfe)) cnt = 2;
while((end = read(input, decbuf+cnt, inputsize-cnt), cnt) > 0 || cnt > 0) {
while((end = read(input, decbuf+cnt, inputsize-cnt)) > 0 || cnt > 0) {
if(end > 0) {
cnt += end;
if((end = is_next_end_fd(input))) {
Expand Down

0 comments on commit b9fcd2e

Please sign in to comment.