printf_buffer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Multibyte and wide buffers for implementing printf-related functions.
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The naming of the multibyte and wide variants is intentionally
  16. consistent, so that it is possible to use the Xprintf macro in
  17. stdio-common/printf_buffer-char.h and
  18. stdio-common/printf_buffer-wchar_t.h to select between them in
  19. type-generic code. */
  20. #ifndef PRINTF_BUFFER_H
  21. #define PRINTF_BUFFER_H
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #include <wchar.h>
  26. /* <printf_buffer_as_file.h> introduces a way to use struct
  27. __printf_buffer objects from FILE * streams. To avoid storing a
  28. function pointer (or vtable pointer) in struct __printf_buffer
  29. (which would defeat libio vtable hardening), a switch statement
  30. over the different flush implementations is used to implement
  31. __printf_buffer_flush.
  32. __printf_buffer_mode_failed is special: it is the sticky failure
  33. indicator. Unlike struct alloc_buffer, this is not folded into
  34. write_ptr, so that snprintf and other string-writing functions can
  35. discover the end of the string even in the error case, to be able
  36. to add the null terminator. */
  37. enum __printf_buffer_mode
  38. {
  39. __printf_buffer_mode_failed,
  40. __printf_buffer_mode_sprintf,
  41. __printf_buffer_mode_snprintf,
  42. __printf_buffer_mode_sprintf_chk,
  43. __printf_buffer_mode_to_file,
  44. __printf_buffer_mode_asprintf,
  45. __printf_buffer_mode_dprintf,
  46. __printf_buffer_mode_strfmon,
  47. __printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
  48. __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */
  49. __printf_buffer_mode_fphex_to_wide, /* For __wprintf_fphex_l_buffer. */
  50. __printf_buffer_mode_obstack, /* For __printf_buffer_flush_obstack. */
  51. };
  52. /* Buffer for fast character writing with overflow handling.
  53. Typically embedded in another struct with further data that is used
  54. by the flush function. */
  55. struct __printf_buffer
  56. {
  57. /* These pointer members follow FILE streams. write_ptr and
  58. write_end must be initialized to cover the target buffer. See
  59. __printf_buffer_init.
  60. Data can be written directly to *write_ptr while write_ptr !=
  61. write_end, and write_ptr can be advanced accordingly. Note that
  62. is not possible to use the apparently-unused part of the buffer
  63. as scratch space because sprintf (and snprintf, but that is a bit
  64. iffy) must only write the minimum number of characters produced
  65. by the format string and its arguments.
  66. write_base must be initialized to be equal to write_ptr. The
  67. framework uses this pointer to compute the total number of
  68. written bytes, together with the written field. See
  69. __printf_buffer_done.
  70. write_base and write_end are only read by the generic functions
  71. after initialization, only the flush implementation called from
  72. __printf_buffer_flush might change these pointers. See the
  73. comment on Xprintf (buffer_do_flush) in Xprintf_buffer_flush.c
  74. for details regarding the flush operation. */
  75. char *write_base;
  76. char *write_ptr;
  77. char *write_end;
  78. /* Number of characters written so far (excluding the current
  79. buffer). Potentially updated on flush. The actual number of
  80. written bytes also includes the unflushed-but-written buffer
  81. part, write_ptr - write_base. A 64-bit value is used to avoid
  82. the need for overflow checks. */
  83. uint64_t written;
  84. /* Identifies the flush callback. */
  85. enum __printf_buffer_mode mode;
  86. };
  87. /* Marks the buffer as failed, so that __printf_buffer_has_failed
  88. returns true and future flush operations are no-ops. */
  89. static inline void
  90. __printf_buffer_mark_failed (struct __printf_buffer *buf)
  91. {
  92. buf->mode = __printf_buffer_mode_failed;
  93. }
  94. /* Returns true if the sticky error indicator of the buffer has been
  95. set to failed. */
  96. static inline bool __attribute_warn_unused_result__
  97. __printf_buffer_has_failed (struct __printf_buffer *buf)
  98. {
  99. return buf->mode == __printf_buffer_mode_failed;
  100. }
  101. /* Initialization of a buffer, using the memory region from [BASE,
  102. END) as the initial buffer contents. */
  103. static inline void
  104. __printf_buffer_init_end (struct __printf_buffer *buf, char *base, char *end,
  105. enum __printf_buffer_mode mode)
  106. {
  107. buf->write_base = base;
  108. buf->write_ptr = base;
  109. buf->write_end = end;
  110. buf->written = 0;
  111. buf->mode = mode;
  112. }
  113. /* Initialization of a buffer, using the memory region from [BASE, BASE +LEN)
  114. as the initial buffer contents. LEN can be zero. */
  115. static inline void
  116. __printf_buffer_init (struct __printf_buffer *buf, char *base, size_t len,
  117. enum __printf_buffer_mode mode)
  118. {
  119. __printf_buffer_init_end (buf, base, base + len, mode);
  120. }
  121. /* Called by printf_buffer_putc for a full buffer. */
  122. void __printf_buffer_putc_1 (struct __printf_buffer *buf, char ch)
  123. attribute_hidden;
  124. /* Writes CH to BUF. */
  125. static inline void
  126. __printf_buffer_putc (struct __printf_buffer *buf, char ch)
  127. {
  128. if (buf->write_ptr != buf->write_end)
  129. *buf->write_ptr++ = ch;
  130. else
  131. __printf_buffer_putc_1 (buf, ch);
  132. }
  133. /* Writes COUNT repeats of CH to BUF. */
  134. void __printf_buffer_pad_1 (struct __printf_buffer *buf,
  135. char ch, size_t count) attribute_hidden;
  136. /* __printf_buffer_pad with fast path for no padding. COUNT is
  137. ssize_t to accommodate signed uses in printf and elsewhere. */
  138. static inline void
  139. __printf_buffer_pad (struct __printf_buffer *buf, char ch, ssize_t count)
  140. {
  141. if (count > 0)
  142. __printf_buffer_pad_1 (buf, ch, count);
  143. }
  144. /* Write COUNT bytes starting at S to BUF. S must not overlap with
  145. the internal buffer. */
  146. void __printf_buffer_write (struct __printf_buffer *buf, const char *s,
  147. size_t count) attribute_hidden;
  148. /* Write S to BUF. S must not overlap with the internal buffer. */
  149. void __printf_buffer_puts_1 (struct __printf_buffer *buf, const char *s)
  150. attribute_hidden;
  151. static inline void
  152. __printf_buffer_puts (struct __printf_buffer *buf, const char *s)
  153. {
  154. if (__builtin_constant_p (__builtin_strlen (s)))
  155. __printf_buffer_write (buf, s, __builtin_strlen (s));
  156. else
  157. __printf_buffer_puts_1 (buf, s);
  158. }
  159. /* Returns the number of bytes written through the buffer, or -1 if
  160. there was an error (that is, __printf_buffer_has_failed (BUF) is true).
  161. The number of written bytes includes pending bytes in the buffer
  162. (between BUF->write_base and BUF->write_ptr).
  163. If the number is larger than INT_MAX, returns -1 and sets errno to
  164. EOVERFLOW. This function does not flush the buffer. If the caller
  165. needs the side effect of flushing, it has to do this
  166. separately. */
  167. int __printf_buffer_done (struct __printf_buffer *buf) attribute_hidden;
  168. /* Internally used to call the flush function. This can be called
  169. explicitly for certain modes to flush the buffer prematuraly. In
  170. such cases, it is often the case that the buffer mode is statically
  171. known, and the flush implementation can be called directly. */
  172. bool __printf_buffer_flush (struct __printf_buffer *buf) attribute_hidden;
  173. /* Wide version of struct __printf_buffer follows. */
  174. enum __wprintf_buffer_mode
  175. {
  176. __wprintf_buffer_mode_failed,
  177. __wprintf_buffer_mode_swprintf,
  178. __wprintf_buffer_mode_to_file,
  179. };
  180. struct __wprintf_buffer
  181. {
  182. wchar_t *write_base;
  183. wchar_t *write_ptr;
  184. wchar_t *write_end;
  185. uint64_t written;
  186. enum __wprintf_buffer_mode mode;
  187. };
  188. static inline void
  189. __wprintf_buffer_mark_failed (struct __wprintf_buffer *buf)
  190. {
  191. buf->mode = __wprintf_buffer_mode_failed;
  192. }
  193. static inline bool __attribute_warn_unused_result__
  194. __wprintf_buffer_has_failed (struct __wprintf_buffer *buf)
  195. {
  196. return buf->mode == __wprintf_buffer_mode_failed;
  197. }
  198. static inline void
  199. __wprintf_buffer_init (struct __wprintf_buffer *buf,
  200. wchar_t *base, size_t len,
  201. enum __wprintf_buffer_mode mode)
  202. {
  203. buf->write_base = base;
  204. buf->write_ptr = base;
  205. buf->write_end = base + len;
  206. buf->written = 0;
  207. buf->mode = mode;
  208. }
  209. void __wprintf_buffer_putc_1 (struct __wprintf_buffer *buf, wchar_t ch)
  210. attribute_hidden;
  211. static inline void
  212. __wprintf_buffer_putc (struct __wprintf_buffer *buf, wchar_t ch)
  213. {
  214. if (buf->write_ptr != buf->write_end)
  215. *buf->write_ptr++ = ch;
  216. else
  217. __wprintf_buffer_putc_1 (buf, ch);
  218. }
  219. void __wprintf_buffer_pad_1 (struct __wprintf_buffer *buf,
  220. wchar_t ch, size_t count) attribute_hidden;
  221. static inline void
  222. __wprintf_buffer_pad (struct __wprintf_buffer *buf, char ch, ssize_t count)
  223. {
  224. if (count > 0)
  225. __wprintf_buffer_pad_1 (buf, ch, count);
  226. }
  227. void __wprintf_buffer_write (struct __wprintf_buffer *buf, const wchar_t *s,
  228. size_t count) attribute_hidden;
  229. void __wprintf_buffer_puts (struct __wprintf_buffer *buf, const wchar_t *s)
  230. attribute_hidden;
  231. int __wprintf_buffer_done (struct __wprintf_buffer *buf) attribute_hidden;
  232. bool __wprintf_buffer_flush (struct __wprintf_buffer *buf) attribute_hidden;
  233. /* Type-generic convenience macros. They are useful if
  234. printf_buffer-char.h or printf_buffer-wchar_t.h is included as
  235. well. */
  236. #define Xprintf_buffer Xprintf (buffer)
  237. #define Xprintf_buffer_done Xprintf (buffer_done)
  238. #define Xprintf_buffer_flush Xprintf (buffer_flush)
  239. #define Xprintf_buffer_has_failed Xprintf (buffer_has_failed)
  240. #define Xprintf_buffer_mark_failed Xprintf (buffer_mark_failed)
  241. #define Xprintf_buffer_pad Xprintf (buffer_pad)
  242. #define Xprintf_buffer_putc Xprintf (buffer_putc)
  243. #define Xprintf_buffer_puts Xprintf (buffer_puts)
  244. #define Xprintf_buffer_write Xprintf (buffer_write)
  245. /* Commonly used buffers. */
  246. struct __printf_buffer_snprintf
  247. {
  248. struct __printf_buffer base;
  249. #define PRINTF_BUFFER_SIZE_DISCARD 128
  250. char discard[PRINTF_BUFFER_SIZE_DISCARD]; /* Used in counting mode. */
  251. };
  252. /* Sets up [BUFFER, BUFFER + LENGTH) as the write target. If LENGTH
  253. is positive, also writes a NUL byte to *BUFFER. */
  254. void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *,
  255. char *buffer, size_t length)
  256. attribute_hidden;
  257. /* Add the null terminator after everything has been written. The
  258. return value is the one expected by printf (see __printf_buffer_done). */
  259. int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *)
  260. attribute_hidden;
  261. /* Flush function implementations follow. They are called from
  262. __printf_buffer_flush. Generic code should not call these flush
  263. functions directly. Some modes have inline implementations. */
  264. void __printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *)
  265. attribute_hidden;
  266. struct __printf_buffer_to_file;
  267. void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *)
  268. attribute_hidden;
  269. struct __printf_buffer_asprintf;
  270. void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *)
  271. attribute_hidden;
  272. struct __printf_buffer_dprintf;
  273. void __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *)
  274. attribute_hidden;
  275. struct __printf_buffer_fp;
  276. void __printf_buffer_flush_fp (struct __printf_buffer_fp *)
  277. attribute_hidden;
  278. struct __printf_buffer_fp_to_wide;
  279. void __printf_buffer_flush_fp_to_wide (struct __printf_buffer_fp_to_wide *)
  280. attribute_hidden;
  281. struct __printf_buffer_fphex_to_wide;
  282. void __printf_buffer_flush_fphex_to_wide (struct
  283. __printf_buffer_fphex_to_wide *)
  284. attribute_hidden;
  285. struct __printf_buffer_obstack;
  286. void __printf_buffer_flush_obstack (struct __printf_buffer_obstack *)
  287. attribute_hidden;
  288. struct __wprintf_buffer_to_file;
  289. void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *)
  290. attribute_hidden;
  291. /* Buffer sizes. These can be tuned as necessary. There is a tension
  292. here between stack consumption, cache usage, and additional system
  293. calls or heap allocations (if the buffer is too small).
  294. Also see PRINTF_BUFFER_SIZE_DISCARD above for snprintf. */
  295. /* Fallback buffer if the underlying FILE * stream does not provide
  296. buffer space. */
  297. #define PRINTF_BUFFER_SIZE_TO_FILE_STAGE 128
  298. /* Temporary buffer used during floating point digit translation. */
  299. #define PRINTF_BUFFER_SIZE_DIGITS 64
  300. /* Size of the initial on-stack buffer for asprintf. It should be
  301. large enough to copy almost all asprintf usages with just a single
  302. (final, correctly sized) heap allocation. */
  303. #define PRINTF_BUFFER_SIZE_ASPRINTF 200
  304. /* This should cover most of the packet-oriented file descriptors,
  305. where boundaries between writes could be visible to readers. But
  306. it is still small enough not to cause too many stack overflow issues. */
  307. #define PRINTF_BUFFER_SIZE_DPRINTF 2048
  308. #endif /* PRINTF_BUFFER_H */