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