1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.24 2007/01/05 15:46:12 fabiankeil Exp $";
3 /*********************************************************************
5 * File : $Source: /cvsroot/ijbswa/current/pcrs.c,v $
7 * Purpose : pcrs is a supplement to the pcre library by Philip Hazel
8 * <ph10@cam.ac.uk> and adds Perl-style substitution. That
9 * is, it mimics Perl's 's' operator. See pcrs(3) for details.
11 * WARNING: This file contains additional functions and bug
12 * fixes that aren't part of the latest official pcrs package
13 * (which apparently is no longer maintained).
15 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
16 * <andreas@oesterhelt.org>
18 * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
20 * This program is free software; you can redistribute it
21 * and/or modify it under the terms of the GNU Lesser
22 * General Public License (LGPL), version 2.1, which should
23 * be included in this distribution (see LICENSE.txt), with
24 * the exception that the permission to replace that license
25 * with the GNU General Public License (GPL) given in section
26 * 3 is restricted to version 2 of the GPL.
28 * This program is distributed in the hope that it will
29 * be useful, but WITHOUT ANY WARRANTY; without even the
30 * implied warranty of MERCHANTABILITY or FITNESS FOR A
31 * PARTICULAR PURPOSE. See the license for more details.
33 * The GNU Lesser General Public License should be included
34 * with this file. If not, you can view it at
35 * http://www.gnu.org/licenses/lgpl.html
36 * or write to the Free Software Foundation, Inc., 59
37 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 * Revision 1.24 2007/01/05 15:46:12 fabiankeil
42 * Don't use strlen() to calculate the length of
43 * the pcrs substitutes. They don't have to be valid C
44 * strings and getting their length wrong can result in
45 * user-controlled memory corruption.
47 * Thanks to Felix Gröbert for reporting the problem
48 * and providing the fix [#1627140].
50 * Revision 1.23 2006/12/29 17:53:05 fabiankeil
51 * Fixed gcc43 conversion warnings.
53 * Revision 1.22 2006/12/24 17:34:20 fabiankeil
54 * Add pcrs_strerror() message for PCRE_ERROR_MATCHLIMIT
55 * and give a hint why an error code might be unknown.
57 * Catch NULL subjects early in pcrs_execute().
59 * Revision 1.21 2006/07/18 14:48:47 david__schmidt
60 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
61 * with what was really the latest development (the v_3_0_branch branch)
63 * Revision 1.19.2.4 2005/05/07 21:50:55 david__schmidt
64 * A few memory leaks plugged (mostly on error paths)
66 * Revision 1.19.2.3 2003/12/04 12:32:45 oes
67 * Append a trailing nullbyte to result to facilitate string processing
69 * Revision 1.19.2.2 2002/10/08 16:22:28 oes
70 * Bugfix: Need to check validity of backreferences explicitly,
71 * because when max_matches are reached and matches is expanded,
72 * realloc() does not zero the memory. Fixes Bug # 606227
74 * Revision 1.19.2.1 2002/08/10 11:23:40 oes
75 * Include prce.h via project.h, where the appropriate
76 * source will have been selected
78 * Revision 1.19 2002/03/08 14:47:48 oes
81 * Revision 1.18 2002/03/08 14:17:14 oes
82 * Fixing -Wconversion warnings
84 * Revision 1.17 2002/03/08 13:45:48 oes
85 * Hiding internal functions
87 * Revision 1.16 2001/11/30 21:32:14 jongfoster
88 * Fixing signed/unsigned comparison (Andreas please check this!)
91 * Revision 1.15 2001/09/20 16:11:06 steudten
93 * Add casting for some string functions.
95 * Revision 1.14 2001/09/09 21:41:57 oes
96 * Fixing yet another silly bug
98 * Revision 1.13 2001/09/06 14:05:59 oes
101 * Revision 1.12 2001/08/18 11:35:00 oes
102 * - Introduced pcrs_strerror()
103 * - made some NULL arguments non-fatal
104 * - added support for \n \r \e \b \t \f \a \0 in substitute
105 * - made quoting adhere to standard rules
106 * - added warning for bad backrefs
107 * - added pcrs_execute_list()
109 * - bugfix & cosmetics
111 * Revision 1.11 2001/08/15 15:32:03 oes
112 * - Added support for Perl's special variables $+, $' and $`
113 * - Improved the substitute parser
114 * - Replaced the hard limit for the maximum number of matches
115 * by dynamic reallocation
117 * Revision 1.10 2001/08/05 13:13:11 jongfoster
118 * Making parameters "const" where possible.
120 * Revision 1.9 2001/07/18 17:27:00 oes
121 * Changed interface; Cosmetics
123 * Revision 1.8 2001/06/29 21:45:41 oes
124 * Indentation, CRLF->LF, Tab-> Space
126 * Revision 1.7 2001/06/29 13:33:04 oes
127 * - Cleaned up, renamed and reordered functions,
129 * - Removed my_strsep
130 * - Replaced globalflag with a general flags int
131 * that holds PCRS_GLOBAL, PCRS_SUCCESS, and PCRS_TRIVIAL
132 * - Introduced trivial option that will prevent pcrs
133 * from honouring backreferences in the substitute,
134 * which is useful for large substitutes that are
135 * red in from somewhere and saves the pain of escaping
137 * - Introduced convenience function pcrs_free_joblist()
138 * - Split pcrs_make_job() into pcrs_compile(), which still
139 * takes a complete s/// comand as argument and parses it,
140 * and a new function pcrs_make_job, which takes the
141 * three separate components. This should make for a
142 * much friendlier frontend.
143 * - Removed create_pcrs_job() which was useless
144 * - Fixed a bug in pcrs_execute
145 * - Success flag is now handled by pcrs instead of user
147 * Revision 1.6 2001/06/03 19:12:45 oes
150 * Revision 1.5 2001/05/29 09:50:24 jongfoster
151 * (Fixed one int -> size_t)
153 * Revision 1.4 2001/05/25 14:12:40 oes
154 * Fixed bug: Empty substitutes now detected
156 * Revision 1.3 2001/05/25 11:03:55 oes
157 * Added sanity check for NULL jobs to pcrs_exec_substitution
159 * Revision 1.2 2001/05/22 18:46:04 oes
161 * Added support for PCRE_UNGREEDY behaviour to pcrs,
162 * which is selected by the (nonstandard and therefore
163 * capital) letter 'U' in the option string.
164 * It causes the quantifiers to be ungreedy by default.
165 * Appending a ? turns back to greedy (!).
167 * Revision 1.1.1.1 2001/05/15 13:59:02 oes
168 * Initial import of version 2.9.3 source tree
171 *********************************************************************/
175 * Include project.h just so that the right pcre.h gets
176 * included from there
180 /* For snprintf only */
181 #include "miscutil.h"
189 const char pcrs_h_rcs[] = PCRS_H_VERSION;
192 * Internal prototypes
195 static int pcrs_parse_perl_options(const char *optstring, int *flags);
196 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
197 int capturecount, int *errptr);
199 /*********************************************************************
201 * Function : pcrs_strerror
203 * Description : Return a string describing a given error code.
206 * 1 : error = the error code
208 * Returns : char * to the descriptive string
210 *********************************************************************/
211 const char *pcrs_strerror(const int error)
217 /* Passed-through PCRE error: */
218 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
220 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
221 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
222 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
223 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
224 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
226 /* Can't happen / not passed: */
227 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
228 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
230 #ifdef PCRE_ERROR_MATCHLIMIT
232 * Only reported by PCRE versions newer than our own.
234 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
235 #endif /* def PCRE_ERROR_MATCHLIMIT */
238 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
239 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
240 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
241 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
242 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
243 case PCRS_WARN_TRUNCATION:
244 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
247 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
248 * only catch PCRE errors that can happen with our internal
249 * version. If Privoxy is linked against a newer
250 * PCRE version all bets are off ...
252 default: return "Unknown error. Privoxy out of sync with PCRE?";
255 /* error >= 0: No error */
256 return "(pcrs:) Everything's just fine. Thanks for asking.";
261 /*********************************************************************
263 * Function : pcrs_parse_perl_options
265 * Description : This function parses a string containing the options to
266 * Perl's s/// operator. It returns an integer that is the
267 * pcre equivalent of the symbolic optstring.
268 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
269 * 'T' (trivial) options but pcrs needs them, the corresponding
270 * flags are set if 'g'or 'T' is encountered.
271 * Note: The 'T' and 'U' options do not conform to Perl.
274 * 1 : optstring = string with options in perl syntax
275 * 2 : flags = see description
277 * Returns : option integer suitable for pcre
279 *********************************************************************/
280 static int pcrs_parse_perl_options(const char *optstring, int *flags)
286 if (NULL == optstring) return 0;
288 for (i = 0; i < strlen(optstring); i++)
292 case 'e': break; /* ToDo ;-) */
293 case 'g': *flags |= PCRS_GLOBAL; break;
294 case 'i': rc |= PCRE_CASELESS; break;
295 case 'm': rc |= PCRE_MULTILINE; break;
297 case 's': rc |= PCRE_DOTALL; break;
298 case 'x': rc |= PCRE_EXTENDED; break;
299 case 'U': rc |= PCRE_UNGREEDY; break;
300 case 'T': *flags |= PCRS_TRIVIAL; break;
309 /*********************************************************************
311 * Function : pcrs_compile_replacement
313 * Description : This function takes a Perl-style replacement (2nd argument
314 * to the s/// operator and returns a compiled pcrs_substitute,
315 * or NULL if memory allocation for the substitute structure
319 * 1 : replacement = replacement part of s/// operator
321 * 2 : trivialflag = Flag that causes backreferences to be
323 * 3 : capturecount = Number of capturing subpatterns in
324 * the pattern. Needed for $+ handling.
325 * 4 : errptr = pointer to an integer in which error
326 * conditions can be returned.
328 * Returns : pcrs_substitute data structure, or NULL if an
329 * error is encountered. In that case, *errptr has
332 *********************************************************************/
333 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
340 i = k = l = quoted = 0;
345 if (NULL == replacement)
353 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
355 *errptr = PCRS_ERR_NOMEM;
358 memset(r, '\0', sizeof(pcrs_substitute));
360 length = strlen(replacement);
362 if (NULL == (text = (char *)malloc(length + 1)))
365 *errptr = PCRS_ERR_NOMEM;
368 memset(text, '\0', length + 1);
372 * In trivial mode, just copy the substitute text
376 text = strncpy(text, replacement, length + 1);
381 * Else, parse, cut out and record all backreferences
385 while (i < (int)length)
388 if (replacement[i] == '\\')
392 text[k++] = replacement[i++];
397 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
399 switch (replacement[++i])
435 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
437 char *symbol, symbols[] = "'`+&";
438 r->block_length[l] = (size_t)(k - r->block_offset[l]);
440 /* Numerical backreferences */
441 if (isdigit((int)replacement[i + 1]))
443 while (i < (int)length && isdigit((int)replacement[++i]))
445 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
447 if (r->backref[l] > capturecount)
449 *errptr = PCRS_WARN_BADREF;
453 /* Symbolic backreferences: */
454 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
457 if (symbol - symbols == 2) /* $+ */
459 r->backref[l] = capturecount;
461 else if (symbol - symbols == 3) /* $& */
467 r->backref[l] = PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols);
472 /* Invalid backref -> plain '$' */
478 /* Valid and in range? -> record */
479 if (r->backref[l] < PCRS_MAX_SUBMATCHES + 2)
481 r->backref_count[r->backref[l]] += 1;
482 r->block_offset[++l] = k;
486 *errptr = PCRS_WARN_BADREF;
492 /* Plain chars are copied */
493 text[k++] = replacement[i++];
496 } /* -END- if (!trivialflag) */
503 r->length = (size_t)k;
504 r->block_length[l] = (size_t)(k - r->block_offset[l]);
511 /*********************************************************************
513 * Function : pcrs_free_job
515 * Description : Frees the memory used by a pcrs_job struct and its
516 * dependant structures.
519 * 1 : job = pointer to the pcrs_job structure to be freed
521 * Returns : a pointer to the next job, if there was any, or
524 *********************************************************************/
525 pcrs_job *pcrs_free_job(pcrs_job *job)
536 if (job->pattern != NULL) free(job->pattern);
537 if (job->hints != NULL) free(job->hints);
538 if (job->substitute != NULL)
540 if (job->substitute->text != NULL) free(job->substitute->text);
541 free(job->substitute);
550 /*********************************************************************
552 * Function : pcrs_free_joblist
554 * Description : Iterates through a chained list of pcrs_job's and
555 * frees them using pcrs_free_job.
558 * 1 : joblist = pointer to the first pcrs_job structure to
563 *********************************************************************/
564 void pcrs_free_joblist(pcrs_job *joblist)
566 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
573 /*********************************************************************
575 * Function : pcrs_compile_command
577 * Description : Parses a string with a Perl-style s/// command,
578 * calls pcrs_compile, and returns a corresponding
579 * pcrs_job, or NULL if parsing or compiling the job
583 * 1 : command = string with perl-style s/// command
584 * 2 : errptr = pointer to an integer in which error
585 * conditions can be returned.
587 * Returns : a corresponding pcrs_job data structure, or NULL
588 * if an error was encountered. In that case, *errptr
591 *********************************************************************/
592 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
594 int i, k, l, quoted = FALSE;
603 * Tokenize the perl command
605 limit = strlen(command);
608 *errptr = PCRS_ERR_CMDSYNTAX;
613 delimiter = command[1];
616 tokens[l] = (char *) malloc(limit + 1);
618 for (i = 0; i <= (int)limit; i++)
621 if (command[i] == delimiter && !quoted)
628 tokens[0][k++] = '\0';
629 tokens[++l] = tokens[0] + k;
633 else if (command[i] == '\\' && !quoted)
636 if (command[i+1] == delimiter) continue;
642 tokens[0][k++] = command[i];
650 *errptr = PCRS_ERR_CMDSYNTAX;
655 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
662 /*********************************************************************
664 * Function : pcrs_compile
666 * Description : Takes the three arguments to a perl s/// command
667 * and compiles a pcrs_job structure from them.
670 * 1 : pattern = string with perl-style pattern
671 * 2 : substitute = string with perl-style substitute
672 * 3 : options = string with perl-style options
673 * 4 : errptr = pointer to an integer in which error
674 * conditions can be returned.
676 * Returns : a corresponding pcrs_job data structure, or NULL
677 * if an error was encountered. In that case, *errptr
680 *********************************************************************/
681 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
691 * Handle NULL arguments
693 if (pattern == NULL) pattern = "";
694 if (substitute == NULL) substitute = "";
698 * Get and init memory
700 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
702 *errptr = PCRS_ERR_NOMEM;
705 memset(newjob, '\0', sizeof(pcrs_job));
709 * Evaluate the options
711 newjob->options = pcrs_parse_perl_options(options, &flags);
712 newjob->flags = flags;
716 * Compile the pattern
718 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
719 if (newjob->pattern == NULL)
721 pcrs_free_job(newjob);
727 * Generate hints. This has little overhead, since the
728 * hints will be NULL for a boring pattern anyway.
730 newjob->hints = pcre_study(newjob->pattern, 0, &error);
733 *errptr = PCRS_ERR_STUDY;
734 pcrs_free_job(newjob);
740 * Determine the number of capturing subpatterns.
741 * This is needed for handling $+ in the substitute.
743 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
745 pcrs_free_job(newjob);
751 * Compile the substitute
753 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
755 pcrs_free_job(newjob);
764 /*********************************************************************
766 * Function : pcrs_execute_list
768 * Description : This is a multiple job wrapper for pcrs_execute().
769 * Apply the regular substitutions defined by the jobs in
770 * the joblist to the subject.
771 * The subject itself is left untouched, memory for the result
772 * is malloc()ed and it is the caller's responsibility to free
773 * the result when it's no longer needed.
775 * Note: For convenient string handling, a null byte is
776 * appended to the result. It does not count towards the
777 * result_length, though.
781 * 1 : joblist = the chained list of pcrs_jobs to be executed
782 * 2 : subject = the subject string
783 * 3 : subject_length = the subject's length
784 * 4 : result = char** for returning the result
785 * 5 : result_length = size_t* for returning the result's length
787 * Returns : On success, the number of substitutions that were made.
788 * May be > 1 if job->flags contained PCRS_GLOBAL
789 * On failure, the (negative) pcre error code describing the
790 * failure, which may be translated to text using pcrs_strerror().
792 *********************************************************************/
793 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
796 char *old, *new = NULL;
797 int hits, total_hits;
800 *result_length = subject_length;
801 hits = total_hits = 0;
803 for (job = joblist; job != NULL; job = job->next)
805 hits = pcrs_execute(job, old, *result_length, &new, result_length);
807 if (old != subject) free(old);
826 /*********************************************************************
828 * Function : pcrs_execute
830 * Description : Apply the regular substitution defined by the job to the
832 * The subject itself is left untouched, memory for the result
833 * is malloc()ed and it is the caller's responsibility to free
834 * the result when it's no longer needed.
836 * Note: For convenient string handling, a null byte is
837 * appended to the result. It does not count towards the
838 * result_length, though.
841 * 1 : job = the pcrs_job to be executed
842 * 2 : subject = the subject (== original) string
843 * 3 : subject_length = the subject's length
844 * 4 : result = char** for returning the result
845 * 5 : result_length = size_t* for returning the result's length
847 * Returns : On success, the number of substitutions that were made.
848 * May be > 1 if job->flags contained PCRS_GLOBAL
849 * On failure, the (negative) pcre error code describing the
850 * failure, which may be translated to text using pcrs_strerror().
852 *********************************************************************/
853 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
855 int offsets[3 * PCRS_MAX_SUBMATCHES],
860 max_matches = PCRS_MAX_MATCH_INIT;
862 pcrs_match *matches, *dummy;
868 * Sanity check & memory allocation
870 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
873 return(PCRS_ERR_BADJOB);
876 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
879 return(PCRS_ERR_NOMEM);
881 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
885 * Find the pattern and calculate the space
886 * requirements for the result
888 newsize = subject_length;
890 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
892 job->flags |= PCRS_SUCCESS;
893 matches[i].submatches = submatches;
895 for (k = 0; k < submatches; k++)
897 matches[i].submatch_offset[k] = offsets[2 * k];
899 /* Note: Non-found optional submatches have length -1-(-1)==0 */
900 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
902 /* reserve mem for each submatch as often as it is ref'd */
903 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
905 /* plus replacement text size minus match text size */
906 newsize += job->substitute->length - matches[i].submatch_length[0];
908 /* chunk before match */
909 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
910 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
911 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
913 /* chunk after match */
914 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
915 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
916 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
918 /* Storage for matches exhausted? -> Extend! */
919 if (++i >= max_matches)
921 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
922 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
926 return(PCRS_ERR_NOMEM);
931 /* Non-global search or limit reached? */
932 if (!(job->flags & PCRS_GLOBAL)) break;
934 /* Don't loop on empty matches */
935 if (offsets[1] == offset)
936 if ((size_t)offset < subject_length)
940 /* Go find the next one */
944 /* Pass pcre error through if (bad) failiure */
945 if (submatches < PCRE_ERROR_NOMATCH)
954 * Get memory for the result (must be freed by caller!)
955 * and append terminating null byte.
957 if ((*result = (char *)malloc(newsize + 1)) == NULL)
960 return PCRS_ERR_NOMEM;
964 (*result)[newsize] = '\0';
972 result_offset = *result;
974 for (i = 0; i < matches_found; i++)
976 /* copy the chunk preceding the match */
977 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
978 result_offset += matches[i].submatch_offset[0] - offset;
980 /* For every segment of the substitute.. */
981 for (k = 0; k <= job->substitute->backrefs; k++)
983 /* ...copy its text.. */
984 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
985 result_offset += job->substitute->block_length[k];
987 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
988 if (k != job->substitute->backrefs
989 /* ..in legal range.. */
990 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
991 /* ..and referencing a real submatch.. */
992 && job->substitute->backref[k] < matches[i].submatches
993 /* ..that is nonempty.. */
994 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
996 /* ..copy the submatch that is ref'd. */
999 subject + matches[i].submatch_offset[job->substitute->backref[k]],
1000 matches[i].submatch_length[job->substitute->backref[k]]
1002 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
1005 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
1008 /* Copy the rest. */
1009 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
1011 *result_length = newsize;
1013 return matches_found;
1018 * Functions below this line are only part of the pcrs version
1019 * included in Privoxy. If you use any of them you should not
1020 * try to dynamically link against external pcrs versions.
1023 /*********************************************************************
1025 * Function : pcrs_job_is_dynamic
1027 * Description : Checks if a job has the "D" (dynamic) option set.
1030 * 1 : job = The job to check
1032 * Returns : TRUE if the job is indeed dynamic, otherwise
1035 *********************************************************************/
1036 int pcrs_job_is_dynamic (char *job)
1038 const char delimiter = job[1];
1039 const size_t length = strlen(job);
1045 * The shortest valid (but useless)
1046 * dynamic pattern is "s@@@D"
1052 * Everything between the last character
1053 * and the last delimiter is an option ...
1055 for (option = job + length; *option != delimiter; option--)
1060 * ... and if said option is 'D' the job is dynamic.
1070 /*********************************************************************
1072 * Function : pcrs_get_delimiter
1074 * Description : Tries to find a character that is safe to
1075 * be used as a pcrs delimiter for a certain string.
1078 * 1 : string = The string to search in
1080 * Returns : A safe delimiter if one was found, otherwise '\0'.
1082 *********************************************************************/
1083 char pcrs_get_delimiter(const char *string)
1086 * Some characters that are unlikely to
1087 * be part of pcrs replacement strings.
1089 char delimiters[] = "><§#+*~%^°-:;µ!@";
1090 char *d = delimiters;
1092 /* Take the first delimiter that isn't part of the string */
1093 while (*d && NULL != strchr(string, *d))
1103 /*********************************************************************
1105 * Function : pcrs_execute_single_command
1107 * Description : Apply single pcrs command to the subject.
1108 * The subject itself is left untouched, memory for the result
1109 * is malloc()ed and it is the caller's responsibility to free
1110 * the result when it's no longer needed.
1113 * 1 : subject = the subject (== original) string
1114 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1115 * 3 : hits = int* for returning the number of modifications
1117 * Returns : NULL in case of errors, otherwise the
1118 * result of the pcrs command.
1120 *********************************************************************/
1121 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1124 char *result = NULL;
1128 assert(pcrs_command);
1131 size = strlen(subject);
1133 job = pcrs_compile_command(pcrs_command, hits);
1136 *hits = pcrs_execute(job, subject, size, &result, &size);
1148 const static char warning[] = "... [too long, truncated]";
1149 /*********************************************************************
1151 * Function : pcrs_compile_dynamic_command
1153 * Description : Takes a dynamic pcrs command, fills in the
1154 * values of the variables and compiles it.
1157 * 1 : csp = Current client state (buffers, headers, etc...)
1158 * 2 : pcrs_command = The dynamic pcrs command to compile
1159 * 3 : v = NULL terminated array of variables and their values.
1160 * 4 : error = pcrs error code
1162 * Returns : NULL in case of hard errors, otherwise the
1163 * compiled pcrs job.
1165 *********************************************************************/
1166 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1168 char buf[PCRS_BUFFER_SIZE];
1169 const char *original_pcrs_command = pcrs_command;
1170 char *pcrs_command_tmp = NULL;
1171 pcrs_job *job = NULL;
1176 while ((NULL != v->name) && (NULL != pcrs_command))
1178 assert(NULL != v->value);
1180 if (NULL == strstr(pcrs_command, v->name))
1183 * Skip the substitution if the variable
1184 * name isn't part of the pattern.
1190 /* Use pcrs to replace the variable with its value. */
1191 d = pcrs_get_delimiter(v->value);
1194 /* No proper delimiter found */
1195 *error = PCRS_ERR_CMDSYNTAX;
1200 * Variable names are supposed to contain alpha
1201 * numerical characters plus '_' only.
1203 assert(NULL == strchr(v->name, d));
1205 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d);
1207 if (ret >= sizeof(buf))
1210 * Value didn't completely fit into buffer,
1211 * overwrite the end of the substitution text
1212 * with a truncation message and close the pattern
1215 const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */
1216 char *trailer_start = buf + sizeof(buf) - trailer_size;
1218 ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d);
1219 assert(ret == trailer_size - 1);
1220 assert(sizeof(buf) == strlen(buf) + 1);
1224 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1225 if (NULL == pcrs_command_tmp)
1230 if (pcrs_command != original_pcrs_command)
1232 freez(pcrs_command);
1234 pcrs_command = pcrs_command_tmp;
1239 job = pcrs_compile_command(pcrs_command, error);
1240 if (pcrs_command != original_pcrs_command)
1242 freez(pcrs_command);
1247 *error = PCRS_WARN_TRUNCATION;