1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.Checksum;
26 
27 private import glib.Bytes;
28 private import glib.ConstructionException;
29 private import glib.Str;
30 private import glib.c.functions;
31 public  import glib.c.types;
32 private import gtkd.Loader;
33 
34 
35 /**
36  * An opaque structure representing a checksumming operation.
37  * To create a new GChecksum, use g_checksum_new(). To free
38  * a GChecksum, use g_checksum_free().
39  *
40  * Since: 2.16
41  */
42 public class Checksum
43 {
44 	/** the main Gtk struct */
45 	protected GChecksum* gChecksum;
46 	protected bool ownedRef;
47 
48 	/** Get the main Gtk struct */
49 	public GChecksum* getChecksumStruct(bool transferOwnership = false)
50 	{
51 		if (transferOwnership)
52 			ownedRef = false;
53 		return gChecksum;
54 	}
55 
56 	/** the main Gtk struct as a void* */
57 	protected void* getStruct()
58 	{
59 		return cast(void*)gChecksum;
60 	}
61 
62 	/**
63 	 * Sets our main struct and passes it to the parent class.
64 	 */
65 	public this (GChecksum* gChecksum, bool ownedRef = false)
66 	{
67 		this.gChecksum = gChecksum;
68 		this.ownedRef = ownedRef;
69 	}
70 
71 	~this ()
72 	{
73 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
74 			g_checksum_free(gChecksum);
75 	}
76 
77 	/**
78 	 * Gets the digest from checksum as a raw binary vector and places it
79 	 * into buffer. The size of the digest depends on the type of checksum.
80 	 *
81 	 * Once this function has been called, the Checksum is closed and can
82 	 * no longer be updated with update().
83 	 *
84 	 * Params:
85 	 *     buffer = output buffer
86 	 *     digestLen = an inout parameter. The caller initializes it to the size of buffer.
87 	 *         After the call it contains the length of the digest.
88 	 *
89 	 * Since: 2.16
90 	 */
91 	public void getDigest(ref ubyte[] buffer)
92 	{
93 		size_t digestLen = buffer.length;
94 
95 		g_checksum_get_digest(gChecksum, buffer.ptr, &digestLen);
96 
97 		buffer = buffer[0 .. digestLen];
98 	}
99 
100 	/**
101 	 */
102 
103 	/**
104 	 * Creates a new #GChecksum, using the checksum algorithm @checksum_type.
105 	 * If the @checksum_type is not known, %NULL is returned.
106 	 * A #GChecksum can be used to compute the checksum, or digest, of an
107 	 * arbitrary binary blob, using different hashing algorithms.
108 	 *
109 	 * A #GChecksum works by feeding a binary blob through g_checksum_update()
110 	 * until there is data to be checked; the digest can then be extracted
111 	 * using g_checksum_get_string(), which will return the checksum as a
112 	 * hexadecimal string; or g_checksum_get_digest(), which will return a
113 	 * vector of raw bytes. Once either g_checksum_get_string() or
114 	 * g_checksum_get_digest() have been called on a #GChecksum, the checksum
115 	 * will be closed and it won't be possible to call g_checksum_update()
116 	 * on it anymore.
117 	 *
118 	 * Params:
119 	 *     checksumType = the desired type of checksum
120 	 *
121 	 * Returns: the newly created #GChecksum, or %NULL.
122 	 *     Use g_checksum_free() to free the memory allocated by it.
123 	 *
124 	 * Since: 2.16
125 	 *
126 	 * Throws: ConstructionException GTK+ fails to create the object.
127 	 */
128 	public this(GChecksumType checksumType)
129 	{
130 		auto __p = g_checksum_new(checksumType);
131 
132 		if(__p is null)
133 		{
134 			throw new ConstructionException("null returned by new");
135 		}
136 
137 		this(cast(GChecksum*) __p);
138 	}
139 
140 	/**
141 	 * Copies a #GChecksum. If @checksum has been closed, by calling
142 	 * g_checksum_get_string() or g_checksum_get_digest(), the copied
143 	 * checksum will be closed as well.
144 	 *
145 	 * Returns: the copy of the passed #GChecksum. Use
146 	 *     g_checksum_free() when finished using it.
147 	 *
148 	 * Since: 2.16
149 	 */
150 	public Checksum copy()
151 	{
152 		auto __p = g_checksum_copy(gChecksum);
153 
154 		if(__p is null)
155 		{
156 			return null;
157 		}
158 
159 		return new Checksum(cast(GChecksum*) __p, true);
160 	}
161 
162 	/**
163 	 * Frees the memory allocated for @checksum.
164 	 *
165 	 * Since: 2.16
166 	 */
167 	public void free()
168 	{
169 		g_checksum_free(gChecksum);
170 		ownedRef = false;
171 	}
172 
173 	/**
174 	 * Gets the digest as a hexadecimal string.
175 	 *
176 	 * Once this function has been called the #GChecksum can no longer be
177 	 * updated with g_checksum_update().
178 	 *
179 	 * The hexadecimal characters will be lower case.
180 	 *
181 	 * Returns: the hexadecimal representation of the checksum. The
182 	 *     returned string is owned by the checksum and should not be modified
183 	 *     or freed.
184 	 *
185 	 * Since: 2.16
186 	 */
187 	public string getString()
188 	{
189 		return Str.toString(g_checksum_get_string(gChecksum));
190 	}
191 
192 	/**
193 	 * Resets the state of the @checksum back to its initial state.
194 	 *
195 	 * Since: 2.18
196 	 */
197 	public void reset()
198 	{
199 		g_checksum_reset(gChecksum);
200 	}
201 
202 	/**
203 	 * Feeds @data into an existing #GChecksum. The checksum must still be
204 	 * open, that is g_checksum_get_string() or g_checksum_get_digest() must
205 	 * not have been called on @checksum.
206 	 *
207 	 * Params:
208 	 *     data = buffer used to compute the checksum
209 	 *
210 	 * Since: 2.16
211 	 */
212 	public void update(char[] data)
213 	{
214 		g_checksum_update(gChecksum, data.ptr, cast(ptrdiff_t)data.length);
215 	}
216 
217 	/**
218 	 * Gets the length in bytes of digests of type @checksum_type
219 	 *
220 	 * Params:
221 	 *     checksumType = a #GChecksumType
222 	 *
223 	 * Returns: the checksum length, or -1 if @checksum_type is
224 	 *     not supported.
225 	 *
226 	 * Since: 2.16
227 	 */
228 	public static ptrdiff_t typeGetLength(GChecksumType checksumType)
229 	{
230 		return g_checksum_type_get_length(checksumType);
231 	}
232 
233 	/**
234 	 * Computes the checksum for a binary @data. This is a
235 	 * convenience wrapper for g_checksum_new(), g_checksum_get_string()
236 	 * and g_checksum_free().
237 	 *
238 	 * The hexadecimal string returned will be in lower case.
239 	 *
240 	 * Params:
241 	 *     checksumType = a #GChecksumType
242 	 *     data = binary blob to compute the digest of
243 	 *
244 	 * Returns: the digest of the binary data as a
245 	 *     string in hexadecimal, or %NULL if g_checksum_new() fails for
246 	 *     @checksum_type. The returned string should be freed with g_free() when
247 	 *     done using it.
248 	 *
249 	 * Since: 2.34
250 	 */
251 	public static string computeChecksumForBytes(GChecksumType checksumType, Bytes data)
252 	{
253 		auto retStr = g_compute_checksum_for_bytes(checksumType, (data is null) ? null : data.getBytesStruct());
254 
255 		scope(exit) Str.freeString(retStr);
256 		return Str.toString(retStr);
257 	}
258 
259 	/**
260 	 * Computes the checksum for a binary @data of @length. This is a
261 	 * convenience wrapper for g_checksum_new(), g_checksum_get_string()
262 	 * and g_checksum_free().
263 	 *
264 	 * The hexadecimal string returned will be in lower case.
265 	 *
266 	 * Params:
267 	 *     checksumType = a #GChecksumType
268 	 *     data = binary blob to compute the digest of
269 	 *
270 	 * Returns: the digest of the binary data as a
271 	 *     string in hexadecimal, or %NULL if g_checksum_new() fails for
272 	 *     @checksum_type. The returned string should be freed with g_free() when
273 	 *     done using it.
274 	 *
275 	 * Since: 2.16
276 	 */
277 	public static string computeChecksumForData(GChecksumType checksumType, char[] data)
278 	{
279 		auto retStr = g_compute_checksum_for_data(checksumType, data.ptr, cast(size_t)data.length);
280 
281 		scope(exit) Str.freeString(retStr);
282 		return Str.toString(retStr);
283 	}
284 
285 	/**
286 	 * Computes the checksum of a string.
287 	 *
288 	 * The hexadecimal string returned will be in lower case.
289 	 *
290 	 * Params:
291 	 *     checksumType = a #GChecksumType
292 	 *     str = the string to compute the checksum of
293 	 *     length = the length of the string, or -1 if the string is null-terminated.
294 	 *
295 	 * Returns: the checksum as a hexadecimal string,
296 	 *     or %NULL if g_checksum_new() fails for @checksum_type. The returned string
297 	 *     should be freed with g_free() when done using it.
298 	 *
299 	 * Since: 2.16
300 	 */
301 	public static string computeChecksumForString(GChecksumType checksumType, string str, ptrdiff_t length)
302 	{
303 		auto retStr = g_compute_checksum_for_string(checksumType, Str.toStringz(str), length);
304 
305 		scope(exit) Str.freeString(retStr);
306 		return Str.toString(retStr);
307 	}
308 }