Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[sfrench/cifs-2.6.git] / drivers / acpi / acpica / utstrsuppt.c
1 /*******************************************************************************
2  *
3  * Module Name: utstrsuppt - Support functions for string-to-integer conversion
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2018, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
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
47 #define _COMPONENT          ACPI_UTILITIES
48 ACPI_MODULE_NAME("utstrsuppt")
49
50 /* Local prototypes */
51 static acpi_status
52 acpi_ut_insert_digit(u64 *accumulated_value, u32 base, int ascii_digit);
53
54 static acpi_status
55 acpi_ut_strtoul_multiply64(u64 multiplicand, u32 base, u64 *out_product);
56
57 static acpi_status acpi_ut_strtoul_add64(u64 addend1, u32 digit, u64 *out_sum);
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    acpi_ut_convert_octal_string
62  *
63  * PARAMETERS:  string                  - Null terminated input string
64  *              return_value_ptr        - Where the converted value is returned
65  *
66  * RETURN:      Status and 64-bit converted integer
67  *
68  * DESCRIPTION: Performs a base 8 conversion of the input string to an
69  *              integer value, either 32 or 64 bits.
70  *
71  * NOTE:        Maximum 64-bit unsigned octal value is 01777777777777777777777
72  *              Maximum 32-bit unsigned octal value is 037777777777
73  *
74  ******************************************************************************/
75
76 acpi_status acpi_ut_convert_octal_string(char *string, u64 *return_value_ptr)
77 {
78         u64 accumulated_value = 0;
79         acpi_status status = AE_OK;
80
81         /* Convert each ASCII byte in the input string */
82
83         while (*string) {
84
85                 /* Character must be ASCII 0-7, otherwise terminate with no error */
86
87                 if (!(ACPI_IS_OCTAL_DIGIT(*string))) {
88                         break;
89                 }
90
91                 /* Convert and insert this octal digit into the accumulator */
92
93                 status = acpi_ut_insert_digit(&accumulated_value, 8, *string);
94                 if (ACPI_FAILURE(status)) {
95                         status = AE_OCTAL_OVERFLOW;
96                         break;
97                 }
98
99                 string++;
100         }
101
102         /* Always return the value that has been accumulated */
103
104         *return_value_ptr = accumulated_value;
105         return (status);
106 }
107
108 /*******************************************************************************
109  *
110  * FUNCTION:    acpi_ut_convert_decimal_string
111  *
112  * PARAMETERS:  string                  - Null terminated input string
113  *              return_value_ptr        - Where the converted value is returned
114  *
115  * RETURN:      Status and 64-bit converted integer
116  *
117  * DESCRIPTION: Performs a base 10 conversion of the input string to an
118  *              integer value, either 32 or 64 bits.
119  *
120  * NOTE:        Maximum 64-bit unsigned decimal value is 18446744073709551615
121  *              Maximum 32-bit unsigned decimal value is 4294967295
122  *
123  ******************************************************************************/
124
125 acpi_status acpi_ut_convert_decimal_string(char *string, u64 *return_value_ptr)
126 {
127         u64 accumulated_value = 0;
128         acpi_status status = AE_OK;
129
130         /* Convert each ASCII byte in the input string */
131
132         while (*string) {
133
134                 /* Character must be ASCII 0-9, otherwise terminate with no error */
135
136                 if (!isdigit(*string)) {
137                         break;
138                 }
139
140                 /* Convert and insert this decimal digit into the accumulator */
141
142                 status = acpi_ut_insert_digit(&accumulated_value, 10, *string);
143                 if (ACPI_FAILURE(status)) {
144                         status = AE_DECIMAL_OVERFLOW;
145                         break;
146                 }
147
148                 string++;
149         }
150
151         /* Always return the value that has been accumulated */
152
153         *return_value_ptr = accumulated_value;
154         return (status);
155 }
156
157 /*******************************************************************************
158  *
159  * FUNCTION:    acpi_ut_convert_hex_string
160  *
161  * PARAMETERS:  string                  - Null terminated input string
162  *              return_value_ptr        - Where the converted value is returned
163  *
164  * RETURN:      Status and 64-bit converted integer
165  *
166  * DESCRIPTION: Performs a base 16 conversion of the input string to an
167  *              integer value, either 32 or 64 bits.
168  *
169  * NOTE:        Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
170  *              Maximum 32-bit unsigned hex value is 0xFFFFFFFF
171  *
172  ******************************************************************************/
173
174 acpi_status acpi_ut_convert_hex_string(char *string, u64 *return_value_ptr)
175 {
176         u64 accumulated_value = 0;
177         acpi_status status = AE_OK;
178
179         /* Convert each ASCII byte in the input string */
180
181         while (*string) {
182
183                 /* Must be ASCII A-F, a-f, or 0-9, otherwise terminate with no error */
184
185                 if (!isxdigit(*string)) {
186                         break;
187                 }
188
189                 /* Convert and insert this hex digit into the accumulator */
190
191                 status = acpi_ut_insert_digit(&accumulated_value, 16, *string);
192                 if (ACPI_FAILURE(status)) {
193                         status = AE_HEX_OVERFLOW;
194                         break;
195                 }
196
197                 string++;
198         }
199
200         /* Always return the value that has been accumulated */
201
202         *return_value_ptr = accumulated_value;
203         return (status);
204 }
205
206 /*******************************************************************************
207  *
208  * FUNCTION:    acpi_ut_remove_leading_zeros
209  *
210  * PARAMETERS:  string                  - Pointer to input ASCII string
211  *
212  * RETURN:      Next character after any leading zeros. This character may be
213  *              used by the caller to detect end-of-string.
214  *
215  * DESCRIPTION: Remove any leading zeros in the input string. Return the
216  *              next character after the final ASCII zero to enable the caller
217  *              to check for the end of the string (NULL terminator).
218  *
219  ******************************************************************************/
220
221 char acpi_ut_remove_leading_zeros(char **string)
222 {
223
224         while (**string == ACPI_ASCII_ZERO) {
225                 *string += 1;
226         }
227
228         return (**string);
229 }
230
231 /*******************************************************************************
232  *
233  * FUNCTION:    acpi_ut_remove_whitespace
234  *
235  * PARAMETERS:  string                  - Pointer to input ASCII string
236  *
237  * RETURN:      Next character after any whitespace. This character may be
238  *              used by the caller to detect end-of-string.
239  *
240  * DESCRIPTION: Remove any leading whitespace in the input string. Return the
241  *              next character after the final ASCII zero to enable the caller
242  *              to check for the end of the string (NULL terminator).
243  *
244  ******************************************************************************/
245
246 char acpi_ut_remove_whitespace(char **string)
247 {
248
249         while (isspace((u8)**string)) {
250                 *string += 1;
251         }
252
253         return (**string);
254 }
255
256 /*******************************************************************************
257  *
258  * FUNCTION:    acpi_ut_detect_hex_prefix
259  *
260  * PARAMETERS:  string                  - Pointer to input ASCII string
261  *
262  * RETURN:      TRUE if a "0x" prefix was found at the start of the string
263  *
264  * DESCRIPTION: Detect and remove a hex "0x" prefix
265  *
266  ******************************************************************************/
267
268 u8 acpi_ut_detect_hex_prefix(char **string)
269 {
270
271         if ((**string == ACPI_ASCII_ZERO) &&
272             (tolower((int)*(*string + 1)) == 'x')) {
273                 *string += 2;   /* Go past the leading 0x */
274                 return (TRUE);
275         }
276
277         return (FALSE);         /* Not a hex string */
278 }
279
280 /*******************************************************************************
281  *
282  * FUNCTION:    acpi_ut_detect_octal_prefix
283  *
284  * PARAMETERS:  string                  - Pointer to input ASCII string
285  *
286  * RETURN:      True if an octal "0" prefix was found at the start of the
287  *              string
288  *
289  * DESCRIPTION: Detect and remove an octal prefix (zero)
290  *
291  ******************************************************************************/
292
293 u8 acpi_ut_detect_octal_prefix(char **string)
294 {
295
296         if (**string == ACPI_ASCII_ZERO) {
297                 *string += 1;   /* Go past the leading 0 */
298                 return (TRUE);
299         }
300
301         return (FALSE);         /* Not an octal string */
302 }
303
304 /*******************************************************************************
305  *
306  * FUNCTION:    acpi_ut_insert_digit
307  *
308  * PARAMETERS:  accumulated_value       - Current value of the integer value
309  *                                        accumulator. The new value is
310  *                                        returned here.
311  *              base                    - Radix, either 8/10/16
312  *              ascii_digit             - ASCII single digit to be inserted
313  *
314  * RETURN:      Status and result of the convert/insert operation. The only
315  *              possible returned exception code is numeric overflow of
316  *              either the multiply or add conversion operations.
317  *
318  * DESCRIPTION: Generic conversion and insertion function for all bases:
319  *
320  *              1) Multiply the current accumulated/converted value by the
321  *              base in order to make room for the new character.
322  *
323  *              2) Convert the new character to binary and add it to the
324  *              current accumulated value.
325  *
326  *              Note: The only possible exception indicates an integer
327  *              overflow (AE_NUMERIC_OVERFLOW)
328  *
329  ******************************************************************************/
330
331 static acpi_status
332 acpi_ut_insert_digit(u64 *accumulated_value, u32 base, int ascii_digit)
333 {
334         acpi_status status;
335         u64 product;
336
337         /* Make room in the accumulated value for the incoming digit */
338
339         status = acpi_ut_strtoul_multiply64(*accumulated_value, base, &product);
340         if (ACPI_FAILURE(status)) {
341                 return (status);
342         }
343
344         /* Add in the new digit, and store the sum to the accumulated value */
345
346         status =
347             acpi_ut_strtoul_add64(product,
348                                   acpi_ut_ascii_char_to_hex(ascii_digit),
349                                   accumulated_value);
350
351         return (status);
352 }
353
354 /*******************************************************************************
355  *
356  * FUNCTION:    acpi_ut_strtoul_multiply64
357  *
358  * PARAMETERS:  multiplicand            - Current accumulated converted integer
359  *              base                    - Base/Radix
360  *              out_product             - Where the product is returned
361  *
362  * RETURN:      Status and 64-bit product
363  *
364  * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
365  *              well as 32-bit overflow if necessary (if the current global
366  *              integer width is 32).
367  *
368  ******************************************************************************/
369
370 static acpi_status
371 acpi_ut_strtoul_multiply64(u64 multiplicand, u32 base, u64 *out_product)
372 {
373         u64 product;
374         u64 quotient;
375
376         /* Exit if either operand is zero */
377
378         *out_product = 0;
379         if (!multiplicand || !base) {
380                 return (AE_OK);
381         }
382
383         /*
384          * Check for 64-bit overflow before the actual multiplication.
385          *
386          * Notes: 64-bit division is often not supported on 32-bit platforms
387          * (it requires a library function), Therefore ACPICA has a local
388          * 64-bit divide function. Also, Multiplier is currently only used
389          * as the radix (8/10/16), to the 64/32 divide will always work.
390          */
391         acpi_ut_short_divide(ACPI_UINT64_MAX, base, &quotient, NULL);
392         if (multiplicand > quotient) {
393                 return (AE_NUMERIC_OVERFLOW);
394         }
395
396         product = multiplicand * base;
397
398         /* Check for 32-bit overflow if necessary */
399
400         if ((acpi_gbl_integer_bit_width == 32) && (product > ACPI_UINT32_MAX)) {
401                 return (AE_NUMERIC_OVERFLOW);
402         }
403
404         *out_product = product;
405         return (AE_OK);
406 }
407
408 /*******************************************************************************
409  *
410  * FUNCTION:    acpi_ut_strtoul_add64
411  *
412  * PARAMETERS:  addend1                 - Current accumulated converted integer
413  *              digit                   - New hex value/char
414  *              out_sum                 - Where sum is returned (Accumulator)
415  *
416  * RETURN:      Status and 64-bit sum
417  *
418  * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
419  *              well as 32-bit overflow if necessary (if the current global
420  *              integer width is 32).
421  *
422  ******************************************************************************/
423
424 static acpi_status acpi_ut_strtoul_add64(u64 addend1, u32 digit, u64 *out_sum)
425 {
426         u64 sum;
427
428         /* Check for 64-bit overflow before the actual addition */
429
430         if ((addend1 > 0) && (digit > (ACPI_UINT64_MAX - addend1))) {
431                 return (AE_NUMERIC_OVERFLOW);
432         }
433
434         sum = addend1 + digit;
435
436         /* Check for 32-bit overflow if necessary */
437
438         if ((acpi_gbl_integer_bit_width == 32) && (sum > ACPI_UINT32_MAX)) {
439                 return (AE_NUMERIC_OVERFLOW);
440         }
441
442         *out_sum = sum;
443         return (AE_OK);
444 }