hash.h 8.2 KB

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