1 /******************************************************************************
3 * Module Name: cmclib - Local implementation of C library functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
48 * These implementations of standard C Library routines can optionally be
49 * used if a C library is not available. In general, they are less efficient
50 * than an inline or assembly implementation
53 #define _COMPONENT ACPI_UTILITIES
54 ACPI_MODULE_NAME("cmclib")
56 #ifndef ACPI_USE_SYSTEM_CLIBRARY
59 /*******************************************************************************
61 * FUNCTION: acpi_ut_memcmp (memcmp)
63 * PARAMETERS: buffer1 - First Buffer
64 * buffer2 - Second Buffer
65 * count - Maximum # of bytes to compare
67 * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
69 * DESCRIPTION: Compare two Buffers, with a maximum length
71 ******************************************************************************/
72 int acpi_ut_memcmp(const char *buffer1, const char *buffer2, acpi_size count)
75 return ((count == ACPI_SIZE_MAX) ? 0 : ((unsigned char)*buffer1 -
76 (unsigned char)*buffer2));
79 /*******************************************************************************
81 * FUNCTION: acpi_ut_memcpy (memcpy)
83 * PARAMETERS: dest - Target of the copy
84 * src - Source buffer to copy
85 * count - Number of bytes to copy
89 * DESCRIPTION: Copy arbitrary bytes of memory
91 ******************************************************************************/
93 void *acpi_ut_memcpy(void *dest, const void *src, acpi_size count)
95 char *new = (char *)dest;
96 char *old = (char *)src;
108 /*******************************************************************************
110 * FUNCTION: acpi_ut_memset (memset)
112 * PARAMETERS: dest - Buffer to set
113 * value - Value to set each byte of memory
114 * count - Number of bytes to set
118 * DESCRIPTION: Initialize a buffer to a known value.
120 ******************************************************************************/
122 void *acpi_ut_memset(void *dest, u8 value, acpi_size count)
124 char *new = (char *)dest;
135 /*******************************************************************************
137 * FUNCTION: acpi_ut_strlen (strlen)
139 * PARAMETERS: string - Null terminated string
143 * DESCRIPTION: Returns the length of the input string
145 ******************************************************************************/
147 acpi_size acpi_ut_strlen(const char *string)
151 /* Count the string until a null is encountered */
161 /*******************************************************************************
163 * FUNCTION: acpi_ut_strcpy (strcpy)
165 * PARAMETERS: dst_string - Target of the copy
166 * src_string - The source string to copy
170 * DESCRIPTION: Copy a null terminated string
172 ******************************************************************************/
174 char *acpi_ut_strcpy(char *dst_string, const char *src_string)
176 char *string = dst_string;
178 /* Move bytes brute force */
180 while (*src_string) {
181 *string = *src_string;
193 /*******************************************************************************
195 * FUNCTION: acpi_ut_strncpy (strncpy)
197 * PARAMETERS: dst_string - Target of the copy
198 * src_string - The source string to copy
199 * count - Maximum # of bytes to copy
203 * DESCRIPTION: Copy a null terminated string, with a maximum length
205 ******************************************************************************/
207 char *acpi_ut_strncpy(char *dst_string, const char *src_string, acpi_size count)
209 char *string = dst_string;
211 /* Copy the string */
213 for (string = dst_string;
214 count && (count--, (*string++ = *src_string++));) {;
217 /* Pad with nulls if necessary */
224 /* Return original pointer */
229 /*******************************************************************************
231 * FUNCTION: acpi_ut_strcmp (strcmp)
233 * PARAMETERS: string1 - First string
234 * string2 - Second string
236 * RETURN: Index where strings mismatched, or 0 if strings matched
238 * DESCRIPTION: Compare two null terminated strings
240 ******************************************************************************/
242 int acpi_ut_strcmp(const char *string1, const char *string2)
245 for (; (*string1 == *string2); string2++) {
251 return ((unsigned char)*string1 - (unsigned char)*string2);
254 #ifdef ACPI_FUTURE_IMPLEMENTATION
255 /* Not used at this time */
256 /*******************************************************************************
258 * FUNCTION: acpi_ut_strchr (strchr)
260 * PARAMETERS: string - Search string
261 * ch - character to search for
263 * RETURN: Ptr to char or NULL if not found
265 * DESCRIPTION: Search a string for a character
267 ******************************************************************************/
269 char *acpi_ut_strchr(const char *string, int ch)
272 for (; (*string); string++) {
273 if ((*string) == (char)ch) {
274 return ((char *)string);
282 /*******************************************************************************
284 * FUNCTION: acpi_ut_strncmp (strncmp)
286 * PARAMETERS: string1 - First string
287 * string2 - Second string
288 * count - Maximum # of bytes to compare
290 * RETURN: Index where strings mismatched, or 0 if strings matched
292 * DESCRIPTION: Compare two null terminated strings, with a maximum length
294 ******************************************************************************/
296 int acpi_ut_strncmp(const char *string1, const char *string2, acpi_size count)
299 for (; count-- && (*string1 == *string2); string2++) {
305 return ((count == ACPI_SIZE_MAX) ? 0 : ((unsigned char)*string1 -
306 (unsigned char)*string2));
309 /*******************************************************************************
311 * FUNCTION: acpi_ut_strcat (Strcat)
313 * PARAMETERS: dst_string - Target of the copy
314 * src_string - The source string to copy
318 * DESCRIPTION: Append a null terminated string to a null terminated string
320 ******************************************************************************/
322 char *acpi_ut_strcat(char *dst_string, const char *src_string)
326 /* Find end of the destination string */
328 for (string = dst_string; *string++;) {;
331 /* Concatenate the string */
333 for (--string; (*string++ = *src_string++);) {;
339 /*******************************************************************************
341 * FUNCTION: acpi_ut_strncat (strncat)
343 * PARAMETERS: dst_string - Target of the copy
344 * src_string - The source string to copy
345 * count - Maximum # of bytes to copy
349 * DESCRIPTION: Append a null terminated string to a null terminated string,
350 * with a maximum count.
352 ******************************************************************************/
354 char *acpi_ut_strncat(char *dst_string, const char *src_string, acpi_size count)
360 /* Find end of the destination string */
362 for (string = dst_string; *string++;) {;
365 /* Concatenate the string */
367 for (--string; (*string++ = *src_string++) && --count;) {;
370 /* Null terminate if necessary */
380 /*******************************************************************************
382 * FUNCTION: acpi_ut_strstr (strstr)
384 * PARAMETERS: string1 - Target string
385 * string2 - Substring to search for
387 * RETURN: Where substring match starts, Null if no match found
389 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
390 * full implementation of strstr, only sufficient for command
393 ******************************************************************************/
395 char *acpi_ut_strstr(char *string1, char *string2)
399 if (acpi_ut_strlen(string2) > acpi_ut_strlen(string1)) {
403 /* Walk entire string, comparing the letters */
405 for (string = string1; *string2;) {
406 if (*string2 != *string) {
417 /*******************************************************************************
419 * FUNCTION: acpi_ut_strtoul (strtoul)
421 * PARAMETERS: string - Null terminated string
422 * terminater - Where a pointer to the terminating byte is
424 * base - Radix of the string
426 * RETURN: Converted value
428 * DESCRIPTION: Convert a string into a 32-bit unsigned value.
429 * Note: use acpi_ut_strtoul64 for 64-bit integers.
431 ******************************************************************************/
433 u32 acpi_ut_strtoul(const char *string, char **terminator, u32 base)
438 const char *string_start;
439 u32 return_value = 0;
440 acpi_status status = AE_OK;
443 * Save the value of the pointer to the buffer's first
444 * character, save the current errno value, and then
445 * skip over any white space in the buffer:
447 string_start = string;
448 while (ACPI_IS_SPACE(*string) || *string == '\t') {
453 * The buffer may contain an optional plus or minus sign.
454 * If it does, then skip over it but remember what is was:
456 if (*string == '-') {
459 } else if (*string == '+') {
467 * If the input parameter Base is zero, then we need to
468 * determine if it is octal, decimal, or hexadecimal:
471 if (*string == '0') {
472 if (acpi_ut_to_lower(*(++string)) == 'x') {
481 } else if (base < 2 || base > 36) {
483 * The specified Base parameter is not in the domain of
490 * For octal and hexadecimal bases, skip over the leading
491 * 0 or 0x, if they are present.
493 if (base == 8 && *string == '0') {
498 *string == '0' && acpi_ut_to_lower(*(++string)) == 'x') {
503 * Main loop: convert the string to an unsigned long:
506 if (ACPI_IS_DIGIT(*string)) {
507 index = (u32)((u8)*string - '0');
509 index = (u32)acpi_ut_to_upper(*string);
510 if (ACPI_IS_UPPER(index)) {
511 index = index - 'A' + 10;
522 * Check to see if value is out of range:
525 if (return_value > ((ACPI_UINT32_MAX - (u32)index) / (u32)base)) {
527 return_value = 0; /* reset */
529 return_value *= base;
530 return_value += index;
539 * If appropriate, update the caller's pointer to the next
540 * unconverted character in the buffer.
543 if (converted == 0 && return_value == 0 && string != NULL) {
544 *terminator = (char *)string_start;
546 *terminator = (char *)string;
550 if (status == AE_ERROR) {
551 return_value = ACPI_UINT32_MAX;
555 * If a minus sign was present, then "the conversion is negated":
557 if (sign == NEGATIVE) {
558 return_value = (ACPI_UINT32_MAX - return_value) + 1;
561 return (return_value);
564 /*******************************************************************************
566 * FUNCTION: acpi_ut_to_upper (TOUPPER)
568 * PARAMETERS: c - Character to convert
570 * RETURN: Converted character as an int
572 * DESCRIPTION: Convert character to uppercase
574 ******************************************************************************/
576 int acpi_ut_to_upper(int c)
579 return (ACPI_IS_LOWER(c) ? ((c) - 0x20) : (c));
582 /*******************************************************************************
584 * FUNCTION: acpi_ut_to_lower (TOLOWER)
586 * PARAMETERS: c - Character to convert
588 * RETURN: Converted character as an int
590 * DESCRIPTION: Convert character to lowercase
592 ******************************************************************************/
594 int acpi_ut_to_lower(int c)
597 return (ACPI_IS_UPPER(c) ? ((c) + 0x20) : (c));
600 /*******************************************************************************
602 * FUNCTION: is* functions
604 * DESCRIPTION: is* functions use the ctype table below
606 ******************************************************************************/
608 const u8 _acpi_ctype[257] = {
609 _ACPI_CN, /* 0x00 0 NUL */
610 _ACPI_CN, /* 0x01 1 SOH */
611 _ACPI_CN, /* 0x02 2 STX */
612 _ACPI_CN, /* 0x03 3 ETX */
613 _ACPI_CN, /* 0x04 4 EOT */
614 _ACPI_CN, /* 0x05 5 ENQ */
615 _ACPI_CN, /* 0x06 6 ACK */
616 _ACPI_CN, /* 0x07 7 BEL */
617 _ACPI_CN, /* 0x08 8 BS */
618 _ACPI_CN | _ACPI_SP, /* 0x09 9 TAB */
619 _ACPI_CN | _ACPI_SP, /* 0x0A 10 LF */
620 _ACPI_CN | _ACPI_SP, /* 0x0B 11 VT */
621 _ACPI_CN | _ACPI_SP, /* 0x0C 12 FF */
622 _ACPI_CN | _ACPI_SP, /* 0x0D 13 CR */
623 _ACPI_CN, /* 0x0E 14 SO */
624 _ACPI_CN, /* 0x0F 15 SI */
625 _ACPI_CN, /* 0x10 16 DLE */
626 _ACPI_CN, /* 0x11 17 DC1 */
627 _ACPI_CN, /* 0x12 18 DC2 */
628 _ACPI_CN, /* 0x13 19 DC3 */
629 _ACPI_CN, /* 0x14 20 DC4 */
630 _ACPI_CN, /* 0x15 21 NAK */
631 _ACPI_CN, /* 0x16 22 SYN */
632 _ACPI_CN, /* 0x17 23 ETB */
633 _ACPI_CN, /* 0x18 24 CAN */
634 _ACPI_CN, /* 0x19 25 EM */
635 _ACPI_CN, /* 0x1A 26 SUB */
636 _ACPI_CN, /* 0x1B 27 ESC */
637 _ACPI_CN, /* 0x1C 28 FS */
638 _ACPI_CN, /* 0x1D 29 GS */
639 _ACPI_CN, /* 0x1E 30 RS */
640 _ACPI_CN, /* 0x1F 31 US */
641 _ACPI_XS | _ACPI_SP, /* 0x20 32 ' ' */
642 _ACPI_PU, /* 0x21 33 '!' */
643 _ACPI_PU, /* 0x22 34 '"' */
644 _ACPI_PU, /* 0x23 35 '#' */
645 _ACPI_PU, /* 0x24 36 '$' */
646 _ACPI_PU, /* 0x25 37 '%' */
647 _ACPI_PU, /* 0x26 38 '&' */
648 _ACPI_PU, /* 0x27 39 ''' */
649 _ACPI_PU, /* 0x28 40 '(' */
650 _ACPI_PU, /* 0x29 41 ')' */
651 _ACPI_PU, /* 0x2A 42 '*' */
652 _ACPI_PU, /* 0x2B 43 '+' */
653 _ACPI_PU, /* 0x2C 44 ',' */
654 _ACPI_PU, /* 0x2D 45 '-' */
655 _ACPI_PU, /* 0x2E 46 '.' */
656 _ACPI_PU, /* 0x2F 47 '/' */
657 _ACPI_XD | _ACPI_DI, /* 0x30 48 '0' */
658 _ACPI_XD | _ACPI_DI, /* 0x31 49 '1' */
659 _ACPI_XD | _ACPI_DI, /* 0x32 50 '2' */
660 _ACPI_XD | _ACPI_DI, /* 0x33 51 '3' */
661 _ACPI_XD | _ACPI_DI, /* 0x34 52 '4' */
662 _ACPI_XD | _ACPI_DI, /* 0x35 53 '5' */
663 _ACPI_XD | _ACPI_DI, /* 0x36 54 '6' */
664 _ACPI_XD | _ACPI_DI, /* 0x37 55 '7' */
665 _ACPI_XD | _ACPI_DI, /* 0x38 56 '8' */
666 _ACPI_XD | _ACPI_DI, /* 0x39 57 '9' */
667 _ACPI_PU, /* 0x3A 58 ':' */
668 _ACPI_PU, /* 0x3B 59 ';' */
669 _ACPI_PU, /* 0x3C 60 '<' */
670 _ACPI_PU, /* 0x3D 61 '=' */
671 _ACPI_PU, /* 0x3E 62 '>' */
672 _ACPI_PU, /* 0x3F 63 '?' */
673 _ACPI_PU, /* 0x40 64 '@' */
674 _ACPI_XD | _ACPI_UP, /* 0x41 65 'A' */
675 _ACPI_XD | _ACPI_UP, /* 0x42 66 'B' */
676 _ACPI_XD | _ACPI_UP, /* 0x43 67 'C' */
677 _ACPI_XD | _ACPI_UP, /* 0x44 68 'D' */
678 _ACPI_XD | _ACPI_UP, /* 0x45 69 'E' */
679 _ACPI_XD | _ACPI_UP, /* 0x46 70 'F' */
680 _ACPI_UP, /* 0x47 71 'G' */
681 _ACPI_UP, /* 0x48 72 'H' */
682 _ACPI_UP, /* 0x49 73 'I' */
683 _ACPI_UP, /* 0x4A 74 'J' */
684 _ACPI_UP, /* 0x4B 75 'K' */
685 _ACPI_UP, /* 0x4C 76 'L' */
686 _ACPI_UP, /* 0x4D 77 'M' */
687 _ACPI_UP, /* 0x4E 78 'N' */
688 _ACPI_UP, /* 0x4F 79 'O' */
689 _ACPI_UP, /* 0x50 80 'P' */
690 _ACPI_UP, /* 0x51 81 'Q' */
691 _ACPI_UP, /* 0x52 82 'R' */
692 _ACPI_UP, /* 0x53 83 'S' */
693 _ACPI_UP, /* 0x54 84 'T' */
694 _ACPI_UP, /* 0x55 85 'U' */
695 _ACPI_UP, /* 0x56 86 'V' */
696 _ACPI_UP, /* 0x57 87 'W' */
697 _ACPI_UP, /* 0x58 88 'X' */
698 _ACPI_UP, /* 0x59 89 'Y' */
699 _ACPI_UP, /* 0x5A 90 'Z' */
700 _ACPI_PU, /* 0x5B 91 '[' */
701 _ACPI_PU, /* 0x5C 92 '\' */
702 _ACPI_PU, /* 0x5D 93 ']' */
703 _ACPI_PU, /* 0x5E 94 '^' */
704 _ACPI_PU, /* 0x5F 95 '_' */
705 _ACPI_PU, /* 0x60 96 '`' */
706 _ACPI_XD | _ACPI_LO, /* 0x61 97 'a' */
707 _ACPI_XD | _ACPI_LO, /* 0x62 98 'b' */
708 _ACPI_XD | _ACPI_LO, /* 0x63 99 'c' */
709 _ACPI_XD | _ACPI_LO, /* 0x64 100 'd' */
710 _ACPI_XD | _ACPI_LO, /* 0x65 101 'e' */
711 _ACPI_XD | _ACPI_LO, /* 0x66 102 'f' */
712 _ACPI_LO, /* 0x67 103 'g' */
713 _ACPI_LO, /* 0x68 104 'h' */
714 _ACPI_LO, /* 0x69 105 'i' */
715 _ACPI_LO, /* 0x6A 106 'j' */
716 _ACPI_LO, /* 0x6B 107 'k' */
717 _ACPI_LO, /* 0x6C 108 'l' */
718 _ACPI_LO, /* 0x6D 109 'm' */
719 _ACPI_LO, /* 0x6E 110 'n' */
720 _ACPI_LO, /* 0x6F 111 'o' */
721 _ACPI_LO, /* 0x70 112 'p' */
722 _ACPI_LO, /* 0x71 113 'q' */
723 _ACPI_LO, /* 0x72 114 'r' */
724 _ACPI_LO, /* 0x73 115 's' */
725 _ACPI_LO, /* 0x74 116 't' */
726 _ACPI_LO, /* 0x75 117 'u' */
727 _ACPI_LO, /* 0x76 118 'v' */
728 _ACPI_LO, /* 0x77 119 'w' */
729 _ACPI_LO, /* 0x78 120 'x' */
730 _ACPI_LO, /* 0x79 121 'y' */
731 _ACPI_LO, /* 0x7A 122 'z' */
732 _ACPI_PU, /* 0x7B 123 '{' */
733 _ACPI_PU, /* 0x7C 124 '|' */
734 _ACPI_PU, /* 0x7D 125 '}' */
735 _ACPI_PU, /* 0x7E 126 '~' */
736 _ACPI_CN, /* 0x7F 127 DEL */
738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 to 0x8F */
739 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 to 0x9F */
740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 to 0xAF */
741 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 to 0xBF */
742 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 to 0xCF */
743 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 to 0xDF */
744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE0 to 0xEF */
745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xF0 to 0xFF */
749 #endif /* ACPI_USE_SYSTEM_CLIBRARY */