From patchwork Sun Jun 9 11:00:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13473 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 DFA4B447159 for ; Sun, 9 Jun 2019 14:15:46 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C6F3E68AABA; Sun, 9 Jun 2019 14:15:46 +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 E1610689D7A for ; Sun, 9 Jun 2019 14:15:40 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id b17so6302831wrq.11 for ; Sun, 09 Jun 2019 04:15:40 -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=be/+nImTNLk+zRsBzEXJL2eKm33hVOLCDnl/dLNBn+Y=; b=tEk8+uHhYwJ344JjIOBNFns0IdFUQh4Y2OXmulHCiHvx0BckQDU9U1R5Gd7TN6/5AS +BWt9EXT2LoV0u285chj1EBuP33woaC8xCYJO/8pw09pvy4nlXtngxEI0sURmiKK1NWe CUgWGqsYqtMx3sxi7Z4LuAa0537B3zl1XXiducKiKH2TK+PEAP7HPhPpqE/zSdtsReFM +kbPKvfiBK1RNrbCrp20kkf+yA8yiWKHJQdiUIRTAqVQPlHGrA+pD4GBW2jxWOxjM98/ PQVtd6msSBL8wPcmEqSm9jLWu0ZT4kR+Rq9S3RRUMb8JyE/c121d80vHABzmuX215raB GxVA== 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=be/+nImTNLk+zRsBzEXJL2eKm33hVOLCDnl/dLNBn+Y=; b=TKjXoA3/2SBhPOjF3IdgZwWFMlErgS4M4m31P1CfEU2VdcO2/JmN6mfypIe5ZA3YO0 x3p5BAQeKSLYR0sfRMOW+lOnXt/FtGTSJSoWyJ2rmjEeqtlaV8IthS27mhivOfvsFOvD KOozXuGXAINhg9Qjwb+FU3Udd/eVkM+z2Jerox1M1K/QffKSBHusHD721STZ+QG2ZHnP 7WzcWCxTfeXj9GWreyfnHhF/5dydqMvLe4y7VDojG+SlYaAXrKzjBC+zYptwUoWaxOoG tURq6joCpg8d2klJKG2kb4lk4GKvqIyQGlWiYhWq6i/ojz28Is2ohHly1h5e8No5Z3fB ZrEQ== X-Gm-Message-State: APjAAAVzm34SBzNvIUHe59dc6IJQgC16v8pSsPPtXdhWi+aFt98t8EoC 8b0SKhUrEb2o+9hhMBRRDwEIjFQY X-Google-Smtp-Source: APXvYqyu2bxgDvCWKKBtZPwQkpy78ROwDXgrkyahyYFneWV/i5Fiz+XjtIeqcu7HeAz/UciL9eWC2g== X-Received: by 2002:a5d:6748:: with SMTP id l8mr15845492wrw.199.1560078578765; Sun, 09 Jun 2019 04:09:38 -0700 (PDT) Received: from localhost.localdomain (ipbcc063db.dynamic.kabel-deutschland.de. [188.192.99.219]) by smtp.gmail.com with ESMTPSA id e7sm6055079wmd.0.2019.06.09.04.09.37 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 09 Jun 2019 04:09:38 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Jun 2019 13:00:51 +0200 Message-Id: <20190609110053.4012-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190609110053.4012-1-andreas.rheinhardt@gmail.com> References: <20190604111632.GZ3118@michaelspb> <20190609110053.4012-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/5] startcode: Stop overreading 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" Up until now ff_startcode_find_candidate_c could overread; it relied on zero-padding after the buffer in order to function correctly. This has been changed: No overreads occur any more. The ultimate goal behind all this is to create a high-performance function for searching of startcodes that can be applied even in scenarios where the buffer is not padded. Signed-off-by: Andreas Rheinhardt --- libavcodec/startcode.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libavcodec/startcode.c b/libavcodec/startcode.c index 373572365b..8774109df5 100644 --- a/libavcodec/startcode.c +++ b/libavcodec/startcode.c @@ -34,20 +34,23 @@ int ff_startcode_find_candidate_c(const uint8_t *buf, int size) const uint8_t *start = buf, *end = buf + size; #define INITIALIZATION(mod) do { \ - for (; buf < end && (uintptr_t)buf % mod; buf++) \ - if (!*buf) \ - return buf - start; \ + if (mod > end - start) \ + goto near_end; \ + for (; (uintptr_t)buf % mod; buf++) \ + if (!*buf) \ + return buf - start; \ + /* Effective end for MAIN_LOOP in order not to overread. */ \ + end -= mod; \ } while (0) #define READ(bitness) AV_RN ## bitness ## A #define MAIN_LOOP(bitness, mask1, mask2) do { \ - /* we check p < end instead of p + 3 / 7 because it is - * simpler and there must be AV_INPUT_BUFFER_PADDING_SIZE - * bytes at the end. */ \ - for (; buf < end; buf += bitness / 8) \ + for (; buf <= end; buf += bitness / 8) \ if ((~READ(bitness)(buf) & (READ(bitness)(buf) - mask1)) \ & mask2) \ break; \ + /* Revert to the real end. */ \ + end += bitness / 8; \ } while (0) #if HAVE_FAST_64BIT @@ -57,6 +60,8 @@ int ff_startcode_find_candidate_c(const uint8_t *buf, int size) INITIALIZATION(4); MAIN_LOOP(32, 0x01010101U, 0x80808080U); #endif + +near_end: for (; buf < end; buf++) if (!*buf) break;