VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/pathhost-posix.cpp@ 30111

Last change on this file since 30111 was 28916, checked in by vboxsync, 14 years ago

Don't use RTStrCopyEx with RTSTR_MAX.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1/* $Id: pathhost-posix.cpp 28916 2010-04-29 18:13:54Z vboxsync $ */
2/** @file
3 * IPRT - Path Convertions, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_PATH
32#include "internal/iprt.h"
33#include "internal/path.h"
34#include "internal/thread.h"
35
36#include <iprt/env.h>
37#include <iprt/string.h>
38#include <iprt/once.h>
39
40
41/*******************************************************************************
42* Global Variables *
43*******************************************************************************/
44/** Initialize once object. */
45static RTONCE g_OnceInitPathConv = RTONCE_INITIALIZER;
46/** If set, then we can pass UTF-8 thru directly. */
47static bool g_fPassthruUtf8 = false;
48/** The UTF-8 to FS iconv cache entry. */
49static RTSTRICONV g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
50/** The FS to UTF-8 iconv cache entry. */
51static RTSTRICONV g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
52/** The codeset we're using. */
53static char g_szFsCodeset[32];
54
55
56/**
57 * Do a case insensitive compare where the 2nd string is known and can be case
58 * folded when writing the code.
59 *
60 * @returns see strcmp.
61 * @param pszStr1 The string to compare against pszLower and
62 * pszUpper.
63 * @param pszUpper The upper case edition of the 2nd string.
64 * @param pszLower The lower case edition of the 2nd string.
65 */
66static int rtPathStrICmp(const char *pszStr1, const char *pszUpper, const char *pszLower)
67{
68 Assert(strlen(pszLower) == strlen(pszUpper));
69 for (;;)
70 {
71 char ch1 = *pszStr1++;
72 char ch2Upper = *pszUpper++;
73 char ch2Lower = *pszLower++;
74 if ( ch1 != ch2Upper
75 && ch1 != ch2Lower)
76 return ch1 < ch2Upper ? -1 : 1;
77 if (!ch1)
78 return 0;
79 }
80}
81
82/**
83 * Is the specified codeset something we can treat as UTF-8.
84 *
85 * @returns true if we can do UTF-8 passthru, false if not.
86 * @param pszCodeset The codeset in question.
87 */
88static bool rtPathConvInitIsUtf8(const char *pszCodeset)
89{
90 /* Paranoia. */
91 if (!pszCodeset)
92 return false;
93
94 /*
95 * Avoid RTStrICmp at this point.
96 */
97 static struct
98 {
99 const char *pszUpper;
100 const char *pszLower;
101 } const s_aUtf8Compatible[] =
102 {
103 /* The default locale. */
104 { "C" , "c" },
105 { "POSIX" , "posix" },
106 /* 7-bit ASCII. */
107 { "ANSI_X3.4-1968" , "ansi_x3.4-1968" },
108 { "ANSI_X3.4-1986" , "ansi_x3.4-1986" },
109 { "US-ASCII" , "us-ascii" },
110 { "ISO646-US" , "iso646-us" },
111 { "ISO_646.IRV:1991" , "iso_646.irv:1991" },
112 { "ISO-IR-6" , "iso-ir-6" },
113 { "IBM367" , "ibm367" },
114 /* UTF-8 */
115 { "UTF-8" , "utf-8" },
116 { "UTF8" , "utf8" },
117 { "ISO-10646/UTF-8" , "iso-10646/utf-8" },
118 { "ISO-10646/UTF8" , "iso-10646/utf8" }
119 };
120
121 for (size_t i = 0; i < RT_ELEMENTS(s_aUtf8Compatible); i++)
122 if (!rtPathStrICmp(pszCodeset, s_aUtf8Compatible[i].pszUpper, s_aUtf8Compatible[i].pszLower))
123 return true;
124
125 return false;
126}
127
128
129/**
130 * Init once for the path conversion code.
131 *
132 * @returns IPRT status code.
133 * @param pvUser1 Unused.
134 * @param pvUser2 Unused.
135 */
136static DECLCALLBACK(int32_t) rtPathConvInitOnce(void *pvUser1, void *pvUser2)
137{
138 /*
139 * Read the environment variable, no mercy on misconfigs here except that
140 * empty values are quietly ignored. (We use a temp buffer for stripping.)
141 */
142 char *pszEnvValue = NULL;
143 char szEnvValue[sizeof(g_szFsCodeset)];
144 int rc = RTEnvGetEx(RTENV_DEFAULT, RTPATH_CODESET_ENV_VAR, szEnvValue, sizeof(szEnvValue), NULL);
145 if (rc != VERR_ENV_VAR_NOT_FOUND && RT_FAILURE(rc))
146 return rc;
147 if (RT_SUCCESS(rc))
148 pszEnvValue = RTStrStrip(szEnvValue);
149
150 if (pszEnvValue && *pszEnvValue)
151 {
152 g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszEnvValue);
153 g_enmFsToUtf8Idx = RTSTRICONV_FS_TO_UTF8;
154 g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_FS;
155 strcpy(g_szFsCodeset, pszEnvValue);
156 }
157 else
158 {
159 const char *pszCodeset = rtStrGetLocaleCodeset();
160 size_t cchCodeset = pszCodeset ? strlen(pszCodeset) : sizeof(g_szFsCodeset);
161 if (cchCodeset >= sizeof(g_szFsCodeset))
162 /* This shouldn't happen, but we'll manage. */
163 g_szFsCodeset[0] = '\0';
164 else
165 {
166 memcpy(g_szFsCodeset, pszCodeset, cchCodeset + 1);
167 pszCodeset = g_szFsCodeset;
168 }
169 g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszCodeset);
170 g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
171 g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
172 }
173
174 NOREF(pvUser1); NOREF(pvUser2);
175 return VINF_SUCCESS;
176}
177
178
179int rtPathToNative(char const **ppszNativePath, const char *pszPath, const char *pszBasePath)
180{
181 *ppszNativePath = NULL;
182
183 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
184 if (RT_SUCCESS(rc))
185 {
186 if (g_fPassthruUtf8 || !*pszPath)
187 *ppszNativePath = pszPath;
188 else
189 rc = rtStrConvert(pszPath, strlen(pszPath), "UTF-8",
190 (char **)ppszNativePath, 0, g_szFsCodeset,
191 2, g_enmUtf8ToFsIdx);
192 }
193 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
194 return rc;
195}
196
197
198void rtPathFreeNative(char const *pszNativePath, const char *pszPath)
199{
200 if ( pszNativePath != pszPath
201 && pszNativePath)
202 RTStrFree((char *)pszNativePath);
203}
204
205
206int rtPathFromNative(const char **ppszPath, const char *pszNativePath, const char *pszBasePath)
207{
208 *ppszPath = NULL;
209
210 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
211 if (RT_SUCCESS(rc))
212 {
213 if (g_fPassthruUtf8 || !*pszNativePath)
214 {
215 size_t cCpsIgnored;
216 size_t cchNativePath;
217 rc = rtUtf8Length(pszNativePath, RTSTR_MAX, &cCpsIgnored, &cchNativePath);
218 if (RT_SUCCESS(rc))
219 {
220 char *pszPath;
221 *ppszPath = pszPath = RTStrAlloc(cchNativePath + 1);
222 if (pszPath)
223 memcpy(pszPath, pszNativePath, cchNativePath + 1);
224 else
225 rc = VERR_NO_STR_MEMORY;
226 }
227 }
228 else
229 rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
230 (char **)ppszPath, 0, "UTF-8",
231 2, g_enmFsToUtf8Idx);
232 }
233 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
234 return rc;
235}
236
237
238void rtPathFreeIprt(const char *pszPath, const char *pszNativePath)
239{
240 if ( pszPath != pszNativePath
241 && !pszPath)
242 RTStrFree((char *)pszPath);
243}
244
245
246int rtPathFromNativeCopy(char *pszPath, size_t cbPath, const char *pszNativePath, const char *pszBasePath)
247{
248 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
249 if (RT_SUCCESS(rc))
250 {
251 if (g_fPassthruUtf8 || !*pszNativePath)
252 rc = RTStrCopy(pszPath, cbPath, pszNativePath);
253 else if (cbPath)
254 rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
255 &pszPath, cbPath, "UTF-8",
256 2, g_enmFsToUtf8Idx);
257 else
258 rc = VERR_BUFFER_OVERFLOW;
259 }
260
261 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
262 return rc;
263}
264
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