utstrsuppt.c revision 1.1.1.2 1 1.1 christos /*******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: utstrsuppt - Support functions for string-to-integer conversion
4 1.1 christos *
5 1.1 christos ******************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2017, 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 #define _COMPONENT ACPI_UTILITIES
48 1.1 christos ACPI_MODULE_NAME ("utstrsuppt")
49 1.1 christos
50 1.1 christos
51 1.1 christos /* Local prototypes */
52 1.1 christos
53 1.1 christos static ACPI_STATUS
54 1.1 christos AcpiUtInsertDigit (
55 1.1 christos UINT64 *AccumulatedValue,
56 1.1 christos UINT32 Base,
57 1.1 christos int AsciiDigit);
58 1.1 christos
59 1.1 christos static ACPI_STATUS
60 1.1 christos AcpiUtStrtoulMultiply64 (
61 1.1 christos UINT64 Multiplicand,
62 1.1.1.2 christos UINT32 Base,
63 1.1 christos UINT64 *OutProduct);
64 1.1 christos
65 1.1 christos static ACPI_STATUS
66 1.1 christos AcpiUtStrtoulAdd64 (
67 1.1 christos UINT64 Addend1,
68 1.1.1.2 christos UINT32 Digit,
69 1.1 christos UINT64 *OutSum);
70 1.1 christos
71 1.1 christos
72 1.1 christos /*******************************************************************************
73 1.1 christos *
74 1.1 christos * FUNCTION: AcpiUtConvertOctalString
75 1.1 christos *
76 1.1 christos * PARAMETERS: String - Null terminated input string
77 1.1 christos * ReturnValuePtr - Where the converted value is returned
78 1.1 christos *
79 1.1 christos * RETURN: Status and 64-bit converted integer
80 1.1 christos *
81 1.1 christos * DESCRIPTION: Performs a base 8 conversion of the input string to an
82 1.1 christos * integer value, either 32 or 64 bits.
83 1.1 christos *
84 1.1 christos * NOTE: Maximum 64-bit unsigned octal value is 01777777777777777777777
85 1.1 christos * Maximum 32-bit unsigned octal value is 037777777777
86 1.1 christos *
87 1.1 christos ******************************************************************************/
88 1.1 christos
89 1.1 christos ACPI_STATUS
90 1.1 christos AcpiUtConvertOctalString (
91 1.1 christos char *String,
92 1.1 christos UINT64 *ReturnValuePtr)
93 1.1 christos {
94 1.1 christos UINT64 AccumulatedValue = 0;
95 1.1 christos ACPI_STATUS Status = AE_OK;
96 1.1 christos
97 1.1 christos
98 1.1 christos /* Convert each ASCII byte in the input string */
99 1.1 christos
100 1.1 christos while (*String)
101 1.1 christos {
102 1.1 christos /* Character must be ASCII 0-7, otherwise terminate with no error */
103 1.1 christos
104 1.1 christos if (!(ACPI_IS_OCTAL_DIGIT (*String)))
105 1.1 christos {
106 1.1 christos break;
107 1.1 christos }
108 1.1 christos
109 1.1 christos /* Convert and insert this octal digit into the accumulator */
110 1.1 christos
111 1.1 christos Status = AcpiUtInsertDigit (&AccumulatedValue, 8, *String);
112 1.1 christos if (ACPI_FAILURE (Status))
113 1.1 christos {
114 1.1 christos Status = AE_OCTAL_OVERFLOW;
115 1.1 christos break;
116 1.1 christos }
117 1.1 christos
118 1.1 christos String++;
119 1.1 christos }
120 1.1 christos
121 1.1 christos /* Always return the value that has been accumulated */
122 1.1 christos
123 1.1 christos *ReturnValuePtr = AccumulatedValue;
124 1.1 christos return (Status);
125 1.1 christos }
126 1.1 christos
127 1.1 christos
128 1.1 christos /*******************************************************************************
129 1.1 christos *
130 1.1 christos * FUNCTION: AcpiUtConvertDecimalString
131 1.1 christos *
132 1.1 christos * PARAMETERS: String - Null terminated input string
133 1.1 christos * ReturnValuePtr - Where the converted value is returned
134 1.1 christos *
135 1.1 christos * RETURN: Status and 64-bit converted integer
136 1.1 christos *
137 1.1 christos * DESCRIPTION: Performs a base 10 conversion of the input string to an
138 1.1 christos * integer value, either 32 or 64 bits.
139 1.1 christos *
140 1.1 christos * NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615
141 1.1 christos * Maximum 32-bit unsigned decimal value is 4294967295
142 1.1 christos *
143 1.1 christos ******************************************************************************/
144 1.1 christos
145 1.1 christos ACPI_STATUS
146 1.1 christos AcpiUtConvertDecimalString (
147 1.1 christos char *String,
148 1.1 christos UINT64 *ReturnValuePtr)
149 1.1 christos {
150 1.1 christos UINT64 AccumulatedValue = 0;
151 1.1 christos ACPI_STATUS Status = AE_OK;
152 1.1 christos
153 1.1 christos
154 1.1 christos /* Convert each ASCII byte in the input string */
155 1.1 christos
156 1.1 christos while (*String)
157 1.1 christos {
158 1.1 christos /* Character must be ASCII 0-9, otherwise terminate with no error */
159 1.1 christos
160 1.1 christos if (!isdigit (*String))
161 1.1 christos {
162 1.1 christos break;
163 1.1 christos }
164 1.1 christos
165 1.1 christos /* Convert and insert this decimal digit into the accumulator */
166 1.1 christos
167 1.1 christos Status = AcpiUtInsertDigit (&AccumulatedValue, 10, *String);
168 1.1 christos if (ACPI_FAILURE (Status))
169 1.1 christos {
170 1.1 christos Status = AE_DECIMAL_OVERFLOW;
171 1.1 christos break;
172 1.1 christos }
173 1.1 christos
174 1.1 christos String++;
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* Always return the value that has been accumulated */
178 1.1 christos
179 1.1 christos *ReturnValuePtr = AccumulatedValue;
180 1.1 christos return (Status);
181 1.1 christos }
182 1.1 christos
183 1.1 christos
184 1.1 christos /*******************************************************************************
185 1.1 christos *
186 1.1 christos * FUNCTION: AcpiUtConvertHexString
187 1.1 christos *
188 1.1 christos * PARAMETERS: String - Null terminated input string
189 1.1 christos * ReturnValuePtr - Where the converted value is returned
190 1.1 christos *
191 1.1 christos * RETURN: Status and 64-bit converted integer
192 1.1 christos *
193 1.1 christos * DESCRIPTION: Performs a base 16 conversion of the input string to an
194 1.1 christos * integer value, either 32 or 64 bits.
195 1.1 christos *
196 1.1 christos * NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
197 1.1 christos * Maximum 32-bit unsigned hex value is 0xFFFFFFFF
198 1.1 christos *
199 1.1 christos ******************************************************************************/
200 1.1 christos
201 1.1 christos ACPI_STATUS
202 1.1 christos AcpiUtConvertHexString (
203 1.1 christos char *String,
204 1.1 christos UINT64 *ReturnValuePtr)
205 1.1 christos {
206 1.1 christos UINT64 AccumulatedValue = 0;
207 1.1 christos ACPI_STATUS Status = AE_OK;
208 1.1 christos
209 1.1 christos
210 1.1 christos /* Convert each ASCII byte in the input string */
211 1.1 christos
212 1.1 christos while (*String)
213 1.1 christos {
214 1.1 christos /* Must be ASCII A-F, a-f, or 0-9, otherwise terminate with no error */
215 1.1 christos
216 1.1 christos if (!isxdigit (*String))
217 1.1 christos {
218 1.1 christos break;
219 1.1 christos }
220 1.1 christos
221 1.1 christos /* Convert and insert this hex digit into the accumulator */
222 1.1 christos
223 1.1 christos Status = AcpiUtInsertDigit (&AccumulatedValue, 16, *String);
224 1.1 christos if (ACPI_FAILURE (Status))
225 1.1 christos {
226 1.1 christos Status = AE_HEX_OVERFLOW;
227 1.1 christos break;
228 1.1 christos }
229 1.1 christos
230 1.1 christos String++;
231 1.1 christos }
232 1.1 christos
233 1.1 christos /* Always return the value that has been accumulated */
234 1.1 christos
235 1.1 christos *ReturnValuePtr = AccumulatedValue;
236 1.1 christos return (Status);
237 1.1 christos }
238 1.1 christos
239 1.1 christos
240 1.1 christos /*******************************************************************************
241 1.1 christos *
242 1.1 christos * FUNCTION: AcpiUtRemoveLeadingZeros
243 1.1 christos *
244 1.1 christos * PARAMETERS: String - Pointer to input ASCII string
245 1.1 christos *
246 1.1 christos * RETURN: Next character after any leading zeros. This character may be
247 1.1 christos * used by the caller to detect end-of-string.
248 1.1 christos *
249 1.1 christos * DESCRIPTION: Remove any leading zeros in the input string. Return the
250 1.1 christos * next character after the final ASCII zero to enable the caller
251 1.1 christos * to check for the end of the string (NULL terminator).
252 1.1 christos *
253 1.1 christos ******************************************************************************/
254 1.1 christos
255 1.1 christos char
256 1.1 christos AcpiUtRemoveLeadingZeros (
257 1.1 christos char **String)
258 1.1 christos {
259 1.1 christos
260 1.1 christos while (**String == ACPI_ASCII_ZERO)
261 1.1 christos {
262 1.1 christos *String += 1;
263 1.1 christos }
264 1.1 christos
265 1.1 christos return (**String);
266 1.1 christos }
267 1.1 christos
268 1.1 christos
269 1.1 christos /*******************************************************************************
270 1.1 christos *
271 1.1 christos * FUNCTION: AcpiUtRemoveWhitespace
272 1.1 christos *
273 1.1 christos * PARAMETERS: String - Pointer to input ASCII string
274 1.1 christos *
275 1.1 christos * RETURN: Next character after any whitespace. This character may be
276 1.1 christos * used by the caller to detect end-of-string.
277 1.1 christos *
278 1.1 christos * DESCRIPTION: Remove any leading whitespace in the input string. Return the
279 1.1 christos * next character after the final ASCII zero to enable the caller
280 1.1 christos * to check for the end of the string (NULL terminator).
281 1.1 christos *
282 1.1 christos ******************************************************************************/
283 1.1 christos
284 1.1 christos char
285 1.1 christos AcpiUtRemoveWhitespace (
286 1.1 christos char **String)
287 1.1 christos {
288 1.1 christos
289 1.1 christos while (isspace ((UINT8) **String))
290 1.1 christos {
291 1.1 christos *String += 1;
292 1.1 christos }
293 1.1 christos
294 1.1 christos return (**String);
295 1.1 christos }
296 1.1 christos
297 1.1 christos
298 1.1 christos /*******************************************************************************
299 1.1 christos *
300 1.1 christos * FUNCTION: AcpiUtDetectHexPrefix
301 1.1 christos *
302 1.1 christos * PARAMETERS: String - Pointer to input ASCII string
303 1.1 christos *
304 1.1 christos * RETURN: TRUE if a "0x" prefix was found at the start of the string
305 1.1 christos *
306 1.1 christos * DESCRIPTION: Detect and remove a hex "0x" prefix
307 1.1 christos *
308 1.1 christos ******************************************************************************/
309 1.1 christos
310 1.1 christos BOOLEAN
311 1.1 christos AcpiUtDetectHexPrefix (
312 1.1 christos char **String)
313 1.1 christos {
314 1.1 christos
315 1.1 christos if ((**String == ACPI_ASCII_ZERO) &&
316 1.1 christos (tolower ((int) *(*String + 1)) == 'x'))
317 1.1 christos {
318 1.1 christos *String += 2; /* Go past the leading 0x */
319 1.1 christos return (TRUE);
320 1.1 christos }
321 1.1 christos
322 1.1 christos return (FALSE); /* Not a hex string */
323 1.1 christos }
324 1.1 christos
325 1.1 christos
326 1.1 christos /*******************************************************************************
327 1.1 christos *
328 1.1 christos * FUNCTION: AcpiUtDetectOctalPrefix
329 1.1 christos *
330 1.1 christos * PARAMETERS: String - Pointer to input ASCII string
331 1.1 christos *
332 1.1 christos * RETURN: True if an octal "0" prefix was found at the start of the
333 1.1 christos * string
334 1.1 christos *
335 1.1 christos * DESCRIPTION: Detect and remove an octal prefix (zero)
336 1.1 christos *
337 1.1 christos ******************************************************************************/
338 1.1 christos
339 1.1 christos BOOLEAN
340 1.1 christos AcpiUtDetectOctalPrefix (
341 1.1 christos char **String)
342 1.1 christos {
343 1.1 christos
344 1.1 christos if (**String == ACPI_ASCII_ZERO)
345 1.1 christos {
346 1.1 christos *String += 1; /* Go past the leading 0 */
347 1.1 christos return (TRUE);
348 1.1 christos }
349 1.1 christos
350 1.1 christos return (FALSE); /* Not an octal string */
351 1.1 christos }
352 1.1 christos
353 1.1 christos
354 1.1 christos /*******************************************************************************
355 1.1 christos *
356 1.1 christos * FUNCTION: AcpiUtInsertDigit
357 1.1 christos *
358 1.1 christos * PARAMETERS: AccumulatedValue - Current value of the integer value
359 1.1 christos * accumulator. The new value is
360 1.1 christos * returned here.
361 1.1 christos * Base - Radix, either 8/10/16
362 1.1 christos * AsciiDigit - ASCII single digit to be inserted
363 1.1 christos *
364 1.1 christos * RETURN: Status and result of the convert/insert operation. The only
365 1.1 christos * possible returned exception code is numeric overflow of
366 1.1 christos * either the multiply or add conversion operations.
367 1.1 christos *
368 1.1 christos * DESCRIPTION: Generic conversion and insertion function for all bases:
369 1.1 christos *
370 1.1 christos * 1) Multiply the current accumulated/converted value by the
371 1.1 christos * base in order to make room for the new character.
372 1.1 christos *
373 1.1 christos * 2) Convert the new character to binary and add it to the
374 1.1 christos * current accumulated value.
375 1.1 christos *
376 1.1 christos * Note: The only possible exception indicates an integer
377 1.1 christos * overflow (AE_NUMERIC_OVERFLOW)
378 1.1 christos *
379 1.1 christos ******************************************************************************/
380 1.1 christos
381 1.1 christos static ACPI_STATUS
382 1.1 christos AcpiUtInsertDigit (
383 1.1 christos UINT64 *AccumulatedValue,
384 1.1 christos UINT32 Base,
385 1.1 christos int AsciiDigit)
386 1.1 christos {
387 1.1 christos ACPI_STATUS Status;
388 1.1 christos UINT64 Product;
389 1.1 christos
390 1.1 christos
391 1.1 christos /* Make room in the accumulated value for the incoming digit */
392 1.1 christos
393 1.1 christos Status = AcpiUtStrtoulMultiply64 (*AccumulatedValue, Base, &Product);
394 1.1 christos if (ACPI_FAILURE (Status))
395 1.1 christos {
396 1.1 christos return (Status);
397 1.1 christos }
398 1.1 christos
399 1.1 christos /* Add in the new digit, and store the sum to the accumulated value */
400 1.1 christos
401 1.1 christos Status = AcpiUtStrtoulAdd64 (Product, AcpiUtAsciiCharToHex (AsciiDigit),
402 1.1 christos AccumulatedValue);
403 1.1 christos
404 1.1 christos return (Status);
405 1.1 christos }
406 1.1 christos
407 1.1 christos
408 1.1 christos /*******************************************************************************
409 1.1 christos *
410 1.1 christos * FUNCTION: AcpiUtStrtoulMultiply64
411 1.1 christos *
412 1.1 christos * PARAMETERS: Multiplicand - Current accumulated converted integer
413 1.1.1.2 christos * Base - Base/Radix
414 1.1 christos * OutProduct - Where the product is returned
415 1.1 christos *
416 1.1 christos * RETURN: Status and 64-bit product
417 1.1 christos *
418 1.1 christos * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
419 1.1 christos * well as 32-bit overflow if necessary (if the current global
420 1.1 christos * integer width is 32).
421 1.1 christos *
422 1.1 christos ******************************************************************************/
423 1.1 christos
424 1.1 christos static ACPI_STATUS
425 1.1 christos AcpiUtStrtoulMultiply64 (
426 1.1 christos UINT64 Multiplicand,
427 1.1.1.2 christos UINT32 Base,
428 1.1 christos UINT64 *OutProduct)
429 1.1 christos {
430 1.1 christos UINT64 Product;
431 1.1.1.2 christos UINT64 Quotient;
432 1.1 christos
433 1.1 christos
434 1.1 christos /* Exit if either operand is zero */
435 1.1 christos
436 1.1 christos *OutProduct = 0;
437 1.1.1.2 christos if (!Multiplicand || !Base)
438 1.1 christos {
439 1.1 christos return (AE_OK);
440 1.1 christos }
441 1.1 christos
442 1.1.1.2 christos /*
443 1.1.1.2 christos * Check for 64-bit overflow before the actual multiplication.
444 1.1.1.2 christos *
445 1.1.1.2 christos * Notes: 64-bit division is often not supported on 32-bit platforms
446 1.1.1.2 christos * (it requires a library function), Therefore ACPICA has a local
447 1.1.1.2 christos * 64-bit divide function. Also, Multiplier is currently only used
448 1.1.1.2 christos * as the radix (8/10/16), to the 64/32 divide will always work.
449 1.1.1.2 christos */
450 1.1.1.2 christos AcpiUtShortDivide (ACPI_UINT64_MAX, Base, &Quotient, NULL);
451 1.1.1.2 christos if (Multiplicand > Quotient)
452 1.1 christos {
453 1.1 christos return (AE_NUMERIC_OVERFLOW);
454 1.1 christos }
455 1.1 christos
456 1.1.1.2 christos Product = Multiplicand * Base;
457 1.1 christos
458 1.1 christos /* Check for 32-bit overflow if necessary */
459 1.1 christos
460 1.1 christos if ((AcpiGbl_IntegerBitWidth == 32) && (Product > ACPI_UINT32_MAX))
461 1.1 christos {
462 1.1 christos return (AE_NUMERIC_OVERFLOW);
463 1.1 christos }
464 1.1 christos
465 1.1 christos *OutProduct = Product;
466 1.1 christos return (AE_OK);
467 1.1 christos }
468 1.1 christos
469 1.1 christos
470 1.1 christos /*******************************************************************************
471 1.1 christos *
472 1.1 christos * FUNCTION: AcpiUtStrtoulAdd64
473 1.1 christos *
474 1.1 christos * PARAMETERS: Addend1 - Current accumulated converted integer
475 1.1.1.2 christos * Digit - New hex value/char
476 1.1 christos * OutSum - Where sum is returned (Accumulator)
477 1.1 christos *
478 1.1 christos * RETURN: Status and 64-bit sum
479 1.1 christos *
480 1.1 christos * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
481 1.1 christos * well as 32-bit overflow if necessary (if the current global
482 1.1 christos * integer width is 32).
483 1.1 christos *
484 1.1 christos ******************************************************************************/
485 1.1 christos
486 1.1 christos static ACPI_STATUS
487 1.1 christos AcpiUtStrtoulAdd64 (
488 1.1 christos UINT64 Addend1,
489 1.1.1.2 christos UINT32 Digit,
490 1.1 christos UINT64 *OutSum)
491 1.1 christos {
492 1.1 christos UINT64 Sum;
493 1.1 christos
494 1.1 christos
495 1.1 christos /* Check for 64-bit overflow before the actual addition */
496 1.1 christos
497 1.1.1.2 christos if ((Addend1 > 0) && (Digit > (ACPI_UINT64_MAX - Addend1)))
498 1.1 christos {
499 1.1 christos return (AE_NUMERIC_OVERFLOW);
500 1.1 christos }
501 1.1 christos
502 1.1.1.2 christos Sum = Addend1 + Digit;
503 1.1 christos
504 1.1 christos /* Check for 32-bit overflow if necessary */
505 1.1 christos
506 1.1 christos if ((AcpiGbl_IntegerBitWidth == 32) && (Sum > ACPI_UINT32_MAX))
507 1.1 christos {
508 1.1 christos return (AE_NUMERIC_OVERFLOW);
509 1.1 christos }
510 1.1 christos
511 1.1 christos *OutSum = Sum;
512 1.1 christos return (AE_OK);
513 1.1 christos }
514