Skip to content

Commit

Permalink
Convert structs to typedefs: sequences and filters (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharple committed Mar 2, 2021
1 parent 918478f commit 932c3b7
Show file tree
Hide file tree
Showing 31 changed files with 634 additions and 791 deletions.
8 changes: 4 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ noinst_PROGRAMS = \

detox_SOURCES = \
builtin_table.c \
config_file.c \
config_file_yacc.y \
config_file_lex.l \
clean_string.c \
config_file.c \
config_file_dump.c \
config_file_lex.l \
config_file_spoof.c \
config_file_yacc.y \
detox.c \
detox_struct.c \
file.c \
filelist.c \
filter.c \
parse_options.c \
parse_table.c \
sequence.c \
Expand Down
45 changes: 12 additions & 33 deletions src/clean_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@
/*
* Translates ISO 8859-1 characters (Latin-1) into lower ASCII characters.
*/
char *clean_iso8859_1(char *filename, struct clean_string_options *options)
char *clean_iso8859_1(char *filename, table_t *table)
{
char *output, *input_walk, *output_walk, *replace_walk;
int new_value;

table_t *table = NULL;

if (filename == NULL) {
return NULL;
}

if (options == NULL || options->table == NULL) {
if (table == NULL) {
fprintf(stderr, "internal error\n");
exit(EXIT_FAILURE);
}

table = options->table;

output = malloc((strlen(filename) * table->max_data_length) + 1);
if (output == NULL) {
fprintf(stderr, "out of memory: %s\n", strerror(errno));
Expand Down Expand Up @@ -85,23 +81,19 @@ char *clean_iso8859_1(char *filename, struct clean_string_options *options)
/*
* Translates unsafe characters using a translation table.
*/
char *clean_safe(char *filename, struct clean_string_options *options)
char *clean_safe(char *filename, table_t *table)
{
char *output, *input_walk, *output_walk, *replace_walk;

table_t *table = NULL;

if (filename == NULL) {
return NULL;
}

if (options == NULL || options->table == NULL) {
if (table == NULL) {
fprintf(stderr, "internal error\n");
exit(EXIT_FAILURE);
}

table = options->table;

output = malloc((strlen(filename) * table->max_data_length) + 1);
if (output == NULL) {
fprintf(stderr, "out of memory: %s\n", strerror(errno));
Expand Down Expand Up @@ -143,7 +135,7 @@ char *clean_safe(char *filename, struct clean_string_options *options)
* Cleans up any CGI encoded characters, in the form "%" followed by 2 hex
* digits.
*/
char *clean_uncgi(char *filename, struct clean_string_options *options)
char *clean_uncgi(char *filename)
{
char *output, *input_walk, *output_walk;
char conv[3];
Expand Down Expand Up @@ -189,21 +181,15 @@ char *clean_uncgi(char *filename, struct clean_string_options *options)
* If a hash character, underscore, or dash are present at the start of the
* filename, they will be removed.
*/
char *clean_wipeup(char *filename, struct clean_string_options *options)
char *clean_wipeup(char *filename, int remove_trailing)
{
char *output, *input_walk, *output_walk;
int remove_trailing;
char *search, *seek, *current;

if (filename == NULL) {
return NULL;
}

remove_trailing = 0;
if (options != NULL) {
remove_trailing = options->remove_trailing;
}

/* remove any -, _, or # at beginning of string */
while (*filename == '-' || *filename == '_' || *filename == '#') {
filename++;
Expand Down Expand Up @@ -263,26 +249,22 @@ char *clean_wipeup(char *filename, struct clean_string_options *options)
* Translates UTF-8 characters (Unicode Translation Format - 8 Bit) into
* Unicode and then runs the translation table.
*/
char *clean_utf_8(char *filename, struct clean_string_options *options)
char *clean_utf_8(char *filename, table_t *table)
{
char *output, *input_walk, *output_walk, *replace_walk;
int new_value, expected_chars;

table_t *table = NULL;

int characters_eaten;

if (filename == NULL) {
return NULL;
}

if (options == NULL || options->table == NULL) {
if (table == NULL) {
fprintf(stderr, "internal error\n");
exit(EXIT_FAILURE);
}

table = options->table;

output = malloc((strlen(filename) * table->max_data_length) + 1);
if (output == NULL) {
fprintf(stderr, "out of memory: %s\n", strerror(errno));
Expand Down Expand Up @@ -402,26 +384,23 @@ char *clean_utf_8(char *filename, struct clean_string_options *options)
return output;
}


/*
* Trims a file down to specified length.
*/
char *clean_max_length(char *filename, struct clean_string_options *options)
char *clean_max_length(char *filename, size_t max_length)
{
char *extension;
char *input_walk;
char *output;
size_t body_length;
size_t extension_length;
size_t max_length;

if (filename == NULL) {
return NULL;
}

max_length = 256;
if (options != NULL) {
max_length = options->max_length;
if (max_length <= 0) {
max_length = 256;
}

// check to see if the file is smaller than the max length
Expand Down Expand Up @@ -475,7 +454,7 @@ char *clean_max_length(char *filename, struct clean_string_options *options)
/*
* Converts all characters to lowercase.
*/
char *clean_lower(char *filename, struct clean_string_options *options)
char *clean_lower(char *filename)
{
char *output, *input_walk, *output_walk;

Expand Down
14 changes: 7 additions & 7 deletions src/clean_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

#include "detox_struct.h"

extern char *clean_iso8859_1(char *filename, struct clean_string_options *options);
extern char *clean_safe(char *filename, struct clean_string_options *options);
extern char *clean_uncgi(char *filename, struct clean_string_options *options);
extern char *clean_wipeup(char *filename, struct clean_string_options *options);
extern char *clean_utf_8(char *filename, struct clean_string_options *options);
extern char *clean_max_length(char *filename, struct clean_string_options *options);
extern char *clean_lower(char *filename, struct clean_string_options *options);
extern char *clean_iso8859_1(char *filename, table_t *table);
extern char *clean_safe(char *filename, table_t *table);
extern char *clean_uncgi(char *filename);
extern char *clean_wipeup(char *filename, int remove_trailing);
extern char *clean_utf_8(char *filename, table_t *table);
extern char *clean_max_length(char *filename, size_t max_length);
extern char *clean_lower(char *filename);

#endif /* __CLEAN_STRING_H */
70 changes: 25 additions & 45 deletions src/config_file_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

void dump_config_file(config_file_t *config_file, options_t *main_options)
{
struct detox_sequence_list *list_work = NULL;
struct detox_sequence_filter *work = NULL;
sequence_t *sequence = NULL;
filter_t *filter = NULL;
char *file_walk;
int count = 0;

if (!main_options->verbose) {
printf("available sequences:\n");
}

list_work = config_file->sequences;
sequence = config_file->sequences;

while (list_work != NULL) {
while (sequence != NULL) {
if (main_options->verbose) {
if (count++ > 0) {
printf("\n");
Expand All @@ -36,61 +36,41 @@ void dump_config_file(config_file_t *config_file, options_t *main_options)
} else {
printf("\t");
}
printf("%s%s\n", list_work->name, (main_options->sequence_to_use == list_work->head) ? " (*)" : "");
printf("%s%s\n", sequence->name, (main_options->sequence_to_use == sequence) ? " (*)" : "");
if (main_options->verbose) {
printf("\tsource file: %s\n", list_work->source_filename);
printf("\tsource file: %s\n", sequence->source_filename);

work = list_work->head;
while (work != NULL) {
if (work->cleaner == &clean_uncgi) {
filter = sequence->filters;
while (filter != NULL) {
if (filter->cleaner == FILTER_UNCGI) {
printf("\tcleaner: uncgi\n");
} else if (work->cleaner == &clean_safe) {
} else if (filter->cleaner == FILTER_SAFE) {
printf("\tcleaner: safe\n");
if (work->options != NULL) {
if (work->options->builtin != NULL) {
printf("\t\tbuiltin table: %s\n", work->options->builtin);
} else if (work->options->filename != NULL) {
printf("\t\ttranslation table: %s\n", work->options->filename);
}
}
} else if (work->cleaner == &clean_wipeup) {
} else if (filter->cleaner == FILTER_WIPEUP) {
printf("\tcleaner: wipeup\n");
if (work->options != NULL) {
printf("\t\tremove trailing: %s\n", work->options->remove_trailing ? "yes" : "no");
}
} else if (work->cleaner == &clean_iso8859_1) {
printf("\t\tremove trailing: %s\n", filter->remove_trailing ? "yes" : "no");
} else if (filter->cleaner == FILTER_ISO8859_1) {
printf("\tcleaner: iso8859_1\n");
if (work->options != NULL) {
if (work->options->builtin != NULL) {
printf("\t\tbuiltin table: %s\n", work->options->builtin);
} else if (work->options->filename != NULL) {
printf("\t\ttranslation table: %s\n", work->options->filename);
}
}
} else if (work->cleaner == &clean_utf_8) {
} else if (filter->cleaner == FILTER_UTF_8) {
printf("\tcleaner: utf_8\n");
if (work->options != NULL) {
if (work->options->builtin != NULL) {
printf("\t\tbuiltin table: %s\n", work->options->builtin);
} else if (work->options->filename != NULL) {
printf("\t\ttranslation table: %s\n", work->options->filename);
}
}
} else if (work->cleaner == &clean_max_length) {
} else if (filter->cleaner == FILTER_MAX_LENGTH) {
printf("\tcleaner: max length\n");
if (work->options != NULL) {
printf("\t\tlength: %d\n", (unsigned int) work->options->max_length);
}
}
if (work->cleaner == &clean_lower) {
printf("\t\tlength: %d\n", (unsigned int) filter->max_length);
} else if (filter->cleaner == FILTER_LOWER) {
printf("\tcleaner: lower\n");
}

work = work->next;
if (filter->builtin != NULL) {
printf("\t\tbuiltin table: %s\n", filter->builtin);
} else if (filter->filename != NULL) {
printf("\t\ttranslation table: %s\n", filter->filename);
}

filter = filter->next;
}
}

list_work = list_work->next;
sequence = sequence->next;
}

if (filelist_count(config_file->files_to_ignore) > 0) {
Expand Down
Loading

0 comments on commit 932c3b7

Please sign in to comment.