shithub: openh264

Download patch

ref: 2f5f25b7e3cb1ae137750f97a1161128663c8a4f
parent: 55336dee6437d8224f80cb320d03651791cadbc9
author: Martin Storsjö <martin@martin.st>
date: Sun Jan 26 08:31:56 EST 2014

Merge the encoder/decoder crt_util_safe_x.h into the common lib

--- a/codec/build/win32/dec/WelsDecCore.vcproj
+++ b/codec/build/win32/dec/WelsDecCore.vcproj
@@ -891,6 +891,10 @@
 					>
 				</File>
 				<File
+					RelativePath="..\..\..\common\crt_util_safe_x.cpp"
+					>
+				</File>
+				<File
 					RelativePath="..\..\..\decoder\core\src\deblocking.cpp"
 					>
 				</File>
--- a/codec/build/win32/enc/WelsEncCore.vcproj
+++ b/codec/build/win32/enc/WelsEncCore.vcproj
@@ -400,6 +400,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\common\crt_util_safe_x.cpp"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\encoder\core\src\deblocking.cpp"
 				>
 				<FileConfiguration
--- /dev/null
+++ b/codec/common/crt_util_safe_x.cpp
@@ -1,0 +1,258 @@
+/*!
+ * \copy
+ *     Copyright (c)  2009-2013, Cisco Systems
+ *     All rights reserved.
+ *
+ *     Redistribution and use in source and binary forms, with or without
+ *     modification, are permitted provided that the following conditions
+ *     are met:
+ *
+ *        * Redistributions of source code must retain the above copyright
+ *          notice, this list of conditions and the following disclaimer.
+ *
+ *        * Redistributions in binary form must reproduce the above copyright
+ *          notice, this list of conditions and the following disclaimer in
+ *          the documentation and/or other materials provided with the
+ *          distribution.
+ *
+ *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *     POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * \file	crt_utils_safe_x.cpp
+ *
+ * \brief	common tool/function utilization
+ *
+ * \date	03/10/2009 Created
+ *
+ *************************************************************************************
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+#include <time.h>
+#if defined(_WIN32)
+#include <windows.h>
+#include <sys/types.h>
+#include <sys/timeb.h>
+#ifndef _MSC_VER
+#include <sys/time.h>
+#ifndef HAVE_STRNLEN
+#define strnlen(a,b) strlen(a)
+#endif //!HAVE_STRNLEN
+#endif //!_MSC_VER
+#else
+#include <sys/time.h>
+#include <sys/timeb.h>
+#endif //_WIN32
+
+#include "macros.h"
+#include "crt_util_safe_x.h"	// Safe CRT routines like utils for cross platforms
+
+#if  defined(_WIN32) && defined(_MSC_VER)
+
+#if  defined(_MSC_VER) && (_MSC_VER>=1500)
+
+int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
+  va_list  pArgPtr;
+  int32_t  iRc;
+
+  va_start (pArgPtr, kpFormat);
+
+  iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
+
+  va_end (pArgPtr);
+
+  return iRc;
+}
+
+str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
+  strncpy_s (pDest, iSizeInBytes, kpSrc, iCount);
+
+  return pDest;
+}
+
+int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
+  return strnlen_s (kpStr, iMaxlen);
+}
+
+int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
+  return vsprintf_s (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
+}
+
+WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
+  WelsFileHandle* pFp = NULL;
+  if (fopen_s (&pFp, kpFilename, kpMode) != 0) {
+    return NULL;
+  }
+
+  return pFp;
+}
+
+int32_t WelsFclose (WelsFileHandle* pFp) {
+  return fclose (pFp);
+}
+
+int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
+  return _ftime_s (pTp);
+}
+
+int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
+  struct tm   sTimeNow;
+
+  localtime_s (&sTimeNow, &kpTp->time);
+
+  return strftime (pBuffer, iSize, kpFormat, &sTimeNow);
+}
+
+#else
+
+int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
+  va_list pArgPtr;
+  int32_t iRc;
+
+  va_start (pArgPtr, kpFormat);
+
+  iRc = vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
+
+  va_end (pArgPtr);
+
+  return iRc;
+}
+
+str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
+  strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
+
+  return pDest;
+}
+
+int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
+  return strlen (kpStr); //confirmed_safe_unsafe_usage
+}
+
+int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
+  return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
+}
+
+
+WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
+  return fopen (kpFilename, kpMode);
+}
+
+int32_t WelsFclose (WelsFileHandle* pFp) {
+  return fclose (pFp);
+}
+
+int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
+  _ftime (pTp);
+  return 0;
+}
+
+int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
+  struct tm*   pTnow;
+
+  pTnow = localtime (&kpTp->time);
+
+  return strftime (pBuffer, iSize, kpFormat, pTnow);
+}
+
+
+#endif // _MSC_VER
+
+#else  //GCC
+
+int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
+  va_list pArgPtr;
+  int32_t iRc;
+
+  va_start (pArgPtr, kpFormat);
+
+  iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
+
+  va_end (pArgPtr);
+
+  return iRc;
+}
+
+str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
+  return strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
+}
+
+#if !defined(MACOS) && !defined(UNIX) && !defined(APPLE_IOS)
+int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
+  return strnlen (kpStr, iMaxlen); //confirmed_safe_unsafe_usage
+}
+#else
+int32_t WelsStrnlen (const str_t* kpString, int32_t iMaxlen) {
+  // In mac os, there is no strnlen in string.h, we can only use strlen instead of strnlen or
+  // implement strnlen by ourself
+
+#if 1
+  return strlen (kpString); //confirmed_safe_unsafe_usage
+#else
+  const str_t* kpSrc;
+  for (kpSrc = kpString; iMaxlen-- && *kpSrc != '\0'; ++kpSrc)
+    return kpSrc - kpString;
+#endif
+
+}
+#endif
+
+int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
+  return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
+}
+
+WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
+  return fopen (kpFilename, kpMode);
+}
+
+int32_t WelsFclose (WelsFileHandle*   pFp) {
+  return fclose (pFp);
+}
+
+int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
+  struct timeval  sTv;
+
+  if (gettimeofday (&sTv, NULL)) {
+    return -1;
+  }
+
+  pTp->time = sTv.tv_sec;
+  pTp->millitm = (uint16_t)sTv.tv_usec / 1000;
+
+  return 0;
+}
+
+int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
+  struct tm*   pTnow;
+
+  pTnow = localtime (&kpTp->time);
+
+  return strftime (pBuffer, iSize, kpFormat, pTnow);
+}
+
+#endif
+
+
+int32_t WelsFwrite (const void_t* kpBuffer, int32_t iSize, int32_t iCount, WelsFileHandle* pFp) {
+  return fwrite (kpBuffer, iSize, iCount, pFp);
+}
+
+uint16_t WelsGetMillsecond (const SWelsTime* kpTp) {
+  return kpTp->millitm;
+}
+
+int32_t WelsFflush (WelsFileHandle* pFp) {
+  return fflush (pFp);
+}
--- /dev/null
+++ b/codec/common/crt_util_safe_x.h
@@ -1,0 +1,454 @@
+/*!
+ * \copy
+ *     Copyright (c)  2010-2013, Cisco Systems
+ *     All rights reserved.
+ *
+ *     Redistribution and use in source and binary forms, with or without
+ *     modification, are permitted provided that the following conditions
+ *     are met:
+ *
+ *        * Redistributions of source code must retain the above copyright
+ *          notice, this list of conditions and the following disclaimer.
+ *
+ *        * Redistributions in binary form must reproduce the above copyright
+ *          notice, this list of conditions and the following disclaimer in
+ *          the documentation and/or other materials provided with the
+ *          distribution.
+ *
+ *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *     POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * \file	crt_util_safe_x.h
+ *
+ * \brief	Safe CRT like util for cross platfroms support
+ *
+ * \date	06/04/2010 Created
+ *
+ *************************************************************************************
+ */
+#ifndef WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
+#define WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+
+#if defined(_WIN32)
+#include <windows.h>
+#include <sys/types.h>
+#include <sys/timeb.h>
+#else
+#include <sys/timeb.h>
+#include <sys/time.h>
+#include "typedefs.h"
+#endif//_WIN32
+
+#include "typedefs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define     WELS_FILE_SEEK_SET           SEEK_SET
+#define     WELS_FILE_SEEK_CUR           SEEK_CUR
+#define     WESL_FILE_SEEK_END           SEEK_END
+
+typedef      FILE  WelsFileHandle;
+
+#ifdef _WIN32
+typedef      struct _timeb     SWelsTime;
+#else
+typedef      struct timeb      SWelsTime;
+#endif
+
+int32_t   WelsSnprintf (str_t* buffer,  int32_t sizeOfBuffer,  const str_t* format, ...);
+str_t*   WelsStrncpy (str_t* dest, int32_t sizeInBytes, const str_t* src, int32_t count);
+str_t*   WelsStrcat (str_t* dest, int32_t sizeInBytes, str_t* src);
+int32_t   WelsStrnlen (const str_t* str,  int32_t maxlen);
+int32_t   WelsVsprintf (str_t* buffer, int32_t sizeOfBuffer, const str_t* format, va_list argptr);
+
+WelsFileHandle*        WelsFopen (const str_t* filename,  const str_t* mode);
+int32_t                WelsFclose (WelsFileHandle*   fp);
+int32_t                WelsFread (void* buffer, int32_t size, int32_t count, WelsFileHandle* fp);
+int32_t                WelsFwrite (const void* buffer, int32_t size, int32_t count, WelsFileHandle* fp);
+int32_t                WelsFseek (WelsFileHandle* fp, int32_t offset, int32_t origin);
+int32_t                WelsFflush (WelsFileHandle* fp);
+
+int32_t                WelsGetTimeOfDay (SWelsTime* tp);
+int32_t                WelsStrftime (str_t* buffer, int32_t size, const str_t* format, const SWelsTime* tp);
+uint16_t               WelsGetMillsecond (const SWelsTime* tp);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+
+/*
+ * Safe Lib specific errno codes.  These can be added to the errno.h file
+ * if desired.
+ */
+#define ESNULLP         ( 400 )       /* null ptr                    */
+#define ESZEROL         ( 401 )       /* length is zero              */
+#define ESLEMIN         ( 402 )       /* length is below min         */
+#define ESLEMAX         ( 403 )       /* length exceeds max          */
+#define ESOVRLP         ( 404 )       /* overlap undefined           */
+#define ESEMPTY         ( 405 )       /* empty string                */
+#define ESNOSPC         ( 406 )       /* not enough space for s2     */
+#define ESUNTERM        ( 407 )       /* unterminated string         */
+#define ESNODIFF        ( 408 )       /* no difference               */
+#define ESNOTFND        ( 409 )       /* not found                   */
+
+/* EOK may or may not be defined in errno.h */
+#ifndef EOK
+#define EOK   0
+#endif
+
+#if (defined(WIN32) && defined(_MSC_VER) && (_MSC_VER<1500)) || defined(__GNUC__)
+
+static __inline int wels_strncpy_s (char* dest, int dmax, const char* src, int slen) {
+int orig_dmax;
+char* orig_dest;
+const char* overlap_bumper;
+
+if (dest == NULL) {
+//        invoke_safe_lib_constraint_handler("strncpy_s: dest is null",
+//                   NULL, ESNULLP);
+  return (ESNULLP);
+}
+
+if (dmax <= 0) {
+//        invoke_safe_lib_constraint_handler("strncpy_s: dmax is 0",
+//                   NULL, ESZEROL);
+  return (ESZEROL);
+}
+
+//    if (dmax > RSIZE_MAX_STR) {
+//        invoke_safe_lib_constraint_handler("strncpy_s: dmax exceeds max",
+//                   NULL, ESLEMAX);
+//        return (ESLEMAX);
+//    }
+
+if (src == NULL) {
+//        handle_error(orig_dest, orig_dmax, "strncpy_s: src is null", ESNULLP);
+  return (ESNULLP);
+}
+
+if (slen <= 0) {
+//        handle_error(orig_dest, orig_dmax, "strncpy_s: slen is zero", ESZEROL);
+  return (ESZEROL);
+}
+
+//    if (slen > RSIZE_MAX_STR) {
+//        handle_error(orig_dest, orig_dmax, "strncpy_s: slen exceeds max", ESLEMAX);
+//        return (ESLEMAX);
+//    }
+
+/* hold base in case src was not copied */
+orig_dmax = dmax;
+orig_dest = dest;
+
+if (dest < src) {
+  overlap_bumper = src;
+
+  while (dmax > 0) {
+    if (dest == overlap_bumper) {
+//                handle_error(orig_dest, orig_dmax, "strncpy_s: overlapping objects", ESOVRLP);
+      return (ESOVRLP);
+    }
+
+    if (slen == 0) {
+      /*
+       * Copying truncated to slen chars.  Note that the TR says to
+       * copy slen chars plus the null char.  We null the slack.
+       */
+#ifdef SAFE_LIB_STR_NULL_SLACK
+      while (dmax) {
+        *dest = '\0';
+        dmax--;
+        dest++;
+      }
+#else
+      *dest = '\0';
+#endif
+      return (EOK);
+    }
+
+    *dest = *src;
+    if (*dest == '\0') {
+#ifdef SAFE_LIB_STR_NULL_SLACK
+      /* null slack */
+      while (dmax) {
+        *dest = '\0';
+        dmax--;
+        dest++;
+      }
+#endif
+      return (EOK);
+    }
+
+    dmax--;
+    slen--;
+    dest++;
+    src++;
+  }
+
+} else {
+  overlap_bumper = dest;
+
+  while (dmax > 0) {
+    if (src == overlap_bumper) {
+//                handle_error(orig_dest, orig_dmax, "strncpy_s: overlapping objects", ESOVRLP);
+      return (ESOVRLP);
+    }
+
+    if (slen == 0) {
+      /*
+       * Copying truncated to slen chars.  Note that the TR says to
+       * copy slen chars plus the null char.  We null the slack.
+       */
+#ifdef SAFE_LIB_STR_NULL_SLACK
+      while (dmax) {
+        *dest = '\0';
+        dmax--;
+        dest++;
+      }
+#else
+      *dest = '\0';
+#endif
+      return (EOK);
+    }
+
+    *dest = *src;
+    if (*dest == '\0') {
+#ifdef SAFE_LIB_STR_NULL_SLACK
+      /* null slack */
+      while (dmax) {
+        *dest = '\0';
+        dmax--;
+        dest++;
+      }
+#endif
+      return (EOK);
+    }
+
+    dmax--;
+    slen--;
+    dest++;
+    src++;
+  }
+}
+
+/*
+ * the entire src was not copied, so zero the string
+ */
+//    handle_error(orig_dest, orig_dmax, "strncpy_s: not enough space for src", ESNOSPC);
+return (ESNOSPC);
+}
+
+static __inline int wels_strcat_s (char* dest, int dmax, const char* src) {
+  int orig_dmax;
+  char* orig_dest;
+  const char* overlap_bumper;
+
+  if (dest == NULL) {
+//        invoke_safe_lib_constraint_handler("strcat_s: dest is null",
+//                   NULL, ESNULLP);
+    return (ESNULLP);
+  }
+
+  if (src == NULL) {
+//        invoke_safe_lib_constraint_handler("strcat_s: src is null",
+//                   NULL, ESNULLP);
+    return (ESNULLP);
+  }
+
+  if (dmax <= 0) {
+//        invoke_safe_lib_constraint_handler("strcat_s: dmax is 0",
+//                   NULL, ESZEROL);
+    return (ESZEROL);
+  }
+
+//    if (dmax > RSIZE_MAX_STR) {
+//        invoke_safe_lib_constraint_handler("strcat_s: dmax exceeds max",
+//                   NULL, ESLEMAX);
+//        return (ESLEMAX);
+//    }
+
+  /* hold base of dest in case src was not copied */
+  orig_dmax = dmax;
+  orig_dest = dest;
+
+  if (dest < src) {
+    overlap_bumper = src;
+
+    /* Find the end of dest */
+    while (*dest != '\0') {
+
+      if (dest == overlap_bumper) {
+//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
+        return (ESOVRLP);
+      }
+
+      dest++;
+      dmax--;
+      if (dmax == 0) {
+//                handle_error(orig_dest, orig_dmax, "strcat_s: dest unterminated", ESUNTERM);
+        return (ESUNTERM);
+      }
+    }
+
+    while (dmax > 0) {
+      if (dest == overlap_bumper) {
+//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
+        return (ESOVRLP);
+      }
+
+      *dest = *src;
+      if (*dest == '\0') {
+#ifdef SAFE_LIB_STR_NULL_SLACK
+        /* null slack to clear any data */
+        while (dmax) {
+          *dest = '\0';
+          dmax--;
+          dest++;
+        }
+#endif
+        return (EOK);
+      }
+
+      dmax--;
+      dest++;
+      src++;
+    }
+
+  } else {
+    overlap_bumper = dest;
+
+    /* Find the end of dest */
+    while (*dest != '\0') {
+
+      /*
+       * NOTE: no need to check for overlap here since src comes first
+       * in memory and we're not incrementing src here.
+       */
+      dest++;
+      dmax--;
+      if (dmax == 0) {
+//                handle_error(orig_dest, orig_dmax, "strcat_s: dest unterminated", ESUNTERM);
+        return (ESUNTERM);
+      }
+    }
+
+    while (dmax > 0) {
+      if (src == overlap_bumper) {
+//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
+        return (ESOVRLP);
+      }
+
+      *dest = *src;
+      if (*dest == '\0') {
+#ifdef SAFE_LIB_STR_NULL_SLACK
+        /* null slack to clear any data */
+        while (dmax) {
+          *dest = '\0';
+          dmax--;
+          dest++;
+        }
+#endif
+        return (EOK);
+      }
+
+      dmax--;
+      dest++;
+      src++;
+    }
+  }
+
+  /*
+   * the entire src was not copied, so null the string
+   */
+//    handle_error(orig_dest, orig_dmax, "strcat_s: not enough space for src", ESNOSPC);
+
+  return (ESNOSPC);
+}
+
+static __inline int wels_strnlen_s (const char* dest, int dmax) {
+  int count;
+
+  if (dest == NULL) {
+    return (0);
+  }
+
+  if (dmax <= 0) {
+//        invoke_safe_lib_constraint_handler("strnlen_s: dmax is 0",
+//                   NULL, ESZEROL);
+    return (0);
+  }
+
+//    if (dmax > RSIZE_MAX_STR) {
+//        invoke_safe_lib_constraint_handler("strnlen_s: dmax exceeds max",
+//                   NULL, ESLEMAX);
+//        return (0);
+//    }
+
+  count = 0;
+  while (*dest && dmax) {
+    count++;
+    dmax--;
+    dest++;
+  }
+
+  return (count);
+}
+
+#endif//(WIN32 && _MSC_VER && _MSC_VER<1500) || __GNUC__
+
+#if defined(WIN32) && defined(_MSC_VER)
+#if _MSC_VER >= 1500	// VS2008
+#define SNPRINTF	_snprintf_s
+#define LOCALTIME	localtime_s
+#define FTIME		_ftime_s
+#define STRNCPY		strncpy_s
+#define STRCAT		strcat_s
+#define STRNLEN		strnlen_s
+#define VSPRINTF	vsprintf_s
+#define FOPEN		fopen_s
+#else	// mainly for VC6
+#define SNPRINTF	_snprintf
+#define LOCALTIME	localtime
+#define FTIME		_ftime
+#define STRNCPY		wels_strncpy_s	// override s.t.r.n.c.p.y here for safe
+#define STRCAT		wels_strcat_s	// override s.t.r.c.a.t here for safe
+#define STRNLEN		wels_strnlen_s	// override s.t.r.n.l.e.n here for safe
+#define VSPRINTF	vsprintf
+#define FOPEN		fopen
+#endif//_MSC_VER >= 1500
+
+#else//__GNUC__
+
+#define SNPRINTF	snprintf
+#define LOCALTIME	localtime
+#define STRNCPY		wels_strncpy_s	// override s.t.r.n.c.p.y here for safe
+#define STRCAT		wels_strcat_s	// override s.t.r.c.a.t here for safe
+#define STRNLEN		wels_strnlen_s	// override s.t.r.n.l.e.n here for safe
+#define VSPRINTF	vsprintf
+#define FOPEN		fopen
+
+#endif//WIN32
+
+#endif//WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
--- a/codec/common/targets.mk
+++ b/codec/common/targets.mk
@@ -2,6 +2,7 @@
 COMMON_SRCDIR=codec/common
 COMMON_CPP_SRCS=\
 	$(COMMON_SRCDIR)/./cpu.cpp\
