| 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 <string.h> |
| 13 |
#include <ctype.h> |
| 14 |
|
| 15 |
#include "clean_string.h" |
| 16 |
#include "wrapped.h" |
| 17 |
#include "table.h" |
| 18 |
|
| 19 |
#define ISO8859_1_UPPER_BIT 0x80 |
| 20 |
|
| 21 |
/** |
| 22 |
* Transliterates ISO 8859-1 characters (Latin-1) into lower ASCII characters. |
| 23 |
* |
| 24 |
* @param filename The filename to clean. |
| 25 |
* @param table_t The transliteration table to use. |
| 26 |
* |
| 27 |
* @return The updated filename or NULL if an error occurred. |
| 28 |
*/ |
| 29 |
char *clean_iso8859_1(char *filename, table_t *table) |
| 30 |
{ |
| 31 |
char *output, *input_walk, *output_walk, *replace_walk; |
| 32 |
int new_value; |
| 33 |
|
| 34 |
if (filename == NULL) { |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
if (table == NULL) { |
| 39 |
fprintf(stderr, "internal error\n"); |
| 40 |
exit(EXIT_FAILURE); |
| 41 |
} |
| 42 |
|
| 43 |
output = wrapped_malloc((strlen(filename) * table->max_data_length) + 1); |
| 44 |
|
| 45 |
input_walk = filename; |
| 46 |
output_walk = output; |
| 47 |
|
| 48 |
while (*input_walk != '\0') { |
| 49 |
if ((*input_walk & ISO8859_1_UPPER_BIT) == 0) { |
| 50 |
*output_walk++ = *input_walk++; |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
new_value = (unsigned char) input_walk[0]; |
| 55 |
|
| 56 |
replace_walk = table_get(table, new_value); |
| 57 |
|
| 58 |
if (replace_walk == NULL) { |
| 59 |
if (table->default_translation == NULL) { |
| 60 |
/* |
| 61 |
* Null translation == leave it alone |
| 62 |
*/ |
| 63 |
*output_walk++ = *input_walk++; |
| 64 |
continue; |
| 65 |
} else { |
| 66 |
replace_walk = table->default_translation; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
while (*replace_walk != '\0') { |
| 71 |
*output_walk++ = *replace_walk++; |
| 72 |
} |
| 73 |
|
| 74 |
input_walk++; |
| 75 |
} |
| 76 |
|
| 77 |
*output_walk = '\0'; |
| 78 |
|
| 79 |
return output; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Replaces difficult-to-work-with characters with underscores and dashes. |
| 84 |
* |
| 85 |
* @param filename The filename to clean. |
| 86 |
* @param table_t The replacement table to use. |
| 87 |
* |
| 88 |
* @return The updated filename or NULL if an error occurred. |
| 89 |
*/ |
| 90 |
char *clean_safe(char *filename, table_t *table) |
| 91 |
{ |
| 92 |
char *output, *input_walk, *output_walk, *replace_walk; |
| 93 |
|
| 94 |
if (filename == NULL) { |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
if (table == NULL) { |
| 99 |
fprintf(stderr, "internal error\n"); |
| 100 |
exit(EXIT_FAILURE); |
| 101 |
} |
| 102 |
|
| 103 |
output = wrapped_malloc((strlen(filename) * table->max_data_length) + 1); |
| 104 |
|
| 105 |
input_walk = filename; |
| 106 |
output_walk = output; |
| 107 |
|
| 108 |
while (*input_walk != '\0') { |
| 109 |
replace_walk = table_get(table, *input_walk); |
| 110 |
if (replace_walk == NULL) { |
| 111 |
if (table->default_translation == NULL) { |
| 112 |
|
| 113 |
/* |
| 114 |
* Null translation == leave it alone |
| 115 |
*/ |
| 116 |
*output_walk++ = *input_walk++; |
| 117 |
continue; |
| 118 |
} else { |
| 119 |
replace_walk = table->default_translation; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
while (*replace_walk != '\0') { |
| 124 |
*output_walk++ = *replace_walk++; |
| 125 |
} |
| 126 |
|
| 127 |
input_walk++; |
| 128 |
} |
| 129 |
|
| 130 |
*output_walk = '\0'; |
| 131 |
|
| 132 |
return output; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Cleans up any CGI encoded characters, in the form "%" followed by 2 hex |
| 137 |
* digits. |
| 138 |
* |
| 139 |
* @param filename The filename to clean. |
| 140 |
* |
| 141 |
* @return The updated filename or NULL if an error occurred. |
| 142 |
*/ |
| 143 |
char *clean_uncgi(char *filename) |
| 144 |
{ |
| 145 |
char *output, *input_walk, *output_walk; |
| 146 |
char conv[3]; |
| 147 |
|
| 148 |
if (filename == NULL) { |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
output = wrapped_malloc(strlen(filename) + 1); |
| 153 |
|
| 154 |
input_walk = filename; |
| 155 |
output_walk = output; |
| 156 |
|
| 157 |
while (*input_walk != '\0') { |
| 158 |
if (input_walk[0] == '%' && isxdigit((int) input_walk[1]) && isxdigit((int) input_walk[2])) { |
| 159 |
conv[0] = input_walk[1]; |
| 160 |
conv[1] = input_walk[2]; |
| 161 |
conv[2] = 0; |
| 162 |
*output_walk++ = (char) strtol(conv, NULL, 16); |
| 163 |
input_walk += 3; |
| 164 |
} else if (input_walk[0] == '+') { |
| 165 |
*output_walk++ = ' '; |
| 166 |
input_walk++; |
| 167 |
} else { |
| 168 |
*output_walk++ = *input_walk++; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
*output_walk = '\0'; |
| 173 |
|
| 174 |
return output; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Reduces any series of underscores or dashes to a single character. The dash |
| 180 |
* takes precedence. |
| 181 |
* |
| 182 |
* If remove_trailing is set, then periods are added to the set of characters |
| 183 |
* to work on. The period then takes precedence, followed by the dash. |
| 184 |
* |
| 185 |
* If a hash character, underscore, or dash are present at the start of the |
| 186 |
* filename, they will be removed. |
| 187 |
* |
| 188 |
* @param filename The filename to clean. |
| 189 |
* @param remove_trailing Whether or not to include periods in the set of |
| 190 |
* characters to operate on. |
| 191 |
* |
| 192 |
* @return The updated filename or NULL if an error occurred. |
| 193 |
*/ |
| 194 |
char *clean_wipeup(char *filename, int remove_trailing) |
| 195 |
{ |
| 196 |
char *output, *input_walk, *output_walk; |
| 197 |
char *search, *seek, *current; |
| 198 |
|
| 199 |
if (filename == NULL) { |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
/* remove any -, _, or # at beginning of string */ |
| 204 |
while (*filename == '-' || *filename == '_' || *filename == '#') { |
| 205 |
filename++; |
| 206 |
} |
| 207 |
|
| 208 |
output = wrapped_malloc(strlen(filename) + 1); |
| 209 |
|
| 210 |
search = wrapped_strdup(remove_trailing ? ".-_" : "-_"); |
| 211 |
|
| 212 |
input_walk = filename; |
| 213 |
output_walk = output; |
| 214 |
current = NULL; |
| 215 |
|
| 216 |
while (*input_walk != '\0') { |
| 217 |
seek = strchr(search, *input_walk); |
| 218 |
if (seek != NULL) { |
| 219 |
if (current == NULL || seek < current) { |
| 220 |
current = seek; |
| 221 |
} |
| 222 |
|
| 223 |
input_walk++; |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
if (current != NULL) { |
| 228 |
*output_walk++ = current[0]; |
| 229 |
current = NULL; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
*output_walk++ = *input_walk++; |
| 234 |
} |
| 235 |
|
| 236 |
if (current != NULL) { |
| 237 |
*output_walk++ = current[0]; |
| 238 |
} |
| 239 |
|
| 240 |
*output_walk = '\0'; |
| 241 |
|
| 242 |
free(search); |
| 243 |
|
| 244 |
return output; |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* Trims a file down to specified length. |
| 249 |
*/ |
| 250 |
char *clean_max_length(char *filename, size_t max_length) |
| 251 |
{ |
| 252 |
char *extension; |
| 253 |
char *input_walk; |
| 254 |
char *output; |
| 255 |
size_t body_length; |
| 256 |
size_t extension_length; |
| 257 |
|
| 258 |
if (filename == NULL) { |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
|
| 262 |
if (max_length <= 0) { |
| 263 |
max_length = 256; |
| 264 |
} |
| 265 |
|
| 266 |
// check to see if the file is smaller than the max length |
| 267 |
if (strlen(filename) <= max_length) { |
| 268 |
return wrapped_strdup(filename); |
| 269 |
} |
| 270 |
|
| 271 |
output = wrapped_malloc(max_length + 1); |
| 272 |
|
| 273 |
snprintf(output, max_length + 1, "%s", filename); |
| 274 |
|
| 275 |
// check to see if the file has no extension |
| 276 |
extension = input_walk = strrchr(filename, '.'); |
| 277 |
if (input_walk == NULL) { |
| 278 |
return output; |
| 279 |
} |
| 280 |
|
| 281 |
if (strlen(extension) == 1) { |
| 282 |
return output; |
| 283 |
} |
| 284 |
|
| 285 |
// look back 5 characters for a second extension |
| 286 |
while (--input_walk > filename) { |
| 287 |
if (extension - input_walk > 5) { |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
if (*input_walk == '.') { |
| 292 |
extension = input_walk; |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
extension_length = strlen(extension); |
| 298 |
if (max_length <= extension_length) { |
| 299 |
fprintf(stderr, "warning: max_length %d is less than required file length for '%s'. giving up.\n", (int) max_length, filename); |
| 300 |
free(output); |
| 301 |
return wrapped_strdup(filename); |
| 302 |
} |
| 303 |
|
| 304 |
body_length = max_length - extension_length; |
| 305 |
|
| 306 |
snprintf(output + body_length, extension_length + 1, "%s", extension); |
| 307 |
|
| 308 |
return output; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Converts all characters to lowercase. |
| 313 |
* |
| 314 |
* @param filename The filename to clean. |
| 315 |
* |
| 316 |
* @return The updated filename or NULL if an error occurred. |
| 317 |
*/ |
| 318 |
char *clean_lower(char *filename) |
| 319 |
{ |
| 320 |
char *output, *input_walk, *output_walk; |
| 321 |
|
| 322 |
if (filename == NULL) { |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
output = wrapped_malloc(strlen(filename) + 1); |
| 327 |
|
| 328 |
input_walk = filename; |
| 329 |
output_walk = output; |
| 330 |
|
| 331 |
while (*input_walk != '\0') { |
| 332 |
if (isupper((int) *input_walk)) { |
| 333 |
*output_walk++ = tolower((int) *input_walk++); |
| 334 |
} else { |
| 335 |
*output_walk++ = *input_walk++; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
*output_walk = '\0'; |
| 340 |
|
| 341 |
return output; |
| 342 |
} |