utstrtoul64.c (0d3c24e936feefeca854073ccb40613cd6eba9a9) | utstrtoul64.c (fe97d28704147ba72f7d7859909f80b4bb5a17d4) |
---|---|
1/******************************************************************************* 2 * | 1/******************************************************************************* 2 * |
3 * Module Name: utstrtoul64 - string to 64-bit integer support | 3 * Module Name: utstrtoul64 - string-to-integer support for both 64-bit 4 * and 32-bit integers |
4 * 5 ******************************************************************************/ 6 7/* 8 * Copyright (C) 2000 - 2017, Intel Corp. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without --- 27 unchanged lines hidden (view full) --- 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGES. 42 */ 43 44#include <acpi/acpi.h> 45#include "accommon.h" 46 | 5 * 6 ******************************************************************************/ 7 8/* 9 * Copyright (C) 2000 - 2017, Intel Corp. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without --- 27 unchanged lines hidden (view full) --- 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42 * POSSIBILITY OF SUCH DAMAGES. 43 */ 44 45#include <acpi/acpi.h> 46#include "accommon.h" 47 |
47/******************************************************************************* 48 * 49 * The functions in this module satisfy the need for 64-bit string-to-integer 50 * conversions on both 32-bit and 64-bit platforms. 51 * 52 ******************************************************************************/ 53 | |
54#define _COMPONENT ACPI_UTILITIES 55ACPI_MODULE_NAME("utstrtoul64") 56 | 48#define _COMPONENT ACPI_UTILITIES 49ACPI_MODULE_NAME("utstrtoul64") 50 |
57/* Local prototypes */ 58static u64 acpi_ut_strtoul_base10(char *string, u32 flags); 59 60static u64 acpi_ut_strtoul_base16(char *string, u32 flags); 61 | |
62/******************************************************************************* 63 * | 51/******************************************************************************* 52 * |
64 * String conversion rules as written in the ACPI specification. The error 65 * conditions and behavior are different depending on the type of conversion. | 53 * This module contains the external string to 64/32-bit unsigned integer 54 * conversion functions: |
66 * | 55 * |
56 * 1) Standard strtoul() function with 64-bit support. This is mostly used by 57 * the iASL compiler. 58 * 2) Runtime "Explicit conversion" as defined in the ACPI specification. 59 * 3) Runtime "Implicit conversion" as defined in the ACPI specification. |
|
67 * | 60 * |
68 * Implicit data type conversion: string-to-integer 69 * -------------------------------------------------- | 61 * Current users of this module: |
70 * | 62 * |
71 * Base is always 16. This is the ACPI_STRTOUL_BASE16 case. | 63 * interpreter - Implicit and explicit conversions, GPE method names 64 * debugger - Command line input string conversion 65 * iASL - Main parser, conversion of constants to integers 66 * iASL - Data Table Compiler parser (constant math expressions) 67 * iASL - Preprocessor (constant math expressions) 68 * acpi_dump - Input table addresses 69 * acpi_exec - Testing of the acpi_ut_strtoul64 function |
72 * | 70 * |
73 * Example: 74 * Add ("BA98", Arg0, Local0) | 71 * Notes concerning users of these interfaces: |
75 * | 72 * |
76 * The integer is initialized to the value zero. 77 * The ASCII string is interpreted as a hexadecimal constant. | 73 * acpi_gbl_integer_byte_width is used to set the 32/64 bit limit. This global 74 * must be set to the proper width. For the core ACPICA code, the width 75 * depends on the DSDT version. For iASL, the default width is 64 bits for 76 * all parsers, but error checking is performed later to flag cases where 77 * a 64-bit constant is wrongly defined in a 32-bit DSDT/SSDT. |
78 * | 78 * |
79 * 1) A "0x" prefix is not allowed. However, ACPICA allows this for 80 * compatibility with previous ACPICA. (NO ERROR) | 79 * In ACPI, the only place where octal numbers are supported is within 80 * the ASL language itself. There is no runtime support for octal. |
81 * | 81 * |
82 * 2) Terminates when the size of an integer is reached (32 or 64 bits). 83 * (NO ERROR) 84 * 85 * 3) The first non-hex character terminates the conversion without error. 86 * (NO ERROR) 87 * 88 * 4) Conversion of a null (zero-length) string to an integer is not 89 * allowed. However, ACPICA allows this for compatibility with previous 90 * ACPICA. This conversion returns the value 0. (NO ERROR) 91 * 92 * 93 * Explicit data type conversion: to_integer() with string operand 94 * --------------------------------------------------------------- 95 * 96 * Base is either 10 (default) or 16 (with 0x prefix) 97 * 98 * Examples: 99 * to_integer ("1000") 100 * to_integer ("0xABCD") 101 * 102 * 1) Can be (must be) either a decimal or hexadecimal numeric string. 103 * A hex value must be prefixed by "0x" or it is interpreted as a decimal. 104 * 105 * 2) The value must not exceed the maximum of an integer value. ACPI spec 106 * states the behavior is "unpredictable", so ACPICA matches the behavior 107 * of the implicit conversion case.(NO ERROR) 108 * 109 * 3) Behavior on the first non-hex character is not specified by the ACPI 110 * spec, so ACPICA matches the behavior of the implicit conversion case 111 * and terminates. (NO ERROR) 112 * 113 * 4) A null (zero-length) string is illegal. 114 * However, ACPICA allows this for compatibility with previous ACPICA. 115 * This conversion returns the value 0. (NO ERROR) 116 * | |
117 ******************************************************************************/ | 82 ******************************************************************************/ |
118 | |
119/******************************************************************************* 120 * 121 * FUNCTION: acpi_ut_strtoul64 122 * | 83/******************************************************************************* 84 * 85 * FUNCTION: acpi_ut_strtoul64 86 * |
123 * PARAMETERS: string - Null terminated input string 124 * flags - Conversion info, see below | 87 * PARAMETERS: string - Null terminated input string. 88 * Must be a valid pointer |
125 * return_value - Where the converted integer is | 89 * return_value - Where the converted integer is |
126 * returned | 90 * returned. Must be a valid pointer |
127 * | 91 * |
128 * RETURN: Status and Converted value | 92 * RETURN: Status and converted integer 93 * Returns an exception on numeric overflow |
129 * | 94 * |
130 * DESCRIPTION: Convert a string into an unsigned value. Performs either a 131 * 32-bit or 64-bit conversion, depending on the input integer 132 * size in Flags (often the current mode of the interpreter). | 95 * DESCRIPTION: Convert a string into an unsigned integer. Performs either a 96 * 32-bit or 64-bit conversion, depending on the current global 97 * integer width. Supports Decimal, Hex, and Octal strings. |
133 * | 98 * |
134 * Values for Flags: 135 * ACPI_STRTOUL_32BIT - Max integer value is 32 bits 136 * ACPI_STRTOUL_64BIT - Max integer value is 64 bits 137 * ACPI_STRTOUL_BASE16 - Input string is hexadecimal. Default 138 * is 10/16 based on string prefix (0x). | 99 * Current users of this function: |
139 * | 100 * |
140 * NOTES: 141 * Negative numbers are not supported, as they are not supported by ACPI. 142 * 143 * Supports only base 16 or base 10 strings/values. Does not 144 * support Octal strings, as these are not supported by ACPI. 145 * 146 * Current users of this support: 147 * 148 * interpreter - Implicit and explicit conversions, GPE method names 149 * debugger - Command line input string conversion 150 * iASL - Main parser, conversion of constants to integers 151 * iASL - Data Table Compiler parser (constant math expressions) | |
152 * iASL - Preprocessor (constant math expressions) | 101 * iASL - Preprocessor (constant math expressions) |
153 * acpi_dump - Input table addresses 154 * acpi_exec - Testing of the acpi_ut_strtoul64 function | 102 * iASL - Main parser, conversion of ASL constants to integers 103 * iASL - Data Table Compiler parser (constant math expressions) |
155 * | 104 * |
156 * Note concerning callers: 157 * acpi_gbl_integer_byte_width can be used to set the 32/64 limit. If used, 158 * this global should be set to the proper width. For the core ACPICA code, 159 * this width depends on the DSDT version. For iASL, the default byte 160 * width is always 8 for the parser, but error checking is performed later 161 * to flag cases where a 64-bit constant is defined in a 32-bit DSDT/SSDT. 162 * | |
163 ******************************************************************************/ | 105 ******************************************************************************/ |
164 165acpi_status acpi_ut_strtoul64(char *string, u32 flags, u64 *return_value) | 106acpi_status acpi_ut_strtoul64(char *string, u64 *return_value) |
166{ 167 acpi_status status = AE_OK; | 107{ 108 acpi_status status = AE_OK; |
168 u32 base; | 109 u32 base = 10; /* Default is decimal */ |
169 170 ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string); 171 | 110 111 ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string); 112 |
172 /* Parameter validation */ 173 174 if (!string || !return_value) { 175 return_ACPI_STATUS(AE_BAD_PARAMETER); 176 } 177 | |
178 *return_value = 0; 179 | 113 *return_value = 0; 114 |
180 /* Check for zero-length string, returns 0 */ | 115 /* Null return string returns a value of zero */ |
181 182 if (*string == 0) { 183 return_ACPI_STATUS(AE_OK); 184 } 185 | 116 117 if (*string == 0) { 118 return_ACPI_STATUS(AE_OK); 119 } 120 |
186 /* Skip over any white space at start of string */ 187 188 while (isspace((int)*string)) { 189 string++; 190 } 191 192 /* End of string? return 0 */ 193 194 if (*string == 0) { 195 return_ACPI_STATUS(AE_OK); 196 } 197 | |
198 /* 199 * 1) The "0x" prefix indicates base 16. Per the ACPI specification, 200 * the "0x" prefix is only allowed for implicit (non-strict) conversions. | 121 /* 122 * 1) The "0x" prefix indicates base 16. Per the ACPI specification, 123 * the "0x" prefix is only allowed for implicit (non-strict) conversions. |
201 * However, we always allow it for compatibility with older ACPICA. | 124 * However, we always allow it for compatibility with older ACPICA and 125 * just plain on principle. |
202 */ | 126 */ |
203 if ((*string == ACPI_ASCII_ZERO) && 204 (tolower((int)*(string + 1)) == 'x')) { 205 string += 2; /* Go past the 0x */ 206 if (*string == 0) { 207 return_ACPI_STATUS(AE_OK); /* Return value 0 */ 208 } 209 | 127 if (acpi_ut_detect_hex_prefix(&string)) { |
210 base = 16; 211 } 212 | 128 base = 16; 129 } 130 |
213 /* 2) Force to base 16 (implicit conversion case) */ 214 215 else if (flags & ACPI_STRTOUL_BASE16) { 216 base = 16; | 131 /* 132 * 2) Check for an octal constant, defined to be a leading zero 133 * followed by an valid octal digit (0-7) 134 */ 135 else if (acpi_ut_detect_octal_prefix(&string)) { 136 base = 8; |
217 } 218 | 137 } 138 |
219 /* 3) Default fallback is to Base 10 */ 220 221 else { 222 base = 10; | 139 if (!acpi_ut_remove_leading_zeros(&string)) { 140 return_ACPI_STATUS(AE_OK); /* Return value 0 */ |
223 } 224 | 141 } 142 |
225 /* Skip all leading zeros */ | 143 /* 144 * Perform the base 8, 10, or 16 conversion. A numeric overflow will 145 * return an exception. 146 */ 147 switch (base) { 148 case 8: 149 status = acpi_ut_convert_octal_string(string, return_value); 150 break; |
226 | 151 |
227 while (*string == ACPI_ASCII_ZERO) { 228 string++; 229 if (*string == 0) { 230 return_ACPI_STATUS(AE_OK); /* Return value 0 */ 231 } 232 } | 152 case 10: 153 status = acpi_ut_convert_decimal_string(string, return_value); 154 break; |
233 | 155 |
234 /* Perform the base 16 or 10 conversion */ | 156 case 16: 157 status = acpi_ut_convert_hex_string(string, return_value); 158 break; |
235 | 159 |
236 if (base == 16) { 237 *return_value = acpi_ut_strtoul_base16(string, flags); 238 } else { 239 *return_value = acpi_ut_strtoul_base10(string, flags); | 160 default: 161 status = AE_AML_INTERNAL; /* Should never happen */ 162 break; |
240 } 241 242 return_ACPI_STATUS(status); 243} 244 245/******************************************************************************* 246 * | 163 } 164 165 return_ACPI_STATUS(status); 166} 167 168/******************************************************************************* 169 * |
247 * FUNCTION: acpi_ut_strtoul_base10 | 170 * FUNCTION: acpi_ut_implicit_strtoul64 |
248 * | 171 * |
249 * PARAMETERS: string - Null terminated input string 250 * flags - Conversion info | 172 * PARAMETERS: string - Null terminated input string. 173 * Must be a valid pointer |
251 * | 174 * |
252 * RETURN: 64-bit converted integer | 175 * RETURN: Converted integer |
253 * | 176 * |
254 * DESCRIPTION: Performs a base 10 conversion of the input string to an 255 * integer value, either 32 or 64 bits. 256 * Note: String must be valid and non-null. | 177 * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon 178 * an "implicit conversion" by the ACPI specification. Used by 179 * many ASL operators that require an integer operand, and support 180 * an automatic (implicit) conversion from a string operand 181 * to the final integer operand. The restriction is that only 182 * hex strings are supported. |
257 * | 183 * |
184 * ----------------------------------------------------------------------------- 185 * 186 * Base is always 16, either with or without the 0x prefix. 187 * 188 * Examples (both are hex values): 189 * Add ("BA98", Arg0, Local0) 190 * Subtract ("0x12345678", Arg1, Local1) 191 * 192 * Rules extracted from the ACPI specification: 193 * 194 * The converted integer is initialized to the value zero. 195 * The ASCII string is interpreted as a hexadecimal constant. 196 * 197 * 1) A "0x" prefix is not allowed. However, ACPICA allows this as an 198 * ACPI extension on general principle. (NO ERROR) 199 * 200 * 2) Terminates when the size of an integer is reached (32 or 64 bits). 201 * There are no numeric overflow conditions. (NO ERROR) 202 * 203 * 3) The first non-hex character terminates the conversion and returns 204 * the current accumulated value of the converted integer (NO ERROR). 205 * 206 * 4) Conversion of a null (zero-length) string to an integer is 207 * technically allowed. However, ACPICA allows as an ACPI extension. 208 * The conversion returns the value 0. (NO ERROR) 209 * 210 * Note: there are no error conditions returned by this function. At 211 * the minimum, a value of zero is returned. 212 * 213 * Current users of this function: 214 * 215 * interpreter - All runtime implicit conversions, as per ACPI specification 216 * iASL - Data Table Compiler parser (constant math expressions) 217 * |
|
258 ******************************************************************************/ 259 | 218 ******************************************************************************/ 219 |
260static u64 acpi_ut_strtoul_base10(char *string, u32 flags) | 220u64 acpi_ut_implicit_strtoul64(char *string) |
261{ | 221{ |
262 int ascii_digit; 263 u64 next_value; 264 u64 return_value = 0; | 222 u64 converted_integer = 0; |
265 | 223 |
266 /* Main loop: convert each ASCII byte in the input string */ | 224 ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string); |
267 | 225 |
268 while (*string) { 269 ascii_digit = *string; 270 if (!isdigit(ascii_digit)) { | 226 /* 227 * Per the ACPI specification, only hexadecimal is supported for 228 * implicit conversions, and the "0x" prefix is "not allowed". 229 * However, allow a "0x" prefix as an ACPI extension. 230 */ 231 acpi_ut_detect_hex_prefix(&string); |
271 | 232 |
272 /* Not ASCII 0-9, terminate */ 273 274 goto exit; 275 } 276 277 /* Convert and insert (add) the decimal digit */ 278 279 acpi_ut_short_multiply(return_value, 10, &next_value); 280 next_value += (ascii_digit - ACPI_ASCII_ZERO); 281 282 /* Check for overflow (32 or 64 bit) - return current converted value */ 283 284 if (((flags & ACPI_STRTOUL_32BIT) && (next_value > ACPI_UINT32_MAX)) || (next_value < return_value)) { /* 64-bit overflow case */ 285 goto exit; 286 } 287 288 return_value = next_value; 289 string++; | 233 if (!acpi_ut_remove_leading_zeros(&string)) { 234 return_VALUE(0); |
290 } 291 | 235 } 236 |
292exit: 293 return (return_value); | 237 /* 238 * Ignore overflow as per the ACPI specification. This is implemented by 239 * ignoring the return status below. On overflow, the input string is 240 * simply truncated. 241 */ 242 acpi_ut_convert_hex_string(string, &converted_integer); 243 return_VALUE(converted_integer); |
294} 295 296/******************************************************************************* 297 * | 244} 245 246/******************************************************************************* 247 * |
298 * FUNCTION: acpi_ut_strtoul_base16 | 248 * FUNCTION: acpi_ut_explicit_strtoul64 |
299 * | 249 * |
300 * PARAMETERS: string - Null terminated input string 301 * flags - conversion info | 250 * PARAMETERS: string - Null terminated input string. 251 * Must be a valid pointer |
302 * | 252 * |
303 * RETURN: 64-bit converted integer | 253 * RETURN: Converted integer |
304 * | 254 * |
305 * DESCRIPTION: Performs a base 16 conversion of the input string to an 306 * integer value, either 32 or 64 bits. 307 * Note: String must be valid and non-null. | 255 * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon 256 * an "explicit conversion" by the ACPI specification. The 257 * main restriction is that only hex and decimal are supported. |
308 * | 258 * |
259 * ----------------------------------------------------------------------------- 260 * 261 * Base is either 10 (default) or 16 (with 0x prefix). There is no octal 262 * (base 8), as per the ACPI specification. 263 * 264 * Examples: 265 * to_integer ("1000") Decimal 266 * to_integer ("0xABCD") Hex 267 * 268 * Rules extracted from the ACPI specification: 269 * 270 * 1) Thi input string is either a decimal or hexadecimal numeric string. 271 * A hex value must be prefixed by "0x" or it is interpreted as decimal. 272 * 273 * 2) The value must not exceed the maximum of an integer value 274 * (32 or 64 bits). The ACPI specification states the behavior is 275 * "unpredictable", so ACPICA matches the behavior of the implicit 276 * conversion case. There are no numeric overflow conditions. (NO ERROR) 277 * 278 * 3) Behavior on the first non-hex character is not specified by the ACPI 279 * specification (for the to_integer operator), so ACPICA matches the 280 * behavior of the implicit conversion case. It terminates the 281 * conversion and returns the current accumulated value of the converted 282 * integer. (NO ERROR) 283 * 284 * 4) Conversion of a null (zero-length) string to an integer is 285 * technically allowed. However, ACPICA allows as an ACPI extension. 286 * The conversion returns the value 0. (NO ERROR) 287 * 288 * Note: there are no error conditions returned by this function. At 289 * the minimum, a value of zero is returned. 290 * 291 * Current users of this function: 292 * 293 * interpreter - Runtime ASL to_integer operator, as per the ACPI specification 294 * |
|
309 ******************************************************************************/ 310 | 295 ******************************************************************************/ 296 |
311static u64 acpi_ut_strtoul_base16(char *string, u32 flags) | 297u64 acpi_ut_explicit_strtoul64(char *string) |
312{ | 298{ |
313 int ascii_digit; 314 u32 valid_digits = 1; 315 u64 return_value = 0; | 299 u64 converted_integer = 0; 300 u32 base = 10; /* Default is decimal */ |
316 | 301 |
317 /* Main loop: convert each ASCII byte in the input string */ | 302 ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string); |
318 | 303 |
319 while (*string) { | 304 /* 305 * Only Hex and Decimal are supported, as per the ACPI specification. 306 * 0x prefix means hex; otherwise decimal is assumed. 307 */ 308 if (acpi_ut_detect_hex_prefix(&string)) { 309 base = 16; 310 } |
320 | 311 |
321 /* Check for overflow (32 or 64 bit) - return current converted value */ | 312 if (!acpi_ut_remove_leading_zeros(&string)) { 313 return_VALUE(0); 314 } |
322 | 315 |
323 if ((valid_digits > 16) || 324 ((valid_digits > 8) && (flags & ACPI_STRTOUL_32BIT))) { 325 goto exit; 326 } | 316 /* 317 * Ignore overflow as per the ACPI specification. This is implemented by 318 * ignoring the return status below. On overflow, the input string is 319 * simply truncated. 320 */ 321 switch (base) { 322 case 10: 323 default: 324 acpi_ut_convert_decimal_string(string, &converted_integer); 325 break; |
327 | 326 |
328 ascii_digit = *string; 329 if (!isxdigit(ascii_digit)) { 330 331 /* Not Hex ASCII A-F, a-f, or 0-9, terminate */ 332 333 goto exit; 334 } 335 336 /* Convert and insert the hex digit */ 337 338 acpi_ut_short_shift_left(return_value, 4, &return_value); 339 return_value |= acpi_ut_ascii_char_to_hex(ascii_digit); 340 341 string++; 342 valid_digits++; | 327 case 16: 328 acpi_ut_convert_hex_string(string, &converted_integer); 329 break; |
343 } 344 | 330 } 331 |
345exit: 346 return (return_value); | 332 return_VALUE(converted_integer); |
347} | 333} |