shithub: sox

Download patch

ref: 30c4f1c78e050abff9443e048b95c16060f42063
parent: 560be6db0127f4ad1eb5d63ab5e63245c95fb6c1
author: cbagwell <cbagwell>
date: Thu Mar 1 14:17:15 EST 2001

Minor util code cleanups.

--- a/mix.c
+++ b/mix.c
@@ -336,20 +336,17 @@
 
 void init() {
 
-	/* init files */
-	informat.info.rate      = mixformat.info.rate = outformat.info.rate  = 0;
-	informat.info.size      = mixformat.info.size = outformat.info.size  = -1;
-	informat.info.encoding  = mixformat.info.encoding = outformat.info.encoding = -1;
-	informat.info.channels  = mixformat.info.channels = outformat.info.channels = -1;
-	informat.comment   = mixformat.comment = outformat.comment = NULL;
-	informat.swap      = mixformat.swap = 0;
-	informat.filetype  = mixformat.filetype = outformat.filetype  = (char *) 0;
-	informat.fp        = stdin;
-	mixformat.fp       = NULL;
-	outformat.fp       = stdout;
-	informat.filename  = "input";
-	mixformat.filename  = "mix";
-	outformat.filename = "output";
+    /* init files */
+    st_initformat(&informat);
+    st_initformat(&mixformat);
+    st_initformat(&outformat);
+
+    informat.fp        = stdin;
+    mixformat.fp       = NULL;
+    outformat.fp       = stdout;
+    informat.filename  = "input";
+    mixformat.filename  = "mix";
+    outformat.filename = "output";
 }
 
 /* 
@@ -367,10 +364,12 @@
     
     /* Read and write starters can change their formats. */
     (* informat.h->startread)(&informat);
-    st_checkformat(&informat);
+    if (st_checkformat(&informat))
+	st_fail("bad input format");
     
     (* mixformat.h->startread)(&mixformat);
-    st_checkformat(&mixformat);
+    if (st_checkformat(&mixformat))
+	st_fail("bad input format");
     
     if (dovolume)
 	st_report("Volume factor: %f\n", volume);
