]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/redspark.c
Merge commit 'c11c693accaad65d3f4afa44c27f2338a2e3bf8f'
[ffmpeg] / libavformat / redspark.c
index 8535bef11ab636b20ba82be066d689e1b8dba477..c247046584e0770e2c64bd98af4e96d6d8a5035d 100644 (file)
@@ -26,6 +26,7 @@
 #include "internal.h"
 
 #define HEADER_SIZE 4096
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
 
 typedef struct RedSparkContext {
     int         samples_count;
@@ -38,11 +39,13 @@ static int redspark_probe(AVProbeData *p)
 
     /* Decrypt first 8 bytes of the header */
     data = AV_RB32(p->buf);
-    data = data ^ (key = data ^ 0x52656453);
+    key  = data ^ 0x52656453;
+    data ^= key;
     AV_WB32(header, data);
-    key = (key << 11) | (key >> 21);
+    key = rol(key, 11);
 
-    data = AV_RB32(p->buf + 4) ^ (((key << 3) | (key >> 29)) + key);
+    key += rol(key, 3);
+    data = AV_RB32(p->buf + 4) ^ key;
     AV_WB32(header + 4, data);
 
     if (AV_RB64(header) == AV_RB64("RedSpark"))
@@ -59,7 +62,7 @@ static int redspark_read_header(AVFormatContext *s)
     GetByteContext gbc;
     int i, coef_off, ret = 0;
     uint32_t key, data;
-    uint8_t *header, *pbc;
+    uint8_t header[HEADER_SIZE];
     AVStream *st;
 
     st = avformat_new_stream(s, NULL);
@@ -67,20 +70,17 @@ static int redspark_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     par = st->codecpar;
 
-    header = av_malloc(HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
-    if (!header)
-        return AVERROR(ENOMEM);
-    pbc = header;
-
     /* Decrypt header */
     data = avio_rb32(pb);
-    data = data ^ (key = data ^ 0x52656453);
-    bytestream_put_be32(&pbc, data);
-    key = (key << 11) | (key >> 21);
+    key  = data ^ 0x52656453;
+    data ^= key;
+    AV_WB32(header, data);
+    key = rol(key, 11);
 
     for (i = 4; i < HEADER_SIZE; i += 4) {
-        data = avio_rb32(pb) ^ (key = ((key << 3) | (key >> 29)) + key);
-        bytestream_put_be32(&pbc, data);
+        key += rol(key, 3);
+        data = avio_rb32(pb) ^ key;
+        AV_WB32(header + i, data);
     }
 
     par->codec_id    = AV_CODEC_ID_ADPCM_THP;
@@ -91,8 +91,7 @@ static int redspark_read_header(AVFormatContext *s)
     par->sample_rate = bytestream2_get_be32u(&gbc);
     if (par->sample_rate <= 0 || par->sample_rate > 96000) {
         av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate);
-        ret = AVERROR_INVALIDDATA;
-        goto fail;
+        return AVERROR_INVALIDDATA;
     }
 
     st->duration = bytestream2_get_be32u(&gbc) * 14;
@@ -100,8 +99,7 @@ static int redspark_read_header(AVFormatContext *s)
     bytestream2_skipu(&gbc, 10);
     par->channels = bytestream2_get_byteu(&gbc);
     if (!par->channels) {
-        ret = AVERROR_INVALIDDATA;
-        goto fail;
+        return AVERROR_INVALIDDATA;
     }
 
     coef_off = 0x54 + par->channels * 8;
@@ -109,30 +107,24 @@ static int redspark_read_header(AVFormatContext *s)
         coef_off += 16;
 
     if (coef_off + par->channels * (32 + 14) > HEADER_SIZE) {
-        ret = AVERROR_INVALIDDATA;
-        goto fail;
+        return AVERROR_INVALIDDATA;
     }
 
     if (ff_alloc_extradata(par, 32 * par->channels)) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
+        return AVERROR_INVALIDDATA;
     }
 
     /* Get the ADPCM table */
     bytestream2_seek(&gbc, coef_off, SEEK_SET);
     for (i = 0; i < par->channels; i++) {
         if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) {
-            ret = AVERROR_INVALIDDATA;
-            goto fail;
+            return AVERROR_INVALIDDATA;
         }
         bytestream2_skipu(&gbc, 14);
     }
 
     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
 
-fail:
-    av_free(header);
-
     return ret;
 }