X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=pcrs.c;h=0f5145aa5f975dfd7b200d4dcf4cc51483af96e8;hp=ee51d0a39f1a5cdd4a4d37c03d8f948cc001c93e;hb=ae931e26de71fe1d1bd124ed66e98dbd3f11deaf;hpb=863e576592f86370bee46a442292d2e4930b3fd9 diff --git a/pcrs.c b/pcrs.c index ee51d0a3..0f5145aa 100644 --- a/pcrs.c +++ b/pcrs.c @@ -1,5 +1,4 @@ -const char pcrs_rcs[] = "$Id: pcrs.c,v 1.23 2006/12/29 17:53:05 fabiankeil Exp $"; - +const char pcrs_rcs[] = "$Id: pcrs.c,v 1.36 2011/03/03 14:49:08 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/pcrs.c,v $ @@ -8,10 +7,15 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.23 2006/12/29 17:53:05 fabiankeil Exp $ * and adds Perl-style substitution. That * is, it mimics Perl's 's' operator. See pcrs(3) for details. * + * WARNING: This file contains additional functions and bug + * fixes that aren't part of the latest official pcrs package + * (which apparently is no longer maintained). * * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt * * + * Copyright (C) 2006, 2007 Fabian Keil + * * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU Lesser * General Public License (LGPL), version 2.1, which should @@ -31,131 +35,12 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.23 2006/12/29 17:53:05 fabiankeil Exp $ * or write to the Free Software Foundation, Inc., 59 * Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - * Revisions : - * $Log: pcrs.c,v $ - * Revision 1.23 2006/12/29 17:53:05 fabiankeil - * Fixed gcc43 conversion warnings. - * - * Revision 1.22 2006/12/24 17:34:20 fabiankeil - * Add pcrs_strerror() message for PCRE_ERROR_MATCHLIMIT - * and give a hint why an error code might be unknown. - * - * Catch NULL subjects early in pcrs_execute(). - * - * Revision 1.21 2006/07/18 14:48:47 david__schmidt - * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch) - * with what was really the latest development (the v_3_0_branch branch) - * - * Revision 1.19.2.4 2005/05/07 21:50:55 david__schmidt - * A few memory leaks plugged (mostly on error paths) - * - * Revision 1.19.2.3 2003/12/04 12:32:45 oes - * Append a trailing nullbyte to result to facilitate string processing - * - * Revision 1.19.2.2 2002/10/08 16:22:28 oes - * Bugfix: Need to check validity of backreferences explicitly, - * because when max_matches are reached and matches is expanded, - * realloc() does not zero the memory. Fixes Bug # 606227 - * - * Revision 1.19.2.1 2002/08/10 11:23:40 oes - * Include prce.h via project.h, where the appropriate - * source will have been selected - * - * Revision 1.19 2002/03/08 14:47:48 oes - * Cosmetics - * - * Revision 1.18 2002/03/08 14:17:14 oes - * Fixing -Wconversion warnings - * - * Revision 1.17 2002/03/08 13:45:48 oes - * Hiding internal functions - * - * Revision 1.16 2001/11/30 21:32:14 jongfoster - * Fixing signed/unsigned comparison (Andreas please check this!) - * One tab->space - * - * Revision 1.15 2001/09/20 16:11:06 steudten - * - * Add casting for some string functions. - * - * Revision 1.14 2001/09/09 21:41:57 oes - * Fixing yet another silly bug - * - * Revision 1.13 2001/09/06 14:05:59 oes - * Fixed silly bug - * - * Revision 1.12 2001/08/18 11:35:00 oes - * - Introduced pcrs_strerror() - * - made some NULL arguments non-fatal - * - added support for \n \r \e \b \t \f \a \0 in substitute - * - made quoting adhere to standard rules - * - added warning for bad backrefs - * - added pcrs_execute_list() - * - fixed comments - * - bugfix & cosmetics - * - * Revision 1.11 2001/08/15 15:32:03 oes - * - Added support for Perl's special variables $+, $' and $` - * - Improved the substitute parser - * - Replaced the hard limit for the maximum number of matches - * by dynamic reallocation - * - * Revision 1.10 2001/08/05 13:13:11 jongfoster - * Making parameters "const" where possible. - * - * Revision 1.9 2001/07/18 17:27:00 oes - * Changed interface; Cosmetics - * - * Revision 1.8 2001/06/29 21:45:41 oes - * Indentation, CRLF->LF, Tab-> Space - * - * Revision 1.7 2001/06/29 13:33:04 oes - * - Cleaned up, renamed and reordered functions, - * improved comments - * - Removed my_strsep - * - Replaced globalflag with a general flags int - * that holds PCRS_GLOBAL, PCRS_SUCCESS, and PCRS_TRIVIAL - * - Introduced trivial option that will prevent pcrs - * from honouring backreferences in the substitute, - * which is useful for large substitutes that are - * red in from somewhere and saves the pain of escaping - * the backrefs - * - Introduced convenience function pcrs_free_joblist() - * - Split pcrs_make_job() into pcrs_compile(), which still - * takes a complete s/// comand as argument and parses it, - * and a new function pcrs_make_job, which takes the - * three separate components. This should make for a - * much friendlier frontend. - * - Removed create_pcrs_job() which was useless - * - Fixed a bug in pcrs_execute - * - Success flag is now handled by pcrs instead of user - * - * Revision 1.6 2001/06/03 19:12:45 oes - * added FIXME - * - * Revision 1.5 2001/05/29 09:50:24 jongfoster - * (Fixed one int -> size_t) - * - * Revision 1.4 2001/05/25 14:12:40 oes - * Fixed bug: Empty substitutes now detected - * - * Revision 1.3 2001/05/25 11:03:55 oes - * Added sanity check for NULL jobs to pcrs_exec_substitution - * - * Revision 1.2 2001/05/22 18:46:04 oes - * - * Added support for PCRE_UNGREEDY behaviour to pcrs, - * which is selected by the (nonstandard and therefore - * capital) letter 'U' in the option string. - * It causes the quantifiers to be ungreedy by default. - * Appending a ? turns back to greedy (!). - * - * Revision 1.1.1.1 2001/05/15 13:59:02 oes - * Initial import of version 2.9.3 source tree - * - * *********************************************************************/ - + + +#include +#include +#include /* * Include project.h just so that the right pcre.h gets @@ -163,8 +48,10 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.23 2006/12/29 17:53:05 fabiankeil Exp $ */ #include "project.h" -#include -#include +/* For snprintf only */ +#include "miscutil.h" +/* For xtoi */ +#include "encode.h" #include "pcrs.h" @@ -177,6 +64,7 @@ const char pcrs_h_rcs[] = PCRS_H_VERSION; static int pcrs_parse_perl_options(const char *optstring, int *flags); static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr); +static int is_hex_sequence(const char *sequence); /********************************************************************* * @@ -192,7 +80,7 @@ static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int tr *********************************************************************/ const char *pcrs_strerror(const int error) { - if (error < 0) + if (error != 0) { switch (error) { @@ -222,6 +110,8 @@ const char *pcrs_strerror(const int error) case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern"; case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute"; case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range"; + case PCRS_WARN_TRUNCATION: + return "(pcrs:) At least one variable was too big and has been truncated before compilation"; /* * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we @@ -402,6 +292,20 @@ static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int tr } i++; } + else if (is_hex_sequence(&replacement[i])) + { + /* + * Replace a hex sequence with a single + * character with the sequence's ascii value. + * e.g.: '\x7e' => '~' + */ + const int ascii_value = xtoi(&replacement[i+2]); + + assert(ascii_value > 0); + assert(ascii_value < 256); + text[k++] = (char)ascii_value; + i += 4; + } else { quoted = 1; @@ -444,7 +348,7 @@ static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int tr } else /* $' or $` */ { - r->backref[l] = PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols); + r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols)); } i += 2; } @@ -493,7 +397,7 @@ plainchar: * Function : pcrs_free_job * * Description : Frees the memory used by a pcrs_job struct and its - * dependant structures. + * dependent structures. * * Parameters : * 1 : job = pointer to the pcrs_job structure to be freed @@ -577,7 +481,7 @@ pcrs_job *pcrs_compile_command(const char *command, int *errptr) char *tokens[4]; pcrs_job *newjob; - i = k = l = 0; + k = l = 0; /* * Tokenize the perl command @@ -778,7 +682,7 @@ int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, c old = subject; *result_length = subject_length; - hits = total_hits = 0; + total_hits = 0; for (job = joblist; job != NULL; job = job->next) { @@ -830,7 +734,7 @@ int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, c * failure, which may be translated to text using pcrs_strerror(). * *********************************************************************/ -int pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **result, size_t *result_length) +int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length) { int offsets[3 * PCRS_MAX_SUBMATCHES], offset, @@ -842,7 +746,7 @@ int pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **res pcrs_match *matches, *dummy; char *result_offset; - offset = i = k = 0; + offset = i = 0; /* * Sanity check & memory allocation @@ -995,6 +899,268 @@ int pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **res } +#define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x))) + +/********************************************************************* + * + * Function : is_hex_sequence + * + * Description : Checks the first four characters of a string + * and decides if they are a valid hex sequence + * (like '\x40'). + * + * Parameters : + * 1 : sequence = The string to check + * + * Returns : Non-zero if it's valid sequence, or + * Zero if it isn't. + * + *********************************************************************/ +static int is_hex_sequence(const char *sequence) +{ + return (sequence[0] == '\\' && + sequence[1] == 'x' && + is_hex_digit(sequence[2]) && + is_hex_digit(sequence[3])); +} + + +/* + * Functions below this line are only part of the pcrs version + * included in Privoxy. If you use any of them you should not + * try to dynamically link against external pcrs versions. + */ + +/********************************************************************* + * + * Function : pcrs_job_is_dynamic + * + * Description : Checks if a job has the "D" (dynamic) option set. + * + * Parameters : + * 1 : job = The job to check + * + * Returns : TRUE if the job is indeed dynamic, otherwise + * FALSE + * + *********************************************************************/ +int pcrs_job_is_dynamic (char *job) +{ + const char delimiter = job[1]; + const size_t length = strlen(job); + char *option; + + if (length < 5) + { + /* + * The shortest valid (but useless) + * dynamic pattern is "s@@@D" + */ + return FALSE; + } + + /* + * Everything between the last character + * and the last delimiter is an option ... + */ + for (option = job + length; *option != delimiter; option--) + { + if (*option == 'D') + { + /* + * ... and if said option is 'D' the job is dynamic. + */ + return TRUE; + } + } + return FALSE; + +} + + +/********************************************************************* + * + * Function : pcrs_get_delimiter + * + * Description : Tries to find a character that is safe to + * be used as a pcrs delimiter for a certain string. + * + * Parameters : + * 1 : string = The string to search in + * + * Returns : A safe delimiter if one was found, otherwise '\0'. + * + *********************************************************************/ +char pcrs_get_delimiter(const char *string) +{ + /* + * Some characters that are unlikely to + * be part of pcrs replacement strings. + */ + char delimiters[] = "><§#+*~%^°-:;µ!@"; + char *d = delimiters; + + /* Take the first delimiter that isn't part of the string */ + while (*d && NULL != strchr(string, *d)) + { + d++; + } + return *d; + +} + + +/********************************************************************* + * + * Function : pcrs_execute_single_command + * + * Description : Apply single pcrs command to the subject. + * The subject itself is left untouched, memory for the result + * is malloc()ed and it is the caller's responsibility to free + * the result when it's no longer needed. + * + * Parameters : + * 1 : subject = the subject (== original) string + * 2 : pcrs_command = the pcrs command as string (s@foo@bar@) + * 3 : hits = int* for returning the number of modifications + * + * Returns : NULL in case of errors, otherwise the + * result of the pcrs command. + * + *********************************************************************/ +char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits) +{ + size_t size; + char *result = NULL; + pcrs_job *job; + + assert(subject); + assert(pcrs_command); + + *hits = 0; + size = strlen(subject); + + job = pcrs_compile_command(pcrs_command, hits); + if (NULL != job) + { + *hits = pcrs_execute(job, subject, size, &result, &size); + if (*hits < 0) + { + freez(result); + } + pcrs_free_job(job); + } + return result; + +} + + +static const char warning[] = "... [too long, truncated]"; +/********************************************************************* + * + * Function : pcrs_compile_dynamic_command + * + * Description : Takes a dynamic pcrs command, fills in the + * values of the variables and compiles it. + * + * Parameters : + * 1 : pcrs_command = The dynamic pcrs command to compile + * 2 : v = NULL terminated array of variables and their values. + * 3 : error = pcrs error code + * + * Returns : NULL in case of hard errors, otherwise the + * compiled pcrs job. + * + *********************************************************************/ +pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error) +{ + char buf[PCRS_BUFFER_SIZE]; + const char *original_pcrs_command = pcrs_command; + char *pcrs_command_tmp = NULL; + pcrs_job *job = NULL; + int truncation = 0; + char d; + int ret; + + while ((NULL != v->name) && (NULL != pcrs_command)) + { + assert(NULL != v->value); + + if (NULL == strstr(pcrs_command, v->name)) + { + /* + * Skip the substitution if the variable + * name isn't part of the pattern. + */ + v++; + continue; + } + + /* Use pcrs to replace the variable with its value. */ + d = pcrs_get_delimiter(v->value); + if ('\0' == d) + { + /* No proper delimiter found */ + *error = PCRS_ERR_CMDSYNTAX; + return NULL; + } + + /* + * Variable names are supposed to contain alpha + * numerical characters plus '_' only. + */ + assert(NULL == strchr(v->name, d)); + + ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d); + assert(ret >= 0); + if (ret >= sizeof(buf)) + { + /* + * Value didn't completely fit into buffer, + * overwrite the end of the substitution text + * with a truncation message and close the pattern + * properly. + */ + const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */ + char *trailer_start = buf + sizeof(buf) - trailer_size; + + ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d); + assert(ret == trailer_size - 1); + assert(sizeof(buf) == strlen(buf) + 1); + truncation = 1; + } + + pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error); + if (NULL == pcrs_command_tmp) + { + return NULL; + } + + if (pcrs_command != original_pcrs_command) + { + freez(pcrs_command); + } + pcrs_command = pcrs_command_tmp; + + v++; + } + + job = pcrs_compile_command(pcrs_command, error); + if (pcrs_command != original_pcrs_command) + { + freez(pcrs_command); + } + + if (truncation) + { + *error = PCRS_WARN_TRUNCATION; + } + + return job; + +} + + /* Local Variables: tab-width: 3