libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / vsftpd / ascii.c
blobcf1a7b11a95e4c541ddaa509c0ed45a2b4708122
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * ascii.c
7 * Routines to handle ASCII mode tranfers. Yuk.
8 */
10 #include "ascii.h"
12 struct ascii_to_bin_ret
13 vsf_ascii_ascii_to_bin(char* p_buf, unsigned int in_len, int prev_cr)
15 /* Task: translate all \r\n into plain \n. A plain \r not followed by \n must
16 * not be removed.
18 struct ascii_to_bin_ret ret;
19 unsigned int indexx = 0;
20 unsigned int written = 0;
21 char* p_out = p_buf + 1;
22 ret.last_was_cr = 0;
23 if (prev_cr && (!in_len || p_out[0] != '\n'))
25 p_buf[0] = '\r';
26 ret.p_buf = p_buf;
27 written++;
29 else
31 ret.p_buf = p_out;
33 while (indexx < in_len)
35 char the_char = p_buf[indexx + 1];
36 if (the_char != '\r')
38 *p_out++ = the_char;
39 written++;
41 else if (indexx == in_len - 1)
43 ret.last_was_cr = 1;
45 else if (p_buf[indexx + 2] != '\n')
47 *p_out++ = the_char;
48 written++;
50 indexx++;
52 ret.stored = written;
53 return ret;
56 struct bin_to_ascii_ret
57 vsf_ascii_bin_to_ascii(const char* p_in,
58 char* p_out,
59 unsigned int in_len,
60 int prev_cr)
62 /* Task: translate all \n not preceeded by \r into \r\n.
63 * Note that \r\n stays as \r\n. We used to map it to \r\r\n like wu-ftpd
64 * but have switched to leaving it, like the more popular proftpd.
66 struct bin_to_ascii_ret ret = { 0, 0 };
67 unsigned int indexx = 0;
68 unsigned int written = 0;
69 char last_char = 0;
70 if (prev_cr)
72 last_char = '\r';
73 ret.last_was_cr = 1;
75 while (indexx < in_len)
77 char the_char = p_in[indexx];
78 if (the_char == '\n' && last_char != '\r')
80 *p_out++ = '\r';
81 written++;
83 *p_out++ = the_char;
84 written++;
85 indexx++;
86 last_char = the_char;
87 if (the_char == '\r')
89 ret.last_was_cr = 1;
91 else
93 ret.last_was_cr = 0;
96 ret.stored = written;
97 return ret;