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