utnonansi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utnonansi - Non-ansi C library functions
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #define _COMPONENT ACPI_UTILITIES
  10. ACPI_MODULE_NAME("utnonansi")
  11. /*
  12. * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
  13. * string functions.
  14. */
  15. /*******************************************************************************
  16. *
  17. * FUNCTION: acpi_ut_strlwr (strlwr)
  18. *
  19. * PARAMETERS: src_string - The source string to convert
  20. *
  21. * RETURN: None
  22. *
  23. * DESCRIPTION: Convert a string to lowercase
  24. *
  25. ******************************************************************************/
  26. void acpi_ut_strlwr(char *src_string)
  27. {
  28. char *string;
  29. ACPI_FUNCTION_ENTRY();
  30. if (!src_string) {
  31. return;
  32. }
  33. /* Walk entire string, lowercasing the letters */
  34. for (string = src_string; *string; string++) {
  35. *string = (char)tolower((int)*string);
  36. }
  37. }
  38. /*******************************************************************************
  39. *
  40. * FUNCTION: acpi_ut_strupr (strupr)
  41. *
  42. * PARAMETERS: src_string - The source string to convert
  43. *
  44. * RETURN: None
  45. *
  46. * DESCRIPTION: Convert a string to uppercase
  47. *
  48. ******************************************************************************/
  49. void acpi_ut_strupr(char *src_string)
  50. {
  51. char *string;
  52. ACPI_FUNCTION_ENTRY();
  53. if (!src_string) {
  54. return;
  55. }
  56. /* Walk entire string, uppercasing the letters */
  57. for (string = src_string; *string; string++) {
  58. *string = (char)toupper((int)*string);
  59. }
  60. }
  61. /******************************************************************************
  62. *
  63. * FUNCTION: acpi_ut_stricmp (stricmp)
  64. *
  65. * PARAMETERS: string1 - first string to compare
  66. * string2 - second string to compare
  67. *
  68. * RETURN: int that signifies string relationship. Zero means strings
  69. * are equal.
  70. *
  71. * DESCRIPTION: Case-insensitive string compare. Implementation of the
  72. * non-ANSI stricmp function.
  73. *
  74. ******************************************************************************/
  75. int acpi_ut_stricmp(char *string1, char *string2)
  76. {
  77. int c1;
  78. int c2;
  79. do {
  80. c1 = tolower((int)*string1);
  81. c2 = tolower((int)*string2);
  82. string1++;
  83. string2++;
  84. }
  85. while ((c1 == c2) && (c1));
  86. return (c1 - c2);
  87. }
  88. #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION) || defined (ACPI_DEBUG_OUTPUT)
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
  92. *
  93. * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
  94. * functions. This is the size of the Destination buffer.
  95. *
  96. * RETURN: TRUE if the operation would overflow the destination buffer.
  97. *
  98. * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
  99. * the result of the operation will not overflow the output string
  100. * buffer.
  101. *
  102. * NOTE: These functions are typically only helpful for processing
  103. * user input and command lines. For most ACPICA code, the
  104. * required buffer length is precisely calculated before buffer
  105. * allocation, so the use of these functions is unnecessary.
  106. *
  107. ******************************************************************************/
  108. u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
  109. {
  110. if (strlen(source) >= dest_size) {
  111. return (TRUE);
  112. }
  113. strcpy(dest, source);
  114. return (FALSE);
  115. }
  116. u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
  117. {
  118. if ((strlen(dest) + strlen(source)) >= dest_size) {
  119. return (TRUE);
  120. }
  121. strcat(dest, source);
  122. return (FALSE);
  123. }
  124. u8
  125. acpi_ut_safe_strncat(char *dest,
  126. acpi_size dest_size,
  127. char *source, acpi_size max_transfer_length)
  128. {
  129. acpi_size actual_transfer_length;
  130. actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
  131. if ((strlen(dest) + actual_transfer_length) >= dest_size) {
  132. return (TRUE);
  133. }
  134. strncat(dest, source, max_transfer_length);
  135. return (FALSE);
  136. }
  137. void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
  138. {
  139. /* Always terminate destination string */
  140. strncpy(dest, source, dest_size);
  141. dest[dest_size - 1] = 0;
  142. }
  143. #endif