utprint.c 16 KB

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