From 20cb252b483c6797a77219be3faa6513e35adf51 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Fri, 14 Apr 2023 15:00:58 -0400 Subject: Eliminate magic numbers --- diff --git a/src/gzip.c b/src/gzip.c index dc22ee0..6e9528f 100644 --- a/src/gzip.c +++ b/src/gzip.c @@ -21,6 +21,9 @@ #include #include "defs.h" #include "gzip.h" +#include "ustar.h" + +#define OPKG_OPK_GZIP_WINDOW_BITS_ (15 + 16) struct opkg_opk_gzip { int (*read)(void *, char **, size_t *); @@ -46,7 +49,7 @@ opkg_opk_gzip_init(int (*read)(void *, char **, size_t *), void *user_data) gzip->stream.zalloc = Z_NULL; gzip->stream.zfree = Z_NULL; gzip->stream.opaque = Z_NULL; - if (inflateInit2(&gzip->stream, 15 + 16) != Z_OK) { + if (inflateInit2(&gzip->stream, OPKG_OPK_GZIP_WINDOW_BITS_) != Z_OK) { free(gzip); return NULL; } @@ -58,7 +61,7 @@ int opkg_opk_gzip_read(struct opkg_opk_gzip *gzip, void *record) { gzip->stream.next_out = record; - gzip->stream.avail_out = 512; + gzip->stream.avail_out = OPKG_OPK_USTAR_RECORD_SIZE; for (;;) { if (gzip->stream.avail_in == 0) { diff --git a/src/main.c b/src/main.c index 79ed68c..25659b6 100644 --- a/src/main.c +++ b/src/main.c @@ -23,9 +23,11 @@ #include "gzip.h" #include "ustar.h" +#define OPKG_OPK_MAIN_FILE_BUFFER_SIZE_ 8192 + struct _opkg_opk_main_file { FILE *file; - char buffer[8192]; + char buffer[OPKG_OPK_MAIN_FILE_BUFFER_SIZE_]; }; static int @@ -34,7 +36,8 @@ _opkg_opk_main_file_read(void *user_data, char **buffer, size_t *size) struct _opkg_opk_main_file *file = user_data; *buffer = file->buffer; - *size = fread(file->buffer, 1, 8192, file->file); + *size = fread(file->buffer, 1, OPKG_OPK_MAIN_FILE_BUFFER_SIZE_, + file->file); if (feof(file->file)) { return OPKG_OPK_END; } else if (ferror(file->file) || *size == 0) { diff --git a/src/ustar.c b/src/ustar.c index 4805880..f403524 100644 --- a/src/ustar.c +++ b/src/ustar.c @@ -25,6 +25,8 @@ #include "gzip.h" #include "ustar.h" +#define OPKG_OPK_USTAR_NUM_BASE_ 8 + struct _opkg_opk_ustar_header { unsigned char name [100]; unsigned char mode [8]; @@ -70,7 +72,7 @@ static int _opkg_opk_ustar_next(struct opkg_opk_ustar *ustar, struct _opkg_opk_ustar_header *header) { - static unsigned char record[512]; + static unsigned char record[OPKG_OPK_USTAR_RECORD_SIZE]; char *size_end; switch (opkg_opk_gzip_read(ustar->gzip, header)) { @@ -80,8 +82,8 @@ _opkg_opk_ustar_next(struct opkg_opk_ustar *ustar, case OPKG_OPK_ERROR: return OPKG_OPK_ERROR; } - memset(record, 0, 512); - if (memcmp(header, record, 512) == 0) { + memset(record, 0, OPKG_OPK_USTAR_RECORD_SIZE); + if (memcmp(header, record, OPKG_OPK_USTAR_RECORD_SIZE) == 0) { return OPKG_OPK_END; } if (memcmp(header->magic, "ustar", strlen("ustar")) != 0) { @@ -89,7 +91,7 @@ _opkg_opk_ustar_next(struct opkg_opk_ustar *ustar, } ustar->data_size_remaining = strtol((char *) header->size, &size_end, - 8); + OPKG_OPK_USTAR_NUM_BASE_); if (*size_end != '\0') { return OPKG_OPK_ERROR; } @@ -111,8 +113,8 @@ opkg_opk_ustar_list(struct opkg_opk_ustar *ustar, if (record.prefix[0] != '\0') { sprintf(member->name, "%s/%s", record.prefix, record.name); } else { - memcpy(member->name, record.name, 100); - member->name[100] = '\0'; + memcpy(member->name, record.name, sizeof(record.name)); + member->name[sizeof(record.name)] = '\0'; } while (ustar->data_size_remaining > 0) { @@ -129,7 +131,7 @@ int opkg_opk_ustar_seek(struct opkg_opk_ustar *ustar, const char *member) { static struct _opkg_opk_ustar_header record; - static unsigned char name[257]; + static unsigned char name[OPKG_OPK_USTAR_NAME_MAX_LEN]; int ret; for (;;) { @@ -142,8 +144,8 @@ opkg_opk_ustar_seek(struct opkg_opk_ustar *ustar, const char *member) sprintf((char *) name, "%s/%s", record.prefix, record.name); } else { - memcpy(name, record.name, 100); - name[100] = '\0'; + memcpy(name, record.name, sizeof(record.name)); + name[sizeof(record.name)] = '\0'; } if (strcmp((char *) name, member) == 0) { @@ -178,11 +180,11 @@ opkg_opk_ustar_read(struct opkg_opk_ustar *ustar, char *buffer, size_t *size) return OPKG_OPK_ERROR; } - if (ustar->data_size_remaining >= 512) { + if (ustar->data_size_remaining >= OPKG_OPK_USTAR_RECORD_SIZE) { if (size != NULL) { - *size = 512; + *size = OPKG_OPK_USTAR_RECORD_SIZE; } - ustar->data_size_remaining -= 512; + ustar->data_size_remaining -= OPKG_OPK_USTAR_RECORD_SIZE; } else { if (size != NULL) { *size = ustar->data_size_remaining; diff --git a/src/ustar.h b/src/ustar.h index ccb10a2..596f598 100644 --- a/src/ustar.h +++ b/src/ustar.h @@ -20,10 +20,14 @@ #ifndef OPKG_OPK_USTAR_H_ #define OPKG_OPK_USTAR_H_ +#define OPKG_OPK_USTAR_RECORD_SIZE 512 +#define OPKG_OPK_USTAR_NAME_MAX_LEN 257 /* prefix[155] + '/' + name[100] + '\0' + */ + struct opkg_opk_ustar; struct opkg_opk_ustar_member { - char name[257]; /* prefix[155] + '/' + name[100] + '\0' */ + char name[OPKG_OPK_USTAR_NAME_MAX_LEN]; }; struct opkg_opk_ustar * -- cgit v0.9.1