1189251Ssam/* 2214734Srpaulo * OS specific functions 3214734Srpaulo * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi> 4189251Ssam * 5252726Srpaulo * This software may be distributed under the terms of the BSD license. 6252726Srpaulo * See README for more details. 7189251Ssam */ 8189251Ssam 9189251Ssam#ifndef OS_H 10189251Ssam#define OS_H 11189251Ssam 12189251Ssamtypedef long os_time_t; 13189251Ssam 14189251Ssam/** 15189251Ssam * os_sleep - Sleep (sec, usec) 16189251Ssam * @sec: Number of seconds to sleep 17189251Ssam * @usec: Number of microseconds to sleep 18189251Ssam */ 19189251Ssamvoid os_sleep(os_time_t sec, os_time_t usec); 20189251Ssam 21189251Ssamstruct os_time { 22189251Ssam os_time_t sec; 23189251Ssam os_time_t usec; 24189251Ssam}; 25189251Ssam 26281806Srpaulostruct os_reltime { 27281806Srpaulo os_time_t sec; 28281806Srpaulo os_time_t usec; 29281806Srpaulo}; 30281806Srpaulo 31189251Ssam/** 32189251Ssam * os_get_time - Get current time (sec, usec) 33189251Ssam * @t: Pointer to buffer for the time 34189251Ssam * Returns: 0 on success, -1 on failure 35189251Ssam */ 36189251Ssamint os_get_time(struct os_time *t); 37189251Ssam 38281806Srpaulo/** 39281806Srpaulo * os_get_reltime - Get relative time (sec, usec) 40281806Srpaulo * @t: Pointer to buffer for the time 41281806Srpaulo * Returns: 0 on success, -1 on failure 42281806Srpaulo */ 43281806Srpauloint os_get_reltime(struct os_reltime *t); 44189251Ssam 45189251Ssam 46281806Srpaulo/* Helpers for handling struct os_time */ 47189251Ssam 48281806Srpaulostatic inline int os_time_before(struct os_time *a, struct os_time *b) 49281806Srpaulo{ 50281806Srpaulo return (a->sec < b->sec) || 51281806Srpaulo (a->sec == b->sec && a->usec < b->usec); 52281806Srpaulo} 53189251Ssam 54281806Srpaulo 55281806Srpaulostatic inline void os_time_sub(struct os_time *a, struct os_time *b, 56281806Srpaulo struct os_time *res) 57281806Srpaulo{ 58281806Srpaulo res->sec = a->sec - b->sec; 59281806Srpaulo res->usec = a->usec - b->usec; 60281806Srpaulo if (res->usec < 0) { 61281806Srpaulo res->sec--; 62281806Srpaulo res->usec += 1000000; 63281806Srpaulo } 64281806Srpaulo} 65281806Srpaulo 66281806Srpaulo 67281806Srpaulo/* Helpers for handling struct os_reltime */ 68281806Srpaulo 69281806Srpaulostatic inline int os_reltime_before(struct os_reltime *a, 70281806Srpaulo struct os_reltime *b) 71281806Srpaulo{ 72281806Srpaulo return (a->sec < b->sec) || 73281806Srpaulo (a->sec == b->sec && a->usec < b->usec); 74281806Srpaulo} 75281806Srpaulo 76281806Srpaulo 77281806Srpaulostatic inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b, 78281806Srpaulo struct os_reltime *res) 79281806Srpaulo{ 80281806Srpaulo res->sec = a->sec - b->sec; 81281806Srpaulo res->usec = a->usec - b->usec; 82281806Srpaulo if (res->usec < 0) { 83281806Srpaulo res->sec--; 84281806Srpaulo res->usec += 1000000; 85281806Srpaulo } 86281806Srpaulo} 87281806Srpaulo 88281806Srpaulo 89281806Srpaulostatic inline void os_reltime_age(struct os_reltime *start, 90281806Srpaulo struct os_reltime *age) 91281806Srpaulo{ 92281806Srpaulo struct os_reltime now; 93281806Srpaulo 94281806Srpaulo os_get_reltime(&now); 95281806Srpaulo os_reltime_sub(&now, start, age); 96281806Srpaulo} 97281806Srpaulo 98281806Srpaulo 99281806Srpaulostatic inline int os_reltime_expired(struct os_reltime *now, 100281806Srpaulo struct os_reltime *ts, 101281806Srpaulo os_time_t timeout_secs) 102281806Srpaulo{ 103281806Srpaulo struct os_reltime age; 104281806Srpaulo 105281806Srpaulo os_reltime_sub(now, ts, &age); 106281806Srpaulo return (age.sec > timeout_secs) || 107281806Srpaulo (age.sec == timeout_secs && age.usec > 0); 108281806Srpaulo} 109281806Srpaulo 110281806Srpaulo 111281806Srpaulostatic inline int os_reltime_initialized(struct os_reltime *t) 112281806Srpaulo{ 113281806Srpaulo return t->sec != 0 || t->usec != 0; 114281806Srpaulo} 115281806Srpaulo 116281806Srpaulo 117189251Ssam/** 118189251Ssam * os_mktime - Convert broken-down time into seconds since 1970-01-01 119189251Ssam * @year: Four digit year 120189251Ssam * @month: Month (1 .. 12) 121189251Ssam * @day: Day of month (1 .. 31) 122189251Ssam * @hour: Hour (0 .. 23) 123189251Ssam * @min: Minute (0 .. 59) 124189251Ssam * @sec: Second (0 .. 60) 125189251Ssam * @t: Buffer for returning calendar time representation (seconds since 126189251Ssam * 1970-01-01 00:00:00) 127189251Ssam * Returns: 0 on success, -1 on failure 128189251Ssam * 129189251Ssam * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time 130189251Ssam * which is used by POSIX mktime(). 131189251Ssam */ 132189251Ssamint os_mktime(int year, int month, int day, int hour, int min, int sec, 133189251Ssam os_time_t *t); 134189251Ssam 135252726Srpaulostruct os_tm { 136252726Srpaulo int sec; /* 0..59 or 60 for leap seconds */ 137252726Srpaulo int min; /* 0..59 */ 138252726Srpaulo int hour; /* 0..23 */ 139252726Srpaulo int day; /* 1..31 */ 140252726Srpaulo int month; /* 1..12 */ 141252726Srpaulo int year; /* Four digit year */ 142252726Srpaulo}; 143189251Ssam 144252726Srpauloint os_gmtime(os_time_t t, struct os_tm *tm); 145252726Srpaulo 146189251Ssam/** 147189251Ssam * os_daemonize - Run in the background (detach from the controlling terminal) 148189251Ssam * @pid_file: File name to write the process ID to or %NULL to skip this 149189251Ssam * Returns: 0 on success, -1 on failure 150189251Ssam */ 151189251Ssamint os_daemonize(const char *pid_file); 152189251Ssam 153189251Ssam/** 154189251Ssam * os_daemonize_terminate - Stop running in the background (remove pid file) 155189251Ssam * @pid_file: File name to write the process ID to or %NULL to skip this 156189251Ssam */ 157189251Ssamvoid os_daemonize_terminate(const char *pid_file); 158189251Ssam 159189251Ssam/** 160189251Ssam * os_get_random - Get cryptographically strong pseudo random data 161189251Ssam * @buf: Buffer for pseudo random data 162189251Ssam * @len: Length of the buffer 163189251Ssam * Returns: 0 on success, -1 on failure 164189251Ssam */ 165189251Ssamint os_get_random(unsigned char *buf, size_t len); 166189251Ssam 167189251Ssam/** 168189251Ssam * os_random - Get pseudo random value (not necessarily very strong) 169189251Ssam * Returns: Pseudo random value 170189251Ssam */ 171189251Ssamunsigned long os_random(void); 172189251Ssam 173189251Ssam/** 174189251Ssam * os_rel2abs_path - Get an absolute path for a file 175189251Ssam * @rel_path: Relative path to a file 176189251Ssam * Returns: Absolute path for the file or %NULL on failure 177189251Ssam * 178189251Ssam * This function tries to convert a relative path of a file to an absolute path 179189251Ssam * in order for the file to be found even if current working directory has 180189251Ssam * changed. The returned value is allocated and caller is responsible for 181189251Ssam * freeing it. It is acceptable to just return the same path in an allocated 182189251Ssam * buffer, e.g., return strdup(rel_path). This function is only used to find 183189251Ssam * configuration files when os_daemonize() may have changed the current working 184189251Ssam * directory and relative path would be pointing to a different location. 185189251Ssam */ 186189251Ssamchar * os_rel2abs_path(const char *rel_path); 187189251Ssam 188189251Ssam/** 189189251Ssam * os_program_init - Program initialization (called at start) 190189251Ssam * Returns: 0 on success, -1 on failure 191189251Ssam * 192189251Ssam * This function is called when a programs starts. If there are any OS specific 193189251Ssam * processing that is needed, it can be placed here. It is also acceptable to 194189251Ssam * just return 0 if not special processing is needed. 195189251Ssam */ 196189251Ssamint os_program_init(void); 197189251Ssam 198189251Ssam/** 199189251Ssam * os_program_deinit - Program deinitialization (called just before exit) 200189251Ssam * 201189251Ssam * This function is called just before a program exists. If there are any OS 202189251Ssam * specific processing, e.g., freeing resourced allocated in os_program_init(), 203189251Ssam * it should be done here. It is also acceptable for this function to do 204189251Ssam * nothing. 205189251Ssam */ 206189251Ssamvoid os_program_deinit(void); 207189251Ssam 208189251Ssam/** 209189251Ssam * os_setenv - Set environment variable 210189251Ssam * @name: Name of the variable 211189251Ssam * @value: Value to set to the variable 212189251Ssam * @overwrite: Whether existing variable should be overwritten 213189251Ssam * Returns: 0 on success, -1 on error 214189251Ssam * 215189251Ssam * This function is only used for wpa_cli action scripts. OS wrapper does not 216189251Ssam * need to implement this if such functionality is not needed. 217189251Ssam */ 218189251Ssamint os_setenv(const char *name, const char *value, int overwrite); 219189251Ssam 220189251Ssam/** 221189251Ssam * os_unsetenv - Delete environent variable 222189251Ssam * @name: Name of the variable 223189251Ssam * Returns: 0 on success, -1 on error 224189251Ssam * 225189251Ssam * This function is only used for wpa_cli action scripts. OS wrapper does not 226189251Ssam * need to implement this if such functionality is not needed. 227189251Ssam */ 228189251Ssamint os_unsetenv(const char *name); 229189251Ssam 230189251Ssam/** 231189251Ssam * os_readfile - Read a file to an allocated memory buffer 232189251Ssam * @name: Name of the file to read 233189251Ssam * @len: For returning the length of the allocated buffer 234189251Ssam * Returns: Pointer to the allocated buffer or %NULL on failure 235189251Ssam * 236189251Ssam * This function allocates memory and reads the given file to this buffer. Both 237189251Ssam * binary and text files can be read with this function. The caller is 238189251Ssam * responsible for freeing the returned buffer with os_free(). 239189251Ssam */ 240189251Ssamchar * os_readfile(const char *name, size_t *len); 241189251Ssam 242189251Ssam/** 243281806Srpaulo * os_file_exists - Check whether the specified file exists 244281806Srpaulo * @fname: Path and name of the file 245281806Srpaulo * Returns: 1 if the file exists or 0 if not 246281806Srpaulo */ 247281806Srpauloint os_file_exists(const char *fname); 248281806Srpaulo 249281806Srpaulo/** 250289549Srpaulo * os_fdatasync - Sync a file's (for a given stream) state with storage device 251289549Srpaulo * @stream: the stream to be flushed 252289549Srpaulo * Returns: 0 if the operation succeeded or -1 on failure 253289549Srpaulo */ 254289549Srpauloint os_fdatasync(FILE *stream); 255289549Srpaulo 256289549Srpaulo/** 257189251Ssam * os_zalloc - Allocate and zero memory 258189251Ssam * @size: Number of bytes to allocate 259189251Ssam * Returns: Pointer to allocated and zeroed memory or %NULL on failure 260189251Ssam * 261189251Ssam * Caller is responsible for freeing the returned buffer with os_free(). 262189251Ssam */ 263189251Ssamvoid * os_zalloc(size_t size); 264189251Ssam 265252726Srpaulo/** 266252726Srpaulo * os_calloc - Allocate and zero memory for an array 267252726Srpaulo * @nmemb: Number of members in the array 268252726Srpaulo * @size: Number of bytes in each member 269252726Srpaulo * Returns: Pointer to allocated and zeroed memory or %NULL on failure 270252726Srpaulo * 271252726Srpaulo * This function can be used as a wrapper for os_zalloc(nmemb * size) when an 272252726Srpaulo * allocation is used for an array. The main benefit over os_zalloc() is in 273252726Srpaulo * having an extra check to catch integer overflows in multiplication. 274252726Srpaulo * 275252726Srpaulo * Caller is responsible for freeing the returned buffer with os_free(). 276252726Srpaulo */ 277252726Srpaulostatic inline void * os_calloc(size_t nmemb, size_t size) 278252726Srpaulo{ 279252726Srpaulo if (size && nmemb > (~(size_t) 0) / size) 280252726Srpaulo return NULL; 281252726Srpaulo return os_zalloc(nmemb * size); 282252726Srpaulo} 283189251Ssam 284252726Srpaulo 285189251Ssam/* 286189251Ssam * The following functions are wrapper for standard ANSI C or POSIX functions. 287189251Ssam * By default, they are just defined to use the standard function name and no 288189251Ssam * os_*.c implementation is needed for them. This avoids extra function calls 289189251Ssam * by allowing the C pre-processor take care of the function name mapping. 290189251Ssam * 291189251Ssam * If the target system uses a C library that does not provide these functions, 292189251Ssam * build_config.h can be used to define the wrappers to use a different 293189251Ssam * function name. This can be done on function-by-function basis since the 294189251Ssam * defines here are only used if build_config.h does not define the os_* name. 295189251Ssam * If needed, os_*.c file can be used to implement the functions that are not 296189251Ssam * included in the C library on the target system. Alternatively, 297189251Ssam * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case 298189251Ssam * these functions need to be implemented in os_*.c file for the target system. 299189251Ssam */ 300189251Ssam 301189251Ssam#ifdef OS_NO_C_LIB_DEFINES 302189251Ssam 303189251Ssam/** 304189251Ssam * os_malloc - Allocate dynamic memory 305189251Ssam * @size: Size of the buffer to allocate 306189251Ssam * Returns: Allocated buffer or %NULL on failure 307189251Ssam * 308189251Ssam * Caller is responsible for freeing the returned buffer with os_free(). 309189251Ssam */ 310189251Ssamvoid * os_malloc(size_t size); 311189251Ssam 312189251Ssam/** 313189251Ssam * os_realloc - Re-allocate dynamic memory 314189251Ssam * @ptr: Old buffer from os_malloc() or os_realloc() 315189251Ssam * @size: Size of the new buffer 316189251Ssam * Returns: Allocated buffer or %NULL on failure 317189251Ssam * 318189251Ssam * Caller is responsible for freeing the returned buffer with os_free(). 319189251Ssam * If re-allocation fails, %NULL is returned and the original buffer (ptr) is 320189251Ssam * not freed and caller is still responsible for freeing it. 321189251Ssam */ 322189251Ssamvoid * os_realloc(void *ptr, size_t size); 323189251Ssam 324189251Ssam/** 325189251Ssam * os_free - Free dynamic memory 326189251Ssam * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL 327189251Ssam */ 328189251Ssamvoid os_free(void *ptr); 329189251Ssam 330189251Ssam/** 331189251Ssam * os_memcpy - Copy memory area 332189251Ssam * @dest: Destination 333189251Ssam * @src: Source 334189251Ssam * @n: Number of bytes to copy 335189251Ssam * Returns: dest 336189251Ssam * 337189251Ssam * The memory areas src and dst must not overlap. os_memmove() can be used with 338189251Ssam * overlapping memory. 339189251Ssam */ 340189251Ssamvoid * os_memcpy(void *dest, const void *src, size_t n); 341189251Ssam 342189251Ssam/** 343189251Ssam * os_memmove - Copy memory area 344189251Ssam * @dest: Destination 345189251Ssam * @src: Source 346189251Ssam * @n: Number of bytes to copy 347189251Ssam * Returns: dest 348189251Ssam * 349189251Ssam * The memory areas src and dst may overlap. 350189251Ssam */ 351189251Ssamvoid * os_memmove(void *dest, const void *src, size_t n); 352189251Ssam 353189251Ssam/** 354189251Ssam * os_memset - Fill memory with a constant byte 355189251Ssam * @s: Memory area to be filled 356189251Ssam * @c: Constant byte 357189251Ssam * @n: Number of bytes started from s to fill with c 358189251Ssam * Returns: s 359189251Ssam */ 360189251Ssamvoid * os_memset(void *s, int c, size_t n); 361189251Ssam 362189251Ssam/** 363189251Ssam * os_memcmp - Compare memory areas 364189251Ssam * @s1: First buffer 365189251Ssam * @s2: Second buffer 366189251Ssam * @n: Maximum numbers of octets to compare 367189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is 368189251Ssam * found to be less than, to match, or be greater than s2. Only first n 369189251Ssam * characters will be compared. 370189251Ssam */ 371189251Ssamint os_memcmp(const void *s1, const void *s2, size_t n); 372189251Ssam 373189251Ssam/** 374189251Ssam * os_strdup - Duplicate a string 375189251Ssam * @s: Source string 376189251Ssam * Returns: Allocated buffer with the string copied into it or %NULL on failure 377189251Ssam * 378189251Ssam * Caller is responsible for freeing the returned buffer with os_free(). 379189251Ssam */ 380189251Ssamchar * os_strdup(const char *s); 381189251Ssam 382189251Ssam/** 383189251Ssam * os_strlen - Calculate the length of a string 384189251Ssam * @s: '\0' terminated string 385189251Ssam * Returns: Number of characters in s (not counting the '\0' terminator) 386189251Ssam */ 387189251Ssamsize_t os_strlen(const char *s); 388189251Ssam 389189251Ssam/** 390189251Ssam * os_strcasecmp - Compare two strings ignoring case 391189251Ssam * @s1: First string 392189251Ssam * @s2: Second string 393189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is 394189251Ssam * found to be less than, to match, or be greatred than s2 395189251Ssam */ 396189251Ssamint os_strcasecmp(const char *s1, const char *s2); 397189251Ssam 398189251Ssam/** 399189251Ssam * os_strncasecmp - Compare two strings ignoring case 400189251Ssam * @s1: First string 401189251Ssam * @s2: Second string 402189251Ssam * @n: Maximum numbers of characters to compare 403189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is 404189251Ssam * found to be less than, to match, or be greater than s2. Only first n 405189251Ssam * characters will be compared. 406189251Ssam */ 407189251Ssamint os_strncasecmp(const char *s1, const char *s2, size_t n); 408189251Ssam 409189251Ssam/** 410189251Ssam * os_strchr - Locate the first occurrence of a character in string 411189251Ssam * @s: String 412189251Ssam * @c: Character to search for 413189251Ssam * Returns: Pointer to the matched character or %NULL if not found 414189251Ssam */ 415189251Ssamchar * os_strchr(const char *s, int c); 416189251Ssam 417189251Ssam/** 418189251Ssam * os_strrchr - Locate the last occurrence of a character in string 419189251Ssam * @s: String 420189251Ssam * @c: Character to search for 421189251Ssam * Returns: Pointer to the matched character or %NULL if not found 422189251Ssam */ 423189251Ssamchar * os_strrchr(const char *s, int c); 424189251Ssam 425189251Ssam/** 426189251Ssam * os_strcmp - Compare two strings 427189251Ssam * @s1: First string 428189251Ssam * @s2: Second string 429189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is 430189251Ssam * found to be less than, to match, or be greatred than s2 431189251Ssam */ 432189251Ssamint os_strcmp(const char *s1, const char *s2); 433189251Ssam 434189251Ssam/** 435189251Ssam * os_strncmp - Compare two strings 436189251Ssam * @s1: First string 437189251Ssam * @s2: Second string 438189251Ssam * @n: Maximum numbers of characters to compare 439189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is 440189251Ssam * found to be less than, to match, or be greater than s2. Only first n 441189251Ssam * characters will be compared. 442189251Ssam */ 443189251Ssamint os_strncmp(const char *s1, const char *s2, size_t n); 444189251Ssam 445189251Ssam/** 446189251Ssam * os_strstr - Locate a substring 447189251Ssam * @haystack: String (haystack) to search from 448189251Ssam * @needle: Needle to search from haystack 449189251Ssam * Returns: Pointer to the beginning of the substring or %NULL if not found 450189251Ssam */ 451189251Ssamchar * os_strstr(const char *haystack, const char *needle); 452189251Ssam 453189251Ssam/** 454189251Ssam * os_snprintf - Print to a memory buffer 455189251Ssam * @str: Memory buffer to print into 456189251Ssam * @size: Maximum length of the str buffer 457189251Ssam * @format: printf format 458189251Ssam * Returns: Number of characters printed (not including trailing '\0'). 459189251Ssam * 460189251Ssam * If the output buffer is truncated, number of characters which would have 461189251Ssam * been written is returned. Since some C libraries return -1 in such a case, 462189251Ssam * the caller must be prepared on that value, too, to indicate truncation. 463189251Ssam * 464189251Ssam * Note: Some C library implementations of snprintf() may not guarantee null 465189251Ssam * termination in case the output is truncated. The OS wrapper function of 466189251Ssam * os_snprintf() should provide this guarantee, i.e., to null terminate the 467189251Ssam * output buffer if a C library version of the function is used and if that 468189251Ssam * function does not guarantee null termination. 469189251Ssam * 470189251Ssam * If the target system does not include snprintf(), see, e.g., 471189251Ssam * http://www.ijs.si/software/snprintf/ for an example of a portable 472189251Ssam * implementation of snprintf. 473189251Ssam */ 474189251Ssamint os_snprintf(char *str, size_t size, const char *format, ...); 475189251Ssam 476189251Ssam#else /* OS_NO_C_LIB_DEFINES */ 477189251Ssam 478214734Srpaulo#ifdef WPA_TRACE 479214734Srpaulovoid * os_malloc(size_t size); 480214734Srpaulovoid * os_realloc(void *ptr, size_t size); 481214734Srpaulovoid os_free(void *ptr); 482214734Srpaulochar * os_strdup(const char *s); 483214734Srpaulo#else /* WPA_TRACE */ 484189251Ssam#ifndef os_malloc 485189251Ssam#define os_malloc(s) malloc((s)) 486189251Ssam#endif 487189251Ssam#ifndef os_realloc 488189251Ssam#define os_realloc(p, s) realloc((p), (s)) 489189251Ssam#endif 490189251Ssam#ifndef os_free 491189251Ssam#define os_free(p) free((p)) 492189251Ssam#endif 493214734Srpaulo#ifndef os_strdup 494214734Srpaulo#ifdef _MSC_VER 495214734Srpaulo#define os_strdup(s) _strdup(s) 496214734Srpaulo#else 497214734Srpaulo#define os_strdup(s) strdup(s) 498214734Srpaulo#endif 499214734Srpaulo#endif 500214734Srpaulo#endif /* WPA_TRACE */ 501189251Ssam 502189251Ssam#ifndef os_memcpy 503189251Ssam#define os_memcpy(d, s, n) memcpy((d), (s), (n)) 504189251Ssam#endif 505189251Ssam#ifndef os_memmove 506189251Ssam#define os_memmove(d, s, n) memmove((d), (s), (n)) 507189251Ssam#endif 508189251Ssam#ifndef os_memset 509189251Ssam#define os_memset(s, c, n) memset(s, c, n) 510189251Ssam#endif 511189251Ssam#ifndef os_memcmp 512189251Ssam#define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n)) 513189251Ssam#endif 514189251Ssam 515189251Ssam#ifndef os_strlen 516189251Ssam#define os_strlen(s) strlen(s) 517189251Ssam#endif 518189251Ssam#ifndef os_strcasecmp 519189251Ssam#ifdef _MSC_VER 520189251Ssam#define os_strcasecmp(s1, s2) _stricmp((s1), (s2)) 521189251Ssam#else 522189251Ssam#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2)) 523189251Ssam#endif 524189251Ssam#endif 525189251Ssam#ifndef os_strncasecmp 526189251Ssam#ifdef _MSC_VER 527189251Ssam#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n)) 528189251Ssam#else 529189251Ssam#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n)) 530189251Ssam#endif 531189251Ssam#endif 532189251Ssam#ifndef os_strchr 533189251Ssam#define os_strchr(s, c) strchr((s), (c)) 534189251Ssam#endif 535189251Ssam#ifndef os_strcmp 536189251Ssam#define os_strcmp(s1, s2) strcmp((s1), (s2)) 537189251Ssam#endif 538189251Ssam#ifndef os_strncmp 539189251Ssam#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) 540189251Ssam#endif 541189251Ssam#ifndef os_strrchr 542189251Ssam#define os_strrchr(s, c) strrchr((s), (c)) 543189251Ssam#endif 544189251Ssam#ifndef os_strstr 545189251Ssam#define os_strstr(h, n) strstr((h), (n)) 546189251Ssam#endif 547189251Ssam 548189251Ssam#ifndef os_snprintf 549189251Ssam#ifdef _MSC_VER 550189251Ssam#define os_snprintf _snprintf 551189251Ssam#else 552189251Ssam#define os_snprintf snprintf 553189251Ssam#endif 554189251Ssam#endif 555189251Ssam 556189251Ssam#endif /* OS_NO_C_LIB_DEFINES */ 557189251Ssam 558189251Ssam 559281806Srpaulostatic inline int os_snprintf_error(size_t size, int res) 560281806Srpaulo{ 561281806Srpaulo return res < 0 || (unsigned int) res >= size; 562281806Srpaulo} 563281806Srpaulo 564281806Srpaulo 565252726Srpaulostatic inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size) 566252726Srpaulo{ 567252726Srpaulo if (size && nmemb > (~(size_t) 0) / size) 568252726Srpaulo return NULL; 569252726Srpaulo return os_realloc(ptr, nmemb * size); 570252726Srpaulo} 571252726Srpaulo 572281806Srpaulo/** 573281806Srpaulo * os_remove_in_array - Remove a member from an array by index 574281806Srpaulo * @ptr: Pointer to the array 575281806Srpaulo * @nmemb: Current member count of the array 576281806Srpaulo * @size: The size per member of the array 577281806Srpaulo * @idx: Index of the member to be removed 578281806Srpaulo */ 579281806Srpaulostatic inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size, 580281806Srpaulo size_t idx) 581281806Srpaulo{ 582281806Srpaulo if (idx < nmemb - 1) 583281806Srpaulo os_memmove(((unsigned char *) ptr) + idx * size, 584281806Srpaulo ((unsigned char *) ptr) + (idx + 1) * size, 585281806Srpaulo (nmemb - idx - 1) * size); 586281806Srpaulo} 587252726Srpaulo 588189251Ssam/** 589189251Ssam * os_strlcpy - Copy a string with size bound and NUL-termination 590189251Ssam * @dest: Destination 591189251Ssam * @src: Source 592189251Ssam * @siz: Size of the target buffer 593189251Ssam * Returns: Total length of the target string (length of src) (not including 594189251Ssam * NUL-termination) 595189251Ssam * 596189251Ssam * This function matches in behavior with the strlcpy(3) function in OpenBSD. 597189251Ssam */ 598189251Ssamsize_t os_strlcpy(char *dest, const char *src, size_t siz); 599189251Ssam 600281806Srpaulo/** 601281806Srpaulo * os_memcmp_const - Constant time memory comparison 602281806Srpaulo * @a: First buffer to compare 603281806Srpaulo * @b: Second buffer to compare 604281806Srpaulo * @len: Number of octets to compare 605281806Srpaulo * Returns: 0 if buffers are equal, non-zero if not 606281806Srpaulo * 607281806Srpaulo * This function is meant for comparing passwords or hash values where 608281806Srpaulo * difference in execution time could provide external observer information 609281806Srpaulo * about the location of the difference in the memory buffers. The return value 610281806Srpaulo * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to 611281806Srpaulo * sort items into a defined order. Unlike os_memcmp(), execution time of 612281806Srpaulo * os_memcmp_const() does not depend on the contents of the compared memory 613281806Srpaulo * buffers, but only on the total compared length. 614281806Srpaulo */ 615281806Srpauloint os_memcmp_const(const void *a, const void *b, size_t len); 616189251Ssam 617346981Scy 618281806Srpaulo/** 619346981Scy * os_memdup - Allocate duplicate of passed memory chunk 620346981Scy * @src: Source buffer to duplicate 621346981Scy * @len: Length of source buffer 622346981Scy * Returns: %NULL if allocation failed, copy of src buffer otherwise 623346981Scy * 624346981Scy * This function allocates a memory block like os_malloc() would, and 625346981Scy * copies the given source buffer into it. 626346981Scy */ 627346981Scyvoid * os_memdup(const void *src, size_t len); 628346981Scy 629346981Scy/** 630281806Srpaulo * os_exec - Execute an external program 631281806Srpaulo * @program: Path to the program 632281806Srpaulo * @arg: Command line argument string 633281806Srpaulo * @wait_completion: Whether to wait until the program execution completes 634281806Srpaulo * Returns: 0 on success, -1 on error 635281806Srpaulo */ 636281806Srpauloint os_exec(const char *program, const char *arg, int wait_completion); 637281806Srpaulo 638281806Srpaulo 639189251Ssam#ifdef OS_REJECT_C_LIB_FUNCTIONS 640189251Ssam#define malloc OS_DO_NOT_USE_malloc 641189251Ssam#define realloc OS_DO_NOT_USE_realloc 642189251Ssam#define free OS_DO_NOT_USE_free 643189251Ssam#define memcpy OS_DO_NOT_USE_memcpy 644189251Ssam#define memmove OS_DO_NOT_USE_memmove 645189251Ssam#define memset OS_DO_NOT_USE_memset 646189251Ssam#define memcmp OS_DO_NOT_USE_memcmp 647189251Ssam#undef strdup 648189251Ssam#define strdup OS_DO_NOT_USE_strdup 649189251Ssam#define strlen OS_DO_NOT_USE_strlen 650189251Ssam#define strcasecmp OS_DO_NOT_USE_strcasecmp 651189251Ssam#define strncasecmp OS_DO_NOT_USE_strncasecmp 652189251Ssam#undef strchr 653189251Ssam#define strchr OS_DO_NOT_USE_strchr 654189251Ssam#undef strcmp 655189251Ssam#define strcmp OS_DO_NOT_USE_strcmp 656189251Ssam#undef strncmp 657189251Ssam#define strncmp OS_DO_NOT_USE_strncmp 658189251Ssam#undef strncpy 659189251Ssam#define strncpy OS_DO_NOT_USE_strncpy 660189251Ssam#define strrchr OS_DO_NOT_USE_strrchr 661189251Ssam#define strstr OS_DO_NOT_USE_strstr 662189251Ssam#undef snprintf 663189251Ssam#define snprintf OS_DO_NOT_USE_snprintf 664189251Ssam 665189251Ssam#define strcpy OS_DO_NOT_USE_strcpy 666189251Ssam#endif /* OS_REJECT_C_LIB_FUNCTIONS */ 667189251Ssam 668289549Srpaulo 669289549Srpaulo#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) 670289549Srpaulo#define TEST_FAIL() testing_test_fail() 671289549Srpauloint testing_test_fail(void); 672337817Scyextern char wpa_trace_fail_func[256]; 673337817Scyextern unsigned int wpa_trace_fail_after; 674337817Scyextern char wpa_trace_test_fail_func[256]; 675337817Scyextern unsigned int wpa_trace_test_fail_after; 676289549Srpaulo#else 677289549Srpaulo#define TEST_FAIL() 0 678289549Srpaulo#endif 679289549Srpaulo 680189251Ssam#endif /* OS_H */ 681