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