From patchwork Thu May 16 22:30:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13155 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3C21144964F for ; Fri, 17 May 2019 01:43:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2313C689D5F; Fri, 17 May 2019 01:43:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5C6BC689B96 for ; Fri, 17 May 2019 01:43:29 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id f8so3656845wrt.1 for ; Thu, 16 May 2019 15:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DvuVQIXJSgrR6fqYo4Y5sXsOfkKaWim29YT3bekp8dU=; b=Wj/HGZWHu4I2ncy3j345mhkpgzlDDFeGmm9GA266obZBRsFU5TTHYcsZ9vv+SRt85N PuiVMoVguO/vgOwiI2F1w5A8+xNDI3YmdhFie/wPk2Re2bEgkmjXB8uhSIE8peYHt0HV 1M3OkVfPMjb4YTZo7H/VzGbd52etCRawvDmAWfd1WFcLtSyojKpzcZ9qFgLOaqMRP6Bl QNT94fKSk6/eHGdk6uhgp0hHqgevKFwdXw9GEVhD3v+JhXBdZ++sxfR+ABrhWMBZI5eo y3SkG7cUjO2/xcVddIgF/eNjqg2eu5PqSjEdWADCgqfkb8YGU3pgchu+fFyi5XJvJOrD 49rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DvuVQIXJSgrR6fqYo4Y5sXsOfkKaWim29YT3bekp8dU=; b=qggHnG8f9FdQd8938c2d+7MmNK0mbHbX3E0fDzs7QgQTjfYrjCWbLzYhTBrH2/EvtV QV9lzhWAJLSG0xdVuZfAA9TPi69VeeBjoSeP2K/P8ZkSEeXPWRugxinxAWt14eN33un9 SuCQYSpwcXySOG0FA744g60PH1sZPqLLX1MWLEMqZsoM7LAeXgPbFyhu3cvADDiBISLB 6/I1LUGUkSiZPypJR8Z7X3oHF5RI+ahyMAAm1sSMKKHRX0sY4SCOOeo3sIEnUPISVfZr OS78hGnfdjeYxvNvYnnhgjbBMXcotfnxB3ZM7Pq5OO4d+GlLC5Pz9IwWucgJlO191Uop tM9Q== X-Gm-Message-State: APjAAAXFsALGxwE7eLhUSjZxQehR35Naf2Z3JAly68q8ak+QqkJ3pFYl P071gXaUGttfRFheoysT/R8bi0z6 X-Google-Smtp-Source: APXvYqznYm+3x2Yfane0t6CruOz/XPIBjDPZl2uqTkKYlRFf6hAJNlhoJ18PjQxSho0paFit5ZAycw== X-Received: by 2002:adf:e850:: with SMTP id d16mr16735363wrn.269.1558046608723; Thu, 16 May 2019 15:43:28 -0700 (PDT) Received: from localhost.localdomain (ipbcc18715.dynamic.kabel-deutschland.de. [188.193.135.21]) by smtp.gmail.com with ESMTPSA id i185sm11168725wmg.32.2019.05.16.15.43.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 15:43:28 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 May 2019 00:30:16 +0200 Message-Id: <20190516223018.30827-33-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> References: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 32/37] avformat/matroskadec: Accept more unknown-length elements X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The current Matroska specifications mandate that only two elements may use an unknown-length length: Segments and clusters. But this was not always so: For the greater part of Matroska's existence, all master elements were allowed to make use of the unknown-length feature. And there were muxers creating such files: For several years libavformat's Matroska muxer used unknown-length for all master elements when the output wasn't seekable. This only stopped in March 2010 with 2529bb30. And even afterwards it was possible (albeit unlikely) for libavformat to create unknown-length master elements that are in violation of today's specifications, namely if the master element was so big that the seek backwards to update the size could no longer be performed inside the AVIOContext's write buffer. This has only been fixed in October 2016 (with the patches that introduced support for writing CRC-32 elements). Libavformat's Matroska demuxer meanwhile has never really supported unknown-length elements besides segments and clusters. Support for the latter was hardcoded. This commit changes this: Now all master elements for which a syntax to parse them is available are supported. This includes the files produced by old versions of libavformat's muxer. More precisely, master elements that have unknown length and are about to be parsed (not skipped) are supported; only a warning is emitted for them. For normal files, this means that level 1 elements after the clusters that are encountered after the clusters have been parsed (i.e. not because they are referenced by the seekhead at the beginning of the file) are still unsupported (they would be skipped at this point if their length were known). Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 85d8252f6d..404e5005a7 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1277,15 +1277,20 @@ static int ebml_parse(MatroskaDemuxContext *matroska, av_log(matroska->ctx, AV_LOG_ERROR, "Unknown-sized element " "at 0x%"PRIx64" inside parent with finite size\n", pos); return AVERROR_INVALIDDATA; - } else if (id != MATROSKA_ID_CLUSTER) { - // According to the specifications only clusters and segments - // are allowed to be unknown-sized. - av_log(matroska->ctx, AV_LOG_ERROR, - "Found unknown-sized element other than a cluster at " - "0x%"PRIx64". Dropping the invalid element.\n", pos); - return AVERROR_INVALIDDATA; - } else + } else { level_check = 0; + if (id != MATROSKA_ID_CLUSTER && (syntax->type == EBML_LEVEL1 + || syntax->type == EBML_NEST)) { + // According to the current specifications only clusters and + // segments are allowed to be unknown-length. We also accept + // other unknown-length master elements. + av_log(matroska->ctx, AV_LOG_WARNING, + "Found unknown-length element 0x%"PRIX32" other than " + "a cluster at 0x%"PRIx64". Spec-incompliant, but " + "parsing will nevertheless be attempted.\n", id, pos); + update_pos = -1; + } + } } else level_check = 0; @@ -1351,7 +1356,7 @@ static int ebml_parse(MatroskaDemuxContext *matroska, } } - if (update_pos) { + if (update_pos > 0) { // We have found an element that is allowed at this place // in the hierarchy and it passed all checks, so treat the beginning // of the element as the "last known good" position.