From 2d8efc08c41f368b01bc354030bf2c9ae47bd99c Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 18 Feb 2025 13:10:14 +0900 Subject: [PATCH] Refactor white/black-list code --- src/archiver.c | 49 +++++++------------------------------------------ src/helpers.c | 43 +++++++++++++++++++------------------------ src/helpers.h | 8 ++------ 3 files changed, 28 insertions(+), 72 deletions(-) diff --git a/src/archiver.c b/src/archiver.c index 6a4005c..b4f4f3c 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -1708,12 +1708,7 @@ int symlinks_and_files_from_files(void *data, void *ud) { if (!simple_archiver_helper_string_allowed_lists( file_info->filename, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends)) { + state->parsed)) { return 0; } @@ -7043,12 +7038,7 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract, const uint_fast8_t lists_allowed = simple_archiver_helper_string_allowed_lists( link_name, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends); + state->parsed); if (lists_allowed) { fprintf(stderr, "SYMLINK %3" PRIu32 " of %3" PRIu32 "\n", idx + 1, u32); @@ -7399,12 +7389,7 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract, if (simple_archiver_helper_string_allowed_lists( file_info->filename, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends)) { + state->parsed)) { file_info->other_flags |= 2; } @@ -8069,12 +8054,7 @@ int simple_archiver_parse_archive_version_2(FILE *in_f, int_fast8_t do_extract, const uint_fast8_t lists_allowed = simple_archiver_helper_string_allowed_lists( buf, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends); + state->parsed); uint8_t perms_flags[4]; if (fread(perms_flags, 1, 2, in_f) != 2) { @@ -8429,12 +8409,7 @@ int simple_archiver_parse_archive_version_3(FILE *in_f, const uint_fast8_t lists_allowed = simple_archiver_helper_string_allowed_lists( link_name, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends); + state->parsed); if (!do_extract && lists_allowed) { fprintf(stderr, "SYMLINK %3" PRIu32 " of %3" PRIu32 "\n", idx + 1, count); @@ -9044,12 +9019,7 @@ int simple_archiver_parse_archive_version_3(FILE *in_f, } else if (simple_archiver_helper_string_allowed_lists( file_info->filename, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends)) { + state->parsed)) { file_info->other_flags |= 2; } @@ -9776,12 +9746,7 @@ int simple_archiver_parse_archive_version_3(FILE *in_f, simple_archiver_helper_string_allowed_lists( archive_dir_name, state->parsed->flags & 0x20000 ? 1 : 0, - state->parsed->whitelist_contains, - state->parsed->whitelist_begins, - state->parsed->whitelist_ends, - state->parsed->blacklist_contains, - state->parsed->blacklist_begins, - state->parsed->blacklist_ends); + state->parsed); uint8_t perms_flags[4]; if (fread(perms_flags, 1, 2, in_f) != 2) { diff --git a/src/helpers.c b/src/helpers.c index 7f5ac95..d8e1068 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -722,15 +722,10 @@ uint_fast8_t simple_archiver_helper_string_ends(const char *cstring, uint_fast8_t simple_archiver_helper_string_allowed_lists( const char *cstring, uint_fast8_t case_i, - const SDArchiverLinkedList *w_contains, - const SDArchiverLinkedList *w_begins, - const SDArchiverLinkedList *w_ends, - const SDArchiverLinkedList *b_contains, - const SDArchiverLinkedList *b_begins, - const SDArchiverLinkedList *b_ends) { - if (w_contains) { - for (const SDArchiverLLNode *node = w_contains->head->next; - node != w_contains->tail; + const SDArchiverParsed *parsed) { + if (parsed->whitelist_contains) { + for (const SDArchiverLLNode *node = parsed->whitelist_contains->head->next; + node != parsed->whitelist_contains->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_contains( @@ -740,9 +735,9 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( } } } - if (w_begins) { - for (const SDArchiverLLNode *node = w_begins->head->next; - node != w_begins->tail; + if (parsed->whitelist_begins) { + for (const SDArchiverLLNode *node = parsed->whitelist_begins->head->next; + node != parsed->whitelist_begins->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_starts( @@ -752,9 +747,9 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( } } } - if (w_ends) { - for (const SDArchiverLLNode *node = w_ends->head->next; - node != w_ends->tail; + if (parsed->whitelist_ends) { + for (const SDArchiverLLNode *node = parsed->whitelist_ends->head->next; + node != parsed->whitelist_ends->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_ends(cstring, node->data, case_i)) { @@ -764,10 +759,10 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( } } - if (b_contains) { + if (parsed->blacklist_contains) { uint_fast8_t contains_all = 1; - for (const SDArchiverLLNode *node = b_contains->head->next; - node != b_contains->tail; + for (const SDArchiverLLNode *node = parsed->blacklist_contains->head->next; + node != parsed->blacklist_contains->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_contains( @@ -782,9 +777,9 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( return 0; } } - if (b_begins) { - for (const SDArchiverLLNode *node = b_begins->head->next; - node != b_begins->tail; + if (parsed->blacklist_begins) { + for (const SDArchiverLLNode *node = parsed->blacklist_begins->head->next; + node != parsed->blacklist_begins->tail; node = node->next) { if (node->data) { if (simple_archiver_helper_string_starts(cstring, node->data, case_i)) { @@ -793,9 +788,9 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( } } } - if (b_ends) { - for (const SDArchiverLLNode *node = b_ends->head->next; - node != b_ends->tail; + if (parsed->blacklist_ends) { + for (const SDArchiverLLNode *node = parsed->blacklist_ends->head->next; + node != parsed->blacklist_ends->tail; node = node->next) { if (node->data) { if (simple_archiver_helper_string_ends(cstring, node->data, case_i)) { diff --git a/src/helpers.h b/src/helpers.h index 04936cd..676e1a8 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -25,6 +25,7 @@ // Local includes. #include "data_structures/linked_list.h" +#include "parser.h" static const uint32_t MAX_SYMBOLIC_LINK_SIZE = 512; @@ -142,11 +143,6 @@ uint_fast8_t simple_archiver_helper_string_ends(const char *cstring, uint_fast8_t simple_archiver_helper_string_allowed_lists( const char *cstring, uint_fast8_t case_i, - const SDArchiverLinkedList *w_contains, - const SDArchiverLinkedList *w_begins, - const SDArchiverLinkedList *w_ends, - const SDArchiverLinkedList *b_contains, - const SDArchiverLinkedList *b_begins, - const SDArchiverLinkedList *b_ends); + const SDArchiverParsed *parsed); #endif -- 2.49.0