fix compiler warning
[ladish.git] / daemon / escape.c
blob1d4e0ccd0395dbd3a53d45f13d8328e26c6ace04
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the escape helper functions
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "escape.h"
29 static char hex_digits[] = "0123456789ABCDEF";
31 #define HEX_TO_INT(hexchar) ((hexchar) <= '9' ? hexchar - '0' : 10 + (hexchar - 'A'))
33 void escape(const char ** src_ptr, char ** dst_ptr, unsigned int flags)
35 const char * src;
36 char * dst;
38 src = *src_ptr;
39 dst = *dst_ptr;
41 while (*src != 0)
43 switch (*src)
45 case '/': /* used as separator for address components */
46 case '\'':
47 case '>':
48 case '%':
49 if ((flags & LADISH_ESCAPE_FLAG_OTHER) == 0)
51 break;
53 case '<': /* invalid attribute value char (XML spec) */
54 case '&': /* invalid attribute value char (XML spec) */
55 case '"': /* we store attribute values in double quotes - invalid attribute value char (XML spec) */
56 if ((flags & LADISH_ESCAPE_FLAG_XML_ATTR) == 0)
58 break;
60 dst[0] = '%';
61 dst[1] = hex_digits[*src >> 4];
62 dst[2] = hex_digits[*src & 0x0F];
63 dst += 3;
64 src++;
65 continue;
68 *dst++ = *src++;
71 *src_ptr = src;
72 *dst_ptr = dst;
75 void escape_simple(const char * src_ptr, char * dst_ptr, unsigned int flags)
77 escape(&src_ptr, &dst_ptr, flags);
78 *dst_ptr = 0;
81 size_t unescape(const char * src, size_t src_len, char * dst)
83 size_t dst_len;
85 dst_len = 0;
87 while (src_len)
89 if (src_len >= 3 &&
90 src[0] == '%' &&
91 ((src[1] >= '0' && src[1] <= '9') ||
92 (src[1] >= 'A' && src[1] <= 'F')) &&
93 ((src[2] >= '0' && src[2] <= '9') ||
94 (src[2] >= 'A' && src[2] <= 'F')))
96 *dst = (HEX_TO_INT(src[1]) << 4) | HEX_TO_INT(src[2]);
97 //lash_info("unescaping %c%c%c to '%c'", src[0], src[1], src[2], *dst);
98 src_len -= 3;
99 src += 3;
101 else
103 *dst = *src;
104 src_len--;
105 src++;
107 dst++;
108 dst_len++;
111 return dst_len;
114 void unescape_simple(char * buffer)
116 unescape(buffer, strlen(buffer) + 1, buffer);
119 char * unescape_dup(const char * src)
121 size_t len;
122 char * dst;
124 len = strlen(src) + 1;
126 dst = malloc(len);
127 if (dst != NULL)
129 unescape(src, len, dst);
132 return dst;