1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17/*
18 * Part of Very Secure FTPd
19 * Licence: GPL v2
20 * Author: Chris Evans
21 * ascii.c
22 *
23 * Routines to handle ASCII mode tranfers. Yuk.
24 */
25
26#include "ascii.h"
27
28struct ascii_to_bin_ret
29vsf_ascii_ascii_to_bin(char* p_buf, unsigned int in_len, int prev_cr)
30{
31  /* Task: translate all \r\n into plain \n. A plain \r not followed by \n must
32   * not be removed.
33   */
34  struct ascii_to_bin_ret ret;
35  unsigned int indexx = 0;
36  unsigned int written = 0;
37  char* p_out = p_buf + 1;
38  ret.last_was_cr = 0;
39  if (prev_cr && (!in_len || p_out[0] != '\n'))
40  {
41    p_buf[0] = '\r';
42    ret.p_buf = p_buf;
43    written++;
44  }
45  else
46  {
47    ret.p_buf = p_out;
48  }
49  while (indexx < in_len)
50  {
51    char the_char = p_buf[indexx + 1];
52    if (the_char != '\r')
53    {
54      *p_out++ = the_char;
55      written++;
56    }
57    else if (indexx == in_len - 1)
58    {
59      ret.last_was_cr = 1;
60    }
61    else if (p_buf[indexx + 2] != '\n')
62    {
63      *p_out++ = the_char;
64      written++;
65    }
66    indexx++;
67  }
68  ret.stored = written;
69  return ret;
70}
71
72unsigned int
73vsf_ascii_bin_to_ascii(const char* p_in, char* p_out, unsigned int in_len)
74{
75  /* Task: translate all \n into \r\n. Note that \r\n becomes \r\r\n. That's
76   * what wu-ftpd does, and it's easier :-)
77   */
78  unsigned int indexx = 0;
79  unsigned int written = 0;
80  while (indexx < in_len)
81  {
82    char the_char = p_in[indexx];
83    if (the_char == '\n')
84    {
85      *p_out++ = '\r';
86      written++;
87    }
88    *p_out++ = the_char;
89    written++;
90    indexx++;
91  }
92  return written;
93}
94
95