utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. case 'x':
  399. base = 16;
  400. break;
  401. case 'd':
  402. case 'i':
  403. type |= ACPI_FORMAT_SIGN;
  404. case 'u':
  405. break;
  406. case 'p':
  407. if (width == -1) {
  408. width = 2 * sizeof(void *);
  409. type |= ACPI_FORMAT_ZERO;
  410. }
  411. p = va_arg(args, void *);
  412. pos =
  413. acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
  414. 16, width, precision, type);
  415. continue;
  416. default:
  417. pos = acpi_ut_bound_string_output(pos, end, '%');
  418. if (*format) {
  419. pos =
  420. acpi_ut_bound_string_output(pos, end,
  421. *format);
  422. } else {
  423. --format;
  424. }
  425. continue;
  426. }
  427. if (qualifier == 'L') {
  428. number = va_arg(args, u64);
  429. if (type & ACPI_FORMAT_SIGN) {
  430. number = (s64)number;
  431. }
  432. } else if (qualifier == 'l') {
  433. number = va_arg(args, unsigned long);
  434. if (type & ACPI_FORMAT_SIGN) {
  435. number = (s32)number;
  436. }
  437. } else if (qualifier == 'h') {
  438. number = (u16)va_arg(args, int);
  439. if (type & ACPI_FORMAT_SIGN) {
  440. number = (s16)number;
  441. }
  442. } else {
  443. number = va_arg(args, unsigned int);
  444. if (type & ACPI_FORMAT_SIGN) {
  445. number = (signed int)number;
  446. }
  447. }
  448. pos = acpi_ut_format_number(pos, end, number, base,
  449. width, precision, type);
  450. }
  451. if (size > 0) {
  452. if (pos < end) {
  453. *pos = '\0';
  454. } else {
  455. end[-1] = '\0';
  456. }
  457. }
  458. return ((int)ACPI_PTR_DIFF(pos, string));
  459. }
  460. /*******************************************************************************
  461. *
  462. * FUNCTION: snprintf
  463. *
  464. * PARAMETERS: string - String with boundary
  465. * size - Boundary of the string
  466. * Format, ... - Standard printf format
  467. *
  468. * RETURN: Number of bytes actually written.
  469. *
  470. * DESCRIPTION: Formatted output to a string.
  471. *
  472. ******************************************************************************/
  473. int snprintf(char *string, acpi_size size, const char *format, ...)
  474. {
  475. va_list args;
  476. int length;
  477. va_start(args, format);
  478. length = vsnprintf(string, size, format, args);
  479. va_end(args);
  480. return (length);
  481. }
  482. /*******************************************************************************
  483. *
  484. * FUNCTION: sprintf
  485. *
  486. * PARAMETERS: string - String with boundary
  487. * Format, ... - Standard printf format
  488. *
  489. * RETURN: Number of bytes actually written.
  490. *
  491. * DESCRIPTION: Formatted output to a string.
  492. *
  493. ******************************************************************************/
  494. int sprintf(char *string, const char *format, ...)
  495. {
  496. va_list args;
  497. int length;
  498. va_start(args, format);
  499. length = vsnprintf(string, ACPI_UINT32_MAX, format, args);
  500. va_end(args);
  501. return (length);
  502. }
  503. #ifdef ACPI_APPLICATION
  504. /*******************************************************************************
  505. *
  506. * FUNCTION: vprintf
  507. *
  508. * PARAMETERS: format - Standard printf format
  509. * args - Argument list
  510. *
  511. * RETURN: Number of bytes actually written.
  512. *
  513. * DESCRIPTION: Formatted output to stdout using argument list pointer.
  514. *
  515. ******************************************************************************/
  516. int vprintf(const char *format, va_list args)
  517. {
  518. acpi_cpu_flags flags;
  519. int length;
  520. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  521. length = vsnprintf(acpi_gbl_print_buffer,
  522. sizeof(acpi_gbl_print_buffer), format, args);
  523. (void)fwrite(acpi_gbl_print_buffer, length, 1, ACPI_FILE_OUT);
  524. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  525. return (length);
  526. }
  527. /*******************************************************************************
  528. *
  529. * FUNCTION: printf
  530. *
  531. * PARAMETERS: Format, ... - Standard printf format
  532. *
  533. * RETURN: Number of bytes actually written.
  534. *
  535. * DESCRIPTION: Formatted output to stdout.
  536. *
  537. ******************************************************************************/
  538. int printf(const char *format, ...)
  539. {
  540. va_list args;
  541. int length;
  542. va_start(args, format);
  543. length = vprintf(format, args);
  544. va_end(args);
  545. return (length);
  546. }
  547. /*******************************************************************************
  548. *
  549. * FUNCTION: vfprintf
  550. *
  551. * PARAMETERS: file - File descriptor
  552. * format - Standard printf format
  553. * args - Argument list
  554. *
  555. * RETURN: Number of bytes actually written.
  556. *
  557. * DESCRIPTION: Formatted output to a file using argument list pointer.
  558. *
  559. ******************************************************************************/
  560. int vfprintf(FILE * file, const char *format, va_list args)
  561. {
  562. acpi_cpu_flags flags;
  563. int length;
  564. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  565. length = vsnprintf(acpi_gbl_print_buffer,
  566. sizeof(acpi_gbl_print_buffer), format, args);
  567. (void)fwrite(acpi_gbl_print_buffer, length, 1, file);
  568. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  569. return (length);
  570. }
  571. /*******************************************************************************
  572. *
  573. * FUNCTION: fprintf
  574. *
  575. * PARAMETERS: file - File descriptor
  576. * Format, ... - Standard printf format
  577. *
  578. * RETURN: Number of bytes actually written.
  579. *
  580. * DESCRIPTION: Formatted output to a file.
  581. *
  582. ******************************************************************************/
  583. int fprintf(FILE * file, const char *format, ...)
  584. {
  585. va_list args;
  586. int length;
  587. va_start(args, format);
  588. length = vfprintf(file, format, args);
  589. va_end(args);
  590. return (length);
  591. }
  592. #endif