@@ -405,7 +404,10 @@
     if (writing) {
 	st_copyformat(&informat, &outformat);
 	(* outformat.h->startwrite)(&outformat);
-	st_checkformat(&outformat);
+
+	if (st_checkformat(&outformat))
+	    st_fail("bad output format");
+
 	st_report("Output file: using sample rate %lu\n\tsize %s, encoding %s, %d %s",
 	       outformat.info.rate, st_sizes_str[outformat.info.size], 
 	       st_encodings_str[outformat.info.encoding], outformat.info.channels, 
--- a/src/handlers.c
+++ b/src/handlers.c
@@ -363,8 +363,10 @@
 extern LONG st_nothing_success();
 
 st_format_t st_formats[] = {
-	{aiffnames, ST_FILE_STEREO | ST_FILE_SEEK,		/* SGI/Apple AIFF */
-		st_aiffstartread, st_aiffread, st_aiffstopread,
+	{aiffnames, 					/* SGI/Apple AIFF */
+
+	    ST_FILE_STEREO | ST_FILE_LOOPS | ST_FILE_SEEK,
+	        st_aiffstartread, st_aiffread, st_aiffstopread,
 		st_aiffstartwrite, st_aiffwrite, st_aiffstopwrite, st_aiffseek},
 	{alnames, ST_FILE_STEREO,		/* a-law byte raw */
 		st_alstartread, st_rawread, st_rawstopread,
--- a/src/sox.c
+++ b/src/sox.c
@@ -392,18 +392,14 @@
 
 static void init(void) {
 
-	/* init files */
-	informat.info.rate      = outformat.info.rate  = 0;
-	informat.info.size      = outformat.info.size  = -1;
-	informat.info.encoding  = outformat.info.encoding = -1;
-	informat.info.channels  = outformat.info.channels = -1;
-	informat.comment   = outformat.comment = NULL;
-	informat.swap      = 0;
-	informat.filetype  = outformat.filetype  = (char *) 0;
-	informat.fp        = stdin;
-	outformat.fp       = stdout;
-	informat.filename  = "input";
-	outformat.filename = "output";
+    /* init files */
+    st_initformat(&informat);
+    st_initformat(&outformat);
+
+    informat.fp        = stdin;
+    outformat.fp       = stdout;
+    informat.filename  = "input";
+    outformat.filename = "output";
 }
 
 /* 
@@ -425,6 +421,7 @@
     {
         st_fail(informat.st_errstr);
     }
+
     if ( st_checkformat(&informat) )
 		st_fail("bad input format");
     
@@ -484,7 +481,7 @@
 	}
 	if (st_checkformat(&outformat))
 		st_fail("bad output format");
-	st_cmpformats(&informat, &outformat);
+
 	st_report("Output file: using sample rate %lu\n\tsize %s, encoding %s, %d %s",
 	       outformat.info.rate, st_sizes_str[outformat.info.size], 
 	       st_encodings_str[outformat.info.encoding], outformat.info.channels, 
--- a/src/st.h
+++ b/src/st.h
@@ -182,6 +182,8 @@
 #define ST_SIZE_DOUBLE	6
 #define ST_SIZE_IEEE	7	/* IEEE 80-bit floats. */
 
+#define ST_SIZE_MAX     7
+
 /* Style field */
 #define ST_ENCODING_UNSIGNED	1 /* unsigned linear: Sound Blaster */
 #define ST_ENCODING_SIGN2	2 /* signed linear 2's comp: Mac */
@@ -191,6 +193,8 @@
 #define ST_ENCODING_IMA_ADPCM	6 /* Compressed PCM */
 #define ST_ENCODING_GSM		7 /* GSM 6.10 33-byte frame lossy compression */
 
+#define ST_ENCODING_MAX         7
+
 /* declared in misc.c */
 extern const char *st_sizes_str[];
 extern const char *st_encodings_str[];
@@ -330,9 +334,9 @@
 int st_geteffect(eff_t, char *);
 int st_updateeffect(eff_t, ft_t, ft_t, int);
 int st_gettype(ft_t);
-int st_checkformat(ft_t);
+void st_initformat(ft_t ft);
 void st_copyformat(ft_t, ft_t);
-void st_cmpformats(ft_t, ft_t);
+int st_checkformat(ft_t);
 double st_parsetime(char *);
 
 /* FIXME: Recording hacks shouldn't display a "sigint" style interface.
--- a/src/util.c
+++ b/src/util.c
@@ -35,8 +35,7 @@
  */
 char *myname = 0;
 
-void
-st_report(const char *fmt, ...) 
+void st_report(const char *fmt, ...) 
 {
 	va_list args;
 
@@ -50,9 +49,7 @@
 	fprintf(stderr, "\n");
 }
 
-
-void
-st_warn(const char *fmt, ...) 
+void st_warn(const char *fmt, ...) 
 {
 	va_list args;
 
@@ -64,8 +61,7 @@
 	fprintf(stderr, "\n");
 }
 
-void
-st_fail(const char *fmt, ...) 
+void st_fail(const char *fmt, ...) 
 {
 	va_list args;
 	extern void cleanup();
@@ -86,8 +82,7 @@
  * Note:  Changing vsprintf to vsnprintf should help that but bad
  * references to strings can still cause overflow.
  */
-void
-st_fail_errno(ft_t ft, int st_errno, const char *fmt, ...)
+void st_fail_errno(ft_t ft, int st_errno, const char *fmt, ...)
 {
 	va_list args;
 
@@ -125,8 +120,7 @@
 	return 0;
 }
 
-int strcmpcase(s1, s2)
-char *s1, *s2;
+int strcmpcase(char *s1, char *s2)
 {
 	while(*s1 && *s2 && (tolower(*s1) == tolower(*s2)))
 		s1++, s2++;
@@ -136,16 +130,16 @@
 /*
  * Check that we have a known format suffix string.
  */
-int
-st_gettype(formp)
-ft_t formp;
+int st_gettype(ft_t formp)
 {
 	char **list;
 	int i;
 
 	if (! formp->filetype){
-st_fail_errno(formp,ST_EFMT,"Must give file type for %s file, either as suffix or with -t option",
-formp->filename);
+            st_fail_errno(formp,
+		          ST_EFMT,
+			  "Must give file type for %s file, either as suffix or with -t option",
+			  formp->filename);
 		return(ST_EFMT);
 	}
 	for(i = 0; st_formats[i].names; i++) {
@@ -331,9 +325,25 @@
  * File format routines 
  */
 
-void st_copyformat(ft, ft2)
-ft_t ft, ft2;
+void st_initformat(ft_t ft)
 {
+    ft->filename = 0;
+    ft->filetype = 0;
+    ft->fp = 0;
+
+    ft->info.rate = 0;
+    ft->info.size = -1;
+    ft->info.encoding = -1;
+    ft->info.channels = -1;
+
+    ft->comment = 0;
+    ft->swap = 0;
+
+    /* FIXME: This should zero out the reset of the structures */
+}
+
+void st_copyformat(ft_t ft, ft_t ft2)
+{
 	int noise = 0, i;
 	double factor;
 
@@ -353,10 +363,15 @@
 		ft2->info.channels = ft->info.channels;
 		noise = 1;
 	}
+
+	/* FIXME: Do not copy pointers!  This should be at least
+	 * a malloc+strcpy.
+	 */
 	if (ft2->comment == NULL) {
 		ft2->comment = ft->comment;
 		noise = 1;
 	}
+
 	/* 
 	 * copy loop info, resizing appropriately 
 	 * it's in samples, so # channels don't matter
@@ -372,50 +387,49 @@
 	ft2->instr = ft->instr;
 }
 
-void st_cmpformats(ft, ft2)
-ft_t ft, ft2;
-{
-}
-
 /* check that all settings have been given */
-int st_checkformat(ft) 
-ft_t ft;
+int st_checkformat(ft_t ft) 
 {
 
 	ft->st_errno = ST_SUCCESS;
 
 	if (ft->info.rate == 0)
+	{
 		st_fail_errno(ft,ST_EFMT,"Sampling rate for %s file was not given\n", ft->filename);
-	if ((ft->info.rate < 100) || (ft->info.rate > 999999L))
-		st_fail_errno(ft,ST_EFMT,"Sampling rate %lu for %s file is bogus\n", 
-			ft->info.rate, ft->filename);
+		return ST_EOF;
+	}
+
 	if (ft->info.size == -1)
+	{
 		st_fail_errno(ft,ST_EFMT,"Data size was not given for %s file\nUse one of -b/-w/-l/-f/-d/-D", ft->filename);
+		return ST_EOF;
+	}
+
 	if (ft->info.encoding == -1 && ft->info.size != ST_SIZE_FLOAT)
+	{
 		st_fail_errno(ft,ST_EFMT,"Data encoding was not given for %s file\nUse one of -s/-u/-U/-A", ft->filename);
-/* I put these here because some modules call st_fail_errno with 
- *	st_sizes_str[ft->info.size] etc.  I don't think the library should 
- *	seg fault even if the app using doesn't init ft properly or overflows 
- *	into it. 
- *  anyway to check length on st_sizes_str[] ? */ 
-	if ((ft->info.size < 0) || (ft->info.size > 7))
+		return ST_EOF;
+	}
+
+	if ((ft->info.size <= 0) || (ft->info.size > ST_SIZE_MAX))
+	{
 		st_fail_errno(ft,ST_EFMT,"Data size %i for %s file is bogus\n", ft->filename,ft->info.size);
+		return ST_EOF;
+	}
+
 	/* anyway to check length on st_encoding_str[] ? */ 
-	if (ft->info.encoding < -1 || ft->info.encoding > 7)
+	if (ft->info.encoding <= 0  || ft->info.encoding > ST_ENCODING_MAX)
+	{
 		st_fail_errno(ft,ST_EFMT,"Data encoding %i for %s file is bogus\n", ft->filename,ft->info.encoding);
-	/* it's so common, might as well default */
-	if (ft->info.channels == -1)
-		ft->info.channels = 1;
-	/*	st_fail("Number of output channels was not given for %s file",
-			ft->filename); */
-	return ft->st_errno;
+		return ST_EOF;
+	}
+
+	return ST_SUCCESS;
 }
 
 static ft_t ft_queue[2];
 
-void
-sigint(s)
-int s;
+void sigint(int s)
 {
     if (s == SIGINT) {
 	if (ft_queue[0])
@@ -425,9 +439,7 @@
     }
 }
 
-void
-sigintreg(ft)
-ft_t ft;
+void sigintreg(ft_t ft)
 {
     if (ft_queue[0] == 0)
 	ft_queue[0] = ft;
@@ -438,9 +450,7 @@
 
 /* Parse a time specification in hh:mm:ss.frac format.  Returns -1 */
 /* on failure. */
-double
-st_parsetime(str)
-char *str;
+double st_parsetime(char *str)
 {
     double time, moretime;
     if (sscanf(str, "%lf", &time) != 1)