yading@10: /* yading@10: * C99-compatible snprintf() and vsnprintf() implementations yading@10: * Copyright (c) 2012 Ronald S. Bultje yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: #include "compat/va_copy.h" yading@10: #include "libavutil/error.h" yading@10: yading@10: #if defined(__MINGW32__) yading@10: #define EOVERFLOW EFBIG yading@10: #endif yading@10: yading@10: int avpriv_snprintf(char *s, size_t n, const char *fmt, ...) yading@10: { yading@10: va_list ap; yading@10: int ret; yading@10: yading@10: va_start(ap, fmt); yading@10: ret = avpriv_vsnprintf(s, n, fmt, ap); yading@10: va_end(ap); yading@10: yading@10: return ret; yading@10: } yading@10: yading@10: int avpriv_vsnprintf(char *s, size_t n, const char *fmt, yading@10: va_list ap) yading@10: { yading@10: int ret; yading@10: va_list ap_copy; yading@10: yading@10: if (n == 0) yading@10: return _vscprintf(fmt, ap); yading@10: else if (n > INT_MAX) yading@10: return AVERROR(EOVERFLOW); yading@10: yading@10: /* we use n - 1 here because if the buffer is not big enough, the MS yading@10: * runtime libraries don't add a terminating zero at the end. MSDN yading@10: * recommends to provide _snprintf/_vsnprintf() a buffer size that yading@10: * is one less than the actual buffer, and zero it before calling yading@10: * _snprintf/_vsnprintf() to workaround this problem. yading@10: * See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */ yading@10: memset(s, 0, n); yading@10: va_copy(ap_copy, ap); yading@10: ret = _vsnprintf(s, n - 1, fmt, ap_copy); yading@10: va_end(ap_copy); yading@10: if (ret == -1) yading@10: ret = _vscprintf(fmt, ap); yading@10: yading@10: return ret; yading@10: }