VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.4/utils.c@ 83298

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

rdesktop 1.8.3 modified for VBox

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 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/*
21 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
22 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
23 * the General Public License version 2 (GPLv2) at this time for any software where
24 * a choice of GPL license versions is made available with the language indicating
25 * that GPLv2 or any later version may be used, or where a choice of which version
26 * of the GPL is applied is otherwise unspecified.
27 */
28
29#include <stdio.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <errno.h>
33#ifdef HAVE_ICONV_H
34#include <iconv.h>
35#endif
36#include "rdesktop.h"
37
38
39#ifdef HAVE_ICONV
40extern char g_codepage[16];
41static RD_BOOL g_iconv_works = True;
42#endif
43
44
45
46char *
47utils_string_escape(const char *str)
48{
49 const char *p;
50 char *pe, *e, esc[4];
51 size_t es;
52 int cnt;
53
54 /* count indices */
55 cnt = 0;
56 p = str;
57 while (*(p++) != '\0')
58 if ((unsigned char) *p < 32 || *p == '%')
59 cnt++;
60
61 /* if no characters needs escaping return copy of str */
62 if (cnt == 0)
63 return strdup(str);
64
65 /* allocate new mem for result */
66 es = strlen(str) + (cnt * 3) + 1;
67 pe = e = xmalloc(es);
68 memset(e, 0, es);
69 p = str;
70 while (*p != '\0')
71 {
72 if ((unsigned char) *p < 32 || *p == '%')
73 {
74 snprintf(esc, 4, "%%%02X", *p);
75 memcpy(pe, esc, 3);
76 pe += 3;
77 }
78 else
79 {
80 *pe = *p;
81 pe++;
82 }
83
84 p++;
85 }
86
87 return e;
88}
89
90char *
91utils_string_unescape(const char *str)
92{
93 char *ns, *ps, *pd, c;
94
95 ns = xmalloc(strlen(str) + 1);
96 memcpy(ns, str, strlen(str) + 1);
97 ps = pd = ns;
98
99 while (*ps != '\0')
100 {
101 /* check if found escaped character */
102 if (ps[0] == '%')
103 {
104 if (sscanf(ps, "%%%2hhX", &c) == 1)
105 {
106 pd[0] = c;
107 ps += 3;
108 pd++;
109 continue;
110 }
111 }
112
113 /* just copy over the char */
114 *pd = *ps;
115 ps++;
116 pd++;
117 }
118 pd[0] = '\0';
119
120 return ns;
121}
122
123int
124utils_mkdir_safe(const char *path, int mask)
125{
126 int res = 0;
127 struct stat st;
128
129 res = stat(path, &st);
130 if (res == -1)
131 return mkdir(path, mask);
132
133 if (!S_ISDIR(st.st_mode))
134 {
135 errno = EEXIST;
136 return -1;
137 }
138
139 return 0;
140}
141
142int
143utils_mkdir_p(const char *path, int mask)
144{
145 int res;
146 char *ptok;
147 char pt[PATH_MAX];
148 char bp[PATH_MAX];
149
150 if (!path || strlen(path) == 0)
151 {
152 errno = EINVAL;
153 return -1;
154 }
155 if (strlen(path) > PATH_MAX)
156 {
157 errno = E2BIG;
158 return -1;
159 }
160
161 res = 0;
162 pt[0] = bp[0] = '\0';
163 strcpy(bp, path);
164
165 ptok = strtok(bp, "/");
166 if (ptok == NULL)
167 return utils_mkdir_safe(path, mask);
168
169 do
170 {
171 if (ptok != bp)
172 strcat(pt, "/");
173
174 strcat(pt, ptok);
175 res = utils_mkdir_safe(pt, mask);
176 if (res != 0)
177 return res;
178
179 }
180 while ((ptok = strtok(NULL, "/")) != NULL);
181
182 return 0;
183}
184
185/* Convert from system locale string to utf-8 */
186int
187utils_locale_to_utf8(const char *src, size_t is, char *dest, size_t os)
188{
189#ifdef HAVE_ICONV
190 static iconv_t *iconv_h = (iconv_t) - 1;
191 if (strncmp(g_codepage, "UTF-8", strlen("UTF-8")) == 0)
192 goto pass_trough_as_is;
193
194 if (g_iconv_works == False)
195 goto pass_trough_as_is;
196
197 /* if not already initialize */
198 if (iconv_h == (iconv_t) - 1)
199 {
200 if ((iconv_h = iconv_open("UTF-8", g_codepage)) == (iconv_t) - 1)
201 {
202 warning("utils_string_to_utf8: iconv_open[%s -> %s] fail %p\n",
203 g_codepage, "UTF-8", iconv_h);
204
205 g_iconv_works = False;
206 goto pass_trough_as_is;
207 }
208 }
209
210 /* convert string */
211 if (iconv(iconv_h, (ICONV_CONST char **) &src, &is, &dest, &os) == (size_t) - 1)
212 {
213 iconv_close(iconv_h);
214 iconv_h = (iconv_t) - 1;
215 warning("utils_string_to_utf8: iconv(1) fail, errno %d\n", errno);
216
217 g_iconv_works = False;
218 goto pass_trough_as_is;
219 }
220
221 /* Out couldn't hold the entire convertion */
222 if (is != 0)
223 return -1;
224
225#endif
226 pass_trough_as_is:
227 /* can dest hold strcpy of src */
228 if (os < (strlen(src) + 1))
229 return -1;
230
231 memcpy(dest, src, strlen(src) + 1);
232 return 0;
233}
Note: See TracBrowser for help on using the repository browser.

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