LCOV - code coverage report
Current view: top level - libavutil - bprint.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 142 167 85.0 %
Date: 2017-12-14 19:11:59 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Nicolas George
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <stdarg.h>
      22             : #include <stdio.h>
      23             : #include <string.h>
      24             : #include <time.h>
      25             : #include "avassert.h"
      26             : #include "avstring.h"
      27             : #include "bprint.h"
      28             : #include "common.h"
      29             : #include "compat/va_copy.h"
      30             : #include "error.h"
      31             : #include "mem.h"
      32             : 
      33             : #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
      34             : #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
      35             : 
      36        1836 : static int av_bprint_alloc(AVBPrint *buf, unsigned room)
      37             : {
      38             :     char *old_str, *new_str;
      39             :     unsigned min_size, new_size;
      40             : 
      41        1836 :     if (buf->size == buf->size_max)
      42        1058 :         return AVERROR(EIO);
      43         778 :     if (!av_bprint_is_complete(buf))
      44           0 :         return AVERROR_INVALIDDATA; /* it is already truncated anyway */
      45         778 :     min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
      46         778 :     new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
      47         778 :     if (new_size < min_size)
      48         399 :         new_size = FFMIN(buf->size_max, min_size);
      49         778 :     old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
      50         778 :     new_str = av_realloc(old_str, new_size);
      51         778 :     if (!new_str)
      52           0 :         return AVERROR(ENOMEM);
      53         778 :     if (!old_str)
      54         410 :         memcpy(new_str, buf->str, buf->len + 1);
      55         778 :     buf->str  = new_str;
      56         778 :     buf->size = new_size;
      57         778 :     return 0;
      58             : }
      59             : 
      60     3767952 : static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
      61             : {
      62             :     /* arbitrary margin to avoid small overflows */
      63     3767952 :     extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
      64     3767952 :     buf->len += extra_len;
      65     3767952 :     if (buf->size)
      66     3767576 :         buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
      67     3767952 : }
      68             : 
      69     1308870 : void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
      70             : {
      71     1308870 :     unsigned size_auto = (char *)buf + sizeof(*buf) -
      72             :                          buf->reserved_internal_buffer;
      73             : 
      74     1308870 :     if (size_max == 1)
      75      961818 :         size_max = size_auto;
      76     1308870 :     buf->str      = buf->reserved_internal_buffer;
      77     1308870 :     buf->len      = 0;
      78     1308870 :     buf->size     = FFMIN(size_auto, size_max);
      79     1308870 :     buf->size_max = size_max;
      80     1308870 :     *buf->str = 0;
      81     1308870 :     if (size_init > buf->size)
      82         400 :         av_bprint_alloc(buf, size_init - 1);
      83     1308870 : }
      84             : 
      85     2840106 : void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
      86             : {
      87     2840106 :     buf->str      = buffer;
      88     2840106 :     buf->len      = 0;
      89     2840106 :     buf->size     = size;
      90     2840106 :     buf->size_max = size;
      91     2840106 :     *buf->str = 0;
      92     2840106 : }
      93             : 
      94     3218168 : void av_bprintf(AVBPrint *buf, const char *fmt, ...)
      95             : {
      96             :     unsigned room;
      97             :     char *dst;
      98             :     va_list vl;
      99             :     int extra_len;
     100             : 
     101             :     while (1) {
     102     3218180 :         room = av_bprint_room(buf);
     103     3218168 :         dst = room ? buf->str + buf->len : NULL;
     104     3218168 :         va_start(vl, fmt);
     105     3218168 :         extra_len = vsnprintf(dst, room, fmt, vl);
     106     3218168 :         va_end(vl);
     107     3218168 :         if (extra_len <= 0)
     108           9 :             return;
     109     3218159 :         if (extra_len < room)
     110     3217090 :             break;
     111        1069 :         if (av_bprint_alloc(buf, extra_len))
     112        1057 :             break;
     113             :     }
     114     3218147 :     av_bprint_grow(buf, extra_len);
     115             : }
     116             : 
     117      317027 : void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
     118             : {
     119             :     unsigned room;
     120             :     char *dst;
     121             :     int extra_len;
     122             :     va_list vl;
     123             : 
     124             :     while (1) {
     125      317027 :         room = av_bprint_room(buf);
     126      317027 :         dst = room ? buf->str + buf->len : NULL;
     127      317027 :         va_copy(vl, vl_arg);
     128      317027 :         extra_len = vsnprintf(dst, room, fmt, vl);
     129      317027 :         va_end(vl);
     130      317027 :         if (extra_len <= 0)
     131       11032 :             return;
     132      305995 :         if (extra_len < room)
     133      305995 :             break;
     134           0 :         if (av_bprint_alloc(buf, extra_len))
     135           0 :             break;
     136             :     }
     137      305995 :     av_bprint_grow(buf, extra_len);
     138             : }
     139             : 
     140      166889 : void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
     141             : {
     142             :     unsigned room, real_n;
     143             : 
     144             :     while (1) {
     145      166889 :         room = av_bprint_room(buf);
     146      166889 :         if (n < room)
     147      166889 :             break;
     148           0 :         if (av_bprint_alloc(buf, n))
     149           0 :             break;
     150             :     }
     151      166889 :     if (room) {
     152      166889 :         real_n = FFMIN(n, room - 1);
     153      166889 :         memset(buf->str + buf->len, c, real_n);
     154             :     }
     155      166889 :     av_bprint_grow(buf, n);
     156      166889 : }
     157             : 
     158       77284 : void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
     159             : {
     160             :     unsigned room, real_n;
     161             : 
     162             :     while (1) {
     163       77648 :         room = av_bprint_room(buf);
     164       77284 :         if (size < room)
     165       76920 :             break;
     166         364 :         if (av_bprint_alloc(buf, size))
     167           0 :             break;
     168             :     }
     169       76920 :     if (room) {
     170       76920 :         real_n = FFMIN(size, room - 1);
     171       76920 :         memcpy(buf->str + buf->len, data, real_n);
     172             :     }
     173       76920 :     av_bprint_grow(buf, size);
     174       76920 : }
     175             : 
     176           2 : void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
     177             : {
     178             :     unsigned room;
     179             :     size_t l;
     180             : 
     181           2 :     if (!*fmt)
     182           0 :         return;
     183             :     while (1) {
     184           2 :         room = av_bprint_room(buf);
     185           2 :         if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
     186           1 :             break;
     187             :         /* strftime does not tell us how much room it would need: let us
     188             :            retry with twice as much until the buffer is large enough */
     189           1 :         room = !room ? strlen(fmt) + 1 :
     190             :                room <= INT_MAX / 2 ? room * 2 : INT_MAX;
     191           1 :         if (av_bprint_alloc(buf, room)) {
     192             :             /* impossible to grow, try to manage something useful anyway */
     193           1 :             room = av_bprint_room(buf);
     194           1 :             if (room < 1024) {
     195             :                 /* if strftime fails because the buffer has (almost) reached
     196             :                    its maximum size, let us try in a local buffer; 1k should
     197             :                    be enough to format any real date+time string */
     198             :                 char buf2[1024];
     199           1 :                 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
     200           1 :                     av_bprintf(buf, "%s", buf2);
     201           1 :                     return;
     202             :                 }
     203             :             }
     204           0 :             if (room) {
     205             :                 /* if anything else failed and the buffer is not already
     206             :                    truncated, let us add a stock string and force truncation */
     207             :                 static const char txt[] = "[truncated strftime output]";
     208           0 :                 memset(buf->str + buf->len, '!', room);
     209           0 :                 memcpy(buf->str + buf->len, txt, FFMIN(sizeof(txt) - 1, room));
     210           0 :                 av_bprint_grow(buf, room); /* force truncation */
     211             :             }
     212           0 :             return;
     213             :         }
     214             :     }
     215           1 :     av_bprint_grow(buf, l);
     216             : }
     217             : 
     218           3 : void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
     219             :                           unsigned char **mem, unsigned *actual_size)
     220             : {
     221           3 :     if (size > av_bprint_room(buf))
     222           2 :         av_bprint_alloc(buf, size);
     223           3 :     *actual_size = av_bprint_room(buf);
     224           3 :     *mem = *actual_size ? buf->str + buf->len : NULL;
     225           3 : }
     226             : 
     227       25533 : void av_bprint_clear(AVBPrint *buf)
     228             : {
     229       25533 :     if (buf->len) {
     230       10909 :         *buf->str = 0;
     231       10909 :         buf->len  = 0;
     232             :     }
     233       25533 : }
     234             : 
     235      347055 : int av_bprint_finalize(AVBPrint *buf, char **ret_str)
     236             : {
     237      347055 :     unsigned real_size = FFMIN(buf->len + 1, buf->size);
     238             :     char *str;
     239      347055 :     int ret = 0;
     240             : 
     241      347055 :     if (ret_str) {
     242        5406 :         if (av_bprint_is_allocated(buf)) {
     243         369 :             str = av_realloc(buf->str, real_size);
     244         369 :             if (!str)
     245           0 :                 str = buf->str;
     246         369 :             buf->str = NULL;
     247             :         } else {
     248        5037 :             str = av_malloc(real_size);
     249        5037 :             if (str)
     250        5037 :                 memcpy(str, buf->str, real_size);
     251             :             else
     252           0 :                 ret = AVERROR(ENOMEM);
     253             :         }
     254        5406 :         *ret_str = str;
     255             :     } else {
     256      341649 :         if (av_bprint_is_allocated(buf))
     257          44 :             av_freep(&buf->str);
     258             :     }
     259      347055 :     buf->size = real_size;
     260      347055 :     return ret;
     261             : }
     262             : 
     263             : #define WHITESPACES " \n\t\r"
     264             : 
     265         178 : void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
     266             :                       enum AVEscapeMode mode, int flags)
     267             : {
     268         178 :     const char *src0 = src;
     269             : 
     270         178 :     if (mode == AV_ESCAPE_MODE_AUTO)
     271           0 :         mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
     272             : 
     273         178 :     switch (mode) {
     274           0 :     case AV_ESCAPE_MODE_QUOTE:
     275             :         /* enclose the string between '' */
     276           0 :         av_bprint_chars(dstbuf, '\'', 1);
     277           0 :         for (; *src; src++) {
     278           0 :             if (*src == '\'')
     279           0 :                 av_bprintf(dstbuf, "'\\''");
     280             :             else
     281           0 :                 av_bprint_chars(dstbuf, *src, 1);
     282             :         }
     283           0 :         av_bprint_chars(dstbuf, '\'', 1);
     284           0 :         break;
     285             : 
     286             :     /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
     287         178 :     default:
     288             :         /* \-escape characters */
     289         979 :         for (; *src; src++) {
     290         801 :             int is_first_last       = src == src0 || !*(src+1);
     291         801 :             int is_ws               = !!strchr(WHITESPACES, *src);
     292         801 :             int is_strictly_special = special_chars && strchr(special_chars, *src);
     293         801 :             int is_special          =
     294         855 :                 is_strictly_special || strchr("'\\", *src) ||
     295           0 :                 (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
     296             : 
     297        1566 :             if (is_strictly_special ||
     298        1530 :                 (!(flags & AV_ESCAPE_FLAG_STRICT) &&
     299         747 :                  (is_special || (is_ws && is_first_last))))
     300          54 :                 av_bprint_chars(dstbuf, '\\', 1);
     301         801 :             av_bprint_chars(dstbuf, *src, 1);
     302             :         }
     303         178 :         break;
     304             :     }
     305         178 : }

Generated by: LCOV version 1.13