Skip to content

Commit

Permalink
Converted all strings to char; fixed resulting issues. (resolves #31
Browse files Browse the repository at this point in the history
…and #32)
  • Loading branch information
dharple committed Feb 13, 2021
1 parent d78bab3 commit 492b32f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 84 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed
- Converted unsigned char strings to char strings, which broken iso8859-1
processing. Updated iso8859-1 processing so that it works as before.
Updated CFLAGS to catch any issues that arise in the future. [#31]

### Fixed
- src/Makefile.am no longer deletes src/config_file.h when `make
maintainer-clean` is run.

## [1.4.0] - 2021-02-11
### Added
- Regression tests for basic functionality, based on old custom scripts.
Expand Down Expand Up @@ -163,6 +172,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
[1.2.1]: https://github.com/dharple/detox/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/dharple/detox/releases/tag/v1.2.0

[#31]: https://github.com/dharple/detox/issues/31
[#30]: https://github.com/dharple/detox/issues/30
[#19]: https://github.com/dharple/detox/issues/19
[#17]: https://github.com/dharple/detox/issues/17
Expand Down
4 changes: 1 addition & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
BUILT_SOURCES = config_file.h
AM_CFLAGS = -g -O2 -Wall -DYY_NO_INPUT -DYY_NO_UNPUT -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\"
AM_YFLAGS = -d

AM_CPPFLAGS = -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\"

bin_PROGRAMS = detox inline-detox
detox_SOURCES = detox.c file.c clean_string.c table.c parse_table.c config_file_yacc.y config_file_lex.l config_file_spoof.c config_file_dump.c parse_options.c
inline_detox_SOURCES = $(detox_SOURCES)
Expand Down
116 changes: 62 additions & 54 deletions src/clean_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
#include "parse_table.h"
#include "table.h"

#define ISO8859_1_UPPER_BIT 0x80
#define ISO8859_LOWER_BITS 0x7f

/*
* Translates ISO8859.1 characters (Latin-1) into lower ASCII characters.
*/
unsigned char *clean_iso8859_1_basic(unsigned char *s, void *opts)
char *clean_iso8859_1_basic(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk, *replace_walk;
int replace_pos;
char *output, *input_walk, *output_walk, *replace_walk;
int new_value;

if (s == NULL) {
return NULL;
Expand All @@ -49,18 +51,20 @@ unsigned char *clean_iso8859_1_basic(unsigned char *s, void *opts)
output_walk = output;

while (*input_walk != '\0') {
if (*input_walk >= ISO8859_1_OFFSET) {
replace_pos = *input_walk - ISO8859_1_OFFSET;
replace_walk = (unsigned char *)&iso8859_1_trans[replace_pos];

while (*replace_walk != '\0') {
*output_walk++ = *replace_walk++;
}
input_walk++;
}
else {
if ((*input_walk & ISO8859_1_UPPER_BIT) == 0) {
*output_walk++ = *input_walk++;
continue;
}

new_value = *input_walk & ISO8859_LOWER_BITS;

replace_walk = (char *)&iso8859_1_trans[new_value];

while (*replace_walk != '\0') {
*output_walk++ = *replace_walk++;
}

input_walk++;
}

*output_walk = '\0';
Expand All @@ -71,9 +75,10 @@ unsigned char *clean_iso8859_1_basic(unsigned char *s, void *opts)
/*
* Translates ISO8859.1 characters (Latin-1) into lower ASCII characters.
*/
unsigned char *clean_iso8859_1(unsigned char *s, void *opts)
char *clean_iso8859_1(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk, *replace_walk;
char *output, *input_walk, *output_walk, *replace_walk;
int new_value;

struct translation_table *table = NULL;
struct clean_string_options *options = NULL;
Expand All @@ -100,30 +105,33 @@ unsigned char *clean_iso8859_1(unsigned char *s, void *opts)
output_walk = output;

while (*input_walk != '\0') {
if (*input_walk >= ISO8859_1_OFFSET) {
replace_walk = table_get(table, *input_walk);
if (replace_walk == NULL) {
if (table->default_translation == NULL) {
/*
* Null translation == leave it alone
*/
*output_walk++ = *input_walk++;
continue;
}
else {
replace_walk = table->default_translation;
}
}
if ((*input_walk & ISO8859_1_UPPER_BIT) == 0) {
*output_walk++ = *input_walk++;
continue;
}

while (*replace_walk != '\0') {
*output_walk++ = *replace_walk++;
}
new_value = (unsigned char) *input_walk;

input_walk++;
replace_walk = table_get(table, new_value);

if (replace_walk == NULL) {
if (table->default_translation == NULL) {
/*
* Null translation == leave it alone
*/
*output_walk++ = *input_walk++;
continue;
}
else {
replace_walk = table->default_translation;
}
}
else {
*output_walk++ = *input_walk++;

while (*replace_walk != '\0') {
*output_walk++ = *replace_walk++;
}

input_walk++;
}

*output_walk = '\0';
Expand All @@ -149,9 +157,9 @@ unsigned char *clean_iso8859_1(unsigned char *s, void *opts)
* ( ) [ ] { }
*
*/
unsigned char *clean_safe_basic(unsigned char *s, void *opts)
char *clean_safe_basic(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk;
char *output, *input_walk, *output_walk;

if (s == NULL) {
return NULL;
Expand Down Expand Up @@ -238,9 +246,9 @@ unsigned char *clean_safe_basic(unsigned char *s, void *opts)
/*
* Translates unsafe characters
*/
unsigned char *clean_safe(unsigned char *s, void *opts)
char *clean_safe(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk, *replace_walk;
char *output, *input_walk, *output_walk, *replace_walk;

struct translation_table *table = NULL;
struct clean_string_options *options = NULL;
Expand Down Expand Up @@ -300,10 +308,10 @@ unsigned char *clean_safe(unsigned char *s, void *opts)
* Cleans up any CGI encoded characters, in the form "%" followed by 2 hex
* digits.
*/
unsigned char *clean_uncgi(unsigned char *s, void *opts)
char *clean_uncgi(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk;
unsigned char conv[3];
char *output, *input_walk, *output_walk;
char conv[3];

if (s == NULL) {
return NULL;
Expand All @@ -323,7 +331,7 @@ unsigned char *clean_uncgi(unsigned char *s, void *opts)
conv[0] = input_walk[1];
conv[1] = input_walk[2];
conv[2] = 0;
*output_walk++ = (unsigned char)strtol(conv, NULL, 16);
*output_walk++ = (char) strtol(conv, NULL, 16);
input_walk += 3;
}
else {
Expand All @@ -348,9 +356,9 @@ unsigned char *clean_uncgi(unsigned char *s, void *opts)
* Strips any "-", "_" or "#" from the beginning of a string.
*
*/
unsigned char *clean_wipeup(unsigned char *s, void *opts)
char *clean_wipeup(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk;
char *output, *input_walk, *output_walk;
int matched;
int remove_trailing;

Expand Down Expand Up @@ -436,9 +444,9 @@ unsigned char *clean_wipeup(unsigned char *s, void *opts)
* Translates UTF-8 characters (Unicode Translation Format - 8 Bit) into
* Unicode and then lower ASCII characters.
*/
unsigned char *clean_utf_8_basic(unsigned char *s, void *opts)
char *clean_utf_8_basic(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk, *replace_walk;
char *output, *input_walk, *output_walk, *replace_walk;
int new_value, expected_chars;

if (s == NULL) {
Expand Down Expand Up @@ -529,7 +537,7 @@ unsigned char *clean_utf_8_basic(unsigned char *s, void *opts)
continue;
}

replace_walk = (unsigned char *)&unicode_trans[new_value];
replace_walk = (char *)&unicode_trans[new_value];

while (*replace_walk != '\0') {
*output_walk++ = *replace_walk++;
Expand All @@ -545,9 +553,9 @@ unsigned char *clean_utf_8_basic(unsigned char *s, void *opts)
* Translates UTF-8 characters (Unicode Translation Format - 8 Bit) into
* Unicode and then runs the translation table.
*/
unsigned char *clean_utf_8(unsigned char *s, void *opts)
char *clean_utf_8(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk, *replace_walk;
char *output, *input_walk, *output_walk, *replace_walk;
int new_value, expected_chars;

struct translation_table *table = NULL;
Expand Down Expand Up @@ -687,9 +695,9 @@ unsigned char *clean_utf_8(unsigned char *s, void *opts)
/*
* Trims a file down to specified length.
*/
unsigned char *clean_max_length(unsigned char *s, void *opts)
char *clean_max_length(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk;
char *output, *input_walk, *output_walk;
size_t max_length;
size_t s_length;
size_t ext_length;
Expand Down Expand Up @@ -741,9 +749,9 @@ unsigned char *clean_max_length(unsigned char *s, void *opts)
/*
* Converts all characters to lowercase.
*/
unsigned char *clean_lower(unsigned char *s, void *opts)
char *clean_lower(char *s, void *opts)
{
unsigned char *output, *input_walk, *output_walk;
char *output, *input_walk, *output_walk;

if (s == NULL) {
return NULL;
Expand Down
20 changes: 10 additions & 10 deletions src/clean_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ struct clean_string_options {
void *translation_table;
};

extern unsigned char *clean_iso8859_1_basic(unsigned char *s, void *options);
extern unsigned char *clean_iso8859_1(unsigned char *s, void *options);
extern unsigned char *clean_safe_basic(unsigned char *s, void *options);
extern unsigned char *clean_safe(unsigned char *s, void *options);
extern unsigned char *clean_uncgi(unsigned char *s, void *options);
extern unsigned char *clean_wipeup(unsigned char *s, void *options);
extern unsigned char *clean_utf_8_basic(unsigned char *s, void *options);
extern unsigned char *clean_utf_8(unsigned char *s, void *options);
extern unsigned char *clean_max_length(unsigned char *s, void *opts);
extern unsigned char *clean_lower(unsigned char *s, void *opts);
extern char *clean_iso8859_1_basic(char *s, void *options);
extern char *clean_iso8859_1(char *s, void *options);
extern char *clean_safe_basic(char *s, void *options);
extern char *clean_safe(char *s, void *options);
extern char *clean_uncgi(char *s, void *options);
extern char *clean_wipeup(char *s, void *options);
extern char *clean_utf_8_basic(char *s, void *options);
extern char *clean_utf_8(char *s, void *options);
extern char *clean_max_length(char *s, void *opts);
extern char *clean_lower(char *s, void *opts);

#endif /* __CLEAN_STRING_H */
4 changes: 2 additions & 2 deletions src/detox.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct detox_sequence_list {
struct detox_sequence_entry {
struct detox_sequence_entry *next;

unsigned char *(*cleaner) (unsigned char *str, void *options);
char *(*cleaner) (char *str, void *options);
void *options;
};

Expand All @@ -42,7 +42,7 @@ struct detox_sequence_entry {
struct detox_ignore_entry {
struct detox_ignore_entry *next;

unsigned char *filename;
char *filename;
};

/*
Expand Down
22 changes: 11 additions & 11 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ static char badfiles[3][30] = {
/*
* Internal function declarations
*/
static int ignore_file(unsigned char *filename, struct detox_options *options);
static int ignore_file(char *filename, struct detox_options *options);

/*
* Renames file to a safe filename.
*/
unsigned char *parse_file(unsigned char *filename, struct detox_options *options)
char *parse_file(char *filename, struct detox_options *options)
{
unsigned char *old_filename, *old_filename_ptr, *new_filename;
unsigned char *work, *hold;
char *old_filename, *old_filename_ptr, *new_filename;
char *work, *hold;

struct stat stat_info_old;
struct stat stat_info_new;
Expand Down Expand Up @@ -157,9 +157,9 @@ unsigned char *parse_file(unsigned char *filename, struct detox_options *options
/*
* Handles directory.
*/
void parse_dir(unsigned char *indir, struct detox_options *options)
void parse_dir(char *indir, struct detox_options *options)
{
unsigned char *new_file, *work;
char *new_file, *work;
DIR *dir_handle;
struct dirent *dir_entry;
struct stat stat_info;
Expand All @@ -177,7 +177,7 @@ void parse_dir(unsigned char *indir, struct detox_options *options)
}

new_file_length = strlen(indir) + 1024;
new_file = (char *)malloc(new_file_length);
new_file = malloc(new_file_length);
if (new_file == NULL) {
fprintf(stderr, "out of memory: %s\n", strerror(errno));
return;
Expand Down Expand Up @@ -230,7 +230,7 @@ void parse_dir(unsigned char *indir, struct detox_options *options)
/*
* Handles a special file.
*/
void parse_special(unsigned char *in, struct detox_options *options)
void parse_special(char *in, struct detox_options *options)
{
struct stat stat_info;
char *new_file, *work;
Expand Down Expand Up @@ -287,7 +287,7 @@ void parse_special(unsigned char *in, struct detox_options *options)
/*
* Determines if the file should be ignored
*/
static int ignore_file(unsigned char *filename, struct detox_options *options)
static int ignore_file(char *filename, struct detox_options *options)
{
struct detox_ignore_entry *ignore_walk;
int x;
Expand All @@ -312,11 +312,11 @@ static int ignore_file(unsigned char *filename, struct detox_options *options)
/*
* Renames file to a safe filename.
*/
void parse_inline(unsigned char *filename, struct detox_options *options)
void parse_inline(char *filename, struct detox_options *options)
{
struct detox_sequence_entry *sequence;
FILE *fp;
unsigned char *base, *work, *hold;
char *base, *work, *hold;
size_t buf_size;

if (filename != NULL) {
Expand Down
Loading

0 comments on commit 492b32f

Please sign in to comment.