Skip to content

Commit b9fcd2e

Browse files
committed
fix base16384_decode_fd
1 parent 9cfa8ad commit b9fcd2e

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

base16384.c

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int main(int argc, char** argv) {
103103
case base16384_err_write_file: perror("base16384_err_write_file"); break;
104104
case base16384_err_open_input_file: perror("base16384_err_open_input_file"); break;
105105
case base16384_err_map_input_file: perror("base16384_err_map_input_file"); break;
106+
case base16384_err_read_file: perror("base16384_err_read_file"); break;
106107
default: perror("base16384"); break;
107108
}
108109
return exitstat;

base16384.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum base16384_err_t {
3232
base16384_err_write_file,
3333
base16384_err_open_input_file,
3434
base16384_err_map_input_file,
35+
base16384_err_read_file,
3536
};
3637
// base16384_err_t is the return value of base16384_en/decode_file
3738
typedef enum base16384_err_t base16384_err_t;

file.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#ifdef __cosmopolitan
3636
#define get_file_size(filepath) ((off_t)GetFileSize(filepath))
3737
#else
38-
static off_t get_file_size(const char* filepath) {
38+
static inline off_t get_file_size(const char* filepath) {
3939
struct stat statbuf;
4040
return stat(filepath, &statbuf)?-1:statbuf.st_size;
4141
}
@@ -152,7 +152,7 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d
152152

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

155-
static int is_next_end(FILE* fp) {
155+
static inline int is_next_end(FILE* fp) {
156156
int ch = fgetc(fp);
157157
if(ch == EOF) return 0;
158158
if(ch == '=') return fgetc(fp);
@@ -246,7 +246,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha
246246
return base16384_err_ok;
247247
}
248248

249-
static int is_next_end_fd(int fd) {
249+
static inline int is_next_end_fd(int fd) {
250250
char ch = 0;
251251
read(fd, &ch, 1);
252252
if(ch == '=') {
@@ -267,9 +267,11 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
267267
int cnt = 0;
268268
int end = 0;
269269
decbuf[0] = 0;
270-
read(input, decbuf, 2);
270+
if(read(input, decbuf, 2) < 2) {
271+
return base16384_err_read_file;
272+
}
271273
if(decbuf[0] != (char)(0xfe)) cnt = 2;
272-
while((end = read(input, decbuf+cnt, inputsize-cnt), cnt) > 0 || cnt > 0) {
274+
while((end = read(input, decbuf+cnt, inputsize-cnt)) > 0 || cnt > 0) {
273275
if(end > 0) {
274276
cnt += end;
275277
if((end = is_next_end_fd(input))) {

0 commit comments

Comments
 (0)