📄 utils.h
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! \file * \brief Utility functions */#ifndef _ASTERISK_UTILS_H#define _ASTERISK_UTILS_H#include "asterisk/network.h"#include <time.h> /* we want to override localtime_r */#include <unistd.h>#include "asterisk/lock.h"#include "asterisk/time.h"#include "asterisk/logger.h"#include "asterisk/localtime.h"/*! \note \verbatim Note: It is very important to use only unsigned variables to hold bit flags, as otherwise you can fall prey to the compiler's sign-extension antics if you try to use the top two bits in your variable. The flag macros below use a set of compiler tricks to verify that the caller is using an "unsigned int" variable to hold the flags, and nothing else. If the caller uses any other type of variable, a warning message similar to this: warning: comparison of distinct pointer types lacks cast will be generated. The "dummy" variable below is used to make these comparisons. Also note that at -O2 or above, this type-safety checking does _not_ produce any additional object code at all. \endverbatim*/extern unsigned int __unsigned_int_flags_dummy;#define ast_test_flag(p,flag) ({ \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags & (flag)); \ })#define ast_set_flag(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags |= (flag)); \ } while(0)#define ast_clear_flag(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags &= ~(flag)); \ } while(0)#define ast_copy_flags(dest,src,flagz) do { \ typeof ((dest)->flags) __d = (dest)->flags; \ typeof ((src)->flags) __s = (src)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__d == &__x); \ (void) (&__s == &__x); \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0)#define ast_set2_flag(p,value,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ if (value) \ (p)->flags |= (flag); \ else \ (p)->flags &= ~(flag); \ } while (0)#define ast_set_flags_to(p,flag,value) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ (p)->flags &= ~(flag); \ (p)->flags |= (value); \ } while (0)/* The following 64-bit flag code can most likely be erased after app_dial is reorganized to either reduce the large number of options, or handle them in some other way. At the time of this writing, app_dial would be the only user of 64-bit option flags */extern uint64_t __unsigned_int_flags_dummy64;#define ast_test_flag64(p,flag) ({ \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags & (flag)); \ })#define ast_set_flag64(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags |= (flag)); \ } while(0)#define ast_clear_flag64(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags &= ~(flag)); \ } while(0)#define ast_copy_flags64(dest,src,flagz) do { \ typeof ((dest)->flags) __d = (dest)->flags; \ typeof ((src)->flags) __s = (src)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__d == &__x); \ (void) (&__s == &__x); \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0)#define ast_set2_flag64(p,value,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__p == &__x); \ if (value) \ (p)->flags |= (flag); \ else \ (p)->flags &= ~(flag); \ } while (0)#define ast_set_flags_to64(p,flag,value) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy64) __x = 0; \ (void) (&__p == &__x); \ (p)->flags &= ~(flag); \ (p)->flags |= (value); \ } while (0)/* Non-type checking variations for non-unsigned int flags. You should only use non-unsigned int flags where required by protocol etc and if you know what you're doing :) */#define ast_test_flag_nonstd(p,flag) \ ((p)->flags & (flag))#define ast_set_flag_nonstd(p,flag) do { \ ((p)->flags |= (flag)); \ } while(0)#define ast_clear_flag_nonstd(p,flag) do { \ ((p)->flags &= ~(flag)); \ } while(0)#define ast_copy_flags_nonstd(dest,src,flagz) do { \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0)#define ast_set2_flag_nonstd(p,value,flag) do { \ if (value) \ (p)->flags |= (flag); \ else \ (p)->flags &= ~(flag); \ } while (0)#define AST_FLAGS_ALL UINT_MAX/*! \brief Structure used to handle boolean flags */struct ast_flags { unsigned int flags;};/*! \brief Structure used to handle a large number of boolean flags == used only in app_dial?*/struct ast_flags64 { uint64_t flags;};struct ast_hostent { struct hostent hp; char buf[1024];};/*! \brief Thread-safe gethostbyname function to use in Asterisk */struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);/*! \brief Produces MD5 hash based on input string */void ast_md5_hash(char *output, char *input);/*! \brief Produces SHA1 hash based on input string */void ast_sha1_hash(char *output, char *input);int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks);/*! * \brief Encode data in base64 * \param dst the destination buffer * \param src the source data to be encoded * \param srclen the number of bytes present in the source buffer * \param max the maximum number of bytes to write into the destination * buffer, *including* the terminating NULL character. */int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);/*! * \brief Decode data from base64 * \param dst the destination buffer * \param src the source buffer * \param max The maximum number of bytes to write into the destination * buffer. Note that this function will not ensure that the * destination buffer is NULL terminated. So, in general, * this parameter should be sizeof(dst) - 1. */int ast_base64decode(unsigned char *dst, const char *src, int max);/*! \brief Turn text string to URI-encoded %XX version \note At this point, we're converting from ISO-8859-x (8-bit), not UTF8 as in the SIP protocol spec If doreserved == 1 we will convert reserved characters also. RFC 2396, section 2.4 outbuf needs to have more memory allocated than the instring to have room for the expansion. Every char that is converted is replaced by three ASCII characters. \param string String to be converted \param outbuf Resulting encoded string \param buflen Size of output buffer \param doreserved Convert reserved characters*/char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved);/*! \brief Decode URI, URN, URL (overwrite string) \param s String to be decoded */void ast_uri_decode(char *s);static force_inline void ast_slinear_saturated_add(short *input, short *value){ int res; res = (int) *input + *value; if (res > 32767) *input = 32767; else if (res < -32767) *input = -32767; else *input = (short) res;}static force_inline void ast_slinear_saturated_subtract(short *input, short *value){ int res; res = (int) *input - *value; if (res > 32767) *input = 32767; else if (res < -32767) *input = -32767; else *input = (short) res;} static force_inline void ast_slinear_saturated_multiply(short *input, short *value){ int res; res = (int) *input * *value; if (res > 32767) *input = 32767; else if (res < -32767) *input = -32767; else *input = (short) res;}static force_inline void ast_slinear_saturated_divide(short *input, short *value){ *input /= *value;}#ifdef localtime_r#undef localtime_r#endif#define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__int ast_utils_init(void);int ast_wait_for_input(int fd, int ms);/*! \brief Try to write string, but wait no more than ms milliseconds before timing out. \note If you are calling ast_carefulwrite, it is assumed that you are calling it on a file descriptor that _DOES_ have NONBLOCK set. This way, there is only one system call made to do a write, unless we actually have a need to wait. This way, we get better performance.*/int ast_carefulwrite(int fd, char *s, int len, int timeoutms);/*! * \brief Write data to a file stream with a timeout * * \param f the file stream to write to * \param fd the file description to poll on to know when the file stream can * be written to without blocking. * \param s the buffer to write from * \param len the number of bytes to write * \param timeoutms The maximum amount of time to block in this function trying * to write, specified in milliseconds. * * \note This function assumes that the associated file stream has been set up * as non-blocking. * * \retval 0 success * \retval -1 error */int ast_careful_fwrite(FILE *f, int fd, const char *s, size_t len, int timeoutms);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -