4 * encoding.c - get DNS/Local encodings
6 * Software\JPNIC\IDN\Where
10 * \PerProg\<name>\Where
11 * \PerProg\<name>\Encoding
15 * Copyright (c) 2000,2001,2002 Japan Network Information Center.
16 * All rights reserved.
18 * By using this file, you agree to the terms and conditions set forth bellow.
20 * LICENSE TERMS AND CONDITIONS
22 * The following License Terms and Conditions apply, unless a different
23 * license is obtained from Japan Network Information Center ("JPNIC"),
24 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
25 * Chiyoda-ku, Tokyo 101-0047, Japan.
27 * 1. Use, Modification and Redistribution (including distribution of any
28 * modified or derived work) in source and/or binary forms is permitted
29 * under this License Terms and Conditions.
31 * 2. Redistribution of source code must retain the copyright notices as they
32 * appear in each source code file, this License Terms and Conditions.
34 * 3. Redistribution in binary form must reproduce the Copyright Notice,
35 * this License Terms and Conditions, in the documentation and/or other
36 * materials provided with the distribution. For the purposes of binary
37 * distribution the "Copyright Notice" refers to the following language:
38 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
40 * 4. The name of JPNIC may not be used to endorse or promote products
41 * derived from this Software without specific prior written approval of
44 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
45 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
51 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
52 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
53 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
54 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
63 #include "wrapcommon.h"
70 * Registry of Encodings
73 #define IDNKEY_WRAPPER "Software\\JPNIC\\IDN"
74 #define IDNKEY_PERPROG "Software\\JPNIC\\IDN\\PerProg"
75 #define IDNVAL_WHERE "Where"
76 #define IDNVAL_ENCODE "Encoding"
77 #define IDNVAL_LOGLVL "LogLevel"
78 #define IDNVAL_LOGFILE "LogFile"
79 #define IDNVAL_INSDIR "InstallDir"
81 static int GetRegistry(HKEY top
, const char *key
, const char *name
,
82 DWORD type
, void *param
, DWORD length
);
83 static char *GetPerProgKey(char *buf
, size_t len
);
84 static int GetFromRegistry(const char *name
, int where
, DWORD type
,
85 void *param
, DWORD length
);
86 static int GetIntFromRegistry(const char *name
, int defvalue
, int where
);
87 static BOOL
GetStringFromRegistry(const char *name
, char *result
,
88 size_t length
, int where
);
91 GetRegistry(HKEY top
, const char *key
, const char *name
, DWORD type
,
92 void *param
, DWORD length
)
98 stat
= RegOpenKeyEx(top
, key
, 0, KEY_READ
, &hk
);
99 if (stat
!= ERROR_SUCCESS
) {
103 stat
= RegQueryValueEx(hk
, (LPCTSTR
)name
, NULL
,
104 &realtype
, (LPBYTE
)param
, &length
);
108 if (stat
!= ERROR_SUCCESS
|| realtype
!= type
)
115 GetPerProgKey(char *buf
, size_t len
)
121 GetModuleFileName(NULL
, exename
, 256);
123 for (p
= exename
, last
= NULL
; *p
!= '\0'; p
++) {
124 if (*p
== '/' || *p
== '\\') {
128 strcpy(prgname
, (last
== NULL
) ? exename
: (last
+ 1));
129 if ((p
= strrchr(prgname
, '.')) != NULL
) {
133 if (strlen(IDNKEY_PERPROG
) + 1 + strlen(prgname
) >= len
) {
136 sprintf(buf
, "%s\\%s", IDNKEY_PERPROG
, prgname
);
141 GetFromRegistry(const char *name
, int where
, DWORD type
,
142 void *param
, DWORD length
)
144 if (where
& IDN_PERPROG
) {
146 * First, try program specific setting.
151 * Try HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE.
153 if (GetPerProgKey(keyname
, sizeof(keyname
)) != NULL
) {
154 if (((where
& IDN_CURUSER
) &&
155 GetRegistry(HKEY_CURRENT_USER
, keyname
, name
,
156 type
, param
, length
)) ||
157 GetRegistry(HKEY_LOCAL_MACHINE
, keyname
, name
,
158 type
, param
, length
)) {
164 if (where
& IDN_GLOBAL
) {
166 * Try global setting.
168 if (((where
& IDN_CURUSER
) &&
169 GetRegistry(HKEY_CURRENT_USER
, IDNKEY_WRAPPER
, name
,
170 type
, param
, length
)) ||
171 GetRegistry(HKEY_LOCAL_MACHINE
, IDNKEY_WRAPPER
, name
,
172 type
, param
, length
)) {
184 GetIntFromRegistry(const char *name
, int defvalue
, int where
)
188 if (GetFromRegistry(name
, where
, REG_DWORD
, ¶m
, sizeof(param
))) {
195 GetStringFromRegistry(const char *name
, char *result
, size_t length
, int where
)
197 if (GetFromRegistry(name
, where
, REG_SZ
, result
, (DWORD
)length
)) {
204 * idnEncodeWhere - which module should convert domain name
209 int v
= GetIntFromRegistry(IDNVAL_WHERE
, IDN_ENCODE_ALWAYS
,
210 IDN_GLOBAL
|IDN_PERPROG
|IDN_CURUSER
);
212 idnLogPrintf(idn_log_level_trace
, "idnEncodeWhere: %d\n", v
);
217 * idnGetLogFile - refer to log file
220 idnGetLogFile(char *file
, size_t len
)
222 BOOL v
= GetStringFromRegistry(IDNVAL_LOGFILE
, file
, len
,
223 IDN_GLOBAL
|IDN_CURUSER
);
225 idnLogPrintf(idn_log_level_trace
, "idnGetLogFile: %-.100s\n",
226 (v
== TRUE
) ? file
: "<none>");
231 * idnGetPrgEncoding - refer to Program's Local Encoding
233 * use program name as registry key
236 idnGetPrgEncoding(char *enc
, size_t len
)
238 if (GetStringFromRegistry(IDNVAL_ENCODE
, enc
, len
,
239 IDN_PERPROG
|IDN_CURUSER
) != TRUE
||
241 sprintf(enc
, "CP%d", GetACP());
243 idnLogPrintf(idn_log_level_trace
,
244 "idnGetPrgEncoding: %-.30s\n", enc
);
254 int v
= GetIntFromRegistry(IDNVAL_LOGLVL
, 0,
255 IDN_GLOBAL
|IDN_CURUSER
);
257 idnLogPrintf(idn_log_level_trace
, "idnGetLogLevel: %d\n", v
);
262 * idnGetInstallDir - get idn wrapper install directory
265 idnGetInstallDir(char *dir
, size_t len
)
267 /* No need to look at HKEY_CURRENT_USER */
268 BOOL v
= GetStringFromRegistry(IDNVAL_INSDIR
, dir
, len
, IDN_GLOBAL
);
270 idnLogPrintf(idn_log_level_trace
, "idnGetInstallDir: %-.100s\n",
271 (v
== TRUE
) ? dir
: "<none>");