zrtp_string.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. #ifndef __ZRTP_STRING_H__
  10. #define __ZRTP_STRING_H__
  11. #include "zrtp_config.h"
  12. /**
  13. * \file zrtp_strings.h
  14. * \brief libzrtp safe strings
  15. */
  16. /*============================================================================*/
  17. /* Libzrtp Strings */
  18. /*============================================================================*/
  19. #define ZRTP_STRING8 12
  20. #define ZRTP_STRING16 20
  21. #define ZRTP_STRING32 36
  22. #define ZRTP_STRING64 68
  23. #define ZRTP_STRING128 132
  24. #define ZRTP_STRING256 260
  25. #define ZRTP_STRING1024 1028
  26. #if ( ZRTP_PLATFORM != ZP_SYMBIAN )
  27. #pragma pack(push, 1)
  28. #endif
  29. typedef struct zrtp_stringn
  30. {
  31. uint16_t length;
  32. uint16_t max_length;
  33. char buffer[0];
  34. } zrtp_stringn_t;
  35. typedef struct zrtp_string8
  36. {
  37. uint16_t length;
  38. uint16_t max_length;
  39. char buffer[ZRTP_STRING8];
  40. } zrtp_string8_t;
  41. typedef struct zrtp_string16
  42. {
  43. uint16_t length;
  44. uint16_t max_length;
  45. char buffer[ZRTP_STRING16];
  46. } zrtp_string16_t;
  47. typedef struct zrtp_string32
  48. {
  49. uint16_t length;
  50. uint16_t max_length;
  51. char buffer[ZRTP_STRING32];
  52. } zrtp_string32_t;
  53. typedef struct zrtp_string64
  54. {
  55. uint16_t length;
  56. uint16_t max_length;
  57. char buffer[ZRTP_STRING64];
  58. } zrtp_string64_t;
  59. typedef struct zrtp_string128
  60. {
  61. uint16_t length;
  62. uint16_t max_length;
  63. char buffer[ZRTP_STRING128];
  64. } zrtp_string128_t;
  65. typedef struct zrtp_string256
  66. {
  67. uint16_t length;
  68. uint16_t max_length;
  69. char buffer[ZRTP_STRING256];
  70. } zrtp_string256_t;
  71. typedef struct zrtp_string1024
  72. {
  73. uint16_t length;
  74. uint16_t max_length;
  75. char buffer[ZRTP_STRING1024];
  76. } zrtp_string1024_t;
  77. #if ( ZRTP_PLATFORM != ZP_SYMBIAN )
  78. #pragma pack(pop)
  79. #endif
  80. /**
  81. * \defgroup zrtp_strings Libzrtp Safe Strings
  82. *
  83. * Using standard C-like strings is potentially dangerous in any program. All standard functions for
  84. * working with c-strings rely on zero-termination, since c-strings don't contain a representation
  85. * of their length. This can cause many mistakes. Moreover, it is impossible to use these strings
  86. * for storing binary data.
  87. *
  88. * To solve these problems libzrtp uses zstrings instead of normal c-strings. A zstring is just a
  89. * wrapped c-string that stores its own length. Use the following data types, macros and utility
  90. * functions for working with zstrings in your applications.
  91. *
  92. * zstrings are easy to use, and at the same time light-weight and flexible.
  93. * We use two groups of zstring types:
  94. * \li zrtp_stringn_t - base type for all operations with zstrings;
  95. * \li zrtp_stringXX_t group - storage types.
  96. *
  97. * One can use any zrtp_stringXX_t type (big enough to store necessary data) esired and operate with
  98. * it using global zstring functions. To cast zrtp_stringXX_t to zrtp_stringn_t, the \ref ZSTR_GV
  99. * and \ref ZSTR_GVP macros can be used.
  100. *
  101. * The main principle of running zstrings is storing its current data size. So to avoid mistakes and
  102. * mess it is advised to use preestablished initialization macros. The description of each follows.
  103. * \{
  104. */
  105. /**
  106. * \brief Casts zrtp_stringXX_t to a pointer to zrtp_stringn_t.
  107. *
  108. * This macro prevents static casts caused by using zstring functions. Prevents mistakes and makes
  109. * zstrings safer to use.
  110. * \sa ZSTR_GVP
  111. */
  112. #define ZSTR_GV(pstr) \
  113. (zrtp_stringn_t*)((char*)pstr.buffer - sizeof(pstr.max_length) - sizeof(pstr.length))
  114. /**
  115. * \brief Casts zrtp_stringXX_t* to a pointer to zrtp_stringn_t.
  116. *
  117. * This macro prevents static casts from using zstring functions.
  118. * \sa ZSTR_GV
  119. */
  120. #define ZSTR_GVP(pstr) \
  121. (zrtp_stringn_t*)((char*)pstr->buffer - sizeof(pstr->max_length) - sizeof(pstr->length))
  122. /**
  123. * \brief Macro for empty zstring initialization
  124. * \warning Use this macro on every zrtp_string structure allocation.
  125. * usage: \code zrtp_string_t zstr = ZSTR_INIT_EMPTY(zstr); \endcode
  126. */
  127. #define ZSTR_INIT_EMPTY(a) { 0, sizeof(a.buffer) - 1, { 0 }}
  128. /**
  129. * \brief Macro for zstring initialization from a constant C-string
  130. * usage: \code zrtp_string_t zstr = ZSTR_INIT_WITH_CONST_CSTRING("zstring use example"); \endcode
  131. */
  132. #define ZSTR_INIT_WITH_CONST_CSTRING(s) {sizeof(s) - 1, 0, s}
  133. /**
  134. * \brief Macro for zstring clearing
  135. *
  136. * Use this macro for initializing already created zstrings
  137. * usage: \code ZSTR_SET_EMPTY(zstr); \endcode
  138. */
  139. #define ZSTR_SET_EMPTY(a)\
  140. { a.length = 0; a.max_length = sizeof(a.buffer) - 1; a.buffer[0] = 0; }
  141. #if defined(__cplusplus)
  142. extern "C"
  143. {
  144. #endif
  145. /**
  146. * \brief compare two zstrings
  147. *
  148. * Function compares the two strings left and right.
  149. * \param left - one string for comparing;
  150. * \param right - the other string for comparing.
  151. * \return
  152. * - -1 if left string less than right;
  153. * - 0 if left string is equal to right;
  154. * - 1 if left string greater than right.
  155. */
  156. int zrtp_zstrcmp(const zrtp_stringn_t *left, const zrtp_stringn_t *right);
  157. /**
  158. * \brief Copy a zstring
  159. *
  160. * The zrtp_zstrcpy function copies the string pointed by src to the structure pointed to by dst.
  161. * \param src source string;
  162. * \param dst destination string.
  163. */
  164. void zrtp_zstrcpy(zrtp_stringn_t *dst, const zrtp_stringn_t *src);
  165. /**
  166. * \brief Copy first N bytes of zstring
  167. *
  168. * The zrtp_zstrncpy function copies the first N bytes from the string pointed to by src to the
  169. * structure pointed by dst.
  170. * \param src - source string;
  171. * \param dst - destination string;
  172. * \param size - nuber of bytes to copy.
  173. */
  174. void zrtp_zstrncpy(zrtp_stringn_t *dst, const zrtp_stringn_t *src, uint16_t size);
  175. /**
  176. * @brief Copy a c-string into a z-string
  177. * \param dst - destination zsyring
  178. * \param src - source c-string to be copied.
  179. */
  180. void zrtp_zstrcpyc(zrtp_stringn_t *dst, const char *src);
  181. /**
  182. * \brief Copy first N bytes of a c-string into a z-string
  183. * \param dst - destination zsyring
  184. * \param src - source c-string to be copied.
  185. * \param size - number of bytes to be copied from \c src to \c dst
  186. */
  187. void zrtp_zstrncpyc(zrtp_stringn_t *dst, const char *src, uint16_t size);
  188. /**
  189. * \brief Concatenate two strings
  190. *
  191. * The zrtp_zstrcat function appends the src string to the dst string. If dst string doesn't have
  192. * enough space it will be truncated.
  193. * \param src source string;
  194. * \param dst destination string.
  195. */
  196. void zrtp_zstrcat(zrtp_stringn_t *dst, const zrtp_stringn_t *src);
  197. /**
  198. * \brief Clear a zstring
  199. * \param zstr - string for clearing;
  200. */
  201. void zrtp_wipe_zstring(zrtp_stringn_t *zstr);
  202. /**
  203. * \brief Compare two binary strings
  204. *
  205. * This function is used to prevent errors caused by other, non byte-to-byte comparison
  206. * implementations. The secret sorting function is sensitive to such things.
  207. *
  208. * \param s1 - first string for comparison
  209. * \param s2 - second string for comparison
  210. * \param n - number of bytes to be compared
  211. * \return - an integer less than, equal to, or greater than zero, if the first n bytes of s1
  212. * is found, respectively, to be less than, to match, or to be greater than the first n bytes of s2.
  213. */
  214. int zrtp_memcmp(const void* s1, const void* s2, uint32_t n);
  215. /**
  216. * \brief Converts binary data to the hex string representation
  217. *
  218. * \param bin - pointer to the binary buffer for converting;
  219. * \param bin_size - binary data size;
  220. * \param buff - destination buffer;
  221. * \param buff_size - destination buffer size.
  222. * \return
  223. * - pointer to the buff with converted data;
  224. * - "Buffer too small" in case of error.
  225. */
  226. const char* hex2str(const char* bin, int bin_size, char* buff, int buff_size);
  227. /**
  228. * \brief Converts hex string to the binary representation
  229. *
  230. * \param buff - source buffer for converting;
  231. * \param buff_size - source buffer size;
  232. * \param bin - pointer to the destination binary buffer;
  233. * \param bin_size - binary data size;
  234. * \return
  235. * - pointer to the buff with converted data, or NULL in case of error.
  236. */
  237. char *str2hex(const char* buff, int buff_size, char* bin, int bin_size);
  238. #if defined(__cplusplus)
  239. }
  240. #endif
  241. /** \} */
  242. #endif /* __ZRTP_STRING_H__ */