// Write file to pipe, and read from other pipe.
char write_buf[1024];
char read_buf[1024];
- int write_again = 0;
- int write_done = 0;
- int read_done = 0;
+ int_fast8_t write_again = 0;
+ int_fast8_t write_done = 0;
+ int_fast8_t read_done = 0;
size_t write_count;
size_t read_count;
ssize_t ret;
temp_to_write->buf = malloc(4);
temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
- ((unsigned char *)temp_to_write->buf)[idx] = 0;
+ ((uint8_t *)temp_to_write->buf)[idx] = 0;
}
// Get file stats.
}
if ((stat_buf.st_mode & S_IRUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
}
if ((stat_buf.st_mode & S_IWUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x4;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
}
if ((stat_buf.st_mode & S_IXUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x8;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
}
if ((stat_buf.st_mode & S_IRGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x10;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
}
if ((stat_buf.st_mode & S_IWGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x20;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
}
if ((stat_buf.st_mode & S_IXGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x40;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
}
if ((stat_buf.st_mode & S_IROTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x80;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
}
if ((stat_buf.st_mode & S_IWOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x1;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
}
if ((stat_buf.st_mode & S_IXOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
}
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
temp_to_write->buf = malloc(4);
temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
- ((unsigned char *)temp_to_write->buf)[idx] = 0;
+ ((uint8_t *)temp_to_write->buf)[idx] = 0;
}
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
}
if ((stat_buf.st_mode & S_IRUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
}
if ((stat_buf.st_mode & S_IWUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x4;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
}
if ((stat_buf.st_mode & S_IXUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x8;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
}
if ((stat_buf.st_mode & S_IRGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x10;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
}
if ((stat_buf.st_mode & S_IWGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x20;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
}
if ((stat_buf.st_mode & S_IXGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x40;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
}
if ((stat_buf.st_mode & S_IROTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x80;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
}
if ((stat_buf.st_mode & S_IWOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x1;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
}
if ((stat_buf.st_mode & S_IXOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
}
#else
// Unsupported platform. Just set the permission bits for user.
- ((unsigned char *)temp_to_write->buf)[0] |= 0xE;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0xE;
#endif
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
temp_to_write->buf = malloc(4);
temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
- ((unsigned char *)temp_to_write->buf)[idx] = 0;
+ ((uint8_t *)temp_to_write->buf)[idx] = 0;
}
// Set "is symbolic link" flag.
- ((unsigned char *)temp_to_write->buf)[0] = 1;
+ ((uint8_t *)temp_to_write->buf)[0] = 1;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
}
if ((stat_buf.st_mode & S_IRUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
}
if ((stat_buf.st_mode & S_IWUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x4;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
}
if ((stat_buf.st_mode & S_IXUSR) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x8;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
}
if ((stat_buf.st_mode & S_IRGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x10;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
}
if ((stat_buf.st_mode & S_IWGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x20;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
}
if ((stat_buf.st_mode & S_IXGRP) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x40;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
}
if ((stat_buf.st_mode & S_IROTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[0] |= 0x80;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
}
if ((stat_buf.st_mode & S_IWOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x1;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
}
if ((stat_buf.st_mode & S_IXOTH) != 0) {
- ((unsigned char *)temp_to_write->buf)[1] |= 0x2;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
}
#else
// Unsupported platform. Just set the permission bits for user.
- ((unsigned char *)temp_to_write->buf)[0] |= 0xE;
+ ((uint8_t *)temp_to_write->buf)[0] |= 0xE;
#endif
// Need to get abs_path for checking/setting a flag before storing flags.
// link_substr, dest_substr);
// Generate the relative path.
- int has_slash = 0;
+ int_fast8_t has_slash = 0;
idx = 0;
do {
for (; link_substr[idx] != '/' && link_substr[idx] != 0; ++idx);
fprintf(stderr,
"NOTICE: abs_path exists, \"--no-abs-symlink\" not specified, "
"and link refers to file NOT in archive; preferring abs_path.\n");
- ((unsigned char *)temp_to_write->buf)[1] |= 0x4;
+ ((uint8_t *)temp_to_write->buf)[1] |= 0x4;
}
// Store the 4 byte bit-flags for file.
return SDAS_NO_COMPRESSOR;
} else if (state->parsed->compressor && state->parsed->decompressor) {
// Write the four flag bytes with first bit set.
- unsigned char c = 1;
+ uint8_t c = 1;
if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE;
}
}
} else {
// Write the four flag bytes with first bit NOT set.
- unsigned char c = 0;
+ uint8_t c = 0;
for (size_t i = 0; i < 4; ++i) {
if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE;
return SDAS_SUCCESS;
}
-int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
+int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state) {
- unsigned char buf[1024];
+ uint8_t buf[1024];
memset(buf, 0, 1024);
uint16_t u16;
uint32_t u32;
uint64_t u64;
- int is_compressed = 0;
+ int_fast8_t is_compressed = 0;
if (fread(buf, 1, 18, in_f) != 18) {
return SDAS_INVALID_FILE;
__attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
- unsigned char *uc_heap_buf = heap_buf;
+ uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE;
}
__attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
- unsigned char *uc_heap_buf = heap_buf;
+ uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE;
}
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;
+ int_fast8_t skip = 0;
__attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *hash_map = NULL;
if (state && state->parsed->working_files &&
__attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
- unsigned char *uc_heap_buf = heap_buf;
+ uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE;
}
fprintf(stderr, " File size: %lu\n", u64);
}
- int skip_due_to_map = 0;
+ int_fast8_t skip_due_to_map = 0;
if (hash_map != NULL && out_f_name) {
if (simple_archiver_hash_map_get(hash_map, out_f_name,
strlen(out_f_name) + 1) == NULL) {
}
uint64_t compressed_file_size = u64;
- int write_again = 0;
- int write_pipe_done = 0;
- int read_pipe_done = 0;
+ int_fast8_t write_again = 0;
+ int_fast8_t write_pipe_done = 0;
+ int_fast8_t read_pipe_done = 0;
size_t fread_ret;
char recv_buf[1024];
size_t amount_to_read;
} else {
// Is a symbolic link.
- int abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0;
+ int_fast8_t abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0;
fprintf(stderr, " Absolute path is %s\n",
(abs_preferred ? "preferred" : "NOT preferred"));
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
+// Standard library includes.
#include <stdio.h>
+#include <stdint.h>
+// Local includes.
#include "data_structures/hash_map.h"
#include "data_structures/linked_list.h"
#include "parser.h"
typedef struct SDArchiverState {
/*
*/
- unsigned int flags;
+ uint32_t flags;
const SDArchiverParsed *parsed;
FILE *out_f;
SDArchiverHashMap *map;
const SDArchiverLinkedList *filenames);
/// Returns zero on success.
-int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
+int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state);
/// Returns zero on success.
unsigned long long temp = 0;
size_t count = 0;
for (size_t idx = 0; idx < key_size; ++idx) {
- temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count);
+ temp |= ((unsigned long long)*((uint8_t *)key + idx)) << (8 * count);
++count;
if (count >= 8) {
count = 0;
#define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32
+// Standard library includes.
#include <stddef.h>
+#include <stdint.h>
+// Local includes.
#include "linked_list.h"
typedef struct SDArchiverHashMap {
#include "linked_list.h"
+// Standard library includes.
#include <stdlib.h>
+#include <stdint.h>
SDArchiverLinkedList *simple_archiver_list_init(void) {
SDArchiverLinkedList *list = malloc(sizeof(SDArchiverLinkedList));
return 0;
}
- int removed_count = 0;
+ int32_t removed_count = 0;
SDArchiverLLNode *node = list->head;
- int iter_removed = 0;
+ int32_t iter_removed = 0;
while (node) {
if (iter_removed == 0) {
node = node->next;
*priority_heap = new_priority_heap;
}
-int simple_archiver_priority_heap_default_less(long long a, long long b) {
+int simple_archiver_priority_heap_default_less(int64_t a, int64_t b) {
return a < b ? 1 : 0;
}
}
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
- int (*less_fn)(long long, long long)) {
+ int (*less_fn)(int64_t, int64_t)) {
SDArchiverPHeap *priority_heap = malloc(sizeof(SDArchiverPHeap));
priority_heap->capacity = SC_SA_DS_PRIORITY_HEAP_START_SIZE;
}
void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
- long long priority, void *data,
+ int64_t priority, void *data,
void (*data_cleanup_fn)(void *)) {
if (!priority_heap) {
return;
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
+// Standard library includes.
+#include <stdint.h>
+
#define SC_SA_DS_PRIORITY_HEAP_START_SIZE 32
typedef struct SDArchiverPHNode {
- long long priority;
+ int64_t priority;
void *data;
void (*data_cleanup_fn)(void *);
/// Is non-zero if valid.
typedef struct SDArchiverPHeap {
SDArchiverPHNode *nodes;
- unsigned long long capacity;
- unsigned long long size;
- int (*less_fn)(long long, long long);
+ uint64_t capacity;
+ uint64_t size;
+ int (*less_fn)(int64_t, int64_t);
} SDArchiverPHeap;
/// Default "less" function to determine if a has higher priority than b.
/// Returns non-zero if "less".
-int simple_archiver_priority_heap_default_less(long long a, long long b);
+int simple_archiver_priority_heap_default_less(int64_t a, int64_t b);
SDArchiverPHeap *simple_archiver_priority_heap_init(void);
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
- int (*less_fn)(long long, long long));
+ int (*less_fn)(int64_t, int64_t));
/// It is recommended to use the double-pointer version of priority-heap free
/// as that will ensure the variable holding the pointer will end up pointing
/// If data_cleanup_fn is NULL, then "free()" is used on data when freed.
void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
- long long priority, void *data,
+ int64_t priority, void *data,
void (*data_cleanup_fn)(void *));
/// Returns NULL if empty or if priority_heap is NULL.
//
// `data_structures/test.c` is the source for testing data structure code.
+// Standard library includes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
+// Local includes.
#include "../algorithms/linear_congruential_gen.h"
#include "hash_map.h"
#include "linked_list.h"
#define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100
-static int checks_checked = 0;
-static int checks_passed = 0;
+static int32_t checks_checked = 0;
+static int32_t checks_passed = 0;
#define CHECK_TRUE(x) \
do { \
return strcmp(data, "three") == 0 ? 1 : 0;
}
-int more_fn(long long a, long long b) { return a > b ? 1 : 0; }
+int more_fn(int64_t a, int64_t b) { return a > b ? 1 : 0; }
int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
__attribute__((unused)) size_t key_size,
hash_map = simple_archiver_hash_map_init();
{
- int *value, *key;
+ int32_t *value, *key;
- for (unsigned int idx = 0; idx < 20; ++idx) {
- value = malloc(sizeof(int));
- key = malloc(sizeof(int));
+ for (uint32_t idx = 0; idx < 20; ++idx) {
+ value = malloc(sizeof(int32_t));
+ key = malloc(sizeof(int32_t));
*value = idx;
*key = idx;
- simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int), NULL,
+ simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int32_t), NULL,
NULL);
}
}
- int value, key;
+ int32_t value, key;
void *value_ptr;
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
- value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int));
+ value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
CHECK_TRUE(value_ptr != NULL);
- CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0);
+ CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
}
key = 5;
- simple_archiver_hash_map_remove(hash_map, &key, sizeof(int));
+ simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
key = 15;
- simple_archiver_hash_map_remove(hash_map, &key, sizeof(int));
+ simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
- value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int));
+ value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
if (key != 5 && key != 15) {
CHECK_TRUE(value_ptr != NULL);
- CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0);
+ CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
} else {
CHECK_TRUE(value_ptr == NULL);
}
// Rehash test for Memcheck.
hash_map = simple_archiver_hash_map_init();
- for (unsigned int idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1;
+ for (uint32_t idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1;
++idx) {
- unsigned int *copy_value = malloc(sizeof(unsigned int));
+ uint32_t *copy_value = malloc(sizeof(uint32_t));
*copy_value = idx;
- unsigned int *copy_key = malloc(sizeof(unsigned int));
+ uint32_t *copy_key = malloc(sizeof(uint32_t));
*copy_key = idx;
simple_archiver_hash_map_insert(hash_map, copy_value, copy_key,
- sizeof(unsigned int), NULL, NULL);
+ sizeof(uint32_t), NULL, NULL);
}
simple_archiver_hash_map_free(&hash_map);
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
found) == 0);
- for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
+ for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
++idx) {
CHECK_TRUE(found[idx] == 1);
}
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn2,
found) == 2);
- for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
+ for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
++idx) {
CHECK_TRUE(found[idx] == 1);
}
priority_heap = simple_archiver_priority_heap_init();
// Just 3 elements.
- for (unsigned int idx = 0; idx < 3; ++idx) {
- unsigned int *data = malloc(sizeof(unsigned int));
+ for (uint32_t idx = 0; idx < 3; ++idx) {
+ uint32_t *data = malloc(sizeof(uint32_t));
*data = idx;
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
}
- for (unsigned int idx = 0; idx < 3; ++idx) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = 0; idx < 3; ++idx) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data);
}
// 100 elements.
- unsigned int max = 100;
+ uint32_t max = 100;
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = malloc(sizeof(unsigned int));
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = malloc(sizeof(uint32_t));
*data = idx;
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
}
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx);
}
// Insert in reverse order.
- for (unsigned int idx = max; idx-- > 0;) {
- unsigned int *data = malloc(sizeof(unsigned int));
+ for (uint32_t idx = max; idx-- > 0;) {
+ uint32_t *data = malloc(sizeof(uint32_t));
*data = idx;
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
}
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx);
}
// Insert in random order.
- unsigned int *array = malloc(sizeof(unsigned int) * max);
- for (unsigned int idx = 0; idx < max; ++idx) {
+ uint32_t *array = malloc(sizeof(uint32_t) * max);
+ for (uint32_t idx = 0; idx < max; ++idx) {
array[idx] = idx;
}
// Deterministic randomization.
- for (unsigned int idx = max - 1; idx-- > 0;) {
- unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) %
+ for (uint32_t idx = max - 1; idx-- > 0;) {
+ uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
(unsigned long long)(idx + 1);
if (max - 1 != other_idx) {
- unsigned int temp = array[max - 1];
+ uint32_t temp = array[max - 1];
array[max - 1] = array[other_idx];
array[other_idx] = temp;
}
}
// Insert the deterministically randomized array.
- for (unsigned int idx = 0; idx < max; ++idx) {
+ for (uint32_t idx = 0; idx < max; ++idx) {
simple_archiver_priority_heap_insert(priority_heap, array[idx],
array + idx, no_free_fn);
}
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data);
// Insert, don't pop, do free, for memcheck.
priority_heap = simple_archiver_priority_heap_init();
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = malloc(sizeof(unsigned int));
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = malloc(sizeof(uint32_t));
*data = idx;
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
}
// Reverse priority.
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
- for (unsigned int idx = 0; idx < max; ++idx) {
- unsigned int *data = malloc(sizeof(unsigned int));
+ for (uint32_t idx = 0; idx < max; ++idx) {
+ uint32_t *data = malloc(sizeof(uint32_t));
*data = idx;
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
}
- for (unsigned int idx = max; idx-- > 0;) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = max; idx-- > 0;) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx);
// Insert in random order with reverse-priority-heap.
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
- array = malloc(sizeof(unsigned int) * max);
- for (unsigned int idx = 0; idx < max; ++idx) {
+ array = malloc(sizeof(uint32_t) * max);
+ for (uint32_t idx = 0; idx < max; ++idx) {
array[idx] = idx;
}
// Deterministic randomization.
- for (unsigned int idx = max - 1; idx-- > 0;) {
- unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) %
+ for (uint32_t idx = max - 1; idx-- > 0;) {
+ uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
(unsigned long long)(idx + 1);
if (max - 1 != other_idx) {
- unsigned int temp = array[max - 1];
+ uint32_t temp = array[max - 1];
array[max - 1] = array[other_idx];
array[other_idx] = temp;
}
}
// Insert the deterministically randomized array.
- for (unsigned int idx = 0; idx < max; ++idx) {
+ for (uint32_t idx = 0; idx < max; ++idx) {
simple_archiver_priority_heap_insert(priority_heap, array[idx],
array + idx, no_free_fn);
}
- for (unsigned int idx = max; idx-- > 0;) {
- unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
+ for (uint32_t idx = max; idx-- > 0;) {
+ uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx);
if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data);
void simple_archiver_helper_16_bit_be(uint16_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) {
- unsigned char c = ((unsigned char *)value)[0];
- ((unsigned char *)value)[0] = ((unsigned char *)value)[1];
- ((unsigned char *)value)[1] = c;
+ uint8_t c = ((uint8_t *)value)[0];
+ ((uint8_t *)value)[0] = ((uint8_t *)value)[1];
+ ((uint8_t *)value)[1] = c;
}
}
void simple_archiver_helper_32_bit_be(uint32_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) {
- for (unsigned int i = 0; i < 2; ++i) {
- unsigned char c = ((unsigned char *)value)[i];
- ((unsigned char *)value)[i] = ((unsigned char *)value)[3 - i];
- ((unsigned char *)value)[3 - i] = c;
+ for (uint32_t i = 0; i < 2; ++i) {
+ uint8_t c = ((uint8_t *)value)[i];
+ ((uint8_t *)value)[i] = ((uint8_t *)value)[3 - i];
+ ((uint8_t *)value)[3 - i] = c;
}
}
}
void simple_archiver_helper_64_bit_be(uint64_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) {
- for (unsigned int i = 0; i < 4; ++i) {
- unsigned char c = ((unsigned char *)value)[i];
- ((unsigned char *)value)[i] = ((unsigned char *)value)[7 - i];
- ((unsigned char *)value)[7 - i] = c;
+ for (uint32_t i = 0; i < 4; ++i) {
+ uint8_t c = ((uint8_t *)value)[i];
+ ((uint8_t *)value)[i] = ((uint8_t *)value)[7 - i];
+ ((uint8_t *)value)[7 - i] = c;
}
}
}
char **simple_archiver_helper_cmd_string_to_argv(const char *cmd) {
- unsigned int capacity = 16;
- unsigned int idx = 0;
+ uint32_t capacity = 16;
+ uint32_t idx = 0;
// Size of every pointer is the same, so using size of (void*) should be ok.
char **args = malloc(sizeof(void *) * capacity);
memset(args, 0, sizeof(void *) * capacity);
- unsigned int word_capacity = 16;
- unsigned int word_idx = 0;
+ uint32_t word_capacity = 16;
+ uint32_t word_idx = 0;
char *word = malloc(word_capacity);
memset(word, 0, word_capacity);
for (const char *c = cmd; *c != 0; ++c) {
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
+// Standard library includes.
#include <stdint.h>
#include <stdio.h>
-static const unsigned int MAX_SYMBOLIC_LINK_SIZE = 512;
+static const uint32_t MAX_SYMBOLIC_LINK_SIZE = 512;
/// Returns non-zero if this system is big-endian.
int simple_archiver_helper_is_big_endian(void);
--argc;
++argv;
- int is_remaining_args = 0;
+ int_fast8_t is_remaining_args = 0;
while (argc > 0) {
if (!is_remaining_args) {
}
if (parsed->working_files) {
char **ptr = parsed->working_files;
- unsigned int idx = 0;
+ uint32_t idx = 0;
while (ptr[idx]) {
free(ptr[idx]);
++idx;
}
// Remove "./" entries inside the file path.
- int slash_found = 0;
- int dot_found = 0;
+ int_fast8_t slash_found = 0;
+ int_fast8_t dot_found = 0;
for (idx = strlen(file_info->filename); idx-- > 0;) {
if (file_info->filename[idx] == '/') {
if (dot_found) {
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
+// Standard library includes.
+#include <stdint.h>
+
+// Local includes.
#include "data_structures/linked_list.h"
typedef struct SDArchiverParsed {
/// 0b xxxx 1xxx - Allow extract overwrite.
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
/// 0b xx1x xxxx - Do not save absolute paths for symlinks.
- unsigned int flags;
+ uint32_t flags;
/// Null-terminated string.
char *filename;
/// Null-terminated string.
//
// `test.c` is the source for testing code.
+// Standard library includes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
+// Local includes.
#include "helpers.h"
#include "parser_internal.h"
-static int checks_checked = 0;
-static int checks_passed = 0;
+static int32_t checks_checked = 0;
+static int32_t checks_passed = 0;
#define CHECK_TRUE(x) \
do { \
int main(void) {
// Test parser.
{
- unsigned int idx =
+ size_t idx =
simple_archiver_parser_internal_get_first_non_current_idx("test");
CHECK_TRUE(idx == 0);
// Only if system is little-endian.
if (simple_archiver_helper_is_big_endian() == 0) {
uint16_t u16 = 0x0102;
- CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
- CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
+ CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
+ CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
simple_archiver_helper_16_bit_be(&u16);
- CHECK_TRUE(((unsigned char *)&u16)[0] == 1);
- CHECK_TRUE(((unsigned char *)&u16)[1] == 2);
+ CHECK_TRUE(((uint8_t *)&u16)[0] == 1);
+ CHECK_TRUE(((uint8_t *)&u16)[1] == 2);
simple_archiver_helper_16_bit_be(&u16);
- CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
- CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
+ CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
+ CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
uint32_t u32 = 0x01020304;
- CHECK_TRUE(((unsigned char *)&u32)[0] == 4);
- CHECK_TRUE(((unsigned char *)&u32)[1] == 3);
- CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
- CHECK_TRUE(((unsigned char *)&u32)[3] == 1);
+ CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
+ CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
+ CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
+ CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
simple_archiver_helper_32_bit_be(&u32);
- CHECK_TRUE(((unsigned char *)&u32)[0] == 1);
- CHECK_TRUE(((unsigned char *)&u32)[1] == 2);
- CHECK_TRUE(((unsigned char *)&u32)[2] == 3);
- CHECK_TRUE(((unsigned char *)&u32)[3] == 4);
+ CHECK_TRUE(((uint8_t *)&u32)[0] == 1);
+ CHECK_TRUE(((uint8_t *)&u32)[1] == 2);
+ CHECK_TRUE(((uint8_t *)&u32)[2] == 3);
+ CHECK_TRUE(((uint8_t *)&u32)[3] == 4);
simple_archiver_helper_32_bit_be(&u32);
- CHECK_TRUE(((unsigned char *)&u32)[0] == 4);
- CHECK_TRUE(((unsigned char *)&u32)[1] == 3);
- CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
- CHECK_TRUE(((unsigned char *)&u32)[3] == 1);
+ CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
+ CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
+ CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
+ CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
uint64_t u64 = 0x010203040a0b0c0d;
- CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd);
- CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc);
- CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb);
- CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa);
- CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4);
- CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3);
- CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2);
- CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1);
+ CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
+ CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
+ CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
+ CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
+ CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
+ CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
+ CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
+ CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
simple_archiver_helper_64_bit_be(&u64);
- CHECK_TRUE(((unsigned char *)&u64)[0] == 0x1);
- CHECK_TRUE(((unsigned char *)&u64)[1] == 0x2);
- CHECK_TRUE(((unsigned char *)&u64)[2] == 0x3);
- CHECK_TRUE(((unsigned char *)&u64)[3] == 0x4);
- CHECK_TRUE(((unsigned char *)&u64)[4] == 0xa);
- CHECK_TRUE(((unsigned char *)&u64)[5] == 0xb);
- CHECK_TRUE(((unsigned char *)&u64)[6] == 0xc);
- CHECK_TRUE(((unsigned char *)&u64)[7] == 0xd);
+ CHECK_TRUE(((uint8_t *)&u64)[0] == 0x1);
+ CHECK_TRUE(((uint8_t *)&u64)[1] == 0x2);
+ CHECK_TRUE(((uint8_t *)&u64)[2] == 0x3);
+ CHECK_TRUE(((uint8_t *)&u64)[3] == 0x4);
+ CHECK_TRUE(((uint8_t *)&u64)[4] == 0xa);
+ CHECK_TRUE(((uint8_t *)&u64)[5] == 0xb);
+ CHECK_TRUE(((uint8_t *)&u64)[6] == 0xc);
+ CHECK_TRUE(((uint8_t *)&u64)[7] == 0xd);
simple_archiver_helper_64_bit_be(&u64);
- CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd);
- CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc);
- CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb);
- CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa);
- CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4);
- CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3);
- CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2);
- CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1);
+ CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
+ CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
+ CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
+ CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
+ CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
+ CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
+ CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
+ CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
}
}
// Test helpers cut substr.
{
const char *s = "one two three.";
- unsigned int s_len = strlen(s);
+ uint32_t s_len = strlen(s);
// Invalid range.
char *out = simple_archiver_helper_cut_substr(s, 1, 0);
CHECK_FALSE(out);