utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utprint - Formatted printing routines
  5. *
  6. * Copyright (C) 2000 - 2025, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #define _COMPONENT ACPI_UTILITIES
  12. ACPI_MODULE_NAME("utprint")
  13. #define ACPI_FORMAT_SIGN 0x01
  14. #define ACPI_FORMAT_SIGN_PLUS 0x02
  15. #define ACPI_FORMAT_SIGN_PLUS_SPACE 0x04
  16. #define ACPI_FORMAT_ZERO 0x08
  17. #define ACPI_FORMAT_LEFT 0x10
  18. #define ACPI_FORMAT_UPPER 0x20
  19. #define ACPI_FORMAT_PREFIX 0x40
  20. /* Local prototypes */
  21. static acpi_size
  22. acpi_ut_bound_string_length(const char *string, acpi_size count);
  23. static char *acpi_ut_bound_string_output(char *string, const char *end, char c);
  24. static char *acpi_ut_format_number(char *string,
  25. char *end,
  26. u64 number,
  27. u8 base, s32 width, s32 precision, u8 type);
  28. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper);
  29. /*******************************************************************************
  30. *
  31. * FUNCTION: acpi_ut_bound_string_length
  32. *
  33. * PARAMETERS: string - String with boundary
  34. * count - Boundary of the string
  35. *
  36. * RETURN: Length of the string. Less than or equal to Count.
  37. *
  38. * DESCRIPTION: Calculate the length of a string with boundary.
  39. *
  40. ******************************************************************************/
  41. static acpi_size
  42. acpi_ut_bound_string_length(const char *string, acpi_size count)
  43. {
  44. u32 length = 0;
  45. while (*string && count) {
  46. length++;
  47. string++;
  48. count--;
  49. }
  50. return (length);
  51. }
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ut_bound_string_output
  55. *
  56. * PARAMETERS: string - String with boundary
  57. * end - Boundary of the string
  58. * c - Character to be output to the string
  59. *
  60. * RETURN: Updated position for next valid character
  61. *
  62. * DESCRIPTION: Output a character into a string with boundary check.
  63. *
  64. ******************************************************************************/
  65. static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
  66. {
  67. if (string < end) {
  68. *string = c;
  69. }
  70. ++string;
  71. return (string);
  72. }
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ut_put_number
  76. *
  77. * PARAMETERS: string - Buffer to hold reverse-ordered string
  78. * number - Integer to be converted
  79. * base - Base of the integer
  80. * upper - Whether or not using upper cased digits
  81. *
  82. * RETURN: Updated position for next valid character
  83. *
  84. * DESCRIPTION: Convert an integer into a string, note that, the string holds a
  85. * reversed ordered number without the trailing zero.
  86. *
  87. ******************************************************************************/
  88. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
  89. {
  90. const char *digits;
  91. u64 digit_index;
  92. char *pos;
  93. pos = string;
  94. digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
  95. if (number == 0) {
  96. *(pos++) = '0';
  97. } else {
  98. while (number) {
  99. (void)acpi_ut_divide(number, base, &number,
  100. &digit_index);
  101. *(pos++) = digits[digit_index];
  102. }
  103. }
  104. /* *(Pos++) = '0'; */
  105. return (pos);
  106. }
  107. /*******************************************************************************
  108. *
  109. * FUNCTION: acpi_ut_scan_number
  110. *
  111. * PARAMETERS: string - String buffer
  112. * number_ptr - Where the number is returned
  113. *
  114. * RETURN: Updated position for next valid character
  115. *
  116. * DESCRIPTION: Scan a string for a decimal integer.
  117. *
  118. ******************************************************************************/
  119. const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
  120. {
  121. u64 number = 0;
  122. while (isdigit((int)*string)) {
  123. acpi_ut_short_multiply(number, 10, &number);
  124. number += *(string++) - '0';
  125. }
  126. *number_ptr = number;
  127. return (string);
  128. }
  129. /*******************************************************************************
  130. *
  131. * FUNCTION: acpi_ut_print_number
  132. *
  133. * PARAMETERS: string - String buffer
  134. * number - The number to be converted
  135. *
  136. * RETURN: Updated position for next valid character
  137. *
  138. * DESCRIPTION: Print a decimal integer into a string.
  139. *
  140. ******************************************************************************/
  141. const char *acpi_ut_print_number(char *string, u64 number)
  142. {
  143. char ascii_string[20];
  144. const char *pos1;
  145. char *pos2;
  146. pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
  147. pos2 = string;
  148. while (pos1 != ascii_string) {
  149. *(pos2++) = *(--pos1);
  150. }
  151. *pos2 = 0;
  152. return (string);
  153. }
  154. /*******************************************************************************
  155. *
  156. * FUNCTION: acpi_ut_format_number
  157. *
  158. * PARAMETERS: string - String buffer with boundary
  159. * end - Boundary of the string
  160. * number - The number to be converted
  161. * base - Base of the integer
  162. * width - Field width
  163. * precision - Precision of the integer
  164. * type - Special printing flags
  165. *
  166. * RETURN: Updated position for next valid character
  167. *
  168. * DESCRIPTION: Print an integer into a string with any base and any precision.
  169. *
  170. ******************************************************************************/
  171. static char *acpi_ut_format_number(char *string,
  172. char *end,
  173. u64 number,
  174. u8 base, s32 width, s32 precision, u8 type)
  175. {
  176. char *pos;
  177. char sign;
  178. char zero;
  179. u8 need_prefix;
  180. u8 upper;
  181. s32 i;
  182. char reversed_string[66];
  183. /* Parameter validation */
  184. if (base < 2 || base > 16) {
  185. return (NULL);
  186. }
  187. if (type & ACPI_FORMAT_LEFT) {
  188. type &= ~ACPI_FORMAT_ZERO;
  189. }
  190. need_prefix = ((type & ACPI_FORMAT_PREFIX)
  191. && base != 10) ? TRUE : FALSE;
  192. upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
  193. zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
  194. /* Calculate size according to sign and prefix */
  195. sign = '\0';
  196. if (type & ACPI_FORMAT_SIGN) {
  197. if ((s64)number < 0) {
  198. sign = '-';
  199. number = -(s64)number;
  200. width--;
  201. } else if (type & ACPI_FORMAT_SIGN_PLUS) {
  202. sign = '+';
  203. width--;
  204. } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
  205. sign = ' ';
  206. width--;
  207. }
  208. }
  209. if (need_prefix) {
  210. width--;
  211. if (base == 16) {
  212. width--;
  213. }
  214. }
  215. /* Generate full string in reverse order */
  216. pos = acpi_ut_put_number(reversed_string, number, base, upper);
  217. i = (s32)ACPI_PTR_DIFF(pos, reversed_string);
  218. /* Printing 100 using %2d gives "100", not "00" */
  219. if (i > precision) {
  220. precision = i;
  221. }
  222. width -= precision;
  223. /* Output the string */
  224. if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
  225. while (--width >= 0) {
  226. string = acpi_ut_bound_string_output(string, end, ' ');
  227. }
  228. }
  229. if (sign) {
  230. string = acpi_ut_bound_string_output(string, end, sign);
  231. }
  232. if (need_prefix) {
  233. string = acpi_ut_bound_string_output(string, end, '0');
  234. if (base == 16) {
  235. string =
  236. acpi_ut_bound_string_output(string, end,
  237. upper ? 'X' : 'x');
  238. }
  239. }
  240. if (!(type & ACPI_FORMAT_LEFT)) {
  241. while (--width >= 0) {
  242. string = acpi_ut_bound_string_output(string, end, zero);
  243. }
  244. }
  245. while (i <= --precision) {
  246. string = acpi_ut_bound_string_output(string, end, '0');
  247. }
  248. while (--i >= 0) {
  249. string = acpi_ut_bound_string_output(string, end,
  250. reversed_string[i]);
  251. }
  252. while (--width >= 0) {
  253. string = acpi_ut_bound_string_output(string, end, ' ');
  254. }
  255. return (string);
  256. }
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: vsnprintf
  260. *
  261. * PARAMETERS: string - String with boundary
  262. * size - Boundary of the string
  263. * format - Standard printf format
  264. * args - Argument list
  265. *
  266. * RETURN: Number of bytes actually written.
  267. *
  268. * DESCRIPTION: Formatted output to a string using argument list pointer.
  269. *
  270. ******************************************************************************/
  271. int vsnprintf(char *string, acpi_size size, const char *format, va_list args)
  272. {
  273. u8 base;
  274. u8 type;
  275. s32 width;
  276. s32 precision;
  277. char qualifier;
  278. u64 number;
  279. char *pos;
  280. char *end;
  281. char c;
  282. const char *s;
  283. const void *p;
  284. s32 length;
  285. int i;
  286. pos = string;
  287. size = ACPI_MIN(size, ACPI_PTR_DIFF(ACPI_MAX_PTR, string));
  288. end = string + size;
  289. for (; *format; ++format) {
  290. if (*format != '%') {
  291. pos = acpi_ut_bound_string_output(pos, end, *format);
  292. continue;
  293. }
  294. type = 0;
  295. base = 10;
  296. /* Process sign */
  297. do {
  298. ++format;
  299. if (*format == '#') {
  300. type |= ACPI_FORMAT_PREFIX;
  301. } else if (*format == '0') {
  302. type |= ACPI_FORMAT_ZERO;
  303. } else if (*format == '+') {
  304. type |= ACPI_FORMAT_SIGN_PLUS;
  305. } else if (*format == ' ') {
  306. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  307. } else if (*format == '-') {
  308. type |= ACPI_FORMAT_LEFT;
  309. } else {
  310. break;
  311. }
  312. } while (1);
  313. /* Process width */
  314. width = -1;
  315. if (isdigit((int)*format)) {
  316. format = acpi_ut_scan_number(format, &number);
  317. width = (s32)number;
  318. } else if (*format == '*') {
  319. ++format;
  320. width = va_arg(args, int);
  321. if (width < 0) {
  322. width = -width;
  323. type |= ACPI_FORMAT_LEFT;
  324. }
  325. }
  326. /* Process precision */
  327. precision = -1;
  328. if (*format == '.') {
  329. ++format;
  330. if (isdigit((int)*format)) {
  331. format = acpi_ut_scan_number(format, &number);
  332. precision = (s32)number;
  333. } else if (*format == '*') {
  334. ++format;
  335. precision = va_arg(args, int);
  336. }
  337. if (precision < 0) {
  338. precision = 0;
  339. }
  340. }
  341. /* Process qualifier */
  342. qualifier = -1;
  343. if (*format == 'h' || *format == 'l' || *format == 'L') {
  344. qualifier = *format;
  345. ++format;
  346. if (qualifier == 'l' && *format == 'l') {
  347. qualifier = 'L';
  348. ++format;
  349. }
  350. }
  351. switch (*format) {
  352. case '%':
  353. pos = acpi_ut_bound_string_output(pos, end, '%');
  354. continue;
  355. case 'c':
  356. if (!(type & ACPI_FORMAT_LEFT)) {
  357. while (--width > 0) {
  358. pos =
  359. acpi_ut_bound_string_output(pos,
  360. end,
  361. ' ');
  362. }
  363. }
  364. c = (char)va_arg(args, int);
  365. pos = acpi_ut_bound_string_output(pos, end, c);
  366. while (--width > 0) {
  367. pos =
  368. acpi_ut_bound_string_output(pos, end, ' ');
  369. }
  370. continue;
  371. case 's':
  372. s = va_arg(args, char *);
  373. if (!s) {
  374. s = "<NULL>";
  375. }
  376. length = (s32)acpi_ut_bound_string_length(s, precision);
  377. if (!(type & ACPI_FORMAT_LEFT)) {
  378. while (length < width--) {
  379. pos =
  380. acpi_ut_bound_string_output(pos,
  381. end,
  382. ' ');
  383. }
  384. }
  385. for (i = 0; i < length; ++i) {
  386. pos = acpi_ut_bound_string_output(pos, end, *s);
  387. ++s;
  388. }
  389. while (length < width--) {
  390. pos =
  391. acpi_ut_bound_string_output(pos, end, ' ');
  392. }
  393. continue;
  394. case 'o':
  395. base = 8;
  396. break;
  397. case 'X':
  398. type |= ACPI_FORMAT_UPPER;
  399. ACPI_FALLTHROUGH;
  400. case 'x':
  401. base = 16;
  402. break;
  403. case 'd':
  404. case 'i':
  405. type |= ACPI_FORMAT_SIGN;
  406. case 'u':
  407. break;
  408. case 'p':
  409. if (width == -1) {
  410. width = 2 * sizeof(void *);
  411. type |= ACPI_FORMAT_ZERO;
  412. }
  413. p = va_arg(args, void *);
  414. pos =
  415. acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
  416. 16, width, precision, type);
  417. continue;
  418. default:
  419. pos = acpi_ut_bound_string_output(pos, end, '%');
  420. if (*format) {
  421. pos =
  422. acpi_ut_bound_string_output(pos, end,
  423. *format);
  424. } else {
  425. --format;
  426. }
  427. continue;
  428. }
  429. if (qualifier == 'L') {
  430. number = va_arg(args, u64);
  431. if (type & ACPI_FORMAT_SIGN) {
  432. number = (s64)number;
  433. }
  434. } else if (qualifier == 'l') {
  435. number = va_arg(args, unsigned long);
  436. if (type & ACPI_FORMAT_SIGN) {
  437. number = (s32)number;
  438. }
  439. } else if (qualifier == 'h') {
  440. number = (u16)va_arg(args, int);
  441. if (type & ACPI_FORMAT_SIGN) {
  442. number = (s16)number;
  443. }
  444. } else {
  445. number = va_arg(args, unsigned int);
  446. if (type & ACPI_FORMAT_SIGN) {
  447. number = (signed int)number;
  448. }
  449. }
  450. pos = acpi_ut_format_number(pos, end, number, base,
  451. width, precision, type);
  452. }
  453. if (size > 0) {
  454. if (pos < end) {
  455. *pos = '\0';
  456. } else {
  457. end[-1] = '\0';
  458. }
  459. }
  460. return ((int)ACPI_PTR_DIFF(pos, string));
  461. }
  462. /*******************************************************************************
  463. *
  464. * FUNCTION: snprintf
  465. *
  466. * PARAMETERS: string - String with boundary
  467. * size - Boundary of the string
  468. * Format, ... - Standard printf format
  469. *
  470. * RETURN: Number of bytes actually written.
  471. *
  472. * DESCRIPTION: Formatted output to a string.
  473. *
  474. ******************************************************************************/
  475. int snprintf(char *string, acpi_size size, const char *format, ...)
  476. {
  477. va_list args;
  478. int length;
  479. va_start(args, format);
  480. length = vsnprintf(string, size, format, args);
  481. va_end(args);
  482. return (length);
  483. }
  484. /*******************************************************************************
  485. *
  486. * FUNCTION: sprintf
  487. *
  488. * PARAMETERS: string - String with boundary
  489. * Format, ... - Standard printf format
  490. *
  491. * RETURN: Number of bytes actually written.
  492. *
  493. * DESCRIPTION: Formatted output to a string.
  494. *
  495. ******************************************************************************/
  496. int sprintf(char *string, const char *format, ...)
  497. {
  498. va_list args;
  499. int length;
  500. va_start(args, format);
  501. length = vsnprintf(string, ACPI_UINT32_MAX, format, args);
  502. va_end(args);
  503. return (length);
  504. }
  505. #ifdef ACPI_APPLICATION
  506. /*******************************************************************************
  507. *
  508. * FUNCTION: vprintf
  509. *
  510. * PARAMETERS: format - Standard printf format
  511. * args - Argument list
  512. *
  513. * RETURN: Number of bytes actually written.
  514. *
  515. * DESCRIPTION: Formatted output to stdout using argument list pointer.
  516. *
  517. ******************************************************************************/
  518. int vprintf(const char *format, va_list args)
  519. {
  520. acpi_cpu_flags flags;
  521. int length;
  522. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  523. length = vsnprintf(acpi_gbl_print_buffer,
  524. sizeof(acpi_gbl_print_buffer), format, args);
  525. (void)fwrite(acpi_gbl_print_buffer, length, 1, ACPI_FILE_OUT);
  526. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  527. return (length);
  528. }
  529. /*******************************************************************************
  530. *
  531. * FUNCTION: printf
  532. *
  533. * PARAMETERS: Format, ... - Standard printf format
  534. *
  535. * RETURN: Number of bytes actually written.
  536. *
  537. * DESCRIPTION: Formatted output to stdout.
  538. *
  539. ******************************************************************************/
  540. int printf(const char *format, ...)
  541. {
  542. va_list args;
  543. int length;
  544. va_start(args, format);
  545. length = vprintf(format, args);
  546. va_end(args);
  547. return (length);
  548. }
  549. /*******************************************************************************
  550. *
  551. * FUNCTION: vfprintf
  552. *
  553. * PARAMETERS: file - File descriptor
  554. * format - Standard printf format
  555. * args - Argument list
  556. *
  557. * RETURN: Number of bytes actually written.
  558. *
  559. * DESCRIPTION: Formatted output to a file using argument list pointer.
  560. *
  561. ******************************************************************************/
  562. int vfprintf(FILE * file, const char *format, va_list args)
  563. {
  564. acpi_cpu_flags flags;
  565. int length;
  566. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  567. length = vsnprintf(acpi_gbl_print_buffer,
  568. sizeof(acpi_gbl_print_buffer), format, args);
  569. (void)fwrite(acpi_gbl_print_buffer, length, 1, file);
  570. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  571. return (length);
  572. }
  573. /*******************************************************************************
  574. *
  575. * FUNCTION: fprintf
  576. *
  577. * PARAMETERS: file - File descriptor
  578. * Format, ... - Standard printf format
  579. *
  580. * RETURN: Number of bytes actually written.
  581. *
  582. * DESCRIPTION: Formatted output to a file.
  583. *
  584. ******************************************************************************/
  585. int fprintf(FILE * file, const char *format, ...)
  586. {
  587. va_list args;
  588. int length;
  589. va_start(args, format);
  590. length = vfprintf(file, format, args);
  591. va_end(args);
  592. return (length);
  593. }
  594. #endif