utstrtoul64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utstrtoul64 - String-to-integer conversion support for both
  5. * 64-bit and 32-bit integers
  6. *
  7. ******************************************************************************/
  8. #include <acpi/acpi.h>
  9. #include "accommon.h"
  10. #define _COMPONENT ACPI_UTILITIES
  11. ACPI_MODULE_NAME("utstrtoul64")
  12. /*******************************************************************************
  13. *
  14. * This module contains the top-level string to 64/32-bit unsigned integer
  15. * conversion functions:
  16. *
  17. * 1) A standard strtoul() function that supports 64-bit integers, base
  18. * 8/10/16, with integer overflow support. This is used mainly by the
  19. * iASL compiler, which implements tighter constraints on integer
  20. * constants than the runtime (interpreter) integer-to-string conversions.
  21. * 2) Runtime "Explicit conversion" as defined in the ACPI specification.
  22. * 3) Runtime "Implicit conversion" as defined in the ACPI specification.
  23. *
  24. * Current users of this module:
  25. *
  26. * iASL - Preprocessor (constants and math expressions)
  27. * iASL - Main parser, conversion of constants to integers
  28. * iASL - Data Table Compiler parser (constants and math expressions)
  29. * interpreter - Implicit and explicit conversions, GPE method names
  30. * interpreter - Repair code for return values from predefined names
  31. * debugger - Command line input string conversion
  32. * acpi_dump - ACPI table physical addresses
  33. * acpi_exec - Support for namespace overrides
  34. *
  35. * Notes concerning users of these interfaces:
  36. *
  37. * acpi_gbl_integer_byte_width is used to set the 32/64 bit limit for explicit
  38. * and implicit conversions. This global must be set to the proper width.
  39. * For the core ACPICA code, the width depends on the DSDT version. For the
  40. * acpi_ut_strtoul64 interface, all conversions are 64 bits. This interface is
  41. * used primarily for iASL, where the default width is 64 bits for all parsers,
  42. * but error checking is performed later to flag cases where a 64-bit constant
  43. * is wrongly defined in a 32-bit DSDT/SSDT.
  44. *
  45. * In ACPI, the only place where octal numbers are supported is within
  46. * the ASL language itself. This is implemented via the main acpi_ut_strtoul64
  47. * interface. According the ACPI specification, there is no ACPI runtime
  48. * support (explicit/implicit) for octal string conversions.
  49. *
  50. ******************************************************************************/
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ut_strtoul64
  54. *
  55. * PARAMETERS: string - Null terminated input string,
  56. * must be a valid pointer
  57. * return_value - Where the converted integer is
  58. * returned. Must be a valid pointer
  59. *
  60. * RETURN: Status and converted integer. Returns an exception on a
  61. * 64-bit numeric overflow
  62. *
  63. * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
  64. * full 64-bit conversion, regardless of the current global
  65. * integer width. Supports Decimal, Hex, and Octal strings.
  66. *
  67. * Current users of this function:
  68. *
  69. * iASL - Preprocessor (constants and math expressions)
  70. * iASL - Main ASL parser, conversion of ASL constants to integers
  71. * iASL - Data Table Compiler parser (constants and math expressions)
  72. * interpreter - Repair code for return values from predefined names
  73. * acpi_dump - ACPI table physical addresses
  74. * acpi_exec - Support for namespace overrides
  75. *
  76. ******************************************************************************/
  77. acpi_status acpi_ut_strtoul64(char *string, u64 *return_value)
  78. {
  79. acpi_status status = AE_OK;
  80. u8 original_bit_width;
  81. u32 base = 10; /* Default is decimal */
  82. ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string);
  83. *return_value = 0;
  84. /* A NULL return string returns a value of zero */
  85. if (*string == 0) {
  86. return_ACPI_STATUS(AE_OK);
  87. }
  88. if (!acpi_ut_remove_whitespace(&string)) {
  89. return_ACPI_STATUS(AE_OK);
  90. }
  91. /*
  92. * 1) Check for a hex constant. A "0x" prefix indicates base 16.
  93. */
  94. if (acpi_ut_detect_hex_prefix(&string)) {
  95. base = 16;
  96. }
  97. /*
  98. * 2) Check for an octal constant, defined to be a leading zero
  99. * followed by sequence of octal digits (0-7)
  100. */
  101. else if (acpi_ut_detect_octal_prefix(&string)) {
  102. base = 8;
  103. }
  104. if (!acpi_ut_remove_leading_zeros(&string)) {
  105. return_ACPI_STATUS(AE_OK); /* Return value 0 */
  106. }
  107. /*
  108. * Force a full 64-bit conversion. The caller (usually iASL) must
  109. * check for a 32-bit overflow later as necessary (If current mode
  110. * is 32-bit, meaning a 32-bit DSDT).
  111. */
  112. original_bit_width = acpi_gbl_integer_bit_width;
  113. acpi_gbl_integer_bit_width = 64;
  114. /*
  115. * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
  116. * will return an exception (to allow iASL to flag the statement).
  117. */
  118. switch (base) {
  119. case 8:
  120. status = acpi_ut_convert_octal_string(string, return_value);
  121. break;
  122. case 10:
  123. status = acpi_ut_convert_decimal_string(string, return_value);
  124. break;
  125. case 16:
  126. default:
  127. status = acpi_ut_convert_hex_string(string, return_value);
  128. break;
  129. }
  130. /* Only possible exception from above is a 64-bit overflow */
  131. acpi_gbl_integer_bit_width = original_bit_width;
  132. return_ACPI_STATUS(status);
  133. }
  134. /*******************************************************************************
  135. *
  136. * FUNCTION: acpi_ut_implicit_strtoul64
  137. *
  138. * PARAMETERS: string - Null terminated input string,
  139. * must be a valid pointer
  140. *
  141. * RETURN: Converted integer
  142. *
  143. * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
  144. * an "implicit conversion" by the ACPI specification. Used by
  145. * many ASL operators that require an integer operand, and support
  146. * an automatic (implicit) conversion from a string operand
  147. * to the final integer operand. The major restriction is that
  148. * only hex strings are supported.
  149. *
  150. * -----------------------------------------------------------------------------
  151. *
  152. * Base is always 16, either with or without the 0x prefix. Decimal and
  153. * Octal strings are not supported, as per the ACPI specification.
  154. *
  155. * Examples (both are hex values):
  156. * Add ("BA98", Arg0, Local0)
  157. * Subtract ("0x12345678", Arg1, Local1)
  158. *
  159. * Conversion rules as extracted from the ACPI specification:
  160. *
  161. * The converted integer is initialized to the value zero.
  162. * The ASCII string is always interpreted as a hexadecimal constant.
  163. *
  164. * 1) According to the ACPI specification, a "0x" prefix is not allowed.
  165. * However, ACPICA allows this as an ACPI extension on general
  166. * principle. (NO ERROR)
  167. *
  168. * 2) The conversion terminates when the size of an integer is reached
  169. * (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
  170. *
  171. * 3) The first non-hex character terminates the conversion and returns
  172. * the current accumulated value of the converted integer (NO ERROR).
  173. *
  174. * 4) Conversion of a null (zero-length) string to an integer is
  175. * technically not allowed. However, ACPICA allows this as an ACPI
  176. * extension. The conversion returns the value 0. (NO ERROR)
  177. *
  178. * NOTE: There are no error conditions returned by this function. At
  179. * the minimum, a value of zero is returned.
  180. *
  181. * Current users of this function:
  182. *
  183. * interpreter - All runtime implicit conversions, as per ACPI specification
  184. * iASL - Data Table Compiler parser (constants and math expressions)
  185. *
  186. ******************************************************************************/
  187. u64 acpi_ut_implicit_strtoul64(char *string)
  188. {
  189. u64 converted_integer = 0;
  190. ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string);
  191. if (!acpi_ut_remove_whitespace(&string)) {
  192. return_VALUE(0);
  193. }
  194. /*
  195. * Per the ACPI specification, only hexadecimal is supported for
  196. * implicit conversions, and the "0x" prefix is "not allowed".
  197. * However, allow a "0x" prefix as an ACPI extension.
  198. */
  199. acpi_ut_remove_hex_prefix(&string);
  200. if (!acpi_ut_remove_leading_zeros(&string)) {
  201. return_VALUE(0);
  202. }
  203. /*
  204. * Ignore overflow as per the ACPI specification. This is implemented by
  205. * ignoring the return status from the conversion function called below.
  206. * On overflow, the input string is simply truncated.
  207. */
  208. acpi_ut_convert_hex_string(string, &converted_integer);
  209. return_VALUE(converted_integer);
  210. }
  211. /*******************************************************************************
  212. *
  213. * FUNCTION: acpi_ut_explicit_strtoul64
  214. *
  215. * PARAMETERS: string - Null terminated input string,
  216. * must be a valid pointer
  217. *
  218. * RETURN: Converted integer
  219. *
  220. * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
  221. * an "explicit conversion" by the ACPI specification. The
  222. * main restriction is that only hex and decimal are supported.
  223. *
  224. * -----------------------------------------------------------------------------
  225. *
  226. * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
  227. * are not supported, as per the ACPI specification.
  228. *
  229. * Examples:
  230. * to_integer ("1000") Decimal
  231. * to_integer ("0xABCD") Hex
  232. *
  233. * Conversion rules as extracted from the ACPI specification:
  234. *
  235. * 1) The input string is either a decimal or hexadecimal numeric string.
  236. * A hex value must be prefixed by "0x" or it is interpreted as decimal.
  237. *
  238. * 2) The value must not exceed the maximum of an integer value
  239. * (32 or 64 bits). The ACPI specification states the behavior is
  240. * "unpredictable", so ACPICA matches the behavior of the implicit
  241. * conversion case. There are no numeric overflow conditions. (NO ERROR)
  242. *
  243. * 3) Behavior on the first non-hex character is not defined by the ACPI
  244. * specification (for the to_integer operator), so ACPICA matches the
  245. * behavior of the implicit conversion case. It terminates the
  246. * conversion and returns the current accumulated value of the converted
  247. * integer. (NO ERROR)
  248. *
  249. * 4) Conversion of a null (zero-length) string to an integer is
  250. * technically not allowed. However, ACPICA allows this as an ACPI
  251. * extension. The conversion returns the value 0. (NO ERROR)
  252. *
  253. * NOTE: There are no error conditions returned by this function. At the
  254. * minimum, a value of zero is returned.
  255. *
  256. * Current users of this function:
  257. *
  258. * interpreter - Runtime ASL to_integer operator, as per the ACPI specification
  259. *
  260. ******************************************************************************/
  261. u64 acpi_ut_explicit_strtoul64(char *string)
  262. {
  263. u64 converted_integer = 0;
  264. u32 base = 10; /* Default is decimal */
  265. ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string);
  266. if (!acpi_ut_remove_whitespace(&string)) {
  267. return_VALUE(0);
  268. }
  269. /*
  270. * Only Hex and Decimal are supported, as per the ACPI specification.
  271. * A "0x" prefix indicates hex; otherwise decimal is assumed.
  272. */
  273. if (acpi_ut_detect_hex_prefix(&string)) {
  274. base = 16;
  275. }
  276. if (!acpi_ut_remove_leading_zeros(&string)) {
  277. return_VALUE(0);
  278. }
  279. /*
  280. * Ignore overflow as per the ACPI specification. This is implemented by
  281. * ignoring the return status from the conversion functions called below.
  282. * On overflow, the input string is simply truncated.
  283. */
  284. switch (base) {
  285. case 10:
  286. default:
  287. acpi_ut_convert_decimal_string(string, &converted_integer);
  288. break;
  289. case 16:
  290. acpi_ut_convert_hex_string(string, &converted_integer);
  291. break;
  292. }
  293. return_VALUE(converted_integer);
  294. }