Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / acpica / utprint.c
bloba98c334c3bb7585f80851d7b61aba2fada13ae54
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: utprint - Formatted printing routines
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
13 #define _COMPONENT ACPI_UTILITIES
14 ACPI_MODULE_NAME("utprint")
16 #define ACPI_FORMAT_SIGN 0x01
17 #define ACPI_FORMAT_SIGN_PLUS 0x02
18 #define ACPI_FORMAT_SIGN_PLUS_SPACE 0x04
19 #define ACPI_FORMAT_ZERO 0x08
20 #define ACPI_FORMAT_LEFT 0x10
21 #define ACPI_FORMAT_UPPER 0x20
22 #define ACPI_FORMAT_PREFIX 0x40
23 /* Local prototypes */
24 static acpi_size
25 acpi_ut_bound_string_length(const char *string, acpi_size count);
27 static char *acpi_ut_bound_string_output(char *string, const char *end, char c);
29 static char *acpi_ut_format_number(char *string,
30 char *end,
31 u64 number,
32 u8 base, s32 width, s32 precision, u8 type);
34 static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper);
36 /*******************************************************************************
38 * FUNCTION: acpi_ut_bound_string_length
40 * PARAMETERS: string - String with boundary
41 * count - Boundary of the string
43 * RETURN: Length of the string. Less than or equal to Count.
45 * DESCRIPTION: Calculate the length of a string with boundary.
47 ******************************************************************************/
49 static acpi_size
50 acpi_ut_bound_string_length(const char *string, acpi_size count)
52 u32 length = 0;
54 while (*string && count) {
55 length++;
56 string++;
57 count--;
60 return (length);
63 /*******************************************************************************
65 * FUNCTION: acpi_ut_bound_string_output
67 * PARAMETERS: string - String with boundary
68 * end - Boundary of the string
69 * c - Character to be output to the string
71 * RETURN: Updated position for next valid character
73 * DESCRIPTION: Output a character into a string with boundary check.
75 ******************************************************************************/
77 static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
80 if (string < end) {
81 *string = c;
84 ++string;
85 return (string);
88 /*******************************************************************************
90 * FUNCTION: acpi_ut_put_number
92 * PARAMETERS: string - Buffer to hold reverse-ordered string
93 * number - Integer to be converted
94 * base - Base of the integer
95 * upper - Whether or not using upper cased digits
97 * RETURN: Updated position for next valid character
99 * DESCRIPTION: Convert an integer into a string, note that, the string holds a
100 * reversed ordered number without the trailing zero.
102 ******************************************************************************/
104 static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
106 const char *digits;
107 u64 digit_index;
108 char *pos;
110 pos = string;
111 digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
113 if (number == 0) {
114 *(pos++) = '0';
115 } else {
116 while (number) {
117 (void)acpi_ut_divide(number, base, &number,
118 &digit_index);
119 *(pos++) = digits[digit_index];
123 /* *(Pos++) = '0'; */
124 return (pos);
127 /*******************************************************************************
129 * FUNCTION: acpi_ut_scan_number
131 * PARAMETERS: string - String buffer
132 * number_ptr - Where the number is returned
134 * RETURN: Updated position for next valid character
136 * DESCRIPTION: Scan a string for a decimal integer.
138 ******************************************************************************/
140 const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
142 u64 number = 0;
144 while (isdigit((int)*string)) {
145 acpi_ut_short_multiply(number, 10, &number);
146 number += *(string++) - '0';
149 *number_ptr = number;
150 return (string);
153 /*******************************************************************************
155 * FUNCTION: acpi_ut_print_number
157 * PARAMETERS: string - String buffer
158 * number - The number to be converted
160 * RETURN: Updated position for next valid character
162 * DESCRIPTION: Print a decimal integer into a string.
164 ******************************************************************************/
166 const char *acpi_ut_print_number(char *string, u64 number)
168 char ascii_string[20];
169 const char *pos1;
170 char *pos2;
172 pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
173 pos2 = string;
175 while (pos1 != ascii_string) {
176 *(pos2++) = *(--pos1);
179 *pos2 = 0;
180 return (string);
183 /*******************************************************************************
185 * FUNCTION: acpi_ut_format_number
187 * PARAMETERS: string - String buffer with boundary
188 * end - Boundary of the string
189 * number - The number to be converted
190 * base - Base of the integer
191 * width - Field width
192 * precision - Precision of the integer
193 * type - Special printing flags
195 * RETURN: Updated position for next valid character
197 * DESCRIPTION: Print an integer into a string with any base and any precision.
199 ******************************************************************************/
201 static char *acpi_ut_format_number(char *string,
202 char *end,
203 u64 number,
204 u8 base, s32 width, s32 precision, u8 type)
206 char *pos;
207 char sign;
208 char zero;
209 u8 need_prefix;
210 u8 upper;
211 s32 i;
212 char reversed_string[66];
214 /* Parameter validation */
216 if (base < 2 || base > 16) {
217 return (NULL);
220 if (type & ACPI_FORMAT_LEFT) {
221 type &= ~ACPI_FORMAT_ZERO;
224 need_prefix = ((type & ACPI_FORMAT_PREFIX)
225 && base != 10) ? TRUE : FALSE;
226 upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
227 zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
229 /* Calculate size according to sign and prefix */
231 sign = '\0';
232 if (type & ACPI_FORMAT_SIGN) {
233 if ((s64)number < 0) {
234 sign = '-';
235 number = -(s64)number;
236 width--;
237 } else if (type & ACPI_FORMAT_SIGN_PLUS) {
238 sign = '+';
239 width--;
240 } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
241 sign = ' ';
242 width--;
245 if (need_prefix) {
246 width--;
247 if (base == 16) {
248 width--;
252 /* Generate full string in reverse order */
254 pos = acpi_ut_put_number(reversed_string, number, base, upper);
255 i = (s32)ACPI_PTR_DIFF(pos, reversed_string);
257 /* Printing 100 using %2d gives "100", not "00" */
259 if (i > precision) {
260 precision = i;
263 width -= precision;
265 /* Output the string */
267 if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
268 while (--width >= 0) {
269 string = acpi_ut_bound_string_output(string, end, ' ');
272 if (sign) {
273 string = acpi_ut_bound_string_output(string, end, sign);
275 if (need_prefix) {
276 string = acpi_ut_bound_string_output(string, end, '0');
277 if (base == 16) {
278 string =
279 acpi_ut_bound_string_output(string, end,
280 upper ? 'X' : 'x');
283 if (!(type & ACPI_FORMAT_LEFT)) {
284 while (--width >= 0) {
285 string = acpi_ut_bound_string_output(string, end, zero);
289 while (i <= --precision) {
290 string = acpi_ut_bound_string_output(string, end, '0');
292 while (--i >= 0) {
293 string = acpi_ut_bound_string_output(string, end,
294 reversed_string[i]);
296 while (--width >= 0) {
297 string = acpi_ut_bound_string_output(string, end, ' ');
300 return (string);
303 /*******************************************************************************
305 * FUNCTION: vsnprintf
307 * PARAMETERS: string - String with boundary
308 * size - Boundary of the string
309 * format - Standard printf format
310 * args - Argument list
312 * RETURN: Number of bytes actually written.
314 * DESCRIPTION: Formatted output to a string using argument list pointer.
316 ******************************************************************************/
318 int vsnprintf(char *string, acpi_size size, const char *format, va_list args)
320 u8 base;
321 u8 type;
322 s32 width;
323 s32 precision;
324 char qualifier;
325 u64 number;
326 char *pos;
327 char *end;
328 char c;
329 const char *s;
330 const void *p;
331 s32 length;
332 int i;
334 pos = string;
335 end = string + size;
337 for (; *format; ++format) {
338 if (*format != '%') {
339 pos = acpi_ut_bound_string_output(pos, end, *format);
340 continue;
343 type = 0;
344 base = 10;
346 /* Process sign */
348 do {
349 ++format;
350 if (*format == '#') {
351 type |= ACPI_FORMAT_PREFIX;
352 } else if (*format == '0') {
353 type |= ACPI_FORMAT_ZERO;
354 } else if (*format == '+') {
355 type |= ACPI_FORMAT_SIGN_PLUS;
356 } else if (*format == ' ') {
357 type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
358 } else if (*format == '-') {
359 type |= ACPI_FORMAT_LEFT;
360 } else {
361 break;
364 } while (1);
366 /* Process width */
368 width = -1;
369 if (isdigit((int)*format)) {
370 format = acpi_ut_scan_number(format, &number);
371 width = (s32)number;
372 } else if (*format == '*') {
373 ++format;
374 width = va_arg(args, int);
375 if (width < 0) {
376 width = -width;
377 type |= ACPI_FORMAT_LEFT;
381 /* Process precision */
383 precision = -1;
384 if (*format == '.') {
385 ++format;
386 if (isdigit((int)*format)) {
387 format = acpi_ut_scan_number(format, &number);
388 precision = (s32)number;
389 } else if (*format == '*') {
390 ++format;
391 precision = va_arg(args, int);
394 if (precision < 0) {
395 precision = 0;
399 /* Process qualifier */
401 qualifier = -1;
402 if (*format == 'h' || *format == 'l' || *format == 'L') {
403 qualifier = *format;
404 ++format;
406 if (qualifier == 'l' && *format == 'l') {
407 qualifier = 'L';
408 ++format;
412 switch (*format) {
413 case '%':
415 pos = acpi_ut_bound_string_output(pos, end, '%');
416 continue;
418 case 'c':
420 if (!(type & ACPI_FORMAT_LEFT)) {
421 while (--width > 0) {
422 pos =
423 acpi_ut_bound_string_output(pos,
424 end,
425 ' ');
429 c = (char)va_arg(args, int);
430 pos = acpi_ut_bound_string_output(pos, end, c);
432 while (--width > 0) {
433 pos =
434 acpi_ut_bound_string_output(pos, end, ' ');
436 continue;
438 case 's':
440 s = va_arg(args, char *);
441 if (!s) {
442 s = "<NULL>";
444 length = (s32)acpi_ut_bound_string_length(s, precision);
445 if (!(type & ACPI_FORMAT_LEFT)) {
446 while (length < width--) {
447 pos =
448 acpi_ut_bound_string_output(pos,
449 end,
450 ' ');
454 for (i = 0; i < length; ++i) {
455 pos = acpi_ut_bound_string_output(pos, end, *s);
456 ++s;
459 while (length < width--) {
460 pos =
461 acpi_ut_bound_string_output(pos, end, ' ');
463 continue;
465 case 'o':
467 base = 8;
468 break;
470 case 'X':
472 type |= ACPI_FORMAT_UPPER;
473 /* FALLTHROUGH */
475 case 'x':
477 base = 16;
478 break;
480 case 'd':
481 case 'i':
483 type |= ACPI_FORMAT_SIGN;
485 case 'u':
487 break;
489 case 'p':
491 if (width == -1) {
492 width = 2 * sizeof(void *);
493 type |= ACPI_FORMAT_ZERO;
496 p = va_arg(args, void *);
497 pos =
498 acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
499 16, width, precision, type);
500 continue;
502 default:
504 pos = acpi_ut_bound_string_output(pos, end, '%');
505 if (*format) {
506 pos =
507 acpi_ut_bound_string_output(pos, end,
508 *format);
509 } else {
510 --format;
512 continue;
515 if (qualifier == 'L') {
516 number = va_arg(args, u64);
517 if (type & ACPI_FORMAT_SIGN) {
518 number = (s64)number;
520 } else if (qualifier == 'l') {
521 number = va_arg(args, unsigned long);
522 if (type & ACPI_FORMAT_SIGN) {
523 number = (s32)number;
525 } else if (qualifier == 'h') {
526 number = (u16)va_arg(args, int);
527 if (type & ACPI_FORMAT_SIGN) {
528 number = (s16)number;
530 } else {
531 number = va_arg(args, unsigned int);
532 if (type & ACPI_FORMAT_SIGN) {
533 number = (signed int)number;
537 pos = acpi_ut_format_number(pos, end, number, base,
538 width, precision, type);
541 if (size > 0) {
542 if (pos < end) {
543 *pos = '\0';
544 } else {
545 end[-1] = '\0';
549 return ((int)ACPI_PTR_DIFF(pos, string));
552 /*******************************************************************************
554 * FUNCTION: snprintf
556 * PARAMETERS: string - String with boundary
557 * size - Boundary of the string
558 * Format, ... - Standard printf format
560 * RETURN: Number of bytes actually written.
562 * DESCRIPTION: Formatted output to a string.
564 ******************************************************************************/
566 int snprintf(char *string, acpi_size size, const char *format, ...)
568 va_list args;
569 int length;
571 va_start(args, format);
572 length = vsnprintf(string, size, format, args);
573 va_end(args);
575 return (length);
578 /*******************************************************************************
580 * FUNCTION: sprintf
582 * PARAMETERS: string - String with boundary
583 * Format, ... - Standard printf format
585 * RETURN: Number of bytes actually written.
587 * DESCRIPTION: Formatted output to a string.
589 ******************************************************************************/
591 int sprintf(char *string, const char *format, ...)
593 va_list args;
594 int length;
596 va_start(args, format);
597 length = vsnprintf(string, ACPI_UINT32_MAX, format, args);
598 va_end(args);
600 return (length);
603 #ifdef ACPI_APPLICATION
604 /*******************************************************************************
606 * FUNCTION: vprintf
608 * PARAMETERS: format - Standard printf format
609 * args - Argument list
611 * RETURN: Number of bytes actually written.
613 * DESCRIPTION: Formatted output to stdout using argument list pointer.
615 ******************************************************************************/
617 int vprintf(const char *format, va_list args)
619 acpi_cpu_flags flags;
620 int length;
622 flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
623 length = vsnprintf(acpi_gbl_print_buffer,
624 sizeof(acpi_gbl_print_buffer), format, args);
626 (void)fwrite(acpi_gbl_print_buffer, length, 1, ACPI_FILE_OUT);
627 acpi_os_release_lock(acpi_gbl_print_lock, flags);
629 return (length);
632 /*******************************************************************************
634 * FUNCTION: printf
636 * PARAMETERS: Format, ... - Standard printf format
638 * RETURN: Number of bytes actually written.
640 * DESCRIPTION: Formatted output to stdout.
642 ******************************************************************************/
644 int printf(const char *format, ...)
646 va_list args;
647 int length;
649 va_start(args, format);
650 length = vprintf(format, args);
651 va_end(args);
653 return (length);
656 /*******************************************************************************
658 * FUNCTION: vfprintf
660 * PARAMETERS: file - File descriptor
661 * format - Standard printf format
662 * args - Argument list
664 * RETURN: Number of bytes actually written.
666 * DESCRIPTION: Formatted output to a file using argument list pointer.
668 ******************************************************************************/
670 int vfprintf(FILE * file, const char *format, va_list args)
672 acpi_cpu_flags flags;
673 int length;
675 flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
676 length = vsnprintf(acpi_gbl_print_buffer,
677 sizeof(acpi_gbl_print_buffer), format, args);
679 (void)fwrite(acpi_gbl_print_buffer, length, 1, file);
680 acpi_os_release_lock(acpi_gbl_print_lock, flags);
682 return (length);
685 /*******************************************************************************
687 * FUNCTION: fprintf
689 * PARAMETERS: file - File descriptor
690 * Format, ... - Standard printf format
692 * RETURN: Number of bytes actually written.
694 * DESCRIPTION: Formatted output to a file.
696 ******************************************************************************/
698 int fprintf(FILE * file, const char *format, ...)
700 va_list args;
701 int length;
703 va_start(args, format);
704 length = vfprintf(file, format, args);
705 va_end(args);
707 return (length);
709 #endif