#include "internal.h"
#define HEADER_SIZE 4096
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
typedef struct RedSparkContext {
int samples_count;
/* 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"))
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);
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;
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;
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;
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;
}