1 /*!
2 * \copy
3 * Copyright (c) 2009-2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * \file crt_utils_safe_x.cpp
33 *
34 * \brief common tool/function utilization
35 *
36 * \date 03/10/2009 Created
37 *
38 *************************************************************************************
39 */
40
41 #include <string.h>
42 #include <stdlib.h>
43 #include <math.h>
44 #include <time.h>
45 #if defined(_WIN32)
46 #include <windows.h>
47 #include <sys/types.h>
48 #include <sys/timeb.h>
49 #ifndef _MSC_VER
50 #include <sys/time.h>
51 #endif //!_MSC_VER
52 #else
53 #include <sys/time.h>
54 #endif //_WIN32
55
56 #include "macros.h"
57 #include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms
58
59 #if defined(_WIN32) && defined(_MSC_VER)
60
61 #if defined(_MSC_VER) && (_MSC_VER>=1500)
62
WelsSnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,...)63 int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
64 va_list pArgPtr;
65 int32_t iRc;
66
67 va_start (pArgPtr, kpFormat);
68
69 iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
70 if (iRc < 0)
71 iRc = iSizeOfBuffer;
72
73 va_end (pArgPtr);
74
75 return iRc;
76 }
77
WelsStrncpy(char * pDest,int32_t iSizeInBytes,const char * kpSrc)78 char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
79 strncpy_s (pDest, iSizeInBytes, kpSrc, _TRUNCATE);
80
81 return pDest;
82 }
83
WelsVsnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,va_list pArgPtr)84 int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
85 int32_t iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
86 if (iRc < 0)
87 iRc = iSizeOfBuffer;
88 return iRc;
89 }
90
WelsFopen(const char * kpFilename,const char * kpMode)91 WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
92 WelsFileHandle* pFp = NULL;
93 if (fopen_s (&pFp, kpFilename, kpMode) != 0) {
94 return NULL;
95 }
96
97 return pFp;
98 }
99
WelsFclose(WelsFileHandle * pFp)100 int32_t WelsFclose (WelsFileHandle* pFp) {
101 return fclose (pFp);
102 }
103
WelsGetTimeOfDay(SWelsTime * pTp)104 int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
105 return _ftime_s (pTp);
106 }
107
WelsStrftime(char * pBuffer,int32_t iSize,const char * kpFormat,const SWelsTime * kpTp)108 int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
109 struct tm sTimeNow;
110 int32_t iRc;
111
112 localtime_s (&sTimeNow, &kpTp->time);
113
114 iRc = (int32_t)strftime (pBuffer, iSize, kpFormat, &sTimeNow);
115 if (iRc == 0)
116 pBuffer[0] = '\0';
117 return iRc;
118 }
119
120 #else
121
WelsSnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,...)122 int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
123 va_list pArgPtr;
124 int32_t iRc;
125
126 va_start (pArgPtr, kpFormat);
127
128 iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
129 if (iRc < 0) {
130 pBuffer[iSizeOfBuffer - 1] = '\0';
131 iRc = iSizeOfBuffer;
132 }
133
134 va_end (pArgPtr);
135
136 return iRc;
137 }
138
WelsStrncpy(char * pDest,int32_t iSizeInBytes,const char * kpSrc)139 char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
140 strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
141 pDest[iSizeInBytes - 1] = '\0';
142
143 return pDest;
144 }
145
WelsVsnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,va_list pArgPtr)146 int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
147 int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
148 if (iRc < 0) {
149 pBuffer[iSizeOfBuffer - 1] = '\0';
150 iRc = iSizeOfBuffer;
151 }
152 return iRc;
153 }
154
155
WelsFopen(const char * kpFilename,const char * kpMode)156 WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
157 return fopen (kpFilename, kpMode);
158 }
159
WelsFclose(WelsFileHandle * pFp)160 int32_t WelsFclose (WelsFileHandle* pFp) {
161 return fclose (pFp);
162 }
163
WelsGetTimeOfDay(SWelsTime * pTp)164 int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
165 _ftime (pTp);
166 return 0;
167 }
168
WelsStrftime(char * pBuffer,int32_t iSize,const char * kpFormat,const SWelsTime * kpTp)169 int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
170 struct tm* pTnow;
171 int32_t iRc;
172
173 pTnow = localtime (&kpTp->time);
174
175 iRc = strftime (pBuffer, iSize, kpFormat, pTnow);
176 if (iRc == 0)
177 pBuffer[0] = '\0';
178 return iRc;
179 }
180
181
182 #endif // _MSC_VER
183
184 #else //GCC
185
WelsSnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,...)186 int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
187 va_list pArgPtr;
188 int32_t iRc;
189
190 va_start (pArgPtr, kpFormat);
191
192 iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
193
194 va_end (pArgPtr);
195
196 return iRc;
197 }
198
WelsStrncpy(char * pDest,int32_t iSizeInBytes,const char * kpSrc)199 char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
200 strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
201 pDest[iSizeInBytes - 1] = '\0';
202 return pDest;
203 }
204
WelsVsnprintf(char * pBuffer,int32_t iSizeOfBuffer,const char * kpFormat,va_list pArgPtr)205 int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
206 return vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
207 }
208
WelsFopen(const char * kpFilename,const char * kpMode)209 WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
210 return fopen (kpFilename, kpMode);
211 }
212
WelsFclose(WelsFileHandle * pFp)213 int32_t WelsFclose (WelsFileHandle* pFp) {
214 return fclose (pFp);
215 }
216
WelsGetTimeOfDay(SWelsTime * pTp)217 int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
218 struct timeval sTv;
219
220 if (gettimeofday (&sTv, NULL)) {
221 return -1;
222 }
223
224 pTp->time = sTv.tv_sec;
225 pTp->millitm = (uint16_t)sTv.tv_usec / 1000;
226
227 return 0;
228 }
229
WelsStrftime(char * pBuffer,int32_t iSize,const char * kpFormat,const SWelsTime * kpTp)230 int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
231 struct tm* pTnow;
232 int32_t iRc;
233
234 pTnow = localtime (&kpTp->time);
235
236 iRc = (int32_t) strftime (pBuffer, iSize, kpFormat, pTnow);
237 if (iRc == 0)
238 pBuffer[0] = '\0';
239 return iRc;
240 }
241
242 #endif
243
244
WelsStrcat(char * pDest,uint32_t uiSizeInBytes,const char * kpSrc)245 char* WelsStrcat (char* pDest, uint32_t uiSizeInBytes, const char* kpSrc) {
246 uint32_t uiCurLen = (uint32_t) strlen (pDest);
247 if (uiSizeInBytes > uiCurLen)
248 return WelsStrncpy (pDest + uiCurLen, uiSizeInBytes - uiCurLen, kpSrc);
249 return pDest;
250 }
251
WelsFwrite(const void * kpBuffer,int32_t iSize,int32_t iCount,WelsFileHandle * pFp)252 int32_t WelsFwrite (const void* kpBuffer, int32_t iSize, int32_t iCount, WelsFileHandle* pFp) {
253 return (int32_t)fwrite (kpBuffer, iSize, iCount, pFp);
254 }
255
WelsGetMillisecond(const SWelsTime * kpTp)256 uint16_t WelsGetMillisecond (const SWelsTime* kpTp) {
257 return kpTp->millitm;
258 }
259
WelsFseek(WelsFileHandle * fp,int32_t offset,int32_t origin)260 int32_t WelsFseek (WelsFileHandle* fp, int32_t offset, int32_t origin) {
261 return fseek (fp, offset, origin);
262 }
263
WelsFflush(WelsFileHandle * pFp)264 int32_t WelsFflush (WelsFileHandle* pFp) {
265 return fflush (pFp);
266 }
267