1251881Speter/*
2251881Speter * log-escape.c :  Functions for escaping log items
3251881Speter *                 copied from Apache httpd
4251881Speter *
5251881Speter * ====================================================================
6251881Speter * Licensed to the Apache Software Foundation (ASF) under one or more
7251881Speter * contributor license agreements.  See the NOTICE file distributed with
8251881Speter * this work for additional information regarding copyright ownership.
9251881Speter * The ASF licenses this file to You under the Apache License, Version 2.0
10251881Speter * (the "License"); you may not use this file except in compliance with
11251881Speter * the License.  You may obtain a copy of the License at
12251881Speter *
13251881Speter *     http://www.apache.org/licenses/LICENSE-2.0
14251881Speter *
15251881Speter * Unless required by applicable law or agreed to in writing, software
16251881Speter * distributed under the License is distributed on an "AS IS" BASIS,
17251881Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18251881Speter * See the License for the specific language governing permissions and
19251881Speter * limitations under the License.
20251881Speter *
21251881Speter * ====================================================================
22251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
23251881Speter *    or more contributor license agreements.  See the NOTICE file
24251881Speter *    distributed with this work for additional information
25251881Speter *    regarding copyright ownership.  The ASF licenses this file
26251881Speter *    to you under the Apache License, Version 2.0 (the
27251881Speter *    "License"); you may not use this file except in compliance
28251881Speter *    with the License.  You may obtain a copy of the License at
29251881Speter *
30251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
31251881Speter *
32251881Speter *    Unless required by applicable law or agreed to in writing,
33251881Speter *    software distributed under the License is distributed on an
34251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35251881Speter *    KIND, either express or implied.  See the License for the
36251881Speter *    specific language governing permissions and limitations
37251881Speter *    under the License.
38251881Speter * ====================================================================
39251881Speter */
40251881Speter
41251881Speter
42251881Speter#include <apr.h>
43251881Speter#define APR_WANT_STRFUNC
44251881Speter#include <apr_want.h>
45251881Speter#include "server.h"
46251881Speter#include "svn_ctype.h"
47251881Speter
48251881Speter/* copied from httpd-2.2.4/server/util.c */
49251881Speter/* c2x takes an unsigned, and expects the caller has guaranteed that
50251881Speter * 0 <= what < 256... which usually means that you have to cast to
51251881Speter * unsigned char first, because (unsigned)(char)(x) first goes through
52251881Speter * signed extension to an int before the unsigned cast.
53251881Speter *
54251881Speter * The reason for this assumption is to assist gcc code generation --
55251881Speter * the unsigned char -> unsigned extension is already done earlier in
56251881Speter * both uses of this code, so there's no need to waste time doing it
57251881Speter * again.
58251881Speter */
59251881Speterstatic const char c2x_table[] = "0123456789abcdef";
60251881Speter
61251881Speter/* copied from httpd-2.2.4/server/util.c */
62251881Speterstatic APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
63251881Speter                                     unsigned char *where)
64251881Speter{
65251881Speter#if APR_CHARSET_EBCDIC
66251881Speter    what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
67251881Speter#endif /*APR_CHARSET_EBCDIC*/
68251881Speter    *where++ = prefix;
69251881Speter    *where++ = c2x_table[what >> 4];
70251881Speter    *where++ = c2x_table[what & 0xf];
71251881Speter    return where;
72251881Speter}
73251881Speter
74251881Speter/* copied from httpd-2.2.4/server/util.c */
75251881Speterapr_size_t escape_errorlog_item(char *dest, const char *source,
76251881Speter                                apr_size_t buflen)
77251881Speter{
78251881Speter    unsigned char *d, *ep;
79251881Speter    const unsigned char *s;
80251881Speter
81251881Speter    if (!source || !buflen) { /* be safe */
82251881Speter        return 0;
83251881Speter    }
84251881Speter
85251881Speter    d = (unsigned char *)dest;
86251881Speter    s = (const unsigned char *)source;
87251881Speter    ep = d + buflen - 1;
88251881Speter
89251881Speter    for (; d < ep && *s; ++s) {
90251881Speter
91251881Speter        /* httpd-2.2.4/server/util.c has this:
92251881Speter             if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
93251881Speter           which does this same check with a fast lookup table.  Well,
94251881Speter           mostly the same; we don't escape quotes, as that does.
95251881Speter        */
96251881Speter        if (*s && (   !svn_ctype_isprint(*s)
97251881Speter                   || *s == '\\'
98251881Speter                   || svn_ctype_iscntrl(*s))) {
99251881Speter            *d++ = '\\';
100251881Speter            if (d >= ep) {
101251881Speter                --d;
102251881Speter                break;
103251881Speter            }
104251881Speter
105251881Speter            switch(*s) {
106251881Speter            case '\b':
107251881Speter                *d++ = 'b';
108251881Speter                break;
109251881Speter            case '\n':
110251881Speter                *d++ = 'n';
111251881Speter                break;
112251881Speter            case '\r':
113251881Speter                *d++ = 'r';
114251881Speter                break;
115251881Speter            case '\t':
116251881Speter                *d++ = 't';
117251881Speter                break;
118251881Speter            case '\v':
119251881Speter                *d++ = 'v';
120251881Speter                break;
121251881Speter            case '\\':
122251881Speter                *d++ = *s;
123251881Speter                break;
124251881Speter            case '"': /* no need for this in error log */
125251881Speter                d[-1] = *s;
126251881Speter                break;
127251881Speter            default:
128251881Speter                if (d >= ep - 2) {
129251881Speter                    ep = --d; /* break the for loop as well */
130251881Speter                    break;
131251881Speter                }
132251881Speter                c2x(*s, 'x', d);
133251881Speter                d += 3;
134251881Speter            }
135251881Speter        }
136251881Speter        else {
137251881Speter            *d++ = *s;
138251881Speter        }
139251881Speter    }
140251881Speter    *d = '\0';
141251881Speter
142251881Speter    return (d - (unsigned char *)dest);
143251881Speter}
144