VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.3/utils.c@ 55121

Last change on this file since 55121 was 55121, checked in by vboxsync, 10 years ago

rdesktop 1.8.3 unmodified

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette