Mercurial > dillo_port1.3
annotate dpi/dpiutil.c @ 2048:5060d415a85a
clickable menu items (even those introducing submenus) MUST have callbacks
I clicked on the "Panel size" item itself instead of any of the
options in its submenu, and: Segfault!
author | corvid <corvid@lavabit.com> |
---|---|
date | Thu, 26 May 2011 02:51:18 +0000 |
parents | cea7268ebb05 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * File: dpiutil.c | |
3 * | |
35 | 4 * Copyright 2004-2007 Jorge Arellano Cid <jcid@dillo.org> |
0 | 5 * |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 3 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 */ | |
12 | |
13 #include <unistd.h> | |
14 #include <stdio.h> | |
15 #include <stdarg.h> | |
16 #include <string.h> | |
17 #include <ctype.h> | |
18 #include <errno.h> | |
19 #include <sys/socket.h> | |
20 | |
21 #include "dpiutil.h" | |
22 | |
23 /* | |
24 * Debugging macros | |
25 */ | |
26 #define _MSG(...) | |
27 #define MSG(...) printf("[dpiutil.c]: " __VA_ARGS__) | |
28 | |
29 | |
30 /* Escaping/De-escaping ---------------------------------------------------*/ | |
31 | |
32 /* | |
33 * Escape URI characters in 'esc_set' as %XX sequences. | |
34 * Return value: New escaped string. | |
35 */ | |
306 | 36 char *Escape_uri_str(const char *str, const char *p_esc_set) |
0 | 37 { |
306 | 38 static const char *esc_set, *hex = "0123456789ABCDEF"; |
39 char *p; | |
0 | 40 Dstr *dstr; |
41 int i; | |
42 | |
43 esc_set = (p_esc_set) ? p_esc_set : "%#:' "; | |
44 dstr = dStr_sized_new(64); | |
45 for (i = 0; str[i]; ++i) { | |
46 if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) { | |
47 dStr_append_c(dstr, '%'); | |
48 dStr_append_c(dstr, hex[(str[i] >> 4) & 15]); | |
49 dStr_append_c(dstr, hex[str[i] & 15]); | |
50 } else { | |
51 dStr_append_c(dstr, str[i]); | |
52 } | |
53 } | |
54 p = dstr->str; | |
55 dStr_free(dstr, FALSE); | |
56 | |
57 return p; | |
58 } | |
59 | |
1305
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
60 /* |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
61 * Unescape %XX sequences in a string. |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
62 * Return value: a new unescaped string |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
63 */ |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
64 char *Unescape_uri_str(const char *s) |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
65 { |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
66 char *p, *buf = dStrdup(s); |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
67 |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
68 if (strchr(s, '%')) { |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
69 for (p = buf; (*p = *s); ++s, ++p) { |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
70 if (*p == '%' && isxdigit(s[1]) && isxdigit(s[2])) { |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
71 *p = (isdigit(s[1]) ? (s[1] - '0') : toupper(s[1]) - 'A' + 10)*16; |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
72 *p += isdigit(s[2]) ? (s[2] - '0') : toupper(s[2]) - 'A' + 10; |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
73 s += 2; |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
74 } |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
75 } |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
76 } |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
77 |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
78 return buf; |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
79 } |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
80 |
29b4740d571e
Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents:
306
diff
changeset
|
81 |
0 | 82 static const char *unsafe_chars = "&<>\"'"; |
83 static const char *unsafe_rep[] = | |
84 { "&", "<", ">", """, "'" }; | |
85 static const int unsafe_rep_len[] = { 5, 4, 4, 6, 5 }; | |
86 | |
87 /* | |
88 * Escape unsafe characters as html entities. | |
89 * Return value: New escaped string. | |
90 */ | |
91 char *Escape_html_str(const char *str) | |
92 { | |
93 int i; | |
94 char *p; | |
95 Dstr *dstr = dStr_sized_new(64); | |
96 | |
97 for (i = 0; str[i]; ++i) { | |
98 if ((p = strchr(unsafe_chars, str[i]))) | |
99 dStr_append(dstr, unsafe_rep[p - unsafe_chars]); | |
100 else | |
101 dStr_append_c(dstr, str[i]); | |
102 } | |
103 p = dstr->str; | |
104 dStr_free(dstr, FALSE); | |
105 | |
106 return p; | |
107 } | |
108 | |
109 /* | |
110 * Unescape a few HTML entities (inverse of Escape_html_str) | |
111 * Return value: New unescaped string. | |
112 */ | |
113 char *Unescape_html_str(const char *str) | |
114 { | |
115 int i, j, k; | |
116 char *u_str = dStrdup(str); | |
117 | |
118 if (!strchr(str, '&')) | |
119 return u_str; | |
120 | |
121 for (i = 0, j = 0; str[i]; ++i) { | |
122 if (str[i] == '&') { | |
123 for (k = 0; k < 5; ++k) { | |
124 if (!dStrncasecmp(str + i, unsafe_rep[k], unsafe_rep_len[k])) { | |
125 i += unsafe_rep_len[k] - 1; | |
126 break; | |
127 } | |
128 } | |
129 u_str[j++] = (k < 5) ? unsafe_chars[k] : str[i]; | |
130 } else { | |
131 u_str[j++] = str[i]; | |
132 } | |
133 } | |
134 u_str[j] = 0; | |
135 | |
136 return u_str; | |
137 } | |
138 | |
139 /* | |
140 * Filter '\n', '\r', "%0D" and "%0A" from the authority part of an FTP url. | |
141 * This helps to avoid a SMTP relaying hack. This filtering could be done | |
142 * only when port == 25, but if the mail server is listening on another | |
143 * port it wouldn't work. | |
144 * Note: AFAIS this should be done by wget. | |
145 */ | |
146 char *Filter_smtp_hack(char *url) | |
147 { | |
148 int i; | |
149 char c; | |
150 | |
151 if (strlen(url) > 6) { /* ftp:// */ | |
152 for (i = 6; (c = url[i]) && c != '/'; ++i) { | |
153 if (c == '\n' || c == '\r') { | |
154 memmove(url + i, url + i + 1, strlen(url + i)); | |
155 --i; | |
156 } else if (c == '%' && url[i+1] == '0' && | |
157 (tolower(url[i+2]) == 'a' || tolower(url[i+2]) == 'd')) { | |
158 memmove(url + i, url + i + 3, strlen(url + i + 2)); | |
159 --i; | |
160 } | |
161 } | |
162 } | |
163 return url; | |
164 } | |
165 |