Imported Upstream version 2.5.11
[libapache-mod-security.git] / apache2 / api / mod_tfn_reverse.c
1
2 #include "httpd.h"
3 #include "http_core.h"
4 #include "http_config.h"
5 #include "http_log.h"
6 #include "http_protocol.h"
7 #include "ap_config.h"
8 #include "apr_optional.h"
9
10 /* Must be declared if modsecurity.h is not included */
11 APR_DECLARE_OPTIONAL_FN(void, modsec_register_tfn, (const char *name, void *fn));
12
13
14 /**
15  * This function will be invoked by
16  * ModSecurity to transform input.
17  */
18 static int reverse(apr_pool_t *mptmp, unsigned char *input,
19     long int input_len, char **rval, long int *rval_len)
20 {
21     /* Transformation functions can choose to do their
22      * thing in-place, overwriting the existing content. This
23      * is normally possible only if the transformed content
24      * is of equal length or shorter.
25      *
26      * If you need to expand the content use the temporary
27      * memory pool mptmp to allocate the space.
28      */
29
30     /* Reverse the string in place, but only if it's long enough. */
31     if (input_len > 1) {
32         long int i = 0;
33         long int j = input_len - 1;
34         while(i < j) {
35             char c = input[i];
36             input[i] = input[j];
37             input[j] = c;
38             i++;
39             j--;
40         }
41     }
42
43     /* Tell ModSecurity about the content
44      * we have generated. In this case we
45      * merely point back to the input buffer.
46      */
47     *rval = (char *)input;
48     *rval_len = input_len;
49
50     /* Must return 1 if the content was
51      * changed, or 0 otherwise.
52      */
53     return 1; 
54 }
55
56 static int hook_pre_config(apr_pool_t *mp, apr_pool_t *mp_log, apr_pool_t *mp_temp) {
57     void (*fn)(const char *name, void *fn);
58
59     /* Look for the registration function
60      * exported by ModSecurity.
61      */
62     fn = APR_RETRIEVE_OPTIONAL_FN(modsec_register_tfn);
63     if (fn) {
64         /* Use it to register our new
65          * transformation function under the
66          * name "reverse".
67          */
68         fn("reverse", (void *)reverse);
69     }
70
71     return OK;
72 }
73
74 static void register_hooks(apr_pool_t *p) {
75     ap_hook_pre_config(hook_pre_config, NULL, NULL, APR_HOOK_LAST);
76 }
77
78 /* Dispatch list for API hooks */
79 module AP_MODULE_DECLARE_DATA tfn_reverse_module = {
80     STANDARD20_MODULE_STUFF, 
81     NULL,                  /* create per-dir    config structures */
82     NULL,                  /* merge  per-dir    config structures */
83     NULL,                  /* create per-server config structures */
84     NULL,                  /* merge  per-server config structures */
85     NULL,                  /* table of config file commands       */
86     register_hooks         /* register hooks                      */
87 };
88