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 /**
22  * @file
23  * @ingroup lavu_hash_generic
24  * Generic hashing API
25  */
26 module ffmpeg.libavutil.hash;
27 extern (C) @nogc nothrow:
28 
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 /**
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 
111 struct AVHashContext;
112 
113 /**
114  * Allocate a hash context for the algorithm specified by name.
115  *
116  * @return  >= 0 for success, a negative error code for failure
117  *
118  * @note The context is not initialized after a call to this function; you must
119  * call av_hash_init() to do so.
120  */
121 int av_hash_alloc (AVHashContext** ctx, const(char)* name);
122 
123 /**
124  * Get the names of available hash algorithms.
125  *
126  * This function can be used to enumerate the algorithms.
127  *
128  * @param[in] i  Index of the hash algorithm, starting from 0
129  * @return       Pointer to a static string or `NULL` if `i` is out of range
130  */
131 const(char)* av_hash_names (int i);
132 
133 /**
134  * Get the name of the algorithm corresponding to the given hash context.
135  */
136 const(char)* av_hash_get_name (const(AVHashContext)* ctx);
137 
138 /**
139  * Maximum value that av_hash_get_size() will currently return.
140  *
141  * You can use this if you absolutely want or need to use static allocation for
142  * the output buffer and are fine with not supporting hashes newly added to
143  * libavutil without recompilation.
144  *
145  * @warning
146  * Adding new hashes with larger sizes, and increasing the macro while doing
147  * so, will not be considered an ABI change. To prevent your code from
148  * overflowing a buffer, either dynamically allocate the output buffer with
149  * av_hash_get_size(), or limit your use of the Hashing API to hashes that are
150  * already in FFmpeg during the time of compilation.
151  */
152 enum AV_HASH_MAX_SIZE = 64;
153 
154 /**
155  * Get the size of the resulting hash value in bytes.
156  *
157  * The maximum value this function will currently return is available as macro
158  * #AV_HASH_MAX_SIZE.
159  *
160  * @param[in]     ctx Hash context
161  * @return            Size of the hash value in bytes
162  */
163 int av_hash_get_size (const(AVHashContext)* ctx);
164 
165 /**
166  * Initialize or reset a hash context.
167  *
168  * @param[in,out] ctx Hash context
169  */
170 void av_hash_init (AVHashContext* ctx);
171 
172 /**
173  * Update a hash context with additional data.
174  *
175  * @param[in,out] ctx Hash context
176  * @param[in]     src Data to be added to the hash context
177  * @param[in]     len Size of the additional data
178  */
179 void av_hash_update (AVHashContext* ctx, const(ubyte)* src, int len);
180 
181 /**
182  * Finalize a hash context and compute the actual hash value.
183  *
184  * The minimum size of `dst` buffer is given by av_hash_get_size() or
185  * #AV_HASH_MAX_SIZE. The use of the latter macro is discouraged.
186  *
187  * It is not safe to update or finalize a hash context again, if it has already
188  * been finalized.
189  *
190  * @param[in,out] ctx Hash context
191  * @param[out]    dst Where the final hash value will be stored
192  *
193  * @see av_hash_final_bin() provides an alternative API
194  */
195 void av_hash_final (AVHashContext* ctx, ubyte* dst);
196 
197 /**
198  * Finalize a hash context and store the actual hash value in a buffer.
199  *
200  * It is not safe to update or finalize a hash context again, if it has already
201  * been finalized.
202  *
203  * If `size` is smaller than the hash size (given by av_hash_get_size()), the
204  * hash is truncated; if size is larger, the buffer is padded with 0.
205  *
206  * @param[in,out] ctx  Hash context
207  * @param[out]    dst  Where the final hash value will be stored
208  * @param[in]     size Number of bytes to write to `dst`
209  */
210 void av_hash_final_bin (AVHashContext* ctx, ubyte* dst, int size);
211 
212 /**
213  * Finalize a hash context and store the hexadecimal representation of the
214  * actual hash value as a string.
215  *
216  * It is not safe to update or finalize a hash context again, if it has already
217  * been finalized.
218  *
219  * The string is always 0-terminated.
220  *
221  * If `size` is smaller than `2 * hash_size + 1`, where `hash_size` is the
222  * value returned by av_hash_get_size(), the string will be truncated.
223  *
224  * @param[in,out] ctx  Hash context
225  * @param[out]    dst  Where the string will be stored
226  * @param[in]     size Maximum number of bytes to write to `dst`
227  */
228 void av_hash_final_hex (AVHashContext* ctx, ubyte* dst, int size);
229 
230 /**
231  * Finalize a hash context and store the Base64 representation of the
232  * actual hash value as a string.
233  *
234  * It is not safe to update or finalize a hash context again, if it has already
235  * been finalized.
236  *
237  * The string is always 0-terminated.
238  *
239  * If `size` is smaller than AV_BASE64_SIZE(hash_size), where `hash_size` is
240  * the value returned by av_hash_get_size(), the string will be truncated.
241  *
242  * @param[in,out] ctx  Hash context
243  * @param[out]    dst  Where the final hash value will be stored
244  * @param[in]     size Maximum number of bytes to write to `dst`
245  */
246 void av_hash_final_b64 (AVHashContext* ctx, ubyte* dst, int size);
247 
248 /**
249  * Free hash context and set hash context pointer to `NULL`.
250  *
251  * @param[in,out] ctx  Pointer to hash context
252  */
253 void av_hash_freep (AVHashContext** ctx);
254 
255 /**
256  * @}
257  * @}
258  */
259 
260 /* AVUTIL_HASH_H */