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