1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/pcrs.c,v $
5 * Purpose : pcrs is a supplement to the pcre library by Philip Hazel
6 * <ph10@cam.ac.uk> and adds Perl-style substitution. That
7 * is, it mimics Perl's 's' operator. See pcrs(3) for details.
9 * WARNING: This file contains additional functions and bug
10 * fixes that aren't part of the latest official pcrs package
11 * (which apparently is no longer maintained).
13 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
14 * <andreas@oesterhelt.org>
16 * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 *********************************************************************/
44 * Include project.h just so that the right pcre.h gets
49 /* For snprintf only */
60 static int pcrs_parse_perl_options(const char *optstring, unsigned int *flags);
61 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
62 int capturecount, int *errptr);
63 static int is_hex_sequence(const char *sequence);
65 /*********************************************************************
67 * Function : pcrs_strerror
69 * Description : Return a string describing a given error code.
72 * 1 : error = the error code
74 * Returns : char * to the descriptive string
76 *********************************************************************/
77 const char *pcrs_strerror(const int error)
85 /* Passed-through PCRE error: */
86 case PCREn(ERROR_NOMEMORY): return "(pcre:) No memory";
88 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
89 case PCREn(ERROR_NULL): return "(pcre:) NULL code or subject or ovector";
90 case PCREn(ERROR_BADOPTION): return "(pcre:) Unrecognized option bit";
91 case PCREn(ERROR_BADMAGIC): return "(pcre:) Bad magic number in code";
92 #if defined(PCRE_ERROR_UNKNOWN_NODE)
93 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
95 /* Can't happen / not passed: */
96 case PCREn(ERROR_NOSUBSTRING): return "(pcre:) Fire in power supply";
97 case PCREn(ERROR_NOMATCH): return "(pcre:) Water in power supply";
99 #ifdef PCRE_ERROR_MATCHLIMIT
101 * Only reported by PCRE versions newer than our own.
103 case PCREn(ERROR_MATCHLIMIT): return "(pcre:) Match limit reached";
104 #endif /* def PCRE_ERROR_MATCHLIMIT */
106 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
107 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
108 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
109 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
110 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
111 case PCRS_WARN_TRUNCATION:
112 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
116 pcre2_get_error_message(error, (PCRE2_UCHAR8*)buf, sizeof(buf));
118 snprintf(buf, sizeof(buf),
119 "Error code %d. For details, check the pcre documentation.",
125 /* error >= 0: No error */
126 return "(pcrs:) Everything's just fine. Thanks for asking.";
131 /*********************************************************************
133 * Function : pcrs_parse_perl_options
135 * Description : This function parses a string containing the options to
136 * Perl's s/// operator. It returns an integer that is the
137 * pcre equivalent of the symbolic optstring.
138 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
139 * 'T' (trivial) options but pcrs needs them, the corresponding
140 * flags are set if 'g'or 'T' is encountered.
141 * Note: The 'T' and 'U' options do not conform to Perl.
144 * 1 : optstring = string with options in perl syntax
145 * 2 : flags = see description
147 * Returns : option integer suitable for pcre
149 *********************************************************************/
150 static int pcrs_parse_perl_options(const char *optstring, unsigned int *flags)
156 if (NULL == optstring) return 0;
158 for (i = 0; i < strlen(optstring); i++)
162 case 'e': break; /* ToDo ;-) */
163 case 'g': *flags |= PCRS_GLOBAL; break;
164 case 'i': rc |= PCREn(CASELESS); break;
165 case 'm': rc |= PCREn(MULTILINE); break;
167 case 's': rc |= PCREn(DOTALL); break;
168 case 'x': rc |= PCREn(EXTENDED); break;
169 case 'D': *flags |= PCRS_DYNAMIC; break;
170 case 'U': rc |= PCREn(UNGREEDY); break;
171 case 'T': *flags |= PCRS_TRIVIAL; break;
181 /*********************************************************************
183 * Function : pcrs_compile_fuzzed_replacement
185 * Description : Wrapper around pcrs_compile_replacement() for
189 * 1 : replacement = replacement part of s/// operator
191 * 2 : errptr = pointer to an integer in which error
192 * conditions can be returned.
194 * Returns : pcrs_substitute data structure, or NULL if an
195 * error is encountered. In that case, *errptr has
198 *********************************************************************/
199 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr)
201 int capturecount = PCRS_MAX_SUBMATCHES; /* XXX: fuzzworthy? */
202 int trivial_flag = 0; /* We don't want to fuzz strncpy() */
204 *errptr = 0; /* XXX: Should pcrs_compile_replacement() do this? */
206 return pcrs_compile_replacement(replacement, trivial_flag, capturecount, errptr);
212 /*********************************************************************
214 * Function : pcrs_compile_replacement
216 * Description : This function takes a Perl-style replacement (2nd argument
217 * to the s/// operator and returns a compiled pcrs_substitute,
218 * or NULL if memory allocation for the substitute structure
222 * 1 : replacement = replacement part of s/// operator
224 * 2 : trivialflag = Flag that causes backreferences to be
226 * 3 : capturecount = Number of capturing subpatterns in
227 * the pattern. Needed for $+ handling.
228 * 4 : errptr = pointer to an integer in which error
229 * conditions can be returned.
231 * Returns : pcrs_substitute data structure, or NULL if an
232 * error is encountered. In that case, *errptr has
235 *********************************************************************/
236 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
244 static size_t length;
246 i = k = l = quoted = 0;
251 if (NULL == replacement)
259 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
261 *errptr = PCRS_ERR_NOMEM;
264 memset(r, '\0', sizeof(pcrs_substitute));
266 length = strlen(replacement);
268 if (NULL == (text = (char *)malloc(length + 1)))
271 *errptr = PCRS_ERR_NOMEM;
274 memset(text, '\0', length + 1);
278 * In trivial mode, just copy the substitute text
282 strlcpy(text, replacement, length + 1);
287 * Else, parse, cut out and record all backreferences
291 while (i < (int)length)
294 if (replacement[i] == '\\')
298 text[k++] = replacement[i++];
303 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
305 switch (replacement[++i])
331 else if (is_hex_sequence(&replacement[i]))
334 * Replace a hex sequence with a single
335 * character with the sequence's ascii value.
336 * e.g.: '\x7e' => '~'
338 const int ascii_value = xtoi(&replacement[i+2]);
340 assert(ascii_value >= 0);
341 assert(ascii_value < 256);
342 text[k++] = (char)ascii_value;
355 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
357 char *symbol, symbols[] = "'`+&";
358 if (l >= PCRS_MAX_SUBMATCHES)
362 *errptr = PCRS_WARN_BADREF;
365 r->block_length[l] = (size_t)(k - r->block_offset[l]);
367 /* Numerical backreferences */
368 if (isdigit((int)replacement[i + 1]))
370 while (i < (int)length && isdigit((int)replacement[++i]))
372 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
374 if (r->backref[l] > capturecount)
378 *errptr = PCRS_WARN_BADREF;
383 /* Symbolic backreferences: */
384 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
387 if (symbol - symbols == 2) /* $+ */
389 r->backref[l] = capturecount;
391 else if (symbol - symbols == 3) /* $& */
397 r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols));
402 /* Invalid backref -> plain '$' */
408 assert(r->backref[l] < PCRS_MAX_SUBMATCHES + 2);
409 /* Valid and in range? -> record */
410 if ((0 <= r->backref[l]) &&
411 (r->backref[l] < PCRS_MAX_SUBMATCHES + 2) &&
412 (l < PCRS_MAX_SUBMATCHES - 1))
414 r->backref_count[r->backref[l]] += 1;
415 r->block_offset[++l] = k;
421 *errptr = PCRS_WARN_BADREF;
428 /* Plain chars are copied */
429 text[k++] = replacement[i++];
432 } /* -END- if (!trivialflag) */
439 r->length = (size_t)k;
440 r->block_length[l] = (size_t)(k - r->block_offset[l]);
447 /*********************************************************************
449 * Function : pcrs_free_job
451 * Description : Frees the memory used by a pcrs_job struct and its
452 * dependent structures.
455 * 1 : job = pointer to the pcrs_job structure to be freed
457 * Returns : a pointer to the next job, if there was any, or
460 *********************************************************************/
461 pcrs_job *pcrs_free_job(pcrs_job *job)
472 if (job->pattern != NULL)
475 pcre2_code_free(job->pattern);
481 if (job->hints != NULL)
483 #ifdef PCRE_CONFIG_JIT
484 pcre_free_study(job->hints);
490 if (job->substitute != NULL)
492 if (job->substitute->text != NULL) free(job->substitute->text);
493 free(job->substitute);
502 /*********************************************************************
504 * Function : pcrs_free_joblist
506 * Description : Iterates through a chained list of pcrs_job's and
507 * frees them using pcrs_free_job.
510 * 1 : joblist = pointer to the first pcrs_job structure to
515 *********************************************************************/
516 void pcrs_free_joblist(pcrs_job *joblist)
518 while (NULL != (joblist = pcrs_free_job(joblist))) {};
525 /*********************************************************************
527 * Function : pcrs_compile_command
529 * Description : Parses a string with a Perl-style s/// command,
530 * calls pcrs_compile, and returns a corresponding
531 * pcrs_job, or NULL if parsing or compiling the job
535 * 1 : command = string with perl-style s/// command
536 * 2 : errptr = pointer to an integer in which error
537 * conditions can be returned.
539 * Returns : a corresponding pcrs_job data structure, or NULL
540 * if an error was encountered. In that case, *errptr
543 *********************************************************************/
544 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
546 int i, k, l, quoted = FALSE;
555 * Tokenize the perl command
557 limit = strlen(command);
560 *errptr = PCRS_ERR_CMDSYNTAX;
565 delimiter = command[1];
568 tokens[l] = (char *) malloc(limit + 1);
570 for (i = 0; i <= (int)limit; i++)
573 if (command[i] == delimiter && !quoted)
580 tokens[0][k++] = '\0';
581 tokens[++l] = tokens[0] + k;
585 else if (command[i] == '\\' && !quoted)
588 if (command[i+1] == delimiter) continue;
594 tokens[0][k++] = command[i];
602 *errptr = PCRS_ERR_CMDSYNTAX;
607 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
614 /*********************************************************************
616 * Function : pcrs_compile
618 * Description : Takes the three arguments to a perl s/// command
619 * and compiles a pcrs_job structure from them.
622 * 1 : pattern = string with perl-style pattern
623 * 2 : substitute = string with perl-style substitute
624 * 3 : options = string with perl-style options
625 * 4 : errptr = pointer to an integer in which error
626 * conditions can be returned.
628 * Returns : a corresponding pcrs_job data structure, or NULL
629 * if an error was encountered. In that case, *errptr
632 *********************************************************************/
633 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
641 int pcre_study_options = 0;
648 * Handle NULL arguments
650 if (pattern == NULL) pattern = "";
651 if (substitute == NULL) substitute = "";
655 * Get and init memory
657 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
659 *errptr = PCRS_ERR_NOMEM;
662 memset(newjob, '\0', sizeof(pcrs_job));
666 * Evaluate the options
668 newjob->options = pcrs_parse_perl_options(options, &flags);
669 newjob->flags = flags;
673 * Compile the pattern
676 PCRE2_SIZE error_offset;
677 newjob->pattern = pcre2_compile((const unsigned char *)pattern,
678 PCRE2_ZERO_TERMINATED, (unsigned)newjob->options, errptr,
679 &error_offset, NULL);
681 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
683 if (newjob->pattern == NULL)
685 pcrs_free_job(newjob);
689 #if defined(PCRE_STUDY_JIT_COMPILE) || defined(HAVE_PCRE2)
690 #ifdef DISABLE_PCRE_JIT_COMPILATION
691 #warning PCRE_STUDY_JIT_COMPILE is supported but Privoxy has been configured not to use it
693 if (!(flags & PCRS_DYNAMIC))
696 /* Try to enable JIT compilation but continue if it's unsupported. */
697 if ((ret = pcre2_jit_compile(newjob->pattern, PCRE2_JIT_COMPLETE)) &&
698 (ret != PCRE2_ERROR_JIT_BADOPTION))
701 pcrs_free_job(newjob);
705 pcre_study_options = PCRE_STUDY_JIT_COMPILE;
713 * Generate hints. This has little overhead, since the
714 * hints will be NULL for a boring pattern anyway.
716 newjob->hints = pcre_study(newjob->pattern, pcre_study_options, &error);
719 *errptr = PCRS_ERR_STUDY;
720 pcrs_free_job(newjob);
726 * Determine the number of capturing subpatterns.
727 * This is needed for handling $+ in the substitute.
730 if (0 > (*errptr = pcre2_pattern_info(newjob->pattern, PCRE2_INFO_CAPTURECOUNT, &capturecount)))
732 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
735 pcrs_free_job(newjob);
741 * Compile the substitute
743 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
745 pcrs_free_job(newjob);
754 /*********************************************************************
756 * Function : pcrs_execute_list
758 * Description : This is a multiple job wrapper for pcrs_execute().
759 * Apply the regular substitutions defined by the jobs in
760 * the joblist to the subject.
761 * The subject itself is left untouched, memory for the result
762 * is malloc()ed and it is the caller's responsibility to free
763 * the result when it's no longer needed.
765 * Note: For convenient string handling, a null byte is
766 * appended to the result. It does not count towards the
767 * result_length, though.
771 * 1 : joblist = the chained list of pcrs_jobs to be executed
772 * 2 : subject = the subject string
773 * 3 : subject_length = the subject's length
774 * 4 : result = char** for returning the result
775 * 5 : result_length = size_t* for returning the result's length
777 * Returns : On success, the number of substitutions that were made.
778 * May be > 1 if job->flags contained PCRS_GLOBAL
779 * On failure, the (negative) pcre error code describing the
780 * failure, which may be translated to text using pcrs_strerror().
782 *********************************************************************/
783 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
786 char *old, *new = NULL;
787 int hits, total_hits;
790 *result_length = subject_length;
793 for (job = joblist; job != NULL; job = job->next)
795 hits = pcrs_execute(job, old, *result_length, &new, result_length);
797 if (old != subject) free(old);
816 /*********************************************************************
818 * Function : pcrs_execute
820 * Description : Apply the regular substitution defined by the job to the
822 * The subject itself is left untouched, memory for the result
823 * is malloc()ed and it is the caller's responsibility to free
824 * the result when it's no longer needed.
826 * Note: For convenient string handling, a null byte is
827 * appended to the result. It does not count towards the
828 * result_length, though.
831 * 1 : job = the pcrs_job to be executed
832 * 2 : subject = the subject (== original) string
833 * 3 : subject_length = the subject's length
834 * 4 : result = char** for returning the result (NULL on error)
835 * 5 : result_length = size_t* for returning the result's length
837 * Returns : On success, the number of substitutions that were made.
838 * May be > 1 if job->flags contained PCRS_GLOBAL
839 * On failure, the (negative) pcre error code describing the
840 * failure, which may be translated to text using pcrs_strerror().
842 *********************************************************************/
843 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
849 max_matches = PCRS_MAX_MATCH_INIT;
852 pcrs_match *matches, *dummy;
853 pcre2_match_data *pcre2_matches;
856 pcrs_match *matches, *dummy;
857 int offsets[3 * PCRS_MAX_SUBMATCHES];
865 * Sanity check & memory allocation
867 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
869 return(PCRS_ERR_BADJOB);
873 if (NULL == (pcre2_matches = pcre2_match_data_create_from_pattern(job->pattern, NULL)))
875 return(PCRS_ERR_NOMEM);
877 offsets = pcre2_get_ovector_pointer(pcre2_matches);
879 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
881 return(PCRS_ERR_NOMEM);
883 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
886 * Find the pattern and calculate the space
887 * requirements for the result
889 newsize = subject_length;
892 while ((submatches = pcre2_match(job->pattern, (const unsigned char *)subject,
893 subject_length, (size_t)offset, 0, pcre2_matches, NULL)) > 0)
895 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
898 job->flags |= PCRS_SUCCESS;
899 matches[i].submatches = submatches;
901 for (k = 0; k < submatches; k++)
903 matches[i].submatch_offset[k] = (int)offsets[2 * k];
905 /* Note: Non-found optional submatches have length -1-(-1)==0 */
906 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
908 /* reserve mem for each submatch as often as it is ref'd */
909 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
911 /* plus replacement text size minus match text size */
912 newsize += job->substitute->length - matches[i].submatch_length[0];
914 /* chunk before match */
915 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
916 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
917 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
919 /* chunk after match */
920 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = (int)offsets[1];
921 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
922 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
924 /* Storage for matches exhausted? -> Extend! */
925 if (++i >= max_matches)
927 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
928 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
931 return(PCRS_ERR_NOMEM);
936 /* Non-global search or limit reached? */
937 if (!(job->flags & PCRS_GLOBAL)) break;
939 /* Don't loop on empty matches */
940 if (offsets[1] == offset)
941 if ((size_t)offset < subject_length)
945 /* Go find the next one */
947 offset = (int)offsets[1];
949 /* Pass pcre error through if (bad) failure */
951 if (submatches < PCRE2_ERROR_NOMATCH)
953 if (submatches < PCRE_ERROR_NOMATCH)
958 pcre2_match_data_free(pcre2_matches);
966 * Get memory for the result (must be freed by caller!)
967 * and append terminating null byte.
969 if ((*result = (char *)malloc(newsize + 1
972 * Work around to prevent invalid reads in the jit code.
980 pcre2_match_data_free(pcre2_matches);
982 return PCRS_ERR_NOMEM;
986 (*result)[newsize] = '\0';
994 result_offset = *result;
996 for (i = 0; i < matches_found; i++)
998 /* copy the chunk preceding the match */
999 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
1000 result_offset += matches[i].submatch_offset[0] - offset;
1002 /* For every segment of the substitute.. */
1003 for (k = 0; k <= job->substitute->backrefs; k++)
1005 /* ...copy its text.. */
1006 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
1007 result_offset += job->substitute->block_length[k];
1009 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
1010 if (k != job->substitute->backrefs
1011 /* ..in legal range.. */
1012 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
1013 /* ..and referencing a real submatch.. */
1014 && job->substitute->backref[k] < matches[i].submatches
1015 /* ..that is nonempty.. */
1016 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
1018 /* ..copy the submatch that is ref'd. */
1021 subject + matches[i].submatch_offset[job->substitute->backref[k]],
1022 matches[i].submatch_length[job->substitute->backref[k]]
1024 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
1027 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
1030 /* Copy the rest. */
1031 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
1033 *result_length = newsize;
1035 pcre2_match_data_free(pcre2_matches);
1038 return matches_found;
1043 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
1045 /*********************************************************************
1047 * Function : is_hex_sequence
1049 * Description : Checks the first four characters of a string
1050 * and decides if they are a valid hex sequence
1054 * 1 : sequence = The string to check
1056 * Returns : Non-zero if it's valid sequence, or
1059 *********************************************************************/
1060 static int is_hex_sequence(const char *sequence)
1062 return (sequence[0] == '\\' &&
1063 sequence[1] == 'x' &&
1064 is_hex_digit(sequence[2]) &&
1065 is_hex_digit(sequence[3]));
1070 * Functions below this line are only part of the pcrs version
1071 * included in Privoxy. If you use any of them you should not
1072 * try to dynamically link against external pcrs versions.
1075 /*********************************************************************
1077 * Function : pcrs_job_is_dynamic
1079 * Description : Checks if a job has the "D" (dynamic) option set.
1082 * 1 : job = The job to check
1084 * Returns : TRUE if the job is indeed dynamic, otherwise
1087 *********************************************************************/
1088 int pcrs_job_is_dynamic(char *job)
1090 const char delimiter = job[1];
1091 const size_t length = strlen(job);
1097 * The shortest valid (but useless)
1098 * dynamic pattern is "s@@@D"
1104 * Everything between the last character
1105 * and the last delimiter is an option ...
1107 for (option = job + length; *option != delimiter; option--)
1112 * ... and if said option is 'D' the job is dynamic.
1122 /*********************************************************************
1124 * Function : pcrs_get_delimiter
1126 * Description : Tries to find a character that is safe to
1127 * be used as a pcrs delimiter for a certain string.
1130 * 1 : string = The string to search in
1132 * Returns : A safe delimiter if one was found, otherwise '\0'.
1134 *********************************************************************/
1135 char pcrs_get_delimiter(const char *string)
1138 * Some characters that are unlikely to
1139 * be part of pcrs replacement strings.
1141 static const char delimiters[] = "><#+*~%^-:;!@";
1142 const char *d = delimiters;
1144 /* Take the first delimiter that isn't part of the string */
1145 while (*d && NULL != strchr(string, *d))
1154 /*********************************************************************
1156 * Function : pcrs_execute_single_command
1158 * Description : Apply single pcrs command to the subject.
1159 * The subject itself is left untouched, memory for the result
1160 * is malloc()ed and it is the caller's responsibility to free
1161 * the result when it's no longer needed.
1164 * 1 : subject = the subject (== original) string
1165 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1166 * 3 : hits = int* for returning the number of modifications
1168 * Returns : NULL in case of errors, otherwise the
1169 * result of the pcrs command.
1171 *********************************************************************/
1172 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1174 size_t buffer_size, new_size;
1175 char *result = NULL;
1179 assert(pcrs_command);
1182 buffer_size = strlen(subject);
1184 job = pcrs_compile_command(pcrs_command, hits);
1187 *hits = pcrs_execute(job, subject, buffer_size, &result, &new_size);
1188 buffer_size = new_size;
1201 /*********************************************************************
1203 * Function : pcrs_compile_dynamic_command
1205 * Description : Takes a dynamic pcrs command, fills in the
1206 * values of the variables and compiles it.
1209 * 1 : pcrs_command = The dynamic pcrs command to compile
1210 * 2 : v = NULL terminated array of variables and their values.
1211 * 3 : error = pcrs error code
1213 * Returns : NULL in case of hard errors, otherwise the
1214 * compiled pcrs job.
1216 *********************************************************************/
1217 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1219 char buf[PCRS_BUFFER_SIZE];
1220 const char *original_pcrs_command = pcrs_command;
1221 char *pcrs_command_tmp = NULL;
1222 pcrs_job *job = NULL;
1227 while ((NULL != v->name) && (NULL != pcrs_command))
1229 assert(NULL != v->value);
1231 if (NULL == strstr(pcrs_command, v->name))
1234 * Skip the substitution if the variable
1235 * name isn't part of the pattern.
1241 /* Use pcrs to replace the variable with its value. */
1242 d = pcrs_get_delimiter(v->value);
1245 /* No proper delimiter found */
1246 *error = PCRS_ERR_CMDSYNTAX;
1247 freez(pcrs_command_tmp);
1252 * Variable names are supposed to contain alpha
1253 * numerical characters plus '_' only.
1255 assert(NULL == strchr(v->name, d));
1257 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cDgT", d, v->name, d, v->value, d);
1259 if (ret >= sizeof(buf))
1262 * Value didn't completely fit into buffer,
1263 * overwrite the end of the substitution text
1264 * with a truncation message and close the pattern
1267 static const char warning[] = "... [too long, truncated]";
1268 const size_t trailer_size = sizeof(warning) + 4; /* 4 for d + "DgT" */
1269 char *trailer_start = buf + sizeof(buf) - trailer_size;
1271 ret = snprintf(trailer_start, trailer_size, "%s%cDgT", warning, d);
1272 assert(ret == trailer_size - 1);
1273 assert(sizeof(buf) == strlen(buf) + 1);
1277 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1278 if (NULL == pcrs_command_tmp)
1283 if (pcrs_command != original_pcrs_command)
1285 freez(pcrs_command);
1287 pcrs_command = pcrs_command_tmp;
1292 job = pcrs_compile_command(pcrs_command, error);
1293 if (pcrs_command != original_pcrs_command)
1295 freez(pcrs_command);
1300 *error = PCRS_WARN_TRUNCATION;