- if (size == ALAC_NEW_KUKI) {
- st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
- return AVERROR(ENOMEM);
- memcpy(st->codec->extradata, "\0\0\0\24alac", 8);
- avio_read(pb, st->codec->extradata + ALAC_HEADER - ALAC_NEW_KUKI, ALAC_NEW_KUKI);
- st->codec->extradata_size = ALAC_HEADER;
- } else {
+ uint8_t preamble[12];
+ if (size < ALAC_NEW_KUKI) {
+ av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
+ avio_skip(pb, size);
+ return AVERROR_INVALIDDATA;
+ }
+ avio_read(pb, preamble, ALAC_PREAMBLE);
+
+ st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (!st->codec->extradata)
+ return AVERROR(ENOMEM);
+
+ /* For the old style cookie, we skip 12 bytes, then read 36 bytes.
+ * The new style cookie only contains the last 24 bytes of what was
+ * 36 bytes in the old style cookie, so we fabricate the first 12 bytes
+ * in that case to maintain compatibility. */
+ if (!memcmp(&preamble[4], "frmaalac", 8)) {