Thread: glib & g_checksum_*

  1. #1
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19

    glib & g_checksum_*

    Though I am a glib fan, I really cannot use the g_checksum_* functions to obtain an MD5 string that is compatible with the md5sum command.
    Code:
    gchar *get_hash(const gchar *filepath) {
    	FILE *fp;
    	if (NULL != (fp = fopen(filepath, "r+b"))) {
    		guchar buffer[65536];
    		gint read_bytes;
    		gchar *result;
    		GChecksum *checksum = g_checksum_new(hash_type);
    		while ((read_bytes = fread(buffer, sizeof(buffer), 1, fp)) > 0)
    			g_checksum_update(checksum, buffer, read_bytes);
    
    		fclose(fp);
    		result = (gchar *) g_checksum_get_string(checksum);
    		g_checksum_free(checksum);
    		return result;
    	}
    	return "";
    }
    Any idea on what is wrong with this code?
    Thanks in advance for any help and or suggestion
    Last edited by sbaginov; 01-26-2011 at 08:38 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would say you need to make a COPY of the string returned by g_checksum_get_string(), before you call g_checksum_free()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    Yes, you are right.

    Code:
    gchar *get_hash(const gchar *filepath, GChecksumType hash_type) {
        FILE *fp;
        if (NULL != (fp = fopen(filepath, "r+b"))) {
            guchar buffer[65536];
            gint read_bytes;
            gchar *result;
            GChecksum *checksum = g_checksum_new(hash_type);
            while ((read_bytes = fread(buffer, sizeof(buffer), 1, fp)) > 0)
                g_checksum_update(checksum, buffer, read_bytes);
    
            fclose(fp);
            result = g_strdup((gchar *) g_checksum_get_string(checksum));
            g_checksum_free(checksum);
            return result;
        }
        return g_strdup("");
    }
    Unfortunately, the returned array contains a different hash value from that returned by md5sum

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your fread parameters are the wrong way round.

    That is, if you're wanting to return the number of bytes, as opposed to the number of buffers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19

    Cool Solved

    ** Thank you so much!!
    Code:
    gchar *get_hash(const gchar *filepath, GChecksumType hash_type) {
    	FILE *fp;
    	if (NULL != (fp = fopen(filepath, "rb"))) {
    		guchar buffer[65536];
    		gint read_bytes;
    		gchar *result;
    		GChecksum *checksum = g_checksum_new(hash_type);
    		while ((read_bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
    			g_checksum_update(checksum, buffer, read_bytes);
    
    		result = g_strdup((gchar *) g_checksum_get_string(checksum));
    
    		fclose(fp);
    		g_checksum_free(checksum);
    
    		return result;
    	}
    	return g_strdup("");
    }
    Last edited by sbaginov; 01-26-2011 at 09:22 AM. Reason: fopen mode fix

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > guchar buffer[65536];
    If you're stuck for choosing a buffer size, then the macro BUFSIZ (in stdio.h) is a good alternative.

    > result = g_strdup((gchar *) g_checksum_get_string(checksum));
    I can't see how this cast was necessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signal generation and handling
    By itisravi in forum Linux Programming
    Replies: 10
    Last Post: 03-23-2010, 10:44 AM
  2. deploying glib function
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 12-10-2008, 08:18 PM
  3. Problems installing gtk
    By hallo007 in forum Linux Programming
    Replies: 4
    Last Post: 06-26-2007, 02:26 PM
  4. Problem with a GLIB
    By giladist in forum C Programming
    Replies: 3
    Last Post: 05-26-2007, 11:29 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM