1e95770afSIan Rogers /* SPDX-License-Identifier: GPL-2.0 */
2e95770afSIan Rogers /*
3e95770afSIan Rogers * Lightweight buffered reading library.
4e95770afSIan Rogers *
5e95770afSIan Rogers * Copyright 2019 Google LLC.
6e95770afSIan Rogers */
7e95770afSIan Rogers #ifndef __API_IO__
8e95770afSIan Rogers #define __API_IO__
9e95770afSIan Rogers
10c9dc580cSIan Rogers #include <errno.h>
11f4c0d530SIan Rogers #include <poll.h>
1253df2b93SIan Rogers #include <stdlib.h>
13c9dc580cSIan Rogers #include <string.h>
1453df2b93SIan Rogers #include <unistd.h>
15af76b2deSArnaldo Carvalho de Melo #include <linux/types.h>
1653df2b93SIan Rogers
17e95770afSIan Rogers struct io {
18e95770afSIan Rogers /* File descriptor being read/ */
19e95770afSIan Rogers int fd;
20e95770afSIan Rogers /* Size of the read buffer. */
21e95770afSIan Rogers unsigned int buf_len;
22e95770afSIan Rogers /* Pointer to storage for buffering read. */
23e95770afSIan Rogers char *buf;
24e95770afSIan Rogers /* End of the storage. */
25e95770afSIan Rogers char *end;
26e95770afSIan Rogers /* Currently accessed data pointer. */
27e95770afSIan Rogers char *data;
28f4c0d530SIan Rogers /* Read timeout, 0 implies no timeout. */
29f4c0d530SIan Rogers int timeout_ms;
30e95770afSIan Rogers /* Set true on when the end of file on read error. */
31e95770afSIan Rogers bool eof;
32e95770afSIan Rogers };
33e95770afSIan Rogers
io__init(struct io * io,int fd,char * buf,unsigned int buf_len)34e95770afSIan Rogers static inline void io__init(struct io *io, int fd,
35e95770afSIan Rogers char *buf, unsigned int buf_len)
36e95770afSIan Rogers {
37e95770afSIan Rogers io->fd = fd;
38e95770afSIan Rogers io->buf_len = buf_len;
39e95770afSIan Rogers io->buf = buf;
40e95770afSIan Rogers io->end = buf;
41e95770afSIan Rogers io->data = buf;
42f4c0d530SIan Rogers io->timeout_ms = 0;
43e95770afSIan Rogers io->eof = false;
44e95770afSIan Rogers }
45e95770afSIan Rogers
46*d163d602SIan Rogers /* Read from fd filling the buffer. Called when io->data == io->end. */
io__fill_buffer(struct io * io)47*d163d602SIan Rogers static inline int io__fill_buffer(struct io *io)
48e95770afSIan Rogers {
49*d163d602SIan Rogers ssize_t n;
50e95770afSIan Rogers
51e95770afSIan Rogers if (io->eof)
52e95770afSIan Rogers return -1;
53e95770afSIan Rogers
54f4c0d530SIan Rogers if (io->timeout_ms != 0) {
55f4c0d530SIan Rogers struct pollfd pfds[] = {
56f4c0d530SIan Rogers {
57f4c0d530SIan Rogers .fd = io->fd,
58f4c0d530SIan Rogers .events = POLLIN,
59f4c0d530SIan Rogers },
60f4c0d530SIan Rogers };
61f4c0d530SIan Rogers
62f4c0d530SIan Rogers n = poll(pfds, 1, io->timeout_ms);
63f4c0d530SIan Rogers if (n == 0)
64f4c0d530SIan Rogers errno = ETIMEDOUT;
65f4c0d530SIan Rogers if (n > 0 && !(pfds[0].revents & POLLIN)) {
66f4c0d530SIan Rogers errno = EIO;
67f4c0d530SIan Rogers n = -1;
68f4c0d530SIan Rogers }
69f4c0d530SIan Rogers if (n <= 0) {
70f4c0d530SIan Rogers io->eof = true;
71f4c0d530SIan Rogers return -1;
72f4c0d530SIan Rogers }
73f4c0d530SIan Rogers }
74f4c0d530SIan Rogers n = read(io->fd, io->buf, io->buf_len);
75e95770afSIan Rogers
76e95770afSIan Rogers if (n <= 0) {
77e95770afSIan Rogers io->eof = true;
78e95770afSIan Rogers return -1;
79e95770afSIan Rogers }
80*d163d602SIan Rogers io->data = &io->buf[0];
81e95770afSIan Rogers io->end = &io->buf[n];
82*d163d602SIan Rogers return 0;
83e95770afSIan Rogers }
84*d163d602SIan Rogers
85*d163d602SIan Rogers /* Reads one character from the "io" file with similar semantics to fgetc. */
io__get_char(struct io * io)86*d163d602SIan Rogers static inline int io__get_char(struct io *io)
87*d163d602SIan Rogers {
88*d163d602SIan Rogers if (io->data == io->end) {
89*d163d602SIan Rogers int ret = io__fill_buffer(io);
90*d163d602SIan Rogers
91*d163d602SIan Rogers if (ret)
92*d163d602SIan Rogers return ret;
93*d163d602SIan Rogers }
94*d163d602SIan Rogers return *io->data++;
95e95770afSIan Rogers }
96e95770afSIan Rogers
97e95770afSIan Rogers /* Read a hexadecimal value with no 0x prefix into the out argument hex. If the
98e95770afSIan Rogers * first character isn't hexadecimal returns -2, io->eof returns -1, otherwise
99e95770afSIan Rogers * returns the character after the hexadecimal value which may be -1 for eof.
100e95770afSIan Rogers * If the read value is larger than a u64 the high-order bits will be dropped.
101e95770afSIan Rogers */
io__get_hex(struct io * io,__u64 * hex)102e95770afSIan Rogers static inline int io__get_hex(struct io *io, __u64 *hex)
103e95770afSIan Rogers {
104e95770afSIan Rogers bool first_read = true;
105e95770afSIan Rogers
106e95770afSIan Rogers *hex = 0;
107e95770afSIan Rogers while (true) {
108e95770afSIan Rogers int ch = io__get_char(io);
109e95770afSIan Rogers
110e95770afSIan Rogers if (ch < 0)
111e95770afSIan Rogers return ch;
112e95770afSIan Rogers if (ch >= '0' && ch <= '9')
113e95770afSIan Rogers *hex = (*hex << 4) | (ch - '0');
114e95770afSIan Rogers else if (ch >= 'a' && ch <= 'f')
115e95770afSIan Rogers *hex = (*hex << 4) | (ch - 'a' + 10);
116e95770afSIan Rogers else if (ch >= 'A' && ch <= 'F')
117e95770afSIan Rogers *hex = (*hex << 4) | (ch - 'A' + 10);
118e95770afSIan Rogers else if (first_read)
119e95770afSIan Rogers return -2;
120e95770afSIan Rogers else
121e95770afSIan Rogers return ch;
122e95770afSIan Rogers first_read = false;
123e95770afSIan Rogers }
124e95770afSIan Rogers }
125e95770afSIan Rogers
126e95770afSIan Rogers /* Read a positive decimal value with out argument dec. If the first character
127e95770afSIan Rogers * isn't a decimal returns -2, io->eof returns -1, otherwise returns the
128e95770afSIan Rogers * character after the decimal value which may be -1 for eof. If the read value
129e95770afSIan Rogers * is larger than a u64 the high-order bits will be dropped.
130e95770afSIan Rogers */
io__get_dec(struct io * io,__u64 * dec)131e95770afSIan Rogers static inline int io__get_dec(struct io *io, __u64 *dec)
132e95770afSIan Rogers {
133e95770afSIan Rogers bool first_read = true;
134e95770afSIan Rogers
135e95770afSIan Rogers *dec = 0;
136e95770afSIan Rogers while (true) {
137e95770afSIan Rogers int ch = io__get_char(io);
138e95770afSIan Rogers
139e95770afSIan Rogers if (ch < 0)
140e95770afSIan Rogers return ch;
141e95770afSIan Rogers if (ch >= '0' && ch <= '9')
142e95770afSIan Rogers *dec = (*dec * 10) + ch - '0';
143e95770afSIan Rogers else if (first_read)
144e95770afSIan Rogers return -2;
145e95770afSIan Rogers else
146e95770afSIan Rogers return ch;
147e95770afSIan Rogers first_read = false;
148e95770afSIan Rogers }
149e95770afSIan Rogers }
150e95770afSIan Rogers
151b6a15269SIan Rogers /* Read up to and including the first delim. */
io__getdelim(struct io * io,char ** line_out,size_t * line_len_out,int delim)152b6a15269SIan Rogers static inline ssize_t io__getdelim(struct io *io, char **line_out, size_t *line_len_out, int delim)
153c9dc580cSIan Rogers {
154c9dc580cSIan Rogers char buf[128];
155c9dc580cSIan Rogers int buf_pos = 0;
156c9dc580cSIan Rogers char *line = NULL, *temp;
157c9dc580cSIan Rogers size_t line_len = 0;
158c9dc580cSIan Rogers int ch = 0;
159c9dc580cSIan Rogers
160c9dc580cSIan Rogers /* TODO: reuse previously allocated memory. */
161c9dc580cSIan Rogers free(*line_out);
162b6a15269SIan Rogers while (ch != delim) {
163c9dc580cSIan Rogers ch = io__get_char(io);
164c9dc580cSIan Rogers
165c9dc580cSIan Rogers if (ch < 0)
166c9dc580cSIan Rogers break;
167c9dc580cSIan Rogers
168c9dc580cSIan Rogers if (buf_pos == sizeof(buf)) {
169c9dc580cSIan Rogers temp = realloc(line, line_len + sizeof(buf));
170c9dc580cSIan Rogers if (!temp)
171c9dc580cSIan Rogers goto err_out;
172c9dc580cSIan Rogers line = temp;
173c9dc580cSIan Rogers memcpy(&line[line_len], buf, sizeof(buf));
174c9dc580cSIan Rogers line_len += sizeof(buf);
175c9dc580cSIan Rogers buf_pos = 0;
176c9dc580cSIan Rogers }
177c9dc580cSIan Rogers buf[buf_pos++] = (char)ch;
178c9dc580cSIan Rogers }
179c9dc580cSIan Rogers temp = realloc(line, line_len + buf_pos + 1);
180c9dc580cSIan Rogers if (!temp)
181c9dc580cSIan Rogers goto err_out;
182c9dc580cSIan Rogers line = temp;
183c9dc580cSIan Rogers memcpy(&line[line_len], buf, buf_pos);
184c9dc580cSIan Rogers line[line_len + buf_pos] = '\0';
185c9dc580cSIan Rogers line_len += buf_pos;
186c9dc580cSIan Rogers *line_out = line;
187c9dc580cSIan Rogers *line_len_out = line_len;
188c9dc580cSIan Rogers return line_len;
189c9dc580cSIan Rogers err_out:
190c9dc580cSIan Rogers free(line);
191c4b5140cSIan Rogers *line_out = NULL;
192c9dc580cSIan Rogers return -ENOMEM;
193c9dc580cSIan Rogers }
194c9dc580cSIan Rogers
io__getline(struct io * io,char ** line_out,size_t * line_len_out)195b6a15269SIan Rogers static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_len_out)
196b6a15269SIan Rogers {
197b6a15269SIan Rogers return io__getdelim(io, line_out, line_len_out, /*delim=*/'\n');
198b6a15269SIan Rogers }
199b6a15269SIan Rogers
200e95770afSIan Rogers #endif /* __API_IO__ */
201