[xiph-commits] r10589 - in trunk/ezstream: . conf src

oddsock at svn.xiph.org oddsock at svn.xiph.org
Wed Dec 14 13:13:27 PST 2005


Author: oddsock
Date: 2005-12-14 13:13:25 -0800 (Wed, 14 Dec 2005)
New Revision: 10589

Modified:
   trunk/ezstream/conf/Makefile.am
   trunk/ezstream/configure.in
   trunk/ezstream/src/ezstream.c
Log:
added some reconnect logic
fixed a buffer overflow on large id3tags
use pclose for popen'd file handles


Modified: trunk/ezstream/conf/Makefile.am
===================================================================
--- trunk/ezstream/conf/Makefile.am	2005-12-14 13:15:48 UTC (rev 10588)
+++ trunk/ezstream/conf/Makefile.am	2005-12-14 21:13:25 UTC (rev 10589)
@@ -2,4 +2,4 @@
 
 AUTOMAKE_OPTIONS = foreign
 
-EXTRA_DIST = ezstream_m3u.xml ezstream_mp3.xml ezstream_vorbis.xml ezstream_reencoding_example.xml
+EXTRA_DIST = ezstream_mp3.xml ezstream_reencoding_example_mp3.xml ezstream_reencoding_example_theora.xml ezstream_reencoding_example_vorbis.xml ezstream_vorbis.xml

Modified: trunk/ezstream/configure.in
===================================================================
--- trunk/ezstream/configure.in	2005-12-14 13:15:48 UTC (rev 10588)
+++ trunk/ezstream/configure.in	2005-12-14 21:13:25 UTC (rev 10589)
@@ -1,4 +1,4 @@
-AC_INIT([ezstream], [0.2.0], [oddsock at xiph.org])
+AC_INIT([ezstream], [0.2.1], [oddsock at xiph.org])
 
 AC_PREREQ(2.54)
 AC_CONFIG_SRCDIR(src/ezstream.c)

Modified: trunk/ezstream/src/ezstream.c
===================================================================
--- trunk/ezstream/src/ezstream.c	2005-12-14 13:15:48 UTC (rev 10588)
+++ trunk/ezstream/src/ezstream.c	2005-12-14 21:13:25 UTC (rev 10589)
@@ -5,14 +5,15 @@
 #include <string.h>
 #ifdef WIN32
 #include <fcntl.h>
-#include <io.h>
-#include <windows.h>
+#include <io.h>
+#include <windows.h>
 #endif
 #include <shout/shout.h>
 #include <getopt.h>
 #include "configfile.h"
-#ifndef WIN32
-#include <libgen.h>
+#ifndef WIN32
+#include <libgen.h>
+#include <unistd.h>
 #endif
 #include <vorbis/vorbisfile.h>
 
@@ -31,7 +32,9 @@
 #endif
 #ifdef WIN32
 #define STRNCASECMP strnicmp
-#define popen _popen
+#define popen _popen
+#define pclose _pclose
+#define snprintf _snprintf
 #else
 #define STRNCASECMP strncasecmp
 #endif
@@ -181,15 +184,15 @@
 	printf("Going to execute (%s)\n", commandString);
 	return(commandString);
 }
-
-#ifdef WIN32
-char *basename(char *fileName) {
-	char *pLast = strrchr(fileName, '\\');
-	if (pLast) {
-		return pLast+1;
-	}
-	return NULL;
-}
+
+#ifdef WIN32
+char *basename(char *fileName) {
+	char *pLast = strrchr(fileName, '\\');
+	if (pLast) {
+		return pLast+1;
+	}
+	return NULL;
+}
 #endif
 char * processMetadata(shout_t *shout, char *extension, char *fileName) {
 	FILE	*filepstream = NULL;
@@ -198,6 +201,8 @@
 	char	*songInfo = NULL;
 	int songLen = 0;
 	ID3Tag	id3tag;
+	char 	temptrackName[31];
+	char 	tempartistName[31];
 
 	filepstream = fopen(fileName, "rb");
 	if (filepstream == NULL) {
@@ -213,11 +218,16 @@
 			fread(&id3tag, 1, 127, filepstream);
 			if (!strncmp(id3tag.tag, "TAG", strlen("TAG"))) {
 				/* We have an Id3 tag */
-				songLen = strlen(id3tag.artistName) + strlen(" - ") + strlen(id3tag.trackName);
+				memset(temptrackName, '\000', sizeof(temptrackName));
+				memset(tempartistName, '\000', sizeof(tempartistName));
+				snprintf(temptrackName, sizeof(temptrackName)-1, "%s", id3tag.trackName);
+				snprintf(tempartistName, sizeof(tempartistName)-1, "%s", id3tag.artistName);
+
+				songLen = sizeof(tempartistName) + strlen(" - ") + sizeof(temptrackName) + 1;
 				songInfo = (char *)malloc(songLen);
 				memset(songInfo, '\000', songLen);
 
-				sprintf(songInfo, "%s - %s", id3tag.artistName, id3tag.trackName);
+				snprintf(songInfo, songLen-1, "%s - %s", tempartistName, temptrackName);
 			}
 		}
 	}
