vsftpd 3.0.2
[tomato/tomato-dir865l.git] / release / src / router / vsftpd / netstr.c
blob69952f4d32b20799c304e057e3d57601cc09c2d9
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 str_empty(p_str);
33 while (1)
35 if (p_readpos + left != p_readbuf + maxlen)
37 bug("poor buffer accounting in str_netfd_alloc");
39 /* Did we hit the max? */
40 if (left == 0)
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 return 0;
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 i++;
61 retval = (*p_readfunc)(p_sess, p_readpos, i);
62 if (vsf_sysutil_retval_is_error(retval) ||
63 (unsigned int) retval != i)
65 die("vsf_sysutil_read_loop");
67 if (p_readpos[i - 1] != term)
69 die("missing terminator in str_netfd_alloc");
71 str_alloc_alt_term(p_str, p_readbuf, term);
72 return (int) i;
75 /* Not found in this read chunk, so consume the data and re-loop */
76 if (bytes_read > left)
78 bug("bytes_read > left in str_netfd_alloc");
80 left -= bytes_read;
81 retval = (*p_readfunc)(p_sess, p_readpos, bytes_read);
82 if (vsf_sysutil_retval_is_error(retval) ||
83 (unsigned int) retval != bytes_read)
85 die("vsf_sysutil_read_loop");
87 p_readpos += bytes_read;
88 } /* END: while(1) */
91 int
92 str_netfd_write(const struct mystr* p_str, int fd)
94 int ret = 0;
95 int retval;
96 unsigned int str_len = str_getlen(p_str);
97 if (str_len == 0)
99 bug("zero str_len in str_netfd_write");
101 retval = str_write_loop(p_str, fd);
102 if (vsf_sysutil_retval_is_error(retval) || (unsigned int) retval != str_len)
104 ret = -1;
106 return ret;
110 str_netfd_read(struct mystr* p_str, int fd, unsigned int len)
112 int retval;
113 str_reserve(p_str, len);
114 str_trunc(p_str, len);
115 retval = str_read_loop(p_str, fd);
116 if (vsf_sysutil_retval_is_error(retval) || (unsigned int) retval != len)
118 return -1;
120 return retval;