Fixes related to compiler warnings
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s

Fix integer mismatch usages/comparisons.

Use "size_t" instead of "unsigned int" for data structures.
This commit is contained in:
Stephen Seo 2024-09-06 14:23:27 +09:00
parent 4cd660ffd5
commit 425fa77900
11 changed files with 138 additions and 97 deletions

View file

@ -39,9 +39,9 @@
#include "helpers.h" #include "helpers.h"
#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp" #define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%lu.tmp"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%uu of %%%uu.\n" #define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%lulu of %%%lulu.\n"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%uu/%%%uu]\n" #define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%lulu/%%%lulu]\n"
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
@ -158,8 +158,8 @@ int write_files_fn(void *data, void *ud) {
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
// Use temp file to store compressed data. // Use temp file to store compressed data.
char temp_filename[512]; char temp_filename[512];
unsigned int idx = 0; size_t idx = 0;
unsigned int temp_dir_end = strlen(state->parsed->temp_dir); size_t temp_dir_end = strlen(state->parsed->temp_dir);
snprintf(temp_filename, 512, TEMP_FILENAME_CMP, state->parsed->temp_dir, snprintf(temp_filename, 512, TEMP_FILENAME_CMP, state->parsed->temp_dir,
state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/", state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/",
idx); idx);
@ -341,7 +341,7 @@ int write_files_fn(void *data, void *ud) {
if (!read_done) { if (!read_done) {
ret = read(pipe_outof_cmd[0], read_buf, 1024); ret = read(pipe_outof_cmd[0], read_buf, 1024);
if (ret > 0) { if (ret > 0) {
read_count = fwrite(read_buf, 1, ret, tmp_fd); read_count = fwrite(read_buf, 1, (size_t)ret, tmp_fd);
if (read_count != (size_t)ret) { if (read_count != (size_t)ret) {
// Write to tmp_fd error. // Write to tmp_fd error.
fprintf(stderr, fprintf(stderr,
@ -384,7 +384,12 @@ int write_files_fn(void *data, void *ud) {
uint16_t u16; uint16_t u16;
uint64_t u64; uint64_t u64;
u16 = strlen(file_info->filename); size_t temp_size = strlen(file_info->filename);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Filename size is too large to store!\n");
return 1;
}
u16 = (uint16_t)temp_size;
// Write filename length. // Write filename length.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -406,7 +411,7 @@ int write_files_fn(void *data, void *ud) {
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((unsigned char *)temp_to_write->buf)[idx] = 0;
} }
@ -474,7 +479,7 @@ int write_files_fn(void *data, void *ud) {
} }
// Write file length. // Write file length.
u64 = end; u64 = (uint64_t)end;
simple_archiver_helper_64_bit_be(&u64); simple_archiver_helper_64_bit_be(&u64);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(8); temp_to_write->buf = malloc(8);
@ -511,7 +516,12 @@ int write_files_fn(void *data, void *ud) {
uint16_t u16; uint16_t u16;
uint64_t u64; uint64_t u64;
u16 = strlen(file_info->filename); size_t temp_size = strlen(file_info->filename);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Filename is too large to store!\n");
return 1;
}
u16 = (uint16_t)temp_size;
// Write filename length. // Write filename length.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -533,7 +543,7 @@ int write_files_fn(void *data, void *ud) {
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((unsigned char *)temp_to_write->buf)[idx] = 0;
} }
@ -606,7 +616,7 @@ int write_files_fn(void *data, void *ud) {
// Error. // Error.
return 1; return 1;
} }
u64 = end; u64 = (uint64_t)end;
simple_archiver_helper_64_bit_be(&u64); simple_archiver_helper_64_bit_be(&u64);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(8); temp_to_write->buf = malloc(8);
@ -647,7 +657,12 @@ int write_files_fn(void *data, void *ud) {
// A symblic link. // A symblic link.
uint16_t u16; uint16_t u16;
u16 = strlen(file_info->filename); size_t temp_size = strlen(file_info->filename);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Filename is too large to store!\n");
return 1;
}
u16 = (uint16_t)temp_size;
// Write filename length. // Write filename length.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -669,7 +684,7 @@ int write_files_fn(void *data, void *ud) {
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((unsigned char *)temp_to_write->buf)[idx] = 0;
} }
@ -746,8 +761,8 @@ int write_files_fn(void *data, void *ud) {
// Compare paths to get relative path. // Compare paths to get relative path.
// Get first non-common char. // Get first non-common char.
unsigned int idx; size_t idx;
unsigned int last_slash; size_t last_slash;
for (idx = 0, last_slash = 0; for (idx = 0, last_slash = 0;
idx < strlen(abs_path) && idx < strlen(link_abs_path); ++idx) { idx < strlen(abs_path) && idx < strlen(link_abs_path); ++idx) {
if (((const char *)abs_path)[idx] != if (((const char *)abs_path)[idx] !=
@ -812,7 +827,12 @@ int write_files_fn(void *data, void *ud) {
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
} else if ((state->parsed->flags & 0x20) == 0) { } else if ((state->parsed->flags & 0x20) == 0) {
// Write absolute path length. // Write absolute path length.
u16 = strlen(abs_path); size_t temp_size = strlen(abs_path);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Absolute path name is too large!\n");
return 1;
}
u16 = (uint16_t)temp_size;
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
@ -841,7 +861,12 @@ int write_files_fn(void *data, void *ud) {
if (rel_path) { if (rel_path) {
// Write relative path length. // Write relative path length.
u16 = strlen(rel_path); size_t temp_size = strlen(rel_path);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Relative path name is too large!\n");
return 1;
}
u16 = (uint16_t)temp_size;
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(2); temp_to_write->buf = malloc(2);
@ -1043,14 +1068,19 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
c = 0; c = 0;
for (unsigned int i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
if (fwrite(&c, 1, 1, out_f) != 1) { if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
} }
// De/compressor bytes. // De/compressor bytes.
u16 = strlen(state->parsed->compressor); size_t temp_size = strlen(state->parsed->compressor);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Compressor cmd string is too large!\n");
return SDAS_NO_COMPRESSOR;
}
u16 = (uint16_t)temp_size;
// To big-endian. // To big-endian.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
// Write the size in big-endian. // Write the size in big-endian.
@ -1065,7 +1095,12 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
u16 = strlen(state->parsed->decompressor); temp_size = strlen(state->parsed->decompressor);
if (temp_size > 0xFFFF) {
fprintf(stderr, "ERROR: Decompressor cmd string is too large!\n");
return SDAS_NO_DECOMPRESSOR;
}
u16 = (uint16_t)temp_size;
// To big-endian. // To big-endian.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
// Write the size in big-endian. // Write the size in big-endian.
@ -1082,7 +1117,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
} else { } else {
// Write the four flag bytes with first bit NOT set. // Write the four flag bytes with first bit NOT set.
unsigned char c = 0; unsigned char c = 0;
for (unsigned int i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
if (fwrite(&c, 1, 1, out_f) != 1) { if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
@ -1091,7 +1126,11 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
// Write file count. // Write file count.
{ {
uint32_t u32 = filenames->count; if (filenames->count > 0xFFFFFFFF) {
fprintf(stderr, "ERROR: Filenames count is too large!\n");
return SDAS_INTERNAL_ERROR;
}
uint32_t u32 = (uint32_t)filenames->count;
simple_archiver_helper_32_bit_be(&u32); simple_archiver_helper_32_bit_be(&u32);
if (fwrite(&u32, 1, 4, out_f) != 4) { if (fwrite(&u32, 1, 4, out_f) != 4) {
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
@ -1232,7 +1271,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
fprintf(stderr, "File count is %u\n", u32); fprintf(stderr, "File count is %u\n", u32);
const uint32_t size = u32; const uint32_t size = u32;
const unsigned int digits = simple_archiver_helper_num_digits(size); const size_t digits = simple_archiver_helper_num_digits(size);
char format_str[128]; char format_str[128];
snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits); snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits);
int skip = 0; int skip = 0;
@ -1242,7 +1281,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
state->parsed->working_files[0] != NULL) { state->parsed->working_files[0] != NULL) {
hash_map = simple_archiver_hash_map_init(); hash_map = simple_archiver_hash_map_init();
for (char **iter = state->parsed->working_files; *iter != NULL; ++iter) { for (char **iter = state->parsed->working_files; *iter != NULL; ++iter) {
int len = strlen(*iter) + 1; size_t len = strlen(*iter) + 1;
char *key = malloc(len); char *key = malloc(len);
memcpy(key, *iter, len); memcpy(key, *iter, len);
key[len - 1] = 0; key[len - 1] = 0;
@ -1326,7 +1365,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
unsigned int permissions = 0; mode_t permissions = 0;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
@ -1537,7 +1576,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
int write_again = 0; int write_again = 0;
int write_pipe_done = 0; int write_pipe_done = 0;
int read_pipe_done = 0; int read_pipe_done = 0;
ssize_t fread_ret; size_t fread_ret;
char recv_buf[1024]; char recv_buf[1024];
size_t amount_to_read; size_t amount_to_read;
while (!write_pipe_done || !read_pipe_done) { while (!write_pipe_done || !read_pipe_done) {
@ -1565,7 +1604,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
// Send over pipe to decompressor. // Send over pipe to decompressor.
if (fread_ret > 0) { if (fread_ret > 0) {
ssize_t write_ret = write(pipe_into_cmd[1], buf, fread_ret); ssize_t write_ret = write(pipe_into_cmd[1], buf, fread_ret);
if (write_ret == fread_ret) { if (write_ret > 0 && (size_t)write_ret == fread_ret) {
// Successful write. // Successful write.
write_again = 0; write_again = 0;
if (compressed_file_size == 0) { if (compressed_file_size == 0) {
@ -1596,7 +1635,8 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
if (!read_pipe_done) { if (!read_pipe_done) {
ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf, 1024); ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf, 1024);
if (read_ret > 0) { if (read_ret > 0) {
size_t fwrite_ret = fwrite(recv_buf, 1, read_ret, out_f); size_t fwrite_ret =
fwrite(recv_buf, 1, (size_t)read_ret, out_f);
if (fwrite_ret == (size_t)read_ret) { if (fwrite_ret == (size_t)read_ret) {
// Success. // Success.
} else if (ferror(out_f)) { } else if (ferror(out_f)) {
@ -1647,7 +1687,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
waitpid(decompressor_pid, NULL, 0); waitpid(decompressor_pid, NULL, 0);
} else { } else {
uint64_t compressed_file_size = u64; uint64_t compressed_file_size = u64;
ssize_t fread_ret; size_t fread_ret;
while (compressed_file_size != 0) { while (compressed_file_size != 0) {
if (compressed_file_size > 1024) { if (compressed_file_size > 1024) {
fread_ret = fread(buf, 1, 1024, in_f); fread_ret = fread(buf, 1, 1024, in_f);
@ -1688,14 +1728,14 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
} else { } else {
while (u64 != 0) { while (u64 != 0) {
if (u64 > 1024) { if (u64 > 1024) {
ssize_t read_ret = fread(buf, 1, 1024, in_f); size_t read_ret = fread(buf, 1, 1024, in_f);
if (read_ret > 0) { if (read_ret > 0) {
u64 -= read_ret; u64 -= read_ret;
} else if (ferror(in_f)) { } else if (ferror(in_f)) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
} else { } else {
ssize_t read_ret = fread(buf, 1, u64, in_f); size_t read_ret = fread(buf, 1, u64, in_f);
if (read_ret > 0) { if (read_ret > 0) {
u64 -= read_ret; u64 -= read_ret;
} else if (ferror(in_f)) { } else if (ferror(in_f)) {

View file

@ -32,9 +32,9 @@ typedef struct SDArchiverState {
const SDArchiverParsed *parsed; const SDArchiverParsed *parsed;
FILE *out_f; FILE *out_f;
SDArchiverHashMap *map; SDArchiverHashMap *map;
unsigned int count; size_t count;
unsigned int max; size_t max;
unsigned int digits; size_t digits;
} SDArchiverState; } SDArchiverState;
enum SDArchiverStateReturns { enum SDArchiverStateReturns {

View file

@ -26,18 +26,18 @@
typedef struct SDArchiverHashMapData { typedef struct SDArchiverHashMapData {
void *value; void *value;
void *key; void *key;
unsigned int key_size; size_t key_size;
void (*value_cleanup_fn)(void *); void (*value_cleanup_fn)(void *);
void (*key_cleanup_fn)(void *); void (*key_cleanup_fn)(void *);
} SDArchiverHashMapData; } SDArchiverHashMapData;
typedef struct SDArchiverHashMapKeyData { typedef struct SDArchiverHashMapKeyData {
void *key; void *key;
unsigned int key_size; size_t key_size;
} SDArchiverHashMapKeyData; } SDArchiverHashMapKeyData;
typedef struct SDArchiverInternalIterContext { typedef struct SDArchiverInternalIterContext {
int (*iter_check_fn)(const void *, unsigned int, const void *, void *); int (*iter_check_fn)(const void *, size_t, const void *, void *);
int ret; int ret;
void *user_data; void *user_data;
} SDArchiverInternalIterContext; } SDArchiverInternalIterContext;
@ -75,11 +75,11 @@ int simple_archiver_hash_map_internal_pick_in_list(void *data, void *ud) {
} }
unsigned long long simple_archiver_hash_map_internal_key_to_hash( unsigned long long simple_archiver_hash_map_internal_key_to_hash(
const void *key, unsigned int key_size) { const void *key, size_t key_size) {
unsigned long long seed = 0; unsigned long long seed = 0;
unsigned long long temp = 0; unsigned long long temp = 0;
unsigned int count = 0; size_t count = 0;
for (unsigned int idx = 0; idx < key_size; ++idx) { for (size_t idx = 0; idx < key_size; ++idx) {
temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count); temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count);
++count; ++count;
if (count >= 8) { if (count >= 8) {
@ -110,13 +110,13 @@ int simple_archiver_hash_map_internal_rehash(SDArchiverHashMap **hash_map) {
// Pointers have the same size (at least on the same machine), so // Pointers have the same size (at least on the same machine), so
// sizeof(void*) should be ok. // sizeof(void*) should be ok.
new_hash_map->buckets = malloc(sizeof(void *) * new_hash_map->buckets_size); new_hash_map->buckets = malloc(sizeof(void *) * new_hash_map->buckets_size);
for (unsigned int idx = 0; idx < new_hash_map->buckets_size; ++idx) { for (size_t idx = 0; idx < new_hash_map->buckets_size; ++idx) {
new_hash_map->buckets[idx] = simple_archiver_list_init(); new_hash_map->buckets[idx] = simple_archiver_list_init();
} }
new_hash_map->count = 0; new_hash_map->count = 0;
// Iterate through the old hash map to populate the new hash map. // Iterate through the old hash map to populate the new hash map.
for (unsigned int bucket_idx = 0; bucket_idx < (*hash_map)->buckets_size; for (size_t bucket_idx = 0; bucket_idx < (*hash_map)->buckets_size;
++bucket_idx) { ++bucket_idx) {
SDArchiverLLNode *node = (*hash_map)->buckets[bucket_idx]->head; SDArchiverLLNode *node = (*hash_map)->buckets[bucket_idx]->head;
while (node) { while (node) {
@ -145,7 +145,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
// Pointers have the same size (at least on the same machine), so // Pointers have the same size (at least on the same machine), so
// sizeof(void*) should be ok. // sizeof(void*) should be ok.
hash_map->buckets = malloc(sizeof(void *) * hash_map->buckets_size); hash_map->buckets = malloc(sizeof(void *) * hash_map->buckets_size);
for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) { for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
hash_map->buckets[idx] = simple_archiver_list_init(); hash_map->buckets[idx] = simple_archiver_list_init();
} }
hash_map->count = 0; hash_map->count = 0;
@ -155,7 +155,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) { void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
if (hash_map && *hash_map) { if (hash_map && *hash_map) {
for (unsigned int idx = 0; idx < (*hash_map)->buckets_size; ++idx) { for (size_t idx = 0; idx < (*hash_map)->buckets_size; ++idx) {
SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx; SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx;
simple_archiver_list_free(linked_list); simple_archiver_list_free(linked_list);
} }
@ -168,7 +168,7 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
} }
int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value, int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
void *key, unsigned int key_size, void *key, size_t key_size,
void (*value_cleanup_fn)(void *), void (*value_cleanup_fn)(void *),
void (*key_cleanup_fn)(void *)) { void (*key_cleanup_fn)(void *)) {
if ((*hash_map)->buckets_size <= (*hash_map)->count) { if ((*hash_map)->buckets_size <= (*hash_map)->count) {
@ -214,7 +214,7 @@ int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
} }
void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map, void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map,
const void *key, unsigned int key_size) { const void *key, size_t key_size) {
unsigned long long hash = unsigned long long hash =
simple_archiver_hash_map_internal_key_to_hash(key, key_size) % simple_archiver_hash_map_internal_key_to_hash(key, key_size) %
hash_map->buckets_size; hash_map->buckets_size;
@ -234,7 +234,7 @@ void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map,
} }
int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
unsigned int key_size) { size_t key_size) {
unsigned long long hash = unsigned long long hash =
simple_archiver_hash_map_internal_key_to_hash(key, key_size) % simple_archiver_hash_map_internal_key_to_hash(key, key_size) %
hash_map->buckets_size; hash_map->buckets_size;
@ -269,15 +269,14 @@ int simple_archiver_internal_hash_map_bucket_iter_fn(void *data, void *ud) {
} }
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map, int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(const void *, int (*iter_check_fn)(const void *, size_t,
unsigned int,
const void *, void *), const void *, void *),
void *user_data) { void *user_data) {
SDArchiverInternalIterContext ctx; SDArchiverInternalIterContext ctx;
ctx.iter_check_fn = iter_check_fn; ctx.iter_check_fn = iter_check_fn;
ctx.ret = 0; ctx.ret = 0;
ctx.user_data = user_data; ctx.user_data = user_data;
for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) { for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
if (simple_archiver_list_get( if (simple_archiver_list_get(
hash_map->buckets[idx], hash_map->buckets[idx],
simple_archiver_internal_hash_map_bucket_iter_fn, &ctx) != 0) { simple_archiver_internal_hash_map_bucket_iter_fn, &ctx) != 0) {

View file

@ -21,12 +21,14 @@
#define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32 #define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32
#include <stddef.h>
#include "linked_list.h" #include "linked_list.h"
typedef struct SDArchiverHashMap { typedef struct SDArchiverHashMap {
SDArchiverLinkedList **buckets; SDArchiverLinkedList **buckets;
unsigned int buckets_size; size_t buckets_size;
unsigned int count; size_t count;
} SDArchiverHashMap; } SDArchiverHashMap;
SDArchiverHashMap *simple_archiver_hash_map_init(void); SDArchiverHashMap *simple_archiver_hash_map_init(void);
@ -40,18 +42,18 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map);
/// NOTICE: You must not pass NULL to value, otherwise all "get" checks will /// NOTICE: You must not pass NULL to value, otherwise all "get" checks will
/// fail for the inserted key. /// fail for the inserted key.
int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value, int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
void *key, unsigned int key_size, void *key, size_t key_size,
void (*value_cleanup_fn)(void *), void (*value_cleanup_fn)(void *),
void (*key_cleanup_fn)(void *)); void (*key_cleanup_fn)(void *));
/// Returns NULL if not found. /// Returns NULL if not found.
void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map, void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map,
const void *key, unsigned int key_size); const void *key, size_t key_size);
/// Returns zero on success. Returns one if more than one entry was removed. /// Returns zero on success. Returns one if more than one entry was removed.
/// Otherwise returns non-zero and non-one value on error. /// Otherwise returns non-zero and non-one value on error.
int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
unsigned int key_size); size_t key_size);
/// Iterates through the hash map with the "iter_check_fn", which is passed the /// Iterates through the hash map with the "iter_check_fn", which is passed the
/// key, key-size, value, and user_data. This function will call "iter_check_fn" /// key, key-size, value, and user_data. This function will call "iter_check_fn"
@ -60,8 +62,7 @@ int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
/// "iter_check_fn" returns zero for every call, then this function will return /// "iter_check_fn" returns zero for every call, then this function will return
/// zero after having iterated through every key-value pair. /// zero after having iterated through every key-value pair.
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map, int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(const void *, int (*iter_check_fn)(const void *, size_t,
unsigned int,
const void *, void *), const void *, void *),
void *user_data); void *user_data);

View file

@ -19,6 +19,8 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_
#include <stddef.h>
typedef struct SDArchiverLLNode { typedef struct SDArchiverLLNode {
struct SDArchiverLLNode *next; struct SDArchiverLLNode *next;
struct SDArchiverLLNode *prev; struct SDArchiverLLNode *prev;
@ -29,7 +31,7 @@ typedef struct SDArchiverLLNode {
typedef struct SDArchiverLinkedList { typedef struct SDArchiverLinkedList {
SDArchiverLLNode *head; SDArchiverLLNode *head;
SDArchiverLLNode *tail; SDArchiverLLNode *tail;
unsigned int count; size_t count;
} SDArchiverLinkedList; } SDArchiverLinkedList;
SDArchiverLinkedList *simple_archiver_list_init(void); SDArchiverLinkedList *simple_archiver_list_init(void);

View file

@ -31,7 +31,7 @@ void simple_archiver_priority_heap_internal_realloc(
new_priority_heap->nodes = new_priority_heap->nodes =
calloc(new_priority_heap->capacity, sizeof(SDArchiverPHNode)); calloc(new_priority_heap->capacity, sizeof(SDArchiverPHNode));
for (unsigned int idx = 1; idx < (*priority_heap)->size + 1; ++idx) { for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) {
if ((*priority_heap)->nodes[idx].is_valid != 0) { if ((*priority_heap)->nodes[idx].is_valid != 0) {
simple_archiver_priority_heap_insert( simple_archiver_priority_heap_insert(
&new_priority_heap, (*priority_heap)->nodes[idx].priority, &new_priority_heap, (*priority_heap)->nodes[idx].priority,
@ -79,7 +79,7 @@ SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) { void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
if (priority_heap && *priority_heap) { if (priority_heap && *priority_heap) {
for (unsigned int idx = 1; idx < (*priority_heap)->size + 1; ++idx) { for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) {
if ((*priority_heap)->nodes[idx].is_valid != 0) { if ((*priority_heap)->nodes[idx].is_valid != 0) {
if ((*priority_heap)->nodes[idx].data_cleanup_fn) { if ((*priority_heap)->nodes[idx].data_cleanup_fn) {
(*priority_heap) (*priority_heap)
@ -109,7 +109,7 @@ void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap,
simple_archiver_priority_heap_internal_realloc(priority_heap); simple_archiver_priority_heap_internal_realloc(priority_heap);
} }
unsigned int hole = (*priority_heap)->size + 1; size_t hole = (*priority_heap)->size + 1;
while (hole > 1 && while (hole > 1 &&
(*priority_heap) (*priority_heap)
@ -146,7 +146,7 @@ void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap) {
SDArchiverPHNode end = priority_heap->nodes[priority_heap->size]; SDArchiverPHNode end = priority_heap->nodes[priority_heap->size];
priority_heap->nodes[priority_heap->size].is_valid = 0; priority_heap->nodes[priority_heap->size].is_valid = 0;
unsigned int hole = 1; size_t hole = 1;
while (hole * 2 + 1 <= priority_heap->size) { while (hole * 2 + 1 <= priority_heap->size) {
if (priority_heap->nodes[hole * 2].is_valid != 0 && if (priority_heap->nodes[hole * 2].is_valid != 0 &&
priority_heap->nodes[hole * 2 + 1].is_valid != 0) { priority_heap->nodes[hole * 2 + 1].is_valid != 0) {

View file

@ -66,7 +66,7 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) {
int more_fn(long long a, long long b) { return a > b ? 1 : 0; } int more_fn(long long a, long long b) { return a > b ? 1 : 0; }
int hash_map_iter_check_fn(__attribute__((unused)) const void *key, int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
__attribute__((unused)) unsigned int key_size, __attribute__((unused)) size_t key_size,
const void *value, void *ud) { const void *value, void *ud) {
char *found_buf = ud; char *found_buf = ud;
const size_t real_value = (const size_t)value; const size_t real_value = (const size_t)value;
@ -79,7 +79,7 @@ int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
} }
int hash_map_iter_check_fn2(__attribute__((unused)) const void *key, int hash_map_iter_check_fn2(__attribute__((unused)) const void *key,
__attribute__((unused)) unsigned int key_size, __attribute__((unused)) size_t key_size,
__attribute__((unused)) const void *value, __attribute__((unused)) const void *value,
__attribute__((unused)) void *ud) { __attribute__((unused)) void *ud) {
return 2; return 2;

View file

@ -59,7 +59,7 @@ void simple_archiver_helper_cleanup_chdir_back(char **original) {
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN
chdir(*original); __attribute__((unused)) int unused_ret = chdir(*original);
#endif #endif
free(*original); free(*original);
*original = NULL; *original = NULL;
@ -221,9 +221,9 @@ int simple_archiver_helper_make_dirs(const char *file_path) {
#endif #endif
} }
char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, char *simple_archiver_helper_cut_substr(const char *s, size_t start_idx,
unsigned int end_idx) { size_t end_idx) {
unsigned int s_len = strlen(s); size_t s_len = strlen(s);
if (start_idx > end_idx || start_idx >= s_len || end_idx > s_len) { if (start_idx > end_idx || start_idx >= s_len || end_idx > s_len) {
return NULL; return NULL;
} else if (end_idx == s_len) { } else if (end_idx == s_len) {
@ -246,8 +246,8 @@ char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx,
} }
} }
unsigned int simple_archiver_helper_num_digits(unsigned int value) { size_t simple_archiver_helper_num_digits(size_t value) {
unsigned int digits = 0; size_t digits = 0;
do { do {
++digits; ++digits;
value /= 10; value /= 10;

View file

@ -50,10 +50,10 @@ int simple_archiver_helper_make_dirs(const char *file_path);
/// Returns non-NULL on success. /// Returns non-NULL on success.
/// Must be free'd with "free()" if non-NULL. /// Must be free'd with "free()" if non-NULL.
/// start_idx is inclusive and end_idx is exclusive. /// start_idx is inclusive and end_idx is exclusive.
char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, char *simple_archiver_helper_cut_substr(const char *s, size_t start_idx,
unsigned int end_idx); size_t end_idx);
unsigned int simple_archiver_helper_num_digits(unsigned int value); size_t simple_archiver_helper_num_digits(size_t value);
void simple_archiver_helper_cleanup_FILE(FILE **fd); void simple_archiver_helper_cleanup_FILE(FILE **fd);
void simple_archiver_helper_cleanup_malloced(void **data); void simple_archiver_helper_cleanup_malloced(void **data);

View file

@ -39,15 +39,15 @@
#include "parser_internal.h" #include "parser_internal.h"
/// Gets the first non "./"-like character in the filename. /// Gets the first non "./"-like character in the filename.
unsigned int simple_archiver_parser_internal_get_first_non_current_idx( size_t simple_archiver_parser_internal_get_first_non_current_idx(
const char *filename) { const char *filename) {
unsigned int idx = 0; size_t idx = 0;
unsigned int known_good_idx = 0; size_t known_good_idx = 0;
const unsigned int length = strlen(filename); const size_t length = strlen(filename);
// 0b0001 - checked that idx char is '.' // 0b0001 - checked that idx char is '.'
// 0b0010 - checked that idx char is '/' // 0b0010 - checked that idx char is '/'
unsigned int flags = 0; size_t flags = 0;
for (; idx < length; ++idx) { for (; idx < length; ++idx) {
if ((flags & 3) == 0) { if ((flags & 3) == 0) {
@ -90,8 +90,8 @@ unsigned int simple_archiver_parser_internal_get_first_non_current_idx(
} }
void simple_archiver_parser_internal_remove_end_slash(char *filename) { void simple_archiver_parser_internal_remove_end_slash(char *filename) {
int len = strlen(filename); size_t len = strlen(filename);
int idx; size_t idx;
for (idx = len; idx-- > 0;) { for (idx = len; idx-- > 0;) {
if (filename[idx] != '/') { if (filename[idx] != '/') {
++idx; ++idx;
@ -236,7 +236,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
out->filename = NULL; out->filename = NULL;
} else { } else {
out->flags &= 0xFFFFFFEF; out->flags &= 0xFFFFFFEF;
int size = strlen(argv[1]) + 1; size_t size = strlen(argv[1]) + 1;
out->filename = malloc(size); out->filename = malloc(size);
strncpy(out->filename, argv[1], size); strncpy(out->filename, argv[1], size);
} }
@ -257,7 +257,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
simple_archiver_print_usage(); simple_archiver_print_usage();
return 1; return 1;
} }
int size = strlen(argv[1]) + 1; size_t size = strlen(argv[1]) + 1;
out->compressor = malloc(size); out->compressor = malloc(size);
strncpy(out->compressor, argv[1], size); strncpy(out->compressor, argv[1], size);
--argc; --argc;
@ -268,7 +268,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
simple_archiver_print_usage(); simple_archiver_print_usage();
return 1; return 1;
} }
int size = strlen(argv[1]) + 1; size_t size = strlen(argv[1]) + 1;
out->decompressor = malloc(size); out->decompressor = malloc(size);
strncpy(out->decompressor, argv[1], size); strncpy(out->decompressor, argv[1], size);
--argc; --argc;
@ -297,15 +297,15 @@ int simple_archiver_parse_args(int argc, const char **argv,
} else { } else {
if (out->working_files == NULL) { if (out->working_files == NULL) {
out->working_files = malloc(sizeof(char *) * 2); out->working_files = malloc(sizeof(char *) * 2);
unsigned int arg_idx = size_t arg_idx =
simple_archiver_parser_internal_get_first_non_current_idx(argv[0]); simple_archiver_parser_internal_get_first_non_current_idx(argv[0]);
unsigned int arg_length = strlen(argv[0] + arg_idx) + 1; size_t arg_length = strlen(argv[0] + arg_idx) + 1;
out->working_files[0] = malloc(arg_length); out->working_files[0] = malloc(arg_length);
strncpy(out->working_files[0], argv[0] + arg_idx, arg_length); strncpy(out->working_files[0], argv[0] + arg_idx, arg_length);
simple_archiver_parser_internal_remove_end_slash(out->working_files[0]); simple_archiver_parser_internal_remove_end_slash(out->working_files[0]);
out->working_files[1] = NULL; out->working_files[1] = NULL;
} else { } else {
int working_size = 1; size_t working_size = 1;
char **ptr = out->working_files; char **ptr = out->working_files;
while (ptr && *ptr) { while (ptr && *ptr) {
++working_size; ++working_size;
@ -318,9 +318,9 @@ int simple_archiver_parse_args(int argc, const char **argv,
// Set new actual last element to NULL. // Set new actual last element to NULL.
out->working_files[working_size] = NULL; out->working_files[working_size] = NULL;
unsigned int arg_idx = size_t arg_idx =
simple_archiver_parser_internal_get_first_non_current_idx(argv[0]); simple_archiver_parser_internal_get_first_non_current_idx(argv[0]);
int size = strlen(argv[0] + arg_idx) + 1; size_t size = strlen(argv[0] + arg_idx) + 1;
// Set last element to the arg. // Set last element to the arg.
out->working_files[working_size - 1] = malloc(size); out->working_files[working_size - 1] = malloc(size);
strncpy(out->working_files[working_size - 1], argv[0] + arg_idx, size); strncpy(out->working_files[working_size - 1], argv[0] + arg_idx, size);
@ -392,7 +392,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
fstatat(AT_FDCWD, file_path, &st, AT_SYMLINK_NOFOLLOW); fstatat(AT_FDCWD, file_path, &st, AT_SYMLINK_NOFOLLOW);
if ((st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFMT) == S_IFLNK) { if ((st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFMT) == S_IFLNK) {
// Is a regular file or a symbolic link. // Is a regular file or a symbolic link.
int len = strlen(file_path) + 1; size_t len = strlen(file_path) + 1;
char *filename = malloc(len); char *filename = malloc(len);
strncpy(filename, file_path, len); strncpy(filename, file_path, len);
if (simple_archiver_hash_map_get(hash_map, filename, len - 1) == NULL) { if (simple_archiver_hash_map_get(hash_map, filename, len - 1) == NULL) {
@ -472,11 +472,11 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
} }
// fprintf(stderr, "dir entry in %s is %s\n", next, // fprintf(stderr, "dir entry in %s is %s\n", next,
// dir_entry->d_name); // dir_entry->d_name);
int combined_size = strlen(next) + strlen(dir_entry->d_name) + 2; size_t combined_size = strlen(next) + strlen(dir_entry->d_name) + 2;
char *combined_path = malloc(combined_size); char *combined_path = malloc(combined_size);
snprintf(combined_path, combined_size, "%s/%s", next, snprintf(combined_path, combined_size, "%s/%s", next,
dir_entry->d_name); dir_entry->d_name);
unsigned int valid_idx = size_t valid_idx =
simple_archiver_parser_internal_get_first_non_current_idx( simple_archiver_parser_internal_get_first_non_current_idx(
combined_path); combined_path);
if (valid_idx > 0) { if (valid_idx > 0) {
@ -574,11 +574,10 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
SDArchiverFileInfo *file_info = iter->data; SDArchiverFileInfo *file_info = iter->data;
// Remove leading "./" entries from files_list. // Remove leading "./" entries from files_list.
unsigned int idx = size_t idx = simple_archiver_parser_internal_get_first_non_current_idx(
simple_archiver_parser_internal_get_first_non_current_idx(
file_info->filename); file_info->filename);
if (idx > 0) { if (idx > 0) {
int len = strlen(file_info->filename) + 1 - idx; size_t len = strlen(file_info->filename) + 1 - idx;
char *substr = malloc(len); char *substr = malloc(len);
strncpy(substr, file_info->filename + idx, len); strncpy(substr, file_info->filename + idx, len);
free(file_info->filename); free(file_info->filename);

View file

@ -21,7 +21,7 @@
#include "parser.h" #include "parser.h"
unsigned int simple_archiver_parser_internal_get_first_non_current_idx( size_t simple_archiver_parser_internal_get_first_non_current_idx(
const char *filename); const char *filename);
#endif #endif