utstrtoul64.c revision 1.1 1 1.1 christos /*******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: utstrtoul64 - string to 64-bit integer support
4 1.1 christos *
5 1.1 christos ******************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2016, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpi.h"
45 1.1 christos #include "accommon.h"
46 1.1 christos
47 1.1 christos
48 1.1 christos /*******************************************************************************
49 1.1 christos *
50 1.1 christos * The functions in this module satisfy the need for 64-bit string-to-integer
51 1.1 christos * conversions on both 32-bit and 64-bit platforms.
52 1.1 christos *
53 1.1 christos ******************************************************************************/
54 1.1 christos
55 1.1 christos #define _COMPONENT ACPI_UTILITIES
56 1.1 christos ACPI_MODULE_NAME ("utstrtoul64")
57 1.1 christos
58 1.1 christos /* Local prototypes */
59 1.1 christos
60 1.1 christos static UINT64
61 1.1 christos AcpiUtStrtoulBase10 (
62 1.1 christos char *String,
63 1.1 christos UINT32 Flags);
64 1.1 christos
65 1.1 christos static UINT64
66 1.1 christos AcpiUtStrtoulBase16 (
67 1.1 christos char *String,
68 1.1 christos UINT32 Flags);
69 1.1 christos
70 1.1 christos
71 1.1 christos /*******************************************************************************
72 1.1 christos *
73 1.1 christos * String conversion rules as written in the ACPI specification. The error
74 1.1 christos * conditions and behavior are different depending on the type of conversion.
75 1.1 christos *
76 1.1 christos *
77 1.1 christos * Implicit data type conversion: string-to-integer
78 1.1 christos * --------------------------------------------------
79 1.1 christos *
80 1.1 christos * Base is always 16. This is the ACPI_STRTOUL_BASE16 case.
81 1.1 christos *
82 1.1 christos * Example:
83 1.1 christos * Add ("BA98", Arg0, Local0)
84 1.1 christos *
85 1.1 christos * The integer is initialized to the value zero.
86 1.1 christos * The ASCII string is interpreted as a hexadecimal constant.
87 1.1 christos *
88 1.1 christos * 1) A "0x" prefix is not allowed. However, ACPICA allows this for
89 1.1 christos * compatibility with previous ACPICA. (NO ERROR)
90 1.1 christos *
91 1.1 christos * 2) Terminates when the size of an integer is reached (32 or 64 bits).
92 1.1 christos * (NO ERROR)
93 1.1 christos *
94 1.1 christos * 3) The first non-hex character terminates the conversion without error.
95 1.1 christos * (NO ERROR)
96 1.1 christos *
97 1.1 christos * 4) Conversion of a null (zero-length) string to an integer is not
98 1.1 christos * allowed. However, ACPICA allows this for compatibility with previous
99 1.1 christos * ACPICA. This conversion returns the value 0. (NO ERROR)
100 1.1 christos *
101 1.1 christos *
102 1.1 christos * Explicit data type conversion: ToInteger() with string operand
103 1.1 christos * ---------------------------------------------------------------
104 1.1 christos *
105 1.1 christos * Base is either 10 (default) or 16 (with 0x prefix)
106 1.1 christos *
107 1.1 christos * Examples:
108 1.1 christos * ToInteger ("1000")
109 1.1 christos * ToInteger ("0xABCD")
110 1.1 christos *
111 1.1 christos * 1) Can be (must be) either a decimal or hexadecimal numeric string.
112 1.1 christos * A hex value must be prefixed by "0x" or it is interpreted as a decimal.
113 1.1 christos *
114 1.1 christos * 2) The value must not exceed the maximum of an integer value. ACPI spec
115 1.1 christos * states the behavior is "unpredictable", so ACPICA matches the behavior
116 1.1 christos * of the implicit conversion case.(NO ERROR)
117 1.1 christos *
118 1.1 christos * 3) Behavior on the first non-hex character is not specified by the ACPI
119 1.1 christos * spec, so ACPICA matches the behavior of the implicit conversion case
120 1.1 christos * and terminates. (NO ERROR)
121 1.1 christos *
122 1.1 christos * 4) A null (zero-length) string is illegal.
123 1.1 christos * However, ACPICA allows this for compatibility with previous ACPICA.
124 1.1 christos * This conversion returns the value 0. (NO ERROR)
125 1.1 christos *
126 1.1 christos ******************************************************************************/
127 1.1 christos
128 1.1 christos
129 1.1 christos /*******************************************************************************
130 1.1 christos *
131 1.1 christos * FUNCTION: AcpiUtStrtoul64
132 1.1 christos *
133 1.1 christos * PARAMETERS: String - Null terminated input string
134 1.1 christos * Flags - Conversion info, see below
135 1.1 christos * ReturnValue - Where the converted integer is
136 1.1 christos * returned
137 1.1 christos *
138 1.1 christos * RETURN: Status and Converted value
139 1.1 christos *
140 1.1 christos * DESCRIPTION: Convert a string into an unsigned value. Performs either a
141 1.1 christos * 32-bit or 64-bit conversion, depending on the input integer
142 1.1 christos * size in Flags (often the current mode of the interpreter).
143 1.1 christos *
144 1.1 christos * Values for Flags:
145 1.1 christos * ACPI_STRTOUL_32BIT - Max integer value is 32 bits
146 1.1 christos * ACPI_STRTOUL_64BIT - Max integer value is 64 bits
147 1.1 christos * ACPI_STRTOUL_BASE16 - Input string is hexadecimal. Default
148 1.1 christos * is 10/16 based on string prefix (0x).
149 1.1 christos *
150 1.1 christos * NOTES:
151 1.1 christos * Negative numbers are not supported, as they are not supported by ACPI.
152 1.1 christos *
153 1.1 christos * Supports only base 16 or base 10 strings/values. Does not
154 1.1 christos * support Octal strings, as these are not supported by ACPI.
155 1.1 christos *
156 1.1 christos * Current users of this support:
157 1.1 christos *
158 1.1 christos * Interpreter - Implicit and explicit conversions, GPE method names
159 1.1 christos * Debugger - Command line input string conversion
160 1.1 christos * iASL - Main parser, conversion of constants to integers
161 1.1 christos * iASL - Data Table Compiler parser (constant math expressions)
162 1.1 christos * iASL - Preprocessor (constant math expressions)
163 1.1 christos * AcpiDump - Input table addresses
164 1.1 christos * AcpiExec - Testing of the AcpiUtStrtoul64 function
165 1.1 christos *
166 1.1 christos * Note concerning callers:
167 1.1 christos * AcpiGbl_IntegerByteWidth can be used to set the 32/64 limit. If used,
168 1.1 christos * this global should be set to the proper width. For the core ACPICA code,
169 1.1 christos * this width depends on the DSDT version. For iASL, the default byte
170 1.1 christos * width is always 8 for the parser, but error checking is performed later
171 1.1 christos * to flag cases where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
172 1.1 christos *
173 1.1 christos ******************************************************************************/
174 1.1 christos
175 1.1 christos ACPI_STATUS
176 1.1 christos AcpiUtStrtoul64 (
177 1.1 christos char *String,
178 1.1 christos UINT32 Flags,
179 1.1 christos UINT64 *ReturnValue)
180 1.1 christos {
181 1.1 christos ACPI_STATUS Status = AE_OK;
182 1.1 christos UINT32 Base;
183 1.1 christos
184 1.1 christos
185 1.1 christos ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
186 1.1 christos
187 1.1 christos
188 1.1 christos /* Parameter validation */
189 1.1 christos
190 1.1 christos if (!String || !ReturnValue)
191 1.1 christos {
192 1.1 christos return_ACPI_STATUS (AE_BAD_PARAMETER);
193 1.1 christos }
194 1.1 christos
195 1.1 christos *ReturnValue = 0;
196 1.1 christos
197 1.1 christos /* Check for zero-length string, returns 0 */
198 1.1 christos
199 1.1 christos if (*String == 0)
200 1.1 christos {
201 1.1 christos return_ACPI_STATUS (AE_OK);
202 1.1 christos }
203 1.1 christos
204 1.1 christos /* Skip over any white space at start of string */
205 1.1 christos
206 1.1 christos while (isspace ((int) *String))
207 1.1 christos {
208 1.1 christos String++;
209 1.1 christos }
210 1.1 christos
211 1.1 christos /* End of string? return 0 */
212 1.1 christos
213 1.1 christos if (*String == 0)
214 1.1 christos {
215 1.1 christos return_ACPI_STATUS (AE_OK);
216 1.1 christos }
217 1.1 christos
218 1.1 christos /*
219 1.1 christos * 1) The "0x" prefix indicates base 16. Per the ACPI specification,
220 1.1 christos * the "0x" prefix is only allowed for implicit (non-strict) conversions.
221 1.1 christos * However, we always allow it for compatibility with older ACPICA.
222 1.1 christos */
223 1.1 christos if ((*String == ACPI_ASCII_ZERO) &&
224 1.1 christos (tolower ((int) *(String + 1)) == 'x'))
225 1.1 christos {
226 1.1 christos String += 2; /* Go past the 0x */
227 1.1 christos if (*String == 0)
228 1.1 christos {
229 1.1 christos return_ACPI_STATUS (AE_OK); /* Return value 0 */
230 1.1 christos }
231 1.1 christos
232 1.1 christos Base = 16;
233 1.1 christos }
234 1.1 christos
235 1.1 christos /* 2) Force to base 16 (implicit conversion case) */
236 1.1 christos
237 1.1 christos else if (Flags & ACPI_STRTOUL_BASE16)
238 1.1 christos {
239 1.1 christos Base = 16;
240 1.1 christos }
241 1.1 christos
242 1.1 christos /* 3) Default fallback is to Base 10 */
243 1.1 christos
244 1.1 christos else
245 1.1 christos {
246 1.1 christos Base = 10;
247 1.1 christos }
248 1.1 christos
249 1.1 christos /* Skip all leading zeros */
250 1.1 christos
251 1.1 christos while (*String == ACPI_ASCII_ZERO)
252 1.1 christos {
253 1.1 christos String++;
254 1.1 christos if (*String == 0)
255 1.1 christos {
256 1.1 christos return_ACPI_STATUS (AE_OK); /* Return value 0 */
257 1.1 christos }
258 1.1 christos }
259 1.1 christos
260 1.1 christos /* Perform the base 16 or 10 conversion */
261 1.1 christos
262 1.1 christos if (Base == 16)
263 1.1 christos {
264 1.1 christos *ReturnValue = AcpiUtStrtoulBase16 (String, Flags);
265 1.1 christos }
266 1.1 christos else
267 1.1 christos {
268 1.1 christos *ReturnValue = AcpiUtStrtoulBase10 (String, Flags);
269 1.1 christos }
270 1.1 christos
271 1.1 christos return_ACPI_STATUS (Status);
272 1.1 christos }
273 1.1 christos
274 1.1 christos
275 1.1 christos /*******************************************************************************
276 1.1 christos *
277 1.1 christos * FUNCTION: AcpiUtStrtoulBase10
278 1.1 christos *
279 1.1 christos * PARAMETERS: String - Null terminated input string
280 1.1 christos * Flags - Conversion info
281 1.1 christos *
282 1.1 christos * RETURN: 64-bit converted integer
283 1.1 christos *
284 1.1 christos * DESCRIPTION: Performs a base 10 conversion of the input string to an
285 1.1 christos * integer value, either 32 or 64 bits.
286 1.1 christos * Note: String must be valid and non-null.
287 1.1 christos *
288 1.1 christos ******************************************************************************/
289 1.1 christos
290 1.1 christos static UINT64
291 1.1 christos AcpiUtStrtoulBase10 (
292 1.1 christos char *String,
293 1.1 christos UINT32 Flags)
294 1.1 christos {
295 1.1 christos int AsciiDigit;
296 1.1 christos UINT64 NextValue;
297 1.1 christos UINT64 ReturnValue = 0;
298 1.1 christos
299 1.1 christos
300 1.1 christos /* Main loop: convert each ASCII byte in the input string */
301 1.1 christos
302 1.1 christos while (*String)
303 1.1 christos {
304 1.1 christos AsciiDigit = *String;
305 1.1 christos if (!isdigit (AsciiDigit))
306 1.1 christos {
307 1.1 christos /* Not ASCII 0-9, terminate */
308 1.1 christos
309 1.1 christos goto Exit;
310 1.1 christos }
311 1.1 christos
312 1.1 christos /* Convert and insert (add) the decimal digit */
313 1.1 christos
314 1.1 christos NextValue =
315 1.1 christos (ReturnValue * 10) + (AsciiDigit - ACPI_ASCII_ZERO);
316 1.1 christos
317 1.1 christos /* Check for overflow (32 or 64 bit) - return current converted value */
318 1.1 christos
319 1.1 christos if (((Flags & ACPI_STRTOUL_32BIT) && (NextValue > ACPI_UINT32_MAX)) ||
320 1.1 christos (NextValue < ReturnValue)) /* 64-bit overflow case */
321 1.1 christos {
322 1.1 christos goto Exit;
323 1.1 christos }
324 1.1 christos
325 1.1 christos ReturnValue = NextValue;
326 1.1 christos String++;
327 1.1 christos }
328 1.1 christos
329 1.1 christos Exit:
330 1.1 christos return (ReturnValue);
331 1.1 christos }
332 1.1 christos
333 1.1 christos
334 1.1 christos /*******************************************************************************
335 1.1 christos *
336 1.1 christos * FUNCTION: AcpiUtStrtoulBase16
337 1.1 christos *
338 1.1 christos * PARAMETERS: String - Null terminated input string
339 1.1 christos * Flags - conversion info
340 1.1 christos *
341 1.1 christos * RETURN: 64-bit converted integer
342 1.1 christos *
343 1.1 christos * DESCRIPTION: Performs a base 16 conversion of the input string to an
344 1.1 christos * integer value, either 32 or 64 bits.
345 1.1 christos * Note: String must be valid and non-null.
346 1.1 christos *
347 1.1 christos ******************************************************************************/
348 1.1 christos
349 1.1 christos static UINT64
350 1.1 christos AcpiUtStrtoulBase16 (
351 1.1 christos char *String,
352 1.1 christos UINT32 Flags)
353 1.1 christos {
354 1.1 christos int AsciiDigit;
355 1.1 christos UINT32 ValidDigits = 1;
356 1.1 christos UINT64 ReturnValue = 0;
357 1.1 christos
358 1.1 christos
359 1.1 christos /* Main loop: convert each ASCII byte in the input string */
360 1.1 christos
361 1.1 christos while (*String)
362 1.1 christos {
363 1.1 christos /* Check for overflow (32 or 64 bit) - return current converted value */
364 1.1 christos
365 1.1 christos if ((ValidDigits > 16) ||
366 1.1 christos ((ValidDigits > 8) && (Flags & ACPI_STRTOUL_32BIT)))
367 1.1 christos {
368 1.1 christos goto Exit;
369 1.1 christos }
370 1.1 christos
371 1.1 christos AsciiDigit = *String;
372 1.1 christos if (!isxdigit (AsciiDigit))
373 1.1 christos {
374 1.1 christos /* Not Hex ASCII A-F, a-f, or 0-9, terminate */
375 1.1 christos
376 1.1 christos goto Exit;
377 1.1 christos }
378 1.1 christos
379 1.1 christos /* Convert and insert the hex digit */
380 1.1 christos
381 1.1 christos ReturnValue =
382 1.1 christos (ReturnValue << 4) | AcpiUtAsciiCharToHex (AsciiDigit);
383 1.1 christos
384 1.1 christos String++;
385 1.1 christos ValidDigits++;
386 1.1 christos }
387 1.1 christos
388 1.1 christos Exit:
389 1.1 christos return (ReturnValue);
390 1.1 christos }
391