Compare commits

..

4 commits

Author SHA1 Message Date
2252b7d732 Refactor data_structures/hash_map rehash fn
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 5s
2024-09-11 12:46:27 +09:00
88d228644e Cleanup related to change of hash-map insert fn
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s
2024-09-09 17:10:17 +09:00
3408183b39 Refactor hash_map insert to use single ptr
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s
Previous implementation used a double ptr for inserting into a hash map.
This refactoring allows for only needing to use a single ptr.

Fixes #17 .
2024-09-09 11:25:00 +09:00
425fa77900 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.
2024-09-06 14:28:31 +09:00
11 changed files with 170 additions and 123 deletions

View file

@ -39,9 +39,9 @@
#include "helpers.h"
#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%uu of %%%uu.\n"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%uu/%%%uu]\n"
#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%lu.tmp"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%lulu of %%%lulu.\n"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%lulu/%%%lulu]\n"
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
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
// Use temp file to store compressed data.
char temp_filename[512];
unsigned int idx = 0;
unsigned int temp_dir_end = strlen(state->parsed->temp_dir);
size_t idx = 0;
size_t temp_dir_end = strlen(state->parsed->temp_dir);
snprintf(temp_filename, 512, TEMP_FILENAME_CMP, state->parsed->temp_dir,
state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/",
idx);
@ -341,7 +341,7 @@ int write_files_fn(void *data, void *ud) {
if (!read_done) {
ret = read(pipe_outof_cmd[0], read_buf, 1024);
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) {
// Write to tmp_fd error.
fprintf(stderr,
@ -384,7 +384,12 @@ int write_files_fn(void *data, void *ud) {
uint16_t u16;
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.
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->buf = malloc(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;
}
@ -474,7 +479,7 @@ int write_files_fn(void *data, void *ud) {
}
// Write file length.
u64 = end;
u64 = (uint64_t)end;
simple_archiver_helper_64_bit_be(&u64);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(8);
@ -511,7 +516,12 @@ int write_files_fn(void *data, void *ud) {
uint16_t u16;
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.
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->buf = malloc(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;
}
@ -606,7 +616,7 @@ int write_files_fn(void *data, void *ud) {
// Error.
return 1;
}
u64 = end;
u64 = (uint64_t)end;
simple_archiver_helper_64_bit_be(&u64);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(8);
@ -647,7 +657,12 @@ int write_files_fn(void *data, void *ud) {
// A symblic link.
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.
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->buf = malloc(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;
}
@ -746,8 +761,8 @@ int write_files_fn(void *data, void *ud) {
// Compare paths to get relative path.
// Get first non-common char.
unsigned int idx;
unsigned int last_slash;
size_t idx;
size_t last_slash;
for (idx = 0, last_slash = 0;
idx < strlen(abs_path) && idx < strlen(link_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);
} else if ((state->parsed->flags & 0x20) == 0) {
// 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);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
@ -841,7 +861,12 @@ int write_files_fn(void *data, void *ud) {
if (rel_path) {
// 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);
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(2);
@ -884,7 +909,7 @@ int write_files_fn(void *data, void *ud) {
int filenames_to_abs_map_fn(void *data, void *ud) {
SDArchiverFileInfo *file_info = data;
void **ptr_array = ud;
SDArchiverHashMap **abs_filenames = ptr_array[0];
SDArchiverHashMap *abs_filenames = ptr_array[0];
const char *user_cwd = ptr_array[1];
__attribute__((cleanup(
simple_archiver_helper_cleanup_chdir_back))) char *original_cwd = NULL;
@ -1011,7 +1036,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
__attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *abs_filenames = simple_archiver_hash_map_init();
void **ptr_array = malloc(sizeof(void *) * 2);
ptr_array[0] = &abs_filenames;
ptr_array[0] = abs_filenames;
ptr_array[1] = (void *)state->parsed->user_cwd;
if (simple_archiver_list_get(filenames, filenames_to_abs_map_fn, ptr_array)) {
free(ptr_array);
@ -1043,14 +1068,19 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
return SDAS_FAILED_TO_WRITE;
}
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) {
return SDAS_FAILED_TO_WRITE;
}
}
// 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.
simple_archiver_helper_16_bit_be(&u16);
// 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;
}
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.
simple_archiver_helper_16_bit_be(&u16);
// Write the size in big-endian.
@ -1082,7 +1117,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
} else {
// Write the four flag bytes with first bit NOT set.
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) {
return SDAS_FAILED_TO_WRITE;
}
@ -1091,7 +1126,11 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
// 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);
if (fwrite(&u32, 1, 4, out_f) != 4) {
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);
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];
snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits);
int skip = 0;
@ -1242,12 +1281,12 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
state->parsed->working_files[0] != NULL) {
hash_map = simple_archiver_hash_map_init();
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);
memcpy(key, *iter, len);
key[len - 1] = 0;
simple_archiver_hash_map_insert(
&hash_map, key, key, len,
hash_map, key, key, len,
simple_archiver_helper_datastructure_cleanup_nop, NULL);
// fprintf(stderr, "\"%s\" put in map\n", key);
}
@ -1326,7 +1365,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
return SDAS_INVALID_FILE;
}
unsigned int permissions = 0;
mode_t permissions = 0;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
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_pipe_done = 0;
int read_pipe_done = 0;
ssize_t fread_ret;
size_t fread_ret;
char recv_buf[1024];
size_t amount_to_read;
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.
if (fread_ret > 0) {
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.
write_again = 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) {
ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf, 1024);
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) {
// Success.
} 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);
} else {
uint64_t compressed_file_size = u64;
ssize_t fread_ret;
size_t fread_ret;
while (compressed_file_size != 0) {
if (compressed_file_size > 1024) {
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 {
while (u64 != 0) {
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) {
u64 -= read_ret;
} else if (ferror(in_f)) {
return SDAS_INTERNAL_ERROR;
}
} 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) {
u64 -= read_ret;
} else if (ferror(in_f)) {

View file

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

View file

@ -26,18 +26,18 @@
typedef struct SDArchiverHashMapData {
void *value;
void *key;
unsigned int key_size;
size_t key_size;
void (*value_cleanup_fn)(void *);
void (*key_cleanup_fn)(void *);
} SDArchiverHashMapData;
typedef struct SDArchiverHashMapKeyData {
void *key;
unsigned int key_size;
size_t key_size;
} SDArchiverHashMapKeyData;
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;
void *user_data;
} 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(
const void *key, unsigned int key_size) {
const void *key, size_t key_size) {
unsigned long long seed = 0;
unsigned long long temp = 0;
unsigned int count = 0;
for (unsigned int idx = 0; idx < key_size; ++idx) {
size_t count = 0;
for (size_t idx = 0; idx < key_size; ++idx) {
temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count);
++count;
if (count >= 8) {
@ -101,28 +101,27 @@ void simple_archiver_hash_map_internal_no_free_fn(
}
/// Returns 0 on success.
int simple_archiver_hash_map_internal_rehash(SDArchiverHashMap **hash_map) {
if (!hash_map || !*hash_map) {
int simple_archiver_hash_map_internal_rehash(SDArchiverHashMap *hash_map) {
if (!hash_map) {
return 1;
}
SDArchiverHashMap *new_hash_map = malloc(sizeof(SDArchiverHashMap));
new_hash_map->buckets_size = (*hash_map)->buckets_size * 2;
SDArchiverHashMap new_hash_map;
new_hash_map.buckets_size = hash_map->buckets_size * 2;
// Pointers have the same size (at least on the same machine), so
// sizeof(void*) should be ok.
new_hash_map->buckets = malloc(sizeof(void *) * new_hash_map->buckets_size);
for (unsigned int idx = 0; idx < new_hash_map->buckets_size; ++idx) {
new_hash_map->buckets[idx] = simple_archiver_list_init();
new_hash_map.buckets = malloc(sizeof(void *) * new_hash_map.buckets_size);
for (size_t idx = 0; idx < new_hash_map.buckets_size; ++idx) {
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.
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) {
SDArchiverLLNode *node = (*hash_map)->buckets[bucket_idx]->head;
SDArchiverLLNode *node = hash_map->buckets[bucket_idx]->head;
while (node) {
node = node->next;
if (node && node != (*hash_map)->buckets[bucket_idx]->tail &&
node->data) {
if (node && node != hash_map->buckets[bucket_idx]->tail && node->data) {
SDArchiverHashMapData *data = node->data;
simple_archiver_hash_map_insert(&new_hash_map, data->value, data->key,
data->key_size, data->value_cleanup_fn,
@ -133,7 +132,14 @@ int simple_archiver_hash_map_internal_rehash(SDArchiverHashMap **hash_map) {
}
}
simple_archiver_hash_map_free(hash_map);
// Free the buckets in the old hash_map.
for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
SDArchiverLinkedList **linked_list = hash_map->buckets + idx;
simple_archiver_list_free(linked_list);
}
free(hash_map->buckets);
// Move the new buckets and related data into the old hash_map.
*hash_map = new_hash_map;
return 0;
@ -145,7 +151,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
// Pointers have the same size (at least on the same machine), so
// sizeof(void*) should be ok.
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->count = 0;
@ -155,7 +161,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
void simple_archiver_hash_map_free(SDArchiverHashMap **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;
simple_archiver_list_free(linked_list);
}
@ -167,11 +173,11 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
}
}
int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
void *key, unsigned int key_size,
int simple_archiver_hash_map_insert(SDArchiverHashMap *hash_map, void *value,
void *key, size_t key_size,
void (*value_cleanup_fn)(void *),
void (*key_cleanup_fn)(void *)) {
if ((*hash_map)->buckets_size <= (*hash_map)->count) {
if (hash_map->buckets_size <= hash_map->count) {
simple_archiver_hash_map_internal_rehash(hash_map);
}
@ -184,13 +190,13 @@ int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
unsigned long long hash =
simple_archiver_hash_map_internal_key_to_hash(key, key_size) %
(*hash_map)->buckets_size;
hash_map->buckets_size;
int result = simple_archiver_list_add_front(
(*hash_map)->buckets[hash], data,
hash_map->buckets[hash], data,
simple_archiver_hash_map_internal_cleanup_data);
if (result == 0) {
++(*hash_map)->count;
++hash_map->count;
return 0;
} else {
if (value) {
@ -214,7 +220,7 @@ int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
}
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 =
simple_archiver_hash_map_internal_key_to_hash(key, key_size) %
hash_map->buckets_size;
@ -234,7 +240,7 @@ void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map,
}
int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
unsigned int key_size) {
size_t key_size) {
unsigned long long hash =
simple_archiver_hash_map_internal_key_to_hash(key, key_size) %
hash_map->buckets_size;
@ -269,15 +275,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 (*iter_check_fn)(const void *,
unsigned int,
int (*iter_check_fn)(const void *, size_t,
const void *, void *),
void *user_data) {
SDArchiverInternalIterContext ctx;
ctx.iter_check_fn = iter_check_fn;
ctx.ret = 0;
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(
hash_map->buckets[idx],
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
#include <stddef.h>
#include "linked_list.h"
typedef struct SDArchiverHashMap {
SDArchiverLinkedList **buckets;
unsigned int buckets_size;
unsigned int count;
size_t buckets_size;
size_t count;
} SDArchiverHashMap;
SDArchiverHashMap *simple_archiver_hash_map_init(void);
@ -39,19 +41,19 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map);
/// If key_cleanup_fn is NULL, then "free" is used instead.
/// NOTICE: You must not pass NULL to value, otherwise all "get" checks will
/// fail for the inserted key.
int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value,
void *key, unsigned int key_size,
int simple_archiver_hash_map_insert(SDArchiverHashMap *hash_map, void *value,
void *key, size_t key_size,
void (*value_cleanup_fn)(void *),
void (*key_cleanup_fn)(void *));
/// Returns NULL if not found.
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.
/// Otherwise returns non-zero and non-one value on error.
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
/// 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
/// zero after having iterated through every key-value pair.
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(const void *,
unsigned int,
int (*iter_check_fn)(const void *, size_t,
const void *, void *),
void *user_data);

View file

@ -19,6 +19,8 @@
#ifndef 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 {
struct SDArchiverLLNode *next;
struct SDArchiverLLNode *prev;
@ -29,7 +31,7 @@ typedef struct SDArchiverLLNode {
typedef struct SDArchiverLinkedList {
SDArchiverLLNode *head;
SDArchiverLLNode *tail;
unsigned int count;
size_t count;
} SDArchiverLinkedList;
SDArchiverLinkedList *simple_archiver_list_init(void);

View file

@ -31,7 +31,7 @@ void simple_archiver_priority_heap_internal_realloc(
new_priority_heap->nodes =
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) {
simple_archiver_priority_heap_insert(
&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) {
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].data_cleanup_fn) {
(*priority_heap)
@ -109,7 +109,7 @@ void simple_archiver_priority_heap_insert(SDArchiverPHeap **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 &&
(*priority_heap)
@ -146,7 +146,7 @@ void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap) {
SDArchiverPHNode end = priority_heap->nodes[priority_heap->size];
priority_heap->nodes[priority_heap->size].is_valid = 0;
unsigned int hole = 1;
size_t hole = 1;
while (hole * 2 + 1 <= priority_heap->size) {
if (priority_heap->nodes[hole * 2].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 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) {
char *found_buf = ud;
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,
__attribute__((unused)) unsigned int key_size,
__attribute__((unused)) size_t key_size,
__attribute__((unused)) const void *value,
__attribute__((unused)) void *ud) {
return 2;
@ -145,8 +145,8 @@ int main(void) {
key = malloc(sizeof(int));
*value = idx;
*key = idx;
simple_archiver_hash_map_insert(&hash_map, value, key, sizeof(int),
NULL, NULL);
simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int), NULL,
NULL);
}
}
@ -184,7 +184,7 @@ int main(void) {
*copy_value = idx;
unsigned int *copy_key = malloc(sizeof(unsigned int));
*copy_key = idx;
simple_archiver_hash_map_insert(&hash_map, copy_value, copy_key,
simple_archiver_hash_map_insert(hash_map, copy_value, copy_key,
sizeof(unsigned int), NULL, NULL);
}
simple_archiver_hash_map_free(&hash_map);
@ -193,7 +193,7 @@ int main(void) {
hash_map = simple_archiver_hash_map_init();
for (size_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
simple_archiver_hash_map_insert(&hash_map, (void *)idx, &idx,
simple_archiver_hash_map_insert(hash_map, (void *)idx, &idx,
sizeof(size_t), no_free_fn, no_free_fn);
}

View file

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

View file

@ -50,10 +50,10 @@ int simple_archiver_helper_make_dirs(const char *file_path);
/// Returns non-NULL on success.
/// Must be free'd with "free()" if non-NULL.
/// start_idx is inclusive and end_idx is exclusive.
char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx,
unsigned int end_idx);
char *simple_archiver_helper_cut_substr(const char *s, size_t start_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_malloced(void **data);

View file

@ -39,15 +39,15 @@
#include "parser_internal.h"
/// 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) {
unsigned int idx = 0;
unsigned int known_good_idx = 0;
const unsigned int length = strlen(filename);
size_t idx = 0;
size_t known_good_idx = 0;
const size_t length = strlen(filename);
// 0b0001 - checked that idx char is '.'
// 0b0010 - checked that idx char is '/'
unsigned int flags = 0;
size_t flags = 0;
for (; idx < length; ++idx) {
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) {
int len = strlen(filename);
int idx;
size_t len = strlen(filename);
size_t idx;
for (idx = len; idx-- > 0;) {
if (filename[idx] != '/') {
++idx;
@ -236,7 +236,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
out->filename = NULL;
} else {
out->flags &= 0xFFFFFFEF;
int size = strlen(argv[1]) + 1;
size_t size = strlen(argv[1]) + 1;
out->filename = malloc(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();
return 1;
}
int size = strlen(argv[1]) + 1;
size_t size = strlen(argv[1]) + 1;
out->compressor = malloc(size);
strncpy(out->compressor, argv[1], size);
--argc;
@ -268,7 +268,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
simple_archiver_print_usage();
return 1;
}
int size = strlen(argv[1]) + 1;
size_t size = strlen(argv[1]) + 1;
out->decompressor = malloc(size);
strncpy(out->decompressor, argv[1], size);
--argc;
@ -297,15 +297,15 @@ int simple_archiver_parse_args(int argc, const char **argv,
} else {
if (out->working_files == NULL) {
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]);
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);
strncpy(out->working_files[0], argv[0] + arg_idx, arg_length);
simple_archiver_parser_internal_remove_end_slash(out->working_files[0]);
out->working_files[1] = NULL;
} else {
int working_size = 1;
size_t working_size = 1;
char **ptr = out->working_files;
while (ptr && *ptr) {
++working_size;
@ -318,9 +318,9 @@ int simple_archiver_parse_args(int argc, const char **argv,
// Set new actual last element to 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]);
int size = strlen(argv[0] + arg_idx) + 1;
size_t size = strlen(argv[0] + arg_idx) + 1;
// Set last element to the arg.
out->working_files[working_size - 1] = malloc(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);
if ((st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFMT) == S_IFLNK) {
// 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);
strncpy(filename, file_path, len);
if (simple_archiver_hash_map_get(hash_map, filename, len - 1) == NULL) {
@ -442,7 +442,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
simple_archiver_list_add(files_list, file_info,
simple_archiver_internal_free_file_info_fn);
simple_archiver_hash_map_insert(
&hash_map, &hash_map_sentinel, filename, len - 1,
hash_map, &hash_map_sentinel, filename, len - 1,
simple_archiver_helper_datastructure_cleanup_nop,
simple_archiver_helper_datastructure_cleanup_nop);
} else {
@ -472,11 +472,11 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
}
// fprintf(stderr, "dir entry in %s is %s\n", next,
// 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);
snprintf(combined_path, combined_size, "%s/%s", next,
dir_entry->d_name);
unsigned int valid_idx =
size_t valid_idx =
simple_archiver_parser_internal_get_first_non_current_idx(
combined_path);
if (valid_idx > 0) {
@ -541,7 +541,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
files_list, file_info,
simple_archiver_internal_free_file_info_fn);
simple_archiver_hash_map_insert(
&hash_map, &hash_map_sentinel, combined_path,
hash_map, &hash_map_sentinel, combined_path,
combined_size - 1,
simple_archiver_helper_datastructure_cleanup_nop,
simple_archiver_helper_datastructure_cleanup_nop);
@ -574,11 +574,10 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
SDArchiverFileInfo *file_info = iter->data;
// Remove leading "./" entries from files_list.
unsigned int idx =
simple_archiver_parser_internal_get_first_non_current_idx(
file_info->filename);
size_t idx = simple_archiver_parser_internal_get_first_non_current_idx(
file_info->filename);
if (idx > 0) {
int len = strlen(file_info->filename) + 1 - idx;
size_t len = strlen(file_info->filename) + 1 - idx;
char *substr = malloc(len);
strncpy(substr, file_info->filename + idx, len);
free(file_info->filename);

View file

@ -21,7 +21,7 @@
#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);
#endif