RDK Documentation (Open Sourced RDK Components)
safec_lib.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 #ifndef SAFEC_DUMMY_API
22 #include "safe_str_lib.h"
23 #include "safe_mem_lib.h"
24 
25 /* Macro is defined for non clobbering of the safec secure string API strcpy_s & memcpy_s function*/
26 /* strcpy_s overwrites the old value and nulls the dest when encounters an error*/
27 #define STRCPY_S_NOCLOBBER(dst,dmax,src) ((src) ? (strlen(src) < dmax ? strcpy_s(dst,dmax,src) : ESNOSPC):ESNULLP)
28 #define MEMCPY_S_NOCLOBBER(dst,dmax,src,len) ((src) ? (len <= dmax ? memcpy_s(dst,dmax,src,len) : ESNOSPC):ESNULLP)
29 #endif
30 
31 /*
32  * SAFECLIB Error Handling Logging APIs
33  */
34 #define RDK_SAFECLIB_ERR(rc) fprintf(stderr, "safeclib error at rc - %d %s:%s %d",rc, __FILE__, __FUNCTION__, __LINE__)
35 
36 #define ERR_CHK(rc) \
37  if(rc !=EOK) { \
38  RDK_SAFECLIB_ERR(rc); \
39  }
40 
41 #ifdef SAFEC_DUMMY_API
42 #include <string.h>
43 typedef int errno_t;
44 #define EOK 0
45 #define ESNULLP 400 /* null ptr */
46 #define ESLEMAX 403 /* length exceeds RSIZE_MAX */
47 
48 #define strcpy_s(dst,max,src) (src)?((max > strlen(src))?EOK:ESLEMAX):ESNULLP; \
49  if((src) && (max > strlen(src))) strcpy(dst,src);
50 
51 #define strncpy_s(dst,max,src,len) (src)?((len <= max)?EOK:ESLEMAX):ESNULLP; \
52  if(src && (len <= max)) strncpy(dst,src,len);
53 
54 #define memset_s(dst,max_1,c,max) EOK; \
55  memset(dst,c,max);
56 
57 #define strcat_s(dst,max,src) (src)?((max > strlen(src))?EOK:ESLEMAX):ESNULLP; \
58  if((src) && (max > strlen(src))) strcat(dst,src);
59 
60 #define strncat_s(dst,max,src,len) (src)?((len <= max)?EOK:ESLEMAX):ESNULLP; \
61  if(src && (len <= max)) strncat(dst,src,len);
62 
63 #define memcpy_s(dst,max,src,len) (src)?((len <= max)?EOK:ESLEMAX):ESNULLP; \
64  if(src && (len <= max)) memcpy(dst,src,len);
65 
66 #define STRCPY_S_NOCLOBBER(dst,max,src) (src)?((max > strlen(src))?EOK:ESLEMAX):ESNULLP; \
67  if(src && (strlen(src) < max)) strcpy(dst, src);
68 
69 #define MEMCPY_S_NOCLOBBER(dst,max,src,len) (src) ? ((len <= max)?EOK:ESLEMAX):ESNULLP; \
70  if(src && (len <= max)) memcpy(dst, src, len);
71 
72 #define strtok_s(dest, dmax, delim, ptr) strtok_r(dest, delim, ptr)
73 
74 #define sprintf_s( dst, max, fmt, ...) EOK; \
75  sprintf( dst, fmt, ##__VA_ARGS__ );
76 
77 static inline int strcmp_s(const char *dst, int dmax, const char *src, int *r) {
78  if((src == NULL) || (dst == NULL) || (dmax == 0))
79  return ESNULLP;
80 
81  *r = strcmp(dst, src);
82  return EOK;
83 }
84 
85 static inline int strcasecmp_s(const char *dst, int dmax, const char *src, int *r) {
86  if((src == NULL) || (dst == NULL) || (dmax == 0))
87  return ESNULLP;
88 
89  *r = strcasecmp(dst, src);
90  return EOK;
91 }
92 #endif