/drivers/acpi/acpica/utstrsuppt.c

https://bitbucket.org/updatelee/v4l-updatelee · C · 408 lines · 142 code · 78 blank · 188 comment · 29 complexity · 3d611c7060313d177794d57e8f81b460 MD5 · raw file

  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utstrsuppt - Support functions for string-to-integer conversion
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #define _COMPONENT ACPI_UTILITIES
  10. ACPI_MODULE_NAME("utstrsuppt")
  11. /* Local prototypes */
  12. static acpi_status
  13. acpi_ut_insert_digit(u64 *accumulated_value, u32 base, int ascii_digit);
  14. static acpi_status
  15. acpi_ut_strtoul_multiply64(u64 multiplicand, u32 base, u64 *out_product);
  16. static acpi_status acpi_ut_strtoul_add64(u64 addend1, u32 digit, u64 *out_sum);
  17. /*******************************************************************************
  18. *
  19. * FUNCTION: acpi_ut_convert_octal_string
  20. *
  21. * PARAMETERS: string - Null terminated input string
  22. * return_value_ptr - Where the converted value is returned
  23. *
  24. * RETURN: Status and 64-bit converted integer
  25. *
  26. * DESCRIPTION: Performs a base 8 conversion of the input string to an
  27. * integer value, either 32 or 64 bits.
  28. *
  29. * NOTE: Maximum 64-bit unsigned octal value is 01777777777777777777777
  30. * Maximum 32-bit unsigned octal value is 037777777777
  31. *
  32. ******************************************************************************/
  33. acpi_status acpi_ut_convert_octal_string(char *string, u64 *return_value_ptr)
  34. {
  35. u64 accumulated_value = 0;
  36. acpi_status status = AE_OK;
  37. /* Convert each ASCII byte in the input string */
  38. while (*string) {
  39. /* Character must be ASCII 0-7, otherwise terminate with no error */
  40. if (!(ACPI_IS_OCTAL_DIGIT(*string))) {
  41. break;
  42. }
  43. /* Convert and insert this octal digit into the accumulator */
  44. status = acpi_ut_insert_digit(&accumulated_value, 8, *string);
  45. if (ACPI_FAILURE(status)) {
  46. status = AE_OCTAL_OVERFLOW;
  47. break;
  48. }
  49. string++;
  50. }
  51. /* Always return the value that has been accumulated */
  52. *return_value_ptr = accumulated_value;
  53. return (status);
  54. }
  55. /*******************************************************************************
  56. *
  57. * FUNCTION: acpi_ut_convert_decimal_string
  58. *
  59. * PARAMETERS: string - Null terminated input string
  60. * return_value_ptr - Where the converted value is returned
  61. *
  62. * RETURN: Status and 64-bit converted integer
  63. *
  64. * DESCRIPTION: Performs a base 10 conversion of the input string to an
  65. * integer value, either 32 or 64 bits.
  66. *
  67. * NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615
  68. * Maximum 32-bit unsigned decimal value is 4294967295
  69. *
  70. ******************************************************************************/
  71. acpi_status acpi_ut_convert_decimal_string(char *string, u64 *return_value_ptr)
  72. {
  73. u64 accumulated_value = 0;
  74. acpi_status status = AE_OK;
  75. /* Convert each ASCII byte in the input string */
  76. while (*string) {
  77. /* Character must be ASCII 0-9, otherwise terminate with no error */
  78. if (!isdigit(*string)) {
  79. break;
  80. }
  81. /* Convert and insert this decimal digit into the accumulator */
  82. status = acpi_ut_insert_digit(&accumulated_value, 10, *string);
  83. if (ACPI_FAILURE(status)) {
  84. status = AE_DECIMAL_OVERFLOW;
  85. break;
  86. }
  87. string++;
  88. }
  89. /* Always return the value that has been accumulated */
  90. *return_value_ptr = accumulated_value;
  91. return (status);
  92. }
  93. /*******************************************************************************
  94. *
  95. * FUNCTION: acpi_ut_convert_hex_string
  96. *
  97. * PARAMETERS: string - Null terminated input string
  98. * return_value_ptr - Where the converted value is returned
  99. *
  100. * RETURN: Status and 64-bit converted integer
  101. *
  102. * DESCRIPTION: Performs a base 16 conversion of the input string to an
  103. * integer value, either 32 or 64 bits.
  104. *
  105. * NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
  106. * Maximum 32-bit unsigned hex value is 0xFFFFFFFF
  107. *
  108. ******************************************************************************/
  109. acpi_status acpi_ut_convert_hex_string(char *string, u64 *return_value_ptr)
  110. {
  111. u64 accumulated_value = 0;
  112. acpi_status status = AE_OK;
  113. /* Convert each ASCII byte in the input string */
  114. while (*string) {
  115. /* Must be ASCII A-F, a-f, or 0-9, otherwise terminate with no error */
  116. if (!isxdigit(*string)) {
  117. break;
  118. }
  119. /* Convert and insert this hex digit into the accumulator */
  120. status = acpi_ut_insert_digit(&accumulated_value, 16, *string);
  121. if (ACPI_FAILURE(status)) {
  122. status = AE_HEX_OVERFLOW;
  123. break;
  124. }
  125. string++;
  126. }
  127. /* Always return the value that has been accumulated */
  128. *return_value_ptr = accumulated_value;
  129. return (status);
  130. }
  131. /*******************************************************************************
  132. *
  133. * FUNCTION: acpi_ut_remove_leading_zeros
  134. *
  135. * PARAMETERS: string - Pointer to input ASCII string
  136. *
  137. * RETURN: Next character after any leading zeros. This character may be
  138. * used by the caller to detect end-of-string.
  139. *
  140. * DESCRIPTION: Remove any leading zeros in the input string. Return the
  141. * next character after the final ASCII zero to enable the caller
  142. * to check for the end of the string (NULL terminator).
  143. *
  144. ******************************************************************************/
  145. char acpi_ut_remove_leading_zeros(char **string)
  146. {
  147. while (**string == ACPI_ASCII_ZERO) {
  148. *string += 1;
  149. }
  150. return (**string);
  151. }
  152. /*******************************************************************************
  153. *
  154. * FUNCTION: acpi_ut_remove_whitespace
  155. *
  156. * PARAMETERS: string - Pointer to input ASCII string
  157. *
  158. * RETURN: Next character after any whitespace. This character may be
  159. * used by the caller to detect end-of-string.
  160. *
  161. * DESCRIPTION: Remove any leading whitespace in the input string. Return the
  162. * next character after the final ASCII zero to enable the caller
  163. * to check for the end of the string (NULL terminator).
  164. *
  165. ******************************************************************************/
  166. char acpi_ut_remove_whitespace(char **string)
  167. {
  168. while (isspace((u8)**string)) {
  169. *string += 1;
  170. }
  171. return (**string);
  172. }
  173. /*******************************************************************************
  174. *
  175. * FUNCTION: acpi_ut_detect_hex_prefix
  176. *
  177. * PARAMETERS: string - Pointer to input ASCII string
  178. *
  179. * RETURN: TRUE if a "0x" prefix was found at the start of the string
  180. *
  181. * DESCRIPTION: Detect and remove a hex "0x" prefix
  182. *
  183. ******************************************************************************/
  184. u8 acpi_ut_detect_hex_prefix(char **string)
  185. {
  186. if ((**string == ACPI_ASCII_ZERO) &&
  187. (tolower((int)*(*string + 1)) == 'x')) {
  188. *string += 2; /* Go past the leading 0x */
  189. return (TRUE);
  190. }
  191. return (FALSE); /* Not a hex string */
  192. }
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_ut_detect_octal_prefix
  196. *
  197. * PARAMETERS: string - Pointer to input ASCII string
  198. *
  199. * RETURN: True if an octal "0" prefix was found at the start of the
  200. * string
  201. *
  202. * DESCRIPTION: Detect and remove an octal prefix (zero)
  203. *
  204. ******************************************************************************/
  205. u8 acpi_ut_detect_octal_prefix(char **string)
  206. {
  207. if (**string == ACPI_ASCII_ZERO) {
  208. *string += 1; /* Go past the leading 0 */
  209. return (TRUE);
  210. }
  211. return (FALSE); /* Not an octal string */
  212. }
  213. /*******************************************************************************
  214. *
  215. * FUNCTION: acpi_ut_insert_digit
  216. *
  217. * PARAMETERS: accumulated_value - Current value of the integer value
  218. * accumulator. The new value is
  219. * returned here.
  220. * base - Radix, either 8/10/16
  221. * ascii_digit - ASCII single digit to be inserted
  222. *
  223. * RETURN: Status and result of the convert/insert operation. The only
  224. * possible returned exception code is numeric overflow of
  225. * either the multiply or add conversion operations.
  226. *
  227. * DESCRIPTION: Generic conversion and insertion function for all bases:
  228. *
  229. * 1) Multiply the current accumulated/converted value by the
  230. * base in order to make room for the new character.
  231. *
  232. * 2) Convert the new character to binary and add it to the
  233. * current accumulated value.
  234. *
  235. * Note: The only possible exception indicates an integer
  236. * overflow (AE_NUMERIC_OVERFLOW)
  237. *
  238. ******************************************************************************/
  239. static acpi_status
  240. acpi_ut_insert_digit(u64 *accumulated_value, u32 base, int ascii_digit)
  241. {
  242. acpi_status status;
  243. u64 product;
  244. /* Make room in the accumulated value for the incoming digit */
  245. status = acpi_ut_strtoul_multiply64(*accumulated_value, base, &product);
  246. if (ACPI_FAILURE(status)) {
  247. return (status);
  248. }
  249. /* Add in the new digit, and store the sum to the accumulated value */
  250. status =
  251. acpi_ut_strtoul_add64(product,
  252. acpi_ut_ascii_char_to_hex(ascii_digit),
  253. accumulated_value);
  254. return (status);
  255. }
  256. /*******************************************************************************
  257. *
  258. * FUNCTION: acpi_ut_strtoul_multiply64
  259. *
  260. * PARAMETERS: multiplicand - Current accumulated converted integer
  261. * base - Base/Radix
  262. * out_product - Where the product is returned
  263. *
  264. * RETURN: Status and 64-bit product
  265. *
  266. * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
  267. * well as 32-bit overflow if necessary (if the current global
  268. * integer width is 32).
  269. *
  270. ******************************************************************************/
  271. static acpi_status
  272. acpi_ut_strtoul_multiply64(u64 multiplicand, u32 base, u64 *out_product)
  273. {
  274. u64 product;
  275. u64 quotient;
  276. /* Exit if either operand is zero */
  277. *out_product = 0;
  278. if (!multiplicand || !base) {
  279. return (AE_OK);
  280. }
  281. /*
  282. * Check for 64-bit overflow before the actual multiplication.
  283. *
  284. * Notes: 64-bit division is often not supported on 32-bit platforms
  285. * (it requires a library function), Therefore ACPICA has a local
  286. * 64-bit divide function. Also, Multiplier is currently only used
  287. * as the radix (8/10/16), to the 64/32 divide will always work.
  288. */
  289. acpi_ut_short_divide(ACPI_UINT64_MAX, base, &quotient, NULL);
  290. if (multiplicand > quotient) {
  291. return (AE_NUMERIC_OVERFLOW);
  292. }
  293. product = multiplicand * base;
  294. /* Check for 32-bit overflow if necessary */
  295. if ((acpi_gbl_integer_bit_width == 32) && (product > ACPI_UINT32_MAX)) {
  296. return (AE_NUMERIC_OVERFLOW);
  297. }
  298. *out_product = product;
  299. return (AE_OK);
  300. }
  301. /*******************************************************************************
  302. *
  303. * FUNCTION: acpi_ut_strtoul_add64
  304. *
  305. * PARAMETERS: addend1 - Current accumulated converted integer
  306. * digit - New hex value/char
  307. * out_sum - Where sum is returned (Accumulator)
  308. *
  309. * RETURN: Status and 64-bit sum
  310. *
  311. * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
  312. * well as 32-bit overflow if necessary (if the current global
  313. * integer width is 32).
  314. *
  315. ******************************************************************************/
  316. static acpi_status acpi_ut_strtoul_add64(u64 addend1, u32 digit, u64 *out_sum)
  317. {
  318. u64 sum;
  319. /* Check for 64-bit overflow before the actual addition */
  320. if ((addend1 > 0) && (digit > (ACPI_UINT64_MAX - addend1))) {
  321. return (AE_NUMERIC_OVERFLOW);
  322. }
  323. sum = addend1 + digit;
  324. /* Check for 32-bit overflow if necessary */
  325. if ((acpi_gbl_integer_bit_width == 32) && (sum > ACPI_UINT32_MAX)) {
  326. return (AE_NUMERIC_OVERFLOW);
  327. }
  328. *out_sum = sum;
  329. return (AE_OK);
  330. }