Browse code

lavf/ftp: make response parsing more RFC compliant

Signed-off-by: Lukasz Marek <lukasz.m.luki2@gmail.com>

Lukasz Marek authored on 2014/07/04 04:08:23
Showing 1 changed files
... ...
@@ -119,7 +119,7 @@ static int ftp_get_line(FTPContext *s, char *line, int line_size)
119 119
  */
120 120
 static int ftp_status(FTPContext *s, char **line, const int response_codes[])
121 121
 {
122
-    int err, i, dash = 0, result = 0, code_found = 0;
122
+    int err, i, dash = 0, result = 0, code_found = 0, linesize;
123 123
     char buf[CONTROL_BUFFER_SIZE];
124 124
     AVBPrint line_buffer;
125 125
 
... ...
@@ -135,25 +135,36 @@ static int ftp_status(FTPContext *s, char **line, const int response_codes[])
135 135
 
136 136
         av_log(s, AV_LOG_DEBUG, "%s\n", buf);
137 137
 
138
-        if (strlen(buf) < 4)
139
-            continue;
140
-
138
+        linesize = strlen(buf);
141 139
         err = 0;
142
-        for (i = 0; i < 3; ++i) {
143
-            if (buf[i] < '0' || buf[i] > '9')
144
-                continue;
145
-            err *= 10;
146
-            err += buf[i] - '0';
140
+        if (linesize >= 3) {
141
+            for (i = 0; i < 3; ++i) {
142
+                if (buf[i] < '0' || buf[i] > '9') {
143
+                    err = 0;
144
+                    break;
145
+                }
146
+                err *= 10;
147
+                err += buf[i] - '0';
148
+            }
147 149
         }
148
-        dash = !!(buf[3] == '-');
149
-
150
-        for (i = 0; response_codes[i]; ++i) {
151
-            if (err == response_codes[i]) {
152
-                if (line)
153
-                    av_bprintf(&line_buffer, "%s", buf);
154
-                code_found = 1;
155
-                result = err;
156
-                break;
150
+
151
+        if (!code_found) {
152
+            for (i = 0; response_codes[i]; ++i) {
153
+                if (err == response_codes[i]) {
154
+                    code_found = 1;
155
+                    result = err;
156
+                    break;
157
+                }
158
+            }
159
+        }
160
+        if (code_found) {
161
+            if (line)
162
+                av_bprintf(&line_buffer, "%s\r\n", buf);
163
+            if (linesize >= 4) {
164
+                if (!dash && buf[3] == '-')
165
+                    dash = err;
166
+                else if (err == dash && buf[3] == ' ')
167
+                    dash = 0;
157 168
             }
158 169
         }
159 170
     }