1 /*******************************************************************************
2 *
3 * Module Name: utstring - Common functions for strings and characters
4 *
5 ******************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2013, 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 #include "acnamesp.h"
47
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME("utstring")
50
51 /*
52 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
53 * version of strtoul.
54 */
55 #ifdef ACPI_ASL_COMPILER
56 /*******************************************************************************
57 *
58 * FUNCTION: acpi_ut_strlwr (strlwr)
59 *
60 * PARAMETERS: src_string - The source string to convert
61 *
62 * RETURN: None
63 *
64 * DESCRIPTION: Convert string to lowercase
65 *
66 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
67 *
68 ******************************************************************************/
acpi_ut_strlwr(char * src_string)69 void acpi_ut_strlwr(char *src_string)
70 {
71 char *string;
72
73 ACPI_FUNCTION_ENTRY();
74
75 if (!src_string) {
76 return;
77 }
78
79 /* Walk entire string, lowercasing the letters */
80
81 for (string = src_string; *string; string++) {
82 *string = (char)ACPI_TOLOWER(*string);
83 }
84
85 return;
86 }
87
88 /******************************************************************************
89 *
90 * FUNCTION: acpi_ut_stricmp (stricmp)
91 *
92 * PARAMETERS: string1 - first string to compare
93 * string2 - second string to compare
94 *
95 * RETURN: int that signifies string relationship. Zero means strings
96 * are equal.
97 *
98 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
99 * strings with no case sensitivity)
100 *
101 ******************************************************************************/
102
acpi_ut_stricmp(char * string1,char * string2)103 int acpi_ut_stricmp(char *string1, char *string2)
104 {
105 int c1;
106 int c2;
107
108 do {
109 c1 = tolower((int)*string1);
110 c2 = tolower((int)*string2);
111
112 string1++;
113 string2++;
114 }
115 while ((c1 == c2) && (c1));
116
117 return (c1 - c2);
118 }
119 #endif
120
121 /*******************************************************************************
122 *
123 * FUNCTION: acpi_ut_strupr (strupr)
124 *
125 * PARAMETERS: src_string - The source string to convert
126 *
127 * RETURN: None
128 *
129 * DESCRIPTION: Convert string to uppercase
130 *
131 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
132 *
133 ******************************************************************************/
134
acpi_ut_strupr(char * src_string)135 void acpi_ut_strupr(char *src_string)
136 {
137 char *string;
138
139 ACPI_FUNCTION_ENTRY();
140
141 if (!src_string) {
142 return;
143 }
144
145 /* Walk entire string, uppercasing the letters */
146
147 for (string = src_string; *string; string++) {
148 *string = (char)ACPI_TOUPPER(*string);
149 }
150
151 return;
152 }
153
154 /*******************************************************************************
155 *
156 * FUNCTION: acpi_ut_strtoul64
157 *
158 * PARAMETERS: string - Null terminated string
159 * base - Radix of the string: 16 or ACPI_ANY_BASE;
160 * ACPI_ANY_BASE means 'in behalf of to_integer'
161 * ret_integer - Where the converted integer is returned
162 *
163 * RETURN: Status and Converted value
164 *
165 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
166 * 32-bit or 64-bit conversion, depending on the current mode
167 * of the interpreter.
168 * NOTE: Does not support Octal strings, not needed.
169 *
170 ******************************************************************************/
171
acpi_ut_strtoul64(char * string,u32 base,u64 * ret_integer)172 acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer)
173 {
174 u32 this_digit = 0;
175 u64 return_value = 0;
176 u64 quotient;
177 u64 dividend;
178 u32 to_integer_op = (base == ACPI_ANY_BASE);
179 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
180 u8 valid_digits = 0;
181 u8 sign_of0x = 0;
182 u8 term = 0;
183
184 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
185
186 switch (base) {
187 case ACPI_ANY_BASE:
188 case 16:
189 break;
190
191 default:
192 /* Invalid Base */
193 return_ACPI_STATUS(AE_BAD_PARAMETER);
194 }
195
196 if (!string) {
197 goto error_exit;
198 }
199
200 /* Skip over any white space in the buffer */
201
202 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
203 string++;
204 }
205
206 if (to_integer_op) {
207 /*
208 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
209 * We need to determine if it is decimal or hexadecimal.
210 */
211 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
212 sign_of0x = 1;
213 base = 16;
214
215 /* Skip over the leading '0x' */
216 string += 2;
217 } else {
218 base = 10;
219 }
220 }
221
222 /* Any string left? Check that '0x' is not followed by white space. */
223
224 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
225 if (to_integer_op) {
226 goto error_exit;
227 } else {
228 goto all_done;
229 }
230 }
231
232 /*
233 * Perform a 32-bit or 64-bit conversion, depending upon the current
234 * execution mode of the interpreter
235 */
236 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
237
238 /* Main loop: convert the string to a 32- or 64-bit integer */
239
240 while (*string) {
241 if (ACPI_IS_DIGIT(*string)) {
242
243 /* Convert ASCII 0-9 to Decimal value */
244
245 this_digit = ((u8)*string) - '0';
246 } else if (base == 10) {
247
248 /* Digit is out of range; possible in to_integer case only */
249
250 term = 1;
251 } else {
252 this_digit = (u8)ACPI_TOUPPER(*string);
253 if (ACPI_IS_XDIGIT((char)this_digit)) {
254
255 /* Convert ASCII Hex char to value */
256
257 this_digit = this_digit - 'A' + 10;
258 } else {
259 term = 1;
260 }
261 }
262
263 if (term) {
264 if (to_integer_op) {
265 goto error_exit;
266 } else {
267 break;
268 }
269 } else if ((valid_digits == 0) && (this_digit == 0)
270 && !sign_of0x) {
271
272 /* Skip zeros */
273 string++;
274 continue;
275 }
276
277 valid_digits++;
278
279 if (sign_of0x
280 && ((valid_digits > 16)
281 || ((valid_digits > 8) && mode32))) {
282 /*
283 * This is to_integer operation case.
284 * No any restrictions for string-to-integer conversion,
285 * see ACPI spec.
286 */
287 goto error_exit;
288 }
289
290 /* Divide the digit into the correct position */
291
292 (void)acpi_ut_short_divide((dividend - (u64)this_digit),
293 base, "ient, NULL);
294
295 if (return_value > quotient) {
296 if (to_integer_op) {
297 goto error_exit;
298 } else {
299 break;
300 }
301 }
302
303 return_value *= base;
304 return_value += this_digit;
305 string++;
306 }
307
308 /* All done, normal exit */
309
310 all_done:
311
312 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
313 ACPI_FORMAT_UINT64(return_value)));
314
315 *ret_integer = return_value;
316 return_ACPI_STATUS(AE_OK);
317
318 error_exit:
319 /* Base was set/validated above */
320
321 if (base == 10) {
322 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
323 } else {
324 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
325 }
326 }
327
328 /*******************************************************************************
329 *
330 * FUNCTION: acpi_ut_print_string
331 *
332 * PARAMETERS: string - Null terminated ASCII string
333 * max_length - Maximum output length
334 *
335 * RETURN: None
336 *
337 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
338 * sequences.
339 *
340 ******************************************************************************/
341
acpi_ut_print_string(char * string,u8 max_length)342 void acpi_ut_print_string(char *string, u8 max_length)
343 {
344 u32 i;
345
346 if (!string) {
347 acpi_os_printf("<\"NULL STRING PTR\">");
348 return;
349 }
350
351 acpi_os_printf("\"");
352 for (i = 0; string[i] && (i < max_length); i++) {
353
354 /* Escape sequences */
355
356 switch (string[i]) {
357 case 0x07:
358 acpi_os_printf("\\a"); /* BELL */
359 break;
360
361 case 0x08:
362 acpi_os_printf("\\b"); /* BACKSPACE */
363 break;
364
365 case 0x0C:
366 acpi_os_printf("\\f"); /* FORMFEED */
367 break;
368
369 case 0x0A:
370 acpi_os_printf("\\n"); /* LINEFEED */
371 break;
372
373 case 0x0D:
374 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
375 break;
376
377 case 0x09:
378 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
379 break;
380
381 case 0x0B:
382 acpi_os_printf("\\v"); /* VERTICAL TAB */
383 break;
384
385 case '\'': /* Single Quote */
386 case '\"': /* Double Quote */
387 case '\\': /* Backslash */
388 acpi_os_printf("\\%c", (int)string[i]);
389 break;
390
391 default:
392
393 /* Check for printable character or hex escape */
394
395 if (ACPI_IS_PRINT(string[i])) {
396 /* This is a normal character */
397
398 acpi_os_printf("%c", (int)string[i]);
399 } else {
400 /* All others will be Hex escapes */
401
402 acpi_os_printf("\\x%2.2X", (s32) string[i]);
403 }
404 break;
405 }
406 }
407 acpi_os_printf("\"");
408
409 if (i == max_length && string[i]) {
410 acpi_os_printf("...");
411 }
412 }
413
414 /*******************************************************************************
415 *
416 * FUNCTION: acpi_ut_valid_acpi_char
417 *
418 * PARAMETERS: char - The character to be examined
419 * position - Byte position (0-3)
420 *
421 * RETURN: TRUE if the character is valid, FALSE otherwise
422 *
423 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
424 * 1) Upper case alpha
425 * 2) numeric
426 * 3) underscore
427 *
428 * We allow a '!' as the last character because of the ASF! table
429 *
430 ******************************************************************************/
431
acpi_ut_valid_acpi_char(char character,u32 position)432 u8 acpi_ut_valid_acpi_char(char character, u32 position)
433 {
434
435 if (!((character >= 'A' && character <= 'Z') ||
436 (character >= '0' && character <= '9') || (character == '_'))) {
437
438 /* Allow a '!' in the last position */
439
440 if (character == '!' && position == 3) {
441 return (TRUE);
442 }
443
444 return (FALSE);
445 }
446
447 return (TRUE);
448 }
449
450 /*******************************************************************************
451 *
452 * FUNCTION: acpi_ut_valid_acpi_name
453 *
454 * PARAMETERS: name - The name to be examined
455 *
456 * RETURN: TRUE if the name is valid, FALSE otherwise
457 *
458 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
459 * 1) Upper case alpha
460 * 2) numeric
461 * 3) underscore
462 *
463 ******************************************************************************/
464
acpi_ut_valid_acpi_name(u32 name)465 u8 acpi_ut_valid_acpi_name(u32 name)
466 {
467 u32 i;
468
469 ACPI_FUNCTION_ENTRY();
470
471 for (i = 0; i < ACPI_NAME_SIZE; i++) {
472 if (!acpi_ut_valid_acpi_char
473 ((ACPI_CAST_PTR(char, &name))[i], i)) {
474 return (FALSE);
475 }
476 }
477
478 return (TRUE);
479 }
480
481 /*******************************************************************************
482 *
483 * FUNCTION: acpi_ut_repair_name
484 *
485 * PARAMETERS: name - The ACPI name to be repaired
486 *
487 * RETURN: Repaired version of the name
488 *
489 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
490 * return the new name. NOTE: the Name parameter must reside in
491 * read/write memory, cannot be a const.
492 *
493 * An ACPI Name must consist of valid ACPI characters. We will repair the name
494 * if necessary because we don't want to abort because of this, but we want
495 * all namespace names to be printable. A warning message is appropriate.
496 *
497 * This issue came up because there are in fact machines that exhibit
498 * this problem, and we want to be able to enable ACPI support for them,
499 * even though there are a few bad names.
500 *
501 ******************************************************************************/
502
acpi_ut_repair_name(char * name)503 void acpi_ut_repair_name(char *name)
504 {
505 u32 i;
506 u8 found_bad_char = FALSE;
507 u32 original_name;
508
509 ACPI_FUNCTION_NAME(ut_repair_name);
510
511 ACPI_MOVE_NAME(&original_name, name);
512
513 /* Check each character in the name */
514
515 for (i = 0; i < ACPI_NAME_SIZE; i++) {
516 if (acpi_ut_valid_acpi_char(name[i], i)) {
517 continue;
518 }
519
520 /*
521 * Replace a bad character with something printable, yet technically
522 * still invalid. This prevents any collisions with existing "good"
523 * names in the namespace.
524 */
525 name[i] = '*';
526 found_bad_char = TRUE;
527 }
528
529 if (found_bad_char) {
530
531 /* Report warning only if in strict mode or debug mode */
532
533 if (!acpi_gbl_enable_interpreter_slack) {
534 ACPI_WARNING((AE_INFO,
535 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
536 original_name, name));
537 } else {
538 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
539 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
540 original_name, name));
541 }
542 }
543 }
544
545 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
546 /*******************************************************************************
547 *
548 * FUNCTION: ut_convert_backslashes
549 *
550 * PARAMETERS: pathname - File pathname string to be converted
551 *
552 * RETURN: Modifies the input Pathname
553 *
554 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
555 * the entire input file pathname string.
556 *
557 ******************************************************************************/
558
ut_convert_backslashes(char * pathname)559 void ut_convert_backslashes(char *pathname)
560 {
561
562 if (!pathname) {
563 return;
564 }
565
566 while (*pathname) {
567 if (*pathname == '\\') {
568 *pathname = '/';
569 }
570
571 pathname++;
572 }
573 }
574 #endif
575