Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Checksum in a struct

Reply
Thread Tools

Re: Checksum in a struct

 
 
Eric Sosman
Guest
Posts: n/a
 
      07-17-2012
On 7/16/2012 5:01 PM, alex wrote:
> On Wed, 11 Jul 2012 11:59:42 -0400, Eric Sosman wrote:
>> A problem with the approach you've outlined is that the
>> checksum computation will include the values of any padding bytes -- the
>> size of `z' in your example almost begs for some padding bytes to be
>> inserted. Since padding bytes are not necessarily preserved when
>> assigning structs or even when assigning to struct elements, a checksum
>> that includes padding bytes is unlikely to be very useful.

>
> Are you sure about this??


Yes.

> I would expect a struct assign/deepcopy to be
> implemented "under the hood" using memcpy(), not { s1.a=s2.a;
> s1.b=s2.b; } etc. Pretty sure that's what GCC does.


My car is blue. Therefore, cars are blue.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d


 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      07-21-2012
pozz <(E-Mail Removed)> writes:

> Il 12/07/2012 17:08, Eric Sosman ha scritto:

[snip]
>> If you want to write a struct and a checksum to a file and
>> verify the checksum when you read it back, keep the checksum as
>> a separate variable and don't put it inside the struct.

>
> Could I ignore the "randomness" of the padding bytes? I read that
> the padding bytes can be randomly changed even assigning a value to a
> field of the struct. My application should work in this way:
>
> - at startup, read the configuration file, calculate and verify the
> checksum: if it isn't correct, use a default struct;
>
> - when a field changes (after assigning it the new value), calculate
> the new checksum and save both (struct and checksum) to the file;
>
> - during the normal execution of the application, the fields of the
> struct are accessed many times.
>
> In this situation, could I calculate the checksum on the entire
> memory area of the struct (with padding bytes)? [snip]


Yes, provided (1) if the whole struct is updated, either you
calculate a new checksum from scratch or you make sure the
padding bytes are copied also (eg, by using memcpy()) and
use the checksum of the source struct, and (2) the checksum
is stored in a way so as not to perturb the struct's padding
bytes (this can be done by putting the checksum outside the
struct in question, or by updating an in-struct checksum
using, eg, memcpy()).

This question was asked fairly clearly and obviously is the most
important one to answer; I don't know why it wasn't responded
to more directly.

You are right that reading a struct or its members has no
effect on its padding bytes.
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      07-21-2012
Eric Sosman <(E-Mail Removed)> writes:

> On 7/11/2012 10:56 AM, 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?

>
> You'd better hope so
>
> A problem with the approach you've outlined is that the
> checksum computation will include the values of any padding
> bytes -- the size of `z' in your example almost begs for some
> padding bytes to be inserted. [snip elaboration]


You're assuming he wants a checksum on the "logical
value" of the struct. If what he wants is a checksum
on the physical value of the struct -- which appears to
be what he does want -- then this approach will work fine
(provided of course care is taken so that storing the
checksum will not perturb the padding bytes, which I
have already addressed in an earlier reply).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Checksum in a struct Stefan Ram C Programming 2 07-25-2012 12:13 PM
Re: Checksum in a struct jadill33@gmail.com C Programming 0 07-12-2012 09:23 PM
Re: Checksum in a struct fmassei@gmail.com C Programming 0 07-11-2012 04:39 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments