From patchwork Tue Oct 18 11:32:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Sabatini X-Patchwork-Id: 1050 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.133 with SMTP id o127csp655482vsd; Tue, 18 Oct 2016 04:44:10 -0700 (PDT) X-Received: by 10.28.68.213 with SMTP id r204mr382749wma.105.1476791050441; Tue, 18 Oct 2016 04:44:10 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 14si48276568wje.234.2016.10.18.04.44.09; Tue, 18 Oct 2016 04:44:10 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8760F689A9C; Tue, 18 Oct 2016 14:44:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f193.google.com (mail-qk0-f193.google.com [209.85.220.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3C18B6898D3 for ; Tue, 18 Oct 2016 14:43:59 +0300 (EEST) Received: by mail-qk0-f193.google.com with SMTP id n189so16545926qke.1 for ; Tue, 18 Oct 2016 04:44:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=gbS5ZNxkiX1JyfdsA6R7+nThRFvLQEssHrhu8V0VPAo=; b=X9zd0VCM44M7yRFFZiPuuHQvZuZajdxopANien0y9HlI8EatbWaLjJ4q2h0nwiah93 MJWgEL6D9R4XjDmflLDec1xEMUJ0GJW+iIN3pDStotPYgeGD+KUMo9Qxff/yVJmweHtG Z8GoUNytxPgVDud6OKwMFzKYgS88ZSDP2ouoEp8kLlOUDj159+5oEu96rhs5+1R8gZZe 1lYC3mVRPy8bjKknf8pwfwDu3DsmgG9PWj8DDcdOxX5R+z5fLeSVrS5wzT7RDCdRojhi 2Lv1OPPODug6Z8jRnz4m/5OYko7IWbYwme/khxyn/scwnGAFcBBfaUaUfjOwJ764HZum mnbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=gbS5ZNxkiX1JyfdsA6R7+nThRFvLQEssHrhu8V0VPAo=; b=LI+3e7mFfSUXdgLvAp/OqxmiPTuejZ7AU5dkzPemSNz+2lEe2rjmXhc8FU6XLJkYHm GbWVCdjr5O/NIC7Cr69wuwEtWJoSDNchh9m0lIWhYdwC3KrP4hE5N1qmn5mtUu7fa5GN B7HsFtGQBi04vZ9krCgPMWTsUjpHaqSh2fAzz1kC7JsQlfKHY8f1YbM522BF38jVskCb nZmjvkpfL1IVCKyP94d8LoMTcP5T/Ye6AAs/oGsvV65uspK//sP4oPpdN787WG53Jn6T jt6jrMlrtliGhakozq/sbVRgmSIc4D7Wh+aCrapu+t335yXaNbhLx3Ba2rdZx1n5yVVC hKBQ== X-Gm-Message-State: AA6/9Rm3uWTum4VVPEhNnuwa4cau9RXvcni7yJmzBIHuYNYtvTTsYo3zuT07WCJfwuINAA== X-Received: by 10.194.188.37 with SMTP id fx5mr14223356wjc.170.1476790336382; Tue, 18 Oct 2016 04:32:16 -0700 (PDT) Received: from barisone ([151.56.144.129]) by smtp.gmail.com with ESMTPSA id f2sm61952782wjr.2.2016.10.18.04.32.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Oct 2016 04:32:15 -0700 (PDT) Received: by barisone (Postfix, from userid 1000) id 1D2701A896B; Tue, 18 Oct 2016 13:32:13 +0200 (CEST) Date: Tue, 18 Oct 2016 13:32:12 +0200 From: Stefano Sabatini To: FFmpeg development discussions and patches Message-ID: <20161018113212.GI7148@barisone> Mail-Followup-To: FFmpeg development discussions and patches References: <1476380459-3167-1-git-send-email-stefasab@gmail.com> <20161013195919.GV4602@nb4> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20161013195919.GV4602@nb4> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [FFmpeg-devel] [PATCH] lavf/aviobuf: add ff_get_line2() variant 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" On date Thursday 2016-10-13 21:59:19 +0200, Michael Niedermayer encoded: > On Thu, Oct 13, 2016 at 07:40:59PM +0200, Stefano Sabatini wrote: > > This allows to probe if the read line was partially discarded. > > --- > > libavformat/aviobuf.c | 10 +++++++++- > > libavformat/internal.h | 14 ++++++++++++++ > > 2 files changed, 23 insertions(+), 1 deletion(-) > > > > diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c > > index 134d627..28183b4 100644 > > --- a/libavformat/aviobuf.c > > +++ b/libavformat/aviobuf.c > > @@ -764,18 +764,26 @@ unsigned int avio_rb32(AVIOContext *s) > > > > int ff_get_line(AVIOContext *s, char *buf, int maxlen) > > { > > - int i = 0; > > + return ff_get_line2(s, buf, maxlen, NULL); > > +} > > + > > +int ff_get_line2(AVIOContext *s, char *buf, int maxlen, int *readlen) > > +{ > > + int i = 0, j = 0; > > char c; > > > > do { > > c = avio_r8(s); > > if (c && i < maxlen-1) > > buf[i++] = c; > > + j++; > > } while (c != '\n' && c != '\r' && c); > > "\n" and "\0" would i belive both have a j=1 but they would have > differig i > is that intended ? (seems to me that this would make truncation > detection more annoying) > or am i missing something that avoids this difference? Improved version in attachment. From 58c1cad434447d48246e153e3a1a391d72d23c7b Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Thu, 13 Oct 2016 16:36:30 +0200 Subject: [PATCH] lavf/aviobuf: add ff_get_line2() variant This allows to probe if the read line was partially discarded. --- libavformat/aviobuf.c | 18 +++++++++++++++--- libavformat/internal.h | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 134d627..29bcf1e 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -764,18 +764,30 @@ unsigned int avio_rb32(AVIOContext *s) int ff_get_line(AVIOContext *s, char *buf, int maxlen) { - int i = 0; + return ff_get_line2(s, buf, maxlen, NULL); +} + +int ff_get_line2(AVIOContext *s, char *buf, int maxlen, int *readlen) +{ + int i = 0, j = 0; char c; - do { + while (1) { c = avio_r8(s); if (c && i < maxlen-1) buf[i++] = c; - } while (c != '\n' && c != '\r' && c); + if (c != '\n' && c != '\r' && c) { + j++; + } else { + break; + } + } if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s)) avio_skip(s, -1); buf[i] = 0; + if (readlen) + *readlen = j; return i; } diff --git a/libavformat/internal.h b/libavformat/internal.h index 49244fa..fc49571 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -274,6 +274,20 @@ void ff_put_v(AVIOContext *bc, uint64_t val); */ int ff_get_line(AVIOContext *s, char *buf, int maxlen); +/** + * Read a whole line of text from AVIOContext. Stop reading after reaching + * either a \\n, a \\0 or EOF. The returned string is always \\0-terminated, + * and may be truncated if the buffer is too small. + * + * @param s the read-only AVIOContext + * @param buf buffer to store the read line + * @param maxlen size of the buffer + * @param readlen length of the read line, not including the final \\0 + * @return the length of the string written in the buffer, not including the + * final \\0 + */ +int ff_get_line2(AVIOContext *s, char *buf, int maxlen, int *readlen); + #define SPACE_CHARS " \t\r\n" /** -- 1.9.1