1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.21 2006/07/18 14:48:47 david__schmidt 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.
12 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
13 * <andreas@oesterhelt.org>
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU Lesser
17 * General Public License (LGPL), version 2.1, which should
18 * be included in this distribution (see LICENSE.txt), with
19 * the exception that the permission to replace that license
20 * with the GNU General Public License (GPL) given in section
21 * 3 is restricted to version 2 of the GPL.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the license for more details.
28 * The GNU Lesser General Public License should be included
29 * with this file. If not, you can view it at
30 * http://www.gnu.org/licenses/lgpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.21 2006/07/18 14:48:47 david__schmidt
37 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
38 * with what was really the latest development (the v_3_0_branch branch)
40 * Revision 1.19.2.4 2005/05/07 21:50:55 david__schmidt
41 * A few memory leaks plugged (mostly on error paths)
43 * Revision 1.19.2.3 2003/12/04 12:32:45 oes
44 * Append a trailing nullbyte to result to facilitate string processing
46 * Revision 1.19.2.2 2002/10/08 16:22:28 oes
47 * Bugfix: Need to check validity of backreferences explicitly,
48 * because when max_matches are reached and matches is expanded,
49 * realloc() does not zero the memory. Fixes Bug # 606227
51 * Revision 1.19.2.1 2002/08/10 11:23:40 oes
52 * Include prce.h via project.h, where the appropriate
53 * source will have been selected
55 * Revision 1.19 2002/03/08 14:47:48 oes
58 * Revision 1.18 2002/03/08 14:17:14 oes
59 * Fixing -Wconversion warnings
61 * Revision 1.17 2002/03/08 13:45:48 oes
62 * Hiding internal functions
64 * Revision 1.16 2001/11/30 21:32:14 jongfoster
65 * Fixing signed/unsigned comparison (Andreas please check this!)
68 * Revision 1.15 2001/09/20 16:11:06 steudten
70 * Add casting for some string functions.
72 * Revision 1.14 2001/09/09 21:41:57 oes
73 * Fixing yet another silly bug
75 * Revision 1.13 2001/09/06 14:05:59 oes
78 * Revision 1.12 2001/08/18 11:35:00 oes
79 * - Introduced pcrs_strerror()
80 * - made some NULL arguments non-fatal
81 * - added support for \n \r \e \b \t \f \a \0 in substitute
82 * - made quoting adhere to standard rules
83 * - added warning for bad backrefs
84 * - added pcrs_execute_list()
86 * - bugfix & cosmetics
88 * Revision 1.11 2001/08/15 15:32:03 oes
89 * - Added support for Perl's special variables $+, $' and $`
90 * - Improved the substitute parser
91 * - Replaced the hard limit for the maximum number of matches
92 * by dynamic reallocation
94 * Revision 1.10 2001/08/05 13:13:11 jongfoster
95 * Making parameters "const" where possible.
97 * Revision 1.9 2001/07/18 17:27:00 oes
98 * Changed interface; Cosmetics
100 * Revision 1.8 2001/06/29 21:45:41 oes
101 * Indentation, CRLF->LF, Tab-> Space
103 * Revision 1.7 2001/06/29 13:33:04 oes
104 * - Cleaned up, renamed and reordered functions,
106 * - Removed my_strsep
107 * - Replaced globalflag with a general flags int
108 * that holds PCRS_GLOBAL, PCRS_SUCCESS, and PCRS_TRIVIAL
109 * - Introduced trivial option that will prevent pcrs
110 * from honouring backreferences in the substitute,
111 * which is useful for large substitutes that are
112 * red in from somewhere and saves the pain of escaping
114 * - Introduced convenience function pcrs_free_joblist()
115 * - Split pcrs_make_job() into pcrs_compile(), which still
116 * takes a complete s/// comand as argument and parses it,
117 * and a new function pcrs_make_job, which takes the
118 * three separate components. This should make for a
119 * much friendlier frontend.
120 * - Removed create_pcrs_job() which was useless
121 * - Fixed a bug in pcrs_execute
122 * - Success flag is now handled by pcrs instead of user
124 * Revision 1.6 2001/06/03 19:12:45 oes
127 * Revision 1.5 2001/05/29 09:50:24 jongfoster
128 * (Fixed one int -> size_t)
130 * Revision 1.4 2001/05/25 14:12:40 oes
131 * Fixed bug: Empty substitutes now detected
133 * Revision 1.3 2001/05/25 11:03:55 oes
134 * Added sanity check for NULL jobs to pcrs_exec_substitution
136 * Revision 1.2 2001/05/22 18:46:04 oes
138 * Added support for PCRE_UNGREEDY behaviour to pcrs,
139 * which is selected by the (nonstandard and therefore
140 * capital) letter 'U' in the option string.
141 * It causes the quantifiers to be ungreedy by default.
142 * Appending a ? turns back to greedy (!).
144 * Revision 1.1.1.1 2001/05/15 13:59:02 oes
145 * Initial import of version 2.9.3 source tree
148 *********************************************************************/
152 * Include project.h just so that the right pcre.h gets
153 * included from there
162 const char pcrs_h_rcs[] = PCRS_H_VERSION;
165 * Internal prototypes
168 static int pcrs_parse_perl_options(const char *optstring, int *flags);
169 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
170 int capturecount, int *errptr);
172 /*********************************************************************
174 * Function : pcrs_strerror
176 * Description : Return a string describing a given error code.
179 * 1 : error = the error code
181 * Returns : char * to the descriptive string
183 *********************************************************************/
184 const char *pcrs_strerror(const int error)
190 /* Passed-through PCRE error: */
191 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
193 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
194 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
195 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
196 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
197 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
199 /* Can't happen / not passed: */
200 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
201 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
203 #ifdef PCRE_ERROR_MATCHLIMIT
205 * Only reported by PCRE versions newer than our own.
207 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
208 #endif /* def PCRE_ERROR_MATCHLIMIT */
211 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
212 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
213 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
214 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
215 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
218 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
219 * only catch PCRE errors that can happen with our internal
220 * version. If Privoxy is linked against a newer
221 * PCRE version all bets are off ...
223 default: return "Unknown error. Privoxy out of sync with PCRE?";
226 /* error >= 0: No error */
227 return "(pcrs:) Everything's just fine. Thanks for asking.";
232 /*********************************************************************
234 * Function : pcrs_parse_perl_options
236 * Description : This function parses a string containing the options to
237 * Perl's s/// operator. It returns an integer that is the
238 * pcre equivalent of the symbolic optstring.
239 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
240 * 'T' (trivial) options but pcrs needs them, the corresponding
241 * flags are set if 'g'or 'T' is encountered.
242 * Note: The 'T' and 'U' options do not conform to Perl.
245 * 1 : optstring = string with options in perl syntax
246 * 2 : flags = see description
248 * Returns : option integer suitable for pcre
250 *********************************************************************/
251 static int pcrs_parse_perl_options(const char *optstring, int *flags)
257 if (NULL == optstring) return 0;
259 for (i = 0; i < strlen(optstring); i++)
263 case 'e': break; /* ToDo ;-) */
264 case 'g': *flags |= PCRS_GLOBAL; break;
265 case 'i': rc |= PCRE_CASELESS; break;
266 case 'm': rc |= PCRE_MULTILINE; break;
268 case 's': rc |= PCRE_DOTALL; break;
269 case 'x': rc |= PCRE_EXTENDED; break;
270 case 'U': rc |= PCRE_UNGREEDY; break;
271 case 'T': *flags |= PCRS_TRIVIAL; break;
280 /*********************************************************************
282 * Function : pcrs_compile_replacement
284 * Description : This function takes a Perl-style replacement (2nd argument
285 * to the s/// operator and returns a compiled pcrs_substitute,
286 * or NULL if memory allocation for the substitute structure
290 * 1 : replacement = replacement part of s/// operator
292 * 2 : trivialflag = Flag that causes backreferences to be
294 * 3 : capturecount = Number of capturing subpatterns in
295 * the pattern. Needed for $+ handling.
296 * 4 : errptr = pointer to an integer in which error
297 * conditions can be returned.
299 * Returns : pcrs_substitute data structure, or NULL if an
300 * error is encountered. In that case, *errptr has
303 *********************************************************************/
304 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
311 i = k = l = quoted = 0;
316 if (NULL == replacement)
324 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
326 *errptr = PCRS_ERR_NOMEM;
329 memset(r, '\0', sizeof(pcrs_substitute));
331 length = strlen(replacement);
333 if (NULL == (text = (char *)malloc(length + 1)))
336 *errptr = PCRS_ERR_NOMEM;
339 memset(text, '\0', length + 1);
343 * In trivial mode, just copy the substitute text
347 text = strncpy(text, replacement, length + 1);
352 * Else, parse, cut out and record all backreferences
356 while (i < (int)length)
359 if (replacement[i] == '\\')
363 text[k++] = replacement[i++];
368 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
370 switch (replacement[++i])
406 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
408 char *symbol, symbols[] = "'`+&";
409 r->block_length[l] = k - r->block_offset[l];
411 /* Numerical backreferences */
412 if (isdigit((int)replacement[i + 1]))
414 while (i < (int)length && isdigit((int)replacement[++i]))
416 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
418 if (r->backref[l] > capturecount)
420 *errptr = PCRS_WARN_BADREF;
424 /* Symbolic backreferences: */
425 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
428 if (symbol - symbols == 2) /* $+ */
430 r->backref[l] = capturecount;
432 else if (symbol - symbols == 3) /* $& */
438 r->backref[l] = PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols);
443 /* Invalid backref -> plain '$' */
449 /* Valid and in range? -> record */
450 if (r->backref[l] < PCRS_MAX_SUBMATCHES + 2)
452 r->backref_count[r->backref[l]] += 1;
453 r->block_offset[++l] = k;
457 *errptr = PCRS_WARN_BADREF;
463 /* Plain chars are copied */
464 text[k++] = replacement[i++];
467 } /* -END- if (!trivialflag) */
474 r->block_length[l] = k - r->block_offset[l];
481 /*********************************************************************
483 * Function : pcrs_free_job
485 * Description : Frees the memory used by a pcrs_job struct and its
486 * dependant structures.
489 * 1 : job = pointer to the pcrs_job structure to be freed
491 * Returns : a pointer to the next job, if there was any, or
494 *********************************************************************/
495 pcrs_job *pcrs_free_job(pcrs_job *job)
506 if (job->pattern != NULL) free(job->pattern);
507 if (job->hints != NULL) free(job->hints);
508 if (job->substitute != NULL)
510 if (job->substitute->text != NULL) free(job->substitute->text);
511 free(job->substitute);
520 /*********************************************************************
522 * Function : pcrs_free_joblist
524 * Description : Iterates through a chained list of pcrs_job's and
525 * frees them using pcrs_free_job.
528 * 1 : joblist = pointer to the first pcrs_job structure to
533 *********************************************************************/
534 void pcrs_free_joblist(pcrs_job *joblist)
536 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
543 /*********************************************************************
545 * Function : pcrs_compile_command
547 * Description : Parses a string with a Perl-style s/// command,
548 * calls pcrs_compile, and returns a corresponding
549 * pcrs_job, or NULL if parsing or compiling the job
553 * 1 : command = string with perl-style s/// command
554 * 2 : errptr = pointer to an integer in which error
555 * conditions can be returned.
557 * Returns : a corresponding pcrs_job data structure, or NULL
558 * if an error was encountered. In that case, *errptr
561 *********************************************************************/
562 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
564 int i, k, l, quoted = FALSE;
573 * Tokenize the perl command
575 limit = strlen(command);
578 *errptr = PCRS_ERR_CMDSYNTAX;
583 delimiter = command[1];
586 tokens[l] = (char *) malloc(limit + 1);
588 for (i = 0; i <= (int)limit; i++)
591 if (command[i] == delimiter && !quoted)
598 tokens[0][k++] = '\0';
599 tokens[++l] = tokens[0] + k;
603 else if (command[i] == '\\' && !quoted)
606 if (command[i+1] == delimiter) continue;
612 tokens[0][k++] = command[i];
620 *errptr = PCRS_ERR_CMDSYNTAX;
625 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
632 /*********************************************************************
634 * Function : pcrs_compile
636 * Description : Takes the three arguments to a perl s/// command
637 * and compiles a pcrs_job structure from them.
640 * 1 : pattern = string with perl-style pattern
641 * 2 : substitute = string with perl-style substitute
642 * 3 : options = string with perl-style options
643 * 4 : errptr = pointer to an integer in which error
644 * conditions can be returned.
646 * Returns : a corresponding pcrs_job data structure, or NULL
647 * if an error was encountered. In that case, *errptr
650 *********************************************************************/
651 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
661 * Handle NULL arguments
663 if (pattern == NULL) pattern = "";
664 if (substitute == NULL) substitute = "";
668 * Get and init memory
670 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
672 *errptr = PCRS_ERR_NOMEM;
675 memset(newjob, '\0', sizeof(pcrs_job));
679 * Evaluate the options
681 newjob->options = pcrs_parse_perl_options(options, &flags);
682 newjob->flags = flags;
686 * Compile the pattern
688 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
689 if (newjob->pattern == NULL)
691 pcrs_free_job(newjob);
697 * Generate hints. This has little overhead, since the
698 * hints will be NULL for a boring pattern anyway.
700 newjob->hints = pcre_study(newjob->pattern, 0, &error);
703 *errptr = PCRS_ERR_STUDY;
704 pcrs_free_job(newjob);
710 * Determine the number of capturing subpatterns.
711 * This is needed for handling $+ in the substitute.
713 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
715 pcrs_free_job(newjob);
721 * Compile the substitute
723 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
725 pcrs_free_job(newjob);
734 /*********************************************************************
736 * Function : pcrs_execute_list
738 * Description : This is a multiple job wrapper for pcrs_execute().
739 * Apply the regular substitutions defined by the jobs in
740 * the joblist to the subject.
741 * The subject itself is left untouched, memory for the result
742 * is malloc()ed and it is the caller's responsibility to free
743 * the result when it's no longer needed.
745 * Note: For convenient string handling, a null byte is
746 * appended to the result. It does not count towards the
747 * result_length, though.
751 * 1 : joblist = the chained list of pcrs_jobs to be executed
752 * 2 : subject = the subject string
753 * 3 : subject_length = the subject's length
754 * 4 : result = char** for returning the result
755 * 5 : result_length = size_t* for returning the result's length
757 * Returns : On success, the number of substitutions that were made.
758 * May be > 1 if job->flags contained PCRS_GLOBAL
759 * On failure, the (negative) pcre error code describing the
760 * failure, which may be translated to text using pcrs_strerror().
762 *********************************************************************/
763 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
766 char *old, *new = NULL;
767 int hits, total_hits;
770 *result_length = subject_length;
771 hits = total_hits = 0;
773 for (job = joblist; job != NULL; job = job->next)
775 hits = pcrs_execute(job, old, *result_length, &new, result_length);
777 if (old != subject) free(old);
796 /*********************************************************************
798 * Function : pcrs_execute
800 * Description : Apply the regular substitution defined by the job to the
802 * The subject itself is left untouched, memory for the result
803 * is malloc()ed and it is the caller's responsibility to free
804 * the result when it's no longer needed.
806 * Note: For convenient string handling, a null byte is
807 * appended to the result. It does not count towards the
808 * result_length, though.
811 * 1 : job = the pcrs_job to be executed
812 * 2 : subject = the subject (== original) string
813 * 3 : subject_length = the subject's length
814 * 4 : result = char** for returning the result
815 * 5 : result_length = size_t* for returning the result's length
817 * Returns : On success, the number of substitutions that were made.
818 * May be > 1 if job->flags contained PCRS_GLOBAL
819 * On failure, the (negative) pcre error code describing the
820 * failure, which may be translated to text using pcrs_strerror().
822 *********************************************************************/
823 int pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **result, size_t *result_length)
825 int offsets[3 * PCRS_MAX_SUBMATCHES],
830 max_matches = PCRS_MAX_MATCH_INIT;
832 pcrs_match *matches, *dummy;
838 * Sanity check & memory allocation
840 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
843 return(PCRS_ERR_BADJOB);
846 if (NULL == (matches = (pcrs_match *)malloc(max_matches * sizeof(pcrs_match))))
849 return(PCRS_ERR_NOMEM);
851 memset(matches, '\0', max_matches * sizeof(pcrs_match));
855 * Find the pattern and calculate the space
856 * requirements for the result
858 newsize = subject_length;
860 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
862 job->flags |= PCRS_SUCCESS;
863 matches[i].submatches = submatches;
865 for (k = 0; k < submatches; k++)
867 matches[i].submatch_offset[k] = offsets[2 * k];
869 /* Note: Non-found optional submatches have length -1-(-1)==0 */
870 matches[i].submatch_length[k] = offsets[2 * k + 1] - offsets[2 * k];
872 /* reserve mem for each submatch as often as it is ref'd */
873 newsize += matches[i].submatch_length[k] * job->substitute->backref_count[k];
875 /* plus replacement text size minus match text size */
876 newsize += strlen(job->substitute->text) - matches[i].submatch_length[0];
878 /* chunk before match */
879 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
880 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = offsets[0];
881 newsize += offsets[0] * job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
883 /* chunk after match */
884 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
885 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - offsets[1] - 1;
886 newsize += (subject_length - offsets[1]) * job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
888 /* Storage for matches exhausted? -> Extend! */
889 if (++i >= max_matches)
891 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
892 if (NULL == (dummy = (pcrs_match *)realloc(matches, max_matches * sizeof(pcrs_match))))
896 return(PCRS_ERR_NOMEM);
901 /* Non-global search or limit reached? */
902 if (!(job->flags & PCRS_GLOBAL)) break;
904 /* Don't loop on empty matches */
905 if (offsets[1] == offset)
906 if ((size_t)offset < subject_length)
910 /* Go find the next one */
914 /* Pass pcre error through if (bad) failiure */
915 if (submatches < PCRE_ERROR_NOMATCH)
924 * Get memory for the result (must be freed by caller!)
925 * and append terminating null byte.
927 if ((*result = (char *)malloc(newsize + 1)) == NULL)
930 return PCRS_ERR_NOMEM;
934 (*result)[newsize] = '\0';
942 result_offset = *result;
944 for (i = 0; i < matches_found; i++)
946 /* copy the chunk preceding the match */
947 memcpy(result_offset, subject + offset, (size_t)matches[i].submatch_offset[0] - offset);
948 result_offset += matches[i].submatch_offset[0] - offset;
950 /* For every segment of the substitute.. */
951 for (k = 0; k <= job->substitute->backrefs; k++)
953 /* ...copy its text.. */
954 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
955 result_offset += job->substitute->block_length[k];
957 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
958 if (k != job->substitute->backrefs
959 /* ..in legal range.. */
960 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
961 /* ..and referencing a real submatch.. */
962 && job->substitute->backref[k] < matches[i].submatches
963 /* ..that is nonempty.. */
964 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
966 /* ..copy the submatch that is ref'd. */
969 subject + matches[i].submatch_offset[job->substitute->backref[k]],
970 matches[i].submatch_length[job->substitute->backref[k]]
972 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
975 offset = matches[i].submatch_offset[0] + matches[i].submatch_length[0];
979 memcpy(result_offset, subject + offset, subject_length - offset);
981 *result_length = newsize;
983 return matches_found;