1/*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * ascii.c
6 *
7 * Routines to handle ASCII mode tranfers. Yuk.
8 */
9
10#include "ascii.h"
11
12struct ascii_to_bin_ret
13vsf_ascii_ascii_to_bin(char* p_buf, unsigned int in_len, int prev_cr)
14{
15  /* Task: translate all \r\n into plain \n. A plain \r not followed by \n must
16   * not be removed.
17   */
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'))
24  {
25    p_buf[0] = '\r';
26    ret.p_buf = p_buf;
27    written++;
28  }
29  else
30  {
31    ret.p_buf = p_out;
32  }
33  while (indexx < in_len)
34  {
35    char the_char = p_buf[indexx + 1];
36    if (the_char != '\r')
37    {
38      *p_out++ = the_char;
39      written++;
40    }
41    else if (indexx == in_len - 1)
42    {
43      ret.last_was_cr = 1;
44    }
45    else if (p_buf[indexx + 2] != '\n')
46    {
47      *p_out++ = the_char;
48      written++;
49    }
50    indexx++;
51  }
52  ret.stored = written;
53  return ret;
54}
55
56unsigned int
57vsf_ascii_bin_to_ascii(const char* p_in, char* p_out, unsigned int in_len)
58{
59  /* Task: translate all \n into \r\n. Note that \r\n becomes \r\r\n. That's
60   * what wu-ftpd does, and it's easier :-)
61   */
62  unsigned int indexx = 0;
63  unsigned int written = 0;
64  while (indexx < in_len)
65  {
66    char the_char = p_in[indexx];
67    if (the_char == '\n')
68    {
69      *p_out++ = '\r';
70      written++;
71    }
72    *p_out++ = the_char;
73    written++;
74    indexx++;
75  }
76  return written;
77}
78
79