+	$(COMMON_SRCDIR)/./crt_util_safe_x.cpp\
 	$(COMMON_SRCDIR)/./deblocking_common.cpp\
 	$(COMMON_SRCDIR)/./logging.cpp\
 	$(COMMON_SRCDIR)/./WelsThreadLib.cpp\
--- a/codec/decoder/core/inc/crt_util_safe_x.h
+++ /dev/null
@@ -1,104 +1,0 @@
-/*!
- * \copy
- *     Copyright (c)  2010-2013, Cisco Systems
- *     All rights reserved.
- *
- *     Redistribution and use in source and binary forms, with or without
- *     modification, are permitted provided that the following conditions
- *     are met:
- *
- *        * Redistributions of source code must retain the above copyright
- *          notice, this list of conditions and the following disclaimer.
- *
- *        * Redistributions in binary form must reproduce the above copyright
- *          notice, this list of conditions and the following disclaimer in
- *          the documentation and/or other materials provided with the
- *          distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- *     POSSIBILITY OF SUCH DAMAGE.
- *
- *
- * \file	crt_util_safe_x.h
- *
- * \brief	Safe CRT like util for cross platfroms support
- *
- * \date	06/04/2010 Created
- *
- *************************************************************************************
- */
-#ifndef WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
-#define WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <math.h>
-#include <time.h>
-
-#if defined(_WIN32)
-#include <windows.h>
-#include <sys/types.h>
-#include <sys/timeb.h>
-#else
-#include <sys/timeb.h>
-#include <sys/time.h>
-#include "typedefs.h"
-#endif//_WIN32
-
-#include "typedefs.h"
-
-namespace WelsDec {
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#define     WELS_FILE_SEEK_SET           SEEK_SET
-#define     WELS_FILE_SEEK_CUR           SEEK_CUR
-#define     WESL_FILE_SEEK_END           SEEK_END
-
-typedef      FILE  WelsFileHandle;
-
-#ifdef _WIN32
-typedef      struct _timeb     SWelsTime;
-#else
-typedef      struct timeb      SWelsTime;
-#endif
-
-int32_t   WelsSnprintf (str_t* buffer,  int32_t sizeOfBuffer,  const str_t* format, ...);
-str_t*   WelsStrncpy (str_t* dest, int32_t sizeInBytes, const str_t* src, int32_t count);
-str_t*   WelsStrcat (str_t* dest, int32_t sizeInBytes, str_t* src);
-int32_t   WelsStrnlen (const str_t* str,  int32_t maxlen);
-int32_t   WelsVsprintf (str_t* buffer, int32_t sizeOfBuffer, const str_t* format, va_list argptr);
-
-WelsFileHandle*        WelsFopen (const str_t* filename,  const str_t* mode);
-int32_t                WelsFclose (WelsFileHandle*   fp);
-int32_t                WelsFread (void* buffer, int32_t size, int32_t count, WelsFileHandle* fp);
-int32_t                WelsFwrite (const void* buffer, int32_t size, int32_t count, WelsFileHandle* fp);
-int32_t                WelsFseek (WelsFileHandle* fp, int32_t offset, int32_t origin);
-int32_t                WelsFflush (WelsFileHandle* fp);
-
-int32_t                WelsGetTimeOfDay (SWelsTime* tp);
-int32_t                WelsStrftime (str_t* buffer, int32_t size, const str_t* format, const SWelsTime* tp);
-uint16_t               WelsGetMillsecond (const SWelsTime* tp);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-} // namespace WelsDec
-
-#endif//WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
--- a/codec/decoder/core/src/utils.cpp
+++ b/codec/decoder/core/src/utils.cpp
@@ -38,32 +38,8 @@
  *************************************************************************************
  */
 
