utnonansi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*******************************************************************************
  2. *
  3. * Module Name: utnonansi - Non-ansi C library functions
  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. #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. /*******************************************************************************
  124. *
  125. * FUNCTION: acpi_ut_strtoul64
  126. *
  127. * PARAMETERS: string - Null terminated string
  128. * base - Radix of the string: 16 or ACPI_ANY_BASE;
  129. * ACPI_ANY_BASE means 'in behalf of to_integer'
  130. * ret_integer - Where the converted integer is returned
  131. *
  132. * RETURN: Status and Converted value
  133. *
  134. * DESCRIPTION: Convert a string into an unsigned value. Performs either a
  135. * 32-bit or 64-bit conversion, depending on the current mode
  136. * of the interpreter.
  137. *
  138. * NOTE: Does not support Octal strings, not needed.
  139. *
  140. ******************************************************************************/
  141. acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer)
  142. {
  143. u32 this_digit = 0;
  144. u64 return_value = 0;
  145. u64 quotient;
  146. u64 dividend;
  147. u32 to_integer_op = (base == ACPI_ANY_BASE);
  148. u32 mode32 = (acpi_gbl_integer_byte_width == 4);
  149. u8 valid_digits = 0;
  150. u8 sign_of0x = 0;
  151. u8 term = 0;
  152. ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
  153. switch (base) {
  154. case ACPI_ANY_BASE:
  155. case 16:
  156. break;
  157. default:
  158. /* Invalid Base */
  159. return_ACPI_STATUS(AE_BAD_PARAMETER);
  160. }
  161. if (!string) {
  162. goto error_exit;
  163. }
  164. /* Skip over any white space in the buffer */
  165. while ((*string) && (isspace((int)*string) || *string == '\t')) {
  166. string++;
  167. }
  168. if (to_integer_op) {
  169. /*
  170. * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
  171. * We need to determine if it is decimal or hexadecimal.
  172. */
  173. if ((*string == '0') && (tolower((int)*(string + 1)) == 'x')) {
  174. sign_of0x = 1;
  175. base = 16;
  176. /* Skip over the leading '0x' */
  177. string += 2;
  178. } else {
  179. base = 10;
  180. }
  181. }
  182. /* Any string left? Check that '0x' is not followed by white space. */
  183. if (!(*string) || isspace((int)*string) || *string == '\t') {
  184. if (to_integer_op) {
  185. goto error_exit;
  186. } else {
  187. goto all_done;
  188. }
  189. }
  190. /*
  191. * Perform a 32-bit or 64-bit conversion, depending upon the current
  192. * execution mode of the interpreter
  193. */
  194. dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  195. /* Main loop: convert the string to a 32- or 64-bit integer */
  196. while (*string) {
  197. if (isdigit((int)*string)) {
  198. /* Convert ASCII 0-9 to Decimal value */
  199. this_digit = ((u8)*string) - '0';
  200. } else if (base == 10) {
  201. /* Digit is out of range; possible in to_integer case only */
  202. term = 1;
  203. } else {
  204. this_digit = (u8)toupper((int)*string);
  205. if (isxdigit((int)this_digit)) {
  206. /* Convert ASCII Hex char to value */
  207. this_digit = this_digit - 'A' + 10;
  208. } else {
  209. term = 1;
  210. }
  211. }
  212. if (term) {
  213. if (to_integer_op) {
  214. goto error_exit;
  215. } else {
  216. break;
  217. }
  218. } else if ((valid_digits == 0) && (this_digit == 0)
  219. && !sign_of0x) {
  220. /* Skip zeros */
  221. string++;
  222. continue;
  223. }
  224. valid_digits++;
  225. if (sign_of0x
  226. && ((valid_digits > 16)
  227. || ((valid_digits > 8) && mode32))) {
  228. /*
  229. * This is to_integer operation case.
  230. * No any restrictions for string-to-integer conversion,
  231. * see ACPI spec.
  232. */
  233. goto error_exit;
  234. }
  235. /* Divide the digit into the correct position */
  236. (void)acpi_ut_short_divide((dividend - (u64)this_digit),
  237. base, &quotient, NULL);
  238. if (return_value > quotient) {
  239. if (to_integer_op) {
  240. goto error_exit;
  241. } else {
  242. break;
  243. }
  244. }
  245. return_value *= base;
  246. return_value += this_digit;
  247. string++;
  248. }
  249. /* All done, normal exit */
  250. all_done:
  251. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  252. ACPI_FORMAT_UINT64(return_value)));
  253. *ret_integer = return_value;
  254. return_ACPI_STATUS(AE_OK);
  255. error_exit:
  256. /* Base was set/validated above */
  257. if (base == 10) {
  258. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  259. } else {
  260. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  261. }
  262. }
  263. #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
  264. /*******************************************************************************
  265. *
  266. * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
  267. *
  268. * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
  269. * functions. This is the size of the Destination buffer.
  270. *
  271. * RETURN: TRUE if the operation would overflow the destination buffer.
  272. *
  273. * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
  274. * the result of the operation will not overflow the output string
  275. * buffer.
  276. *
  277. * NOTE: These functions are typically only helpful for processing
  278. * user input and command lines. For most ACPICA code, the
  279. * required buffer length is precisely calculated before buffer
  280. * allocation, so the use of these functions is unnecessary.
  281. *
  282. ******************************************************************************/
  283. u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
  284. {
  285. if (strlen(source) >= dest_size) {
  286. return (TRUE);
  287. }
  288. strcpy(dest, source);
  289. return (FALSE);
  290. }
  291. u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
  292. {
  293. if ((strlen(dest) + strlen(source)) >= dest_size) {
  294. return (TRUE);
  295. }
  296. strcat(dest, source);
  297. return (FALSE);
  298. }
  299. u8
  300. acpi_ut_safe_strncat(char *dest,
  301. acpi_size dest_size,
  302. char *source, acpi_size max_transfer_length)
  303. {
  304. acpi_size actual_transfer_length;
  305. actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
  306. if ((strlen(dest) + actual_transfer_length) >= dest_size) {
  307. return (TRUE);
  308. }
  309. strncat(dest, source, max_transfer_length);
  310. return (FALSE);
  311. }
  312. #endif