xref: /linux/include/linux/seq_buf.h (revision e3f8dfafd7d6ef905afd6603b0b57984abd0677b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SEQ_BUF_H
3 #define _LINUX_SEQ_BUF_H
4 
5 #include <linux/bug.h>
6 #include <linux/minmax.h>
7 #include <linux/seq_file.h>
8 #include <linux/string.h>
9 #include <linux/types.h>
10 
11 /*
12  * Trace sequences are used to allow a function to call several other functions
13  * to create a string of data to use.
14  */
15 
16 /**
17  * struct seq_buf - seq buffer structure
18  * @buffer:	pointer to the buffer
19  * @size:	size of the buffer
20  * @len:	the amount of data inside the buffer
21  */
22 struct seq_buf {
23 	char			*buffer;
24 	size_t			size;
25 	size_t			len;
26 };
27 
28 #define DECLARE_SEQ_BUF(NAME, SIZE)			\
29 	struct seq_buf NAME = {				\
30 		.buffer = (char[SIZE]) { 0 },		\
31 		.size = SIZE,				\
32 	}
33 
34 /**
35  * seq_buf_clear - reset the seq_buf to be read / appended from the beginning
36  * @s: the seq_buf handle
37  */
38 static inline void seq_buf_clear(struct seq_buf *s)
39 {
40 	s->len = 0;
41 	if (s->size)
42 		s->buffer[0] = '\0';
43 }
44 
45 /**
46  * seq_buf_init - initialize a seq_buf
47  * @s: the seq_buf handle
48  * @buf: pointer to the buffer
49  * @size: total size of @buf
50  *
51  * The contents of the buffer are ignored.
52  */
53 static inline void
54 seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
55 {
56 	s->buffer = buf;
57 	s->size = size;
58 	seq_buf_clear(s);
59 }
60 
61 /*
62  * seq_buf have a buffer that might overflow. When this happens
63  * len is set to be greater than size.
64  */
65 static inline bool
66 seq_buf_has_overflowed(struct seq_buf *s)
67 {
68 	return s->len > s->size;
69 }
70 
71 /*
72  * Mark @s as overflowed, which discards the length of what it holds. The
73  * bytes up to its last one are the string from then on, as that is where
74  * seq_buf_str() terminates it, so clear whatever was not written: a writer
75  * sets len to how much it filled, and anything past that was never adopted.
76  */
77 static inline void
78 seq_buf_set_overflow(struct seq_buf *s)
79 {
80 	if (s->len < s->size)
81 		memset(s->buffer + s->len, 0, s->size - s->len);
82 
83 	s->len = s->size + 1;
84 }
85 
86 /**
87  * seq_buf_init_append - initialize a seq_buf over a buffer that may
88  *			 already hold NUL-terminated content
89  * @s: the seq_buf handle
90  * @buf: pointer to the (possibly non-empty) buffer
91  * @size: total size of @buf
92  *
93  * Unlike seq_buf_init(), which always starts @buf at len=0, this
94  * preserves whatever NUL-terminated content @buf already holds and
95  * positions @s to append after it. Useful for converting code that used
96  * to append to an existing buffer with strlcat()/scnprintf() and friends.
97  *
98  * If @buf holds no NUL within @size, @s starts out overflowed, as
99  * strlcat() treats such a buffer as already truncated.
100  */
101 static inline void
102 seq_buf_init_append(struct seq_buf *s, char *buf, unsigned int size)
103 {
104 	s->buffer = buf;
105 	s->size = size;
106 	s->len = strnlen(buf, size);
107 	if (s->len == size)
108 		seq_buf_set_overflow(s);
109 }
110 
111 /*
112  * How much buffer is left on the seq_buf?
113  */
114 static inline unsigned int
115 seq_buf_buffer_left(struct seq_buf *s)
116 {
117 	if (seq_buf_has_overflowed(s))
118 		return 0;
119 
120 	return s->size - s->len;
121 }
122 
123 /* How much buffer was written? */
124 static inline unsigned int seq_buf_used(struct seq_buf *s)
125 {
126 	return min(s->len, s->size);
127 }
128 
129 /*
130  * NUL-terminate the buffer in @s: directly after the data when there is
131  * room for it, otherwise in the last byte of the buffer. @s->size must not
132  * be zero.
133  *
134  * Returns: the offset of the NUL.
135  */
136 static inline size_t __seq_buf_terminate(struct seq_buf *s)
137 {
138 	size_t end;
139 
140 	if (seq_buf_buffer_left(s))
141 		end = s->len;
142 	else
143 		end = s->size - 1;
144 
145 	s->buffer[end] = 0;
146 
147 	return end;
148 }
149 
150 /**
151  * seq_buf_str - get NUL-terminated C string from seq_buf
152  * @s: the seq_buf handle
153  *
154  * This makes sure that the buffer in @s is NUL-terminated and
155  * safe to read as a string.
156  *
157  * Note, if this is called when the buffer has overflowed, then
158  * the last byte of the buffer is zeroed, and the len will still
159  * point passed it. The same happens when the buffer is exactly
160  * full: the NUL takes the place of the last byte written, which is
161  * lost, though seq_buf_used() still counts it.
162  *
163  * A zero-sized seq_buf has nowhere to put a NUL, so the empty string
164  * is returned instead of writing to @s->buffer. Any other seq_buf
165  * returns @s->buffer, even when it holds an empty string, so callers
166  * always get their own buffer back.
167  *
168  * After this function is called, s->buffer is safe to use
169  * in string operations.
170  *
171  * Returns: @s->buf after making sure it is terminated.
172  */
173 static inline const char *seq_buf_str(struct seq_buf *s)
174 {
175 	if (s->size == 0)
176 		return "";
177 
178 	__seq_buf_terminate(s);
179 
180 	return s->buffer;
181 }
182 
183 /**
184  * seq_buf_strlen - get the length of the NUL-terminated C string in seq_buf
185  * @s: the seq_buf handle
186  *
187  * This makes sure that the buffer in @s is NUL-terminated, exactly as
188  * seq_buf_str() does, and returns the length of the resulting string
189  * without walking it. Unlike seq_buf_used(), this does not count the byte
190  * given up to the NUL when the buffer is full or has overflowed. When the
191  * buffer is exactly full, that byte is the last one written, and calling
192  * either function loses it.
193  *
194  * A zero-sized seq_buf holds no string, so 0 is returned without writing
195  * to @s->buffer, matching what seq_buf_str() returns for one.
196  *
197  * After this function is called, s->buffer is safe to use
198  * in string operations.
199  *
200  * Returns: the offset of the NUL that terminates @s->buffer. That is the
201  * length of the string unless an earlier NUL is in the way, either one the
202  * data written to @s carried itself, or one seq_buf_set_overflow() left
203  * behind when it cleared what no writer had claimed.
204  */
205 static inline size_t seq_buf_strlen(struct seq_buf *s)
206 {
207 	if (s->size == 0)
208 		return 0;
209 
210 	return __seq_buf_terminate(s);
211 }
212 
213 /**
214  * seq_buf_terminate - NUL-terminate the string in a seq_buf
215  * @s: the seq_buf handle
216  *
217  * Terminate @s->buffer exactly as seq_buf_str() and seq_buf_strlen() do,
218  * for callers that want neither the pointer nor the length and only need
219  * the buffer to be safe to read as a C string. A zero-sized seq_buf has
220  * nowhere to put a NUL and is left untouched.
221  *
222  * Nothing is returned on purpose: a caller that wants the length should
223  * use seq_buf_strlen(), which says so.
224  *
225  * After this function is called, s->buffer is safe to use
226  * in string operations.
227  */
228 static inline void seq_buf_terminate(struct seq_buf *s)
229 {
230 	if (s->size == 0)
231 		return;
232 
233 	__seq_buf_terminate(s);
234 }
235 
236 /**
237  * seq_buf_get_buf - get buffer to write arbitrary data to
238  * @s: the seq_buf handle
239  * @bufp: the beginning of the buffer is stored here
240  *
241  * Returns: the number of bytes available in the buffer, or zero if
242  * there's no space.
243  */
244 static inline size_t seq_buf_get_buf(struct seq_buf *s, char **bufp)
245 {
246 	WARN_ON(s->len > s->size + 1);
247 
248 	if (s->len < s->size) {
249 		*bufp = s->buffer + s->len;
250 		return s->size - s->len;
251 	}
252 
253 	*bufp = NULL;
254 	return 0;
255 }
256 
257 /**
258  * seq_buf_commit - commit data to the buffer
259  * @s: the seq_buf handle
260  * @num: the number of bytes to commit
261  *
262  * Commit @num bytes of data written to a buffer previously acquired
263  * by seq_buf_get_buf(). To signal an error condition, or that the data
264  * didn't fit in the available space, pass a negative @num value.
265  */
266 static inline void seq_buf_commit(struct seq_buf *s, int num)
267 {
268 	if (num < 0) {
269 		seq_buf_set_overflow(s);
270 	} else {
271 		/* num must be negative on overflow */
272 		BUG_ON(s->len + num > s->size);
273 		s->len += num;
274 	}
275 }
276 
277 /**
278  * seq_buf_pop - pop off the last written character
279  * @s: the seq_buf handle
280  *
281  * Removes the last written character to the seq_buf @s.
282  *
283  * Returns the last character, or -1 if @s is empty or has overflowed.
284  */
285 static inline int seq_buf_pop(struct seq_buf *s)
286 {
287 	if (!s->len || seq_buf_has_overflowed(s))
288 		return -1;
289 
290 	s->len--;
291 	return (unsigned int)s->buffer[s->len];
292 }
293 
294 extern __printf(2, 3)
295 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...);
296 extern __printf(2, 0)
297 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args);
298 extern int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s);
299 extern int seq_buf_to_user(struct seq_buf *s, char __user *ubuf,
300 			   size_t start, int cnt);
301 extern int seq_buf_puts(struct seq_buf *s, const char *str);
302 extern int seq_buf_putc(struct seq_buf *s, unsigned char c);
303 extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
304 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
305 			      unsigned int len);
306 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
307 extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
308 			    int prefix_type, int rowsize, int groupsize,
309 			    const void *buf, size_t len, bool ascii);
310 
311 #ifdef CONFIG_BINARY_PRINTF
312 __printf(2, 0)
313 int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
314 #endif
315 
316 void seq_buf_do_printk(struct seq_buf *s, const char *lvl);
317 
318 #endif /* _LINUX_SEQ_BUF_H */
319