shithub: sox

Download patch

ref: 0aced46eadebfc33172a3c981c5477b7da0e209a
parent: 0da8e4cd5a4a3fe2a98f8beb7d74dbc55d557c19
author: Mans Rullgard <mans@mansr.com>
date: Sat Aug 8 07:41:36 EDT 2020

wav: use lsx_read_fields

--- a/src/wav.c
+++ b/src/wav.c
@@ -129,7 +129,6 @@
 
 static int wavwritehdr(sox_format_t *, int);
 
-
 /****************************************************************************/
 /* IMA ADPCM Support Functions Section                                      */
 /****************************************************************************/
@@ -138,13 +137,8 @@
 {
     priv_t *wav = ft->priv;
     size_t  bytesPerBlock;
+    int err;
 
-    if (len < 2) {
-        lsx_fail_errno(ft, SOX_EOF, "format[%s]: expects cbSize >= %d",
-                       wav_format_str(wav->formatTag), 2);
-        return SOX_EOF;
-    }
-
     if (wav->bitsPerSample != 4) {
         lsx_fail_errno(ft, SOX_EOF,
                        "Can only handle 4-bit IMA ADPCM in wav files");
@@ -151,7 +145,9 @@
         return SOX_EOF;
     }
 
-    lsx_readw(ft, &wav->samplesPerBlock);
+    err = lsx_read_fields(ft, &len, "h", &wav->samplesPerBlock);
+    if (err)
+        return SOX_EOF;
 
     bytesPerBlock = lsx_ima_bytes_per_block(ft->signal.channels,
                                             wav->samplesPerBlock);
@@ -216,13 +212,8 @@
     priv_t *wav = ft->priv;
     size_t  bytesPerBlock;
     int i, errct = 0;
+    int err;
 
-    if (len < 4) {
-        lsx_fail_errno(ft, SOX_EOF, "format[%s]: expects cbSize >= %d",
-                       wav_format_str(wav->formatTag), 4);
-        return SOX_EOF;
-    }
-
     if (wav->bitsPerSample != 4) {
         lsx_fail_errno(ft, SOX_EOF,
                        "Can only handle 4-bit MS ADPCM in wav files");
@@ -229,9 +220,9 @@
         return SOX_EOF;
     }
 
-    lsx_readw(ft, &wav->samplesPerBlock);
-    lsx_readw(ft, &wav->nCoefs);
-    len -= 4;
+    err = lsx_read_fields(ft, &len, "hh", &wav->samplesPerBlock, &wav->nCoefs);
+    if (err)
+        return SOX_EOF;
 
     bytesPerBlock = lsx_ms_adpcm_bytes_per_block(ft->signal.channels,
                                                  wav->samplesPerBlock);
@@ -264,14 +255,13 @@
     wav->lsx_ms_adpcm_i_coefs = lsx_malloc(wav->nCoefs * 2 * sizeof(short));
     wav->ms_adpcm_data = lsx_ms_adpcm_alloc(ft->signal.channels);
 
-    for (i = 0; len >= 2 && i < 2 * wav->nCoefs; i++) {
-        lsx_readsw(ft, &wav->lsx_ms_adpcm_i_coefs[i]);
-        len -= 2;
+    err = lsx_read_fields(ft, &len, "*h",
+                          2 * wav->nCoefs, wav->lsx_ms_adpcm_i_coefs);
+    if (err)
+        return SOX_EOF;
 
-        if (i < 14)
-            errct +=
-                wav->lsx_ms_adpcm_i_coefs[i] != lsx_ms_adpcm_i_coef[i/2][i%2];
-    }
+    for (i = 0; i < 14; i++)
+        errct += wav->lsx_ms_adpcm_i_coefs[i] != lsx_ms_adpcm_i_coef[i/2][i%2];
 
     if (errct)
         lsx_warn("base lsx_ms_adpcm_i_coefs differ in %d/14 positions", errct);
@@ -361,15 +351,12 @@
 static int wav_gsm_fmt(sox_format_t *ft, uint32_t len)
 {
     priv_t *wav = ft->priv;
+    int err;
 
-    if (len < 2) {
-        lsx_fail_errno(ft, SOX_EOF, "format[%s]: expects wExtSize >= %d",
-                       wav_format_str(wav->formatTag), 2);
+    err = lsx_read_fields(ft, &len, "h", &wav->samplesPerBlock);
+    if (err)
         return SOX_EOF;
-    }
 
-    lsx_readw(ft, &wav->samplesPerBlock);
-
     if (wav->blockAlign != 65) {
         lsx_fail_errno(ft, SOX_EOF, "format[%s]: expects blockAlign(%d) = %d",
                        wav_format_str(wav->formatTag), wav->blockAlign, 65);
@@ -646,6 +633,7 @@
     uint16_t wExtSize = 0;       /* extended field for non-PCM */
     const struct wave_format *fmt;
     sox_encoding_t user_enc = ft->encoding.encoding;
+    int err;
 
     if (len < 16) {
         lsx_fail_errno(ft, SOX_EHDR, "WAVE file fmt chunk is too short");
@@ -652,13 +640,15 @@
         return SOX_EOF;
     }
 
-    lsx_readw(ft, &wav->formatTag);
-    lsx_readw(ft, &wChannels);
-    lsx_readdw(ft, &dwSamplesPerSecond);
-    lsx_readdw(ft, &dwAvgBytesPerSec);   /* Average bytes/second */
-    lsx_readw(ft, &wav->blockAlign);     /* Block align */
-    lsx_readw(ft, &wav->bitsPerSample);  /* bits per sample per channel */
-    len -= 16;
+    err = lsx_read_fields(ft, &len, "hhiihh",
+                          &wav->formatTag,
+                          &wChannels,
+                          &dwSamplesPerSecond,
+                          &dwAvgBytesPerSec,
+                          &wav->blockAlign,
+                          &wav->bitsPerSample);
+    if (err)
+        return SOX_EOF;
 
     /* non-PCM formats except alaw and mulaw formats have extended fmt chunk.
      * Check for those cases.
@@ -670,8 +660,9 @@
         lsx_warn("WAVE file missing extended part of fmt chunk");
 
     if (len >= 2) {
-        lsx_readw(ft, &wExtSize);
-        len -= 2;
+        err = lsx_read_fields(ft, &len, "h", &wExtSize);
+        if (err)
+            return SOX_EOF;
     }
 
     if (wExtSize != len) {
@@ -690,11 +681,12 @@
             return SOX_EOF;
         }
 
-        lsx_readw(ft, &numberOfValidBits);
-        lsx_readdw(ft, &speakerPositionMask);
-        lsx_readw(ft, &subFormatTag);
-        lsx_skipbytes(ft, 14);
-        len -= 22;
+        err = lsx_read_fields(ft, &len, "hih14x",
+                              &numberOfValidBits,
+                              &speakerPositionMask,
+                              &subFormatTag);
+        if (err)
+            return SOX_EOF;
 
         if (numberOfValidBits > wav->bitsPerSample) {
             lsx_fail_errno(ft, SOX_EHDR,
@@ -823,6 +815,7 @@
     }
 
     while (!read_chunk_header(ft, magic, &clen)) {
+        uint32_t len = clen;
         off_t cstart = lsx_tell(ft);
         off_t pos;
 
@@ -847,9 +840,12 @@
                 clen = 28;
             }
 
-            lsx_readqw(ft, &ds64_riff_size);
-            lsx_readqw(ft, &ds64_data_size);
-            lsx_readqw(ft, &ds64_sample_count);
+            err = lsx_read_fields(ft, &len, "qqq",
+                                  &ds64_riff_size,
+                                  &ds64_data_size,
+                                  &ds64_sample_count);
+            if (err)
+                return SOX_EOF;
 
             goto next;
         }
@@ -867,12 +863,10 @@
         if (!memcmp(magic, "fact", 4)) {
             uint32_t val;
 
-            if (clen < 4) {
-                lsx_fail_errno(ft, SOX_EHDR, "fact chunk too small");
+            err = lsx_read_fields(ft, &len, "i", &val);
+            if (err)
                 return SOX_EOF;
-            }
 
-            lsx_readdw(ft, &val);
             wav->numSamples = val;
 
             goto next;