1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "func_wrapper.h"
16 #include "securec.h"
17 #ifdef __cplusplus
18 #if __cplusplus
19 extern "C" {
20 #endif
21 #endif
22
23 // start wrap strdup
24 static StrdupFunc g_strdup = NULL;
UpdateStrdupFunc(StrdupFunc func)25 void UpdateStrdupFunc(StrdupFunc func)
26 {
27 g_strdup = func;
28 }
29
__wrap_strdup(const char * string)30 char* __wrap_strdup(const char* string)
31 {
32 if (g_strdup) {
33 return g_strdup(string);
34 } else {
35 return __real_strdup(string);
36 }
37 }
38
39 // start wrap malloc
40 static MallocFunc g_malloc = NULL;
UpdateMallocFunc(MallocFunc func)41 void UpdateMallocFunc(MallocFunc func)
42 {
43 g_malloc = func;
44 }
45
__wrap_malloc(size_t size)46 void* __wrap_malloc(size_t size)
47 {
48 if (g_malloc) {
49 return g_malloc(size);
50 } else {
51 return __real_malloc(size);
52 }
53 }
54
55 // start wrap strncat_s
56 static StrncatSFunc g_strncat_s = NULL;
UpdateStrncatSFunc(StrncatSFunc func)57 void UpdateStrncatSFunc(StrncatSFunc func)
58 {
59 g_strncat_s = func;
60 }
61
__wrap_strncat_s(char * strDest,size_t destMax,const char * strSrc,size_t count)62 int __wrap_strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
63 {
64 if (g_strncat_s) {
65 return g_strncat_s(strDest, destMax, strSrc, count);
66 } else {
67 return __real_strncat_s(strDest, destMax, strSrc, count);
68 }
69 }
70
71 // start wrap mkdir
72 static MkdirFunc g_mkdir = NULL;
UpdateMkdirFunc(MkdirFunc func)73 void UpdateMkdirFunc(MkdirFunc func)
74 {
75 g_mkdir = func;
76 }
77
__wrap_mkdir(const char * path,mode_t mode)78 int __wrap_mkdir(const char *path, mode_t mode)
79 {
80 if (g_mkdir) {
81 return g_mkdir(path, mode);
82 } else {
83 return __real_mkdir(path, mode);
84 }
85 }
86
87 // start wrap mount
88 static MountFunc g_mount = NULL;
UpdateMountFunc(MountFunc func)89 void UpdateMountFunc(MountFunc func)
90 {
91 g_mount = func;
92 }
93
__wrap_mount(const char * source,const char * target,const char * fsType,unsigned long flags,const void * data)94 int __wrap_mount(const char *source, const char *target,
95 const char *fsType, unsigned long flags, const void *data)
96 {
97 if (g_mount) {
98 return g_mount(source, target, fsType, flags, data);
99 } else {
100 return __real_mount(source, target, fsType, flags, data);
101 }
102 }
103
104 // start wrap stat
105 static StatFunc g_stat = NULL;
UpdateStatFunc(StatFunc func)106 void UpdateStatFunc(StatFunc func)
107 {
108 g_stat = func;
109 }
110
__wrap_stat(const char * pathname,struct stat * buf)111 int __wrap_stat(const char *pathname, struct stat *buf)
112 {
113 if (g_stat) {
114 return g_stat(pathname, buf);
115 } else {
116 return __real_stat(pathname, buf);
117 }
118 }
119
120 // start wrap snprintf_s
121 static SnprintfSFunc g_snprintf_s = NULL;
UpdateSnprintfSFunc(SnprintfSFunc func)122 void UpdateSnprintfSFunc(SnprintfSFunc func)
123 {
124 g_snprintf_s = func;
125 }
126
__wrap_snprintf_s(char * strDest,size_t destMax,size_t count,const char * format,...)127 size_t __wrap_snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...)
128 {
129 va_list args;
130 va_start(args, format);
131 size_t rc;
132 if (g_snprintf_s) {
133 rc = g_snprintf_s(strDest, destMax, count, format, args);
134 } else {
135 rc = vsnprintf_s(strDest, destMax, count, format, args);
136 }
137 va_end(args);
138 return rc;
139 }
140
141 // start wrap open
142 static OpenFunc g_open = NULL;
UpdateOpenFunc(OpenFunc func)143 void UpdateOpenFunc(OpenFunc func)
144 {
145 g_open = func;
146 }
147
__wrap_open(const char * pathname,int flag)148 int __wrap_open(const char *pathname, int flag)
149 {
150 if (g_open) {
151 return g_open(pathname, flag);
152 } else {
153 return __real_open(pathname, flag);
154 }
155 }
156
157 // start wrap close
158 static CloseFunc g_close = NULL;
UpdateCloseFunc(CloseFunc func)159 void UpdateCloseFunc(CloseFunc func)
160 {
161 g_close = func;
162 }
163
__wrap_close(int fd)164 int __wrap_close(int fd)
165 {
166 if (g_close) {
167 return g_close(fd);
168 } else {
169 return __real_close(fd);
170 }
171 }
172
173 // start wrap strcpy_s
174 static StrcpySFunc g_strcpy_s = NULL;
UpdateStrcpySFunc(StrcpySFunc func)175 void UpdateStrcpySFunc(StrcpySFunc func)
176 {
177 g_strcpy_s = func;
178 }
179
__wrap_strcpy_s(char * dest,size_t destMax,const char * src)180 int __wrap_strcpy_s(char *dest, size_t destMax, const char *src)
181 {
182 if (g_strcpy_s) {
183 return g_strcpy_s(dest, destMax, src);
184 } else {
185 return __real_strcpy_s(dest, destMax, src);
186 }
187 }
188
189 // start wrap ioctl
190 static IoctlFunc g_ioctl = NULL;
UpdateIoctlFunc(IoctlFunc func)191 void UpdateIoctlFunc(IoctlFunc func)
192 {
193 g_ioctl = func;
194 }
195
__wrap_ioctl(int fd,int req,...)196 int __wrap_ioctl(int fd, int req, ...)
197 {
198 va_list args;
199 va_start(args, req);
200 int rc;
201 if (g_ioctl) {
202 rc = g_ioctl(fd, req, args);
203 } else {
204 rc = __real_ioctl(fd, req, args);
205 }
206 va_end(args);
207 return rc;
208 }
209
210 // start wrap calloc
211 static CallocFunc g_calloc = NULL;
UpdateCallocFunc(CallocFunc func)212 void UpdateCallocFunc(CallocFunc func)
213 {
214 g_calloc = func;
215 }
216
__wrap_calloc(size_t m,size_t n)217 void* __wrap_calloc(size_t m, size_t n)
218 {
219 if (g_calloc) {
220 return g_calloc(m, n);
221 } else {
222 return __real_calloc(m, n);
223 }
224 }
225
226 // start wrap minor
227 static MinorFunc g_minor = NULL;
UpdateMinorFunc(MinorFunc func)228 void UpdateMinorFunc(MinorFunc func)
229 {
230 g_minor = func;
231 }
232
__wrap_minor(dev_t dev)233 int __wrap_minor(dev_t dev)
234 {
235 if (g_minor) {
236 return g_minor(dev);
237 } else {
238 return __real_minor(dev);
239 }
240 }
241
242 // start wrap memset_s
243 static MemsetSFunc g_memset_s = NULL;
UpdateMemsetSFunc(MemsetSFunc func)244 void UpdateMemsetSFunc(MemsetSFunc func)
245 {
246 g_memset_s = func;
247 }
248
__wrap_memset_s(void * dest,size_t destMax,int c,size_t count)249 int __wrap_memset_s(void *dest, size_t destMax, int c, size_t count)
250 {
251 if (g_memset_s) {
252 return g_memset_s(dest, destMax, c, count);
253 } else {
254 return __real_memset_s(dest, destMax, c, count);
255 }
256 }
257
258 // start wrap memcpy_s
259 static MemcpySFunc g_memcpy_s = NULL;
UpdateMemcpySFunc(MemcpySFunc func)260 void UpdateMemcpySFunc(MemcpySFunc func)
261 {
262 g_memcpy_s = func;
263 }
264
__wrap_memcpy_s(void * dest,size_t destMax,const void * src,size_t count)265 int __wrap_memcpy_s(void *dest, size_t destMax, const void *src, size_t count)
266 {
267 if (g_memcpy_s) {
268 return g_memcpy_s(dest, destMax, src, count);
269 } else {
270 return __real_memcpy_s(dest, destMax, src, count);
271 }
272 }
273
274 #ifdef __cplusplus
275 #if __cplusplus
276 }
277 #endif
278 #endif
279