xref: /freebsd-13-stable/contrib/subversion/subversion/libsvn_subr/apr_escape.c (revision 83b1614a2e395e821e8a3064f1dad1c854a9e3f9)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* The code in this file is copied from APR (initially from APR 1.7.0)
18  * to provide compatibility for building against APR < 1.5.0.
19  */
20 
21 #include "private/svn_dep_compat.h"
22 
23 #if !APR_VERSION_AT_LEAST(1,5,0)
24 
25 #include <apr_lib.h>
26 #include <apr_strings.h>
27 
28 /* from apr_escape_test_char.h */
29 #define T_ESCAPE_SHELL_CMD     (1)
30 #define T_ESCAPE_PATH_SEGMENT  (2)
31 #define T_OS_ESCAPE_PATH       (4)
32 #define T_ESCAPE_ECHO          (8)
33 #define T_ESCAPE_URLENCODED    (16)
34 #define T_ESCAPE_XML           (32)
35 #define T_ESCAPE_LDAP_DN       (64)
36 #define T_ESCAPE_LDAP_FILTER   (128)
37 
38 static const unsigned char test_char_table[256] = {
39     224,222,222,222,222,222,222,222,222,222,223,222,222,222,222,222,222,222,222,222,
40     222,222,222,222,222,222,222,222,222,222,222,222,6,16,127,22,17,22,49,17,
41     145,145,129,80,80,0,0,18,0,0,0,0,0,0,0,0,0,0,16,87,
42     119,16,119,23,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
43     0,0,0,0,0,0,0,0,0,0,0,23,223,23,23,0,23,0,0,0,
44     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
45     0,0,0,23,23,23,17,222,222,222,222,222,222,222,222,222,222,222,222,222,
46     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
47     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
48     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
49     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
50     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
51     222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222
52 };
53 
54 /* from apr_encode_private.h */
55 #if APR_CHARSET_EBCDIC
56 #error This Subversion compatibility code for APR<1.5 does not support EBCDIC.
57 #else                           /* APR_CHARSET_EBCDIC */
58 #define ENCODE_TO_ASCII(ch)  (ch)
59 #define ENCODE_TO_NATIVE(ch)  (ch)
60 #endif                          /* !APR_CHARSET_EBCDIC */
61 
62 /* we assume the folks using this ensure 0 <= c < 256... which means
63  * you need a cast to (unsigned char) first, you can't just plug a
64  * char in here and get it to work, because if char is signed then it
65  * will first be sign extended.
66  */
67 #define TEST_CHAR(c, f)        (test_char_table[(unsigned)(c)] & (f))
68 
apr_escape_shell(char * escaped,const char * str,apr_ssize_t slen,apr_size_t * len)69 APR_DECLARE(apr_status_t) apr_escape_shell(char *escaped, const char *str,
70         apr_ssize_t slen, apr_size_t *len)
71 {
72     unsigned char *d;
73     const unsigned char *s;
74     apr_size_t size = 1;
75     int found = 0;
76 
77     d = (unsigned char *) escaped;
78     s = (const unsigned char *) str;
79 
80     if (s) {
81         if (d) {
82             for (; *s && slen; ++s, slen--) {
83 #if defined(OS2) || defined(WIN32)
84                 /*
85                  * Newlines to Win32/OS2 CreateProcess() are ill advised.
86                  * Convert them to spaces since they are effectively white
87                  * space to most applications
88                  */
89                 if (*s == '\r' || *s == '\n') {
90                     if (d) {
91                         *d++ = ' ';
92                         found = 1;
93                     }
94                     continue;
95                 }
96 #endif
97                 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
98                     *d++ = '\\';
99                     size++;
100                     found = 1;
101                 }
102                 *d++ = *s;
103                 size++;
104             }
105             *d = '\0';
106         }
107         else {
108             for (; *s && slen; ++s, slen--) {
109                 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
110                     size++;
111                     found = 1;
112                 }
113                 size++;
114             }
115         }
116     }
117 
118     if (len) {
119         *len = size;
120     }
121     if (!found) {
122         return APR_NOTFOUND;
123     }
124 
125     return APR_SUCCESS;
126 }
127 
128 #else  /* APR_VERSION_AT_LEAST(1,5,0) */
129 
130 /* Silence OSX ranlib warnings about object files with no symbols. */
131 #include <apr.h>
132 extern const apr_uint32_t svn__fake__apr_escape;
133 const apr_uint32_t svn__fake__apr_escape = 0xdeadbeef;
134 
135 #endif /* !APR_VERSION_AT_LEAST(1,5,0) */
136