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