hash.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * @ingroup lavu_hash_generic
  23. * Generic hashing API
  24. */
  25. #ifndef AVUTIL_HASH_H
  26. #define AVUTIL_HASH_H
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include "version.h"
  30. /**
  31. * @defgroup lavu_hash Hash Functions
  32. * @ingroup lavu_crypto
  33. * Hash functions useful in multimedia.
  34. *
  35. * Hash functions are widely used in multimedia, from error checking and
  36. * concealment to internal regression testing. libavutil has efficient
  37. * implementations of a variety of hash functions that may be useful for
  38. * FFmpeg and other multimedia applications.
  39. *
  40. * @{
  41. *
  42. * @defgroup lavu_hash_generic Generic Hashing API
  43. * An abstraction layer for all hash functions supported by libavutil.
  44. *
  45. * If your application needs to support a wide range of different hash
  46. * functions, then the Generic Hashing API is for you. It provides a generic,
  47. * reusable API for @ref lavu_hash "all hash functions" implemented in libavutil.
  48. * If you just need to use one particular hash function, use the @ref lavu_hash
  49. * "individual hash" directly.
  50. *
  51. * @section Sample Code
  52. *
  53. * A basic template for using the Generic Hashing API follows:
  54. *
  55. * @code
  56. * struct AVHashContext *ctx = NULL;
  57. * const char *hash_name = NULL;
  58. * uint8_t *output_buf = NULL;
  59. *
  60. * // Select from a string returned by av_hash_names()
  61. * hash_name = ...;
  62. *
  63. * // Allocate a hash context
  64. * ret = av_hash_alloc(&ctx, hash_name);
  65. * if (ret < 0)
  66. * return ret;
  67. *
  68. * // Initialize the hash context
  69. * av_hash_init(ctx);
  70. *
  71. * // Update the hash context with data
  72. * while (data_left) {
  73. * av_hash_update(ctx, data, size);
  74. * }
  75. *
  76. * // Now we have no more data, so it is time to finalize the hash and get the
  77. * // output. But we need to first allocate an output buffer. Note that you can
  78. * // use any memory allocation function, including malloc(), not just
  79. * // av_malloc().
  80. * output_buf = av_malloc(av_hash_get_size(ctx));
  81. * if (!output_buf)
  82. * return AVERROR(ENOMEM);
  83. *
  84. * // Finalize the hash context.
  85. * // You can use any of the av_hash_final*() functions provided, for other
  86. * // output formats. If you do so, be sure to adjust the memory allocation
  87. * // above. See the function documentation below for the exact amount of extra
  88. * // memory needed.
  89. * av_hash_final(ctx, output_buffer);
  90. *
  91. * // Free the context
  92. * av_hash_freep(&ctx);
  93. * @endcode
  94. *
  95. * @section Hash Function-Specific Information
  96. * If the CRC32 hash is selected, the #AV_CRC_32_IEEE polynomial will be
  97. * used.
  98. *
  99. * If the Murmur3 hash is selected, the default seed will be used. See @ref
  100. * lavu_murmur3_seedinfo "Murmur3" for more information.
  101. *
  102. * @{
  103. */
  104. /**
  105. * @example ffhash.c
  106. * This example is a simple command line application that takes one or more
  107. * arguments. It demonstrates a typical use of the hashing API with allocation,
  108. * initialization, updating, and finalizing.
  109. */
  110. struct AVHashContext;
  111. /**
  112. * Allocate a hash context for the algorithm specified by name.
  113. *
  114. * @return >= 0 for success, a negative error code for failure
  115. *
  116. * @note The context is not initialized after a call to this function; you must
  117. * call av_hash_init() to do so.
  118. */
  119. int av_hash_alloc(struct AVHashContext **ctx, const char *name);
  120. /**
  121. * Get the names of available hash algorithms.
  122. *
  123. * This function can be used to enumerate the algorithms.
  124. *
  125. * @param[in] i Index of the hash algorithm, starting from 0
  126. * @return Pointer to a static string or `NULL` if `i` is out of range
  127. */
  128. const char *av_hash_names(int i);
  129. /**
  130. * Get the name of the algorithm corresponding to the given hash context.
  131. */
  132. const char *av_hash_get_name(const struct AVHashContext *ctx);
  133. /**
  134. * Maximum value that av_hash_get_size() will currently return.
  135. *
  136. * You can use this if you absolutely want or need to use static allocation for
  137. * the output buffer and are fine with not supporting hashes newly added to
  138. * libavutil without recompilation.
  139. *
  140. * @warning
  141. * Adding new hashes with larger sizes, and increasing the macro while doing
  142. * so, will not be considered an ABI change. To prevent your code from
  143. * overflowing a buffer, either dynamically allocate the output buffer with
  144. * av_hash_get_size(), or limit your use of the Hashing API to hashes that are
  145. * already in FFmpeg during the time of compilation.
  146. */
  147. #define AV_HASH_MAX_SIZE 64
  148. /**
  149. * Get the size of the resulting hash value in bytes.
  150. *
  151. * The maximum value this function will currently return is available as macro
  152. * #AV_HASH_MAX_SIZE.
  153. *
  154. * @param[in] ctx Hash context
  155. * @return Size of the hash value in bytes
  156. */
  157. int av_hash_get_size(const struct AVHashContext *ctx);
  158. /**
  159. * Initialize or reset a hash context.
  160. *
  161. * @param[in,out] ctx Hash context
  162. */
  163. void av_hash_init(struct AVHashContext *ctx);
  164. /**
  165. * Update a hash context with additional data.
  166. *
  167. * @param[in,out] ctx Hash context
  168. * @param[in] src Data to be added to the hash context
  169. * @param[in] len Size of the additional data
  170. */
  171. #if FF_API_CRYPTO_SIZE_T
  172. void av_hash_update(struct AVHashContext *ctx, const uint8_t *src, int len);
  173. #else
  174. void av_hash_update(struct AVHashContext *ctx, const uint8_t *src, size_t len);
  175. #endif
  176. /**
  177. * Finalize a hash context and compute the actual hash value.
  178. *
  179. * The minimum size of `dst` buffer is given by av_hash_get_size() or
  180. * #AV_HASH_MAX_SIZE. The use of the latter macro is discouraged.
  181. *
  182. * It is not safe to update or finalize a hash context again, if it has already
  183. * been finalized.
  184. *
  185. * @param[in,out] ctx Hash context
  186. * @param[out] dst Where the final hash value will be stored
  187. *
  188. * @see av_hash_final_bin() provides an alternative API
  189. */
  190. void av_hash_final(struct AVHashContext *ctx, uint8_t *dst);
  191. /**
  192. * Finalize a hash context and store the actual hash value in a buffer.
  193. *
  194. * It is not safe to update or finalize a hash context again, if it has already
  195. * been finalized.
  196. *
  197. * If `size` is smaller than the hash size (given by av_hash_get_size()), the
  198. * hash is truncated; if size is larger, the buffer is padded with 0.
  199. *
  200. * @param[in,out] ctx Hash context
  201. * @param[out] dst Where the final hash value will be stored
  202. * @param[in] size Number of bytes to write to `dst`
  203. */
  204. void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size);
  205. /**
  206. * Finalize a hash context and store the hexadecimal representation of the
  207. * actual hash value as a string.
  208. *
  209. * It is not safe to update or finalize a hash context again, if it has already
  210. * been finalized.
  211. *
  212. * The string is always 0-terminated.
  213. *
  214. * If `size` is smaller than `2 * hash_size + 1`, where `hash_size` is the
  215. * value returned by av_hash_get_size(), the string will be truncated.
  216. *
  217. * @param[in,out] ctx Hash context
  218. * @param[out] dst Where the string will be stored
  219. * @param[in] size Maximum number of bytes to write to `dst`
  220. */
  221. void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size);
  222. /**
  223. * Finalize a hash context and store the Base64 representation of the
  224. * actual hash value as a string.
  225. *
  226. * It is not safe to update or finalize a hash context again, if it has already
  227. * been finalized.
  228. *
  229. * The string is always 0-terminated.
  230. *
  231. * If `size` is smaller than AV_BASE64_SIZE(hash_size), where `hash_size` is
  232. * the value returned by av_hash_get_size(), the string will be truncated.
  233. *
  234. * @param[in,out] ctx Hash context
  235. * @param[out] dst Where the final hash value will be stored
  236. * @param[in] size Maximum number of bytes to write to `dst`
  237. */
  238. void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size);
  239. /**
  240. * Free hash context and set hash context pointer to `NULL`.
  241. *
  242. * @param[in,out] ctx Pointer to hash context
  243. */
  244. void av_hash_freep(struct AVHashContext **ctx);
  245. /**
  246. * @}
  247. * @}
  248. */
  249. #endif /* AVUTIL_HASH_H */