-#include <string.h>
-#include <stdlib.h>
-#include <math.h>
-#include <time.h>
-#if defined(_WIN32)
-#include <windows.h>
-#include <sys/types.h>
-#include <sys/timeb.h>
-#ifndef _MSC_VER
-#include <sys/time.h>
-#ifndef HAVE_STRNLEN
-#define strnlen(a,b) strlen(a)
-#endif //!HAVE_STRNLEN
-#endif //!_MSC_VER
-#else
-#include <sys/time.h>
-#include <sys/timeb.h>
-#endif //_WIN32
-
 #include "utils.h"
-#include "macros.h"
-#include "wels_const.h"
-#include "cpu_core.h"
 #include "decoder_context.h"
-#include "crt_util_safe_x.h"	// Safe CRT routines like utils for cross platforms
-#include "mem_align.h"
 
 namespace WelsDec {
 
@@ -80,204 +56,6 @@
   va_start (pVl, kpFmt);
   g_pLog (pCtx->pTraceHandle, iLevel, kpFmt, pVl);
   va_end (pVl);
-}
-
-
-#if  defined(_WIN32) && defined(_MSC_VER)
-
-#if  defined(_MSC_VER) && (_MSC_VER>=1500)
-
-int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
-  va_list  pArgPtr;
-  int32_t  iRc;
-
-  va_start (pArgPtr, kpFormat);
-
-  iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
-
-  va_end (pArgPtr);
-
-  return iRc;
-}
-
-str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
-  strncpy_s (pDest, iSizeInBytes, kpSrc, iCount);
-
-  return pDest;
-}
-
-int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
-  return strnlen_s (kpStr, iMaxlen);
-}
-
-int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
-  return vsprintf_s (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
-}
-
-WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
-  WelsFileHandle* pFp = NULL;
-  if (fopen_s (&pFp, kpFilename, kpMode) != 0) {
-    return NULL;
-  }
-
-  return pFp;
-}
-
-int32_t WelsFclose (WelsFileHandle* pFp) {
-  return fclose (pFp);
-}
-
-int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
-  return _ftime_s (pTp);
-}
-
-int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
-  struct tm   sTimeNow;
-
-  localtime_s (&sTimeNow, &kpTp->time);
-
-  return strftime (pBuffer, iSize, kpFormat, &sTimeNow);
-}
-
-#else
-
-int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
-  va_list pArgPtr;
-  int32_t iRc;
-
-  va_start (pArgPtr, kpFormat);
-
-  iRc = vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
-
-  va_end (pArgPtr);
-
-  return iRc;
-}
-
-str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
-  strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
-
-  return pDest;
-}
-
-int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
-  return strlen (kpStr); //confirmed_safe_unsafe_usage
-}
-
-int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
-  return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
-}
-
-
-WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
-  return fopen (kpFilename, kpMode);
-}
-
-int32_t WelsFclose (WelsFileHandle* pFp) {
-  return fclose (pFp);
-}
-
-int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
-  _ftime (pTp);
-  return 0;
-}
-
-int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
-  struct tm*   pTnow;
-
-  pTnow = localtime (&kpTp->time);
-
-  return strftime (pBuffer, iSize, kpFormat, pTnow);
-}
-
-
-#endif // _MSC_VER
-
-#else  //GCC
-
-int32_t WelsSnprintf (str_t* pBuffer,  int32_t iSizeOfBuffer, const str_t* kpFormat, ...) {
-  va_list pArgPtr;
-  int32_t iRc;
-
-  va_start (pArgPtr, kpFormat);
-
-  iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
-
-  va_end (pArgPtr);
-
-  return iRc;
-}
-
-str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
-  return strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
-}
-
-#if !defined(MACOS) && !defined(UNIX) && !defined(APPLE_IOS)
-int32_t WelsStrnlen (const str_t* kpStr,  int32_t iMaxlen) {
-  return strnlen (kpStr, iMaxlen); //confirmed_safe_unsafe_usage
-}
-#else
-int32_t WelsStrnlen (const str_t* kpString, int32_t iMaxlen) {
-  // In mac os, there is no strnlen in string.h, we can only use strlen instead of strnlen or
-  // implement strnlen by ourself
-
-#if 1
-  return strlen (kpString); //confirmed_safe_unsafe_usage
-#else
-  const str_t* kpSrc;
-  for (kpSrc = kpString; iMaxlen-- && *kpSrc != '\0'; ++kpSrc)
-    return kpSrc - kpString;
-#endif
-
-}
-#endif
-
-int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
-  return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
-}
-
-WelsFileHandle* WelsFopen (const str_t* kpFilename,  const str_t* kpMode) {
-  return fopen (kpFilename, kpMode);
-}
-
-int32_t WelsFclose (WelsFileHandle*   pFp) {
-  return fclose (pFp);
-}
-
-int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
-  struct timeval  sTv;
-
-  if (gettimeofday (&sTv, NULL)) {
-    return -1;
-  }
-
-  pTp->time = sTv.tv_sec;
-  pTp->millitm = (uint16_t)sTv.tv_usec / 1000;
-
-  return 0;
-}
-
-int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
-  struct tm*   pTnow;
-
-  pTnow = localtime (&kpTp->time);
-
-  return strftime (pBuffer, iSize, kpFormat, pTnow);
-}
-
-#endif
-
-
-int32_t WelsFwrite (const void_t* kpBuffer, int32_t iSize, int32_t iCount, WelsFileHandle* pFp) {
-  return fwrite (kpBuffer, iSize, iCount, pFp);
-}
-
-uint16_t WelsGetMillsecond (const SWelsTime* kpTp) {
-  return kpTp->millitm;
-}
-
-int32_t WelsFflush (WelsFileHandle* pFp) {
-  return fflush (pFp);
 }
 
 } // namespace WelsDec
