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  * Conversion parameters:
26  * inFile  = glib-Data-Checksums.html
27  * outPack = glib
28  * outFile = Checksum
29  * strct   = GChecksum
30  * realStrct=
31  * ctorStrct=
32  * clss    = Checksum
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_checksum_
41  * 	- g_
42  * omit structs:
43  * omit prefixes:
44  * omit code:
45  * 	- g_checksum_get_digest
46  * omit signals:
47  * imports:
48  * 	- glib.Str
49  * 	- glib.Bytes
50  * 	- gtkc.Loader
51  * 	- gtkc.paths
52  * structWrap:
53  * 	- GBytes* -> Bytes
54  * 	- GChecksum* -> Checksum
55  * module aliases:
56  * local aliases:
57  * 	- computeChecksumForData -> computeForData
58  * 	- computeChecksumForString -> computeForString
59  * overrides:
60  */
61 
62 module glib.Checksum;
63 
64 public  import gtkc.glibtypes;
65 
66 private import gtkc.glib;
67 private import glib.ConstructionException;
68 
69 
70 private import glib.Str;
71 private import glib.Bytes;
72 private import gtkc.Loader;
73 private import gtkc.paths;
74 
75 
76 
77 
78 /**
79  * GLib provides a generic API for computing checksums (or "digests")
80  * for a sequence of arbitrary bytes, using various hashing algorithms
81  * like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
82  * environments and specifications.
83  *
84  * GLib supports incremental checksums using the GChecksum data
85  * structure, by calling g_checksum_update() as long as there's data
86  * available and then using g_checksum_get_string() or
87  * g_checksum_get_digest() to compute the checksum and return it either
88  * as a string in hexadecimal form, or as a raw sequence of bytes. To
89  * compute the checksum for binary blobs and NUL-terminated strings in
90  * one go, use the convenience functions g_compute_checksum_for_data()
91  * and g_compute_checksum_for_string(), respectively.
92  *
93  * Support for checksums has been added in GLib 2.16
94  */
95 public class Checksum
96 {
97 	
98 	/** the main Gtk struct */
99 	protected GChecksum* gChecksum;
100 	
101 	
102 	public GChecksum* getChecksumStruct()
103 	{
104 		return gChecksum;
105 	}
106 	
107 	
108 	/** the main Gtk struct as a void* */
109 	protected void* getStruct()
110 	{
111 		return cast(void*)gChecksum;
112 	}
113 	
114 	/**
115 	 * Sets our main struct and passes it to the parent class
116 	 */
117 	public this (GChecksum* gChecksum)
118 	{
119 		this.gChecksum = gChecksum;
120 	}
121 	
122 	~this ()
123 	{
124 		if ( Linker.isLoaded(LIBRARY.GLIB) && gChecksum !is null )
125 		{
126 			g_checksum_free(gChecksum);
127 		}
128 	}
129 	
130 	/**
131 	 * Gets the digest from checksum as a raw binary vector and places it
132 	 * into buffer. The size of the digest depends on the type of checksum.
133 	 * Once this function has been called, the GChecksum is closed and can
134 	 * no longer be updated with g_checksum_update().
135 	 * Since 2.16
136 	 * Params:
137 	 * buffer = output buffer
138 	 */
139 	public void getDigest(ref ubyte[] buffer)
140 	{
141 		size_t digestLen = buffer.length;
142 		
143 		// void g_checksum_get_digest (GChecksum *checksum,  guint8 *buffer,  gsize *digest_len);
144 		g_checksum_get_digest(gChecksum, buffer.ptr, &digestLen);
145 		
146 		buffer = buffer[0 .. digestLen];
147 	}
148 	
149 	/**
150 	 */
151 	
152 	/**
153 	 * Gets the length in bytes of digests of type checksum_type
154 	 * Since 2.16
155 	 * Params:
156 	 * checksumType = a GChecksumType
157 	 * Returns: the checksum length, or -1 if checksum_type is not supported.
158 	 */
159 	public static gssize typeGetLength(GChecksumType checksumType)
160 	{
161 		// gssize g_checksum_type_get_length (GChecksumType checksum_type);
162 		return g_checksum_type_get_length(checksumType);
163 	}
164 	
165 	/**
166 	 * Creates a new GChecksum, using the checksum algorithm checksum_type.
167 	 * If the checksum_type is not known, NULL is returned.
168 	 * A GChecksum can be used to compute the checksum, or digest, of an
169 	 * arbitrary binary blob, using different hashing algorithms.
170 	 * A GChecksum works by feeding a binary blob through g_checksum_update()
171 	 * until there is data to be checked; the digest can then be extracted
172 	 * using g_checksum_get_string(), which will return the checksum as a
173 	 * hexadecimal string; or g_checksum_get_digest(), which will return a
174 	 * vector of raw bytes. Once either g_checksum_get_string() or
175 	 * g_checksum_get_digest() have been called on a GChecksum, the checksum
176 	 * will be closed and it won't be possible to call g_checksum_update()
177 	 * on it anymore.
178 	 * Since 2.16
179 	 * Params:
180 	 * checksumType = the desired type of checksum
181 	 * Throws: ConstructionException GTK+ fails to create the object.
182 	 */
183 	public this (GChecksumType checksumType)
184 	{
185 		// GChecksum * g_checksum_new (GChecksumType checksum_type);
186 		auto p = g_checksum_new(checksumType);
187 		if(p is null)
188 		{
189 			throw new ConstructionException("null returned by g_checksum_new(checksumType)");
190 		}
191 		this(cast(GChecksum*) p);
192 	}
193 	
194 	/**
195 	 * Copies a GChecksum. If checksum has been closed, by calling
196 	 * g_checksum_get_string() or g_checksum_get_digest(), the copied
197 	 * checksum will be closed as well.
198 	 * Since 2.16
199 	 * Returns: the copy of the passed GChecksum. Use g_checksum_free() when finished using it.
200 	 */
201 	public Checksum copy()
202 	{
203 		// GChecksum * g_checksum_copy (const GChecksum *checksum);
204 		auto p = g_checksum_copy(gChecksum);
205 		
206 		if(p is null)
207 		{
208 			return null;
209 		}
210 		
211 		return new Checksum(cast(GChecksum*) p);
212 	}
213 	
214 	/**
215 	 * Frees the memory allocated for checksum.
216 	 * Since 2.16
217 	 */
218 	public void free()
219 	{
220 		// void g_checksum_free (GChecksum *checksum);
221 		g_checksum_free(gChecksum);
222 	}
223 	
224 	/**
225 	 * Resets the state of the checksum back to its initial state.
226 	 * Since 2.18
227 	 */
228 	public void reset()
229 	{
230 		// void g_checksum_reset (GChecksum *checksum);
231 		g_checksum_reset(gChecksum);
232 	}
233 	
234 	/**
235 	 * Feeds data into an existing GChecksum. The checksum must still be
236 	 * open, that is g_checksum_get_string() or g_checksum_get_digest() must
237 	 * not have been called on checksum.
238 	 * Since 2.16
239 	 * Params:
240 	 * data = buffer used to compute the checksum. [array length=length][element-type guint8]
241 	 */
242 	public void update(char[] data)
243 	{
244 		// void g_checksum_update (GChecksum *checksum,  const guchar *data,  gssize length);
245 		g_checksum_update(gChecksum, data.ptr, cast(int) data.length);
246 	}
247 	
248 	/**
249 	 * Gets the digest as an hexadecimal string.
250 	 * Once this function has been called the GChecksum can no longer be
251 	 * updated with g_checksum_update().
252 	 * The hexadecimal characters will be lower case.
253 	 * Since 2.16
254 	 * Returns: the hexadecimal representation of the checksum. The returned string is owned by the checksum and should not be modified or freed.
255 	 */
256 	public string getString()
257 	{
258 		// const gchar * g_checksum_get_string (GChecksum *checksum);
259 		return Str.toString(g_checksum_get_string(gChecksum));
260 	}
261 	
262 	/**
263 	 * Computes the checksum for a binary data of length. This is a
264 	 * convenience wrapper for g_checksum_new(), g_checksum_get_string()
265 	 * and g_checksum_free().
266 	 * The hexadecimal string returned will be in lower case.
267 	 * Since 2.16
268 	 * Params:
269 	 * checksumType = a GChecksumType
270 	 * data = binary blob to compute the digest of. [array length=length][element-type guint8]
271 	 * Returns: the digest of the binary data as a string in hexadecimal. The returned string should be freed with g_free() when done using it.
272 	 */
273 	public static string computeForData(GChecksumType checksumType, char[] data)
274 	{
275 		// gchar * g_compute_checksum_for_data (GChecksumType checksum_type,  const guchar *data,  gsize length);
276 		return Str.toString(g_compute_checksum_for_data(checksumType, data.ptr, cast(int) data.length));
277 	}
278 	
279 	/**
280 	 * Computes the checksum of a string.
281 	 * The hexadecimal string returned will be in lower case.
282 	 * Since 2.16
283 	 * Params:
284 	 * checksumType = a GChecksumType
285 	 * str = the string to compute the checksum of
286 	 * Returns: the checksum as a hexadecimal string. The returned string should be freed with g_free() when done using it.
287 	 */
288 	public static string computeForString(GChecksumType checksumType, string str)
289 	{
290 		// gchar * g_compute_checksum_for_string (GChecksumType checksum_type,  const gchar *str,  gssize length);
291 		return Str.toString(g_compute_checksum_for_string(checksumType, cast(char*)str.ptr, cast(int) str.length));
292 	}
293 	
294 	/**
295 	 * Computes the checksum for a binary data. This is a
296 	 * convenience wrapper for g_checksum_new(), g_checksum_get_string()
297 	 * and g_checksum_free().
298 	 * The hexadecimal string returned will be in lower case.
299 	 * Since 2.34
300 	 * Params:
301 	 * checksumType = a GChecksumType
302 	 * data = binary blob to compute the digest of
303 	 * Returns: the digest of the binary data as a string in hexadecimal. The returned string should be freed with g_free() when done using it.
304 	 */
305 	public static string computeChecksumForBytes(GChecksumType checksumType, Bytes data)
306 	{
307 		// gchar * g_compute_checksum_for_bytes (GChecksumType checksum_type,  GBytes *data);
308 		return Str.toString(g_compute_checksum_for_bytes(checksumType, (data is null) ? null : data.getBytesStruct()));
309 	}
310 }