14eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh/* Copyright (c) 2009, Google Inc.
24eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * All rights reserved.
34eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *
44eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * Redistribution and use in source and binary forms, with or without
54eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * modification, are permitted provided that the following conditions are
64eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * met:
74eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *
84eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *     * Redistributions of source code must retain the above copyright
94eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * notice, this list of conditions and the following disclaimer.
104eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *     * Redistributions in binary form must reproduce the above
114eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * copyright notice, this list of conditions and the following disclaimer
124eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * in the documentation and/or other materials provided with the
134eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * distribution.
144eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *     * Neither the name of Google Inc. nor the names of its
154eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * contributors may be used to endorse or promote products derived from
164eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * this software without specific prior written permission.
174eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *
184eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
204eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
214eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
224eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
234eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
244eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
284eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh *
304eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * ---
314eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh * Author: Craig Silverstein
324eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh */
334eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh
344eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#ifndef _WIN32
358477f3174eb9d2cf4e43af222c7bbb6dfd006827Andreas Schuh#  error You should only be including windows/port.cc in a windows environment!
364eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#endif
374eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh
384eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#include <string.h>    // for strlen(), memset(), memcmp()
394eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#include <assert.h>
404eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#include <stdarg.h>    // for va_list, va_start, va_end
414eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#include <windows.h>
428477f3174eb9d2cf4e43af222c7bbb6dfd006827Andreas Schuh
434eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#include "windows_port.h"
444eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh
454eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh// These call the windows _vsnprintf, but always NUL-terminate.
464eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#if !defined(__MINGW32__) && !defined(__MINGW64__)  /* mingw already defines */
47366e9d335c4b5b12135e73808d6b47ab19dd7379Sergey Sharybin#if !(defined(_MSC_VER) && _MSC_VER >= 1900)  /* msvc 2015 already defines */
488d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh
498d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#ifdef _MSC_VER
508d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#  pragma warning(push)
518d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#  pragma warning(disable: 4996) // ignore _vsnprintf security warning
528d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#endif
534eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuhint safe_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
544eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  if (size == 0)        // not even room for a \0?
554eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh    return -1;          // not what C99 says to do, but what windows does
568d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh  str[size-1] = '\0';
574eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  return _vsnprintf(str, size-1, format, ap);
584eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh}
598d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#ifdef _MSC_VER
608d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#  pragma warning(pop)
618d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh#endif
624eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh
634eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuhint snprintf(char *str, size_t size, const char *format, ...) {
644eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  int r;
654eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  va_list ap;
664eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  va_start(ap, format);
674eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  r = vsnprintf(str, size, format, ap);
684eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  va_end(ap);
694eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh  return r;
704eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh}
718d3797cd15371328c8e0fc5f71060fb9ad4e40c7Andreas Schuh
72366e9d335c4b5b12135e73808d6b47ab19dd7379Sergey Sharybin#endif  /* if !(defined(_MSC_VER) && _MSC_VER >= 1900)  */
734eb2cb223b6c2cbab7093a9fca56732c53ec143aAndreas Schuh#endif  /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */
74