diff mbox series

[FFmpeg-devel,04/10] avformat/aadec: Don't unnecessarily reinitialize AVTEA context

Message ID AM7PR03MB6660ABC7D793F67B95E33D008F6D9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit c9b30992102bbc4120b5c0139d1aa8a2dbb2ebb0
Headers show
Series [FFmpeg-devel,01/10] avformat/utils: Make ff_data_to_hex() zero-terminate the string
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 6, 2021, 1:12 a.m. UTC
We use ECB, not CBC mode here, so one does not need to reinitialize
the context; for the same reason, one can also just let av_tea_crypt()
loop over the blocks, avoiding a loop here.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/aadec.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/aadec.c b/libavformat/aadec.c
index 7e97120070..24116b1f70 100644
--- a/libavformat/aadec.c
+++ b/libavformat/aadec.c
@@ -173,6 +173,7 @@  static int aa_read_header(AVFormatContext *s)
     for (i = 0; i < 16; i++)
         av_log(s, AV_LOG_DEBUG, "%02x", c->file_key[i]);
     av_log(s, AV_LOG_DEBUG, "\n");
+    av_tea_init(c->tea_ctx, c->file_key, 16);
 
     /* decoder setup */
     st = avformat_new_stream(s, NULL);
@@ -246,9 +247,6 @@  static int aa_read_header(AVFormatContext *s)
 
 static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    int i;
-    int blocks;
-    uint8_t *buf;
     int ret;
     AADemuxContext *c = s->priv_data;
     uint64_t pos = avio_tell(s->pb);
@@ -279,15 +277,10 @@  static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (ret != c->current_codec_second_size)
         return AVERROR_EOF;
 
-    buf = pkt->data;
-    // decrypt c->current_codec_second_size bytes
+    // decrypt c->current_codec_second_size bytes in blocks of TEA_BLOCK_SIZE
     // trailing bytes are left unencrypted!
-    blocks = c->current_codec_second_size / TEA_BLOCK_SIZE;
-    for (i = 0; i < blocks; i++) {
-        av_tea_init(c->tea_ctx, c->file_key, 16);
-        av_tea_crypt(c->tea_ctx, buf, buf, 1, NULL, 1);
-        buf += TEA_BLOCK_SIZE;
-    }
+    av_tea_crypt(c->tea_ctx, pkt->data, pkt->data,
+                 c->current_codec_second_size / TEA_BLOCK_SIZE, NULL, 1);
 
     // update state
     c->current_chapter_size = c->current_chapter_size - c->current_codec_second_size;