Bump copyright
[privoxy.git] / pcrs.h
1 #ifndef PCRS_H_INCLUDED
2 #define PCRS_H_INCLUDED
3
4 /*********************************************************************
5  *
6  * File        :  $Source: /cvsroot/ijbswa/current/pcrs.h,v $
7  *
8  * Purpose     :  Header file for pcrs.c
9  *
10  * Copyright   :  Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
11  *                <andreas@oesterhelt.org>
12  *
13  *                Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
14  *
15  *                This program is free software; you can redistribute it
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  *********************************************************************/
34
35
36 #ifndef _PCRE_H
37 #include <pcre.h>
38 #endif
39
40 /*
41  * Constants:
42  */
43
44 #define FALSE 0
45 #define TRUE 1
46
47 /* Capacity */
48 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
49 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
50 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
51
52 /*
53  * PCRS error codes
54  *
55  * They are supposed to be handled together with PCRE error
56  * codes and have to start with an offset to prevent overlaps.
57  *
58  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
59  * below -100 should be safe for a while.
60  */
61 #define PCRS_ERR_NOMEM           -100      /* Failed to acquire memory. */
62 #define PCRS_ERR_CMDSYNTAX       -101      /* Syntax of s///-command */
63 #define PCRS_ERR_STUDY           -102      /* pcre error while studying the pattern */
64 #define PCRS_ERR_BADJOB          -103      /* NULL job pointer, pattern or substitute */
65 #define PCRS_WARN_BADREF         -104      /* Backreference out of range */
66 #define PCRS_WARN_TRUNCATION     -105      /* At least one pcrs variable was too big,
67                                             * only the first part was used. */
68
69 /* Flags */
70 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
71 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
72 #define PCRS_SUCCESS         4      /* Job did previously match */
73 #define PCRS_DYNAMIC         8      /* Job is dynamic (used to disable JIT compilation) */
74
75
76 /*
77  * Data types:
78  */
79
80 /* A compiled substitute */
81
82 typedef struct {
83   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
84   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
85   int    backrefs;                               /* The number of backreferences */
86   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
87   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
88   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
89   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
90 } pcrs_substitute;
91
92
93 /*
94  * A match, including all captured subpatterns (submatches)
95  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
96  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
97  * range after the match.
98  */
99
100 typedef struct {
101   int    submatches;                               /* Number of captured subpatterns */
102   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
103   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
104 } pcrs_match;
105
106
107 /* A PCRS job */
108
109 typedef struct PCRS_JOB {
110   pcre *pattern;                            /* The compiled pcre pattern */
111   pcre_extra *hints;                        /* The pcre hints for the pattern */
112   int options;                              /* The pcre options (numeric) */
113   int flags;                                /* The pcrs and user flags (see "Flags" above) */
114   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
115   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
116 } pcrs_job;
117
118
119 /*
120  * Prototypes:
121  */
122
123 /* Main usage */
124 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
125 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
126 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
127 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
128
129 /* Freeing jobs */
130 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
131 extern void             pcrs_free_joblist(pcrs_job *joblist);
132
133 /* Info on errors: */
134 extern const char *pcrs_strerror(const int error);
135
136 extern int pcrs_job_is_dynamic(char *job);
137 extern char pcrs_get_delimiter(const char *string);
138 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
139 /*
140  * Variable/value pair for dynamic pcrs commands.
141  */
142 struct pcrs_variable
143 {
144    const char *name;
145    char *value;
146    int static_value;
147 };
148
149 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
150
151 /* Only relevant for maximum pcrs variable size */
152 #ifndef PCRS_BUFFER_SIZE
153 #define PCRS_BUFFER_SIZE 4000
154 #endif /* ndef PCRS_BUFFER_SIZE */
155
156 #ifdef FUZZ
157 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
158 #endif
159
160 #endif /* ndef PCRS_H_INCLUDED */
161
162 /*
163   Local Variables:
164   tab-width: 3
165   end:
166 */