wolfSSL: Use LIBWOLFSSL_VERSION_HEX to decide whether or not to use WOLFSSL_X509_V_OK
[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 #ifdef HAVE_PCRE2
37 #define PCRE2_CODE_UNIT_WIDTH 8
38 #define PCREn(x) PCRE2_ ## x
39 #ifndef _PCRE2_H
40 #include <pcre2.h>
41 #endif
42 #else
43 #define PCREn(x) PCRE_ ## x
44 #ifndef _PCRE_H
45 #include <pcre.h>
46 #endif
47 #endif
48
49 /*
50  * Constants:
51  */
52
53 #define FALSE 0
54 #define TRUE 1
55
56 /* Capacity */
57 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
58 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
59 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
60
61 /*
62  * PCRS error codes
63  *
64  * They are supposed to be handled together with PCRE error
65  * codes and have to start with an offset to prevent overlaps.
66  *
67  * PCRE 6.7 uses error codes from -1 to -21,
68  * PCRE2 10.42 uses error codes from -66 to 101.
69  * PCRS error codes below -300 should be safe for a while.
70  */
71 #define PCRS_ERR_NOMEM           -300      /* Failed to acquire memory. */
72 #define PCRS_ERR_CMDSYNTAX       -301      /* Syntax of s///-command */
73 #define PCRS_ERR_STUDY           -302      /* pcre error while studying the pattern */
74 #define PCRS_ERR_BADJOB          -303      /* NULL job pointer, pattern or substitute */
75 #define PCRS_WARN_BADREF         -304      /* Backreference out of range */
76 #define PCRS_WARN_TRUNCATION     -305      /* At least one pcrs variable was too big,
77                                             * only the first part was used. */
78
79 /* Flags */
80 #define PCRS_GLOBAL          0x08000000u      /* Job should be applied globally, as with perl's g option */
81 #define PCRS_TRIVIAL         0x10000000u      /* Backreferences in the substitute are ignored */
82 #define PCRS_SUCCESS         0x20000000u      /* Job did previously match */
83 #define PCRS_DYNAMIC         0x40000000u      /* Job is dynamic (used to disable JIT compilation) */
84
85
86 /*
87  * Data types:
88  */
89
90 /* A compiled substitute */
91
92 typedef struct {
93   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
94   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
95   int    backrefs;                               /* The number of backreferences */
96   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
97   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
98   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
99   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
100 } pcrs_substitute;
101
102
103 /*
104  * A match, including all captured subpatterns (submatches)
105  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
106  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
107  * range after the match.
108  */
109
110 typedef struct {
111   int    submatches;                               /* Number of captured subpatterns */
112   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
113   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
114 } pcrs_match;
115
116
117 /* A PCRS job */
118
119 typedef struct PCRS_JOB {
120 #ifdef HAVE_PCRE2
121     pcre2_code *pattern;
122 #else
123   pcre *pattern;                            /* The compiled pcre pattern */
124   pcre_extra *hints;                        /* The pcre hints for the pattern */
125 #endif
126   int options;                              /* The pcre options (numeric) */
127   unsigned int flags;                       /* The pcrs and user flags (see "Flags" above) */
128   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
129   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
130 } pcrs_job;
131
132
133 /*
134  * Prototypes:
135  */
136
137 /* Main usage */
138 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
139 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
140 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
141 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
142
143 /* Freeing jobs */
144 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
145 extern void             pcrs_free_joblist(pcrs_job *joblist);
146
147 /* Info on errors: */
148 extern const char *pcrs_strerror(const int error);
149
150 extern int pcrs_job_is_dynamic(char *job);
151 extern char pcrs_get_delimiter(const char *string);
152 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
153 /*
154  * Variable/value pair for dynamic pcrs commands.
155  */
156 struct pcrs_variable
157 {
158    const char *name;
159    char *value;
160    int static_value;
161 };
162
163 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
164
165 /* Only relevant for maximum pcrs variable size */
166 #ifndef PCRS_BUFFER_SIZE
167 #define PCRS_BUFFER_SIZE 4000
168 #endif /* ndef PCRS_BUFFER_SIZE */
169
170 #ifdef FUZZ
171 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
172 #endif
173
174 #endif /* ndef PCRS_H_INCLUDED */
175
176 /*
177   Local Variables:
178   tab-width: 3
179   end:
180 */