GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 6:35 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: [RESOLVED] GChecksumType?
PostPosted: Fri Apr 17, 2009 9:54 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Anyone want to help me out with a g_compute_checksum_for_data example?

This is what i have so far & obviously this code doesn't work
Code:
gint len = 0;
guchar *buffer = 0;
gchar *md5 = 0;

md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5, buffer, len);
g_print ("MD5 checksum of file: %s\n", md5);
g_free (buffer);


I think if i change guchar *buffer = 0; to guchar *buffer = "Filename";
and then i get the string? length of buffer and apply that value to gint len
it should work. So is g_strv_length the function i use to get the length of buffer?

Or Maybe this is all wrong?

API Reference http://library.gnome.org/devel/glib/sta ... ksums.html

Thanks :)


Last edited by caracal on Fri Apr 24, 2009 1:35 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 1:00 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Just moving my question back to the top of the forum before it gets lost ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 1:46 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

/me responding to the bump;)

If you intend to calculate the sums for text buffers, use g_compute_checksum_for_string function, since you can ignore the length parameter if your string is NULL terminated (and most probably will be).

On the other hand, if you'll be calculating sum for some binary data, the length value will probably come with it, since there is no way of determining the size of the binary chunk. (If data will be read from disk, (f)read family of function returns some size indicator.)

Main point of all this is: if you have NULL terminated string, you don't need length parameter; if you're dealing with non-NULL terminated string or binary blob, there is no way to determine the length of the data and that value should be obtained at data creation (read) time.

BTW, g_strv_lenght has nothing to do with string lengths. This function is intended to be used with NULL-terminated arrays of strings (gchar **array or gchar *array[]) and returns number of strings in array.

Sample usage would be:
Code:
gchar *array[] = { "First string",
                   "Second string",
                   "Third string",
                   NULL };

g_print( "%d\n", g_strv_length( array ) );
This piece of code would print 3 to terminal.

Hope this helped a bit.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 1:57 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
What i wanted to do was calculate the sum for binary data so i was trying to figure out how GChecksumType worked as you can tell i am kinda clueless on going about doing that. I must have googled for days and read as much source code i could find that used it. My non working example was a best guess as there is virtual zero info on usage.

Thanks for the example code and your response it helped. Ill make up a tutorial and post example code once i solve this problem


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 2:31 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

This is how the sum can be calculated for files on the disk.

Code:
#include <stdio.h>
#include <glib.h>

#define MAX_SIZE 1024 * 1024 /* 1 Mb */

int
main( int    argc,
     char **argv )
{
   guchar  data[MAX_SIZE];
   gsize   size = 0;
   FILE   *input;
   gchar  *sum;

   if( argc < 2 )
   {
      g_print( "USAGE: sum <filename> (max filename size is 1 MB)\n" );
      return( 0 );
   }

   input = fopen( argv[1], "rb" );
   size = fread( (void *)data, sizeof( guchar ), MAX_SIZE, input );
   fclose( input );

   sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, data, size );
   g_print( "Checksum for >> %s <<: %s\n", argv[1], sum );
   g_free( sum );

   return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 2:38 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Thanks :)

So is 1 mb file size or file name size?
And why would these to give differant results?

Example gives
Checksum for >> /tmp/gtk+-2.16.1.tar.gz <<: 4dba5b44ad2517d6e84a0093bcaeba78

md5sum gives
fd371592f910b82323182259d33cd229 /tmp/gtk+-2.16.1.tar.gz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 2:59 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
caracal wrote:
What if i wanted to get the sum of say gtk+-2.16.1.tar.gz its 25.7 MB (26924620 bytes) could i just increase the MAX_SIZE or will memory start to be an issue?
The only theoretical limitation on the file length is the gsize's max value (2^32 on 32-bit machines and 2^64 on 64-bit machines). But the practical limitation will probably be the amount of RAM available, since my sample code "slurps" the whole file in at once (and this is the only way of calculating sums with glib).

For larger files you'll have to use some other method of calculation, eg. the one that can calculate sum progressively while reading file in chunks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 4:57 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
So if i use g_checksum_new () and step threw the binary blob with fread while performing g_checksum_update () after every 1meg of data read
and finally calling g_checksum_get_string () i should be able to checksum large files.

Maybe i try to figure it out in python first.
http://docs.python.org/library/md5.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 5:17 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
You're spot on with this one.

A slightly updated sample that do what you wish (reading 1 kb at a time)
Code:
#include <stdio.h>
#include <glib.h>

#define MAX_SIZE 1024 /* 1 kb */

int
main( int    argc,
      char **argv )
{
   GChecksum   *cs;
    guchar       data[MAX_SIZE];
    gsize        size = 0;
    FILE        *input;
    const gchar *sum;

    if( argc < 2 )
    {
        g_print( "USAGE: sum <filename>\n" );
        return( 0 );
    }

   cs = g_checksum_new( G_CHECKSUM_MD5 );
    input = fopen( argv[1], "rb" );
   do
   {
      size = fread( (void *)data, sizeof( guchar ), MAX_SIZE, input );
      g_checksum_update( cs, data, size );
   }
   while( size == MAX_SIZE );
    fclose( input );

    sum = g_checksum_get_string( cs );
    g_print( "Checksum for >> %s <<: %s\n", argv[1], sum );
   g_checksum_free( cs );

    return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 11:21 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Wow thanks i had to go take a nape after sitting in front of term for like 16 hours it was a nice surprise to come back and see your post thanks again :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group