utstring.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*******************************************************************************
  2. *
  3. * Module Name: utstring - Common functions for strings and characters
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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. * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
  49. * version of strtoul.
  50. */
  51. #ifdef ACPI_ASL_COMPILER
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ut_strlwr (strlwr)
  55. *
  56. * PARAMETERS: src_string - The source string to convert
  57. *
  58. * RETURN: None
  59. *
  60. * DESCRIPTION: Convert string to lowercase
  61. *
  62. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  63. *
  64. ******************************************************************************/
  65. void acpi_ut_strlwr(char *src_string)
  66. {
  67. char *string;
  68. ACPI_FUNCTION_ENTRY();
  69. if (!src_string) {
  70. return;
  71. }
  72. /* Walk entire string, lowercasing the letters */
  73. for (string = src_string; *string; string++) {
  74. *string = (char)ACPI_TOLOWER(*string);
  75. }
  76. return;
  77. }
  78. /******************************************************************************
  79. *
  80. * FUNCTION: acpi_ut_stricmp (stricmp)
  81. *
  82. * PARAMETERS: string1 - first string to compare
  83. * string2 - second string to compare
  84. *
  85. * RETURN: int that signifies string relationship. Zero means strings
  86. * are equal.
  87. *
  88. * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
  89. * strings with no case sensitivity)
  90. *
  91. ******************************************************************************/
  92. int acpi_ut_stricmp(char *string1, char *string2)
  93. {
  94. int c1;
  95. int c2;
  96. do {
  97. c1 = tolower((int)*string1);
  98. c2 = tolower((int)*string2);
  99. string1++;
  100. string2++;
  101. }
  102. while ((c1 == c2) && (c1));
  103. return (c1 - c2);
  104. }
  105. #endif
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_strupr (strupr)
  109. *
  110. * PARAMETERS: src_string - The source string to convert
  111. *
  112. * RETURN: None
  113. *
  114. * DESCRIPTION: Convert string to uppercase
  115. *
  116. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  117. *
  118. ******************************************************************************/
  119. void acpi_ut_strupr(char *src_string)
  120. {
  121. char *string;
  122. ACPI_FUNCTION_ENTRY();
  123. if (!src_string) {
  124. return;
  125. }
  126. /* Walk entire string, uppercasing the letters */
  127. for (string = src_string; *string; string++) {
  128. *string = (char)ACPI_TOUPPER(*string);
  129. }
  130. return;
  131. }
  132. /*******************************************************************************
  133. *
  134. * FUNCTION: acpi_ut_strtoul64
  135. *
  136. * PARAMETERS: string - Null terminated string
  137. * base - Radix of the string: 16 or ACPI_ANY_BASE;
  138. * ACPI_ANY_BASE means 'in behalf of to_integer'
  139. * ret_integer - Where the converted integer is returned
  140. *
  141. * RETURN: Status and Converted value
  142. *
  143. * DESCRIPTION: Convert a string into an unsigned value. Performs either a
  144. * 32-bit or 64-bit conversion, depending on the current mode
  145. * of the interpreter.
  146. * NOTE: Does not support Octal strings, not needed.
  147. *
  148. ******************************************************************************/
  149. acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer)
  150. {
  151. u32 this_digit = 0;
  152. u64 return_value = 0;
  153. u64 quotient;
  154. u64 dividend;
  155. u32 to_integer_op = (base == ACPI_ANY_BASE);
  156. u32 mode32 = (acpi_gbl_integer_byte_width == 4);
  157. u8 valid_digits = 0;
  158. u8 sign_of0x = 0;
  159. u8 term = 0;
  160. ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
  161. switch (base) {
  162. case ACPI_ANY_BASE:
  163. case 16:
  164. break;
  165. default:
  166. /* Invalid Base */
  167. return_ACPI_STATUS(AE_BAD_PARAMETER);
  168. }
  169. if (!string) {
  170. goto error_exit;
  171. }
  172. /* Skip over any white space in the buffer */
  173. while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
  174. string++;
  175. }
  176. if (to_integer_op) {
  177. /*
  178. * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
  179. * We need to determine if it is decimal or hexadecimal.
  180. */
  181. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  182. sign_of0x = 1;
  183. base = 16;
  184. /* Skip over the leading '0x' */
  185. string += 2;
  186. } else {
  187. base = 10;
  188. }
  189. }
  190. /* Any string left? Check that '0x' is not followed by white space. */
  191. if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
  192. if (to_integer_op) {
  193. goto error_exit;
  194. } else {
  195. goto all_done;
  196. }
  197. }
  198. /*
  199. * Perform a 32-bit or 64-bit conversion, depending upon the current
  200. * execution mode of the interpreter
  201. */
  202. dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  203. /* Main loop: convert the string to a 32- or 64-bit integer */
  204. while (*string) {
  205. if (ACPI_IS_DIGIT(*string)) {
  206. /* Convert ASCII 0-9 to Decimal value */
  207. this_digit = ((u8)*string) - '0';
  208. } else if (base == 10) {
  209. /* Digit is out of range; possible in to_integer case only */
  210. term = 1;
  211. } else {
  212. this_digit = (u8)ACPI_TOUPPER(*string);
  213. if (ACPI_IS_XDIGIT((char)this_digit)) {
  214. /* Convert ASCII Hex char to value */
  215. this_digit = this_digit - 'A' + 10;
  216. } else {
  217. term = 1;
  218. }
  219. }
  220. if (term) {
  221. if (to_integer_op) {
  222. goto error_exit;
  223. } else {
  224. break;
  225. }
  226. } else if ((valid_digits == 0) && (this_digit == 0)
  227. && !sign_of0x) {
  228. /* Skip zeros */
  229. string++;
  230. continue;
  231. }
  232. valid_digits++;
  233. if (sign_of0x
  234. && ((valid_digits > 16)
  235. || ((valid_digits > 8) && mode32))) {
  236. /*
  237. * This is to_integer operation case.
  238. * No any restrictions for string-to-integer conversion,
  239. * see ACPI spec.
  240. */
  241. goto error_exit;
  242. }
  243. /* Divide the digit into the correct position */
  244. (void)acpi_ut_short_divide((dividend - (u64)this_digit),
  245. base, &quotient, NULL);
  246. if (return_value > quotient) {
  247. if (to_integer_op) {
  248. goto error_exit;
  249. } else {
  250. break;
  251. }
  252. }
  253. return_value *= base;
  254. return_value += this_digit;
  255. string++;
  256. }
  257. /* All done, normal exit */
  258. all_done:
  259. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  260. ACPI_FORMAT_UINT64(return_value)));
  261. *ret_integer = return_value;
  262. return_ACPI_STATUS(AE_OK);
  263. error_exit:
  264. /* Base was set/validated above */
  265. if (base == 10) {
  266. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  267. } else {
  268. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  269. }
  270. }
  271. /*******************************************************************************
  272. *
  273. * FUNCTION: acpi_ut_print_string
  274. *
  275. * PARAMETERS: string - Null terminated ASCII string
  276. * max_length - Maximum output length
  277. *
  278. * RETURN: None
  279. *
  280. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  281. * sequences.
  282. *
  283. ******************************************************************************/
  284. void acpi_ut_print_string(char *string, u8 max_length)
  285. {
  286. u32 i;
  287. if (!string) {
  288. acpi_os_printf("<\"NULL STRING PTR\">");
  289. return;
  290. }
  291. acpi_os_printf("\"");
  292. for (i = 0; string[i] && (i < max_length); i++) {
  293. /* Escape sequences */
  294. switch (string[i]) {
  295. case 0x07:
  296. acpi_os_printf("\\a"); /* BELL */
  297. break;
  298. case 0x08:
  299. acpi_os_printf("\\b"); /* BACKSPACE */
  300. break;
  301. case 0x0C:
  302. acpi_os_printf("\\f"); /* FORMFEED */
  303. break;
  304. case 0x0A:
  305. acpi_os_printf("\\n"); /* LINEFEED */
  306. break;
  307. case 0x0D:
  308. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  309. break;
  310. case 0x09:
  311. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  312. break;
  313. case 0x0B:
  314. acpi_os_printf("\\v"); /* VERTICAL TAB */
  315. break;
  316. case '\'': /* Single Quote */
  317. case '\"': /* Double Quote */
  318. case '\\': /* Backslash */
  319. acpi_os_printf("\\%c", (int)string[i]);
  320. break;
  321. default:
  322. /* Check for printable character or hex escape */
  323. if (ACPI_IS_PRINT(string[i])) {
  324. /* This is a normal character */
  325. acpi_os_printf("%c", (int)string[i]);
  326. } else {
  327. /* All others will be Hex escapes */
  328. acpi_os_printf("\\x%2.2X", (s32) string[i]);
  329. }
  330. break;
  331. }
  332. }
  333. acpi_os_printf("\"");
  334. if (i == max_length && string[i]) {
  335. acpi_os_printf("...");
  336. }
  337. }
  338. /*******************************************************************************
  339. *
  340. * FUNCTION: acpi_ut_valid_acpi_char
  341. *
  342. * PARAMETERS: char - The character to be examined
  343. * position - Byte position (0-3)
  344. *
  345. * RETURN: TRUE if the character is valid, FALSE otherwise
  346. *
  347. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  348. * 1) Upper case alpha
  349. * 2) numeric
  350. * 3) underscore
  351. *
  352. * We allow a '!' as the last character because of the ASF! table
  353. *
  354. ******************************************************************************/
  355. u8 acpi_ut_valid_acpi_char(char character, u32 position)
  356. {
  357. if (!((character >= 'A' && character <= 'Z') ||
  358. (character >= '0' && character <= '9') || (character == '_'))) {
  359. /* Allow a '!' in the last position */
  360. if (character == '!' && position == 3) {
  361. return (TRUE);
  362. }
  363. return (FALSE);
  364. }
  365. return (TRUE);
  366. }
  367. /*******************************************************************************
  368. *
  369. * FUNCTION: acpi_ut_valid_acpi_name
  370. *
  371. * PARAMETERS: name - The name to be examined
  372. *
  373. * RETURN: TRUE if the name is valid, FALSE otherwise
  374. *
  375. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  376. * 1) Upper case alpha
  377. * 2) numeric
  378. * 3) underscore
  379. *
  380. ******************************************************************************/
  381. u8 acpi_ut_valid_acpi_name(u32 name)
  382. {
  383. u32 i;
  384. ACPI_FUNCTION_ENTRY();
  385. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  386. if (!acpi_ut_valid_acpi_char
  387. ((ACPI_CAST_PTR(char, &name))[i], i)) {
  388. return (FALSE);
  389. }
  390. }
  391. return (TRUE);
  392. }
  393. /*******************************************************************************
  394. *
  395. * FUNCTION: acpi_ut_repair_name
  396. *
  397. * PARAMETERS: name - The ACPI name to be repaired
  398. *
  399. * RETURN: Repaired version of the name
  400. *
  401. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  402. * return the new name. NOTE: the Name parameter must reside in
  403. * read/write memory, cannot be a const.
  404. *
  405. * An ACPI Name must consist of valid ACPI characters. We will repair the name
  406. * if necessary because we don't want to abort because of this, but we want
  407. * all namespace names to be printable. A warning message is appropriate.
  408. *
  409. * This issue came up because there are in fact machines that exhibit
  410. * this problem, and we want to be able to enable ACPI support for them,
  411. * even though there are a few bad names.
  412. *
  413. ******************************************************************************/
  414. void acpi_ut_repair_name(char *name)
  415. {
  416. u32 i;
  417. u8 found_bad_char = FALSE;
  418. u32 original_name;
  419. ACPI_FUNCTION_NAME(ut_repair_name);
  420. ACPI_MOVE_NAME(&original_name, name);
  421. /* Check each character in the name */
  422. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  423. if (acpi_ut_valid_acpi_char(name[i], i)) {
  424. continue;
  425. }
  426. /*
  427. * Replace a bad character with something printable, yet technically
  428. * still invalid. This prevents any collisions with existing "good"
  429. * names in the namespace.
  430. */
  431. name[i] = '*';
  432. found_bad_char = TRUE;
  433. }
  434. if (found_bad_char) {
  435. /* Report warning only if in strict mode or debug mode */
  436. if (!acpi_gbl_enable_interpreter_slack) {
  437. ACPI_WARNING((AE_INFO,
  438. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  439. original_name, name));
  440. } else {
  441. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  442. "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
  443. original_name, name));
  444. }
  445. }
  446. }
  447. #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
  448. /*******************************************************************************
  449. *
  450. * FUNCTION: ut_convert_backslashes
  451. *
  452. * PARAMETERS: pathname - File pathname string to be converted
  453. *
  454. * RETURN: Modifies the input Pathname
  455. *
  456. * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
  457. * the entire input file pathname string.
  458. *
  459. ******************************************************************************/
  460. void ut_convert_backslashes(char *pathname)
  461. {
  462. if (!pathname) {
  463. return;
  464. }
  465. while (*pathname) {
  466. if (*pathname == '\\') {
  467. *pathname = '/';
  468. }
  469. pathname++;
  470. }
  471. }
  472. #endif