- Add a hide-tor-exit-notation test with a fingerprint
[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   :  see pcrs.c
11  *
12  * Revisions   :
13  *    $Log: pcrs.h,v $
14  *    Revision 1.15  2007/01/05 15:46:12  fabiankeil
15  *    Don't use strlen() to calculate the length of
16  *    the pcrs substitutes. They don't have to be valid C
17  *    strings and getting their length wrong can result in
18  *    user-controlled memory corruption.
19  *
20  *    Thanks to Felix Gröbert for reporting the problem
21  *    and providing the fix [#1627140].
22  *
23  *    Revision 1.14  2006/12/24 17:27:37  fabiankeil
24  *    Increase pcrs error code offset to prevent overlaps
25  *    with pcre versions newer than our own.
26  *
27  *    Revision 1.13  2006/07/18 14:48:47  david__schmidt
28  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
29  *    with what was really the latest development (the v_3_0_branch branch)
30  *
31  *    Revision 1.11  2002/03/08 14:18:23  oes
32  *    Fixing -Wconversion warnings
33  *
34  *    Revision 1.10  2002/03/08 13:44:48  oes
35  *    Hiding internal functions, preventing double inclusion of pcre.h
36  *
37  *    Revision 1.9  2001/08/18 11:35:29  oes
38  *    - Introduced pcrs_strerror()
39  *    - added pcrs_execute_list()
40  *
41  *    Revision 1.8  2001/08/15 15:32:50  oes
42  *    Replaced the hard limit for the maximum number of matches
43  *    by dynamic reallocation
44  *
45  *    Revision 1.7  2001/08/05 13:13:11  jongfoster
46  *    Making parameters "const" where possible.
47  *
48  *    Revision 1.6  2001/07/29 18:52:06  jongfoster
49  *    Renaming _PCRS_H, and adding "extern C {}"
50  *
51  *    Revision 1.5  2001/07/18 17:27:00  oes
52  *    Changed interface; Cosmetics
53  *
54  *    Revision 1.4  2001/06/29 13:33:19  oes
55  *    - Cleaned up, commented and adapted to reflect the
56  *      changes in pcrs.c
57  *    - Introduced the PCRS_* flags
58  *
59  *    Revision 1.3  2001/06/09 10:58:57  jongfoster
60  *    Removing a single unused #define which referenced BUFSIZ
61  *
62  *    Revision 1.2  2001/05/25 11:03:55  oes
63  *    Added sanity check for NULL jobs to pcrs_exec_substitution
64  *
65  *    Revision 1.1.1.1  2001/05/15 13:59:02  oes
66  *    Initial import of version 2.9.3 source tree
67  *
68  *    Revision 1.4  2001/05/11 01:57:02  rodney
69  *    Added new file header standard w/RCS control tags.
70  *
71  *    revision 1.3  2001/05/08 02:38:13  rodney
72  *    Changed C++ "//" style comment to C style comments.
73  *
74  *    revision 1.2  2001/04/30 02:39:24  rodney
75  *    Made this pcrs.h file conditionally included.
76  *
77  *    revision 1.1  2001/04/16 21:10:38  rodney
78  *    Initial checkin
79  *
80  *********************************************************************/
81
82 #define PCRS_H_VERSION "$Id: pcrs.h,v 1.15 2007/01/05 15:46:12 fabiankeil Exp $"
83 \f
84
85 #ifndef _PCRE_H
86 #include <pcre.h>
87 #endif
88
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92
93 /*
94  * Constants:
95  */
96
97 #define FALSE 0
98 #define TRUE 1
99
100 /* Capacity */
101 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
102 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
103 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
104
105 /*
106  * PCRS error codes
107  *
108  * They are supposed to be handled together with PCRE error
109  * codes and have to start with an offset to prevent overlaps.
110  *
111  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
112  * below -100 should be safe for a while.
113  */
114 #define PCRS_ERR_NOMEM           -100      /* Failed to acquire memory. */
115 #define PCRS_ERR_CMDSYNTAX       -101      /* Syntax of s///-command */
116 #define PCRS_ERR_STUDY           -102      /* pcre error while studying the pattern */
117 #define PCRS_ERR_BADJOB          -103      /* NULL job pointer, pattern or substitute */
118 #define PCRS_WARN_BADREF         -104      /* Backreference out of range */
119 #define PCRS_WARN_TRUNCATION     -105      /* At least one pcrs variable was too big,
120                                             * only the first part was used. */
121
122 /* Flags */
123 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
124 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
125 #define PCRS_SUCCESS         4      /* Job did previously match */
126
127
128 /*
129  * Data types:
130  */
131
132 /* A compiled substitute */
133
134 typedef struct {
135   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
136   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
137   int    backrefs;                               /* The number of backreferences */
138   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
139   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
140   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
141   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
142 } pcrs_substitute;
143
144
145 /*
146  * A match, including all captured subpatterns (submatches)
147  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
148  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
149  * range after the match.
150  */
151
152 typedef struct {
153   int    submatches;                               /* Number of captured subpatterns */
154   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
155   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
156 } pcrs_match;
157
158
159 /* A PCRS job */
160
161 typedef struct PCRS_JOB {
162   pcre *pattern;                            /* The compiled pcre pattern */
163   pcre_extra *hints;                        /* The pcre hints for the pattern */
164   int options;                              /* The pcre options (numeric) */
165   int flags;                                /* The pcrs and user flags (see "Flags" above) */
166   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
167   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
168 } pcrs_job;
169
170
171 /*
172  * Prototypes:
173  */
174
175 /* Main usage */
176 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
177 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
178 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
179 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
180
181 /* Freeing jobs */
182 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
183 extern void             pcrs_free_joblist(pcrs_job *joblist);
184
185 /* Info on errors: */
186 extern const char *pcrs_strerror(const int error);
187
188 extern int pcrs_job_is_dynamic(char *job);
189 extern char pcrs_get_delimiter(const char *string);
190 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
191 /*
192  * Variable/value pair for dynamic pcrs commands.
193  */
194 struct pcrs_variable
195 {
196    const char *name;
197    char *value;
198    int static_value;
199 };
200
201 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
202
203 /* Only relevant for maximum pcrs variable size */
204 #ifndef PCRS_BUFFER_SIZE
205 #define PCRS_BUFFER_SIZE 4000
206 #endif /* ndef PCRS_BUFFER_SIZE */
207
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
211
212 #endif /* ndef PCRS_H_INCLUDED */
213
214 /*
215   Local Variables:
216   tab-width: 3
217   end:
218 */