- Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathio.h
128 lines (107 loc) · 4.03 KB
/
io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* $Id$ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndefIO_H
#defineIO_H
/* Buffer macros. */
#defineBUFFER_USED(b) ((b)->size)
#defineBUFFER_FREE(b) ((b)->space - (b)->off - (b)->size)
#defineBUFFER_IN(b) ((b)->base + (b)->off + (b)->size)
#defineBUFFER_OUT(b) ((b)->base + (b)->off)
/* Buffer structure. */
structbuffer {
u_char*base; /* buffer start */
size_tspace; /* total size of buffer */
size_tsize; /* size of data in buffer */
size_toff; /* offset of data in buffer */
};
/* Limits at which to fail. */
#defineIO_MAXLINELEN (16 * 1024 * 1024) /* 16 MB */
/* IO line endings. */
#defineIO_CRLF "\r\n"
#defineIO_CR "\r"
#defineIO_LF "\n"
/* Initial block size of buffer and minimum amount to try to read. */
#defineIO_BLOCKSIZE 16384
#defineIO_WATERMARK 12288
/* Initial line buffer length. */
#defineIO_LINESIZE 256
/* Amount to poll after in io_update. */
#defineIO_FLUSHSIZE (2 * IO_BLOCKSIZE)
/* IO macros. */
#defineIO_ROUND(n) (((n / IO_BLOCKSIZE) + 1) * IO_BLOCKSIZE)
#defineIO_CLOSED(io) ((io)->flags & IOF_CLOSED)
#defineIO_ERROR(io) ((io)->error)
#defineIO_RDSIZE(io) (BUFFER_USED((io)->rd))
#defineIO_WRSIZE(io) (BUFFER_USED((io)->wr))
/* IO structure. */
structio {
intfd;
intdup_fd; /* duplicate all data to this fd */
SSL*ssl;
char*error;
intflags;
#defineIOF_NEEDFILL 0x1
#defineIOF_NEEDPUSH 0x2
#defineIOF_CLOSED 0x4
#defineIOF_MUSTWR 0x8
structbuffer*rd;
structbuffer*wr;
char*lbuf; /* line buffer */
size_tllen; /* line buffer size */
constchar*eol;
};
/* List of ios. */
ARRAY_DECL(iolist, structio*);
/* buffer.c */
structbuffer*buffer_create(size_t);
voidbuffer_destroy(structbuffer*);
voidbuffer_clear(structbuffer*);
voidbuffer_ensure(structbuffer*, size_t);
voidbuffer_add(structbuffer*, size_t);
voidbuffer_reverse_add(structbuffer*, size_t);
voidbuffer_remove(structbuffer*, size_t);
voidbuffer_reverse_remove(structbuffer*, size_t);
voidbuffer_insert_range(structbuffer*, size_t, size_t);
voidbuffer_delete_range(structbuffer*, size_t, size_t);
voidbuffer_write(structbuffer*, constvoid*, size_t);
voidbuffer_read(structbuffer*, void*, size_t);
voidbuffer_write8(structbuffer*, uint8_t);
voidbuffer_write16(structbuffer*, uint16_t);
uint8_tbuffer_read8(structbuffer*);
uint16_tbuffer_read16(structbuffer*);
/* io.c */
structio*io_create(int, SSL*, constchar*);
voidio_readonly(structio*);
voidio_writeonly(structio*);
voidio_free(structio*);
voidio_close(structio*);
intio_polln(structio**, u_int, structio**, int, char**);
intio_poll(structio*, int, char**);
intio_read2(structio*, void*, size_t);
void*io_read(structio*, size_t);
voidio_write(structio*, constvoid*, size_t);
char*io_readline2(structio*, char**, size_t*);
char*io_readline(structio*);
voidprintflike2io_writeline(structio*, constchar*, ...);
voidio_vwriteline(structio*, constchar*, va_list);
intio_pollline2(structio*, char**, char**, size_t*, int,
char**);
intio_pollline(structio*, char**, int, char**);
intio_flush(structio*, int, char**);
intio_wait(structio*, size_t, int, char**);
intio_update(structio*, int, char**);
#endif/* IO_H */