From patchwork Thu Oct 20 14:05:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Sabatini X-Patchwork-Id: 1087 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.133 with SMTP id o127csp662743vsd; Thu, 20 Oct 2016 07:21:12 -0700 (PDT) X-Received: by 10.25.213.204 with SMTP id m195mr356180lfg.47.1476973272019; Thu, 20 Oct 2016 07:21:12 -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 l74si326097lfb.181.2016.10.20.07.21.11; Thu, 20 Oct 2016 07:21:11 -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 6E9B9689C05; Thu, 20 Oct 2016 17:21:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf0-f67.google.com (mail-lf0-f67.google.com [209.85.215.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 27348689BE3 for ; Thu, 20 Oct 2016 17:20:59 +0300 (EEST) Received: by mail-lf0-f67.google.com with SMTP id l131so245016lfl.0 for ; Thu, 20 Oct 2016 07:21:02 -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=LT2OdP5kgnglsW7iboPLKaZe8KYDLqAeMxI3GuULu8Q=; b=CCVGH9POF72ERZf6DWsLJApJG8u5nFJ0YN4F2uGAa8tyjQi8tpOAere/DuyrVq0pOr s0/ktJP8R45pOZqcosZxddllFSCq0quGH4jmTCvqOQumBaaukCqXuHuP4UdeINc4R5DL njBZKuI+Fkz/4pe7/7ejQXEk/t0HRTBa23aiRDkWLFPJc0Uv04RO5CuzuYGRXWBBhe+6 nq186acB0tHkeGqspifTl+P9CMmXtKFv5BZwhfZ4eIIKg85xGOw3T40pTRauBWKUooZy 9mA5LbOfmDjPm8RRCjKQexluOQDSDs3GgxGEyKKrJsJOor/+0/o405oMWHTqIzdGfhOB bZTA== 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=LT2OdP5kgnglsW7iboPLKaZe8KYDLqAeMxI3GuULu8Q=; b=FzNcmFQ1So0mu2uL+Elu0BKlav5DyTcAk5Vy4a9qtGDGGOwx8ooYWCQOpSeem3WmWj MK0oZrs5F0tkCThkzuvT4Rfs9NvuYNuKLSVdjEZZ95htgkmc2YJqSouAo1FXudDZl4+F yiqbjczfSR+arxyGadT1SNCB8UEwnGbpYLFqTveswDBkq6+Xi4zkvYZoCjQ3h6Ly0MOn TyHZOMzjhQ3xavzp23hoYsSQ+ftSnKM7wRQnK2z462hotDx47Wqowpz3mJXL5vpfko9q c5OAc0iKQWrGFjnSNFl/BOrswsqdWXtGOmRF5aIPQKZYHPtK0PuXbt0nrmwncKwwQvfD 8tlA== X-Gm-Message-State: AA6/9RkuWSKgHaX9CWG52II2bE6rjSeJPIzA0oWS6umQl5vOuS2EPKglTKb8ugJqH13oVw== X-Received: by 10.28.148.22 with SMTP id w22mr2722335wmd.42.1476972320331; Thu, 20 Oct 2016 07:05:20 -0700 (PDT) Received: from barisone ([151.56.95.116]) by smtp.gmail.com with ESMTPSA id kq7sm65955750wjb.0.2016.10.20.07.05.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Oct 2016 07:05:19 -0700 (PDT) Received: by barisone (Postfix, from userid 1000) id 966101A896B; Thu, 20 Oct 2016 16:05:08 +0200 (CEST) Date: Thu, 20 Oct 2016 16:05:07 +0200 From: Stefano Sabatini To: FFmpeg development discussions and patches Message-ID: <20161020140506.GX7148@barisone> Mail-Followup-To: FFmpeg development discussions and patches References: <1476380459-3167-1-git-send-email-stefasab@gmail.com> <20161013195919.GV4602@nb4> <20161018113212.GI7148@barisone> <20161018144802.GX4602@nb4> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20161018144802.GX4602@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 Tuesday 2016-10-18 16:48:02 +0200, Michael Niedermayer encoded: > On Tue, Oct 18, 2016 at 01:32:12PM +0200, Stefano Sabatini wrote: [...] > > aviobuf.c | 18 +++++++++++++++--- > > internal.h | 14 ++++++++++++++ > > 2 files changed, 29 insertions(+), 3 deletions(-) > > 19b979c45f087997ac69fba2caf5504c933acfc8 0001-lavf-aviobuf-add-ff_get_line2-variant.patch > > 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; > > > > + while (1) { > > c = avio_r8(s); > > if (c && i < maxlen-1) > > buf[i++] = c; > > + if (c != '\n' && c != '\r' && c) { > > + j++; > > + } else { > > + break; > > + } > > + } > > a string like "\n" > would have a strlen of 1 > but readlen of 0 > while a string like "X" would have a strlen and readlen of 1 > > is this difference intended ? > or maybe i misread the code You're right, I was confused. Updated patch in attachment, thanks. From 54a5d3469d4d9a3f17f45e7577da6c46cb7e969e 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 | 16 +++++++++++++--- libavformat/internal.h | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 134d627..144a617 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -764,18 +764,28 @@ 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; + if (c) { + if (i < maxlen-1) + buf[i++] = c; + j++; + } } while (c != '\n' && c != '\r' && c); 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