hash.h 8.3 KB

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