File: ascii.c

package info (click to toggle)
vsftpd 3.0.3-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,548 kB
  • sloc: ansic: 16,632; sh: 267; makefile: 51; python: 18
file content (99 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Part of Very Secure FTPd
 * Licence: GPL v2
 * Author: Chris Evans
 * ascii.c
 *
 * Routines to handle ASCII mode tranfers. Yuk.
 */

#include "ascii.h"

struct ascii_to_bin_ret
vsf_ascii_ascii_to_bin(char* p_buf, unsigned int in_len, int prev_cr)
{
  /* Task: translate all \r\n into plain \n. A plain \r not followed by \n must
   * not be removed.
   */
  struct ascii_to_bin_ret ret;
  unsigned int indexx = 0;
  unsigned int written = 0;
  char* p_out = p_buf + 1;
  ret.last_was_cr = 0;
  if (prev_cr && (!in_len || p_out[0] != '\n'))
  {
    p_buf[0] = '\r';
    ret.p_buf = p_buf;
    written++;
  }
  else
  {
    ret.p_buf = p_out;
  }
  while (indexx < in_len)
  {
    char the_char = p_buf[indexx + 1];
    if (the_char != '\r')
    {
      *p_out++ = the_char;
      written++;
    }
    else if (indexx == in_len - 1)
    {
      ret.last_was_cr = 1;
    }
    else if (p_buf[indexx + 2] != '\n')
    {
      *p_out++ = the_char;
      written++;
    }
    indexx++;
  }
  ret.stored = written;
  return ret;
}

struct bin_to_ascii_ret
vsf_ascii_bin_to_ascii(const char* p_in,
                       char* p_out,
                       unsigned int in_len,
                       int prev_cr)
{
  /* Task: translate all \n not preceeded by \r into \r\n.
   * Note that \r\n stays as \r\n. We used to map it to \r\r\n like wu-ftpd
   * but have switched to leaving it, like the more popular proftpd.
   */
  struct bin_to_ascii_ret ret = { 0, 0 };
  unsigned int indexx = 0;
  unsigned int written = 0;
  char last_char = 0;
  if (prev_cr)
  {
    last_char = '\r';
    ret.last_was_cr = 1;
  }
  while (indexx < in_len)
  {
    char the_char = p_in[indexx];
    if (the_char == '\n' && last_char != '\r')
    {
      *p_out++ = '\r';
      written++;
    }
    *p_out++ = the_char;
    written++;
    indexx++;
    last_char = the_char;
    if (the_char == '\r')
    {
      ret.last_was_cr = 1;
    }
    else
    {
      ret.last_was_cr = 0;
    }
  }
  ret.stored = written;
  return ret;
}