1 /*
2  * OS specific functions
3  * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef OS_H
10 #define OS_H
11 
12 #if defined(__ZEPHYR__)
13 #ifndef FILE
14 typedef __FILE FILE;
15 #endif
16 #endif
17 typedef long os_time_t;
18 
19 /**
20  * os_sleep - Sleep (sec, usec)
21  * @sec: Number of seconds to sleep
22  * @usec: Number of microseconds to sleep
23  */
24 void os_sleep(os_time_t sec, os_time_t usec);
25 
26 struct os_time {
27 	os_time_t sec;
28 	os_time_t usec;
29 };
30 
31 struct os_reltime {
32 	os_time_t sec;
33 	os_time_t usec;
34 };
35 
36 /**
37  * os_get_time - Get current time (sec, usec)
38  * @t: Pointer to buffer for the time
39  * Returns: 0 on success, -1 on failure
40  */
41 int os_get_time(struct os_time *t);
42 
43 /**
44  * os_get_reltime - Get relative time (sec, usec)
45  * @t: Pointer to buffer for the time
46  * Returns: 0 on success, -1 on failure
47  */
48 int os_get_reltime(struct os_reltime *t);
49 
50 
51 /* Helpers for handling struct os_time */
52 
os_time_before(struct os_time * a,struct os_time * b)53 static inline int os_time_before(struct os_time *a, struct os_time *b)
54 {
55 	return (a->sec < b->sec) ||
56 	       (a->sec == b->sec && a->usec < b->usec);
57 }
58 
59 
os_time_sub(struct os_time * a,struct os_time * b,struct os_time * res)60 static inline void os_time_sub(struct os_time *a, struct os_time *b,
61 			       struct os_time *res)
62 {
63 	res->sec = a->sec - b->sec;
64 	res->usec = a->usec - b->usec;
65 	if (res->usec < 0) {
66 		res->sec--;
67 		res->usec += 1000000;
68 	}
69 }
70 
71 
72 /* Helpers for handling struct os_reltime */
73 
os_reltime_before(struct os_reltime * a,struct os_reltime * b)74 static inline int os_reltime_before(struct os_reltime *a,
75 				    struct os_reltime *b)
76 {
77 	return (a->sec < b->sec) ||
78 	       (a->sec == b->sec && a->usec < b->usec);
79 }
80 
81 
os_reltime_sub(struct os_reltime * a,struct os_reltime * b,struct os_reltime * res)82 static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
83 				  struct os_reltime *res)
84 {
85 	res->sec = a->sec - b->sec;
86 	res->usec = a->usec - b->usec;
87 	if (res->usec < 0) {
88 		res->sec--;
89 		res->usec += 1000000;
90 	}
91 }
92 
93 
os_reltime_age(struct os_reltime * start,struct os_reltime * age)94 static inline void os_reltime_age(struct os_reltime *start,
95 				  struct os_reltime *age)
96 {
97 	struct os_reltime now;
98 
99 	os_get_reltime(&now);
100 	os_reltime_sub(&now, start, age);
101 }
102 
103 
os_reltime_expired(struct os_reltime * now,struct os_reltime * ts,os_time_t timeout_secs)104 static inline int os_reltime_expired(struct os_reltime *now,
105 				     struct os_reltime *ts,
106 				     os_time_t timeout_secs)
107 {
108 	struct os_reltime age;
109 
110 	os_reltime_sub(now, ts, &age);
111 	return (age.sec > timeout_secs) ||
112 	       (age.sec == timeout_secs && age.usec > 0);
113 }
114 
115 
os_reltime_add_ms(struct os_reltime * ts,int ms)116 static inline void os_reltime_add_ms(struct os_reltime *ts, int ms)
117 {
118 	ts->usec += ms * 1000;
119 	while (ts->usec >= 1000000) {
120 		ts->sec++;
121 		ts->usec -= 1000000;
122 	}
123 	while (ts->usec < 0) {
124 		ts->sec--;
125 		ts->usec += 1000000;
126 	}
127 }
128 
129 
os_reltime_in_ms(struct os_reltime * ts)130 static inline int os_reltime_in_ms(struct os_reltime *ts)
131 {
132 	return ts->sec * 1000 + ts->usec / 1000;
133 }
134 
135 
os_reltime_initialized(struct os_reltime * t)136 static inline int os_reltime_initialized(struct os_reltime *t)
137 {
138 	return t->sec != 0 || t->usec != 0;
139 }
140 
141 
142 /**
143  * os_mktime - Convert broken-down time into seconds since 1970-01-01
144  * @year: Four digit year
145  * @month: Month (1 .. 12)
146  * @day: Day of month (1 .. 31)
147  * @hour: Hour (0 .. 23)
148  * @min: Minute (0 .. 59)
149  * @sec: Second (0 .. 60)
150  * @t: Buffer for returning calendar time representation (seconds since
151  * 1970-01-01 00:00:00)
152  * Returns: 0 on success, -1 on failure
153  *
154  * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
155  * which is used by POSIX mktime().
156  */
157 int os_mktime(int year, int month, int day, int hour, int min, int sec,
158 	      os_time_t *t);
159 
160 struct os_tm {
161 	int sec; /* 0..59 or 60 for leap seconds */
162 	int min; /* 0..59 */
163 	int hour; /* 0..23 */
164 	int day; /* 1..31 */
165 	int month; /* 1..12 */
166 	int year; /* Four digit year */
167 };
168 
169 int os_gmtime(os_time_t t, struct os_tm *tm);
170 
171 /**
172  * os_daemonize - Run in the background (detach from the controlling terminal)
173  * @pid_file: File name to write the process ID to or %NULL to skip this
174  * Returns: 0 on success, -1 on failure
175  */
176 int os_daemonize(const char *pid_file);
177 
178 /**
179  * os_daemonize_terminate - Stop running in the background (remove pid file)
180  * @pid_file: File name to write the process ID to or %NULL to skip this
181  */
182 void os_daemonize_terminate(const char *pid_file);
183 
184 /**
185  * os_get_random - Get cryptographically strong pseudo random data
186  * @buf: Buffer for pseudo random data
187  * @len: Length of the buffer
188  * Returns: 0 on success, -1 on failure
189  */
190 int os_get_random(unsigned char *buf, size_t len);
191 
192 /**
193  * os_random - Get pseudo random value (not necessarily very strong)
194  * Returns: Pseudo random value
195  */
196 unsigned long os_random(void);
197 
198 /**
199  * os_rel2abs_path - Get an absolute path for a file
200  * @rel_path: Relative path to a file
201  * Returns: Absolute path for the file or %NULL on failure
202  *
203  * This function tries to convert a relative path of a file to an absolute path
204  * in order for the file to be found even if current working directory has
205  * changed. The returned value is allocated and caller is responsible for
206  * freeing it. It is acceptable to just return the same path in an allocated
207  * buffer, e.g., return strdup(rel_path). This function is only used to find
208  * configuration files when os_daemonize() may have changed the current working
209  * directory and relative path would be pointing to a different location.
210  */
211 char * os_rel2abs_path(const char *rel_path);
212 
213 /**
214  * os_program_init - Program initialization (called at start)
215  * Returns: 0 on success, -1 on failure
216  *
217  * This function is called when a programs starts. If there are any OS specific
218  * processing that is needed, it can be placed here. It is also acceptable to
219  * just return 0 if not special processing is needed.
220  */
221 int os_program_init(void);
222 
223 /**
224  * os_program_deinit - Program deinitialization (called just before exit)
225  *
226  * This function is called just before a program exists. If there are any OS
227  * specific processing, e.g., freeing resourced allocated in os_program_init(),
228  * it should be done here. It is also acceptable for this function to do
229  * nothing.
230  */
231 void os_program_deinit(void);
232 
233 /**
234  * os_setenv - Set environment variable
235  * @name: Name of the variable
236  * @value: Value to set to the variable
237  * @overwrite: Whether existing variable should be overwritten
238  * Returns: 0 on success, -1 on error
239  *
240  * This function is only used for wpa_cli action scripts. OS wrapper does not
241  * need to implement this if such functionality is not needed.
242  */
243 int os_setenv(const char *name, const char *value, int overwrite);
244 
245 /**
246  * os_unsetenv - Delete environent variable
247  * @name: Name of the variable
248  * Returns: 0 on success, -1 on error
249  *
250  * This function is only used for wpa_cli action scripts. OS wrapper does not
251  * need to implement this if such functionality is not needed.
252  */
253 int os_unsetenv(const char *name);
254 
255 /**
256  * os_readfile - Read a file to an allocated memory buffer
257  * @name: Name of the file to read
258  * @len: For returning the length of the allocated buffer
259  * Returns: Pointer to the allocated buffer or %NULL on failure
260  *
261  * This function allocates memory and reads the given file to this buffer. Both
262  * binary and text files can be read with this function. The caller is
263  * responsible for freeing the returned buffer with os_free().
264  */
265 char * os_readfile(const char *name, size_t *len);
266 
267 /**
268  * os_file_exists - Check whether the specified file exists
269  * @fname: Path and name of the file
270  * Returns: 1 if the file exists or 0 if not
271  */
272 int os_file_exists(const char *fname);
273 
274 /**
275  * os_fdatasync - Sync a file's (for a given stream) state with storage device
276  * @stream: the stream to be flushed
277  * Returns: 0 if the operation succeeded or -1 on failure
278  */
279 int os_fdatasync(FILE *stream);
280 #if defined(__ZEPHYR__)
281 /**
282  * os_malloc - Allocate dynamic memory
283  * @size: Size of the buffer to allocate
284  * Returns: Allocated buffer or %NULL on failure
285  *
286  * Caller is responsible for freeing the returned buffer with os_free().
287  */
288 void *os_malloc(size_t size);
289 
290 /**
291  * os_realloc - Re-allocate dynamic memory
292  * @ptr: Old buffer from os_malloc() or os_realloc()
293  * @size: Size of the new buffer
294  * Returns: Allocated buffer or %NULL on failure
295  *
296  * Caller is responsible for freeing the returned buffer with os_free().
297  * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
298  * not freed and caller is still responsible for freeing it.
299  */
300 void *os_realloc(void *ptr, size_t size);
301 
302 /**
303  * os_free - Free dynamic memory
304  * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
305  */
306 void os_free(void *ptr);
307 #endif
308 
309 /**
310  * os_zalloc - Allocate and zero memory
311  * @size: Number of bytes to allocate
312  * Returns: Pointer to allocated and zeroed memory or %NULL on failure
313  *
314  * Caller is responsible for freeing the returned buffer with os_free().
315  */
316 void * os_zalloc(size_t size);
317 
318 /**
319  * os_calloc - Allocate and zero memory for an array
320  * @nmemb: Number of members in the array
321  * @size: Number of bytes in each member
322  * Returns: Pointer to allocated and zeroed memory or %NULL on failure
323  *
324  * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
325  * allocation is used for an array. The main benefit over os_zalloc() is in
326  * having an extra check to catch integer overflows in multiplication.
327  *
328  * Caller is responsible for freeing the returned buffer with os_free().
329  */
os_calloc(size_t nmemb,size_t size)330 static inline void * os_calloc(size_t nmemb, size_t size)
331 {
332 	if (size && nmemb > (~(size_t) 0) / size)
333 		return NULL;
334 	return os_zalloc(nmemb * size);
335 }
336 
337 
338 /*
339  * The following functions are wrapper for standard ANSI C or POSIX functions.
340  * By default, they are just defined to use the standard function name and no
341  * os_*.c implementation is needed for them. This avoids extra function calls
342  * by allowing the C pre-processor take care of the function name mapping.
343  *
344  * If the target system uses a C library that does not provide these functions,
345  * build_config.h can be used to define the wrappers to use a different
346  * function name. This can be done on function-by-function basis since the
347  * defines here are only used if build_config.h does not define the os_* name.
348  * If needed, os_*.c file can be used to implement the functions that are not
349  * included in the C library on the target system. Alternatively,
350  * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
351  * these functions need to be implemented in os_*.c file for the target system.
352  */
353 
354 #ifdef __ZEPHYR__
355 /**
356  * os_strdup - Duplicate a string
357  * @s: Source string
358  * Returns: Allocated buffer with the string copied into it or %NULL on failure
359  *
360  * Caller is responsible for freeing the returned buffer with os_free().
361  */
362 char *os_strdup(const char *s);
363 
364 /**
365  * os_strcasecmp - Compare two strings ignoring case
366  * @s1: First string
367  * @s2: Second string
368  * Returns: An integer less than, equal to, or greater than zero if s1 is
369  * found to be less than, to match, or be greatred than s2
370  */
371 int os_strcasecmp(const char *s1, const char *s2);
372 
373 /**
374  * os_strncasecmp - Compare two strings ignoring case
375  * @s1: First string
376  * @s2: Second string
377  * @n: Maximum numbers of characters to compare
378  * Returns: An integer less than, equal to, or greater than zero if s1 is
379  * found to be less than, to match, or be greater than s2. Only first n
380  * characters will be compared.
381  */
382 int os_strncasecmp(const char *s1, const char *s2, size_t n);
383 
384 #define CONFIG_BSS_MAX_IDLE_TIME CONFIG_WIFI_NM_WPA_SUPPLICANT_BSS_MAX_IDLE_TIME
385 #endif
386 
387 
388 #ifdef OS_NO_C_LIB_DEFINES
389 
390 /**
391  * os_malloc - Allocate dynamic memory
392  * @size: Size of the buffer to allocate
393  * Returns: Allocated buffer or %NULL on failure
394  *
395  * Caller is responsible for freeing the returned buffer with os_free().
396  */
397 void * os_malloc(size_t size);
398 
399 /**
400  * os_realloc - Re-allocate dynamic memory
401  * @ptr: Old buffer from os_malloc() or os_realloc()
402  * @size: Size of the new buffer
403  * Returns: Allocated buffer or %NULL on failure
404  *
405  * Caller is responsible for freeing the returned buffer with os_free().
406  * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
407  * not freed and caller is still responsible for freeing it.
408  */
409 void * os_realloc(void *ptr, size_t size);
410 
411 /**
412  * os_free - Free dynamic memory
413  * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
414  */
415 void os_free(void *ptr);
416 
417 /**
418  * os_memcpy - Copy memory area
419  * @dest: Destination
420  * @src: Source
421  * @n: Number of bytes to copy
422  * Returns: dest
423  *
424  * The memory areas src and dst must not overlap. os_memmove() can be used with
425  * overlapping memory.
426  */
427 void * os_memcpy(void *dest, const void *src, size_t n);
428 
429 /**
430  * os_memmove - Copy memory area
431  * @dest: Destination
432  * @src: Source
433  * @n: Number of bytes to copy
434  * Returns: dest
435  *
436  * The memory areas src and dst may overlap.
437  */
438 void * os_memmove(void *dest, const void *src, size_t n);
439 
440 /**
441  * os_memset - Fill memory with a constant byte
442  * @s: Memory area to be filled
443  * @c: Constant byte
444  * @n: Number of bytes started from s to fill with c
445  * Returns: s
446  */
447 void * os_memset(void *s, int c, size_t n);
448 
449 /**
450  * os_memcmp - Compare memory areas
451  * @s1: First buffer
452  * @s2: Second buffer
453  * @n: Maximum numbers of octets to compare
454  * Returns: An integer less than, equal to, or greater than zero if s1 is
455  * found to be less than, to match, or be greater than s2. Only first n
456  * characters will be compared.
457  */
458 int os_memcmp(const void *s1, const void *s2, size_t n);
459 
460 /**
461  * os_strdup - Duplicate a string
462  * @s: Source string
463  * Returns: Allocated buffer with the string copied into it or %NULL on failure
464  *
465  * Caller is responsible for freeing the returned buffer with os_free().
466  */
467 char * os_strdup(const char *s);
468 
469 /**
470  * os_strlen - Calculate the length of a string
471  * @s: '\0' terminated string
472  * Returns: Number of characters in s (not counting the '\0' terminator)
473  */
474 size_t os_strlen(const char *s);
475 
476 /**
477  * os_strcasecmp - Compare two strings ignoring case
478  * @s1: First string
479  * @s2: Second string
480  * Returns: An integer less than, equal to, or greater than zero if s1 is
481  * found to be less than, to match, or be greatred than s2
482  */
483 int os_strcasecmp(const char *s1, const char *s2);
484 
485 /**
486  * os_strncasecmp - Compare two strings ignoring case
487  * @s1: First string
488  * @s2: Second string
489  * @n: Maximum numbers of characters to compare
490  * Returns: An integer less than, equal to, or greater than zero if s1 is
491  * found to be less than, to match, or be greater than s2. Only first n
492  * characters will be compared.
493  */
494 int os_strncasecmp(const char *s1, const char *s2, size_t n);
495 
496 /**
497  * os_strchr - Locate the first occurrence of a character in string
498  * @s: String
499  * @c: Character to search for
500  * Returns: Pointer to the matched character or %NULL if not found
501  */
502 char * os_strchr(const char *s, int c);
503 
504 /**
505  * os_strrchr - Locate the last occurrence of a character in string
506  * @s: String
507  * @c: Character to search for
508  * Returns: Pointer to the matched character or %NULL if not found
509  */
510 char * os_strrchr(const char *s, int c);
511 
512 /**
513  * os_strcmp - Compare two strings
514  * @s1: First string
515  * @s2: Second string
516  * Returns: An integer less than, equal to, or greater than zero if s1 is
517  * found to be less than, to match, or be greatred than s2
518  */
519 int os_strcmp(const char *s1, const char *s2);
520 
521 /**
522  * os_strncmp - Compare two strings
523  * @s1: First string
524  * @s2: Second string
525  * @n: Maximum numbers of characters to compare
526  * Returns: An integer less than, equal to, or greater than zero if s1 is
527  * found to be less than, to match, or be greater than s2. Only first n
528  * characters will be compared.
529  */
530 int os_strncmp(const char *s1, const char *s2, size_t n);
531 
532 /**
533  * os_strstr - Locate a substring
534  * @haystack: String (haystack) to search from
535  * @needle: Needle to search from haystack
536  * Returns: Pointer to the beginning of the substring or %NULL if not found
537  */
538 char * os_strstr(const char *haystack, const char *needle);
539 
540 /**
541  * os_snprintf - Print to a memory buffer
542  * @str: Memory buffer to print into
543  * @size: Maximum length of the str buffer
544  * @format: printf format
545  * Returns: Number of characters printed (not including trailing '\0').
546  *
547  * If the output buffer is truncated, number of characters which would have
548  * been written is returned. Since some C libraries return -1 in such a case,
549  * the caller must be prepared on that value, too, to indicate truncation.
550  *
551  * Note: Some C library implementations of snprintf() may not guarantee null
552  * termination in case the output is truncated. The OS wrapper function of
553  * os_snprintf() should provide this guarantee, i.e., to null terminate the
554  * output buffer if a C library version of the function is used and if that
555  * function does not guarantee null termination.
556  *
557  * If the target system does not include snprintf(), see, e.g.,
558  * http://www.ijs.si/software/snprintf/ for an example of a portable
559  * implementation of snprintf.
560  */
561 int os_snprintf(char *str, size_t size, const char *format, ...);
562 
563 #else /* OS_NO_C_LIB_DEFINES */
564 
565 #ifdef WPA_TRACE
566 void * os_malloc(size_t size);
567 void * os_realloc(void *ptr, size_t size);
568 void os_free(void *ptr);
569 char * os_strdup(const char *s);
570 #else /* WPA_TRACE */
571 #ifndef os_malloc
572 #if !defined(__ZEPHYR__)
573 #define os_malloc(s) malloc((s))
574 #endif
575 #endif
576 #ifndef os_realloc
577 #if !defined(__ZEPHYR__)
578 #define os_realloc(p, s) realloc((p), (s))
579 #endif
580 #endif
581 #ifndef os_free
582 #if !defined(__ZEPHYR__)
583 #define os_free(p) free((p))
584 #endif
585 #endif
586 #ifndef os_strdup
587 #ifdef _MSC_VER
588 #define os_strdup(s) _strdup(s)
589 #elif !defined(__ZEPHYR__)
590 #define os_strdup(s) strdup(s)
591 #endif
592 #endif
593 #endif /* WPA_TRACE */
594 
595 #ifndef os_memcpy
596 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
597 #endif
598 #ifndef os_memmove
599 #define os_memmove(d, s, n) memmove((d), (s), (n))
600 #endif
601 #ifndef os_memset
602 #define os_memset(s, c, n) memset(s, c, n)
603 #endif
604 #ifndef os_memcmp
605 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
606 #endif
607 
608 #ifndef os_strlen
609 #define os_strlen(s) strlen(s)
610 #endif
611 #ifndef os_strcasecmp
612 #ifdef _MSC_VER
613 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
614 #elif !defined(__ZEPHYR__)
615 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
616 #endif
617 #endif
618 #ifndef os_strncasecmp
619 #ifdef _MSC_VER
620 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
621 #elif !defined(__ZEPHYR__)
622 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
623 #endif
624 #endif
625 #ifndef os_strchr
626 #define os_strchr(s, c) strchr((s), (c))
627 #endif
628 #ifndef os_strcmp
629 #define os_strcmp(s1, s2) strcmp((s1), (s2))
630 #endif
631 #ifndef os_strncmp
632 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
633 #endif
634 #ifndef os_strrchr
635 #define os_strrchr(s, c) strrchr((s), (c))
636 #endif
637 #ifndef os_strstr
638 #define os_strstr(h, n) strstr((h), (n))
639 #endif
640 
641 #ifndef os_snprintf
642 #ifdef _MSC_VER
643 #define os_snprintf _snprintf
644 #else
645 #define os_snprintf snprintf
646 #endif
647 #endif
648 
649 #endif /* OS_NO_C_LIB_DEFINES */
650 
651 
os_snprintf_error(size_t size,int res)652 static inline int os_snprintf_error(size_t size, int res)
653 {
654 	return res < 0 || (unsigned int) res >= size;
655 }
656 
657 
os_realloc_array(void * ptr,size_t nmemb,size_t size)658 static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
659 {
660 	if (size && nmemb > (~(size_t) 0) / size)
661 		return NULL;
662 	return os_realloc(ptr, nmemb * size);
663 }
664 
665 /**
666  * os_remove_in_array - Remove a member from an array by index
667  * @ptr: Pointer to the array
668  * @nmemb: Current member count of the array
669  * @size: The size per member of the array
670  * @idx: Index of the member to be removed
671  */
os_remove_in_array(void * ptr,size_t nmemb,size_t size,size_t idx)672 static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
673 				      size_t idx)
674 {
675 	if (idx < nmemb - 1)
676 		os_memmove(((unsigned char *) ptr) + idx * size,
677 			   ((unsigned char *) ptr) + (idx + 1) * size,
678 			   (nmemb - idx - 1) * size);
679 }
680 
681 /**
682  * os_strlcpy - Copy a string with size bound and NUL-termination
683  * @dest: Destination
684  * @src: Source
685  * @siz: Size of the target buffer
686  * Returns: Total length of the target string (length of src) (not including
687  * NUL-termination)
688  *
689  * This function matches in behavior with the strlcpy(3) function in OpenBSD.
690  */
691 size_t os_strlcpy(char *dest, const char *src, size_t siz);
692 
693 /**
694  * os_memcmp_const - Constant time memory comparison
695  * @a: First buffer to compare
696  * @b: Second buffer to compare
697  * @len: Number of octets to compare
698  * Returns: 0 if buffers are equal, non-zero if not
699  *
700  * This function is meant for comparing passwords or hash values where
701  * difference in execution time could provide external observer information
702  * about the location of the difference in the memory buffers. The return value
703  * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to
704  * sort items into a defined order. Unlike os_memcmp(), execution time of
705  * os_memcmp_const() does not depend on the contents of the compared memory
706  * buffers, but only on the total compared length.
707  */
708 int os_memcmp_const(const void *a, const void *b, size_t len);
709 
710 /**
711  * os_memdup - Allocate duplicate of passed memory chunk
712  * @src: Source buffer to duplicate
713  * @len: Length of source buffer
714  * Returns: %NULL if allocation failed, copy of src buffer otherwise
715  *
716  * This function allocates a memory block like os_malloc() would, and
717  * copies the given source buffer into it.
718  */
719 void * os_memdup(const void *src, size_t len);
720 
721 /**
722  * os_exec - Execute an external program
723  * @program: Path to the program
724  * @arg: Command line argument string
725  * @wait_completion: Whether to wait until the program execution completes
726  * Returns: 0 on success, -1 on error
727  */
728 int os_exec(const char *program, const char *arg, int wait_completion);
729 
730 
731 #ifdef OS_REJECT_C_LIB_FUNCTIONS
732 #define malloc OS_DO_NOT_USE_malloc
733 #define realloc OS_DO_NOT_USE_realloc
734 #define free OS_DO_NOT_USE_free
735 #define memcpy OS_DO_NOT_USE_memcpy
736 #define memmove OS_DO_NOT_USE_memmove
737 #define memset OS_DO_NOT_USE_memset
738 #define memcmp OS_DO_NOT_USE_memcmp
739 #undef strdup
740 #define strdup OS_DO_NOT_USE_strdup
741 #define strlen OS_DO_NOT_USE_strlen
742 #define strcasecmp OS_DO_NOT_USE_strcasecmp
743 #define strncasecmp OS_DO_NOT_USE_strncasecmp
744 #undef strchr
745 #define strchr OS_DO_NOT_USE_strchr
746 #undef strcmp
747 #define strcmp OS_DO_NOT_USE_strcmp
748 #undef strncmp
749 #define strncmp OS_DO_NOT_USE_strncmp
750 #undef strncpy
751 #define strncpy OS_DO_NOT_USE_strncpy
752 #define strrchr OS_DO_NOT_USE_strrchr
753 #define strstr OS_DO_NOT_USE_strstr
754 #undef snprintf
755 #define snprintf OS_DO_NOT_USE_snprintf
756 
757 #define strcpy OS_DO_NOT_USE_strcpy
758 #endif /* OS_REJECT_C_LIB_FUNCTIONS */
759 
760 
761 #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
762 #define TEST_FAIL() testing_test_fail(NULL, false)
763 #define TEST_FAIL_TAG(tag) testing_test_fail(tag, false)
764 int testing_test_fail(const char *tag, bool is_alloc);
765 int testing_set_fail_pattern(bool is_alloc, char *patterns);
766 int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen);
767 #else
768 #define TEST_FAIL() 0
769 #define TEST_FAIL_TAG(tag) 0
testing_set_fail_pattern(bool is_alloc,char * patterns)770 static inline int testing_set_fail_pattern(bool is_alloc, char *patterns)
771 {
772 	return -1;
773 }
774 
testing_get_fail_pattern(bool is_alloc,char * buf,size_t buflen)775 static inline int testing_get_fail_pattern(bool is_alloc, char *buf,
776 					   size_t buflen)
777 {
778 	return -1;
779 }
780 #endif
781 
782 #endif /* OS_H */
783