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