utstring.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*******************************************************************************
  2. *
  3. * Module Name: utstring - Common functions for strings and characters
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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_valid_acpi_char
  118. *
  119. * PARAMETERS: char - The character to be examined
  120. * position - Byte position (0-3)
  121. *
  122. * RETURN: TRUE if the character is valid, FALSE otherwise
  123. *
  124. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  125. * 1) Upper case alpha
  126. * 2) numeric
  127. * 3) underscore
  128. *
  129. * We allow a '!' as the last character because of the ASF! table
  130. *
  131. ******************************************************************************/
  132. u8 acpi_ut_valid_acpi_char(char character, u32 position)
  133. {
  134. if (!((character >= 'A' && character <= 'Z') ||
  135. (character >= '0' && character <= '9') || (character == '_'))) {
  136. /* Allow a '!' in the last position */
  137. if (character == '!' && position == 3) {
  138. return (TRUE);
  139. }
  140. return (FALSE);
  141. }
  142. return (TRUE);
  143. }
  144. /*******************************************************************************
  145. *
  146. * FUNCTION: acpi_ut_valid_acpi_name
  147. *
  148. * PARAMETERS: name - The name to be examined. Does not have to
  149. * be NULL terminated string.
  150. *
  151. * RETURN: TRUE if the name is valid, FALSE otherwise
  152. *
  153. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  154. * 1) Upper case alpha
  155. * 2) numeric
  156. * 3) underscore
  157. *
  158. ******************************************************************************/
  159. u8 acpi_ut_valid_acpi_name(char *name)
  160. {
  161. u32 i;
  162. ACPI_FUNCTION_ENTRY();
  163. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  164. if (!acpi_ut_valid_acpi_char(name[i], i)) {
  165. return (FALSE);
  166. }
  167. }
  168. return (TRUE);
  169. }
  170. /*******************************************************************************
  171. *
  172. * FUNCTION: acpi_ut_repair_name
  173. *
  174. * PARAMETERS: name - The ACPI name to be repaired
  175. *
  176. * RETURN: Repaired version of the name
  177. *
  178. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  179. * return the new name. NOTE: the Name parameter must reside in
  180. * read/write memory, cannot be a const.
  181. *
  182. * An ACPI Name must consist of valid ACPI characters. We will repair the name
  183. * if necessary because we don't want to abort because of this, but we want
  184. * all namespace names to be printable. A warning message is appropriate.
  185. *
  186. * This issue came up because there are in fact machines that exhibit
  187. * this problem, and we want to be able to enable ACPI support for them,
  188. * even though there are a few bad names.
  189. *
  190. ******************************************************************************/
  191. void acpi_ut_repair_name(char *name)
  192. {
  193. u32 i;
  194. u8 found_bad_char = FALSE;
  195. u32 original_name;
  196. ACPI_FUNCTION_NAME(ut_repair_name);
  197. ACPI_MOVE_NAME(&original_name, name);
  198. /* Check each character in the name */
  199. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  200. if (acpi_ut_valid_acpi_char(name[i], i)) {
  201. continue;
  202. }
  203. /*
  204. * Replace a bad character with something printable, yet technically
  205. * still invalid. This prevents any collisions with existing "good"
  206. * names in the namespace.
  207. */
  208. name[i] = '*';
  209. found_bad_char = TRUE;
  210. }
  211. if (found_bad_char) {
  212. /* Report warning only if in strict mode or debug mode */
  213. if (!acpi_gbl_enable_interpreter_slack) {
  214. ACPI_WARNING((AE_INFO,
  215. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  216. original_name, name));
  217. } else {
  218. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  219. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  220. original_name, name));
  221. }
  222. }
  223. }
  224. #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
  225. /*******************************************************************************
  226. *
  227. * FUNCTION: ut_convert_backslashes
  228. *
  229. * PARAMETERS: pathname - File pathname string to be converted
  230. *
  231. * RETURN: Modifies the input Pathname
  232. *
  233. * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
  234. * the entire input file pathname string.
  235. *
  236. ******************************************************************************/
  237. void ut_convert_backslashes(char *pathname)
  238. {
  239. if (!pathname) {
  240. return;
  241. }
  242. while (*pathname) {
  243. if (*pathname == '\\') {
  244. *pathname = '/';
  245. }
  246. pathname++;
  247. }
  248. }
  249. #endif