Add "Cross-origin resource sharing" (CORS) support
[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  *********************************************************************/
13
14
15 #ifndef _PCRE_H
16 #include <pcre.h>
17 #endif
18
19 /*
20  * Constants:
21  */
22
23 #define FALSE 0
24 #define TRUE 1
25
26 /* Capacity */
27 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
28 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
29 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
30
31 /*
32  * PCRS error codes
33  *
34  * They are supposed to be handled together with PCRE error
35  * codes and have to start with an offset to prevent overlaps.
36  *
37  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
38  * below -100 should be safe for a while.
39  */
40 #define PCRS_ERR_NOMEM           -100      /* Failed to acquire memory. */
41 #define PCRS_ERR_CMDSYNTAX       -101      /* Syntax of s///-command */
42 #define PCRS_ERR_STUDY           -102      /* pcre error while studying the pattern */
43 #define PCRS_ERR_BADJOB          -103      /* NULL job pointer, pattern or substitute */
44 #define PCRS_WARN_BADREF         -104      /* Backreference out of range */
45 #define PCRS_WARN_TRUNCATION     -105      /* At least one pcrs variable was too big,
46                                             * only the first part was used. */
47
48 /* Flags */
49 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
50 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
51 #define PCRS_SUCCESS         4      /* Job did previously match */
52
53
54 /*
55  * Data types:
56  */
57
58 /* A compiled substitute */
59
60 typedef struct {
61   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
62   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
63   int    backrefs;                               /* The number of backreferences */
64   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
65   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
66   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
67   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
68 } pcrs_substitute;
69
70
71 /*
72  * A match, including all captured subpatterns (submatches)
73  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
74  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
75  * range after the match.
76  */
77
78 typedef struct {
79   int    submatches;                               /* Number of captured subpatterns */
80   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
81   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
82 } pcrs_match;
83
84
85 /* A PCRS job */
86
87 typedef struct PCRS_JOB {
88   pcre *pattern;                            /* The compiled pcre pattern */
89   pcre_extra *hints;                        /* The pcre hints for the pattern */
90   int options;                              /* The pcre options (numeric) */
91   int flags;                                /* The pcrs and user flags (see "Flags" above) */
92   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
93   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
94 } pcrs_job;
95
96
97 /*
98  * Prototypes:
99  */
100
101 /* Main usage */
102 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
103 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
104 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
105 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
106
107 /* Freeing jobs */
108 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
109 extern void             pcrs_free_joblist(pcrs_job *joblist);
110
111 /* Info on errors: */
112 extern const char *pcrs_strerror(const int error);
113
114 extern int pcrs_job_is_dynamic(char *job);
115 extern char pcrs_get_delimiter(const char *string);
116 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
117 /*
118  * Variable/value pair for dynamic pcrs commands.
119  */
120 struct pcrs_variable
121 {
122    const char *name;
123    char *value;
124    int static_value;
125 };
126
127 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
128
129 /* Only relevant for maximum pcrs variable size */
130 #ifndef PCRS_BUFFER_SIZE
131 #define PCRS_BUFFER_SIZE 4000
132 #endif /* ndef PCRS_BUFFER_SIZE */
133
134 #ifdef FUZZ
135 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
136 #endif
137
138 #endif /* ndef PCRS_H_INCLUDED */
139
140 /*
141   Local Variables:
142   tab-width: 3
143   end:
144 */