annotate dpi/dpiutil.c @ 2104:3e7e5395f0bc

non-ASCII keybindings Alexander Voigt has kindly done some testing, and it seems that this makes bindings to most keys on a German keyboard possible -- except those that need AltGr don't work yet.
author corvid <corvid@lavabit.com>
date Thu, 23 Jun 2011 19:24:11 +0000
parents cea7268ebb05
children
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: dpiutil.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
4 * Copyright 2004-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <stdarg.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #define MSG(...) printf("[dpiutil.c]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 /* Escaping/De-escaping ---------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 * Escape URI characters in 'esc_set' as %XX sequences.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 * Return value: New escaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 */
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
36 char *Escape_uri_str(const char *str, const char *p_esc_set)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
38 static const char *esc_set, *hex = "0123456789ABCDEF";
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
39 char *p;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 esc_set = (p_esc_set) ? p_esc_set : "%#:' ";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 dStr_append_c(dstr, '%');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 dStr_append_c(dstr, hex[(str[i] >> 4) & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 dStr_append_c(dstr, hex[str[i] & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
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
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 static const char *unsafe_chars = "&<>\"'";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 static const char *unsafe_rep[] =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 { "&amp;", "&lt;", "&gt;", "&quot;", "&#39;" };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 static const int unsafe_rep_len[] = { 5, 4, 4, 6, 5 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 * Escape unsafe characters as html entities.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 * Return value: New escaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 char *Escape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 char *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 Dstr *dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 if ((p = strchr(unsafe_chars, str[i])))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 dStr_append(dstr, unsafe_rep[p - unsafe_chars]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 * Unescape a few HTML entities (inverse of Escape_html_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 * Return value: New unescaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 char *Unescape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 int i, j, k;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 char *u_str = dStrdup(str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 if (!strchr(str, '&'))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 for (i = 0, j = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 if (str[i] == '&') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 for (k = 0; k < 5; ++k) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 if (!dStrncasecmp(str + i, unsafe_rep[k], unsafe_rep_len[k])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 i += unsafe_rep_len[k] - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 u_str[j++] = (k < 5) ? unsafe_chars[k] : str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 u_str[j++] = str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 u_str[j] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 * Filter '\n', '\r', "%0D" and "%0A" from the authority part of an FTP url.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 * This helps to avoid a SMTP relaying hack. This filtering could be done
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 * only when port == 25, but if the mail server is listening on another
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 * port it wouldn't work.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 * Note: AFAIS this should be done by wget.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 char *Filter_smtp_hack(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 char c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 if (strlen(url) > 6) { /* ftp:// */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 for (i = 6; (c = url[i]) && c != '/'; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 if (c == '\n' || c == '\r') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 memmove(url + i, url + i + 1, strlen(url + i));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 } else if (c == '%' && url[i+1] == '0' &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 (tolower(url[i+2]) == 'a' || tolower(url[i+2]) == 'd')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 memmove(url + i, url + i + 3, strlen(url + i + 2));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 return url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165