1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Lightweight buffered reading library.
5 * Copyright 2019 Google LLC.
15 #include <linux/types.h>
18 /* File descriptor being read/ */
20 /* Size of the read buffer. */
22 /* Pointer to storage for buffering read. */
24 /* End of the storage. */
26 /* Currently accessed data pointer. */
28 /* Read timeout, 0 implies no timeout. */
30 /* Set true on when the end of file on read error. */
34 static inline void io__init(struct io
*io
, int fd
,
35 char *buf
, unsigned int buf_len
)
38 io
->buf_len
= buf_len
;
46 /* Read from fd filling the buffer. Called when io->data == io->end. */
47 static inline int io__fill_buffer(struct io
*io
)
54 if (io
->timeout_ms
!= 0) {
55 struct pollfd pfds
[] = {
62 n
= poll(pfds
, 1, io
->timeout_ms
);
65 if (n
> 0 && !(pfds
[0].revents
& POLLIN
)) {
74 n
= read(io
->fd
, io
->buf
, io
->buf_len
);
80 io
->data
= &io
->buf
[0];
81 io
->end
= &io
->buf
[n
];
85 /* Reads one character from the "io" file with similar semantics to fgetc. */
86 static inline int io__get_char(struct io
*io
)
88 if (io
->data
== io
->end
) {
89 int ret
= io__fill_buffer(io
);
97 /* Read a hexadecimal value with no 0x prefix into the out argument hex. If the
98 * first character isn't hexadecimal returns -2, io->eof returns -1, otherwise
99 * returns the character after the hexadecimal value which may be -1 for eof.
100 * If the read value is larger than a u64 the high-order bits will be dropped.
102 static inline int io__get_hex(struct io
*io
, __u64
*hex
)
104 bool first_read
= true;
108 int ch
= io__get_char(io
);
112 if (ch
>= '0' && ch
<= '9')
113 *hex
= (*hex
<< 4) | (ch
- '0');
114 else if (ch
>= 'a' && ch
<= 'f')
115 *hex
= (*hex
<< 4) | (ch
- 'a' + 10);
116 else if (ch
>= 'A' && ch
<= 'F')
117 *hex
= (*hex
<< 4) | (ch
- 'A' + 10);
126 /* Read a positive decimal value with out argument dec. If the first character
127 * isn't a decimal returns -2, io->eof returns -1, otherwise returns the
128 * character after the decimal value which may be -1 for eof. If the read value
129 * is larger than a u64 the high-order bits will be dropped.
131 static inline int io__get_dec(struct io
*io
, __u64
*dec
)
133 bool first_read
= true;
137 int ch
= io__get_char(io
);
141 if (ch
>= '0' && ch
<= '9')
142 *dec
= (*dec
* 10) + ch
- '0';
151 /* Read up to and including the first delim. */
152 static inline ssize_t
io__getdelim(struct io
*io
, char **line_out
, size_t *line_len_out
, int delim
)
156 char *line
= NULL
, *temp
;
160 /* TODO: reuse previously allocated memory. */
162 while (ch
!= delim
) {
163 ch
= io__get_char(io
);
168 if (buf_pos
== sizeof(buf
)) {
169 temp
= realloc(line
, line_len
+ sizeof(buf
));
173 memcpy(&line
[line_len
], buf
, sizeof(buf
));
174 line_len
+= sizeof(buf
);
177 buf
[buf_pos
++] = (char)ch
;
179 temp
= realloc(line
, line_len
+ buf_pos
+ 1);
183 memcpy(&line
[line_len
], buf
, buf_pos
);
184 line
[line_len
+ buf_pos
] = '\0';
187 *line_len_out
= line_len
;
196 static inline ssize_t
io__getline(struct io
*io
, char **line_out
, size_t *line_len_out
)
198 return io__getdelim(io
, line_out
, line_len_out
, /*delim=*/'\n');
201 #endif /* __API_IO__ */