00001 /* 00002 * C99-compatible snprintf() and vsnprintf() implementations 00003 * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include <stdio.h> 00023 #include <stdarg.h> 00024 #include <limits.h> 00025 #include <string.h> 00026 00027 #include "compat/va_copy.h" 00028 #include "libavutil/error.h" 00029 00030 #if defined(__MINGW32__) 00031 #define EOVERFLOW EFBIG 00032 #endif 00033 00034 int avpriv_snprintf(char *s, size_t n, const char *fmt, ...) 00035 { 00036 va_list ap; 00037 int ret; 00038 00039 va_start(ap, fmt); 00040 ret = avpriv_vsnprintf(s, n, fmt, ap); 00041 va_end(ap); 00042 00043 return ret; 00044 } 00045 00046 int avpriv_vsnprintf(char *s, size_t n, const char *fmt, 00047 va_list ap) 00048 { 00049 int ret; 00050 va_list ap_copy; 00051 00052 if (n == 0) 00053 return _vscprintf(fmt, ap); 00054 else if (n > INT_MAX) 00055 return AVERROR(EOVERFLOW); 00056 00057 /* we use n - 1 here because if the buffer is not big enough, the MS 00058 * runtime libraries don't add a terminating zero at the end. MSDN 00059 * recommends to provide _snprintf/_vsnprintf() a buffer size that 00060 * is one less than the actual buffer, and zero it before calling 00061 * _snprintf/_vsnprintf() to workaround this problem. 00062 * See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */ 00063 memset(s, 0, n); 00064 va_copy(ap_copy, ap); 00065 ret = _vsnprintf(s, n - 1, fmt, ap_copy); 00066 va_end(ap_copy); 00067 if (ret == -1) 00068 ret = _vscprintf(fmt, ap); 00069 00070 return ret; 00071 }