Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #172584

Re: Explain function

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Explain function
Date 2023-08-20 07:38 -0700
Organization A noiseless patient Spider
Message-ID <86edjx4w4p.fsf@linuxsc.com> (permalink)
References <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com> <ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> David Brown <david.brown@hesbynett.no> writes:
>
>> On 20/08/2023 11:46, Denis U wrote:
>>
>>> Hi
>>> there is a function generating checksum:
>>> unsigned short foo (unsigned short *addr, int len) {
>>>      unsigned short result;
>>>      unsigned int sum = 0;
>>>     while (len > 1) {
>>>          sum += *addr++;
>>>          len -= 2;
>>>      }
>>>     if (len == 1) {
>>>          sum += *(unsigned char *) addr;
>>>      }
>>>      // some other stuff
>>> }
>>> which I suppose to be called like
>>>   unsigned short addr = 0xabcd;
>>>   foo(&a, 5);
>>> having unsigned short size equals 2 bytes
>>> I don't understand why the function is going to iterate over other
>>> bytes more than 2.  Where could those extra bytes come from?
>>
>> The checksum is not for a 2-byte short - it is for checksuming over an
>> array of 2-byte short values.
>
> Given the check for an odd length, it's clearly intended to produce a
> 16-bit checksum for an arbitrary object of any length -- not just for
> some array of short values.
>
> But if that is the case (and what's the extra test for otherwise) then
> it's a badly written.  short is not a predictable length (but the
> function may date from before the days of uint16_t) and there are
> alignment issues.
>
> To the OP:  where is this from?  Are you supposed to be correcting it, or
> is this supposed to be an example of good C from which you should learn?
>
> Consider using something like this:
>
>   uint_least16_t checksum(const char *addr, size_t len)
>   {
>        uint_fast16_t sum = 0;
>        while (len > 1) {
>             uint16_t tmp;
>             memcpy(&tmp, addr, sizeof tmp);
>             sum += tmp;
>             len -= 2;
>        }
>        return sum + (len ? *(unsigned char *)addr : 0);
>   }

Bleah.

unsigned short
checksum( const char *bytes, size_t n ){
    unsigned short t;
    size_t k = sizeof t;
    unsigned r;

    for(  r = 0;  n >= k;  bytes += k,  n -= k  ){
	memcpy( &t, bytes, k );
	r += t;
    }

    if(  n > 0  ){
	t = 0;
	memcpy( &t, bytes, n  );
	r += t;
    }

    return  r;   
}

Back to comp.lang.c | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Explain function Denis U <ua.kiev.energetix@gmail.com> - 2023-08-20 02:46 -0700
  Re: Explain function David Brown <david.brown@hesbynett.no> - 2023-08-20 14:05 +0200
    Re: Explain function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-20 14:39 +0100
      Re: Explain function David Brown <david.brown@hesbynett.no> - 2023-08-20 16:10 +0200
        Re: Explain function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-20 16:07 +0100
          Re: Explain function David Brown <david.brown@hesbynett.no> - 2023-08-20 18:04 +0200
            Re: Explain function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-20 20:52 +0100
      Re: Explain function Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-08-20 07:20 -0700
        Re: Explain function Spiros Bousbouras <spibou@gmail.com> - 2023-08-20 14:38 +0000
      Re: Explain function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-20 07:38 -0700

csiph-web