| 1 |
/* |
| 2 |
* tclWin32Dll.c -- |
| 3 |
* |
| 4 |
* This file contains the DLL entry point and other low-level bit bashing |
| 5 |
* code that needs inline assembly. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1996 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1998-2000 Scriptics Corporation. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclWinInt.h" |
| 15 |
#if defined(HAVE_CPUID_H) |
| 16 |
# include <cpuid.h> |
| 17 |
#elif defined(HAVE_INTRIN_H) |
| 18 |
# include <intrin.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
/* |
| 22 |
* The following variables keep track of information about this DLL on a |
| 23 |
* per-instance basis. Each time this DLL is loaded, it gets its own new data |
| 24 |
* segment with its own copy of all static and global information. |
| 25 |
*/ |
| 26 |
|
| 27 |
static HINSTANCE hInstance; /* HINSTANCE of this DLL. */ |
| 28 |
|
| 29 |
#if defined(__GNUC__) |
| 30 |
|
| 31 |
/* |
| 32 |
* Need to add noinline flag to DllMain declaration so that gcc -O3 does not |
| 33 |
* inline asm code into DllEntryPoint and cause a compile time error because |
| 34 |
* of redefined local labels. |
| 35 |
*/ |
| 36 |
|
| 37 |
BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason, |
| 38 |
LPVOID reserved) __attribute__ ((noinline)); |
| 39 |
|
| 40 |
#else /* !__GNUC__ */ |
| 41 |
|
| 42 |
/* |
| 43 |
* The following declaration is for the VC++ DLL entry point. |
| 44 |
*/ |
| 45 |
|
| 46 |
BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason, |
| 47 |
LPVOID reserved); |
| 48 |
|
| 49 |
/* |
| 50 |
* VC++ 5.x has no 'cpuid' assembler instruction, so we must emulate it |
| 51 |
*/ |
| 52 |
|
| 53 |
#if defined(_MSC_VER) && (_MSC_VER <= 1100) && defined (_M_IX86) |
| 54 |
#define cpuid __asm __emit 0fh __asm __emit 0a2h |
| 55 |
#endif |
| 56 |
|
| 57 |
#endif /* __GNUC__ */ |
| 58 |
|
| 59 |
/* |
| 60 |
* The following structure and linked list is to allow us to map between |
| 61 |
* volume mount points and drive letters on the fly (no Win API exists for |
| 62 |
* this). |
| 63 |
*/ |
| 64 |
|
| 65 |
typedef struct MountPointMap { |
| 66 |
WCHAR *volumeName; /* Native wide string volume name. */ |
| 67 |
WCHAR driveLetter; /* Drive letter corresponding to the volume |
| 68 |
* name. */ |
| 69 |
struct MountPointMap *nextPtr; |
| 70 |
/* Pointer to next structure in list, or |
| 71 |
* NULL. */ |
| 72 |
} MountPointMap; |
| 73 |
|
| 74 |
/* |
| 75 |
* This is the head of the linked list, which is protected by the mutex which |
| 76 |
* follows, for thread-enabled builds. |
| 77 |
*/ |
| 78 |
|
| 79 |
MountPointMap *driveLetterLookup = NULL; |
| 80 |
TCL_DECLARE_MUTEX(mountPointMap) |
| 81 |
|
| 82 |
/* |
| 83 |
* We will need this below. |
| 84 |
*/ |
| 85 |
|
| 86 |
#ifdef _WIN32 |
| 87 |
#ifndef STATIC_BUILD |
| 88 |
|
| 89 |
/* |
| 90 |
*---------------------------------------------------------------------- |
| 91 |
* |
| 92 |
* DllEntryPoint -- |
| 93 |
* |
| 94 |
* This wrapper function is used by Borland to invoke the initialization |
| 95 |
* code for Tcl. It simply calls the DllMain routine. |
| 96 |
* |
| 97 |
* Results: |
| 98 |
* See DllMain. |
| 99 |
* |
| 100 |
* Side effects: |
| 101 |
* See DllMain. |
| 102 |
* |
| 103 |
*---------------------------------------------------------------------- |
| 104 |
*/ |
| 105 |
|
| 106 |
BOOL APIENTRY |
| 107 |
DllEntryPoint( |
| 108 |
HINSTANCE hInst, /* Library instance handle. */ |
| 109 |
DWORD reason, /* Reason this function is being called. */ |
| 110 |
LPVOID reserved) |
| 111 |
{ |
| 112 |
return DllMain(hInst, reason, reserved); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
*---------------------------------------------------------------------- |
| 117 |
* |
| 118 |
* DllMain -- |
| 119 |
* |
| 120 |
* This routine is called by the VC++ C run time library init code, or |
| 121 |
* the DllEntryPoint routine. It is responsible for initializing various |
| 122 |
* dynamically loaded libraries. |
| 123 |
* |
| 124 |
* Results: |
| 125 |
* TRUE on sucess, FALSE on failure. |
| 126 |
* |
| 127 |
* Side effects: |
| 128 |
* Initializes most rudimentary Windows bits. |
| 129 |
* |
| 130 |
*---------------------------------------------------------------------- |
| 131 |
*/ |
| 132 |
|
| 133 |
BOOL APIENTRY |
| 134 |
DllMain( |
| 135 |
HINSTANCE hInst, /* Library instance handle. */ |
| 136 |
DWORD reason, /* Reason this function is being called. */ |
| 137 |
LPVOID reserved) /* Not used. */ |
| 138 |
{ |
| 139 |
(void)reserved; |
| 140 |
|
| 141 |
switch (reason) { |
| 142 |
case DLL_PROCESS_ATTACH: |
| 143 |
DisableThreadLibraryCalls(hInst); |
| 144 |
TclWinInit(hInst); |
| 145 |
return TRUE; |
| 146 |
|
| 147 |
/* |
| 148 |
* DLL_PROCESS_DETACH is unnecessary as the user should call |
| 149 |
* Tcl_Finalize explicitly before unloading Tcl. |
| 150 |
*/ |
| 151 |
} |
| 152 |
|
| 153 |
return TRUE; |
| 154 |
} |
| 155 |
#endif /* !STATIC_BUILD */ |
| 156 |
#endif /* _WIN32 */ |
| 157 |
|
| 158 |
/* |
| 159 |
*---------------------------------------------------------------------- |
| 160 |
* |
| 161 |
* TclWinGetTclInstance -- |
| 162 |
* |
| 163 |
* Retrieves the global library instance handle. |
| 164 |
* |
| 165 |
* Results: |
| 166 |
* Returns the global library instance handle. |
| 167 |
* |
| 168 |
* Side effects: |
| 169 |
* None. |
| 170 |
* |
| 171 |
*---------------------------------------------------------------------- |
| 172 |
*/ |
| 173 |
|
| 174 |
HINSTANCE |
| 175 |
TclWinGetTclInstance(void) |
| 176 |
{ |
| 177 |
return hInstance; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
*---------------------------------------------------------------------- |
| 182 |
* |
| 183 |
* TclWinInit -- |
| 184 |
* |
| 185 |
* This function initializes the internal state of the tcl library. |
| 186 |
* |
| 187 |
* Results: |
| 188 |
* None. |
| 189 |
* |
| 190 |
* Side effects: |
| 191 |
* Initializes the tclPlatformId variable. |
| 192 |
* |
| 193 |
*---------------------------------------------------------------------- |
| 194 |
*/ |
| 195 |
|
| 196 |
void |
| 197 |
TclWinInit( |
| 198 |
HINSTANCE hInst) /* Library instance handle. */ |
| 199 |
{ |
| 200 |
OSVERSIONINFOW os; |
| 201 |
|
| 202 |
hInstance = hInst; |
| 203 |
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); |
| 204 |
GetVersionExW(&os); |
| 205 |
|
| 206 |
/* |
| 207 |
* We no longer support Win32s or Win9x or Windows CE, so just in case |
| 208 |
* someone manages to get a runtime there, make sure they know that. |
| 209 |
*/ |
| 210 |
|
| 211 |
if (os.dwPlatformId != VER_PLATFORM_WIN32_NT) { |
| 212 |
Tcl_Panic("Windows NT is the only supported platform"); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
*---------------------------------------------------------------------- |
| 218 |
* |
| 219 |
* TclWinGetPlatformId -- |
| 220 |
* |
| 221 |
* Determines whether running under NT, 95, or Win32s, to allow runtime |
| 222 |
* conditional code. |
| 223 |
* |
| 224 |
* Results: |
| 225 |
* The return value is always: |
| 226 |
* VER_PLATFORM_WIN32_NT Win32 on Windows NT, 2000, XP |
| 227 |
* |
| 228 |
* Side effects: |
| 229 |
* None. |
| 230 |
* |
| 231 |
*---------------------------------------------------------------------- |
| 232 |
*/ |
| 233 |
|
| 234 |
int |
| 235 |
TclWinGetPlatformId(void) |
| 236 |
{ |
| 237 |
return VER_PLATFORM_WIN32_NT; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
*------------------------------------------------------------------------- |
| 242 |
* |
| 243 |
* TclWinNoBackslash -- |
| 244 |
* |
| 245 |
* We're always iterating through a string in Windows, changing the |
| 246 |
* backslashes to slashes for use in Tcl. |
| 247 |
* |
| 248 |
* Results: |
| 249 |
* All backslashes in given string are changed to slashes. |
| 250 |
* |
| 251 |
* Side effects: |
| 252 |
* None. |
| 253 |
* |
| 254 |
*------------------------------------------------------------------------- |
| 255 |
*/ |
| 256 |
|
| 257 |
char * |
| 258 |
TclWinNoBackslash( |
| 259 |
char *path) /* String to change. */ |
| 260 |
{ |
| 261 |
char *p; |
| 262 |
|
| 263 |
for (p = path; *p != '\0'; p++) { |
| 264 |
if (*p == '\\') { |
| 265 |
*p = '/'; |
| 266 |
} |
| 267 |
} |
| 268 |
return path; |
| 269 |
} |
| 270 |
|
| 271 |
/* |
| 272 |
*--------------------------------------------------------------------------- |
| 273 |
* |
| 274 |
* TclWinEncodingsCleanup -- |
| 275 |
* |
| 276 |
* Called during finalization to clean up any memory allocated in our |
| 277 |
* mount point map which is used to follow certain kinds of symlinks. |
| 278 |
* |
| 279 |
* Results: |
| 280 |
* None. |
| 281 |
* |
| 282 |
* Side effects: |
| 283 |
* None. |
| 284 |
* |
| 285 |
*--------------------------------------------------------------------------- |
| 286 |
*/ |
| 287 |
|
| 288 |
void |
| 289 |
TclWinEncodingsCleanup(void) |
| 290 |
{ |
| 291 |
MountPointMap *dlIter, *dlIter2; |
| 292 |
|
| 293 |
/* |
| 294 |
* Clean up the mount point map. |
| 295 |
*/ |
| 296 |
|
| 297 |
Tcl_MutexLock(&mountPointMap); |
| 298 |
dlIter = driveLetterLookup; |
| 299 |
while (dlIter != NULL) { |
| 300 |
dlIter2 = dlIter->nextPtr; |
| 301 |
ckfree(dlIter->volumeName); |
| 302 |
ckfree(dlIter); |
| 303 |
dlIter = dlIter2; |
| 304 |
} |
| 305 |
Tcl_MutexUnlock(&mountPointMap); |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
*--------------------------------------------------------------------------- |
| 310 |
* |
| 311 |
* TclWinResetInterfaces -- |
| 312 |
* |
| 313 |
* Called during finalization to reset us to a safe state for reuse. |
| 314 |
* |
| 315 |
* Results: |
| 316 |
* None. |
| 317 |
* |
| 318 |
* Side effects: |
| 319 |
* None. |
| 320 |
* |
| 321 |
*--------------------------------------------------------------------------- |
| 322 |
*/ |
| 323 |
void |
| 324 |
TclWinResetInterfaces(void) |
| 325 |
{ |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
*-------------------------------------------------------------------- |
| 330 |
* |
| 331 |
* TclWinDriveLetterForVolMountPoint |
| 332 |
* |
| 333 |
* Unfortunately, Windows provides no easy way at all to get hold of the |
| 334 |
* drive letter for a volume mount point, but we need that information to |
| 335 |
* understand paths correctly. So, we have to build an associated array |
| 336 |
* to find these correctly, and allow quick and easy lookup from volume |
| 337 |
* mount points to drive letters. |
| 338 |
* |
| 339 |
* We assume here that we are running on a system for which the wide |
| 340 |
* character interfaces are used, which is valid for Win 2000 and WinXP |
| 341 |
* which are the only systems on which this function will ever be called. |
| 342 |
* |
| 343 |
* Result: |
| 344 |
* The drive letter, or -1 if no drive letter corresponds to the given |
| 345 |
* mount point. |
| 346 |
* |
| 347 |
*-------------------------------------------------------------------- |
| 348 |
*/ |
| 349 |
|
| 350 |
char |
| 351 |
TclWinDriveLetterForVolMountPoint( |
| 352 |
const WCHAR *mountPoint) |
| 353 |
{ |
| 354 |
MountPointMap *dlIter, *dlPtr2; |
| 355 |
WCHAR Target[55]; /* Target of mount at mount point */ |
| 356 |
WCHAR drive[4] = L"A:\\"; |
| 357 |
|
| 358 |
/* |
| 359 |
* Detect the volume mounted there. Unfortunately, there is no simple way |
| 360 |
* to map a unique volume name to a DOS drive letter. So, we have to build |
| 361 |
* an associative array. |
| 362 |
*/ |
| 363 |
|
| 364 |
Tcl_MutexLock(&mountPointMap); |
| 365 |
dlIter = driveLetterLookup; |
| 366 |
while (dlIter != NULL) { |
| 367 |
if (wcscmp(dlIter->volumeName, mountPoint) == 0) { |
| 368 |
/* |
| 369 |
* We need to check whether this information is still valid, since |
| 370 |
* either the user or various programs could have adjusted the |
| 371 |
* mount points on the fly. |
| 372 |
*/ |
| 373 |
|
| 374 |
drive[0] = (WCHAR) dlIter->driveLetter; |
| 375 |
|
| 376 |
/* |
| 377 |
* Try to read the volume mount point and see where it points. |
| 378 |
*/ |
| 379 |
|
| 380 |
if (GetVolumeNameForVolumeMountPointW(drive, |
| 381 |
Target, 55) != 0) { |
| 382 |
if (wcscmp(dlIter->volumeName, Target) == 0) { |
| 383 |
/* |
| 384 |
* Nothing has changed. |
| 385 |
*/ |
| 386 |
|
| 387 |
Tcl_MutexUnlock(&mountPointMap); |
| 388 |
return (char) dlIter->driveLetter; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/* |
| 393 |
* If we reach here, unfortunately, this mount point is no longer |
| 394 |
* valid at all. |
| 395 |
*/ |
| 396 |
|
| 397 |
if (driveLetterLookup == dlIter) { |
| 398 |
dlPtr2 = dlIter; |
| 399 |
driveLetterLookup = dlIter->nextPtr; |
| 400 |
} else { |
| 401 |
for (dlPtr2 = driveLetterLookup; |
| 402 |
dlPtr2 != NULL; dlPtr2 = dlPtr2->nextPtr) { |
| 403 |
if (dlPtr2->nextPtr == dlIter) { |
| 404 |
dlPtr2->nextPtr = dlIter->nextPtr; |
| 405 |
dlPtr2 = dlIter; |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/* |
| 412 |
* Now dlPtr2 points to the structure to free. |
| 413 |
*/ |
| 414 |
|
| 415 |
ckfree(dlPtr2->volumeName); |
| 416 |
ckfree(dlPtr2); |
| 417 |
|
| 418 |
/* |
| 419 |
* Restart the loop - we could try to be clever and continue half |
| 420 |
* way through, but the logic is a bit messy, so it's cleanest |
| 421 |
* just to restart. |
| 422 |
*/ |
| 423 |
|
| 424 |
dlIter = driveLetterLookup; |
| 425 |
continue; |
| 426 |
} |
| 427 |
dlIter = dlIter->nextPtr; |
| 428 |
} |
| 429 |
|
| 430 |
/* |
| 431 |
* We couldn't find it, so we must iterate over the letters. |
| 432 |
*/ |
| 433 |
|
| 434 |
for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++) { |
| 435 |
/* |
| 436 |
* Try to read the volume mount point and see where it points. |
| 437 |
*/ |
| 438 |
|
| 439 |
if (GetVolumeNameForVolumeMountPointW(drive, |
| 440 |
Target, 55) != 0) { |
| 441 |
int alreadyStored = 0; |
| 442 |
|
| 443 |
for (dlIter = driveLetterLookup; dlIter != NULL; |
| 444 |
dlIter = dlIter->nextPtr) { |
| 445 |
if (wcscmp(dlIter->volumeName, Target) == 0) { |
| 446 |
alreadyStored = 1; |
| 447 |
break; |
| 448 |
} |
| 449 |
} |
| 450 |
if (!alreadyStored) { |
| 451 |
dlPtr2 = (MountPointMap *)ckalloc(sizeof(MountPointMap)); |
| 452 |
dlPtr2->volumeName = (WCHAR *)TclNativeDupInternalRep(Target); |
| 453 |
dlPtr2->driveLetter = (WCHAR) drive[0]; |
| 454 |
dlPtr2->nextPtr = driveLetterLookup; |
| 455 |
driveLetterLookup = dlPtr2; |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
* Try again. |
| 462 |
*/ |
| 463 |
|
| 464 |
for (dlIter = driveLetterLookup; dlIter != NULL; |
| 465 |
dlIter = dlIter->nextPtr) { |
| 466 |
if (wcscmp(dlIter->volumeName, mountPoint) == 0) { |
| 467 |
Tcl_MutexUnlock(&mountPointMap); |
| 468 |
return (char) dlIter->driveLetter; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* |
| 473 |
* The volume doesn't appear to correspond to a drive letter - we remember |
| 474 |
* that fact and store '-1' so we don't have to look it up each time. |
| 475 |
*/ |
| 476 |
|
| 477 |
dlPtr2 = (MountPointMap *)ckalloc(sizeof(MountPointMap)); |
| 478 |
dlPtr2->volumeName = (WCHAR *)TclNativeDupInternalRep((void *)mountPoint); |
| 479 |
dlPtr2->driveLetter = (WCHAR)-1; |
| 480 |
dlPtr2->nextPtr = driveLetterLookup; |
| 481 |
driveLetterLookup = dlPtr2; |
| 482 |
Tcl_MutexUnlock(&mountPointMap); |
| 483 |
return -1; |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
*--------------------------------------------------------------------------- |
| 488 |
* |
| 489 |
* Tcl_WinUtfToTChar, Tcl_WinTCharToUtf -- |
| 490 |
* |
| 491 |
* Convert between UTF-8 and Unicode when running Windows. |
| 492 |
* |
| 493 |
* On Mac and Unix, all strings exchanged between Tcl and the OS are |
| 494 |
* "char" oriented. We need only one Tcl_Encoding to convert between |
| 495 |
* UTF-8 and the system's native encoding. We use NULL to represent |
| 496 |
* that encoding. |
| 497 |
* |
| 498 |
* On Windows, some strings exchanged between Tcl and the OS are "char" |
| 499 |
* oriented, while others are in Unicode. We need two Tcl_Encoding APIs |
| 500 |
* depending on whether we are targeting a "char" or Unicode interface. |
| 501 |
* |
| 502 |
* Calling Tcl_UtfToExternal() or Tcl_ExternalToUtf() with an encoding |
| 503 |
* of NULL should always used to convert between UTF-8 and the system's |
| 504 |
* "char" oriented encoding. The following two functions are used in |
| 505 |
* Windows-specific code to convert between UTF-8 and Unicode strings. |
| 506 |
* This saves you the trouble of writing the |
| 507 |
* following type of fragment over and over: |
| 508 |
* |
| 509 |
* encoding <- Tcl_GetEncoding("unicode"); |
| 510 |
* nativeBuffer <- UtfToExternal(encoding, utfBuffer); |
| 511 |
* Tcl_FreeEncoding(encoding); |
| 512 |
* |
| 513 |
* By convention, in Windows a WCHAR is a Unicode character. If you plan |
| 514 |
* on targeting a Unicode interface when running on Windows, these |
| 515 |
* functions should be used. If you plan on targetting a "char" oriented |
| 516 |
* function on Windows, use Tcl_UtfToExternal() with an encoding of NULL. |
| 517 |
* |
| 518 |
* Results: |
| 519 |
* The result is a pointer to the string in the desired target encoding. |
| 520 |
* Storage for the result string is allocated in dsPtr; the caller must |
| 521 |
* call Tcl_DStringFree() when the result is no longer needed. |
| 522 |
* |
| 523 |
* Side effects: |
| 524 |
* None. |
| 525 |
* |
| 526 |
*--------------------------------------------------------------------------- |
| 527 |
*/ |
| 528 |
|
| 529 |
TCHAR * |
| 530 |
Tcl_WinUtfToTChar( |
| 531 |
const char *string, /* Source string in UTF-8. */ |
| 532 |
int len, /* Source string length in bytes, or -1 for |
| 533 |
* strlen(). */ |
| 534 |
Tcl_DString *dsPtr) /* Uninitialized or free DString in which the |
| 535 |
* converted string is stored. */ |
| 536 |
{ |
| 537 |
#if TCL_UTF_MAX > 4 |
| 538 |
Tcl_UniChar ch = 0; |
| 539 |
TCHAR *w, *wString; |
| 540 |
const char *p, *end; |
| 541 |
int oldLength; |
| 542 |
#endif |
| 543 |
|
| 544 |
Tcl_DStringInit(dsPtr); |
| 545 |
if (!string) { |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
#if TCL_UTF_MAX > 4 |
| 549 |
|
| 550 |
if (len < 0) { |
| 551 |
len = strlen(string); |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
* Unicode string length in Tcl_UniChars will be <= UTF-8 string length in |
| 556 |
* bytes. |
| 557 |
*/ |
| 558 |
|
| 559 |
oldLength = Tcl_DStringLength(dsPtr); |
| 560 |
|
| 561 |
Tcl_DStringSetLength(dsPtr, |
| 562 |
oldLength + (int) ((len + 1) * sizeof(TCHAR))); |
| 563 |
wString = (TCHAR *) (Tcl_DStringValue(dsPtr) + oldLength); |
| 564 |
|
| 565 |
w = wString; |
| 566 |
p = string; |
| 567 |
end = string + len - 4; |
| 568 |
while (p < end) { |
| 569 |
p += TclUtfToUniChar(p, &ch); |
| 570 |
if (ch > 0xFFFF) { |
| 571 |
*w++ = (WCHAR) (0xD800 + ((ch -= 0x10000) >> 10)); |
| 572 |
*w++ = (WCHAR) (0xDC00 | (ch & 0x3FF)); |
| 573 |
} else { |
| 574 |
*w++ = ch; |
| 575 |
} |
| 576 |
} |
| 577 |
end += 4; |
| 578 |
while (p < end) { |
| 579 |
if (Tcl_UtfCharComplete(p, end-p)) { |
| 580 |
p += TclUtfToUniChar(p, &ch); |
| 581 |
} else { |
| 582 |
ch = UCHAR(*p++); |
| 583 |
} |
| 584 |
if (ch > 0xFFFF) { |
| 585 |
*w++ = (WCHAR) (0xD800 + ((ch -= 0x10000) >> 10)); |
| 586 |
*w++ = (WCHAR) (0xDC00 | (ch & 0x3FF)); |
| 587 |
} else { |
| 588 |
*w++ = ch; |
| 589 |
} |
| 590 |
} |
| 591 |
*w = '\0'; |
| 592 |
Tcl_DStringSetLength(dsPtr, |
| 593 |
oldLength + ((char *) w - (char *) wString)); |
| 594 |
|
| 595 |
return wString; |
| 596 |
#else |
| 597 |
return (TCHAR *)Tcl_UtfToUniCharDString(string, len, dsPtr); |
| 598 |
#endif |
| 599 |
} |
| 600 |
|
| 601 |
char * |
| 602 |
Tcl_WinTCharToUtf( |
| 603 |
const TCHAR *string, /* Source string in Unicode. */ |
| 604 |
int len, /* Source string length in bytes, or -1 for |
| 605 |
* platform-specific string length. */ |
| 606 |
Tcl_DString *dsPtr) /* Uninitialized or free DString in which the |
| 607 |
* converted string is stored. */ |
| 608 |
{ |
| 609 |
#if TCL_UTF_MAX > 4 |
| 610 |
const WCHAR *w, *wEnd; |
| 611 |
char *p, *result; |
| 612 |
int oldLength, blen = 1; |
| 613 |
#endif |
| 614 |
|
| 615 |
Tcl_DStringInit(dsPtr); |
| 616 |
if (!string) { |
| 617 |
return NULL; |
| 618 |
} |
| 619 |
if (len < 0) { |
| 620 |
len = (int)wcslen((WCHAR *)string); |
| 621 |
} else { |
| 622 |
len /= 2; |
| 623 |
} |
| 624 |
#if TCL_UTF_MAX > 4 |
| 625 |
oldLength = Tcl_DStringLength(dsPtr); |
| 626 |
Tcl_DStringSetLength(dsPtr, oldLength + (len + 1) * 4); |
| 627 |
result = Tcl_DStringValue(dsPtr) + oldLength; |
| 628 |
|
| 629 |
p = result; |
| 630 |
wEnd = (WCHAR *)string + len; |
| 631 |
for (w = (WCHAR *)string; w < wEnd; ) { |
| 632 |
if (!blen && ((*w & 0xFC00) != 0xDC00)) { |
| 633 |
/* Special case for handling high surrogates. */ |
| 634 |
p += Tcl_UniCharToUtf(-1, p); |
| 635 |
} |
| 636 |
blen = Tcl_UniCharToUtf(*w, p); |
| 637 |
p += blen; |
| 638 |
if ((*w >= 0xD800) && (blen < 3)) { |
| 639 |
/* Indication that high surrogate is handled */ |
| 640 |
blen = 0; |
| 641 |
} |
| 642 |
w++; |
| 643 |
} |
| 644 |
if (!blen) { |
| 645 |
/* Special case for handling high surrogates. */ |
| 646 |
p += Tcl_UniCharToUtf(-1, p); |
| 647 |
} |
| 648 |
Tcl_DStringSetLength(dsPtr, oldLength + (p - result)); |
| 649 |
|
| 650 |
return result; |
| 651 |
#else |
| 652 |
return Tcl_UniCharToUtfDString((Tcl_UniChar *)string, len, dsPtr); |
| 653 |
#endif |
| 654 |
} |
| 655 |
|
| 656 |
/* |
| 657 |
*------------------------------------------------------------------------ |
| 658 |
* |
| 659 |
* TclWinCPUID -- |
| 660 |
* |
| 661 |
* Get CPU ID information on an Intel box under Windows |
| 662 |
* |
| 663 |
* Results: |
| 664 |
* Returns TCL_OK if successful, TCL_ERROR if CPUID is not supported or |
| 665 |
* fails. |
| 666 |
* |
| 667 |
* Side effects: |
| 668 |
* If successful, stores EAX, EBX, ECX and EDX registers after the CPUID |
| 669 |
* instruction in the four integers designated by 'regsPtr' |
| 670 |
* |
| 671 |
*---------------------------------------------------------------------- |
| 672 |
*/ |
| 673 |
|
| 674 |
int |
| 675 |
TclWinCPUID( |
| 676 |
unsigned int index, /* Which CPUID value to retrieve. */ |
| 677 |
unsigned int *regsPtr) /* Registers after the CPUID. */ |
| 678 |
{ |
| 679 |
int status = TCL_ERROR; |
| 680 |
|
| 681 |
#if defined(HAVE_CPUID_H) |
| 682 |
|
| 683 |
__get_cpuid(index, ®sPtr[0], ®sPtr[1], ®sPtr[2], ®sPtr[3]); |
| 684 |
status = TCL_OK; |
| 685 |
|
| 686 |
#elif defined(HAVE_INTRIN_H) && defined(_WIN64) && defined(HAVE_CPUID) |
| 687 |
|
| 688 |
__cpuid((int *)regsPtr, (int)index); |
| 689 |
status = TCL_OK; |
| 690 |
|
| 691 |
#elif defined(__GNUC__) && defined(HAVE_CPUID) |
| 692 |
# if defined(_WIN64) |
| 693 |
/* |
| 694 |
* Execute the CPUID instruction with the given index, and store results |
| 695 |
* off 'regPtr'. |
| 696 |
*/ |
| 697 |
|
| 698 |
__asm__ __volatile__( |
| 699 |
/* |
| 700 |
* Do the CPUID instruction, and save the results in the 'regsPtr' |
| 701 |
* area. |
| 702 |
*/ |
| 703 |
|
| 704 |
"movl %[rptr], %%edi" "\n\t" |
| 705 |
"movl %[index], %%eax" "\n\t" |
| 706 |
"cpuid" "\n\t" |
| 707 |
"movl %%eax, 0x0(%%edi)" "\n\t" |
| 708 |
"movl %%ebx, 0x4(%%edi)" "\n\t" |
| 709 |
"movl %%ecx, 0x8(%%edi)" "\n\t" |
| 710 |
"movl %%edx, 0xC(%%edi)" "\n\t" |
| 711 |
|
| 712 |
: |
| 713 |
/* No outputs */ |
| 714 |
: |
| 715 |
[index] "m" (index), |
| 716 |
[rptr] "m" (regsPtr) |
| 717 |
: |
| 718 |
"%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); |
| 719 |
status = TCL_OK; |
| 720 |
|
| 721 |
# else |
| 722 |
|
| 723 |
TCLEXCEPTION_REGISTRATION registration; |
| 724 |
|
| 725 |
/* |
| 726 |
* Execute the CPUID instruction with the given index, and store results |
| 727 |
* off 'regPtr'. |
| 728 |
*/ |
| 729 |
|
| 730 |
__asm__ __volatile__( |
| 731 |
/* |
| 732 |
* Construct an TCLEXCEPTION_REGISTRATION to protect the CPUID |
| 733 |
* instruction (early 486's don't have CPUID) |
| 734 |
*/ |
| 735 |
|
| 736 |
"leal %[registration], %%edx" "\n\t" |
| 737 |
"movl %%fs:0, %%eax" "\n\t" |
| 738 |
"movl %%eax, 0x0(%%edx)" "\n\t" /* link */ |
| 739 |
"leal 1f, %%eax" "\n\t" |
| 740 |
"movl %%eax, 0x4(%%edx)" "\n\t" /* handler */ |
| 741 |
"movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */ |
| 742 |
"movl %%esp, 0xC(%%edx)" "\n\t" /* esp */ |
| 743 |
"movl %[error], 0x10(%%edx)" "\n\t" /* status */ |
| 744 |
|
| 745 |
/* |
| 746 |
* Link the TCLEXCEPTION_REGISTRATION on the chain |
| 747 |
*/ |
| 748 |
|
| 749 |
"movl %%edx, %%fs:0" "\n\t" |
| 750 |
|
| 751 |
/* |
| 752 |
* Do the CPUID instruction, and save the results in the 'regsPtr' |
| 753 |
* area. |
| 754 |
*/ |
| 755 |
|
| 756 |
"movl %[rptr], %%edi" "\n\t" |
| 757 |
"movl %[index], %%eax" "\n\t" |
| 758 |
"cpuid" "\n\t" |
| 759 |
"movl %%eax, 0x0(%%edi)" "\n\t" |
| 760 |
"movl %%ebx, 0x4(%%edi)" "\n\t" |
| 761 |
"movl %%ecx, 0x8(%%edi)" "\n\t" |
| 762 |
"movl %%edx, 0xC(%%edi)" "\n\t" |
| 763 |
|
| 764 |
/* |
| 765 |
* Come here on a normal exit. Recover the TCLEXCEPTION_REGISTRATION and |
| 766 |
* store a TCL_OK status. |
| 767 |
*/ |
| 768 |
|
| 769 |
"movl %%fs:0, %%edx" "\n\t" |
| 770 |
"movl %[ok], %%eax" "\n\t" |
| 771 |
"movl %%eax, 0x10(%%edx)" "\n\t" |
| 772 |
"jmp 2f" "\n" |
| 773 |
|
| 774 |
/* |
| 775 |
* Come here on an exception. Get the TCLEXCEPTION_REGISTRATION that we |
| 776 |
* previously put on the chain. |
| 777 |
*/ |
| 778 |
|
| 779 |
"1:" "\t" |
| 780 |
"movl %%fs:0, %%edx" "\n\t" |
| 781 |
"movl 0x8(%%edx), %%edx" "\n\t" |
| 782 |
|
| 783 |
/* |
| 784 |
* Come here however we exited. Restore context from the |
| 785 |
* TCLEXCEPTION_REGISTRATION in case the stack is unbalanced. |
| 786 |
*/ |
| 787 |
|
| 788 |
"2:" "\t" |
| 789 |
"movl 0xC(%%edx), %%esp" "\n\t" |
| 790 |
"movl 0x8(%%edx), %%ebp" "\n\t" |
| 791 |
"movl 0x0(%%edx), %%eax" "\n\t" |
| 792 |
"movl %%eax, %%fs:0" "\n\t" |
| 793 |
|
| 794 |
: |
| 795 |
/* No outputs */ |
| 796 |
: |
| 797 |
[index] "m" (index), |
| 798 |
[rptr] "m" (regsPtr), |
| 799 |
[registration] "m" (registration), |
| 800 |
[ok] "i" (TCL_OK), |
| 801 |
[error] "i" (TCL_ERROR) |
| 802 |
: |
| 803 |
"%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); |
| 804 |
status = registration.status; |
| 805 |
|
| 806 |
# endif /* !_WIN64 */ |
| 807 |
#elif defined(_MSC_VER) && defined(HAVE_CPUID) |
| 808 |
# if defined(_WIN64) |
| 809 |
|
| 810 |
__cpuid(regsPtr, index); |
| 811 |
status = TCL_OK; |
| 812 |
|
| 813 |
# elif defined (_M_IX86) |
| 814 |
/* |
| 815 |
* Define a structure in the stack frame to hold the registers. |
| 816 |
*/ |
| 817 |
|
| 818 |
struct { |
| 819 |
DWORD dw0; |
| 820 |
DWORD dw1; |
| 821 |
DWORD dw2; |
| 822 |
DWORD dw3; |
| 823 |
} regs; |
| 824 |
regs.dw0 = index; |
| 825 |
|
| 826 |
/* |
| 827 |
* Execute the CPUID instruction and save regs in the stack frame. |
| 828 |
*/ |
| 829 |
|
| 830 |
_try { |
| 831 |
_asm { |
| 832 |
push ebx |
| 833 |
push ecx |
| 834 |
push edx |
| 835 |
mov eax, regs.dw0 |
| 836 |
cpuid |
| 837 |
mov regs.dw0, eax |
| 838 |
mov regs.dw1, ebx |
| 839 |
mov regs.dw2, ecx |
| 840 |
mov regs.dw3, edx |
| 841 |
pop edx |
| 842 |
pop ecx |
| 843 |
pop ebx |
| 844 |
} |
| 845 |
|
| 846 |
/* |
| 847 |
* Copy regs back out to the caller. |
| 848 |
*/ |
| 849 |
|
| 850 |
regsPtr[0] = regs.dw0; |
| 851 |
regsPtr[1] = regs.dw1; |
| 852 |
regsPtr[2] = regs.dw2; |
| 853 |
regsPtr[3] = regs.dw3; |
| 854 |
|
| 855 |
status = TCL_OK; |
| 856 |
} __except(EXCEPTION_EXECUTE_HANDLER) { |
| 857 |
/* do nothing */ |
| 858 |
} |
| 859 |
|
| 860 |
# endif |
| 861 |
#else |
| 862 |
(void)index; |
| 863 |
(void)regsPtr; |
| 864 |
/* |
| 865 |
* Don't know how to do assembly code for this compiler and/or |
| 866 |
* architecture. |
| 867 |
*/ |
| 868 |
#endif |
| 869 |
return status; |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* Local Variables: |
| 874 |
* mode: c |
| 875 |
* c-basic-offset: 4 |
| 876 |
* fill-column: 78 |
| 877 |
* End: |
| 878 |
*/ |