utstring.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*******************************************************************************
  2. *
  3. * Module Name: utstring - Common functions for strings and characters
  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. #include "acnamesp.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utstring")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ut_print_string
  50. *
  51. * PARAMETERS: string - Null terminated ASCII string
  52. * max_length - Maximum output length. Used to constrain the
  53. * length of strings during debug output only.
  54. *
  55. * RETURN: None
  56. *
  57. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  58. * sequences.
  59. *
  60. ******************************************************************************/
  61. void acpi_ut_print_string(char *string, u16 max_length)
  62. {
  63. u32 i;
  64. if (!string) {
  65. acpi_os_printf("<\"NULL STRING PTR\">");
  66. return;
  67. }
  68. acpi_os_printf("\"");
  69. for (i = 0; (i < max_length) && string[i]; i++) {
  70. /* Escape sequences */
  71. switch (string[i]) {
  72. case 0x07:
  73. acpi_os_printf("\\a"); /* BELL */
  74. break;
  75. case 0x08:
  76. acpi_os_printf("\\b"); /* BACKSPACE */
  77. break;
  78. case 0x0C:
  79. acpi_os_printf("\\f"); /* FORMFEED */
  80. break;
  81. case 0x0A:
  82. acpi_os_printf("\\n"); /* LINEFEED */
  83. break;
  84. case 0x0D:
  85. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  86. break;
  87. case 0x09:
  88. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  89. break;
  90. case 0x0B:
  91. acpi_os_printf("\\v"); /* VERTICAL TAB */
  92. break;
  93. case '\'': /* Single Quote */
  94. case '\"': /* Double Quote */
  95. case '\\': /* Backslash */
  96. acpi_os_printf("\\%c", (int)string[i]);
  97. break;
  98. default:
  99. /* Check for printable character or hex escape */
  100. if (isprint((int)string[i])) {
  101. /* This is a normal character */
  102. acpi_os_printf("%c", (int)string[i]);
  103. } else {
  104. /* All others will be Hex escapes */
  105. acpi_os_printf("\\x%2.2X", (s32)string[i]);
  106. }
  107. break;
  108. }
  109. }
  110. acpi_os_printf("\"");
  111. if (i == max_length && string[i]) {
  112. acpi_os_printf("...");
  113. }
  114. }
  115. /*******************************************************************************
  116. *
  117. * FUNCTION: acpi_ut_repair_name
  118. *
  119. * PARAMETERS: name - The ACPI name to be repaired
  120. *
  121. * RETURN: Repaired version of the name
  122. *
  123. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  124. * return the new name. NOTE: the Name parameter must reside in
  125. * read/write memory, cannot be a const.
  126. *
  127. * An ACPI Name must consist of valid ACPI characters. We will repair the name
  128. * if necessary because we don't want to abort because of this, but we want
  129. * all namespace names to be printable. A warning message is appropriate.
  130. *
  131. * This issue came up because there are in fact machines that exhibit
  132. * this problem, and we want to be able to enable ACPI support for them,
  133. * even though there are a few bad names.
  134. *
  135. ******************************************************************************/
  136. void acpi_ut_repair_name(char *name)
  137. {
  138. u32 i;
  139. u8 found_bad_char = FALSE;
  140. u32 original_name;
  141. ACPI_FUNCTION_NAME(ut_repair_name);
  142. /*
  143. * Special case for the root node. This can happen if we get an
  144. * error during the execution of module-level code.
  145. */
  146. if (ACPI_COMPARE_NAME(name, "\\___")) {
  147. return;
  148. }
  149. ACPI_MOVE_NAME(&original_name, name);
  150. /* Check each character in the name */
  151. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  152. if (acpi_ut_valid_name_char(name[i], i)) {
  153. continue;
  154. }
  155. /*
  156. * Replace a bad character with something printable, yet technically
  157. * still invalid. This prevents any collisions with existing "good"
  158. * names in the namespace.
  159. */
  160. name[i] = '*';
  161. found_bad_char = TRUE;
  162. }
  163. if (found_bad_char) {
  164. /* Report warning only if in strict mode or debug mode */
  165. if (!acpi_gbl_enable_interpreter_slack) {
  166. ACPI_WARNING((AE_INFO,
  167. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  168. original_name, name));
  169. } else {
  170. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  171. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  172. original_name, name));
  173. }
  174. }
  175. }
  176. #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: ut_convert_backslashes
  180. *
  181. * PARAMETERS: pathname - File pathname string to be converted
  182. *
  183. * RETURN: Modifies the input Pathname
  184. *
  185. * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
  186. * the entire input file pathname string.
  187. *
  188. ******************************************************************************/
  189. void ut_convert_backslashes(char *pathname)
  190. {
  191. if (!pathname) {
  192. return;
  193. }
  194. while (*pathname) {
  195. if (*pathname == '\\') {
  196. *pathname = '/';
  197. }
  198. pathname++;
  199. }
  200. }
  201. #endif