| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "detox_struct.h" |
| 16 |
|
| 17 |
#include "builtin_table.h" |
| 18 |
#include "clean_string.h" |
| 19 |
#include "clean_utf_8.h" |
| 20 |
#include "parse_table.h" |
| 21 |
#include "wrapped.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* Attempts to find a translation table associated with a filter. |
| 25 |
* |
| 26 |
* @param check_filename The filename to look for. |
| 27 |
* |
| 28 |
* @return |
| 29 |
*/ |
| 30 |
table_t *filter_find_table(const char *check_filename) |
| 31 |
{ |
| 32 |
table_t *table = NULL; |
| 33 |
int err; |
| 34 |
char *check_config_file; |
| 35 |
|
| 36 |
check_config_file = alloca(MAX_PATH_LEN); |
| 37 |
if (check_config_file == NULL) { |
| 38 |
fprintf(stderr, "out of memory: %s\n", strerror(errno)); |
| 39 |
exit(EXIT_FAILURE); |
| 40 |
} |
| 41 |
|
| 42 |
#ifdef DATADIR |
| 43 |
err = snprintf(check_config_file, MAX_PATH_LEN, "%s/detox/%s", DATADIR, check_filename); |
| 44 |
if (err < MAX_PATH_LEN) { |
| 45 |
table = parse_table(check_config_file); |
| 46 |
if (table != NULL) { |
| 47 |
return table; |
| 48 |
} |
| 49 |
} |
| 50 |
#endif |
| 51 |
|
| 52 |
err = snprintf(check_config_file, MAX_PATH_LEN, "/usr/share/detox/%s", check_filename); |
| 53 |
if (err < MAX_PATH_LEN) { |
| 54 |
table = parse_table(check_config_file); |
| 55 |
if (table != NULL) { |
| 56 |
return table; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
err = snprintf(check_config_file, MAX_PATH_LEN, "/usr/local/share/detox/%s", check_filename); |
| 61 |
if (err < MAX_PATH_LEN) { |
| 62 |
table = parse_table(check_config_file); |
| 63 |
} |
| 64 |
|
| 65 |
return table; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
filter_t *filter_init(int cleaner) |
| 70 |
{ |
| 71 |
filter_t *ret; |
| 72 |
|
| 73 |
ret = wrapped_malloc(sizeof(filter_t)); |
| 74 |
|
| 75 |
memset(ret, 0, sizeof(filter_t)); |
| 76 |
|
| 77 |
ret->cleaner = cleaner; |
| 78 |
|
| 79 |
return ret; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Uses a builtin table for a filter. |
| 84 |
* |
| 85 |
* @param filter The filter to load a builtin for. |
| 86 |
*/ |
| 87 |
table_t *filter_load_builtin(filter_t *filter) |
| 88 |
{ |
| 89 |
if (filter->cleaner == FILTER_ISO8859_1) { |
| 90 |
return load_builtin_iso8859_1_table(); |
| 91 |
} else if (filter->cleaner == FILTER_UTF_8) { |
| 92 |
return load_builtin_unicode_table(); |
| 93 |
} else if (filter->cleaner == FILTER_SAFE) { |
| 94 |
return load_builtin_safe_table(); |
| 95 |
} |
| 96 |
|
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Uses a builtin table for a filter. |
| 102 |
* |
| 103 |
* Valid values: |
| 104 |
* - builtin:cp1252 |
| 105 |
* - builtin:iso8859_1 |
| 106 |
* - builtin:safe |
| 107 |
* - builtin:unicode |
| 108 |
* |
| 109 |
* @param filename Builtin filename to use |
| 110 |
*/ |
| 111 |
table_t *filter_load_builtin_by_filename(const char *filename) |
| 112 |
{ |
| 113 |
if (strcmp(filename, "iso8859_1") == 0) { |
| 114 |
return load_builtin_iso8859_1_table(); |
| 115 |
} else if (strcmp(filename, "unicode") == 0) { |
| 116 |
return load_builtin_unicode_table(); |
| 117 |
} else if (strcmp(filename, "safe") == 0) { |
| 118 |
return load_builtin_safe_table(); |
| 119 |
} else if (strcmp(filename, "cp1252") == 0) { |
| 120 |
return load_builtin_cp1252_table(); |
| 121 |
} |
| 122 |
|
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Loads the translation table associated with a filter. |
| 128 |
* |
| 129 |
* @param filter The filter to check. |
| 130 |
*/ |
| 131 |
table_t *filter_load_table(filter_t *filter) |
| 132 |
{ |
| 133 |
table_t *table = NULL; |
| 134 |
char *check_filename = NULL; |
| 135 |
int do_search = 1; |
| 136 |
|
| 137 |
if (filter->cleaner == FILTER_ISO8859_1) { |
| 138 |
check_filename = "iso8859_1.tbl"; |
| 139 |
} else if (filter->cleaner == FILTER_UTF_8) { |
| 140 |
check_filename = "unicode.tbl"; |
| 141 |
} else if (filter->cleaner == FILTER_SAFE) { |
| 142 |
check_filename = "safe.tbl"; |
| 143 |
} else { |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
if (filter->builtin != NULL) { |
| 148 |
table = filter_load_builtin_by_filename(filter->builtin); |
| 149 |
if (table == NULL) { |
| 150 |
fprintf(stderr, "detox: unable to locate builtin table \"%s\"\n", filter->builtin); |
| 151 |
exit(EXIT_FAILURE); |
| 152 |
} |
| 153 |
return table; |
| 154 |
} |
| 155 |
|
| 156 |
if (filter->filename != NULL) { |
| 157 |
check_filename = filter->filename; |
| 158 |
do_search = 0; |
| 159 |
} |
| 160 |
|
| 161 |
if (do_search) { |
| 162 |
table = filter_find_table(check_filename); |
| 163 |
if (table != NULL) { |
| 164 |
return table; |
| 165 |
} |
| 166 |
|
| 167 |
// load builtin translation tables |
| 168 |
table = filter_load_builtin(filter); |
| 169 |
|
| 170 |
if (table == NULL) { |
| 171 |
fprintf(stderr, "detox: unable to locate translation table or fall back\n"); |
| 172 |
exit(EXIT_FAILURE); |
| 173 |
} |
| 174 |
} else { |
| 175 |
table = parse_table(check_filename); |
| 176 |
|
| 177 |
if (table == NULL) { |
| 178 |
fprintf(stderr, "detox: unable to parse file: \"%s\"\n", check_filename); |
| 179 |
exit(EXIT_FAILURE); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return table; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Runs a filter. |
| 188 |
* |
| 189 |
* @param filter The filter to run. |
| 190 |
* @param filename The filename to run through the filter. |
| 191 |
* |
| 192 |
* @return The filtered filename, or NULL if a problem was encountered. |
| 193 |
*/ |
| 194 |
char *filter_run(filter_t *filter, char *filename) |
| 195 |
{ |
| 196 |
char *ret; |
| 197 |
|
| 198 |
if (filter == NULL) { |
| 199 |
fprintf(stderr, "internal error\n"); |
| 200 |
exit(EXIT_FAILURE); |
| 201 |
} |
| 202 |
|
| 203 |
if (filename == NULL) { |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
switch (filter->cleaner) { |
| 208 |
case FILTER_ISO8859_1: |
| 209 |
ret = clean_iso8859_1(filename, filter->table); |
| 210 |
break; |
| 211 |
|
| 212 |
case FILTER_LOWER: |
| 213 |
ret = clean_lower(filename); |
| 214 |
break; |
| 215 |
|
| 216 |
case FILTER_MAX_LENGTH: |
| 217 |
ret = clean_max_length(filename, filter->max_length); |
| 218 |
break; |
| 219 |
|
| 220 |
case FILTER_SAFE: |
| 221 |
ret = clean_safe(filename, filter->table); |
| 222 |
break; |
| 223 |
|
| 224 |
case FILTER_UNCGI: |
| 225 |
ret = clean_uncgi(filename); |
| 226 |
break; |
| 227 |
|
| 228 |
case FILTER_UTF_8: |
| 229 |
ret = clean_utf_8(filename, filter->table); |
| 230 |
break; |
| 231 |
|
| 232 |
case FILTER_WIPEUP: |
| 233 |
ret = clean_wipeup(filename, filter->remove_trailing); |
| 234 |
break; |
| 235 |
|
| 236 |
default: |
| 237 |
fprintf(stderr, "detox: unknown filter %d\n", filter->cleaner); |
| 238 |
exit(EXIT_FAILURE); |
| 239 |
} |
| 240 |
|
| 241 |
return ret; |
| 242 |
} |