utstring.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utstring - Common functions for strings and characters
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acnamesp.h"
  10. #define _COMPONENT ACPI_UTILITIES
  11. ACPI_MODULE_NAME("utstring")
  12. /*******************************************************************************
  13. *
  14. * FUNCTION: acpi_ut_print_string
  15. *
  16. * PARAMETERS: string - Null terminated ASCII string
  17. * max_length - Maximum output length. Used to constrain the
  18. * length of strings during debug output only.
  19. *
  20. * RETURN: None
  21. *
  22. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  23. * sequences.
  24. *
  25. ******************************************************************************/
  26. void acpi_ut_print_string(char *string, u16 max_length)
  27. {
  28. u32 i;
  29. if (!string) {
  30. acpi_os_printf("<\"NULL STRING PTR\">");
  31. return;
  32. }
  33. acpi_os_printf("\"");
  34. for (i = 0; (i < max_length) && string[i]; i++) {
  35. /* Escape sequences */
  36. switch (string[i]) {
  37. case 0x07:
  38. acpi_os_printf("\\a"); /* BELL */
  39. break;
  40. case 0x08:
  41. acpi_os_printf("\\b"); /* BACKSPACE */
  42. break;
  43. case 0x0C:
  44. acpi_os_printf("\\f"); /* FORMFEED */
  45. break;
  46. case 0x0A:
  47. acpi_os_printf("\\n"); /* LINEFEED */
  48. break;
  49. case 0x0D:
  50. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  51. break;
  52. case 0x09:
  53. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  54. break;
  55. case 0x0B:
  56. acpi_os_printf("\\v"); /* VERTICAL TAB */
  57. break;
  58. case '\'': /* Single Quote */
  59. case '\"': /* Double Quote */
  60. case '\\': /* Backslash */
  61. acpi_os_printf("\\%c", (int)string[i]);
  62. break;
  63. default:
  64. /* Check for printable character or hex escape */
  65. if (isprint((int)string[i])) {
  66. /* This is a normal character */
  67. acpi_os_printf("%c", (int)string[i]);
  68. } else {
  69. /* All others will be Hex escapes */
  70. acpi_os_printf("\\x%2.2X", (s32)string[i]);
  71. }
  72. break;
  73. }
  74. }
  75. acpi_os_printf("\"");
  76. if (i == max_length && string[i]) {
  77. acpi_os_printf("...");
  78. }
  79. }
  80. /*******************************************************************************
  81. *
  82. * FUNCTION: acpi_ut_repair_name
  83. *
  84. * PARAMETERS: name - The ACPI name to be repaired
  85. *
  86. * RETURN: Repaired version of the name
  87. *
  88. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  89. * return the new name. NOTE: the Name parameter must reside in
  90. * read/write memory, cannot be a const.
  91. *
  92. * An ACPI Name must consist of valid ACPI characters. We will repair the name
  93. * if necessary because we don't want to abort because of this, but we want
  94. * all namespace names to be printable. A warning message is appropriate.
  95. *
  96. * This issue came up because there are in fact machines that exhibit
  97. * this problem, and we want to be able to enable ACPI support for them,
  98. * even though there are a few bad names.
  99. *
  100. ******************************************************************************/
  101. void acpi_ut_repair_name(char *name)
  102. {
  103. u32 i;
  104. u8 found_bad_char = FALSE;
  105. u32 original_name;
  106. ACPI_FUNCTION_NAME(ut_repair_name);
  107. /*
  108. * Special case for the root node. This can happen if we get an
  109. * error during the execution of module-level code.
  110. */
  111. if (ACPI_COMPARE_NAMESEG(name, ACPI_ROOT_PATHNAME)) {
  112. return;
  113. }
  114. ACPI_COPY_NAMESEG(&original_name, name);
  115. /* Check each character in the name */
  116. for (i = 0; i < ACPI_NAMESEG_SIZE; i++) {
  117. if (acpi_ut_valid_name_char(name[i], i)) {
  118. continue;
  119. }
  120. /*
  121. * Replace a bad character with something printable, yet technically
  122. * still invalid. This prevents any collisions with existing "good"
  123. * names in the namespace.
  124. */
  125. name[i] = '*';
  126. found_bad_char = TRUE;
  127. }
  128. if (found_bad_char) {
  129. /* Report warning only if in strict mode or debug mode */
  130. if (!acpi_gbl_enable_interpreter_slack) {
  131. ACPI_WARNING((AE_INFO,
  132. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  133. original_name, name));
  134. } else {
  135. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  136. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  137. original_name, name));
  138. }
  139. }
  140. }
  141. #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
  142. /*******************************************************************************
  143. *
  144. * FUNCTION: ut_convert_backslashes
  145. *
  146. * PARAMETERS: pathname - File pathname string to be converted
  147. *
  148. * RETURN: Modifies the input Pathname
  149. *
  150. * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
  151. * the entire input file pathname string.
  152. *
  153. ******************************************************************************/
  154. void ut_convert_backslashes(char *pathname)
  155. {
  156. if (!pathname) {
  157. return;
  158. }
  159. while (*pathname) {
  160. if (*pathname == '\\') {
  161. *pathname = '/';
  162. }
  163. pathname++;
  164. }
  165. }
  166. #endif