/*1* C99-compatible snprintf() and vsnprintf() implementations2* Copyright (c) 2012 Ronald S. Bultje <[email protected]>3*4* This file is part of FFmpeg.5*6* FFmpeg is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* 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 of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with FFmpeg; if not, write to the Free Software18* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA19*/2021#include <stdio.h>22#include <stdarg.h>23#include <limits.h>24#include <string.h>2526#include "compat/va_copy.h"27#include "libavutil/error.h"2829#if defined(__MINGW32__)30#define EOVERFLOW EFBIG31#endif3233int avpriv_snprintf(char *s, size_t n, const char *fmt, ...)34{35va_list ap;36int ret;3738va_start(ap, fmt);39ret = avpriv_vsnprintf(s, n, fmt, ap);40va_end(ap);4142return ret;43}4445int avpriv_vsnprintf(char *s, size_t n, const char *fmt,46va_list ap)47{48int ret;49va_list ap_copy;5051if (n == 0)52return _vscprintf(fmt, ap);53else if (n > INT_MAX)54return AVERROR(EOVERFLOW);5556/* we use n - 1 here because if the buffer is not big enough, the MS57* runtime libraries don't add a terminating zero at the end. MSDN58* recommends to provide _snprintf/_vsnprintf() a buffer size that59* is one less than the actual buffer, and zero it before calling60* _snprintf/_vsnprintf() to workaround this problem.61* See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */62memset(s, 0, n);63va_copy(ap_copy, ap);64ret = _vsnprintf(s, n - 1, fmt, ap_copy);65va_end(ap_copy);66if (ret == -1)67ret = _vscprintf(fmt, ap);6869return ret;70}717273