utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 - 2022, 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. if (size != ACPI_UINT32_MAX) {
  288. end = string + size;
  289. } else {
  290. end = ACPI_CAST_PTR(char, ACPI_UINT32_MAX);
  291. }
  292. for (; *format; ++format) {
  293. if (*format != '%') {
  294. pos = acpi_ut_bound_string_output(pos, end, *format);
  295. continue;
  296. }
  297. type = 0;
  298. base = 10;
  299. /* Process sign */
  300. do {
  301. ++format;
  302. if (*format == '#') {
  303. type |= ACPI_FORMAT_PREFIX;
  304. } else if (*format == '0') {
  305. type |= ACPI_FORMAT_ZERO;
  306. } else if (*format == '+') {
  307. type |= ACPI_FORMAT_SIGN_PLUS;
  308. } else if (*format == ' ') {
  309. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  310. } else if (*format == '-') {
  311. type |= ACPI_FORMAT_LEFT;
  312. } else {
  313. break;
  314. }
  315. } while (1);
  316. /* Process width */
  317. width = -1;
  318. if (isdigit((int)*format)) {
  319. format = acpi_ut_scan_number(format, &number);
  320. width = (s32)number;
  321. } else if (*format == '*') {
  322. ++format;
  323. width = va_arg(args, int);
  324. if (width < 0) {
  325. width = -width;
  326. type |= ACPI_FORMAT_LEFT;
  327. }
  328. }
  329. /* Process precision */
  330. precision = -1;
  331. if (*format == '.') {
  332. ++format;
  333. if (isdigit((int)*format)) {
  334. format = acpi_ut_scan_number(format, &number);
  335. precision = (s32)number;
  336. } else if (*format == '*') {
  337. ++format;
  338. precision = va_arg(args, int);
  339. }
  340. if (precision < 0) {
  341. precision = 0;
  342. }
  343. }
  344. /* Process qualifier */
  345. qualifier = -1;
  346. if (*format == 'h' || *format == 'l' || *format == 'L') {
  347. qualifier = *format;
  348. ++format;
  349. if (qualifier == 'l' && *format == 'l') {
  350. qualifier = 'L';
  351. ++format;
  352. }
  353. }
  354. switch (*format) {
  355. case '%':
  356. pos = acpi_ut_bound_string_output(pos, end, '%');
  357. continue;
  358. case 'c':
  359. if (!(type & ACPI_FORMAT_LEFT)) {
  360. while (--width > 0) {
  361. pos =
  362. acpi_ut_bound_string_output(pos,
  363. end,
  364. ' ');
  365. }
  366. }
  367. c = (char)va_arg(args, int);
  368. pos = acpi_ut_bound_string_output(pos, end, c);
  369. while (--width > 0) {
  370. pos =
  371. acpi_ut_bound_string_output(pos, end, ' ');
  372. }
  373. continue;
  374. case 's':
  375. s = va_arg(args, char *);
  376. if (!s) {
  377. s = "<NULL>";
  378. }
  379. length = (s32)acpi_ut_bound_string_length(s, precision);
  380. if (!(type & ACPI_FORMAT_LEFT)) {
  381. while (length < width--) {
  382. pos =
  383. acpi_ut_bound_string_output(pos,
  384. end,
  385. ' ');
  386. }
  387. }
  388. for (i = 0; i < length; ++i) {
  389. pos = acpi_ut_bound_string_output(pos, end, *s);
  390. ++s;
  391. }
  392. while (length < width--) {
  393. pos =
  394. acpi_ut_bound_string_output(pos, end, ' ');
  395. }
  396. continue;
  397. case 'o':
  398. base = 8;
  399. break;
  400. case 'X':
  401. type |= ACPI_FORMAT_UPPER;
  402. ACPI_FALLTHROUGH;
  403. case 'x':
  404. base = 16;
  405. break;
  406. case 'd':
  407. case 'i':
  408. type |= ACPI_FORMAT_SIGN;
  409. case 'u':
  410. break;
  411. case 'p':
  412. if (width == -1) {
  413. width = 2 * sizeof(void *);
  414. type |= ACPI_FORMAT_ZERO;
  415. }
  416. p = va_arg(args, void *);
  417. pos =
  418. acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
  419. 16, width, precision, type);
  420. continue;
  421. default:
  422. pos = acpi_ut_bound_string_output(pos, end, '%');
  423. if (*format) {
  424. pos =
  425. acpi_ut_bound_string_output(pos, end,
  426. *format);
  427. } else {
  428. --format;
  429. }
  430. continue;
  431. }
  432. if (qualifier == 'L') {
  433. number = va_arg(args, u64);
  434. if (type & ACPI_FORMAT_SIGN) {
  435. number = (s64)number;
  436. }
  437. } else if (qualifier == 'l') {
  438. number = va_arg(args, unsigned long);
  439. if (type & ACPI_FORMAT_SIGN) {
  440. number = (s32)number;
  441. }
  442. } else if (qualifier == 'h') {
  443. number = (u16)va_arg(args, int);
  444. if (type & ACPI_FORMAT_SIGN) {
  445. number = (s16)number;
  446. }
  447. } else {
  448. number = va_arg(args, unsigned int);
  449. if (type & ACPI_FORMAT_SIGN) {
  450. number = (signed int)number;
  451. }
  452. }
  453. pos = acpi_ut_format_number(pos, end, number, base,
  454. width, precision, type);
  455. }
  456. if (size > 0) {
  457. if (pos < end) {
  458. *pos = '\0';
  459. } else {
  460. end[-1] = '\0';
  461. }
  462. }
  463. return ((int)ACPI_PTR_DIFF(pos, string));
  464. }
  465. /*******************************************************************************
  466. *
  467. * FUNCTION: snprintf
  468. *
  469. * PARAMETERS: string - String with boundary
  470. * size - Boundary of the string
  471. * Format, ... - Standard printf format
  472. *
  473. * RETURN: Number of bytes actually written.
  474. *
  475. * DESCRIPTION: Formatted output to a string.
  476. *
  477. ******************************************************************************/
  478. int snprintf(char *string, acpi_size size, const char *format, ...)
  479. {
  480. va_list args;
  481. int length;
  482. va_start(args, format);
  483. length = vsnprintf(string, size, format, args);
  484. va_end(args);
  485. return (length);
  486. }
  487. /*******************************************************************************
  488. *
  489. * FUNCTION: sprintf
  490. *
  491. * PARAMETERS: string - String with boundary
  492. * Format, ... - Standard printf format
  493. *
  494. * RETURN: Number of bytes actually written.
  495. *
  496. * DESCRIPTION: Formatted output to a string.
  497. *
  498. ******************************************************************************/
  499. int sprintf(char *string, const char *format, ...)
  500. {
  501. va_list args;
  502. int length;
  503. va_start(args, format);
  504. length = vsnprintf(string, ACPI_UINT32_MAX, format, args);
  505. va_end(args);
  506. return (length);
  507. }
  508. #ifdef ACPI_APPLICATION
  509. /*******************************************************************************
  510. *
  511. * FUNCTION: vprintf
  512. *
  513. * PARAMETERS: format - Standard printf format
  514. * args - Argument list
  515. *
  516. * RETURN: Number of bytes actually written.
  517. *
  518. * DESCRIPTION: Formatted output to stdout using argument list pointer.
  519. *
  520. ******************************************************************************/
  521. int vprintf(const char *format, va_list args)
  522. {
  523. acpi_cpu_flags flags;
  524. int length;
  525. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  526. length = vsnprintf(acpi_gbl_print_buffer,
  527. sizeof(acpi_gbl_print_buffer), format, args);
  528. (void)fwrite(acpi_gbl_print_buffer, length, 1, ACPI_FILE_OUT);
  529. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  530. return (length);
  531. }
  532. /*******************************************************************************
  533. *
  534. * FUNCTION: printf
  535. *
  536. * PARAMETERS: Format, ... - Standard printf format
  537. *
  538. * RETURN: Number of bytes actually written.
  539. *
  540. * DESCRIPTION: Formatted output to stdout.
  541. *
  542. ******************************************************************************/
  543. int printf(const char *format, ...)
  544. {
  545. va_list args;
  546. int length;
  547. va_start(args, format);
  548. length = vprintf(format, args);
  549. va_end(args);
  550. return (length);
  551. }
  552. /*******************************************************************************
  553. *
  554. * FUNCTION: vfprintf
  555. *
  556. * PARAMETERS: file - File descriptor
  557. * format - Standard printf format
  558. * args - Argument list
  559. *
  560. * RETURN: Number of bytes actually written.
  561. *
  562. * DESCRIPTION: Formatted output to a file using argument list pointer.
  563. *
  564. ******************************************************************************/
  565. int vfprintf(FILE * file, const char *format, va_list args)
  566. {
  567. acpi_cpu_flags flags;
  568. int length;
  569. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  570. length = vsnprintf(acpi_gbl_print_buffer,
  571. sizeof(acpi_gbl_print_buffer), format, args);
  572. (void)fwrite(acpi_gbl_print_buffer, length, 1, file);
  573. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  574. return (length);
  575. }
  576. /*******************************************************************************
  577. *
  578. * FUNCTION: fprintf
  579. *
  580. * PARAMETERS: file - File descriptor
  581. * Format, ... - Standard printf format
  582. *
  583. * RETURN: Number of bytes actually written.
  584. *
  585. * DESCRIPTION: Formatted output to a file.
  586. *
  587. ******************************************************************************/
  588. int fprintf(FILE * file, const char *format, ...)
  589. {
  590. va_list args;
  591. int length;
  592. va_start(args, format);
  593. length = vfprintf(file, format, args);
  594. va_end(args);
  595. return (length);
  596. }
  597. #endif