Merge trunk HEAD (46152)
[reactos.git] / drivers / bus / acpi / utils / cmclib.c
1 /******************************************************************************
2 *
3 * Module Name: cmclib - Local implementation of C library functions
4 * $Revision: 1.1 $
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000, 2001 R. Byron Moore
10 *
11 * This program 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 * This program 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 this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27 #include <acpi.h>
28
29 /*
30 * These implementations of standard C Library routines can optionally be
31 * used if a C library is not available. In general, they are less efficient
32 * than an inline or assembly implementation
33 */
34
35 #define _COMPONENT MISCELLANEOUS
36 MODULE_NAME ("cmclib")
37
38
39 #ifndef ACPI_USE_SYSTEM_CLIBRARY
40
41 /*******************************************************************************
42 *
43 * FUNCTION: strlen
44 *
45 * PARAMETERS: String - Null terminated string
46 *
47 * RETURN: Length
48 *
49 * DESCRIPTION: Returns the length of the input string
50 *
51 ******************************************************************************/
52
53
54 u32
55 acpi_cm_strlen (
56 const NATIVE_CHAR *string)
57 {
58 u32 length = 0;
59
60
61 /* Count the string until a null is encountered */
62
63 while (*string) {
64 length++;
65 string++;
66 }
67
68 return (length);
69 }
70
71
72 /*******************************************************************************
73 *
74 * FUNCTION: strcpy
75 *
76 * PARAMETERS: Dst_string - Target of the copy
77 * Src_string - The source string to copy
78 *
79 * RETURN: Dst_string
80 *
81 * DESCRIPTION: Copy a null terminated string
82 *
83 ******************************************************************************/
84
85 NATIVE_CHAR *
86 acpi_cm_strcpy (
87 NATIVE_CHAR *dst_string,
88 const NATIVE_CHAR *src_string)
89 {
90 NATIVE_CHAR *string = dst_string;
91
92
93 /* Move bytes brute force */
94
95 while (*src_string) {
96 *string = *src_string;
97
98 string++;
99 src_string++;
100 }
101
102 /* Null terminate */
103
104 *string = 0;
105
106 return (dst_string);
107 }
108
109
110 /*******************************************************************************
111 *
112 * FUNCTION: strncpy
113 *
114 * PARAMETERS: Dst_string - Target of the copy
115 * Src_string - The source string to copy
116 * Count - Maximum # of bytes to copy
117 *
118 * RETURN: Dst_string
119 *
120 * DESCRIPTION: Copy a null terminated string, with a maximum length
121 *
122 ******************************************************************************/
123
124 NATIVE_CHAR *
125 acpi_cm_strncpy (
126 NATIVE_CHAR *dst_string,
127 const NATIVE_CHAR *src_string,
128 NATIVE_UINT count)
129 {
130 NATIVE_CHAR *string = dst_string;
131
132
133 /* Copy the string */
134
135 for (string = dst_string;
136 count && (count--, (*string++ = *src_string++)); ) {;}
137
138 /* Pad with nulls if necessary */
139
140 while (count--) {
141 *string = 0;
142 string++;
143 }
144
145 /* Return original pointer */
146
147 return (dst_string);
148 }
149
150
151 /*******************************************************************************
152 *
153 * FUNCTION: strcmp
154 *
155 * PARAMETERS: String1 - First string
156 * String2 - Second string
157 *
158 * RETURN: Index where strings mismatched, or 0 if strings matched
159 *
160 * DESCRIPTION: Compare two null terminated strings
161 *
162 ******************************************************************************/
163
164 u32
165 acpi_cm_strcmp (
166 const NATIVE_CHAR *string1,
167 const NATIVE_CHAR *string2)
168 {
169
170
171 for ( ; (*string1 == *string2); string2++) {
172 if (!*string1++) {
173 return (0);
174 }
175 }
176
177
178 return ((unsigned char) *string1 - (unsigned char) *string2);
179 }
180
181
182 /*******************************************************************************
183 *
184 * FUNCTION: strncmp
185 *
186 * PARAMETERS: String1 - First string
187 * String2 - Second string
188 * Count - Maximum # of bytes to compare
189 *
190 * RETURN: Index where strings mismatched, or 0 if strings matched
191 *
192 * DESCRIPTION: Compare two null terminated strings, with a maximum length
193 *
194 ******************************************************************************/
195
196 u32
197 acpi_cm_strncmp (
198 const NATIVE_CHAR *string1,
199 const NATIVE_CHAR *string2,
200 NATIVE_UINT count)
201 {
202
203
204 for ( ; count-- && (*string1 == *string2); string2++) {
205 if (!*string1++) {
206 return (0);
207 }
208 }
209
210 return ((count == -1) ? 0 : ((unsigned char) *string1 -
211 (unsigned char) *string2));
212 }
213
214
215 /*******************************************************************************
216 *
217 * FUNCTION: Strcat
218 *
219 * PARAMETERS: Dst_string - Target of the copy
220 * Src_string - The source string to copy
221 *
222 * RETURN: Dst_string
223 *
224 * DESCRIPTION: Append a null terminated string to a null terminated string
225 *
226 ******************************************************************************/
227
228 NATIVE_CHAR *
229 acpi_cm_strcat (
230 NATIVE_CHAR *dst_string,
231 const NATIVE_CHAR *src_string)
232 {
233 NATIVE_CHAR *string;
234
235
236 /* Find end of the destination string */
237
238 for (string = dst_string; *string++; ) { ; }
239
240 /* Concatinate the string */
241
242 for (--string; (*string++ = *src_string++); ) { ; }
243
244 return (dst_string);
245 }
246
247
248 /*******************************************************************************
249 *
250 * FUNCTION: strncat
251 *
252 * PARAMETERS: Dst_string - Target of the copy
253 * Src_string - The source string to copy
254 * Count - Maximum # of bytes to copy
255 *
256 * RETURN: Dst_string
257 *
258 * DESCRIPTION: Append a null terminated string to a null terminated string,
259 * with a maximum count.
260 *
261 ******************************************************************************/
262
263 NATIVE_CHAR *
264 acpi_cm_strncat (
265 NATIVE_CHAR *dst_string,
266 const NATIVE_CHAR *src_string,
267 NATIVE_UINT count)
268 {
269 NATIVE_CHAR *string;
270
271
272 if (count) {
273 /* Find end of the destination string */
274
275 for (string = dst_string; *string++; ) { ; }
276
277 /* Concatinate the string */
278
279 for (--string; (*string++ = *src_string++) && --count; ) { ; }
280
281 /* Null terminate if necessary */
282
283 if (!count) {
284 *string = 0;
285 }
286 }
287
288 return (dst_string);
289 }
290
291
292 /*******************************************************************************
293 *
294 * FUNCTION: memcpy
295 *
296 * PARAMETERS: Dest - Target of the copy
297 * Src - Source buffer to copy
298 * Count - Number of bytes to copy
299 *
300 * RETURN: Dest
301 *
302 * DESCRIPTION: Copy arbitrary bytes of memory
303 *
304 ******************************************************************************/
305
306 void *
307 acpi_cm_memcpy (
308 void *dest,
309 const void *src,
310 NATIVE_UINT count)
311 {
312 NATIVE_CHAR *new = (NATIVE_CHAR *) dest;
313 NATIVE_CHAR *old = (NATIVE_CHAR *) src;
314
315
316 while (count) {
317 *new = *old;
318 new++;
319 old++;
320 count--;
321 }
322
323 return (dest);
324 }
325
326
327 /*******************************************************************************
328 *
329 * FUNCTION: memset
330 *
331 * PARAMETERS: Dest - Buffer to set
332 * Value - Value to set each byte of memory
333 * Count - Number of bytes to set
334 *
335 * RETURN: Dest
336 *
337 * DESCRIPTION: Initialize a buffer to a known value.
338 *
339 ******************************************************************************/
340
341 void *
342 acpi_cm_memset (
343 void *dest,
344 NATIVE_UINT value,
345 NATIVE_UINT count)
346 {
347 NATIVE_CHAR *new = (NATIVE_CHAR *) dest;
348
349
350 while (count) {
351 *new = (char) value;
352 new++;
353 count--;
354 }
355
356 return (dest);
357 }
358
359
360 #define NEGATIVE 1
361 #define POSITIVE 0
362
363
364 #define _ACPI_XA 0x00 /* extra alphabetic - not supported */
365 #define _ACPI_XS 0x40 /* extra space */
366 #define _ACPI_BB 0x00 /* BEL, BS, etc. - not supported */
367 #define _ACPI_CN 0x20 /* CR, FF, HT, NL, VT */
368 #define _ACPI_DI 0x04 /* '0'-'9' */
369 #define _ACPI_LO 0x02 /* 'a'-'z' */
370 #define _ACPI_PU 0x10 /* punctuation */
371 #define _ACPI_SP 0x08 /* space */
372 #define _ACPI_UP 0x01 /* 'A'-'Z' */
373 #define _ACPI_XD 0x80 /* '0'-'9', 'A'-'F', 'a'-'f' */
374
375 static const u8 _acpi_ctype[257] = {
376 _ACPI_CN, /* 0x0 0. */
377 _ACPI_CN, /* 0x1 1. */
378 _ACPI_CN, /* 0x2 2. */
379 _ACPI_CN, /* 0x3 3. */
380 _ACPI_CN, /* 0x4 4. */
381 _ACPI_CN, /* 0x5 5. */
382 _ACPI_CN, /* 0x6 6. */
383 _ACPI_CN, /* 0x7 7. */
384 _ACPI_CN, /* 0x8 8. */
385 _ACPI_CN|_ACPI_SP, /* 0x9 9. */
386 _ACPI_CN|_ACPI_SP, /* 0xA 10. */
387 _ACPI_CN|_ACPI_SP, /* 0xB 11. */
388 _ACPI_CN|_ACPI_SP, /* 0xC 12. */
389 _ACPI_CN|_ACPI_SP, /* 0xD 13. */
390 _ACPI_CN, /* 0xE 14. */
391 _ACPI_CN, /* 0xF 15. */
392 _ACPI_CN, /* 0x10 16. */
393 _ACPI_CN, /* 0x11 17. */
394 _ACPI_CN, /* 0x12 18. */
395 _ACPI_CN, /* 0x13 19. */
396 _ACPI_CN, /* 0x14 20. */
397 _ACPI_CN, /* 0x15 21. */
398 _ACPI_CN, /* 0x16 22. */
399 _ACPI_CN, /* 0x17 23. */
400 _ACPI_CN, /* 0x18 24. */
401 _ACPI_CN, /* 0x19 25. */
402 _ACPI_CN, /* 0x1A 26. */
403 _ACPI_CN, /* 0x1B 27. */
404 _ACPI_CN, /* 0x1C 28. */
405 _ACPI_CN, /* 0x1D 29. */
406 _ACPI_CN, /* 0x1E 30. */
407 _ACPI_CN, /* 0x1F 31. */
408 _ACPI_XS|_ACPI_SP, /* 0x20 32. ' ' */
409 _ACPI_PU, /* 0x21 33. '!' */
410 _ACPI_PU, /* 0x22 34. '"' */
411 _ACPI_PU, /* 0x23 35. '#' */
412 _ACPI_PU, /* 0x24 36. '$' */
413 _ACPI_PU, /* 0x25 37. '%' */
414 _ACPI_PU, /* 0x26 38. '&' */
415 _ACPI_PU, /* 0x27 39. ''' */
416 _ACPI_PU, /* 0x28 40. '(' */
417 _ACPI_PU, /* 0x29 41. ')' */
418 _ACPI_PU, /* 0x2A 42. '*' */
419 _ACPI_PU, /* 0x2B 43. '+' */
420 _ACPI_PU, /* 0x2C 44. ',' */
421 _ACPI_PU, /* 0x2D 45. '-' */
422 _ACPI_PU, /* 0x2E 46. '.' */
423 _ACPI_PU, /* 0x2F 47. '/' */
424 _ACPI_XD|_ACPI_DI, /* 0x30 48. '0' */
425 _ACPI_XD|_ACPI_DI, /* 0x31 49. '1' */
426 _ACPI_XD|_ACPI_DI, /* 0x32 50. '2' */
427 _ACPI_XD|_ACPI_DI, /* 0x33 51. '3' */
428 _ACPI_XD|_ACPI_DI, /* 0x34 52. '4' */
429 _ACPI_XD|_ACPI_DI, /* 0x35 53. '5' */
430 _ACPI_XD|_ACPI_DI, /* 0x36 54. '6' */
431 _ACPI_XD|_ACPI_DI, /* 0x37 55. '7' */
432 _ACPI_XD|_ACPI_DI, /* 0x38 56. '8' */
433 _ACPI_XD|_ACPI_DI, /* 0x39 57. '9' */
434 _ACPI_PU, /* 0x3A 58. ':' */
435 _ACPI_PU, /* 0x3B 59. ';' */
436 _ACPI_PU, /* 0x3C 60. '<' */
437 _ACPI_PU, /* 0x3D 61. '=' */
438 _ACPI_PU, /* 0x3E 62. '>' */
439 _ACPI_PU, /* 0x3F 63. '?' */
440 _ACPI_PU, /* 0x40 64. '@' */
441 _ACPI_XD|_ACPI_UP, /* 0x41 65. 'A' */
442 _ACPI_XD|_ACPI_UP, /* 0x42 66. 'B' */
443 _ACPI_XD|_ACPI_UP, /* 0x43 67. 'C' */
444 _ACPI_XD|_ACPI_UP, /* 0x44 68. 'D' */
445 _ACPI_XD|_ACPI_UP, /* 0x45 69. 'E' */
446 _ACPI_XD|_ACPI_UP, /* 0x46 70. 'F' */
447 _ACPI_UP, /* 0x47 71. 'G' */
448 _ACPI_UP, /* 0x48 72. 'H' */
449 _ACPI_UP, /* 0x49 73. 'I' */
450 _ACPI_UP, /* 0x4A 74. 'J' */
451 _ACPI_UP, /* 0x4B 75. 'K' */
452 _ACPI_UP, /* 0x4C 76. 'L' */
453 _ACPI_UP, /* 0x4D 77. 'M' */
454 _ACPI_UP, /* 0x4E 78. 'N' */
455 _ACPI_UP, /* 0x4F 79. 'O' */
456 _ACPI_UP, /* 0x50 80. 'P' */
457 _ACPI_UP, /* 0x51 81. 'Q' */
458 _ACPI_UP, /* 0x52 82. 'R' */
459 _ACPI_UP, /* 0x53 83. 'S' */
460 _ACPI_UP, /* 0x54 84. 'T' */
461 _ACPI_UP, /* 0x55 85. 'U' */
462 _ACPI_UP, /* 0x56 86. 'V' */
463 _ACPI_UP, /* 0x57 87. 'W' */
464 _ACPI_UP, /* 0x58 88. 'X' */
465 _ACPI_UP, /* 0x59 89. 'Y' */
466 _ACPI_UP, /* 0x5A 90. 'Z' */
467 _ACPI_PU, /* 0x5B 91. '[' */
468 _ACPI_PU, /* 0x5C 92. '\' */
469 _ACPI_PU, /* 0x5D 93. ']' */
470 _ACPI_PU, /* 0x5E 94. '^' */
471 _ACPI_PU, /* 0x5F 95. '_' */
472 _ACPI_PU, /* 0x60 96. '`' */
473 _ACPI_XD|_ACPI_LO, /* 0x61 97. 'a' */
474 _ACPI_XD|_ACPI_LO, /* 0x62 98. 'b' */
475 _ACPI_XD|_ACPI_LO, /* 0x63 99. 'c' */
476 _ACPI_XD|_ACPI_LO, /* 0x64 100. 'd' */
477 _ACPI_XD|_ACPI_LO, /* 0x65 101. 'e' */
478 _ACPI_XD|_ACPI_LO, /* 0x66 102. 'f' */
479 _ACPI_LO, /* 0x67 103. 'g' */
480 _ACPI_LO, /* 0x68 104. 'h' */
481 _ACPI_LO, /* 0x69 105. 'i' */
482 _ACPI_LO, /* 0x6A 106. 'j' */
483 _ACPI_LO, /* 0x6B 107. 'k' */
484 _ACPI_LO, /* 0x6C 108. 'l' */
485 _ACPI_LO, /* 0x6D 109. 'm' */
486 _ACPI_LO, /* 0x6E 110. 'n' */
487 _ACPI_LO, /* 0x6F 111. 'o' */
488 _ACPI_LO, /* 0x70 112. 'p' */
489 _ACPI_LO, /* 0x71 113. 'q' */
490 _ACPI_LO, /* 0x72 114. 'r' */
491 _ACPI_LO, /* 0x73 115. 's' */
492 _ACPI_LO, /* 0x74 116. 't' */
493 _ACPI_LO, /* 0x75 117. 'u' */
494 _ACPI_LO, /* 0x76 118. 'v' */
495 _ACPI_LO, /* 0x77 119. 'w' */
496 _ACPI_LO, /* 0x78 120. 'x' */
497 _ACPI_LO, /* 0x79 121. 'y' */
498 _ACPI_LO, /* 0x7A 122. 'z' */
499 _ACPI_PU, /* 0x7B 123. '{' */
500 _ACPI_PU, /* 0x7C 124. '|' */
501 _ACPI_PU, /* 0x7D 125. '}' */
502 _ACPI_PU, /* 0x7E 126. '~' */
503 _ACPI_CN, /* 0x7F 127. */
504
505 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
506 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
507 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
508 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
510 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
511 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
512 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xF0 to 0x100 */
513 };
514
515 #define IS_UPPER(c) (_acpi_ctype[(unsigned char)(c)] & (_ACPI_UP))
516 #define IS_LOWER(c) (_acpi_ctype[(unsigned char)(c)] & (_ACPI_LO))
517 #define IS_DIGIT(c) (_acpi_ctype[(unsigned char)(c)] & (_ACPI_DI))
518 #define IS_SPACE(c) (_acpi_ctype[(unsigned char)(c)] & (_ACPI_SP))
519 #define IS_XDIGIT(c) (_acpi_ctype[(unsigned char)(c)] & (_ACPI_XD))
520
521
522 /*******************************************************************************
523 *
524 * FUNCTION: Acpi_cm_to_upper
525 *
526 * PARAMETERS:
527 *
528 * RETURN:
529 *
530 * DESCRIPTION: Convert character to uppercase
531 *
532 ******************************************************************************/
533
534 u32
535 acpi_cm_to_upper (
536 u32 c)
537 {
538
539 return (IS_LOWER(c) ? ((c)-0x20) : (c));
540 }
541
542
543 /*******************************************************************************
544 *
545 * FUNCTION: Acpi_cm_to_lower
546 *
547 * PARAMETERS:
548 *
549 * RETURN:
550 *
551 * DESCRIPTION: Convert character to lowercase
552 *
553 ******************************************************************************/
554
555 u32
556 acpi_cm_to_lower (
557 u32 c)
558 {
559
560 return (IS_UPPER(c) ? ((c)+0x20) : (c));
561 }
562
563
564 /*******************************************************************************
565 *
566 * FUNCTION: strupr
567 *
568 * PARAMETERS: Src_string - The source string to convert to
569 *
570 * RETURN: Src_string
571 *
572 * DESCRIPTION: Convert string to uppercase
573 *
574 ******************************************************************************/
575
576 NATIVE_CHAR *
577 acpi_cm_strupr (
578 NATIVE_CHAR *src_string)
579 {
580 NATIVE_CHAR *string;
581
582
583 /* Walk entire string, uppercasing the letters */
584
585 for (string = src_string; *string; ) {
586 *string = (char) acpi_cm_to_upper (*string);
587 string++;
588 }
589
590
591 return (src_string);
592 }
593
594
595 /*******************************************************************************
596 *
597 * FUNCTION: strstr
598 *
599 * PARAMETERS: String1 -
600 * String2
601 *
602 * RETURN:
603 *
604 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
605 * full implementation of strstr, only sufficient for command
606 * matching
607 *
608 ******************************************************************************/
609
610 NATIVE_CHAR *
611 acpi_cm_strstr (
612 NATIVE_CHAR *string1,
613 NATIVE_CHAR *string2)
614 {
615 NATIVE_CHAR *string;
616
617
618 if (acpi_cm_strlen (string2) > acpi_cm_strlen (string1)) {
619 return (NULL);
620 }
621
622 /* Walk entire string, comparing the letters */
623
624 for (string = string1; *string2; ) {
625 if (*string2 != *string) {
626 return (NULL);
627 }
628
629 string2++;
630 string++;
631 }
632
633
634 return (string1);
635 }
636
637
638 /*******************************************************************************
639 *
640 * FUNCTION: strtoul
641 *
642 * PARAMETERS: String - Null terminated string
643 * Terminater - Where a pointer to the terminating byte is returned
644 * Base - Radix of the string
645 *
646 * RETURN: Converted value
647 *
648 * DESCRIPTION: Convert a string into an unsigned value.
649 *
650 ******************************************************************************/
651
652 NATIVE_UINT
653 acpi_cm_strtoul (
654 const NATIVE_CHAR *string,
655 NATIVE_CHAR **terminator,
656 NATIVE_UINT base)
657 {
658 u32 converted = 0;
659 u32 index;
660 u32 sign;
661 const NATIVE_CHAR *string_start;
662 NATIVE_UINT return_value = 0;
663 ACPI_STATUS status = AE_OK;
664
665
666 /*
667 * Save the value of the pointer to the buffer's first
668 * character, save the current errno value, and then
669 * skip over any white space in the buffer:
670 */
671 string_start = string;
672 while (IS_SPACE (*string) || *string == '\t') {
673 ++string;
674 }
675
676 /*
677 * The buffer may contain an optional plus or minus sign.
678 * If it does, then skip over it but remember what is was:
679 */
680 if (*string == '-') {
681 sign = NEGATIVE;
682 ++string;
683 }
684
685 else if (*string == '+') {
686 ++string;
687 sign = POSITIVE;
688 }
689
690 else {
691 sign = POSITIVE;
692 }
693
694 /*
695 * If the input parameter Base is zero, then we need to
696 * determine if it is octal, decimal, or hexadecimal:
697 */
698 if (base == 0) {
699 if (*string == '0') {
700 if (acpi_cm_to_lower (*(++string)) == 'x') {
701 base = 16;
702 ++string;
703 }
704
705 else {
706 base = 8;
707 }
708 }
709
710 else {
711 base = 10;
712 }
713 }
714
715 else if (base < 2 || base > 36) {
716 /*
717 * The specified Base parameter is not in the domain of
718 * this function:
719 */
720 goto done;
721 }
722
723 /*
724 * For octal and hexadecimal bases, skip over the leading
725 * 0 or 0x, if they are present.
726 */
727 if (base == 8 && *string == '0') {
728 string++;
729 }
730
731 if (base == 16 &&
732 *string == '0' &&
733 acpi_cm_to_lower (*(++string)) == 'x') {
734 string++;
735 }
736
737
738 /*
739 * Main loop: convert the string to an unsigned long:
740 */
741 while (*string) {
742 if (IS_DIGIT (*string)) {
743 index = *string - '0';
744 }
745
746 else {
747 index = acpi_cm_to_upper (*string);
748 if (IS_UPPER (index)) {
749 index = index - 'A' + 10;
750 }
751
752 else {
753 goto done;
754 }
755 }
756
757 if (index >= base) {
758 goto done;
759 }
760
761 /*
762 * Check to see if value is out of range:
763 */
764
765 if (return_value > ((ACPI_UINT32_MAX - (u32) index) /
766 (u32) base)) {
767 status = AE_ERROR;
768 return_value = 0L; /* reset */
769 }
770
771 else {
772 return_value *= base;
773 return_value += index;
774 converted = 1;
775 }
776
777 ++string;
778 }
779
780 done:
781 /*
782 * If appropriate, update the caller's pointer to the next
783 * unconverted character in the buffer.
784 */
785 if (terminator) {
786 if (converted == 0 && return_value == 0L && string != NULL) {
787 *terminator = (NATIVE_CHAR *) string_start;
788 }
789
790 else {
791 *terminator = (NATIVE_CHAR *) string;
792 }
793 }
794
795 if (status == AE_ERROR) {
796 return_value = ACPI_UINT32_MAX;
797 }
798
799 /*
800 * If a minus sign was present, then "the conversion is negated":
801 */
802 if (sign == NEGATIVE) {
803 return_value = (ACPI_UINT32_MAX - return_value) + 1;
804 }
805
806 return (return_value);
807 }
808
809 #endif /* ACPI_USE_SYSTEM_CLIBRARY */
810