--- a/codec/encoder/core/inc/crt_util_safe_x.h
+++ /dev/null
@@ -1,411 +1,0 @@
-/*!
- * \copy
- *     Copyright (c)  2010-2013, Cisco Systems
- *     All rights reserved.
- *
- *     Redistribution and use in source and binary forms, with or without
- *     modification, are permitted provided that the following conditions
- *     are met:
- *
- *        * Redistributions of source code must retain the above copyright
- *          notice, this list of conditions and the following disclaimer.
- *
- *        * Redistributions in binary form must reproduce the above copyright
- *          notice, this list of conditions and the following disclaimer in
- *          the documentation and/or other materials provided with the
- *          distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- *     POSSIBILITY OF SUCH DAMAGE.
- *
- *
- * \file	crt_util_safe_x.h
- *
- * \brief	Safe CRT like util for cross platfroms support
- *
- * \date	06/04/2010 Created
- *
- *************************************************************************************
- */
-#ifndef WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
-#define WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <math.h>
-
-#include <time.h>
-#if defined(_WIN32)
-#include <windows.h>
-#include <sys/types.h>
-#include <sys/timeb.h>
-#else
-#include <sys/time.h>
-#include "typedefs.h"
-#endif//WIN32
-
-/*
- * Safe Lib specific errno codes.  These can be added to the errno.h file
- * if desired.
- */
-#define ESNULLP         ( 400 )       /* null ptr                    */
-#define ESZEROL         ( 401 )       /* length is zero              */
-#define ESLEMIN         ( 402 )       /* length is below min         */
-#define ESLEMAX         ( 403 )       /* length exceeds max          */
-#define ESOVRLP         ( 404 )       /* overlap undefined           */
-#define ESEMPTY         ( 405 )       /* empty string                */
-#define ESNOSPC         ( 406 )       /* not enough space for s2     */
-#define ESUNTERM        ( 407 )       /* unterminated string         */
-#define ESNODIFF        ( 408 )       /* no difference               */
-#define ESNOTFND        ( 409 )       /* not found                   */
-
-/* EOK may or may not be defined in errno.h */
-#ifndef EOK
-#define EOK   0
-#endif
-
-#if (defined(WIN32) && defined(_MSC_VER) && (_MSC_VER<1500)) || defined(__GNUC__)
-
-static __inline int wels_strncpy_s (char* dest, int dmax, const char* src, int slen) {
-int orig_dmax;
-char* orig_dest;
-const char* overlap_bumper;
-
-if (dest == NULL) {
-//        invoke_safe_lib_constraint_handler("strncpy_s: dest is null",
-//                   NULL, ESNULLP);
-  return (ESNULLP);
-}
-
-if (dmax <= 0) {
-//        invoke_safe_lib_constraint_handler("strncpy_s: dmax is 0",
-//                   NULL, ESZEROL);
-  return (ESZEROL);
-}
-
-//    if (dmax > RSIZE_MAX_STR) {
-//        invoke_safe_lib_constraint_handler("strncpy_s: dmax exceeds max",
-//                   NULL, ESLEMAX);
-//        return (ESLEMAX);
-//    }
-
-if (src == NULL) {
-//        handle_error(orig_dest, orig_dmax, "strncpy_s: src is null", ESNULLP);
-  return (ESNULLP);
-}
-
-if (slen <= 0) {
-//        handle_error(orig_dest, orig_dmax, "strncpy_s: slen is zero", ESZEROL);
-  return (ESZEROL);
-}
-
-//    if (slen > RSIZE_MAX_STR) {
-//        handle_error(orig_dest, orig_dmax, "strncpy_s: slen exceeds max", ESLEMAX);
-//        return (ESLEMAX);
-//    }
-
-/* hold base in case src was not copied */
-orig_dmax = dmax;
-orig_dest = dest;
-
-if (dest < src) {
-  overlap_bumper = src;
-
-  while (dmax > 0) {
-    if (dest == overlap_bumper) {
-//                handle_error(orig_dest, orig_dmax, "strncpy_s: overlapping objects", ESOVRLP);
-      return (ESOVRLP);
-    }
-
-    if (slen == 0) {
-      /*
-       * Copying truncated to slen chars.  Note that the TR says to
-       * copy slen chars plus the null char.  We null the slack.
-       */
-#ifdef SAFE_LIB_STR_NULL_SLACK
-      while (dmax) {
-        *dest = '\0';
-        dmax--;
-        dest++;
-      }
-#else
-      *dest = '\0';
-#endif
-      return (EOK);
-    }
-
-    *dest = *src;
-    if (*dest == '\0') {
-#ifdef SAFE_LIB_STR_NULL_SLACK
-      /* null slack */
-      while (dmax) {
-        *dest = '\0';
-        dmax--;
-        dest++;
-      }
-#endif
-      return (EOK);
-    }
-
-    dmax--;
-    slen--;
-    dest++;
-    src++;
-  }
-
-} else {
-  overlap_bumper = dest;
-
-  while (dmax > 0) {
-    if (src == overlap_bumper) {
-//                handle_error(orig_dest, orig_dmax, "strncpy_s: overlapping objects", ESOVRLP);
-      return (ESOVRLP);
-    }
-
-    if (slen == 0) {
-      /*
-       * Copying truncated to slen chars.  Note that the TR says to
-       * copy slen chars plus the null char.  We null the slack.
-       */
-#ifdef SAFE_LIB_STR_NULL_SLACK
-      while (dmax) {
-        *dest = '\0';
-        dmax--;
-        dest++;
-      }
-#else
-      *dest = '\0';
-#endif
-      return (EOK);
-    }
-
-    *dest = *src;
-    if (*dest == '\0') {
-#ifdef SAFE_LIB_STR_NULL_SLACK
-      /* null slack */
-      while (dmax) {
-        *dest = '\0';
-        dmax--;
-        dest++;
-      }
-#endif
-      return (EOK);
-    }
-
-    dmax--;
-    slen--;
-    dest++;
-    src++;
-  }
-}
-
-/*
- * the entire src was not copied, so zero the string
- */
-//    handle_error(orig_dest, orig_dmax, "strncpy_s: not enough space for src", ESNOSPC);
-return (ESNOSPC);
-}
-
-static __inline int wels_strcat_s (char* dest, int dmax, const char* src) {
-  int orig_dmax;
-  char* orig_dest;
-  const char* overlap_bumper;
-
-  if (dest == NULL) {
-//        invoke_safe_lib_constraint_handler("strcat_s: dest is null",
-//                   NULL, ESNULLP);
-    return (ESNULLP);
-  }
-
-  if (src == NULL) {
-//        invoke_safe_lib_constraint_handler("strcat_s: src is null",
-//                   NULL, ESNULLP);
-    return (ESNULLP);
-  }
-
-  if (dmax <= 0) {
-//        invoke_safe_lib_constraint_handler("strcat_s: dmax is 0",
-//                   NULL, ESZEROL);
-    return (ESZEROL);
-  }
-
-//    if (dmax > RSIZE_MAX_STR) {
-//        invoke_safe_lib_constraint_handler("strcat_s: dmax exceeds max",
-//                   NULL, ESLEMAX);
-//        return (ESLEMAX);
-//    }
-
-  /* hold base of dest in case src was not copied */
-  orig_dmax = dmax;
-  orig_dest = dest;
-
-  if (dest < src) {
-    overlap_bumper = src;
-
-    /* Find the end of dest */
-    while (*dest != '\0') {
-
-      if (dest == overlap_bumper) {
-//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
-        return (ESOVRLP);
-      }
-
-      dest++;
-      dmax--;
-      if (dmax == 0) {
-//                handle_error(orig_dest, orig_dmax, "strcat_s: dest unterminated", ESUNTERM);
-        return (ESUNTERM);
-      }
-    }
-
-    while (dmax > 0) {
-      if (dest == overlap_bumper) {
-//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
-        return (ESOVRLP);
-      }
-
-      *dest = *src;
-      if (*dest == '\0') {
-#ifdef SAFE_LIB_STR_NULL_SLACK
-        /* null slack to clear any data */
-        while (dmax) {
-          *dest = '\0';
-          dmax--;
-          dest++;
-        }
-#endif
-        return (EOK);
-      }
-
-      dmax--;
-      dest++;
-      src++;
-    }
-
-  } else {
-    overlap_bumper = dest;
-
-    /* Find the end of dest */
-    while (*dest != '\0') {
-
-      /*
-       * NOTE: no need to check for overlap here since src comes first
-       * in memory and we're not incrementing src here.
-       */
-      dest++;
-      dmax--;
-      if (dmax == 0) {
-//                handle_error(orig_dest, orig_dmax, "strcat_s: dest unterminated", ESUNTERM);
-        return (ESUNTERM);
-      }
-    }
-
-    while (dmax > 0) {
-      if (src == overlap_bumper) {
-//                handle_error(orig_dest, orig_dmax, "strcat_s: overlapping objects", ESOVRLP);
-        return (ESOVRLP);
-      }
-
-      *dest = *src;
-      if (*dest == '\0') {
-#ifdef SAFE_LIB_STR_NULL_SLACK
-        /* null slack to clear any data */
-        while (dmax) {
-          *dest = '\0';
-          dmax--;
-          dest++;
-        }
-#endif
-        return (EOK);
-      }
-
-      dmax--;
-      dest++;
-      src++;
-    }
-  }
-
-  /*
-   * the entire src was not copied, so null the string
-   */
-//    handle_error(orig_dest, orig_dmax, "strcat_s: not enough space for src", ESNOSPC);
-
-  return (ESNOSPC);
-}
-
-static __inline int wels_strnlen_s (const char* dest, int dmax) {
-  int count;
-
-  if (dest == NULL) {
-    return (0);
-  }
-
-  if (dmax <= 0) {
-//        invoke_safe_lib_constraint_handler("strnlen_s: dmax is 0",
-//                   NULL, ESZEROL);
-    return (0);
-  }
-
-//    if (dmax > RSIZE_MAX_STR) {
-//        invoke_safe_lib_constraint_handler("strnlen_s: dmax exceeds max",
-//                   NULL, ESLEMAX);
-//        return (0);
-//    }
-
-  count = 0;
-  while (*dest && dmax) {
-    count++;
-    dmax--;
-    dest++;
-  }
-
-  return (count);
-}
-
-#endif//(WIN32 && _MSC_VER && _MSC_VER<1500) || __GNUC__
-
-#if defined(WIN32) && defined(_MSC_VER)
-#if _MSC_VER >= 1500	// VS2008
-#define SNPRINTF	_snprintf_s
-#define LOCALTIME	localtime_s
-#define FTIME		_ftime_s
-#define STRNCPY		strncpy_s
-#define STRCAT		strcat_s
-#define STRNLEN		strnlen_s
-#define VSPRINTF	vsprintf_s
-#define FOPEN		fopen_s
-#else	// mainly for VC6
-#define SNPRINTF	_snprintf
-#define LOCALTIME	localtime
-#define FTIME		_ftime
-#define STRNCPY		wels_strncpy_s	// override s.t.r.n.c.p.y here for safe
-#define STRCAT		wels_strcat_s	// override s.t.r.c.a.t here for safe
-#define STRNLEN		wels_strnlen_s	// override s.t.r.n.l.e.n here for safe
-#define VSPRINTF	vsprintf
-#define FOPEN		fopen
-#endif//_MSC_VER >= 1500
-
-#else//__GNUC__
-
-#define SNPRINTF	snprintf
-#define LOCALTIME	localtime
-#define STRNCPY		wels_strncpy_s	// override s.t.r.n.c.p.y here for safe
-#define STRCAT		wels_strcat_s	// override s.t.r.c.a.t here for safe
-#define STRNLEN		wels_strnlen_s	// override s.t.r.n.l.e.n here for safe
-#define VSPRINTF	vsprintf
-#define FOPEN		fopen
-
-#endif//WIN32
-
-#endif//WELS_CRT_UTIL_SAFE_CROSS_PLATFORMS_H__