PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/acpi/acpica/utstrsuppt.c

https://gitlab.com/Skylake/Staging
C | 428 lines | 149 code | 80 blank | 199 comment | 31 complexity | ffdab671cfff70df8c11400b77574a26 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. char *initial_position = *string;
  187. acpi_ut_remove_hex_prefix(string);
  188. if (*string != initial_position) {
  189. return (TRUE); /* String is past leading 0x */
  190. }
  191. return (FALSE); /* Not a hex string */
  192. }
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_ut_remove_hex_prefix
  196. *
  197. * PARAMETERS: string - Pointer to input ASCII string
  198. *
  199. * RETURN: none
  200. *
  201. * DESCRIPTION: Remove a hex "0x" prefix
  202. *
  203. ******************************************************************************/
  204. void acpi_ut_remove_hex_prefix(char **string)
  205. {
  206. if ((**string == ACPI_ASCII_ZERO) &&
  207. (tolower((int)*(*string + 1)) == 'x')) {
  208. *string += 2; /* Go past the leading 0x */
  209. }
  210. }
  211. /*******************************************************************************
  212. *
  213. * FUNCTION: acpi_ut_detect_octal_prefix
  214. *
  215. * PARAMETERS: string - Pointer to input ASCII string
  216. *
  217. * RETURN: True if an octal "0" prefix was found at the start of the
  218. * string
  219. *
  220. * DESCRIPTION: Detect and remove an octal prefix (zero)
  221. *
  222. ******************************************************************************/
  223. u8 acpi_ut_detect_octal_prefix(char **string)
  224. {
  225. if (**string == ACPI_ASCII_ZERO) {
  226. *string += 1; /* Go past the leading 0 */
  227. return (TRUE);
  228. }
  229. return (FALSE); /* Not an octal string */
  230. }
  231. /*******************************************************************************
  232. *
  233. * FUNCTION: acpi_ut_insert_digit
  234. *
  235. * PARAMETERS: accumulated_value - Current value of the integer value
  236. * accumulator. The new value is
  237. * returned here.
  238. * base - Radix, either 8/10/16
  239. * ascii_digit - ASCII single digit to be inserted
  240. *
  241. * RETURN: Status and result of the convert/insert operation. The only
  242. * possible returned exception code is numeric overflow of
  243. * either the multiply or add conversion operations.
  244. *
  245. * DESCRIPTION: Generic conversion and insertion function for all bases:
  246. *
  247. * 1) Multiply the current accumulated/converted value by the
  248. * base in order to make room for the new character.
  249. *
  250. * 2) Convert the new character to binary and add it to the
  251. * current accumulated value.
  252. *
  253. * Note: The only possible exception indicates an integer
  254. * overflow (AE_NUMERIC_OVERFLOW)
  255. *
  256. ******************************************************************************/
  257. static acpi_status
  258. acpi_ut_insert_digit(u64 *accumulated_value, u32 base, int ascii_digit)
  259. {
  260. acpi_status status;
  261. u64 product;
  262. /* Make room in the accumulated value for the incoming digit */
  263. status = acpi_ut_strtoul_multiply64(*accumulated_value, base, &product);
  264. if (ACPI_FAILURE(status)) {
  265. return (status);
  266. }
  267. /* Add in the new digit, and store the sum to the accumulated value */
  268. status =
  269. acpi_ut_strtoul_add64(product,
  270. acpi_ut_ascii_char_to_hex(ascii_digit),
  271. accumulated_value);
  272. return (status);
  273. }
  274. /*******************************************************************************
  275. *
  276. * FUNCTION: acpi_ut_strtoul_multiply64
  277. *
  278. * PARAMETERS: multiplicand - Current accumulated converted integer
  279. * base - Base/Radix
  280. * out_product - Where the product is returned
  281. *
  282. * RETURN: Status and 64-bit product
  283. *
  284. * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
  285. * well as 32-bit overflow if necessary (if the current global
  286. * integer width is 32).
  287. *
  288. ******************************************************************************/
  289. static acpi_status
  290. acpi_ut_strtoul_multiply64(u64 multiplicand, u32 base, u64 *out_product)
  291. {
  292. u64 product;
  293. u64 quotient;
  294. /* Exit if either operand is zero */
  295. *out_product = 0;
  296. if (!multiplicand || !base) {
  297. return (AE_OK);
  298. }
  299. /*
  300. * Check for 64-bit overflow before the actual multiplication.
  301. *
  302. * Notes: 64-bit division is often not supported on 32-bit platforms
  303. * (it requires a library function), Therefore ACPICA has a local
  304. * 64-bit divide function. Also, Multiplier is currently only used
  305. * as the radix (8/10/16), to the 64/32 divide will always work.
  306. */
  307. acpi_ut_short_divide(ACPI_UINT64_MAX, base, &quotient, NULL);
  308. if (multiplicand > quotient) {
  309. return (AE_NUMERIC_OVERFLOW);
  310. }
  311. product = multiplicand * base;
  312. /* Check for 32-bit overflow if necessary */
  313. if ((acpi_gbl_integer_bit_width == 32) && (product > ACPI_UINT32_MAX)) {
  314. return (AE_NUMERIC_OVERFLOW);
  315. }
  316. *out_product = product;
  317. return (AE_OK);
  318. }
  319. /*******************************************************************************
  320. *
  321. * FUNCTION: acpi_ut_strtoul_add64
  322. *
  323. * PARAMETERS: addend1 - Current accumulated converted integer
  324. * digit - New hex value/char
  325. * out_sum - Where sum is returned (Accumulator)
  326. *
  327. * RETURN: Status and 64-bit sum
  328. *
  329. * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
  330. * well as 32-bit overflow if necessary (if the current global
  331. * integer width is 32).
  332. *
  333. ******************************************************************************/
  334. static acpi_status acpi_ut_strtoul_add64(u64 addend1, u32 digit, u64 *out_sum)
  335. {
  336. u64 sum;
  337. /* Check for 64-bit overflow before the actual addition */
  338. if ((addend1 > 0) && (digit > (ACPI_UINT64_MAX - addend1))) {
  339. return (AE_NUMERIC_OVERFLOW);
  340. }
  341. sum = addend1 + digit;
  342. /* Check for 32-bit overflow if necessary */
  343. if ((acpi_gbl_integer_bit_width == 32) && (sum > ACPI_UINT32_MAX)) {
  344. return (AE_NUMERIC_OVERFLOW);
  345. }
  346. *out_sum = sum;
  347. return (AE_OK);
  348. }