aboutsummaryrefslogtreecommitdiffstats
path: root/utils/frame.c
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-11-01 18:22:39 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-11-01 18:22:39 +0000
commitea96823f2626e2a4178eec54f8a78a666d376ac1 (patch)
treed6fd63606de3355d2e0f7bb2ab751116095a1451 /utils/frame.c
parent1c4fdfd503883d806ba05da5d0e9156b54a4d51b (diff)
fix a bunch of potential problems found by gcc 4.3.x, primarily bare strings being passed to printf()-like functions and ignored results from read()/write() and friends
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@153337 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'utils/frame.c')
-rw-r--r--utils/frame.c262
1 files changed, 160 insertions, 102 deletions
diff --git a/utils/frame.c b/utils/frame.c
index 7fdb1637d..52f2d498e 100644
--- a/utils/frame.c
+++ b/utils/frame.c
@@ -59,14 +59,14 @@ int getremainingfilelength( FILE *anyin, long *result)
{
long i;
- i = ftell (anyin);
+ i = ftell(anyin);
if (i == -1) return FALSE;
- if (fseek (anyin, 0, SEEK_END) == -1) return FALSE;
- *result = ftell (anyin);
+ if (fseek(anyin, 0, SEEK_END) == -1) return FALSE;
+ *result = ftell(anyin);
if (*result == -1) return FALSE;
(*result) -= i;
(*result) /= samplewidth;
- if (fseek (anyin, i, SEEK_SET) == -1) return FALSE;
+ if (fseek(anyin, i, SEEK_SET) == -1) return FALSE;
return TRUE;
}
@@ -81,31 +81,39 @@ void readpkheader( FILE *anyin)
for (i = 0; i < 11; i++)
{
- fread( &tempint, 4, 1, anyin);
- printf( "%d: %d, ", i, tempint);
+ if (!fread( &tempint, 4, 1, anyin)) {
+ return;
+ }
+ printf( "%d: %d, ", i, tempint);
}
printf( "\n");
- fread( blood, 1, 8, anyin);
+ if (!fread( blood, 1, 8, anyin)) {
+ return;
+ }
for (i = 0; i < 8; i++)
- printf( "%d ", blood[i]);
+ printf( "%d ", blood[i]);
printf( "\n");
for (i = 0; i < 8; i++)
- {
- for (x = 128; x > 0; x /= 2)
- printf((blood[i] & x) == 0? "0 ":"1 ");
- printf(i%4==3? "\n":"| ");
- }
+ {
+ for (x = 128; x > 0; x /= 2)
+ printf((blood[i] & x) == 0? "0 ":"1 ");
+ printf(i%4==3? "\n":"| ");
+ }
printf( "\n");
for (i = 0; i < 2; i++)
{
- fread( &tempint, 4, 1, anyin);
- printf( "%d: %d, ", i, tempint);
+ if (!fread( &tempint, 4, 1, anyin)) {
+ return;
+ }
+ printf( "%d: %d, ", i, tempint);
}
printf( "\n");
for (i = 0; i < 2; i++)
{
- fread( &tempushort, 2, 1, anyin);
- printf( "%d: %d, ", i, tempushort);
+ if (!fread( &tempushort, 2, 1, anyin)) {
+ return;
+ }
+ printf( "%d: %d, ", i, tempushort);
}
printf( "\n");
}
@@ -125,59 +133,81 @@ void readwavheader( FILE *anyin)
iswav = FALSE;
if (ftell(anyin) == -1) /* If we cannot seek this file */
- {
- nowav = TRUE; /* -> Pretend this is no wav-file */
- chat("File not seekable: not checking for WAV-header.\n");
- }
+ {
+ nowav = TRUE; /* -> Pretend this is no wav-file */
+ chat("File not seekable: not checking for WAV-header.\n");
+ }
else
- {
- /* Expect four bytes "RIFF" and four bytes filelength */
- fread (str, 1, 8, anyin); /* 0 */
- str[4] = '\0';
- if (strcmp(str, "RIFF") != 0) nowav = TRUE;
- /* Expect eight bytes "WAVEfmt " */
- fread (str, 1, 8, anyin); /* 8 */
- str[8] = '\0';
- if (strcmp(str, "WAVEfmt ") != 0) nowav = TRUE;
- /* Expect length of fmt data, which should be 16 */
- fread (&tempuint, 4, 1, anyin); /* 16 */
- if (tempuint != 16) nowav = TRUE;
- /* Expect format tag, which should be 1 for pcm */
- fread (&tempushort, 2, 1, anyin); /* 20 */
- if (tempushort != 1)
- nowav = TRUE;
- /* Expect number of channels */
- fread (&cn, 2, 1, anyin); /* 20 */
- if (cn != 1 && cn != 2) nowav = TRUE;
- /* Read samplefrequency */
- fread (&sf, 4, 1, anyin); /* 24 */
- /* Read bytes per second: Should be samplefreq * channels * 2 */
- fread (&tempuint, 4, 1, anyin); /* 28 */
- if (tempuint != sf * cn * 2) nowav = TRUE;
- /* read bytes per frame: Should be channels * 2 */
- fread (&tempushort, 2, 1, anyin); /* 32 */
- if (tempushort != cn * 2) nowav = TRUE;
- /* Read bits per sample: Should be 16 */
- fread (&tempushort, 2, 1, anyin); /* 34 */
- if (tempushort != 16) nowav = TRUE;
- fread (str, 4, 1, anyin); /* 36 */
- str[4] = '\0';
- if (strcmp(str, "data") != 0) nowav = TRUE;
- fread (&tempuint, 4, 1, anyin); /* 40 */
- if (nowav)
- {
- fseek (anyin, 0, SEEK_SET); /* Back to beginning of file */
- chat("File has no WAV header.\n");
- }
- else
- {
- samplefrequency = sf;
- channels = cn;
- chat ("Read WAV header: %d channels, samplefrequency %d.\n",
- channels, samplefrequency);
- iswav = TRUE;
- }
- }
+ {
+ /* Expect four bytes "RIFF" and four bytes filelength */
+ if (!fread(str, 1, 8, anyin)) { /* 0 */
+ return;
+ }
+ str[4] = '\0';
+ if (strcmp(str, "RIFF") != 0) nowav = TRUE;
+ /* Expect eight bytes "WAVEfmt " */
+ if (!fread(str, 1, 8, anyin)) { /* 8 */
+ return;
+ }
+ str[8] = '\0';
+ if (strcmp(str, "WAVEfmt ") != 0) nowav = TRUE;
+ /* Expect length of fmt data, which should be 16 */
+ if (!fread(&tempuint, 4, 1, anyin)) { /* 16 */
+ return;
+ }
+ if (tempuint != 16) nowav = TRUE;
+ /* Expect format tag, which should be 1 for pcm */
+ if (!fread(&tempushort, 2, 1, anyin)) { /* 20 */
+ return;
+ }
+ if (tempushort != 1)
+ nowav = TRUE;
+ /* Expect number of channels */
+ if (!fread(&cn, 2, 1, anyin)) { /* 20 */
+ return;
+ }
+ if (cn != 1 && cn != 2) nowav = TRUE;
+ /* Read samplefrequency */
+ if (!fread(&sf, 4, 1, anyin)) { /* 24 */
+ return;
+ }
+ /* Read bytes per second: Should be samplefreq * channels * 2 */
+ if (!fread(&tempuint, 4, 1, anyin)) { /* 28 */
+ return;
+ }
+ if (tempuint != sf * cn * 2) nowav = TRUE;
+ /* read bytes per frame: Should be channels * 2 */
+ if (!fread(&tempushort, 2, 1, anyin)) { /* 32 */
+ return;
+ }
+ if (tempushort != cn * 2) nowav = TRUE;
+ /* Read bits per sample: Should be 16 */
+ if (!fread(&tempushort, 2, 1, anyin)) { /* 34 */
+ return;
+ }
+ if (tempushort != 16) nowav = TRUE;
+ if (!fread(str, 4, 1, anyin)) { /* 36 */
+ return;
+ }
+ str[4] = '\0';
+ if (strcmp(str, "data") != 0) nowav = TRUE;
+ if (!fread(&tempuint, 4, 1, anyin)) { /* 40 */
+ return;
+ }
+ if (nowav)
+ {
+ fseek(anyin, 0, SEEK_SET); /* Back to beginning of file */
+ chat("File has no WAV header.\n");
+ }
+ else
+ {
+ samplefrequency = sf;
+ channels = cn;
+ chat("Read WAV header: %d channels, samplefrequency %d.\n",
+ channels, samplefrequency);
+ iswav = TRUE;
+ }
+ }
return;
}
@@ -192,36 +222,62 @@ void makewavheader( void)
unsigned short tempushort;
/* If fseek fails, don't create the header. */
- if (fseek (out, 0, SEEK_END) != -1)
- {
- filelength = ftell (out);
- chat ("filelength %d, ", filelength);
- fseek (out, 0, SEEK_SET);
- fwrite ("RIFF", 1, 4, out); /* 0 */
- tempuint = filelength - 8; fwrite (&tempuint, 4, 1, out); /* 4 */
- fwrite ("WAVEfmt ", 1, 8, out); /* 8 */
- /* length of fmt data 16 bytes */
- tempuint = 16;
- fwrite (&tempuint, 4, 1, out); /* 16 */
- /* Format tag: 1 for pcm */
- tempushort = 1;
- fwrite (&tempushort, 2, 1, out); /* 20 */
- chat ("%d channels\n", channels);
- fwrite (&channels, 2, 1, out);
- chat ("samplefrequency %d\n", samplefrequency);
- fwrite (&samplefrequency, 4, 1, out); /* 24 */
- /* Bytes per second */
- tempuint = channels * samplefrequency * 2;
- fwrite (&tempuint, 4, 1, out); /* 28 */
- /* Block align */
- tempushort = 2 * channels;
- fwrite (&tempushort, 2, 1, out); /* 32 */
- /* Bits per sample */
- tempushort = 16;
- fwrite (&tempushort, 2, 1, out); /* 34 */
- fwrite ("data", 4, 1, out); /* 36 */
- tempuint = filelength - 44; fwrite (&tempuint, 4, 1, out); /* 40 */
- }
+ if (fseek(out, 0, SEEK_END) != -1)
+ {
+ filelength = ftell(out);
+ chat("filelength %d, ", filelength);
+ fseek(out, 0, SEEK_SET);
+ if (!fwrite("RIFF", 1, 4, out)) { /* 0 */
+ return;
+ }
+ tempuint = filelength - 8;
+ if (!fwrite(&tempuint, 4, 1, out)) { /* 4 */
+ return;
+ }
+ if (!fwrite("WAVEfmt ", 1, 8, out)) { /* 8 */
+ return;
+ }
+ /* length of fmt data 16 bytes */
+ tempuint = 16;
+ if (!fwrite(&tempuint, 4, 1, out)) { /* 16 */
+ return;
+ }
+ /* Format tag: 1 for pcm */
+ tempushort = 1;
+ if (!fwrite(&tempushort, 2, 1, out)) { /* 20 */
+ return;
+ }
+ chat("%d channels\n", channels);
+ if (!fwrite(&channels, 2, 1, out)) {
+ return;
+ }
+ chat("samplefrequency %d\n", samplefrequency);
+ if (!fwrite(&samplefrequency, 4, 1, out)) { /* 24 */
+ return;
+ }
+ /* Bytes per second */
+ tempuint = channels * samplefrequency * 2;
+ if (!fwrite(&tempuint, 4, 1, out)) { /* 28 */
+ return;
+ }
+ /* Block align */
+ tempushort = 2 * channels;
+ if (!fwrite(&tempushort, 2, 1, out)) { /* 32 */
+ return;
+ }
+ /* Bits per sample */
+ tempushort = 16;
+ if (!fwrite(&tempushort, 2, 1, out)) { /* 34 */
+ return;
+ }
+ if (!fwrite("data", 4, 1, out)) { /* 36 */
+ return;
+ }
+ tempuint = filelength - 44;
+ if (!fwrite(&tempuint, 4, 1, out)) { /* 40 */
+ return;
+ }
+ }
return;
}
@@ -870,10 +926,10 @@ int myexit (int value)
case 0:
if (wavout)
makewavheader(); /* Writes a fully informed .WAV header */
- chat ("Success!\n");
+ chat("Success!\n");
break;
default:
- chat ("Failure.\n");
+ chat("Failure.\n");
break;
}
exit (value);
@@ -903,7 +959,9 @@ int workloop( FILE *theinfile, FILE *theoutfile,
/* Call the routine that does the work */
if (!work (buffer, nowlength)) /* On error, stop. */
return FALSE;
- fwrite(buffer, sizeof(short), nowlength, theoutfile);
+ if (!fwrite(buffer, sizeof(short), nowlength, theoutfile)) {
+ return FALSE;
+ }
if (ferror( theoutfile) != 0)
fatalperror("Error writing to output file");
}