utnonansi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*******************************************************************************
  2. *
  3. * Module Name: utnonansi - Non-ansi C library functions
  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. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utnonansi")
  46. /*
  47. * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
  48. * version of strtoul.
  49. */
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ut_strlwr (strlwr)
  53. *
  54. * PARAMETERS: src_string - The source string to convert
  55. *
  56. * RETURN: None
  57. *
  58. * DESCRIPTION: Convert a string to lowercase
  59. *
  60. ******************************************************************************/
  61. void acpi_ut_strlwr(char *src_string)
  62. {
  63. char *string;
  64. ACPI_FUNCTION_ENTRY();
  65. if (!src_string) {
  66. return;
  67. }
  68. /* Walk entire string, lowercasing the letters */
  69. for (string = src_string; *string; string++) {
  70. *string = (char)tolower((int)*string);
  71. }
  72. }
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ut_strupr (strupr)
  76. *
  77. * PARAMETERS: src_string - The source string to convert
  78. *
  79. * RETURN: None
  80. *
  81. * DESCRIPTION: Convert a string to uppercase
  82. *
  83. ******************************************************************************/
  84. void acpi_ut_strupr(char *src_string)
  85. {
  86. char *string;
  87. ACPI_FUNCTION_ENTRY();
  88. if (!src_string) {
  89. return;
  90. }
  91. /* Walk entire string, uppercasing the letters */
  92. for (string = src_string; *string; string++) {
  93. *string = (char)toupper((int)*string);
  94. }
  95. }
  96. /******************************************************************************
  97. *
  98. * FUNCTION: acpi_ut_stricmp (stricmp)
  99. *
  100. * PARAMETERS: string1 - first string to compare
  101. * string2 - second string to compare
  102. *
  103. * RETURN: int that signifies string relationship. Zero means strings
  104. * are equal.
  105. *
  106. * DESCRIPTION: Case-insensitive string compare. Implementation of the
  107. * non-ANSI stricmp function.
  108. *
  109. ******************************************************************************/
  110. int acpi_ut_stricmp(char *string1, char *string2)
  111. {
  112. int c1;
  113. int c2;
  114. do {
  115. c1 = tolower((int)*string1);
  116. c2 = tolower((int)*string2);
  117. string1++;
  118. string2++;
  119. }
  120. while ((c1 == c2) && (c1));
  121. return (c1 - c2);
  122. }
  123. #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
  127. *
  128. * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
  129. * functions. This is the size of the Destination buffer.
  130. *
  131. * RETURN: TRUE if the operation would overflow the destination buffer.
  132. *
  133. * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
  134. * the result of the operation will not overflow the output string
  135. * buffer.
  136. *
  137. * NOTE: These functions are typically only helpful for processing
  138. * user input and command lines. For most ACPICA code, the
  139. * required buffer length is precisely calculated before buffer
  140. * allocation, so the use of these functions is unnecessary.
  141. *
  142. ******************************************************************************/
  143. u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
  144. {
  145. if (strlen(source) >= dest_size) {
  146. return (TRUE);
  147. }
  148. strcpy(dest, source);
  149. return (FALSE);
  150. }
  151. u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
  152. {
  153. if ((strlen(dest) + strlen(source)) >= dest_size) {
  154. return (TRUE);
  155. }
  156. strcat(dest, source);
  157. return (FALSE);
  158. }
  159. u8
  160. acpi_ut_safe_strncat(char *dest,
  161. acpi_size dest_size,
  162. char *source, acpi_size max_transfer_length)
  163. {
  164. acpi_size actual_transfer_length;
  165. actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
  166. if ((strlen(dest) + actual_transfer_length) >= dest_size) {
  167. return (TRUE);
  168. }
  169. strncat(dest, source, max_transfer_length);
  170. return (FALSE);
  171. }
  172. #endif
  173. /*******************************************************************************
  174. *
  175. * FUNCTION: acpi_ut_strtoul64
  176. *
  177. * PARAMETERS: string - Null terminated string
  178. * base - Radix of the string: 16 or 10 or
  179. * ACPI_ANY_BASE
  180. * max_integer_byte_width - Maximum allowable integer,in bytes:
  181. * 4 or 8 (32 or 64 bits)
  182. * ret_integer - Where the converted integer is
  183. * returned
  184. *
  185. * RETURN: Status and Converted value
  186. *
  187. * DESCRIPTION: Convert a string into an unsigned value. Performs either a
  188. * 32-bit or 64-bit conversion, depending on the input integer
  189. * size (often the current mode of the interpreter).
  190. *
  191. * NOTES: Negative numbers are not supported, as they are not supported
  192. * by ACPI.
  193. *
  194. * acpi_gbl_integer_byte_width should be set to the proper width.
  195. * For the core ACPICA code, this width depends on the DSDT
  196. * version. For iASL, the default byte width is always 8 for the
  197. * parser, but error checking is performed later to flag cases
  198. * where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
  199. *
  200. * Does not support Octal strings, not needed at this time.
  201. *
  202. ******************************************************************************/
  203. acpi_status
  204. acpi_ut_strtoul64(char *string,
  205. u32 base, u32 max_integer_byte_width, u64 *ret_integer)
  206. {
  207. u32 this_digit = 0;
  208. u64 return_value = 0;
  209. u64 quotient;
  210. u64 dividend;
  211. u8 valid_digits = 0;
  212. u8 sign_of0x = 0;
  213. u8 term = 0;
  214. ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string);
  215. switch (base) {
  216. case ACPI_ANY_BASE:
  217. case 10:
  218. case 16:
  219. break;
  220. default:
  221. /* Invalid Base */
  222. return_ACPI_STATUS(AE_BAD_PARAMETER);
  223. }
  224. if (!string) {
  225. goto error_exit;
  226. }
  227. /* Skip over any white space in the buffer */
  228. while ((*string) && (isspace((int)*string) || *string == '\t')) {
  229. string++;
  230. }
  231. if (base == ACPI_ANY_BASE) {
  232. /*
  233. * Base equal to ACPI_ANY_BASE means 'Either decimal or hex'.
  234. * We need to determine if it is decimal or hexadecimal.
  235. */
  236. if ((*string == '0') && (tolower((int)*(string + 1)) == 'x')) {
  237. sign_of0x = 1;
  238. base = 16;
  239. /* Skip over the leading '0x' */
  240. string += 2;
  241. } else {
  242. base = 10;
  243. }
  244. }
  245. /* Any string left? Check that '0x' is not followed by white space. */
  246. if (!(*string) || isspace((int)*string) || *string == '\t') {
  247. if (base == ACPI_ANY_BASE) {
  248. goto error_exit;
  249. } else {
  250. goto all_done;
  251. }
  252. }
  253. /*
  254. * Perform a 32-bit or 64-bit conversion, depending upon the input
  255. * byte width
  256. */
  257. dividend = (max_integer_byte_width <= ACPI_MAX32_BYTE_WIDTH) ?
  258. ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  259. /* Main loop: convert the string to a 32- or 64-bit integer */
  260. while (*string) {
  261. if (isdigit((int)*string)) {
  262. /* Convert ASCII 0-9 to Decimal value */
  263. this_digit = ((u8)*string) - '0';
  264. } else if (base == 10) {
  265. /* Digit is out of range; possible in to_integer case only */
  266. term = 1;
  267. } else {
  268. this_digit = (u8)toupper((int)*string);
  269. if (isxdigit((int)this_digit)) {
  270. /* Convert ASCII Hex char to value */
  271. this_digit = this_digit - 'A' + 10;
  272. } else {
  273. term = 1;
  274. }
  275. }
  276. if (term) {
  277. if (base == ACPI_ANY_BASE) {
  278. goto error_exit;
  279. } else {
  280. break;
  281. }
  282. } else if ((valid_digits == 0) && (this_digit == 0)
  283. && !sign_of0x) {
  284. /* Skip zeros */
  285. string++;
  286. continue;
  287. }
  288. valid_digits++;
  289. if (sign_of0x && ((valid_digits > 16) ||
  290. ((valid_digits > 8)
  291. && (max_integer_byte_width <=
  292. ACPI_MAX32_BYTE_WIDTH)))) {
  293. /*
  294. * This is to_integer operation case.
  295. * No restrictions for string-to-integer conversion,
  296. * see ACPI spec.
  297. */
  298. goto error_exit;
  299. }
  300. /* Divide the digit into the correct position */
  301. (void)acpi_ut_short_divide((dividend - (u64)this_digit), base,
  302. &quotient, NULL);
  303. if (return_value > quotient) {
  304. if (base == ACPI_ANY_BASE) {
  305. goto error_exit;
  306. } else {
  307. break;
  308. }
  309. }
  310. return_value *= base;
  311. return_value += this_digit;
  312. string++;
  313. }
  314. /* All done, normal exit */
  315. all_done:
  316. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  317. ACPI_FORMAT_UINT64(return_value)));
  318. *ret_integer = return_value;
  319. return_ACPI_STATUS(AE_OK);
  320. error_exit:
  321. /* Base was set/validated above (10 or 16) */
  322. if (base == 10) {
  323. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  324. } else {
  325. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  326. }
  327. }
  328. #ifdef _OBSOLETE_FUNCTIONS
  329. /* Removed: 01/2016 */
  330. /*******************************************************************************
  331. *
  332. * FUNCTION: strtoul64
  333. *
  334. * PARAMETERS: string - Null terminated string
  335. * terminater - Where a pointer to the terminating byte
  336. * is returned
  337. * base - Radix of the string
  338. *
  339. * RETURN: Converted value
  340. *
  341. * DESCRIPTION: Convert a string into an unsigned value.
  342. *
  343. ******************************************************************************/
  344. acpi_status strtoul64(char *string, u32 base, u64 *ret_integer)
  345. {
  346. u32 index;
  347. u32 sign;
  348. u64 return_value = 0;
  349. acpi_status status = AE_OK;
  350. *ret_integer = 0;
  351. switch (base) {
  352. case 0:
  353. case 8:
  354. case 10:
  355. case 16:
  356. break;
  357. default:
  358. /*
  359. * The specified Base parameter is not in the domain of
  360. * this function:
  361. */
  362. return (AE_BAD_PARAMETER);
  363. }
  364. /* Skip over any white space in the buffer: */
  365. while (isspace((int)*string) || *string == '\t') {
  366. ++string;
  367. }
  368. /*
  369. * The buffer may contain an optional plus or minus sign.
  370. * If it does, then skip over it but remember what is was:
  371. */
  372. if (*string == '-') {
  373. sign = ACPI_SIGN_NEGATIVE;
  374. ++string;
  375. } else if (*string == '+') {
  376. ++string;
  377. sign = ACPI_SIGN_POSITIVE;
  378. } else {
  379. sign = ACPI_SIGN_POSITIVE;
  380. }
  381. /*
  382. * If the input parameter Base is zero, then we need to
  383. * determine if it is octal, decimal, or hexadecimal:
  384. */
  385. if (base == 0) {
  386. if (*string == '0') {
  387. if (tolower((int)*(++string)) == 'x') {
  388. base = 16;
  389. ++string;
  390. } else {
  391. base = 8;
  392. }
  393. } else {
  394. base = 10;
  395. }
  396. }
  397. /*
  398. * For octal and hexadecimal bases, skip over the leading
  399. * 0 or 0x, if they are present.
  400. */
  401. if (base == 8 && *string == '0') {
  402. string++;
  403. }
  404. if (base == 16 && *string == '0' && tolower((int)*(++string)) == 'x') {
  405. string++;
  406. }
  407. /* Main loop: convert the string to an unsigned long */
  408. while (*string) {
  409. if (isdigit((int)*string)) {
  410. index = ((u8)*string) - '0';
  411. } else {
  412. index = (u8)toupper((int)*string);
  413. if (isupper((int)index)) {
  414. index = index - 'A' + 10;
  415. } else {
  416. goto error_exit;
  417. }
  418. }
  419. if (index >= base) {
  420. goto error_exit;
  421. }
  422. /* Check to see if value is out of range: */
  423. if (return_value > ((ACPI_UINT64_MAX - (u64)index) / (u64)base)) {
  424. goto error_exit;
  425. } else {
  426. return_value *= base;
  427. return_value += index;
  428. }
  429. ++string;
  430. }
  431. /* If a minus sign was present, then "the conversion is negated": */
  432. if (sign == ACPI_SIGN_NEGATIVE) {
  433. return_value = (ACPI_UINT32_MAX - return_value) + 1;
  434. }
  435. *ret_integer = return_value;
  436. return (status);
  437. error_exit:
  438. switch (base) {
  439. case 8:
  440. status = AE_BAD_OCTAL_CONSTANT;
  441. break;
  442. case 10:
  443. status = AE_BAD_DECIMAL_CONSTANT;
  444. break;
  445. case 16:
  446. status = AE_BAD_HEX_CONSTANT;
  447. break;
  448. default:
  449. /* Base validated above */
  450. break;
  451. }
  452. return (status);
  453. }
  454. #endif