@@ -263,14 +273,14 @@
 	if (!songInfo) {
 		/* If we didn't get any song info via tags or comments,
 		   then lets just use the filename */
-		char *p1 = NULL;
-		char *p2 = basename(fileName);
+		char *p1 = NULL;
+		char *p2 = basename(fileName);
 		if (p2) {
 			songInfo = strdup(p2);
 			p1 = strrchr(songInfo, '.');
 			if (p1) {
 				*p1 = '\000';
-			}
+			}
 		}
 	}
 
@@ -290,11 +300,11 @@
 	return songInfo;
 }
 
-FILE *openResource(shout_t *shout, char *fileName)
+FILE *openResource(shout_t *shout, char *fileName, int *popenFlag)
 {
 	FILE	*filep = NULL;
-
-    printf("Opening file (%s)\n", fileName);
+
+    printf("Opening file (%s)\n", fileName);
 	if (!strcmp(fileName, "stdin")) {
 #ifdef WIN32
 		_setmode(_fileno(stdin), _O_BINARY);
@@ -314,15 +324,17 @@
 		}
 
 		pMetadata = processMetadata(shout, extension, fileName);
+		*popenFlag = 0;
 		if (pezConfig->reencode) {
 			/* Lets set the metadata first */
 			if (strlen(extension) > 0) {
 				pCommandString = buildCommandString(extension, fileName, pMetadata);	
 				/* Open up the decode/encode loop using popen() */
-				filep = popen(pCommandString, "r");
-#ifdef WIN32
-                _setmode(_fileno(filep), _O_BINARY );
-#endif
+				filep = popen(pCommandString, "r");
+				*popenFlag = 1;
+#ifdef WIN32
+                _setmode(_fileno(filep), _O_BINARY );
+#endif
 				free(pMetadata);
 				free(pCommandString);
 				return filep;
@@ -348,12 +360,13 @@
 int streamFile(shout_t *shout, char *fileName) {
 	FILE	*filepstream = NULL;
 	char buff[4096];
-	long read, ret, total;
+	long read, ret = 0, total;
+	int	popenFlag = 0;
 	
 	
 	printf("Streaming %s\n", fileName);
 	
-	filepstream = openResource(shout, fileName);
+	filepstream = openResource(shout, fileName, &popenFlag);
 	if (!filepstream) {
 		printf("Cannot open %s\n", fileName);
 		return 0;
@@ -365,27 +378,27 @@
 
 		if (read > 0) {
 			ret = shout_send(shout, buff, read);
-			if (ret != SHOUTERR_SUCCESS) {
+			if (ret != SHOUTERR_SUCCESS) {
                 int loop = 1;
-				printf("DEBUG: Send error: %s\n", shout_get_error(shout));
-                
-                while (loop) {
-                    printf("Disconnected from server, reconnecting....\n");                    
-                    shout_close(shout);
-                    if (shout_open(shout) == SHOUTERR_SUCCESS) {
-                        printf("Successful reconnection....\n");                    
-            			ret = shout_send(shout, buff, read);
-                        loop = 0;
-                    }
-                    else {
-                        printf("Reconnect failed..waiting 5 seconds.\n");                    
-#ifdef WIN32
-                        Sleep(5000);
-#else
-                        sleep(5);
-#endif
-                    }
-                }
+				printf("DEBUG: Send error: %s\n", shout_get_error(shout));
+                
+                while (loop) {
+                    printf("Disconnected from server, reconnecting....\n");                    
+                    shout_close(shout);
+                    if (shout_open(shout) == SHOUTERR_SUCCESS) {
+                        printf("Successful reconnection....\n");                    
+            			ret = shout_send(shout, buff, read);
+                        loop = 0;
+                    }
+                    else {
+                        printf("Reconnect failed..waiting 5 seconds.\n");                    
+#ifdef WIN32
+                        Sleep(5000);
+#else
+                        sleep(5);
+#endif
+                    }
+                }
 			}
 			shout_delay(shout);
 		} else {
@@ -394,7 +407,13 @@
 
 		shout_sync(shout);
 	}
-	fclose(filepstream);
+	if (popenFlag) {
+		printf("Closing via pclose\n");
+		pclose(filepstream);
+	}
+	else {
+		fclose(filepstream);
+	}
 	filepstream = NULL;
 	return ret;
 }
@@ -402,8 +421,7 @@
 	FILE	*filep = NULL;
 	char	streamFileName[8096] = "";
 	char	lastStreamFileName[8096] = "";
-	int		loop = 1;
-    int     ret = 1;
+	int		loop = 1;
 
 	filep = fopen(fileName, "r");
 	if (filep == 0) {
@@ -420,7 +438,7 @@
 				strcpy(lastStreamFileName, streamFileName);
 				/* Skip entries that begin with a # */
 				if (strncmp(streamFileName, "#", 1)) {
-					streamFile(shout, streamFileName);
+					streamFile(shout, streamFileName);
 				}
 			}
 			if (rereadPlaylist) {



More information about the commits mailing list