utnonansi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*******************************************************************************
  2. *
  3. * Module Name: utnonansi - Non-ansi C library functions
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utnonansi")
  46. /*
  47. * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
  48. * string functions.
  49. */
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ut_strlwr (strlwr)
  53. *
  54. * PARAMETERS: src_string - The source string to convert
  55. *
  56. * RETURN: None
  57. *
  58. * DESCRIPTION: Convert a string to lowercase
  59. *
  60. ******************************************************************************/
  61. void acpi_ut_strlwr(char *src_string)
  62. {
  63. char *string;
  64. ACPI_FUNCTION_ENTRY();
  65. if (!src_string) {
  66. return;
  67. }
  68. /* Walk entire string, lowercasing the letters */
  69. for (string = src_string; *string; string++) {
  70. *string = (char)tolower((int)*string);
  71. }
  72. }
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ut_strupr (strupr)
  76. *
  77. * PARAMETERS: src_string - The source string to convert
  78. *
  79. * RETURN: None
  80. *
  81. * DESCRIPTION: Convert a string to uppercase
  82. *
  83. ******************************************************************************/
  84. void acpi_ut_strupr(char *src_string)
  85. {
  86. char *string;
  87. ACPI_FUNCTION_ENTRY();
  88. if (!src_string) {
  89. return;
  90. }
  91. /* Walk entire string, uppercasing the letters */
  92. for (string = src_string; *string; string++) {
  93. *string = (char)toupper((int)*string);
  94. }
  95. }
  96. /******************************************************************************
  97. *
  98. * FUNCTION: acpi_ut_stricmp (stricmp)
  99. *
  100. * PARAMETERS: string1 - first string to compare
  101. * string2 - second string to compare
  102. *
  103. * RETURN: int that signifies string relationship. Zero means strings
  104. * are equal.
  105. *
  106. * DESCRIPTION: Case-insensitive string compare. Implementation of the
  107. * non-ANSI stricmp function.
  108. *
  109. ******************************************************************************/
  110. int acpi_ut_stricmp(char *string1, char *string2)
  111. {
  112. int c1;
  113. int c2;
  114. do {
  115. c1 = tolower((int)*string1);
  116. c2 = tolower((int)*string2);
  117. string1++;
  118. string2++;
  119. }
  120. while ((c1 == c2) && (c1));
  121. return (c1 - c2);
  122. }
  123. #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
  127. *
  128. * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
  129. * functions. This is the size of the Destination buffer.
  130. *
  131. * RETURN: TRUE if the operation would overflow the destination buffer.
  132. *
  133. * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
  134. * the result of the operation will not overflow the output string
  135. * buffer.
  136. *
  137. * NOTE: These functions are typically only helpful for processing
  138. * user input and command lines. For most ACPICA code, the
  139. * required buffer length is precisely calculated before buffer
  140. * allocation, so the use of these functions is unnecessary.
  141. *
  142. ******************************************************************************/
  143. u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
  144. {
  145. if (strlen(source) >= dest_size) {
  146. return (TRUE);
  147. }
  148. strcpy(dest, source);
  149. return (FALSE);
  150. }
  151. u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
  152. {
  153. if ((strlen(dest) + strlen(source)) >= dest_size) {
  154. return (TRUE);
  155. }
  156. strcat(dest, source);
  157. return (FALSE);
  158. }
  159. u8
  160. acpi_ut_safe_strncat(char *dest,
  161. acpi_size dest_size,
  162. char *source, acpi_size max_transfer_length)
  163. {
  164. acpi_size actual_transfer_length;
  165. actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
  166. if ((strlen(dest) + actual_transfer_length) >= dest_size) {
  167. return (TRUE);
  168. }
  169. strncat(dest, source, max_transfer_length);
  170. return (FALSE);
  171. }
  172. #endif