xref: /OK3568_Linux_fs/kernel/tools/lib/api/io.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Lightweight buffered reading library.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2019 Google LLC.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #ifndef __API_IO__
8*4882a593Smuzhiyun #define __API_IO__
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <stdlib.h>
11*4882a593Smuzhiyun #include <unistd.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun struct io {
14*4882a593Smuzhiyun 	/* File descriptor being read/ */
15*4882a593Smuzhiyun 	int fd;
16*4882a593Smuzhiyun 	/* Size of the read buffer. */
17*4882a593Smuzhiyun 	unsigned int buf_len;
18*4882a593Smuzhiyun 	/* Pointer to storage for buffering read. */
19*4882a593Smuzhiyun 	char *buf;
20*4882a593Smuzhiyun 	/* End of the storage. */
21*4882a593Smuzhiyun 	char *end;
22*4882a593Smuzhiyun 	/* Currently accessed data pointer. */
23*4882a593Smuzhiyun 	char *data;
24*4882a593Smuzhiyun 	/* Set true on when the end of file on read error. */
25*4882a593Smuzhiyun 	bool eof;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
io__init(struct io * io,int fd,char * buf,unsigned int buf_len)28*4882a593Smuzhiyun static inline void io__init(struct io *io, int fd,
29*4882a593Smuzhiyun 			    char *buf, unsigned int buf_len)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	io->fd = fd;
32*4882a593Smuzhiyun 	io->buf_len = buf_len;
33*4882a593Smuzhiyun 	io->buf = buf;
34*4882a593Smuzhiyun 	io->end = buf;
35*4882a593Smuzhiyun 	io->data = buf;
36*4882a593Smuzhiyun 	io->eof = false;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* Reads one character from the "io" file with similar semantics to fgetc. */
io__get_char(struct io * io)40*4882a593Smuzhiyun static inline int io__get_char(struct io *io)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	char *ptr = io->data;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	if (io->eof)
45*4882a593Smuzhiyun 		return -1;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (ptr == io->end) {
48*4882a593Smuzhiyun 		ssize_t n = read(io->fd, io->buf, io->buf_len);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 		if (n <= 0) {
51*4882a593Smuzhiyun 			io->eof = true;
52*4882a593Smuzhiyun 			return -1;
53*4882a593Smuzhiyun 		}
54*4882a593Smuzhiyun 		ptr = &io->buf[0];
55*4882a593Smuzhiyun 		io->end = &io->buf[n];
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 	io->data = ptr + 1;
58*4882a593Smuzhiyun 	return *ptr;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Read a hexadecimal value with no 0x prefix into the out argument hex. If the
62*4882a593Smuzhiyun  * first character isn't hexadecimal returns -2, io->eof returns -1, otherwise
63*4882a593Smuzhiyun  * returns the character after the hexadecimal value which may be -1 for eof.
64*4882a593Smuzhiyun  * If the read value is larger than a u64 the high-order bits will be dropped.
65*4882a593Smuzhiyun  */
io__get_hex(struct io * io,__u64 * hex)66*4882a593Smuzhiyun static inline int io__get_hex(struct io *io, __u64 *hex)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	bool first_read = true;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	*hex = 0;
71*4882a593Smuzhiyun 	while (true) {
72*4882a593Smuzhiyun 		int ch = io__get_char(io);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		if (ch < 0)
75*4882a593Smuzhiyun 			return ch;
76*4882a593Smuzhiyun 		if (ch >= '0' && ch <= '9')
77*4882a593Smuzhiyun 			*hex = (*hex << 4) | (ch - '0');
78*4882a593Smuzhiyun 		else if (ch >= 'a' && ch <= 'f')
79*4882a593Smuzhiyun 			*hex = (*hex << 4) | (ch - 'a' + 10);
80*4882a593Smuzhiyun 		else if (ch >= 'A' && ch <= 'F')
81*4882a593Smuzhiyun 			*hex = (*hex << 4) | (ch - 'A' + 10);
82*4882a593Smuzhiyun 		else if (first_read)
83*4882a593Smuzhiyun 			return -2;
84*4882a593Smuzhiyun 		else
85*4882a593Smuzhiyun 			return ch;
86*4882a593Smuzhiyun 		first_read = false;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* Read a positive decimal value with out argument dec. If the first character
91*4882a593Smuzhiyun  * isn't a decimal returns -2, io->eof returns -1, otherwise returns the
92*4882a593Smuzhiyun  * character after the decimal value which may be -1 for eof. If the read value
93*4882a593Smuzhiyun  * is larger than a u64 the high-order bits will be dropped.
94*4882a593Smuzhiyun  */
io__get_dec(struct io * io,__u64 * dec)95*4882a593Smuzhiyun static inline int io__get_dec(struct io *io, __u64 *dec)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	bool first_read = true;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	*dec = 0;
100*4882a593Smuzhiyun 	while (true) {
101*4882a593Smuzhiyun 		int ch = io__get_char(io);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		if (ch < 0)
104*4882a593Smuzhiyun 			return ch;
105*4882a593Smuzhiyun 		if (ch >= '0' && ch <= '9')
106*4882a593Smuzhiyun 			*dec = (*dec * 10) + ch - '0';
107*4882a593Smuzhiyun 		else if (first_read)
108*4882a593Smuzhiyun 			return -2;
109*4882a593Smuzhiyun 		else
110*4882a593Smuzhiyun 			return ch;
111*4882a593Smuzhiyun 		first_read = false;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #endif /* __API_IO__ */
116