1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.23 2006/12/29 17:53:05 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.
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.23 2006/12/29 17:53:05 fabiankeil
37 * Fixed gcc43 conversion warnings.
39 * Revision 1.22 2006/12/24 17:34:20 fabiankeil
40 * Add pcrs_strerror() message for PCRE_ERROR_MATCHLIMIT
41 * and give a hint why an error code might be unknown.
43 * Catch NULL subjects early in pcrs_execute().
45 * Revision 1.21 2006/07/18 14:48:47 david__schmidt
46 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
47 * with what was really the latest development (the v_3_0_branch branch)
49 * Revision 1.19.2.4 2005/05/07 21:50:55 david__schmidt
50 * A few memory leaks plugged (mostly on error paths)
52 * Revision 1.19.2.3 2003/12/04 12:32:45 oes
53 * Append a trailing nullbyte to result to facilitate string processing
55 * Revision 1.19.2.2 2002/10/08 16:22:28 oes
56 * Bugfix: Need to check validity of backreferences explicitly,
57 * because when max_matches are reached and matches is expanded,
58 * realloc() does not zero the memory. Fixes Bug # 606227
60 * Revision 1.19.2.1 2002/08/10 11:23:40 oes
61 * Include prce.h via project.h, where the appropriate
62 * source will have been selected
64 * Revision 1.19 2002/03/08 14:47:48 oes
67 * Revision 1.18 2002/03/08 14:17:14 oes
68 * Fixing -Wconversion warnings
70 * Revision 1.17 2002/03/08 13:45:48 oes
71 * Hiding internal functions
73 * Revision 1.16 2001/11/30 21:32:14 jongfoster
74 * Fixing signed/unsigned comparison (Andreas please check this!)
77 * Revision 1.15 2001/09/20 16:11:06 steudten
79 * Add casting for some string functions.
81 * Revision 1.14 2001/09/09 21:41:57 oes
82 * Fixing yet another silly bug
84 * Revision 1.13 2001/09/06 14:05:59 oes
87 * Revision 1.12 2001/08/18 11:35:00 oes
88 * - Introduced pcrs_strerror()
89 * - made some NULL arguments non-fatal
90 * - added support for \n \r \e \b \t \f \a \0 in substitute
91 * - made quoting adhere to standard rules
92 * - added warning for bad backrefs
93 * - added pcrs_execute_list()
95 * - bugfix & cosmetics
97 * Revision 1.11 2001/08/15 15:32:03 oes
98 * - Added support for Perl's special variables $+, $' and $`
99 * - Improved the substitute parser
100 * - Replaced the hard limit for the maximum number of matches
101 * by dynamic reallocation
103 * Revision 1.10 2001/08/05 13:13:11 jongfoster
104 * Making parameters "const" where possible.
106 * Revision 1.9 2001/07/18 17:27:00 oes
107 * Changed interface; Cosmetics
109 * Revision 1.8 2001/06/29 21:45:41 oes
110 * Indentation, CRLF->LF, Tab-> Space
112 * Revision 1.7 2001/06/29 13:33:04 oes
113 * - Cleaned up, renamed and reordered functions,
115 * - Removed my_strsep
116 * - Replaced globalflag with a general flags int
117 * that holds PCRS_GLOBAL, PCRS_SUCCESS, and PCRS_TRIVIAL
118 * - Introduced trivial option that will prevent pcrs
119 * from honouring backreferences in the substitute,
120 * which is useful for large substitutes that are
121 * red in from somewhere and saves the pain of escaping
123 * - Introduced convenience function pcrs_free_joblist()
124 * - Split pcrs_make_job() into pcrs_compile(), which still
125 * takes a complete s/// comand as argument and parses it,
126 * and a new function pcrs_make_job, which takes the
127 * three separate components. This should make for a
128 * much friendlier frontend.
129 * - Removed create_pcrs_job() which was useless
130 * - Fixed a bug in pcrs_execute
131 * - Success flag is now handled by pcrs instead of user
133 * Revision 1.6 2001/06/03 19:12:45 oes
136 * Revision 1.5 2001/05/29 09:50:24 jongfoster
137 * (Fixed one int -> size_t)
139 * Revision 1.4 2001/05/25 14:12:40 oes
140 * Fixed bug: Empty substitutes now detected
142 * Revision 1.3 2001/05/25 11:03:55 oes
143 * Added sanity check for NULL jobs to pcrs_exec_substitution
145 * Revision 1.2 2001/05/22 18:46:04 oes
147 * Added support for PCRE_UNGREEDY behaviour to pcrs,
148 * which is selected by the (nonstandard and therefore
149 * capital) letter 'U' in the option string.
150 * It causes the quantifiers to be ungreedy by default.
151 * Appending a ? turns back to greedy (!).
153 * Revision 1.1.1.1 2001/05/15 13:59:02 oes
154 * Initial import of version 2.9.3 source tree
157 *********************************************************************/
161 * Include project.h just so that the right pcre.h gets
162 * included from there
171 const char pcrs_h_rcs[] = PCRS_H_VERSION;
174 * Internal prototypes
177 static int pcrs_parse_perl_options(const char *optstring, int *flags);
178 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
179 int capturecount, int *errptr);
181 /*********************************************************************
183 * Function : pcrs_strerror
185 * Description : Return a string describing a given error code.
188 * 1 : error = the error code
190 * Returns : char * to the descriptive string
192 *********************************************************************/
193 const char *pcrs_strerror(const int error)
199 /* Passed-through PCRE error: */
200 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
202 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
203 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
204 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
205 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
206 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
208 /* Can't happen / not passed: */
209 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
210 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
212 #ifdef PCRE_ERROR_MATCHLIMIT
214 * Only reported by PCRE versions newer than our own.
216 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
217 #endif /* def PCRE_ERROR_MATCHLIMIT */
220 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
221 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
222 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
223 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
224 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
227 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
228 * only catch PCRE errors that can happen with our internal
229 * version. If Privoxy is linked against a newer
230 * PCRE version all bets are off ...
232 default: return "Unknown error. Privoxy out of sync with PCRE?";
235 /* error >= 0: No error */
236 return "(pcrs:) Everything's just fine. Thanks for asking.";
241 /*********************************************************************
243 * Function : pcrs_parse_perl_options
245 * Description : This function parses a string containing the options to
246 * Perl's s/// operator. It returns an integer that is the
247 * pcre equivalent of the symbolic optstring.
248 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
249 * 'T' (trivial) options but pcrs needs them, the corresponding
250 * flags are set if 'g'or 'T' is encountered.
251 * Note: The 'T' and 'U' options do not conform to Perl.
254 * 1 : optstring = string with options in perl syntax
255 * 2 : flags = see description
257 * Returns : option integer suitable for pcre
259 *********************************************************************/
260 static int pcrs_parse_perl_options(const char *optstring, int *flags)
266 if (NULL == optstring) return 0;
268 for (i = 0; i < strlen(optstring); i++)
272 case 'e': break; /* ToDo ;-) */
273 case 'g': *flags |= PCRS_GLOBAL; break;
274 case 'i': rc |= PCRE_CASELESS; break;
275 case 'm': rc |= PCRE_MULTILINE; break;
277 case 's': rc |= PCRE_DOTALL; break;
278 case 'x': rc |= PCRE_EXTENDED; break;
279 case 'U': rc |= PCRE_UNGREEDY; break;
280 case 'T': *flags |= PCRS_TRIVIAL; break;
289 /*********************************************************************
291 * Function : pcrs_compile_replacement
293 * Description : This function takes a Perl-style replacement (2nd argument
294 * to the s/// operator and returns a compiled pcrs_substitute,
295 * or NULL if memory allocation for the substitute structure
299 * 1 : replacement = replacement part of s/// operator
301 * 2 : trivialflag = Flag that causes backreferences to be
303 * 3 : capturecount = Number of capturing subpatterns in
304 * the pattern. Needed for $+ handling.
305 * 4 : errptr = pointer to an integer in which error
306 * conditions can be returned.
308 * Returns : pcrs_substitute data structure, or NULL if an
309 * error is encountered. In that case, *errptr has
312 *********************************************************************/
313 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
320 i = k = l = quoted = 0;
325 if (NULL == replacement)
333 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
335 *errptr = PCRS_ERR_NOMEM;
338 memset(r, '\0', sizeof(pcrs_substitute));
340 length = strlen(replacement);
342 if (NULL == (text = (char *)malloc(length + 1)))
345 *errptr = PCRS_ERR_NOMEM;
348 memset(text, '\0', length + 1);
352 * In trivial mode, just copy the substitute text
356 text = strncpy(text, replacement, length + 1);
361 * Else, parse, cut out and record all backreferences
365 while (i < (int)length)
368 if (replacement[i] == '\\')
372 text[k++] = replacement[i++];
377 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
379 switch (replacement[++i])
415 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
417 char *symbol, symbols[] = "'`+&";
418 r->block_length[l] = (size_t)(k - r->block_offset[l]);
420 /* Numerical backreferences */
421 if (isdigit((int)replacement[i + 1]))
423 while (i < (int)length && isdigit((int)replacement[++i]))
425 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
427 if (r->backref[l] > capturecount)
429 *errptr = PCRS_WARN_BADREF;
433 /* Symbolic backreferences: */
434 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
437 if (symbol - symbols == 2) /* $+ */
439 r->backref[l] = capturecount;
441 else if (symbol - symbols == 3) /* $& */
447 r->backref[l] = PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols);
452 /* Invalid backref -> plain '$' */
458 /* Valid and in range? -> record */
459 if (r->backref[l] < PCRS_MAX_SUBMATCHES + 2)
461 r->backref_count[r->backref[l]] += 1;
462 r->block_offset[++l] = k;
466 *errptr = PCRS_WARN_BADREF;
472 /* Plain chars are copied */
473 text[k++] = replacement[i++];
476 } /* -END- if (!trivialflag) */
483 r->length = (size_t)k;
484 r->block_length[l] = (size_t)(k - r->block_offset[l]);
491 /*********************************************************************
493 * Function : pcrs_free_job
495 * Description : Frees the memory used by a pcrs_job struct and its
496 * dependant structures.
499 * 1 : job = pointer to the pcrs_job structure to be freed
501 * Returns : a pointer to the next job, if there was any, or
504 *********************************************************************/
505 pcrs_job *pcrs_free_job(pcrs_job *job)
516 if (job->pattern != NULL) free(job->pattern);
517 if (job->hints != NULL) free(job->hints);
518 if (job->substitute != NULL)
520 if (job->substitute->text != NULL) free(job->substitute->text);
521 free(job->substitute);
530 /*********************************************************************
532 * Function : pcrs_free_joblist
534 * Description : Iterates through a chained list of pcrs_job's and
535 * frees them using pcrs_free_job.
538 * 1 : joblist = pointer to the first pcrs_job structure to
543 *********************************************************************/
544 void pcrs_free_joblist(pcrs_job *joblist)
546 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
553 /*********************************************************************
555 * Function : pcrs_compile_command
557 * Description : Parses a string with a Perl-style s/// command,
558 * calls pcrs_compile, and returns a corresponding
559 * pcrs_job, or NULL if parsing or compiling the job
563 * 1 : command = string with perl-style s/// command
564 * 2 : errptr = pointer to an integer in which error
565 * conditions can be returned.
567 * Returns : a corresponding pcrs_job data structure, or NULL
568 * if an error was encountered. In that case, *errptr
571 *********************************************************************/
572 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
574 int i, k, l, quoted = FALSE;
583 * Tokenize the perl command
585 limit = strlen(command);
588 *errptr = PCRS_ERR_CMDSYNTAX;
593 delimiter = command[1];
596 tokens[l] = (char *) malloc(limit + 1);
598 for (i = 0; i <= (int)limit; i++)
601 if (command[i] == delimiter && !quoted)
608 tokens[0][k++] = '\0';
609 tokens[++l] = tokens[0] + k;
613 else if (command[i] == '\\' && !quoted)
616 if (command[i+1] == delimiter) continue;
622 tokens[0][k++] = command[i];
630 *errptr = PCRS_ERR_CMDSYNTAX;
635 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
642 /*********************************************************************
644 * Function : pcrs_compile
646 * Description : Takes the three arguments to a perl s/// command
647 * and compiles a pcrs_job structure from them.
650 * 1 : pattern = string with perl-style pattern
651 * 2 : substitute = string with perl-style substitute
652 * 3 : options = string with perl-style options
653 * 4 : errptr = pointer to an integer in which error
654 * conditions can be returned.
656 * Returns : a corresponding pcrs_job data structure, or NULL
657 * if an error was encountered. In that case, *errptr
660 *********************************************************************/
661 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
671 * Handle NULL arguments
673 if (pattern == NULL) pattern = "";
674 if (substitute == NULL) substitute = "";
678 * Get and init memory
680 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
682 *errptr = PCRS_ERR_NOMEM;
685 memset(newjob, '\0', sizeof(pcrs_job));
689 * Evaluate the options
691 newjob->options = pcrs_parse_perl_options(options, &flags);
692 newjob->flags = flags;
696 * Compile the pattern
698 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
699 if (newjob->pattern == NULL)
701 pcrs_free_job(newjob);
707 * Generate hints. This has little overhead, since the
708 * hints will be NULL for a boring pattern anyway.
710 newjob->hints = pcre_study(newjob->pattern, 0, &error);
713 *errptr = PCRS_ERR_STUDY;
714 pcrs_free_job(newjob);
720 * Determine the number of capturing subpatterns.
721 * This is needed for handling $+ in the substitute.
723 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
725 pcrs_free_job(newjob);
731 * Compile the substitute
733 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
735 pcrs_free_job(newjob);
744 /*********************************************************************
746 * Function : pcrs_execute_list
748 * Description : This is a multiple job wrapper for pcrs_execute().
749 * Apply the regular substitutions defined by the jobs in
750 * the joblist to the subject.
751 * The subject itself is left untouched, memory for the result
752 * is malloc()ed and it is the caller's responsibility to free
753 * the result when it's no longer needed.
755 * Note: For convenient string handling, a null byte is
756 * appended to the result. It does not count towards the
757 * result_length, though.
761 * 1 : joblist = the chained list of pcrs_jobs to be executed
762 * 2 : subject = the subject string
763 * 3 : subject_length = the subject's length
764 * 4 : result = char** for returning the result
765 * 5 : result_length = size_t* for returning the result's length
767 * Returns : On success, the number of substitutions that were made.
768 * May be > 1 if job->flags contained PCRS_GLOBAL
769 * On failure, the (negative) pcre error code describing the
770 * failure, which may be translated to text using pcrs_strerror().
772 *********************************************************************/
773 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
776 char *old, *new = NULL;
777 int hits, total_hits;
780 *result_length = subject_length;
781 hits = total_hits = 0;
783 for (job = joblist; job != NULL; job = job->next)
785 hits = pcrs_execute(job, old, *result_length, &new, result_length);
787 if (old != subject) free(old);
806 /*********************************************************************
808 * Function : pcrs_execute
810 * Description : Apply the regular substitution defined by the job to the
812 * The subject itself is left untouched, memory for the result
813 * is malloc()ed and it is the caller's responsibility to free
814 * the result when it's no longer needed.
816 * Note: For convenient string handling, a null byte is
817 * appended to the result. It does not count towards the
818 * result_length, though.
821 * 1 : job = the pcrs_job to be executed
822 * 2 : subject = the subject (== original) string
823 * 3 : subject_length = the subject's length
824 * 4 : result = char** for returning the result
825 * 5 : result_length = size_t* for returning the result's length
827 * Returns : On success, the number of substitutions that were made.
828 * May be > 1 if job->flags contained PCRS_GLOBAL
829 * On failure, the (negative) pcre error code describing the
830 * failure, which may be translated to text using pcrs_strerror().
832 *********************************************************************/
833 int pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **result, size_t *result_length)
835 int offsets[3 * PCRS_MAX_SUBMATCHES],
840 max_matches = PCRS_MAX_MATCH_INIT;
842 pcrs_match *matches, *dummy;
848 * Sanity check & memory allocation
850 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
853 return(PCRS_ERR_BADJOB);
856 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
859 return(PCRS_ERR_NOMEM);
861 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
865 * Find the pattern and calculate the space
866 * requirements for the result
868 newsize = subject_length;
870 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
872 job->flags |= PCRS_SUCCESS;
873 matches[i].submatches = submatches;
875 for (k = 0; k < submatches; k++)
877 matches[i].submatch_offset[k] = offsets[2 * k];
879 /* Note: Non-found optional submatches have length -1-(-1)==0 */
880 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
882 /* reserve mem for each submatch as often as it is ref'd */
883 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
885 /* plus replacement text size minus match text size */
886 newsize += job->substitute->length - matches[i].submatch_length[0];
888 /* chunk before match */
889 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
890 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
891 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
893 /* chunk after match */
894 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
895 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
896 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
898 /* Storage for matches exhausted? -> Extend! */
899 if (++i >= max_matches)
901 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
902 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
906 return(PCRS_ERR_NOMEM);
911 /* Non-global search or limit reached? */
912 if (!(job->flags & PCRS_GLOBAL)) break;
914 /* Don't loop on empty matches */
915 if (offsets[1] == offset)
916 if ((size_t)offset < subject_length)
920 /* Go find the next one */
924 /* Pass pcre error through if (bad) failiure */
925 if (submatches < PCRE_ERROR_NOMATCH)
934 * Get memory for the result (must be freed by caller!)
935 * and append terminating null byte.
937 if ((*result = (char *)malloc(newsize + 1)) == NULL)
940 return PCRS_ERR_NOMEM;
944 (*result)[newsize] = '\0';
952 result_offset = *result;
954 for (i = 0; i < matches_found; i++)
956 /* copy the chunk preceding the match */
957 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
958 result_offset += matches[i].submatch_offset[0] - offset;
960 /* For every segment of the substitute.. */
961 for (k = 0; k <= job->substitute->backrefs; k++)
963 /* ...copy its text.. */
964 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
965 result_offset += job->substitute->block_length[k];
967 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
968 if (k != job->substitute->backrefs
969 /* ..in legal range.. */
970 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
971 /* ..and referencing a real submatch.. */
972 && job->substitute->backref[k] < matches[i].submatches
973 /* ..that is nonempty.. */
974 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
976 /* ..copy the submatch that is ref'd. */
979 subject + matches[i].submatch_offset[job->substitute->backref[k]],
980 matches[i].submatch_length[job->substitute->backref[k]]
982 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
985 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
989 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
991 *result_length = newsize;
993 return matches_found;