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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* escape/unescape functions.
19 * These functions perform various escaping operations, and are provided in
20 * pairs, a function to query the length of and escape existing buffers, as
21 * well as companion functions to perform the same process to memory
22 * allocated from a pool.
24 * The API is designed to have the smallest possible RAM footprint, and so
25 * will only allocate the exact amount of RAM needed for each conversion.
28 #include "apr_escape.h"
29 #include "apr_escape_test_char.h"
31 #include "apr_strings.h"
33 #if APR_CHARSET_EBCDIC
34 static int convert_a2e[256] = {
35 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
36 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
37 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
38 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
39 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
40 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
41 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
42 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B,
44 0x30, 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF,
45 0x41, 0xAA, 0x4A, 0xB1, 0x9F, 0xB2, 0x6A, 0xB5, 0xBB, 0xB4, 0x9A, 0x8A, 0xB0, 0xCA, 0xAF, 0xBC,
46 0x90, 0x8F, 0xEA, 0xFA, 0xBE, 0xA0, 0xB6, 0xB3, 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8, 0xB9, 0xAB,
47 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9E, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
48 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xBA, 0xAE, 0x59,
49 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
50 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF };
52 static int convert_e2a[256] = {
53 0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F, 0x97, 0x8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A8, 0xAF, 0x5D, 0xB4, 0xD7,
65 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xAD, 0xF4, 0xF6, 0xF2, 0xF3, 0xF5,
66 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xFF,
67 0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5,
68 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, 0x9F };
69 #define RAW_ASCII_CHAR(ch) convert_e2a[(unsigned char)ch]
70 #else /* APR_CHARSET_EBCDIC */
71 #define RAW_ASCII_CHAR(ch) (ch)
72 #endif /* !APR_CHARSET_EBCDIC */
74 /* we assume the folks using this ensure 0 <= c < 256... which means
75 * you need a cast to (unsigned char) first, you can't just plug a
76 * char in here and get it to work, because if char is signed then it
77 * will first be sign extended.
79 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
81 APR_DECLARE(apr_status_t) apr_escape_shell(char *escaped, const char *str,
82 apr_ssize_t slen, apr_size_t *len)
85 const unsigned char *s;
89 d = (unsigned char *) escaped;
90 s = (const unsigned char *) str;
94 for (; *s && slen; ++s, slen--) {
95 #if defined(OS2) || defined(WIN32)
97 * Newlines to Win32/OS2 CreateProcess() are ill advised.
98 * Convert them to spaces since they are effectively white
99 * space to most applications
101 if (*s == '\r' || *s == '\n') {
109 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
120 for (; *s && slen; ++s, slen--) {
121 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
140 APR_DECLARE(const char *) apr_pescape_shell(apr_pool_t *p, const char *str)
144 switch (apr_escape_shell(NULL, str, APR_ESCAPE_STRING, &len)) {
146 char *cmd = apr_palloc(p, len);
147 apr_escape_shell(cmd, str, APR_ESCAPE_STRING, NULL);
158 static char x2c(const char *what)
162 #if !APR_CHARSET_EBCDIC
164 ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
166 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
167 #else /*APR_CHARSET_EBCDIC*/
174 digit = convert_a2e[0xFF & strtol(xstr, NULL, 16)];
175 #endif /*APR_CHARSET_EBCDIC*/
179 APR_DECLARE(apr_status_t) apr_unescape_url(char *escaped, const char *url,
180 apr_ssize_t slen, const char *forbid, const char *reserved, int plus,
185 const char *s = (const char *) url;
186 char *d = (char *) escaped;
187 register int badesc, badpath;
197 for (; *s && slen; ++s, d++, slen--) {
198 if (plus && *s == '+') {
202 else if (*s != '%') {
206 if (!apr_isxdigit(*(s + 1)) || !apr_isxdigit(*(s + 2))) {
212 decoded = x2c(s + 1);
213 if ((decoded == '\0')
214 || (forbid && strchr(forbid, decoded))) {
220 else if (reserved && strchr(reserved, decoded)) {
239 for (; *s && slen; ++s, slen--) {
240 if (plus && *s == '+') {
243 else if (*s != '%') {
244 /* character unchanged */
247 if (!apr_isxdigit(*(s + 1)) || !apr_isxdigit(*(s + 2))) {
252 decoded = x2c(s + 1);
253 if ((decoded == '\0')
254 || (forbid && strchr(forbid, decoded))) {
259 else if (reserved && strchr(reserved, decoded)) {
292 APR_DECLARE(const char *) apr_punescape_url(apr_pool_t *p, const char *url,
293 const char *forbid, const char *reserved, int plus)
297 switch (apr_unescape_url(NULL, url, APR_ESCAPE_STRING, forbid, reserved,
300 char *buf = apr_palloc(p, len);
301 apr_unescape_url(buf, url, APR_ESCAPE_STRING, forbid, reserved, plus,
317 /* c2x takes an unsigned, and expects the caller has guaranteed that
318 * 0 <= what < 256... which usually means that you have to cast to
319 * unsigned char first, because (unsigned)(char)(x) first goes through
320 * signed extension to an int before the unsigned cast.
322 * The reason for this assumption is to assist gcc code generation --
323 * the unsigned char -> unsigned extension is already done earlier in
324 * both uses of this code, so there's no need to waste time doing it
327 static const char c2x_table[] = "0123456789abcdef";
329 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
330 unsigned char *where)
332 #if APR_CHARSET_EBCDIC
333 what = convert_e2a[(unsigned char)what];
334 #endif /*APR_CHARSET_EBCDIC*/
336 *where++ = c2x_table[what >> 4];
337 *where++ = c2x_table[what & 0xf];
341 APR_DECLARE(apr_status_t) apr_escape_path_segment(char *escaped,
342 const char *str, apr_ssize_t slen, apr_size_t *len)
346 const unsigned char *s = (const unsigned char *) str;
347 unsigned char *d = (unsigned char *) escaped;
352 while ((c = *s) && slen) {
353 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
368 while ((c = *s) && slen) {
369 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
390 APR_DECLARE(const char *) apr_pescape_path_segment(apr_pool_t *p,
395 switch (apr_escape_path_segment(NULL, str, APR_ESCAPE_STRING, &len)) {
397 char *cmd = apr_palloc(p, len);
398 apr_escape_path_segment(cmd, str, APR_ESCAPE_STRING, NULL);
409 APR_DECLARE(apr_status_t) apr_escape_path(char *escaped, const char *path,
410 apr_ssize_t slen, int partial, apr_size_t *len)
414 const unsigned char *s = (const unsigned char *) path;
415 unsigned char *d = (unsigned char *) escaped;
423 const char *colon = strchr(path, ':');
424 const char *slash = strchr(path, '/');
426 if (colon && (!slash || colon < slash)) {
436 while ((c = *s) && slen) {
437 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
452 while ((c = *s) && slen) {
453 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
473 APR_DECLARE(const char *) apr_pescape_path(apr_pool_t *p, const char *str,
478 switch (apr_escape_path(NULL, str, APR_ESCAPE_STRING, partial, &len)) {
480 char *path = apr_palloc(p, len);
481 apr_escape_path(path, str, APR_ESCAPE_STRING, partial, NULL);
492 APR_DECLARE(apr_status_t) apr_escape_urlencoded(char *escaped, const char *str,
493 apr_ssize_t slen, apr_size_t *len)
497 const unsigned char *s = (const unsigned char *) str;
498 unsigned char *d = (unsigned char *) escaped;
503 while ((c = *s) && slen) {
504 if (TEST_CHAR(c, T_ESCAPE_URLENCODED)) {
523 while ((c = *s) && slen) {
524 if (TEST_CHAR(c, T_ESCAPE_URLENCODED)) {
548 APR_DECLARE(const char *) apr_pescape_urlencoded(apr_pool_t *p, const char *str)
552 switch (apr_escape_urlencoded(NULL, str, APR_ESCAPE_STRING, &len)) {
554 char *encoded = apr_palloc(p, len);
555 apr_escape_urlencoded(encoded, str, APR_ESCAPE_STRING, NULL);
566 APR_DECLARE(apr_status_t) apr_escape_entity(char *escaped, const char *str,
567 apr_ssize_t slen, int toasc, apr_size_t *len)
571 const unsigned char *s = (const unsigned char *) str;
572 unsigned char *d = (unsigned char *) escaped;
577 while ((c = *s) && slen) {
578 if (TEST_CHAR(c, T_ESCAPE_XML)) {
581 memcpy(d, ">", 4);
587 memcpy(d, "<", 4);
593 memcpy(d, "&", 5);
599 memcpy(d, """, 6);
605 memcpy(d, "'", 6);
613 else if (toasc && !apr_isascii(c)) {
614 int offset = apr_snprintf((char *) d, 6, "&#%3.3d;", c);
629 while ((c = *s) && slen) {
630 if (TEST_CHAR(c, T_ESCAPE_XML)) {
655 else if (toasc && !apr_isascii(c)) {
657 size += apr_snprintf(buf, 6, "&#%3.3d;", c);
679 APR_DECLARE(const char *) apr_pescape_entity(apr_pool_t *p, const char *str,
684 switch (apr_escape_entity(NULL, str, APR_ESCAPE_STRING, toasc, &len)) {
686 char *cmd = apr_palloc(p, len);
687 apr_escape_entity(cmd, str, APR_ESCAPE_STRING, toasc, NULL);
698 /* maximum length of any ISO-LATIN-1 HTML entity name. */
699 #define MAXENTLEN (6)
701 APR_DECLARE(apr_status_t) apr_unescape_entity(char *unescaped, const char *str,
702 apr_ssize_t slen, apr_size_t *len)
710 static const char * const entlist[MAXENTLEN + 1] =
714 "lt\074gt\076", /* 2 */
715 "amp\046ETH\320eth\360", /* 3 */
716 "quot\042Auml\304Euml\313Iuml\317Ouml\326Uuml\334auml\344euml"
717 "\353iuml\357ouml\366uuml\374yuml\377", /* 4 */
718 "Acirc\302Aring\305AElig\306Ecirc\312Icirc\316Ocirc\324Ucirc"
719 "\333THORN\336szlig\337acirc\342aring\345aelig\346ecirc\352"
720 "icirc\356ocirc\364ucirc\373thorn\376", /* 5 */
721 "Agrave\300Aacute\301Atilde\303Ccedil\307Egrave\310Eacute\311"
722 "Igrave\314Iacute\315Ntilde\321Ograve\322Oacute\323Otilde"
723 "\325Oslash\330Ugrave\331Uacute\332Yacute\335agrave\340"
724 "aacute\341atilde\343ccedil\347egrave\350eacute\351igrave"
725 "\354iacute\355ntilde\361ograve\362oacute\363otilde\365"
726 "oslash\370ugrave\371uacute\372yacute\375" /* 6 */
731 for (; *s != '\0' && slen; s++, d++, size++, slen--) {
736 /* find end of entity */
737 for (i = 1; s[i] != ';' && s[i] != '\0' && (slen - i) != 0;
742 if (s[i] == '\0' || (slen - i) == 0) { /* treat as normal data */
747 /* is it numeric ? */
749 for (j = 2, val = 0; j < i && apr_isdigit(s[j]); j++) {
750 val = val * 10 + s[j] - '0';
753 if (j < i || val <= 8 || (val >= 11 && val <= 31)
754 || (val >= 127 && val <= 160) || val >= 256) {
755 d--; /* no data to output */
759 *d = RAW_ASCII_CHAR(val);
765 if (j > MAXENTLEN || entlist[j] == NULL) {
768 continue; /* skip it */
770 for (ents = entlist[j]; *ents != '\0'; ents += i) {
771 if (strncmp(s + 1, ents, j) == 0) {
777 *d = '&'; /* unknown */
780 *d = RAW_ASCII_CHAR(((const unsigned char *) ents)[j]);
790 for (; *s != '\0' && slen; s++, size++, slen--) {
794 /* find end of entity */
795 for (i = 1; s[i] != ';' && s[i] != '\0' && (slen - i) != 0;
800 if (s[i] == '\0' || (slen - i) == 0) { /* treat as normal data */
804 /* is it numeric ? */
806 for (j = 2, val = 0; j < i && apr_isdigit(s[j]); j++) {
807 val = val * 10 + s[j] - '0';
810 if (j < i || val <= 8 || (val >= 11 && val <= 31)
811 || (val >= 127 && val <= 160) || val >= 256) {
812 /* no data to output */
821 if (j > MAXENTLEN || entlist[j] == NULL) {
823 continue; /* skip it */
825 for (ents = entlist[j]; *ents != '\0'; ents += i) {
826 if (strncmp(s + 1, ents, j) == 0) {
854 APR_DECLARE(const char *) apr_punescape_entity(apr_pool_t *p, const char *str)
858 switch (apr_unescape_entity(NULL, str, APR_ESCAPE_STRING, &len)) {
860 char *cmd = apr_palloc(p, len);
861 apr_unescape_entity(cmd, str, APR_ESCAPE_STRING, NULL);
872 APR_DECLARE(apr_status_t) apr_escape_echo(char *escaped, const char *str,
873 apr_ssize_t slen, int quote, apr_size_t *len)
877 const unsigned char *s = (const unsigned char *) str;
878 unsigned char *d = (unsigned char *) escaped;
883 while ((c = *s) && slen) {
884 if (TEST_CHAR(c, T_ESCAPE_ECHO)) {
956 while ((c = *s) && slen) {
957 if (TEST_CHAR(c, T_ESCAPE_ECHO)) {
1002 APR_DECLARE(const char *) apr_pescape_echo(apr_pool_t *p, const char *str,
1007 switch (apr_escape_echo(NULL, str, APR_ESCAPE_STRING, quote, &len)) {
1009 char *cmd = apr_palloc(p, len);
1010 apr_escape_echo(cmd, str, APR_ESCAPE_STRING, quote, NULL);
1013 case APR_NOTFOUND: {
1021 APR_DECLARE(apr_status_t) apr_escape_hex(char *dest, const void *src,
1022 apr_size_t srclen, int colon, apr_size_t *len)
1024 const unsigned char *in = src;
1028 return APR_NOTFOUND;
1032 for (size = 0; size < srclen; size++) {
1033 if (colon && size) {
1036 *dest++ = c2x_table[in[size] >> 4];
1037 *dest++ = c2x_table[in[size] & 0xf];
1043 if (colon && srclen) {
1047 *len = srclen * 2 + 1;
1054 APR_DECLARE(const char *) apr_pescape_hex(apr_pool_t *p, const void *src,
1055 apr_size_t srclen, int colon)
1059 switch (apr_escape_hex(NULL, src, srclen, colon, &len)) {
1061 char *cmd = apr_palloc(p, len);
1062 apr_escape_hex(cmd, src, srclen, colon, NULL);
1065 case APR_NOTFOUND: {
1073 APR_DECLARE(apr_status_t) apr_unescape_hex(void *dest, const char *str,
1074 apr_ssize_t slen, int colon, apr_size_t *len)
1076 apr_size_t size = 0;
1078 const unsigned char *s = (const unsigned char *) str;
1079 unsigned char *d = (unsigned char *) dest;
1081 unsigned char u = 0;
1085 while ((c = *s) && slen) {
1091 if (colon && c == ':' && !flip) {
1096 else if (apr_isdigit(c)) {
1099 else if (apr_isupper(c) && c <= 'F') {
1100 u |= c - ('A' - 10);
1102 else if (apr_islower(c) && c <= 'f') {
1103 u |= c - ('a' - 10);
1124 while ((c = *s) && slen) {
1126 if (colon && c == ':' && !flip) {
1131 else if (apr_isdigit(c)) {
1134 else if (apr_isupper(c) && c <= 'F') {
1137 else if (apr_islower(c) && c <= 'f') {
1159 return APR_NOTFOUND;
1165 APR_DECLARE(const void *) apr_punescape_hex(apr_pool_t *p, const char *str,
1166 int colon, apr_size_t *len)
1170 switch (apr_unescape_hex(NULL, str, APR_ESCAPE_STRING, colon, &size)) {
1172 void *cmd = apr_palloc(p, size);
1173 apr_unescape_hex(cmd, str, APR_ESCAPE_STRING, colon, len);
1177 case APR_NOTFOUND: {