[MPlayer-dev-eng] [PATCH 6/7] stream ftp: Allocate command buffer on-heap

Alexander Strasser eclipse7 at gmx.net
Sun Nov 11 22:01:41 CET 2012


Signed-off-by: Alexander Strasser <eclipse7 at gmx.net>
---
 stream/stream_ftp.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/stream/stream_ftp.c b/stream/stream_ftp.c
index 0949bfe..edf39f5 100644
--- a/stream/stream_ftp.c
+++ b/stream/stream_ftp.c
@@ -51,6 +51,7 @@ static struct stream_priv_s {
   int handle;
   int cavail,cleft;
   char *buf;
+  char *cmd_buf;
 } stream_priv_dflts = {
   "anonymous","no at spam",
   NULL,
@@ -61,9 +62,12 @@ static struct stream_priv_s {
 
   0,
   0,0,
-  NULL
+  NULL,
+  NULL,
 };
 
+#define CMD_BUFSIZE 256
+
 #define BUFSIZE 2048
 
 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
@@ -290,7 +294,7 @@ static int FtpOpenPort(struct stream_priv_s* p) {
 static int FtpOpenData(stream_t* s,off_t newpos) {
   struct stream_priv_s* p = s->priv;
   int resp;
-  char str[256],rsp_txt[256];
+  char rsp_txt[256];
 
   // Open a new connection
   s->fd = FtpOpenPort(p);
@@ -298,21 +302,21 @@ static int FtpOpenData(stream_t* s,off_t newpos) {
   if(s->fd < 0) return 0;
 
   if(newpos > 0) {
-    snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
+    snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"REST %"PRId64, (int64_t)newpos);
 
-    resp = FtpSendCmd(str,p,rsp_txt);
+    resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
     if(resp != 3) {
-      mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
+      mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
       newpos = 0;
     }
   }
 
   // Get the file
-  snprintf(str,255,"RETR %s",p->filename);
-  resp = FtpSendCmd(str,p,rsp_txt);
+  snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"RETR %s",p->filename);
+  resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
 
   if(resp != 1) {
-    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
+    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
     return 0;
   }
 
@@ -407,6 +411,7 @@ static void close_f(stream_t *s) {
   }
 
   free(p->buf);
+  free(p->cmd_buf);
 
   m_struct_free(&stream_opts,p);
 }
@@ -417,7 +422,7 @@ static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
   int resp;
   int64_t len = 0;
   struct stream_priv_s* p = (struct stream_priv_s*)opts;
-  char str[256],rsp_txt[256];
+  char rsp_txt[256];
 
   if(mode != STREAM_READ) {
     mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode);
@@ -433,8 +438,9 @@ static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
 
   // Allocate buffers
   p->buf = malloc(BUFSIZE);
+  p->cmd_buf = malloc(CMD_BUFSIZE);
 
-  if (!p->buf) {
+  if (!p->buf || !p->cmd_buf) {
     close_f(stream);
     m_struct_free(&stream_opts,opts);
     return STREAM_ERROR;
@@ -459,20 +465,20 @@ static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
   }
 
   // Login
-  snprintf(str,255,"USER %s",p->user);
-  resp = FtpSendCmd(str,p,rsp_txt);
+  snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"USER %s",p->user);
+  resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
 
   // password needed
   if(resp == 3) {
-    snprintf(str,255,"PASS %s",p->pass);
-    resp = FtpSendCmd(str,p,rsp_txt);
+    snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"PASS %s",p->pass);
+    resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
     if(resp != 2) {
-      mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
+      mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
       close_f(stream);
       return STREAM_ERROR;
     }
   } else if(resp != 2) {
-    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
+    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
     close_f(stream);
     return STREAM_ERROR;
   }
@@ -486,10 +492,10 @@ static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
   }
 
   // Get the filesize
-  snprintf(str,255,"SIZE %s",p->filename);
-  resp = FtpSendCmd(str,p,rsp_txt);
+  snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"SIZE %s",p->filename);
+  resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
   if(resp != 2) {
-    mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
+    mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
   } else {
     int dummy;
     sscanf(rsp_txt,"%d %"SCNd64,&dummy,&len);
-- 
1.7.10.2.552.gaa3bb87


More information about the MPlayer-dev-eng mailing list