[2826] | 1 | /* $Id: kLdrHlp.c 2842 2006-10-30 00:49:15Z bird $ */
|
---|
[2825] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - The Dynamic Loader, Helper Functions.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLdr.
|
---|
| 10 | *
|
---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLdr; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | /*******************************************************************************
|
---|
| 29 | * Header Files *
|
---|
| 30 | *******************************************************************************/
|
---|
| 31 | #ifdef __OS2__
|
---|
| 32 | # define INCL_BASE
|
---|
| 33 | # define INCL_ERRORS
|
---|
| 34 | # include <os2.h>
|
---|
| 35 | #elif defined(__WIN__)
|
---|
| 36 | # include <Windows.h>
|
---|
| 37 | #else
|
---|
| 38 | # error "port me"
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | #include <kLdr.h>
|
---|
| 42 | #include "kLdrHlp.h"
|
---|
| 43 |
|
---|
| 44 |
|
---|
[2830] | 45 | /*******************************************************************************
|
---|
| 46 | * Global Variables *
|
---|
| 47 | *******************************************************************************/
|
---|
| 48 | #ifdef __OS2__
|
---|
| 49 | /** The loader sempahore. */
|
---|
| 50 | static HMTX g_hmtx;
|
---|
| 51 | /** The base of the stub object.
|
---|
| 52 | * The OS/2 exe stub consists of a single data object. When allocating memory
|
---|
| 53 | * for an executable, we'll have to reuse this. */
|
---|
| 54 | static void *g_pvStub = NULL;
|
---|
| 55 | /** The size of the stub object - 0 if no stub. */
|
---|
| 56 | static size_t g_cbStub = 0;
|
---|
| 57 |
|
---|
| 58 | #elif defined(__WIN__)
|
---|
| 59 | /** The loader sempahore. */
|
---|
| 60 | static CRITICAL_SECTION g_CritSect;
|
---|
| 61 | /** The system info. */
|
---|
| 62 | static SYSTEM_INFO g_SystemInfo;
|
---|
| 63 | #else
|
---|
| 64 | # error "port me"
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
| 67 |
|
---|
[2825] | 68 | /**
|
---|
[2830] | 69 | * Initializes the loader semaphore.
|
---|
| 70 | *
|
---|
| 71 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 72 | */
|
---|
| 73 | int kldrHlpSemInit(void)
|
---|
| 74 | {
|
---|
| 75 | #ifdef __OS2__
|
---|
| 76 | APIRET rc;
|
---|
| 77 | g_hmtx = NULLHANDLE;
|
---|
| 78 | rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
|
---|
| 79 | if (rc)
|
---|
| 80 | return rc;
|
---|
| 81 |
|
---|
| 82 | #elif defined(__WIN__)
|
---|
| 83 | InitializeCriticalSection(&g_CritSect);
|
---|
| 84 |
|
---|
| 85 | #else
|
---|
| 86 | # error "port me"
|
---|
| 87 | #endif
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Terminates the loader semaphore.
|
---|
| 94 | */
|
---|
| 95 | void kldrHlpSemTerm(void)
|
---|
| 96 | {
|
---|
| 97 | #ifdef __OS2__
|
---|
| 98 | APIRET rc;
|
---|
| 99 | HMTX hmtx = g_hmtx;
|
---|
| 100 | g_hmtx = NULLHANDLE;
|
---|
| 101 | rc = DosCloseMutexSem(hmtx);
|
---|
| 102 | if (rc)
|
---|
| 103 | return rc;
|
---|
| 104 |
|
---|
| 105 | #elif defined(__WIN__)
|
---|
| 106 | DeleteCriticalSection(&g_CritSect);
|
---|
| 107 |
|
---|
| 108 | #else
|
---|
| 109 | # error "port me"
|
---|
| 110 | #endif
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Requests the loader sempahore ownership.
|
---|
| 116 | * This can be done recursivly.
|
---|
| 117 | *
|
---|
| 118 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 119 | */
|
---|
| 120 | int kldrHlpSemRequest(void)
|
---|
| 121 | {
|
---|
| 122 | #ifdef __OS2__
|
---|
| 123 | APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
|
---|
| 124 | if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
|
---|
| 125 | {
|
---|
| 126 | unsigned i = 0;
|
---|
| 127 | do
|
---|
| 128 | {
|
---|
| 129 | /** @todo check for deadlocks etc. */
|
---|
| 130 | rc = DosRequestMutexSem(g_hmtx, 1000);
|
---|
| 131 | } while ( ( rc == ERROR_TIMEOUT
|
---|
| 132 | || rc == ERROR_SEM_TIMEOUT
|
---|
| 133 | || rc == ERROR_INTERRUPT)
|
---|
| 134 | && i++ < 120);
|
---|
| 135 | }
|
---|
| 136 | return rc;
|
---|
| 137 |
|
---|
| 138 | #elif defined(__WIN__)
|
---|
| 139 | EnterCriticalSection(&g_CritSect);
|
---|
| 140 | return 0;
|
---|
| 141 |
|
---|
| 142 | #else
|
---|
| 143 | # error "port me"
|
---|
| 144 | #endif
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Releases the loader semaphore ownership.
|
---|
| 150 | * The caller is responsible for making sure it's the semaphore owner!
|
---|
| 151 | */
|
---|
| 152 | void kldrHlpSemRelease(void)
|
---|
| 153 | {
|
---|
| 154 | #ifdef __OS2__
|
---|
| 155 | APIRET rc = DosReleaseMutexSem(g_hmtx);
|
---|
| 156 | kldrHlpAssert(!rc); (void)rc;
|
---|
| 157 |
|
---|
| 158 | #elif defined(__WIN__)
|
---|
| 159 | LeaveCriticalSection(&g_CritSect);
|
---|
| 160 |
|
---|
| 161 | #else
|
---|
| 162 | # error "port me"
|
---|
| 163 | #endif
|
---|
| 164 |
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | #ifdef __OS2__
|
---|
| 168 | static ULONG kldrHlpPageProtToNative(KLDRPROT enmProt)
|
---|
| 169 | {
|
---|
| 170 | switch (enmProt)
|
---|
| 171 | {
|
---|
| 172 | case KLDRPROT_NOACCESS: return PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
| 173 | case KLDRPROT_READONLY: return PAG_COMMIT | PAG_READ;
|
---|
| 174 | case KLDRPROT_READWRITE: return PAG_COMMIT | PAG_READ | PAG_WRITE;
|
---|
| 175 | case KLDRPROT_EXECUTE: return PAG_COMMIT | PAG_EXECUTE;
|
---|
| 176 | case KLDRPROT_EXECUTE_READ: return PAG_COMMIT | PAG_EXECUTE | PAG_READ;
|
---|
| 177 | case KLDRPROT_EXECUTE_READWRITE: return PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
| 178 | default:
|
---|
| 179 | kldrHlpAssert(0);
|
---|
| 180 | return ~0U;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | #elif defined(__WIN__)
|
---|
| 184 | static DWORD kldrHlpPageProtToNative(KLDRPROT enmProt)
|
---|
| 185 | {
|
---|
| 186 | switch (enmProt)
|
---|
| 187 | {
|
---|
| 188 | case KLDRPROT_NOACCESS: return PAGE_NOACCESS;
|
---|
| 189 | case KLDRPROT_READONLY: return PAGE_READONLY;
|
---|
| 190 | case KLDRPROT_READWRITE: return PAGE_READWRITE;
|
---|
| 191 | case KLDRPROT_EXECUTE: return PAGE_EXECUTE;
|
---|
| 192 | case KLDRPROT_EXECUTE_READ: return PAGE_EXECUTE_READ;
|
---|
| 193 | case KLDRPROT_EXECUTE_READWRITE: return PAGE_EXECUTE_READWRITE;
|
---|
| 194 | default:
|
---|
| 195 | kldrHlpAssert(0);
|
---|
| 196 | return ~0U;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | #endif
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * Allocate a chunk of memory with page granularity.
|
---|
| 204 | *
|
---|
| 205 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 206 | * @param ppv Where to store the address of the allocated memory.
|
---|
| 207 | * If fFixed is set, *ppv will on entry contain the desired address (page aligned).
|
---|
| 208 | * @param cb Number of bytes. Page aligned.
|
---|
| 209 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 210 | */
|
---|
| 211 | int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed)
|
---|
| 212 | {
|
---|
| 213 | #ifdef __OS2__
|
---|
| 214 | APIRET rc;
|
---|
| 215 | ULONG fFlags = kldrHlpPageProtToNative(enmProt);;
|
---|
| 216 |
|
---|
| 217 | if (!fFixed)
|
---|
| 218 | {
|
---|
| 219 | /* simple */
|
---|
| 220 | rc = DosAllocMem(ppv, cb, fFlags | OBJ_ANY);
|
---|
| 221 | if (rc == ERROR_INVALID_PARAMETER)
|
---|
| 222 | rc = DosAllocMem(ppv, cb, fFlags);
|
---|
| 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
| 226 | /* not so simple. */
|
---|
| 227 | /** @todo I've got code for this in libc somewhere. */
|
---|
| 228 | }
|
---|
| 229 | if (!rc)
|
---|
| 230 | return 0;
|
---|
| 231 | kldrHlpAssert(0);
|
---|
| 232 | return rc;
|
---|
| 233 |
|
---|
| 234 | #elif defined(__WIN__)
|
---|
[2831] | 235 | /* (We don't have to care about the stub here, because the stub will be unmapped before we get here.) */
|
---|
[2830] | 236 | int rc;
|
---|
| 237 | DWORD fProt = kldrHlpPageProtToNative(enmProt);
|
---|
| 238 |
|
---|
| 239 | if (!g_SystemInfo.dwPageSize)
|
---|
| 240 | GetSystemInfo(&g_SystemInfo);
|
---|
| 241 |
|
---|
| 242 | *ppv = VirtualAlloc(fFixed ? *ppv : NULL, cb, MEM_COMMIT, fProt);
|
---|
| 243 | if (*ppv == NULL)
|
---|
| 244 | {
|
---|
| 245 | rc = GetLastError();
|
---|
| 246 | kldrHlpAssert(0);
|
---|
| 247 | }
|
---|
| 248 | return rc;
|
---|
| 249 |
|
---|
| 250 | #else
|
---|
| 251 | # error "port me"
|
---|
| 252 | #endif
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 | /**
|
---|
| 257 | * Change the protection of one or more pages in an allocation.
|
---|
| 258 | *
|
---|
| 259 | * (This will of course only work correctly on memory allocated by kldrHlpPageAlloc().)
|
---|
| 260 | *
|
---|
| 261 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 262 | * @param pv First page. Page aligned.
|
---|
| 263 | * @param cb Number of bytes. Page aligned.
|
---|
| 264 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 265 | */
|
---|
| 266 | int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt)
|
---|
| 267 | {
|
---|
| 268 | #ifdef __OS2__
|
---|
| 269 | APIRET rc;
|
---|
| 270 | uintptr_t offStub;
|
---|
| 271 | ULONG fFlags = kldrHlpPageProtToNative(enmProt);;
|
---|
| 272 |
|
---|
| 273 | /*
|
---|
| 274 | * The non-stub pages.
|
---|
| 275 | */
|
---|
| 276 | rc = DosSetMem(pv, cb, fFlags);
|
---|
| 277 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 278 | rc = DosSetMem(pv, cb, fFlags | PAG_COMMIT);
|
---|
| 279 | if (rc)
|
---|
| 280 | {
|
---|
| 281 | /* Try page by page. */
|
---|
| 282 | while (cb > 0)
|
---|
| 283 | {
|
---|
| 284 | rc = DosSetMem(pv, 0x1000, fFlags);
|
---|
| 285 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 286 | rc = DosSetMem(pv, 0x1000, fFlags | PAG_COMMIT);
|
---|
| 287 | if (rc)
|
---|
| 288 | return rc;
|
---|
| 289 | pv = (void *)((uintptr_t)pv + 0x1000);
|
---|
| 290 | cb -= 0x1000;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | kldrHlpAssert(!rc);
|
---|
| 294 | return rc;
|
---|
| 295 |
|
---|
| 296 | #elif defined(__WIN__)
|
---|
| 297 | DWORD fOldProt = 0;
|
---|
| 298 | DWORD fProt = kldrHlpPageProtToNative(enmProt);
|
---|
| 299 | int rc = 0;
|
---|
| 300 |
|
---|
| 301 | if (!VirtualProtect(pv, cb, fProt, &fOldProt))
|
---|
| 302 | {
|
---|
| 303 | rc = GetLastError();
|
---|
| 304 | kldrHlpAssert(0);
|
---|
| 305 | }
|
---|
| 306 | return rc;
|
---|
| 307 | #else
|
---|
| 308 | # error "port me"
|
---|
| 309 | #endif
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | * Free memory allocated by kldrHlpPageAlloc().
|
---|
| 315 | *
|
---|
| 316 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 317 | * @param pv The address returned by kldrHlpPageAlloc().
|
---|
| 318 | * @param cb The byte count requested from kldrHlpPageAlloc().
|
---|
| 319 | */
|
---|
| 320 | int kldrHlpPageFree(void *pv, size_t cb)
|
---|
| 321 | {
|
---|
| 322 | #ifdef __OS2__
|
---|
| 323 | APIRET rc;
|
---|
| 324 |
|
---|
| 325 | /*
|
---|
| 326 | * Deal with any portion overlapping with the stub.
|
---|
| 327 | */
|
---|
| 328 | uintptr_t offStub = (uintptr_t)pv - (uintptr_t)g_pvStub;
|
---|
| 329 | if (offStub < g_cbStub)
|
---|
| 330 | {
|
---|
| 331 | /* decommit the pages in the stub. */
|
---|
| 332 | size_t cbSub = KLDR_MIN(g_cbStub - offStub, cb);
|
---|
| 333 | rc = DosSetMem(pv, cbStub, PAG_DECOMMIT);
|
---|
| 334 | if (rc)
|
---|
| 335 | {
|
---|
| 336 | /* Page by page, ignoring errors after the first success. */
|
---|
| 337 | while (cbSub > 0)
|
---|
| 338 | {
|
---|
| 339 | if (!DosSetMem(pv, 0x1000, PAG_DECOMMIT))
|
---|
| 340 | rc = 0;
|
---|
| 341 | pv = (void *)((uintptr_t)pv + 0x1000);
|
---|
| 342 | cbSub -= 0x1000;
|
---|
| 343 | cb -= 0x1000;
|
---|
| 344 | }
|
---|
| 345 | if (rc)
|
---|
| 346 | {
|
---|
| 347 | kldrHlpAssert(!rc);
|
---|
| 348 | return rc;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | else
|
---|
| 352 | {
|
---|
| 353 | cb -= cbSub;
|
---|
| 354 | if (!cb)
|
---|
| 355 | return 0;
|
---|
| 356 | pv = (void *)((uintptr_t)pv + cbSub);
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /*
|
---|
| 361 | * Free the object.
|
---|
| 362 | */
|
---|
| 363 | rc = DosFreeMem(pv);
|
---|
| 364 | kldrHlpAssert(!rc);
|
---|
| 365 | return rc;
|
---|
| 366 |
|
---|
| 367 | #elif defined(__WIN__)
|
---|
| 368 | /*
|
---|
| 369 | * Free the object.
|
---|
| 370 | */
|
---|
| 371 | int rc = 0;
|
---|
| 372 | if (!VirtualFree(pv, 0 /*cb*/, MEM_RELEASE))
|
---|
| 373 | {
|
---|
| 374 | rc = GetLastError();
|
---|
| 375 | kldrHlpAssert(0);
|
---|
| 376 | }
|
---|
| 377 | return rc;
|
---|
| 378 |
|
---|
| 379 | #else
|
---|
| 380 | # error "port me"
|
---|
| 381 | #endif
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
[2825] | 386 | * Get an environment variable.
|
---|
| 387 | *
|
---|
| 388 | * @returns 0 on success, on failure an OS specific status code is returned.
|
---|
| 389 | * @param pszVar The variable name.
|
---|
| 390 | * @param pszVal Where to store the value. NULL is allowed if *pcchVal is 0.
|
---|
| 391 | * @param pcchVal On input the size of the buffer pointed to by pszVal.
|
---|
| 392 | * On output this contains the string length on success, while on
|
---|
| 393 | * failure (including buffer overflow) the require buffer size.
|
---|
| 394 | * If the variable wasn't found, it's set to 0.
|
---|
| 395 | */
|
---|
| 396 | int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal)
|
---|
| 397 | {
|
---|
| 398 | #ifdef __OS2__
|
---|
| 399 | PSZ pszValue = NULL;
|
---|
| 400 | int rc = DosScanEnv((PCSZ)pszVar, &pszValue);
|
---|
| 401 | if (!rc)
|
---|
| 402 | {
|
---|
[2828] | 403 | size_t cch = kLdrHlpStrLen(pszValue);
|
---|
[2825] | 404 | if (pszVal)
|
---|
| 405 | {
|
---|
| 406 | if (*pcchVal > cch)
|
---|
| 407 | {
|
---|
[2828] | 408 | kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
|
---|
[2825] | 409 | *pcchVal = cch;
|
---|
| 410 | }
|
---|
| 411 | else if (*pcchVal)
|
---|
| 412 | {
|
---|
[2828] | 413 | kLdrHlpMemCopy(pszVal, pszValue, *pcchVal);
|
---|
[2825] | 414 | pszVal[*pcchVal - 1] = '\0';
|
---|
| 415 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
| 416 | *pcchVal = cch + 1;
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | else
|
---|
| 420 | {
|
---|
| 421 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
| 422 | *pcchVal = cch + 1;
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | else
|
---|
| 426 | {
|
---|
| 427 | if (pszVal)
|
---|
| 428 | *pszVal = '\0';
|
---|
| 429 | *pcchVal = 0;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | return rc;
|
---|
| 433 |
|
---|
| 434 | #elif defined(__WIN__)
|
---|
| 435 | DWORD cch;
|
---|
| 436 | SetLastError(0);
|
---|
| 437 | cch = GetEnvironmentVariable(pszVar, pszVal, *pcchVal);
|
---|
| 438 | if (cch)
|
---|
| 439 | {
|
---|
| 440 | *pcchVal = cch;
|
---|
| 441 | return 0;
|
---|
| 442 | }
|
---|
| 443 | if (!GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
---|
| 444 | {
|
---|
| 445 | *pcchVal = 0;
|
---|
| 446 | return ERROR_ENVVAR_NOT_FOUND;
|
---|
| 447 | }
|
---|
| 448 | *pcchVal = cch;
|
---|
| 449 | return ERROR_BUFFER_OVERFLOW;
|
---|
| 450 |
|
---|
| 451 | #else
|
---|
| 452 | # error "Port me"
|
---|
| 453 | #endif
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 |
|
---|
| 457 | /**
|
---|
| 458 | * Terminate the process.
|
---|
| 459 | *
|
---|
| 460 | * @param rc The exit status.
|
---|
| 461 | */
|
---|
| 462 | void kldrHlpExit(int rc)
|
---|
| 463 | {
|
---|
| 464 | for (;;)
|
---|
| 465 | {
|
---|
| 466 | #ifdef __OS2__
|
---|
| 467 | DosExit(EXIT_PROCESS, rc);
|
---|
| 468 |
|
---|
| 469 | #elif defined(__WIN__)
|
---|
| 470 | TerminateProcess(GetCurrentProcess(), rc);
|
---|
| 471 |
|
---|
| 472 | #else
|
---|
| 473 | # error "Port me"
|
---|
| 474 | #endif
|
---|
| 475 | kldrHlpAssert(!"Impossible");
|
---|
| 476 | }
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[2830] | 479 |
|
---|
| 480 | /**
|
---|
| 481 | * Sleep for a number of milliseconds.
|
---|
| 482 | * @param cMillies Number of milliseconds to sleep.
|
---|
| 483 | */
|
---|
| 484 | void kldrHlpSleep(unsigned cMillies)
|
---|
| 485 | {
|
---|
| 486 | #ifdef __OS2__
|
---|
| 487 | DosSleep(cMillies);
|
---|
| 488 | #elif defined(__WIN__)
|
---|
| 489 | Sleep(cMillies);
|
---|
| 490 | #else
|
---|
| 491 | usleep(cMillies * 1000);
|
---|
| 492 | #endif
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 |
|
---|
[2836] | 496 | /**
|
---|
| 497 | * Converts an signed integer to an ascii string.
|
---|
| 498 | *
|
---|
| 499 | * @returns psz.
|
---|
| 500 | * @param psz Pointer to the output buffer.
|
---|
| 501 | * @param cch The size of the output buffer.
|
---|
| 502 | * @param lVal The value.
|
---|
| 503 | * @param iBase The base to format it. (2,8,10 or 16)
|
---|
| 504 | */
|
---|
| 505 | char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase)
|
---|
[2825] | 506 | {
|
---|
[2836] | 507 | static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
---|
| 508 | char *pszRet = psz;
|
---|
| 509 |
|
---|
| 510 | if (cch >= (lVal < 0 ? 3U : 2U) && psz)
|
---|
[2825] | 511 | {
|
---|
[2836] | 512 | /* prefix */
|
---|
| 513 | if (lVal < 0)
|
---|
| 514 | {
|
---|
| 515 | *psz++ = '-';
|
---|
| 516 | cch--;
|
---|
| 517 | lVal = -lVal;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /* the digits */
|
---|
| 521 | do
|
---|
| 522 | {
|
---|
| 523 | *psz++ = s_szDigits[lVal % iBase];
|
---|
| 524 | cch--;
|
---|
| 525 | lVal /= iBase;
|
---|
| 526 | } while (lVal && cch > 1);
|
---|
| 527 |
|
---|
| 528 | /* overflow indicator */
|
---|
| 529 | if (lVal)
|
---|
| 530 | psz[-1] = '+';
|
---|
| 531 | }
|
---|
| 532 | else if (!pszRet)
|
---|
| 533 | return pszRet;
|
---|
| 534 | else if (cch < 1 || !pszRet)
|
---|
| 535 | return pszRet;
|
---|
| 536 | else
|
---|
| 537 | *psz++ = '+';
|
---|
| 538 | *psz = '\0';
|
---|
| 539 |
|
---|
| 540 | return pszRet;
|
---|
[2825] | 541 | }
|
---|
| 542 |
|
---|
| 543 |
|
---|
| 544 | /**
|
---|
[2832] | 545 | * Writes a assert string with unix lineendings.
|
---|
| 546 | *
|
---|
| 547 | * @param pszMsg The string.
|
---|
| 548 | */
|
---|
| 549 | static void kldrHlpAssertWrite(const char *pszMsg)
|
---|
| 550 | {
|
---|
| 551 | #if defined(__OS2__) || defined(__WIN__)
|
---|
| 552 | /*
|
---|
| 553 | * Line by line.
|
---|
| 554 | */
|
---|
| 555 | ULONG cbWritten;
|
---|
| 556 | const char *pszNl = kLdrHlpStrChr(pszMsg, '\n');
|
---|
[2842] | 557 | while (pszNl)
|
---|
[2832] | 558 | {
|
---|
| 559 | cbWritten = pszNl - pszMsg;
|
---|
| 560 |
|
---|
| 561 | #ifdef __OS2__
|
---|
| 562 | if (cbWritten)
|
---|
| 563 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
|
---|
| 564 | DosWrite((HFILE)2, "\r\n", 2, &cbWritten);
|
---|
| 565 | #else /* __WIN32__ */
|
---|
| 566 | if (cbWritten)
|
---|
| 567 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
|
---|
| 568 | WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL);
|
---|
| 569 | #endif
|
---|
| 570 |
|
---|
| 571 | /* next */
|
---|
| 572 | pszMsg = pszNl + 1;
|
---|
| 573 | pszNl = kLdrHlpStrChr(pszMsg, '\n');
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | /*
|
---|
| 577 | * Remaining incomplete line.
|
---|
| 578 | */
|
---|
| 579 | if (*pszMsg)
|
---|
| 580 | {
|
---|
| 581 | cbWritten = kLdrHlpStrLen(pszMsg);
|
---|
| 582 | #ifdef __OS2__
|
---|
| 583 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
|
---|
| 584 | #else /* __WIN32__ */
|
---|
| 585 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
|
---|
| 586 | #endif
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | #else
|
---|
| 590 | # error "port me"
|
---|
| 591 | #endif
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 |
|
---|
| 595 | /**
|
---|
[2825] | 596 | * Internal worker for the kLdrHlpAssert() macro.
|
---|
| 597 | *
|
---|
| 598 | * @param pszExpr The assert expression.
|
---|
| 599 | * @param pszFile The filename.
|
---|
| 600 | * @param iLine The line number.
|
---|
| 601 | * @param pszFunction The function name.
|
---|
| 602 | */
|
---|
| 603 | void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
|
---|
| 604 | {
|
---|
[2832] | 605 | char szLine[16];
|
---|
[2825] | 606 |
|
---|
[2832] | 607 | kldrHlpAssertWrite("\n!!!kLdr Assertion Failed!!!\nExpression: ");
|
---|
| 608 | kldrHlpAssertWrite(pszExpr);
|
---|
| 609 | kldrHlpAssertWrite("\nAt: ");
|
---|
| 610 | kldrHlpAssertWrite(pszFile);
|
---|
| 611 | kldrHlpAssertWrite("(");
|
---|
[2836] | 612 | kldrHlpAssertWrite(kldrHlpInt2Ascii(szLine, sizeof(szLine), iLine, 10));
|
---|
[2832] | 613 | kldrHlpAssertWrite(") ");
|
---|
| 614 | kldrHlpAssertWrite(pszFunction);
|
---|
| 615 | kldrHlpAssertWrite("\n");
|
---|
| 616 | }
|
---|
[2825] | 617 |
|
---|
| 618 |
|
---|
[2832] | 619 | #ifdef kLdrHlpStrChr_needed
|
---|
| 620 | char *kLdrHlpStrChr(const char *psz, int ch)
|
---|
| 621 | {
|
---|
| 622 | while (*psz)
|
---|
| 623 | {
|
---|
| 624 | if (*psz == ch)
|
---|
| 625 | return (char *)psz;
|
---|
| 626 | psz++;
|
---|
| 627 | }
|
---|
| 628 | return NULL;
|
---|
| 629 | }
|
---|
| 630 | #endif
|
---|
[2825] | 631 |
|
---|
[2832] | 632 |
|
---|
| 633 | #ifdef kLdrHlpStrChr_needed
|
---|
| 634 | void *kLdrHlpMemChr(const void *pv, int ch, size_t cb)
|
---|
| 635 | {
|
---|
| 636 | const uint8_t *pb = (const uint8_t *)pv;
|
---|
| 637 | const uint8_t b = (uint8_t)ch;
|
---|
| 638 | while (cb)
|
---|
| 639 | {
|
---|
| 640 | if (*pb == b)
|
---|
| 641 | return (void *)pb;
|
---|
| 642 | pb++;
|
---|
| 643 | }
|
---|
| 644 | return NULL;
|
---|
| 645 | }
|
---|
[2825] | 646 | #endif
|
---|
| 647 |
|
---|