usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / vsftpd / netstr.c
blob2e4930ba637ee96c98bf93629e508753548e8522
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * netstr.c
7 * The netstr interface extends the standard string interface, adding
8 * functions which can cope safely with building strings from the network,
9 * and send them out too.
12 #include "netstr.h"
13 #include "str.h"
14 #include "sysstr.h"
15 #include "utility.h"
16 #include "sysutil.h"
18 int
19 str_netfd_alloc(struct vsf_session* p_sess,
20 struct mystr* p_str,
21 char term,
22 char* p_readbuf,
23 unsigned int maxlen,
24 str_netfd_read_t p_peekfunc,
25 str_netfd_read_t p_readfunc)
27 int retval;
28 unsigned int bytes_read;
29 unsigned int i;
30 char* p_readpos = p_readbuf;
31 unsigned int left = maxlen;
32 while (1)
34 if (p_readpos + left != p_readbuf + maxlen)
36 bug("poor buffer accounting in str_netfd_alloc");
38 /* Did we hit the max? */
39 if (left == 0)
41 str_empty(p_str);
42 return -1;
44 retval = (*p_peekfunc)(p_sess, p_readpos, left);
45 if (vsf_sysutil_retval_is_error(retval))
47 die("vsf_sysutil_recv_peek");
49 else if (retval == 0)
51 die("vsf_sysutil_recv_peek: no data");
53 bytes_read = (unsigned int) retval;
54 /* Search for the terminator */
55 for (i=0; i < bytes_read; i++)
57 if (p_readpos[i] == term)
59 /* Got it! */
60 retval = (*p_readfunc)(p_sess, p_readpos, i + 1);
61 if (vsf_sysutil_retval_is_error(retval) ||
62 (unsigned int) retval != i + 1)
64 die("vsf_sysutil_read_loop");
66 if (p_readpos[i] != term)
68 die("missing terminator in str_netfd_alloc");
70 str_alloc_alt_term(p_str, p_readbuf, term);
71 return (int) i;
74 /* Not found in this read chunk, so consume the data and re-loop */
75 if (bytes_read > left)
77 bug("bytes_read > left in str_netfd_alloc");
79 left -= bytes_read;
80 retval = (*p_readfunc)(p_sess, p_readpos, bytes_read);
81 if (vsf_sysutil_retval_is_error(retval) ||
82 (unsigned int) retval != bytes_read)
84 die("vsf_sysutil_read_loop");
86 p_readpos += bytes_read;
87 } /* END: while(1) */
90 int
91 str_netfd_write(const struct mystr* p_str, int fd)
93 int ret = 0;
94 int retval;
95 unsigned int str_len = str_getlen(p_str);
96 if (str_len == 0)
98 bug("zero str_len in str_netfd_write");
100 retval = str_write_loop(p_str, fd);
101 if (vsf_sysutil_retval_is_error(retval) || (unsigned int) retval != str_len)
103 ret = -1;
105 return ret;
109 str_netfd_read(struct mystr* p_str, int fd, unsigned int len)
111 int retval;
112 str_reserve(p_str, len);
113 str_trunc(p_str, len);
114 retval = str_read_loop(p_str, fd);
115 if (vsf_sysutil_retval_is_error(retval) || (unsigned int) retval != len)
117 return -1;
119 return retval;