Re: Checksum in a struct

Discussion in 'C Programming' started by jadill33@gmail.com, Jul 12, 2012.

  1. Guest

    On Wednesday, July 11, 2012 10:56:53 AM UTC-4, pozz wrote:
    > I have a function that computes a 16-bit checksum (following whatever
    > algorithm) of a memory space:
    >
    > unsigned int checksum(const void *buffer, size_t size);
    >
    > I want to embed this checksum in a struct:
    >
    > struct PStruct {
    > int x;
    > unsigned int y;
    > char z[13];
    > ...
    > unsigned int checksum;
    > };
    >
    > How to use the checksum() function above? I propose:
    >
    > struct PStruct ps;
    > ...
    > ps.checksum = checksum(&ps, offsetof(struct PStruct, checksum));
    >
    > Is there a better mechanism?


    Pack the structure by writing all the bytes of each individual member to a byte array. This will avoid the compiler padding for alignment issue. If needed, use the ntoh family of functions to put the necessary 2 and 4 byte words in big-endian before checksum if the need to support architectures with different endians arise.

    \code
    #define member_sizeof( type, member ) sizeof ( ((type*)0)->member )
    size_t pack_PStruct_size( void )
    {
    return member_sizeof (struct PStruct, x) +
    member_sizeof (struct PStruct, y) +
    member_sizeof (struct PStruct, z) +
    ... ;
    }
    \endcode

    You can use 'mempcpy' to simplify packing an array.

    \code
    void* mempcpy( void* dst, const void* src, size_t n )
    {
    return (unsigned char*)memcpy( dst, src, n ) + n;
    }

    void pack_PStruct( unsigned char* packed_array, struct PStruct* ps )
    {
    unsigned char* p = packed_array;

    p = mempcpy( p, ps->x, member_sizeof( struct PStruct, x ) );
    p = mempcpy( p, ps->y, member_sizeof( struct PStruct, y ) );
    p = mempcpy( p, ps->z, member_sizeof( struct PStruct, z ) );
    ...
    }
    \endcode

    Be wary of uninitialized characters in 'z' if you checksum all 13 characters in the array.

    You can tweak the interface as needed.

    Best regards,
    John D.
     
    , Jul 12, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman

    Re: Checksum in a struct

    Eric Sosman, Jul 11, 2012, in forum: C Programming
    Replies:
    12
    Views:
    642
    Tim Rentsch
    Jul 21, 2012
  2. Stefan Ram

    Re: Checksum in a struct

    Stefan Ram, Jul 11, 2012, in forum: C Programming
    Replies:
    2
    Views:
    371
    James Kuyper
    Jul 25, 2012
  3. Re: Checksum in a struct

    , Jul 11, 2012, in forum: C Programming
    Replies:
    0
    Views:
    341
  4. Peter Nilsson

    Re: Checksum in a struct

    Peter Nilsson, Jul 13, 2012, in forum: C Programming
    Replies:
    0
    Views:
    322
    Peter Nilsson
    Jul 13, 2012
  5. Tim Rentsch

    Re: Checksum in a struct

    Tim Rentsch, Jul 21, 2012, in forum: C Programming
    Replies:
    0
    Views:
    338
    Tim Rentsch
    Jul 21, 2012
Loading...

Share This Page