andre@3: /* Copyright (C) 2015 by ETH Zürich andre@3: * Software engineering by Intevation GmbH andre@3: * andre@3: * This file is Free Software under the GNU GPL (v>=2) andre@3: * and comes with ABSOLUTELY NO WARRANTY! andre@3: * See LICENSE.txt for details. andre@3: */ andre@3: #ifndef STRHELP_H andre@3: #define STRHELP_H andre@3: andre@3: #ifdef __cplusplus andre@3: extern "C" { andre@3: #endif andre@3: andre@3: #include andre@3: #include andre@3: andre@3: /** andre@3: * @file strhelp.h andre@3: * @brief Helper functions for c strings and memory management andre@3: * @details strhelp contains terminating memory allocation functions and andre@3: * some conveniance functions to work with c strings or arrays of c andre@3: * strings. andre@3: */ andre@3: andre@3: /** @def To avoid that a compiler optimizes certain memset calls away */ andre@3: #define wipememory2(_ptr,_set,_len) do { \ andre@3: volatile char *_vptr=(volatile char *)(_ptr); \ andre@3: size_t _vlen=(_len); \ andre@3: while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \ andre@3: } while(0) andre@3: /** @def To avoid that a compiler optimizes certain memset calls away */ andre@3: #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len) andre@3: andre@3: andre@3: void *xmalloc( size_t n ); andre@3: /** @brief like malloc but initalizes the values with 0 */ andre@3: void *xmalloc0( size_t n ); andre@3: void *xrealloc( void *a, size_t n ); andre@3: void *xcalloc( size_t n, size_t m ); andre@3: char *xstrndup( const char *string, const size_t len ); andre@3: void xfree ( void *p ); andre@3: andre@3: /** andre@3: * @brief Terminating variant of asprintf andre@3: * andre@3: * This function behaves exactly like asprintf(3) but will terminate andre@3: * when an error occures (usally that means that memoy allocation andre@3: * failed). andre@3: */ andre@3: int xasprintf (char **strp, const char *fmt, ...); andre@3: andre@3: /** andre@3: * @brief Returns the length of the given %NULL-terminated andre@3: * string array str_array. andre@3: * @param[in] str_array a %NULL-terminated array of strings andre@3: * @returns length of str_array. andre@3: */ andre@3: unsigned int strv_length (char **str_array); andre@3: andre@3: /** andre@3: * @brief append a string to a NULL terminated array of strings. andre@3: * andre@3: * @param[in,out] pArray pointer to the NULL terminated list of string pointers. andre@3: * @param[in] string pointer to the string to append to the list. andre@3: * @param[in] len length of the string to append to the list andre@3: */ andre@3: void strv_append (char ***pArray, const char *string, const size_t len); andre@3: andre@3: /** andre@3: * @brief append a string to another string. andre@3: * andre@3: * @param[in,out] pDst pointer to the string to be extended. andre@3: * @param[in,out] dst_len length of the dst string. Will be modified. andre@3: * @param[in] appendage pointer to the string to append. andre@3: * @param[in] len length of the string to append. andre@3: */ andre@3: void str_append_str (char **pDst, size_t *dst_len, const char *appendage, andre@3: const size_t len); andre@3: andre@3: /** andre@3: * @brief Frees the given %NULL-terminated string array. andre@3: * @param[in,out] str_array a %NULL-terminated array of strings andre@3: */ andre@3: void strv_free (char **str_array); andre@3: andre@3: /** andre@3: * @brief Checks whether two strings exactly match andre@3: * @param[in] s1 the first string andre@3: * @param[in] s2 the second string andre@3: * @returns true if s1 and s2 are equal andre@3: */ andre@3: bool str_equal (char *s1, char *s2); andre@3: andre@3: /** andre@3: * @brief Checks whether s2 exactly matches the beginning of s1. andre@3: * @param[in] s1 the string who's beginning is searched andre@3: * @param[in] s2 the string which is searched for andre@3: * @returns true if s1 starts with s2, false otherwise andre@3: */ andre@3: bool str_starts_with (char *s1, char *s2); andre@3: andre@3: /** andre@3: * @brief Trims all white space from the start and end of string. andre@3: * @details the start of the string is trimmed by setting *s to the andre@3: * first non white space character. The end is trimmed by setting the andre@3: * first character after the last non white space character to \0. andre@3: * @param[in,out] s ponter to the string to strip andre@3: */ andre@3: bool str_trim (char **s); andre@3: andre@3: /** @brief decode base64 encoded data andre@3: * andre@3: * The memory allocated for dest needs to be free'd by the andre@3: * caller. andre@3: * andre@3: * _Input warning:_ andre@3: * If the input contains invalid base64 characters an error andre@3: * is returned. andre@3: * andre@3: * If the input is invalid base64 but consists of valid andre@3: * base64 characters _no error_ is returned and dst contains andre@3: * the valid input up to the error. andre@3: * andre@3: * @param [out] dst Pointer to the destination. Needs to be NULL andre@3: * @param [out] dst_size Size allocated for the destination. andre@3: * @param [in] src Pointer to the base64 encoded data. andre@3: * @param [in] src_size Size of the encoded data. andre@3: * andre@3: * @returns 0 on success a polarssl error or -1 otherwise andre@3: */ andre@3: int str_base64_decode(char **dst, size_t *dst_size, char *src, andre@3: size_t src_size); andre@3: andre@30: #ifdef _WIN32 andre@3: andre@3: /** @brief convert a utf8 string to utf16 wchar andre@3: * andre@3: * @param[in] string utf8 string. Must be at least len characters long. andre@3: * @param[in] len number of characters to be converted. andre@3: * andre@3: * @returns pointer to a newly allocated wchar array. NULL on error. andre@3: * andre@3: **/ andre@3: wchar_t *utf8_to_wchar (const char *string, size_t len); andre@3: andre@3: /** @brief convert a local 8 bit (acp) string to utf16 wchar andre@3: * andre@3: * @param[in] string acp string. Must be at least len characters long. andre@3: * @param[in] len number of characters to be converted. andre@3: * andre@3: * @returns pointer to a newly allocated wchar array. NULL on error. andre@3: * andre@3: **/ andre@3: wchar_t *acp_to_wchar (const char *string, size_t len); andre@3: andre@3: /** @brief convert a utf16 string to utf8 andre@3: * andre@3: * @param[in] string utf16 string. Must be at least len characters long. andre@3: * @param[in] len number of characters to be converted. andre@3: * andre@3: * @returns pointer to a newly allocated char array. NULL on error. andre@3: * andre@3: **/ andre@3: char *wchar_to_utf8 (const wchar_t *string, size_t len); andre@3: #endif andre@3: andre@3: #ifdef __cplusplus andre@3: } andre@3: #endif andre@3: andre@3: #endif andre@3: