| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "config.h" |
| 16 |
#include "iconv.h" |
| 17 |
#include "iconvshim.h" |
| 18 |
#include <windows.h> |
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#ifndef ICONVDECL |
| 22 |
#define ICONVDECL |
| 23 |
#endif |
| 24 |
|
| 25 |
#define ICONVSHIM_VERSION "1.1.1" |
| 26 |
|
| 27 |
static FARPROC MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName); |
| 28 |
static int ishim_get_dll_name(char * filepath, int pathlen); |
| 29 |
static int ishim_set_dll_name(const char *filepath); |
| 30 |
static int load_dll(void); |
| 31 |
static void unload_dll(void); |
| 32 |
|
| 33 |
static HINSTANCE f_hinstDll=0; |
| 34 |
static int f_failed=0; |
| 35 |
static char f_dllpath[MAX_PATH]=""; |
| 36 |
static char * f_defaults[] = { "iconv.dll", "libiconv.dll" }; |
| 37 |
|
| 38 |
|
| 39 |
typedef iconv_t (*iconv_open_type)(const char* tocode, const char* fromcode); |
| 40 |
typedef size_t (*iconv_type)(iconv_t cd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); |
| 41 |
typedef int (*iconv_close_type)(iconv_t cd); |
| 42 |
typedef int (*iconvctl_type)(iconv_t cd, int request, void* argument); |
| 43 |
|
| 44 |
|
| 45 |
static struct iconv_fncs_s |
| 46 |
{ |
| 47 |
iconv_open_type iconv_open_x; |
| 48 |
iconv_type iconv_x; |
| 49 |
iconv_close_type iconv_close_x; |
| 50 |
iconvctl_type iconvctl_x; |
| 51 |
} f_iconv_fncs; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
ICONVDECL iconv_t |
| 57 |
iconv_open (const char* tocode, const char* fromcode) |
| 58 |
{ |
| 59 |
if (!load_dll() || !f_iconv_fncs.iconv_open_x) |
| 60 |
return (iconv_t)-1; |
| 61 |
return (*f_iconv_fncs.iconv_open_x)(tocode, fromcode); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
ICONVDECL size_t |
| 70 |
iconv (iconv_t cd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft) |
| 71 |
{ |
| 72 |
if (!load_dll() || !f_iconv_fncs.iconv_x) |
| 73 |
{ |
| 74 |
|
| 75 |
size_t max = *inbytesleft, len; |
| 76 |
if (max > *outbytesleft) |
| 77 |
max = *outbytesleft; |
| 78 |
lstrcpyn(*outbuf, *inbuf, max); |
| 79 |
len = lstrlen(*inbuf); |
| 80 |
*inbuf += len; |
| 81 |
*outbuf += len; |
| 82 |
*inbytesleft -= len; |
| 83 |
*outbytesleft -= len; |
| 84 |
return len; |
| 85 |
} |
| 86 |
return (*f_iconv_fncs.iconv_x)(cd, inbuf, inbytesleft, outbuf, outbytesleft); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
ICONVDECL int |
| 91 |
iconv_close (iconv_t cd) |
| 92 |
{ |
| 93 |
if (!load_dll() || !f_iconv_fncs.iconv_close_x) |
| 94 |
return 0; |
| 95 |
return (*f_iconv_fncs.iconv_close_x)(cd); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
ICONVDECL int |
| 100 |
iconvctl (iconv_t cd, int request, void* argument) |
| 101 |
{ |
| 102 |
if (!load_dll() || !f_iconv_fncs.iconvctl_x) |
| 103 |
return 0; |
| 104 |
return (*f_iconv_fncs.iconvctl_x)(cd, request, argument); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static void |
| 109 |
unload_dll (void) |
| 110 |
{ |
| 111 |
if (!f_hinstDll) |
| 112 |
return; |
| 113 |
memset(&f_iconv_fncs, 0, sizeof(f_iconv_fncs)); |
| 114 |
FreeLibrary(f_hinstDll); |
| 115 |
} |
| 116 |
|
| 117 |
static FARPROC |
| 118 |
MyGetProcAddress (HMODULE hModule, LPCSTR lpProcName) |
| 119 |
{ |
| 120 |
|
| 121 |
const char * prefix ="lib"; |
| 122 |
char buffer[256]; |
| 123 |
FARPROC proc = GetProcAddress(hModule, lpProcName); |
| 124 |
if (proc) |
| 125 |
return proc; |
| 126 |
if (lstrlen(lpProcName)+lstrlen(prefix)+1>sizeof(buffer)) |
| 127 |
return 0; |
| 128 |
lstrcpy(buffer, prefix); |
| 129 |
lstrcat(buffer, lpProcName); |
| 130 |
proc = GetProcAddress(hModule, buffer); |
| 131 |
if (proc) |
| 132 |
return proc; |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static int |
| 137 |
load_dll (void) |
| 138 |
{ |
| 139 |
if (f_hinstDll) |
| 140 |
return 1; |
| 141 |
if (f_failed) |
| 142 |
return 0; |
| 143 |
f_failed=1; |
| 144 |
|
| 145 |
memset(&f_iconv_fncs, 0, sizeof(f_iconv_fncs)); |
| 146 |
|
| 147 |
if (!f_dllpath[0]) |
| 148 |
{ |
| 149 |
|
| 150 |
int i; |
| 151 |
for (i=0; i<sizeof(f_defaults)/sizeof(f_defaults[0]); ++i) |
| 152 |
{ |
| 153 |
if ((f_hinstDll = LoadLibrary(f_defaults[i]))!=NULL) |
| 154 |
{ |
| 155 |
strncpy(f_dllpath, f_defaults[i], sizeof(f_dllpath)); |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
f_hinstDll = LoadLibrary(f_dllpath); |
| 163 |
} |
| 164 |
if (!f_hinstDll) |
| 165 |
return 0; |
| 166 |
|
| 167 |
f_iconv_fncs.iconv_open_x = (iconv_open_type)MyGetProcAddress(f_hinstDll, "iconv_open"); |
| 168 |
f_iconv_fncs.iconv_x = (iconv_type)MyGetProcAddress(f_hinstDll, "iconv"); |
| 169 |
f_iconv_fncs.iconv_close_x = (iconv_close_type)MyGetProcAddress(f_hinstDll, "iconv_close"); |
| 170 |
f_iconv_fncs.iconvctl_x = (iconvctl_type)MyGetProcAddress(f_hinstDll, "iconvctl"); |
| 171 |
|
| 172 |
f_failed=0; |
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
ICONVDECL int |
| 177 |
iconvshim_get_property (const char *name, char * value, int valuelen) |
| 178 |
{ |
| 179 |
static char file_version[] = "file_version:"; |
| 180 |
load_dll(); |
| 181 |
if (!lstrcmp(name, "dll_path")) |
| 182 |
return ishim_get_dll_name(value, valuelen); |
| 183 |
if (!lstrcmp(name, "dll_version")) |
| 184 |
{ |
| 185 |
char path[_MAX_PATH]; |
| 186 |
if (!ishim_get_dll_name(path, sizeof(path))) |
| 187 |
return 0; |
| 188 |
return ishim_get_file_version(path, value, valuelen); |
| 189 |
} |
| 190 |
if (!lstrcmp(name, "shim_version")) |
| 191 |
{ |
| 192 |
lstrcpyn(value, ICONVSHIM_VERSION, valuelen); |
| 193 |
return lstrlen(ICONVSHIM_VERSION); |
| 194 |
} |
| 195 |
if (!strncmp(name, file_version, strlen(file_version))) |
| 196 |
{ |
| 197 |
return ishim_get_file_version(name+strlen(file_version), value, valuelen); |
| 198 |
} |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
ICONVDECL int |
| 203 |
iconvshim_set_property (const char *name, const char *value) |
| 204 |
{ |
| 205 |
if (!strcmp(name, "dll_path")) |
| 206 |
{ |
| 207 |
return ishim_set_dll_name(value); |
| 208 |
} |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static int |
| 213 |
ishim_get_dll_name (char * filepath, int pathlen) |
| 214 |
{ |
| 215 |
if (!f_hinstDll) |
| 216 |
return 0; |
| 217 |
if (!GetModuleFileName(f_hinstDll, filepath, pathlen)) |
| 218 |
return 0; |
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
int |
| 223 |
ishim_get_file_version (const char * filepath, char * verout, int veroutlen) |
| 224 |
{ |
| 225 |
DWORD dwDummyHandle, len; |
| 226 |
BYTE * buf = 0; |
| 227 |
unsigned int verlen; |
| 228 |
LPVOID lpvi; |
| 229 |
VS_FIXEDFILEINFO fileInfo; |
| 230 |
|
| 231 |
if (!filepath || !filepath[0]) return 0; |
| 232 |
|
| 233 |
len = GetFileVersionInfoSize((char *)filepath, &dwDummyHandle); |
| 234 |
if (!len) return 0; |
| 235 |
buf = (BYTE *)malloc(len * sizeof(BYTE)); |
| 236 |
if (!buf) return 0; |
| 237 |
GetFileVersionInfo((char *)filepath, 0, len, buf); |
| 238 |
VerQueryValue(buf, "\\", &lpvi, &verlen); |
| 239 |
fileInfo = *(VS_FIXEDFILEINFO*)lpvi; |
| 240 |
_snprintf(verout, veroutlen, "FV:%d.%d.%d.%d, PV:%d.%d.%d.%d" |
| 241 |
, HIWORD(fileInfo.dwFileVersionMS) |
| 242 |
, LOWORD(fileInfo.dwFileVersionMS) |
| 243 |
, HIWORD(fileInfo.dwFileVersionLS) |
| 244 |
, LOWORD(fileInfo.dwFileVersionLS) |
| 245 |
, HIWORD(fileInfo.dwProductVersionMS) |
| 246 |
, LOWORD(fileInfo.dwProductVersionMS) |
| 247 |
, HIWORD(fileInfo.dwProductVersionLS) |
| 248 |
, LOWORD(fileInfo.dwProductVersionLS)); |
| 249 |
free(buf); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
static int |
| 254 |
ishim_set_dll_name (const char *filepath) |
| 255 |
{ |
| 256 |
if (!filepath) |
| 257 |
filepath = ""; |
| 258 |
lstrcpyn(f_dllpath, filepath, sizeof(f_dllpath)); |
| 259 |
|
| 260 |
unload_dll(); |
| 261 |
f_failed=0; |
| 262 |
return load_dll(); |
| 263 |
} |
| 264 |
|