1 /******************************************************************************
3 * Module Name: cmclib - Local implementation of C library functions
6 *****************************************************************************/
9 * Copyright (C) 2000, 2001 R. Byron Moore
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.
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.
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
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
35 #define _COMPONENT MISCELLANEOUS
36 MODULE_NAME ("cmclib")
39 #ifndef ACPI_USE_SYSTEM_CLIBRARY
41 /*******************************************************************************
45 * PARAMETERS: String - Null terminated string
49 * DESCRIPTION: Returns the length of the input string
51 ******************************************************************************/
56 const NATIVE_CHAR
*string
)
61 /* Count the string until a null is encountered */
72 /*******************************************************************************
76 * PARAMETERS: Dst_string - Target of the copy
77 * Src_string - The source string to copy
81 * DESCRIPTION: Copy a null terminated string
83 ******************************************************************************/
87 NATIVE_CHAR
*dst_string
,
88 const NATIVE_CHAR
*src_string
)
90 NATIVE_CHAR
*string
= dst_string
;
93 /* Move bytes brute force */
96 *string
= *src_string
;
110 /*******************************************************************************
114 * PARAMETERS: Dst_string - Target of the copy
115 * Src_string - The source string to copy
116 * Count - Maximum # of bytes to copy
120 * DESCRIPTION: Copy a null terminated string, with a maximum length
122 ******************************************************************************/
126 NATIVE_CHAR
*dst_string
,
127 const NATIVE_CHAR
*src_string
,
130 NATIVE_CHAR
*string
= dst_string
;
133 /* Copy the string */
135 for (string
= dst_string
;
136 count
&& (count
--, (*string
++ = *src_string
++)); ) {;}
138 /* Pad with nulls if necessary */
145 /* Return original pointer */
151 /*******************************************************************************
155 * PARAMETERS: String1 - First string
156 * String2 - Second string
158 * RETURN: Index where strings mismatched, or 0 if strings matched
160 * DESCRIPTION: Compare two null terminated strings
162 ******************************************************************************/
166 const NATIVE_CHAR
*string1
,
167 const NATIVE_CHAR
*string2
)
171 for ( ; (*string1
== *string2
); string2
++) {
178 return ((unsigned char) *string1
- (unsigned char) *string2
);
182 /*******************************************************************************
186 * PARAMETERS: String1 - First string
187 * String2 - Second string
188 * Count - Maximum # of bytes to compare
190 * RETURN: Index where strings mismatched, or 0 if strings matched
192 * DESCRIPTION: Compare two null terminated strings, with a maximum length
194 ******************************************************************************/
198 const NATIVE_CHAR
*string1
,
199 const NATIVE_CHAR
*string2
,
204 for ( ; count
-- && (*string1
== *string2
); string2
++) {
210 return ((count
== -1) ? 0 : ((unsigned char) *string1
-
211 (unsigned char) *string2
));
215 /*******************************************************************************
219 * PARAMETERS: Dst_string - Target of the copy
220 * Src_string - The source string to copy
224 * DESCRIPTION: Append a null terminated string to a null terminated string
226 ******************************************************************************/
230 NATIVE_CHAR
*dst_string
,
231 const NATIVE_CHAR
*src_string
)
236 /* Find end of the destination string */
238 for (string
= dst_string
; *string
++; ) { ; }
240 /* Concatinate the string */
242 for (--string
; (*string
++ = *src_string
++); ) { ; }
248 /*******************************************************************************
252 * PARAMETERS: Dst_string - Target of the copy
253 * Src_string - The source string to copy
254 * Count - Maximum # of bytes to copy
258 * DESCRIPTION: Append a null terminated string to a null terminated string,
259 * with a maximum count.
261 ******************************************************************************/
265 NATIVE_CHAR
*dst_string
,
266 const NATIVE_CHAR
*src_string
,
273 /* Find end of the destination string */
275 for (string
= dst_string
; *string
++; ) { ; }
277 /* Concatinate the string */
279 for (--string
; (*string
++ = *src_string
++) && --count
; ) { ; }
281 /* Null terminate if necessary */
292 /*******************************************************************************
296 * PARAMETERS: Dest - Target of the copy
297 * Src - Source buffer to copy
298 * Count - Number of bytes to copy
302 * DESCRIPTION: Copy arbitrary bytes of memory
304 ******************************************************************************/
312 NATIVE_CHAR
*new = (NATIVE_CHAR
*) dest
;
313 NATIVE_CHAR
*old
= (NATIVE_CHAR
*) src
;
327 /*******************************************************************************
331 * PARAMETERS: Dest - Buffer to set
332 * Value - Value to set each byte of memory
333 * Count - Number of bytes to set
337 * DESCRIPTION: Initialize a buffer to a known value.
339 ******************************************************************************/
347 NATIVE_CHAR
*new = (NATIVE_CHAR
*) dest
;
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' */
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. */
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 */
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))
522 /*******************************************************************************
524 * FUNCTION: Acpi_cm_to_upper
530 * DESCRIPTION: Convert character to uppercase
532 ******************************************************************************/
539 return (IS_LOWER(c
) ? ((c
)-0x20) : (c
));
543 /*******************************************************************************
545 * FUNCTION: Acpi_cm_to_lower
551 * DESCRIPTION: Convert character to lowercase
553 ******************************************************************************/
560 return (IS_UPPER(c
) ? ((c
)+0x20) : (c
));
564 /*******************************************************************************
568 * PARAMETERS: Src_string - The source string to convert to
572 * DESCRIPTION: Convert string to uppercase
574 ******************************************************************************/
578 NATIVE_CHAR
*src_string
)
583 /* Walk entire string, uppercasing the letters */
585 for (string
= src_string
; *string
; ) {
586 *string
= (char) acpi_cm_to_upper (*string
);
595 /*******************************************************************************
599 * PARAMETERS: String1 -
604 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
605 * full implementation of strstr, only sufficient for command
608 ******************************************************************************/
612 NATIVE_CHAR
*string1
,
613 NATIVE_CHAR
*string2
)
618 if (acpi_cm_strlen (string2
) > acpi_cm_strlen (string1
)) {
622 /* Walk entire string, comparing the letters */
624 for (string
= string1
; *string2
; ) {
625 if (*string2
!= *string
) {
638 /*******************************************************************************
642 * PARAMETERS: String - Null terminated string
643 * Terminater - Where a pointer to the terminating byte is returned
644 * Base - Radix of the string
646 * RETURN: Converted value
648 * DESCRIPTION: Convert a string into an unsigned value.
650 ******************************************************************************/
654 const NATIVE_CHAR
*string
,
655 NATIVE_CHAR
**terminator
,
661 const NATIVE_CHAR
*string_start
;
662 NATIVE_UINT return_value
= 0;
663 ACPI_STATUS status
= AE_OK
;
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:
671 string_start
= string
;
672 while (IS_SPACE (*string
) || *string
== '\t') {
677 * The buffer may contain an optional plus or minus sign.
678 * If it does, then skip over it but remember what is was:
680 if (*string
== '-') {
685 else if (*string
== '+') {
695 * If the input parameter Base is zero, then we need to
696 * determine if it is octal, decimal, or hexadecimal:
699 if (*string
== '0') {
700 if (acpi_cm_to_lower (*(++string
)) == 'x') {
715 else if (base
< 2 || base
> 36) {
717 * The specified Base parameter is not in the domain of
724 * For octal and hexadecimal bases, skip over the leading
725 * 0 or 0x, if they are present.
727 if (base
== 8 && *string
== '0') {
733 acpi_cm_to_lower (*(++string
)) == 'x') {
739 * Main loop: convert the string to an unsigned long:
742 if (IS_DIGIT (*string
)) {
743 index
= *string
- '0';
747 index
= acpi_cm_to_upper (*string
);
748 if (IS_UPPER (index
)) {
749 index
= index
- 'A' + 10;
762 * Check to see if value is out of range:
765 if (return_value
> ((ACPI_UINT32_MAX
- (u32
) index
) /
768 return_value
= 0L; /* reset */
772 return_value
*= base
;
773 return_value
+= index
;
782 * If appropriate, update the caller's pointer to the next
783 * unconverted character in the buffer.
786 if (converted
== 0 && return_value
== 0L && string
!= NULL
) {
787 *terminator
= (NATIVE_CHAR
*) string_start
;
791 *terminator
= (NATIVE_CHAR
*) string
;
795 if (status
== AE_ERROR
) {
796 return_value
= ACPI_UINT32_MAX
;
800 * If a minus sign was present, then "the conversion is negated":
802 if (sign
== NEGATIVE
) {
803 return_value
= (ACPI_UINT32_MAX
- return_value
) + 1;
806 return (return_value
);
809 #endif /* ACPI_USE_SYSTEM_CLIBRARY */