[asterisk-commits] trunk - r8357 in /trunk/formats: format_pcm.c format_pcm_alaw.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Jan 20 14:36:33 MST 2006


Author: mattf
Date: Fri Jan 20 15:36:33 2006
New Revision: 8357

URL: http://svn.digium.com/view/asterisk?rev=8357&view=rev
Log:
Fix for formats so they give better output on failure conditions. (#6141)

Modified:
    trunk/formats/format_pcm.c
    trunk/formats/format_pcm_alaw.c

Modified: trunk/formats/format_pcm.c
URL: http://svn.digium.com/view/asterisk/trunk/formats/format_pcm.c?rev=8357&r1=8356&r2=8357&view=diff
==============================================================================
--- trunk/formats/format_pcm.c (original)
+++ trunk/formats/format_pcm.c Fri Jan 20 15:36:33 2006
@@ -177,6 +177,7 @@
 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
 {
 	long cur, max, offset = 0;
+ 	int ret = -1;	/* assume error */
 
 	cur = ftell(fs->f);
 	fseek(fs->f, 0, SEEK_END);
@@ -193,29 +194,33 @@
 	case SEEK_FORCECUR:
 		offset = cur + sample_offset;
 		break;
-	}
-
-	switch (whence) {
-	case SEEK_FORCECUR:
+	default:
+		ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
+		offset = sample_offset;
+	}
+	if (offset < 0) {
+		ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", offset);
+		offset = 0;
+	}
+	if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
+		size_t left = offset - max;
+
+		while (left) {
+			size_t written = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
+				     (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
+			if (written == -1)
+				break;	/* error */
+			left -= written * sizeof(ulaw_silence[0]);
+		}
+		ret = 0; /* successful */
+	} else {
 		if (offset > max) {
-			size_t left = offset - max;
-			size_t res;
-
-			while (left) {
-				res = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
-					     (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-				if (res == -1)
-					return res;
-				left -= res * sizeof(ulaw_silence[0]);
-			}
-			return offset;
-		}
-		/* fall through */
-	default:
-		offset = (offset > max) ? max : offset;
-		offset = (offset < 0) ? 0 : offset;
-		return fseek(fs->f, offset, SEEK_SET);
-	}
+			ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", offset, max);
+			offset = max;
+		}
+		ret = fseek(fs->f, offset, SEEK_SET);
+	}
+	return ret;
 }
 
 static int pcm_trunc(struct ast_filestream *fs)

Modified: trunk/formats/format_pcm_alaw.c
URL: http://svn.digium.com/view/asterisk/trunk/formats/format_pcm_alaw.c?rev=8357&r1=8356&r2=8357&view=diff
==============================================================================
--- trunk/formats/format_pcm_alaw.c (original)
+++ trunk/formats/format_pcm_alaw.c Fri Jan 20 15:36:33 2006
@@ -252,6 +252,7 @@
 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
 {
 	long cur, max, offset = 0;
+ 	int ret = -1; /* assume error */
 
 	cur = ftell(fs->f);
 	fseek(fs->f, 0, SEEK_END);
@@ -268,29 +269,34 @@
 	case SEEK_FORCECUR:
 		offset = cur + sample_offset;
 		break;
-	}
-
-	switch (whence) {
-	case SEEK_FORCECUR:
-		if (offset > max) {
-			size_t left = offset - max;
-			size_t res;
-
-			while (left) {
-				res = fwrite(alaw_silence, sizeof(alaw_silence[0]),
-					     (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-				if (res == -1)
-					return res;
-				left -= res * sizeof(alaw_silence[0]);
-			}
-			return offset;
-		}
-		/* fall through */
 	default:
-		offset = (offset > max) ? max : offset;
-		offset = (offset < 0) ? 0 : offset;
-		return fseek(fs->f, offset, SEEK_SET);
-	}
+		ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
+		offset = sample_offset;
+	}
+
+	if (offset < 0) {
+		offset = 0;
+		ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", offset);
+	}
+	if (whence == SEEK_FORCECUR && offset > max) {
+		size_t left = offset - max;
+
+		while (left) {
+			size_t written = fwrite(alaw_silence, sizeof(alaw_silence[0]),
+				     (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
+			if (written == -1)
+				break; /* error */
+			left -= written * sizeof(alaw_silence[0]);
+		}
+		ret = 0; /* success */
+	} else {
+                if (offset > max) {
+                        ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", offset, max);
+                        offset = max;
+		}
+                ret = fseek(fs->f, offset, SEEK_SET);
+	}
+	return ret;
 }
 
 static int pcm_trunc(struct ast_filestream *fs)



More information about the asterisk-commits mailing list