source: trunk/kLdr/kLdrHlp.c@ 2867

Last change on this file since 2867 was 2867, checked in by bird, 19 years ago

top half of the filesearching is done.

  • Property svn:keywords set to Id
File size: 21.1 KB
Line 
1/* $Id: kLdrHlp.c 2867 2006-11-11 09:33:17Z bird $ */
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
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48#ifdef __OS2__
49/** The loader sempahore. */
50static 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. */
54static void *g_pvStub = NULL;
55/** The size of the stub object - 0 if no stub. */
56static size_t g_cbStub = 0;
57
58#elif defined(__WIN__)
59/** The loader sempahore. */
60static CRITICAL_SECTION g_CritSect;
61/** The system info. */
62static SYSTEM_INFO g_SystemInfo;
63#else
64# error "port me"
65#endif
66
67
68/**
69 * Initializes the loader semaphore.
70 *
71 * @returns 0 on success, non-zero OS status code on failure.
72 */
73int 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 */
95void 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 */
120int 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 */
152void 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__
168static 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__)
184static 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 */
211int 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__)
235 /* (We don't have to care about the stub here, because the stub will be unmapped before we get here.) */
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 return 0;
245 rc = GetLastError();
246 kldrHlpAssert(0);
247 return rc;
248
249#else
250# error "port me"
251#endif
252}
253
254
255/**
256 * Change the protection of one or more pages in an allocation.
257 *
258 * (This will of course only work correctly on memory allocated by kldrHlpPageAlloc().)
259 *
260 * @returns 0 on success, non-zero OS status code on failure.
261 * @param pv First page. Page aligned.
262 * @param cb Number of bytes. Page aligned.
263 * @param enmProt The new protection. Copy-on-write is invalid.
264 */
265int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt)
266{
267#ifdef __OS2__
268 APIRET rc;
269 uintptr_t offStub;
270 ULONG fFlags = kldrHlpPageProtToNative(enmProt);;
271
272 /*
273 * The non-stub pages.
274 */
275 rc = DosSetMem(pv, cb, fFlags);
276 if (rc && fFlags != PAG_DECOMMIT)
277 rc = DosSetMem(pv, cb, fFlags | PAG_COMMIT);
278 if (rc)
279 {
280 /* Try page by page. */
281 while (cb > 0)
282 {
283 rc = DosSetMem(pv, 0x1000, fFlags);
284 if (rc && fFlags != PAG_DECOMMIT)
285 rc = DosSetMem(pv, 0x1000, fFlags | PAG_COMMIT);
286 if (rc)
287 return rc;
288 pv = (void *)((uintptr_t)pv + 0x1000);
289 cb -= 0x1000;
290 }
291 }
292 kldrHlpAssert(!rc);
293 return rc;
294
295#elif defined(__WIN__)
296 DWORD fOldProt = 0;
297 DWORD fProt = kldrHlpPageProtToNative(enmProt);
298 int rc = 0;
299
300 if (!VirtualProtect(pv, cb, fProt, &fOldProt))
301 {
302 rc = GetLastError();
303 kldrHlpAssert(0);
304 }
305 return rc;
306#else
307# error "port me"
308#endif
309}
310
311
312/**
313 * Free memory allocated by kldrHlpPageAlloc().
314 *
315 * @returns 0 on success, non-zero OS status code on failure.
316 * @param pv The address returned by kldrHlpPageAlloc().
317 * @param cb The byte count requested from kldrHlpPageAlloc().
318 */
319int kldrHlpPageFree(void *pv, size_t cb)
320{
321#ifdef __OS2__
322 APIRET rc;
323
324 /*
325 * Deal with any portion overlapping with the stub.
326 */
327 uintptr_t offStub = (uintptr_t)pv - (uintptr_t)g_pvStub;
328 if (offStub < g_cbStub)
329 {
330 /* decommit the pages in the stub. */
331 size_t cbSub = KLDR_MIN(g_cbStub - offStub, cb);
332 rc = DosSetMem(pv, cbStub, PAG_DECOMMIT);
333 if (rc)
334 {
335 /* Page by page, ignoring errors after the first success. */
336 while (cbSub > 0)
337 {
338 if (!DosSetMem(pv, 0x1000, PAG_DECOMMIT))
339 rc = 0;
340 pv = (void *)((uintptr_t)pv + 0x1000);
341 cbSub -= 0x1000;
342 cb -= 0x1000;
343 }
344 if (rc)
345 {
346 kldrHlpAssert(!rc);
347 return rc;
348 }
349 }
350 else
351 {
352 cb -= cbSub;
353 if (!cb)
354 return 0;
355 pv = (void *)((uintptr_t)pv + cbSub);
356 }
357 }
358
359 /*
360 * Free the object.
361 */
362 rc = DosFreeMem(pv);
363 kldrHlpAssert(!rc);
364 return rc;
365
366#elif defined(__WIN__)
367 /*
368 * Free the object.
369 */
370 int rc = 0;
371 if (!VirtualFree(pv, 0 /*cb*/, MEM_RELEASE))
372 {
373 rc = GetLastError();
374 kldrHlpAssert(0);
375 }
376 return rc;
377
378#else
379# error "port me"
380#endif
381}
382
383
384/**
385 * Get an environment variable.
386 *
387 * @returns 0 on success.
388 * @returns KLDR_ERR_BUFFER_OVERFLOW on if the buffer is too small (it'll be partially filled).
389 * @returns KLDR_ERR_SYMBOL_NOT_FOUND if not found. (Yeah, abusing the status code, but it's only internally...)
390 * @returns OS specfic status code on other error.
391 * @param pszVar The variable name.
392 * @param pszVal Where to store the value.
393 * @param cchVal The size of the buffer pointed to by pszVal.
394 */
395int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal)
396{
397#ifdef __OS2__
398 PSZ pszValue = NULL;
399 int rc;
400
401 *pszVal = '\0';
402 rc = DosScanEnv((PCSZ)pszVar, &pszValue);
403 if (!rc)
404 {
405 size_t cch = kLdrHlpStrLen(pszValue);
406 if (cchVal > cch)
407 kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
408 else
409 {
410 rc = KLDR_ERR_BUFFER_OVERFLOW;
411 if (cchVal > 1)
412 {
413 kLdrHlpMemCopy(pszVal, pszValue, *pcchVal);
414 pszVal[*pcchVal - 1] = '\0';
415 }
416 }
417 }
418 else
419 rc = KLDR_ERR_SYMBOL_NOT_FOUND;
420 return rc;
421
422#elif defined(__WIN__)
423 DWORD cch;
424
425 SetLastError(0);
426 cch = GetEnvironmentVariable(pszVar, pszVal, cchVal);
427 if (cch < cchVal)
428 return 0;
429
430 *pszVal = '\0';
431 if (cch >= cchVal)
432 return KLDR_ERR_BUFFER_OVERFLOW;
433 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
434 return KLDR_ERR_SYMBOL_NOT_FOUND;
435 return GetLastError();
436
437#else
438# error "Port me"
439#endif
440}
441
442
443/**
444 * Gets an environment variable and converts it to a size_t.
445 *
446 * @returns 0 and *pcb on success.
447 * @returns On failure see kldrHlpGetEnv.
448 * @param pszVar The name of the variable.
449 * @param pcb Where to put the value.
450 */
451int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb)
452{
453 size_t cb;
454 unsigned uBase;
455 char szVal[64];
456 size_t cchVal = sizeof(szVal);
457 const char *psz;
458 int rc;
459
460 *pcb = 0;
461 rc = kldrHlpGetEnv(pszVar, szVal, cchVal);
462 if (rc)
463 return rc;
464
465 /* figure out the base. */
466 uBase = 10;
467 psz = szVal;
468 if ( *psz == '0'
469 && (psz[1] == 'x' || psz[1] == 'X'))
470 {
471 uBase = 16;
472 psz += 2;
473 }
474
475 /* convert it up to the first unknown char. */
476 cb = 0;
477 for(;;)
478 {
479 const char ch = *psz;
480 unsigned uDigit;
481 if (!ch)
482 break;
483 else if (ch >= '0' && ch <= '9')
484 uDigit = ch - '0';
485 else if (ch >= 'a' && ch <= 'z')
486 uDigit = ch - 'a' + 10;
487 else if (ch >= 'A' && ch <= 'Z')
488 uDigit = ch - 'A' + 10;
489 else
490 break;
491 if (uDigit >= uBase)
492 break;
493
494 /* add the digit */
495 cb *= uBase;
496 cb += uDigit;
497
498 psz++;
499 }
500
501 /* check for unit */
502 if (*psz == 'm' || *psz == 'M')
503 cb *= 1024*1024;
504 else if (*psz == 'k' ||*psz == 'K')
505 cb *= 1024;
506 else if (*psz == 'g' || *psz == 'G')
507 cb *= 1024*1024*1024;
508
509 *pcb = cb;
510 return 0;
511}
512
513
514/**
515 * Get the pointer to the filename part of the name.
516 *
517 * @returns Pointer to where the filename starts within the string pointed to by pszFilename.
518 * @returns Pointer to the terminator char if no filename.
519 * @param pszFilename The filename to parse.
520 */
521char *kldrHlpGetFilename(const char *pszFilename)
522{
523 const char *pszLast = NULL;
524 for (;;)
525 {
526 char ch = *pszFilename;
527#if defined(__OS2__) || defined(__WIN__)
528 if (ch == '/' || ch == '\\' || ch == ':')
529 {
530 while ((ch = *++pszFilename) == '/' || ch == '\\' || ch == ':')
531 /* nothing */;
532 pszLast = pszFilename;
533 }
534#else
535 if (ch == '/')
536 {
537 while ((ch = *++pszFilename) == '/')
538 /* betsuni */;
539 pszLast = pszFilename;
540 }
541#endif
542 if (!ch)
543 return (char *)(pszLast ? pszLast : pszFilename);
544 pszFilename++;
545 }
546}
547
548
549/**
550 * Gets the filename suffix.
551 *
552 * @returns Pointer to where the suffix starts within the string pointed to by pszFilename.
553 * @returns Pointer to the terminator char if no suffix.
554 * @param pszFilename The filename to parse.
555 */
556char *kldrHlpGetSuff(const char *pszFilename)
557{
558 const char *pszDot = NULL;
559 pszFilename = kldrHlpGetFilename(pszFilename);
560 for (;;)
561 {
562 char ch = *pszFilename;
563 if (ch == '.')
564 {
565 while ((ch = *++pszFilename) == '.')
566 /* nothing */;
567 if (ch)
568 pszDot = pszFilename - 1;
569 }
570 if (!ch)
571 return (char *)(pszDot ? pszDot : pszFilename);
572 pszFilename++;
573 }
574}
575
576
577/**
578 * Gets the filename extention.
579 *
580 * @returns Pointer to where the extension starts within the string pointed to by pszFilename.
581 * @returns Pointer to the terminator char if no extension.
582 * @param pszFilename The filename to parse.
583 */
584char *kldrHlpGetExt(const char *pszFilename)
585{
586 char *psz = kldrHlpGetSuff(pszFilename);
587 return *psz ? psz + 1 : psz;
588}
589
590
591/**
592 * Checks if this is only a filename or if it contains any kind
593 * of drive, directory, or server specs.
594 *
595 * @returns 1 if this is a filename only.
596 * @returns 0 of it's isn't only a filename.
597 * @param pszFilename The filename to parse.
598 */
599int kldrHlpIsFilenameOnly(const char *pszFilename)
600{
601 const char *pszLast = NULL;
602 for (;;)
603 {
604 const char ch = *pszFilename++;
605#if defined(__OS2__) || defined(__WIN__)
606 if (ch == '/' || ch == '\\' || ch == ':')
607#else
608 if (ch == '/')
609#endif
610 return 0;
611 if (!ch)
612 return 1;
613 }
614}
615
616
617/**
618 * Terminate the process.
619 *
620 * @param rc The exit status.
621 */
622void kldrHlpExit(int rc)
623{
624 for (;;)
625 {
626#ifdef __OS2__
627 DosExit(EXIT_PROCESS, rc);
628
629#elif defined(__WIN__)
630 TerminateProcess(GetCurrentProcess(), rc);
631
632#else
633# error "Port me"
634#endif
635 kldrHlpAssert(!"Impossible");
636 }
637}
638
639
640/**
641 * Sleep for a number of milliseconds.
642 * @param cMillies Number of milliseconds to sleep.
643 */
644void kldrHlpSleep(unsigned cMillies)
645{
646#ifdef __OS2__
647 DosSleep(cMillies);
648#elif defined(__WIN__)
649 Sleep(cMillies);
650#else
651 usleep(cMillies * 1000);
652#endif
653}
654
655
656/**
657 * Converts an signed integer to an ascii string.
658 *
659 * @returns psz.
660 * @param psz Pointer to the output buffer.
661 * @param cch The size of the output buffer.
662 * @param lVal The value.
663 * @param iBase The base to format it. (2,8,10 or 16)
664 */
665char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase)
666{
667 static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
668 char *pszRet = psz;
669
670 if (cch >= (lVal < 0 ? 3U : 2U) && psz)
671 {
672 /* prefix */
673 if (lVal < 0)
674 {
675 *psz++ = '-';
676 cch--;
677 lVal = -lVal;
678 }
679
680 /* the digits */
681 do
682 {
683 *psz++ = s_szDigits[lVal % iBase];
684 cch--;
685 lVal /= iBase;
686 } while (lVal && cch > 1);
687
688 /* overflow indicator */
689 if (lVal)
690 psz[-1] = '+';
691 }
692 else if (!pszRet)
693 return pszRet;
694 else if (cch < 1 || !pszRet)
695 return pszRet;
696 else
697 *psz++ = '+';
698 *psz = '\0';
699
700 return pszRet;
701}
702
703
704/**
705 * Writes a assert string with unix lineendings.
706 *
707 * @param pszMsg The string.
708 */
709static void kldrHlpAssertWrite(const char *pszMsg)
710{
711#if defined(__OS2__) || defined(__WIN__)
712 /*
713 * Line by line.
714 */
715 ULONG cbWritten;
716 const char *pszNl = kLdrHlpStrChr(pszMsg, '\n');
717 while (pszNl)
718 {
719 cbWritten = pszNl - pszMsg;
720
721#ifdef __OS2__
722 if (cbWritten)
723 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
724 DosWrite((HFILE)2, "\r\n", 2, &cbWritten);
725#else /* __WIN32__ */
726 if (cbWritten)
727 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
728 WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL);
729#endif
730
731 /* next */
732 pszMsg = pszNl + 1;
733 pszNl = kLdrHlpStrChr(pszMsg, '\n');
734 }
735
736 /*
737 * Remaining incomplete line.
738 */
739 if (*pszMsg)
740 {
741 cbWritten = kLdrHlpStrLen(pszMsg);
742#ifdef __OS2__
743 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
744#else /* __WIN32__ */
745 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
746#endif
747 }
748
749#else
750# error "port me"
751#endif
752}
753
754
755/**
756 * Internal worker for the kLdrHlpAssert() macro.
757 *
758 * @param pszExpr The assert expression.
759 * @param pszFile The filename.
760 * @param iLine The line number.
761 * @param pszFunction The function name.
762 */
763void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
764{
765 char szLine[16];
766
767 kldrHlpAssertWrite("\n!!!kLdr Assertion Failed!!!\nExpression: ");
768 kldrHlpAssertWrite(pszExpr);
769 kldrHlpAssertWrite("\nAt: ");
770 kldrHlpAssertWrite(pszFile);
771 kldrHlpAssertWrite("(");
772 kldrHlpAssertWrite(kldrHlpInt2Ascii(szLine, sizeof(szLine), iLine, 10));
773 kldrHlpAssertWrite(") ");
774 kldrHlpAssertWrite(pszFunction);
775 kldrHlpAssertWrite("\n");
776}
777
778
779#ifdef kLdrHlpStrChr_needed
780char *kLdrHlpStrChr(const char *psz, int ch)
781{
782 while (*psz)
783 {
784 if (*psz == ch)
785 return (char *)psz;
786 psz++;
787 }
788 return NULL;
789}
790#endif
791
792
793#ifdef kLdrHlpStrChr_needed
794void *kLdrHlpMemChr(const void *pv, int ch, size_t cb)
795{
796 const uint8_t *pb = (const uint8_t *)pv;
797 const uint8_t b = (uint8_t)ch;
798 while (cb)
799 {
800 if (*pb == b)
801 return (void *)pb;
802 pb++;
803 }
804 return NULL;
805}
806#endif
807
808
809int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb)
810{
811 const uint8_t *pb1 = (const uint8_t *)pv1;
812 const uint8_t *pb2 = (const uint8_t *)pv2;
813 while (cb)
814 {
815 if (*pb1 != *pb2)
816 {
817 const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
818 const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
819 if (u1 != u2)
820 return (int)*pb1 - (int)*pb2;
821 }
822 pb1++;
823 pb2++;
824 }
825 return 0;
826}
827
828
829int kLdrHlpStrIComp(const char *pv1, const char *pv2)
830{
831 const uint8_t *pb1 = (const uint8_t *)pv1;
832 const uint8_t *pb2 = (const uint8_t *)pv2;
833 for (;;)
834 {
835 if (*pb1 != *pb2)
836 {
837 const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
838 const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
839 if (u1 != u2)
840 return (int)*pb1 - (int)*pb2;
841 }
842 if (!*pb1)
843 break;
844 pb1++;
845 pb2++;
846 }
847 return 0;
848}
849
Note: See TracBrowser for help on using the repository browser.