-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
224 lines (184 loc) · 4.03 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Generic utility functions
Copyright 2013 Henrik Andersson <hean01@cendio.se> for Cendio AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef HAVE_ICONV_H
#include <iconv.h>
#endif
#include "rdesktop.h"
#ifdef HAVE_ICONV
extern char g_codepage[16];
static RD_BOOL g_iconv_works = True;
#endif
char *
utils_string_escape(const char *str)
{
const char *p;
char *pe, *e, esc[4];
size_t es;
int cnt;
/* count indices */
cnt = 0;
p = str;
while (*(p++) != '\0')
if ((unsigned char) *p < 32 || *p == '%')
cnt++;
/* if no characters needs escaping return copy of str */
if (cnt == 0)
return strdup(str);
/* allocate new mem for result */
es = strlen(str) + (cnt * 3) + 1;
pe = e = xmalloc(es);
memset(e, 0, es);
p = str;
while (*p != '\0')
{
if ((unsigned char) *p < 32 || *p == '%')
{
snprintf(esc, 4, "%%%02X", *p);
memcpy(pe, esc, 3);
pe += 3;
}
else
{
*pe = *p;
pe++;
}
p++;
}
return e;
}
char *
utils_string_unescape(const char *str)
{
char *ns, *ps, *pd, c;
ns = xmalloc(strlen(str) + 1);
memcpy(ns, str, strlen(str) + 1);
ps = pd = ns;
while (*ps != '\0')
{
/* check if found escaped character */
if (ps[0] == '%')
{
if (sscanf(ps, "%%%2hhX", &c) == 1)
{
pd[0] = c;
ps += 3;
pd++;
continue;
}
}
/* just copy over the char */
*pd = *ps;
ps++;
pd++;
}
pd[0] = '\0';
return ns;
}
int
utils_mkdir_safe(const char *path, int mask)
{
int res = 0;
struct stat st;
res = stat(path, &st);
if (res == -1)
return mkdir(path, mask);
if (!S_ISDIR(st.st_mode))
{
errno = EEXIST;
return -1;
}
return 0;
}
int
utils_mkdir_p(const char *path, int mask)
{
int res;
char *ptok;
char pt[PATH_MAX];
char bp[PATH_MAX];
if (!path || strlen(path) == 0)
{
errno = EINVAL;
return -1;
}
if (strlen(path) > PATH_MAX)
{
errno = E2BIG;
return -1;
}
res = 0;
pt[0] = bp[0] = '\0';
strcpy(bp, path);
ptok = strtok(bp, "/");
if (ptok == NULL)
return utils_mkdir_safe(path, mask);
do
{
if (ptok != bp)
strcat(pt, "/");
strcat(pt, ptok);
res = utils_mkdir_safe(pt, mask);
if (res != 0)
return res;
}
while ((ptok = strtok(NULL, "/")) != NULL);
return 0;
}
/* Convert from system locale string to utf-8 */
int
utils_locale_to_utf8(const char *src, size_t is, char *dest, size_t os)
{
#ifdef HAVE_ICONV
static iconv_t *iconv_h = (iconv_t) - 1;
if (strncmp(g_codepage, "UTF-8", strlen("UTF-8")) == 0)
goto pass_trough_as_is;
if (g_iconv_works == False)
goto pass_trough_as_is;
/* if not already initialize */
if (iconv_h == (iconv_t) - 1)
{
if ((iconv_h = iconv_open("UTF-8", g_codepage)) == (iconv_t) - 1)
{
warning("utils_string_to_utf8: iconv_open[%s -> %s] fail %p\n",
g_codepage, "UTF-8", iconv_h);
g_iconv_works = False;
goto pass_trough_as_is;
}
}
/* convert string */
if (iconv(iconv_h, (ICONV_CONST char **) &src, &is, &dest, &os) == (size_t) - 1)
{
iconv_close(iconv_h);
iconv_h = (iconv_t) - 1;
warning("utils_string_to_utf8: iconv(1) fail, errno %d\n", errno);
g_iconv_works = False;
goto pass_trough_as_is;
}
/* Out couldn't hold the entire convertion */
if (is != 0)
return -1;
#endif
pass_trough_as_is:
/* can dest hold strcpy of src */
if (os < (strlen(src) + 1))
return -1;
memcpy(dest, src, strlen(src) + 1);
return 0;
}