Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A bug in the code is worth two in the documentation.


devel / comp.lang.c / Explain function

SubjectAuthor
* Explain functionDenis U
`* Re: Explain functionDavid Brown
 `* Re: Explain functionBen Bacarisse
  +* Re: Explain functionDavid Brown
  |`* Re: Explain functionBen Bacarisse
  | `* Re: Explain functionDavid Brown
  |  `- Re: Explain functionBen Bacarisse
  +* Re: Explain functionMalcolm McLean
  |`- Re: Explain functionSpiros Bousbouras
  `- Re: Explain functionTim Rentsch

1
Explain function

<00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27960&group=comp.lang.c#27960

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1451:b0:410:966f:d61f with SMTP id v17-20020a05622a145100b00410966fd61fmr3109qtx.7.1692524780611;
Sun, 20 Aug 2023 02:46:20 -0700 (PDT)
X-Received: by 2002:a05:6a00:2281:b0:688:4632:d5b2 with SMTP id
f1-20020a056a00228100b006884632d5b2mr2549256pfe.6.1692524780078; Sun, 20 Aug
2023 02:46:20 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 20 Aug 2023 02:46:19 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=81.162.246.236; posting-account=nOD7_AoAAACx_Hi9tM1XxnhjtjbO1tiT
NNTP-Posting-Host: 81.162.246.236
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
Subject: Explain function
From: ua.kiev....@gmail.com (Denis U)
Injection-Date: Sun, 20 Aug 2023 09:46:20 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Denis U - Sun, 20 Aug 2023 09:46 UTC

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?

Re: Explain function

<ubsvi8$1bhkn$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27963&group=comp.lang.c#27963

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 14:05:28 +0200
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <ubsvi8$1bhkn$1@dont-email.me>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 20 Aug 2023 12:05:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="28a5ce0b91d98d1cfdd2d8454cd084fc";
logging-data="1427095"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+eVaH5sDNR+CImUgHtdiStey4o2ddolRk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:b6vXuw+yWkfMH0x+IojLYkPipxc=
In-Reply-To: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Sun, 20 Aug 2023 12:05 UTC

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. Checksums are typically used for
integrity checks for data that is stored or transmitted on networks,
serial lines, etc. The idea is that you transmit the data and the
checksum, and on reception you can re-calculate the checksum from the
received data and compare to the received checksum. If there is a
mismatch, something has been corrupted in the transfer - if there is a
match, the transfer was probably fine.

So you would call "foo" as :

unsigned short data[] = { 0xabcd, 0x1234, 0x9421 };
unsigned short cs = foo(data, 6);

Re: Explain function

<877cppvnne.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27968&group=comp.lang.c#27968

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 14:39:17 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <877cppvnne.fsf@bsb.me.uk>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e5d8f68d80d0d28048106bb566360eff";
logging-data="1454848"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gR8Io2H+Je82npX0IUCpUtVy5AOPd0Qw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:CDImr56/byqYvhbUytgMvRjnXTM=
sha1:8jrvO4/a/RYdgIAWpag5MH2qzT0=
X-BSB-Auth: 1.e4e03f094104f418cdc9.20230820143917BST.877cppvnne.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 20 Aug 2023 13:39 UTC

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);
}

--
Ben.

Re: Explain function

<ubt6tj$1cngj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27969&group=comp.lang.c#27969

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 16:10:57 +0200
Organization: A noiseless patient Spider
Lines: 95
Message-ID: <ubt6tj$1cngj$1@dont-email.me>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 20 Aug 2023 14:10:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="28a5ce0b91d98d1cfdd2d8454cd084fc";
logging-data="1465875"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SsRp3gDV5wkW1xxAJBo+22EAIqSaBRHs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:Ps+ouMWNFH/ICzIjHycWUop9xWY=
In-Reply-To: <877cppvnne.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Sun, 20 Aug 2023 14:10 UTC

On 20/08/2023 15:39, Ben Bacarisse wrote:
> 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.

I agree entirely - I just wanted to keep things simple at the start, and
then see where the OP wanted to go from there.

>
> 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);
> }
>

If someone is learning C, I'd assume they are interested in more
practical C. Types like "uint_least16_t" have theoretical advantages
over the fixed size types, but are almost never seen in real-world C
code. If the OP is interested in C as a language and knowing more about
it, then more detail will be appropriate. Even then, I'd question the
appropriateness of returning uint_least16_t here. (And did you skip
incrementing addr as a debugging exercise for the OP? :-) )

uint16_t checksum_16bit_le(const uint8_t *addr, size_t len)
{ uint16_t sum = 0;
while (len > 1) {
uint16_t tmp = *addr++;
tmp += (*addr++) << 8;
sum += tmp;
len -= 2;
}
if (len) {
sum += *addr;
}
return sum;
}

This version gives the same checksum on little-endian and big-endian
processors, unlike the original one - but that is almost certainly what
you want for a checksum, as the checksum algorithm is usually defined
independently of the system it is run on.

And if the OP wants to know about good checksum algorithms, this is not
a good one.

Re: Explain function

<c4b7c42e-a09e-48e6-8d91-988a27fd466en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27971&group=comp.lang.c#27971

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:309:b0:40f:d4a1:48c9 with SMTP id q9-20020a05622a030900b0040fd4a148c9mr24919qtw.4.1692541209073;
Sun, 20 Aug 2023 07:20:09 -0700 (PDT)
X-Received: by 2002:a17:90a:8505:b0:26c:f05d:1f2 with SMTP id
l5-20020a17090a850500b0026cf05d01f2mr1032373pjn.7.1692541208485; Sun, 20 Aug
2023 07:20:08 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 20 Aug 2023 07:20:07 -0700 (PDT)
In-Reply-To: <877cppvnne.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:c4f4:8fc2:f241:72e0;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:c4f4:8fc2:f241:72e0
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c4b7c42e-a09e-48e6-8d91-988a27fd466en@googlegroups.com>
Subject: Re: Explain function
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sun, 20 Aug 2023 14:20:09 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3476
 by: Malcolm McLean - Sun, 20 Aug 2023 14:20 UTC

On Sunday, 20 August 2023 at 14:39:34 UTC+1, Ben Bacarisse wrote:
> David Brown <david...@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);
> }
>
unsigned short checksum(const unsigned char *addr, int len)
{ unsigned short sum = 0;
int i;
for ( i = 0; i <len-1; i+ 2)
{
sum += addr[i+1] * 256 + addr[i];
}
if (i < len)
sum += addr[i];

return sum & 0xFFFF;
}

The question is what to do for endiannness.
Likely you really want the same result, regardless of the endianness of the platform.
And adding the last byte as a low value implies little-endian.

Re: Explain function

<pkAW+Hvkpy4oUuF=8@bongo-ra.co>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27974&group=comp.lang.c#27974

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 14:38:12 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <pkAW+Hvkpy4oUuF=8@bongo-ra.co>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com> <ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
<c4b7c42e-a09e-48e6-8d91-988a27fd466en@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 20 Aug 2023 14:38:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fe72475a7631c8f312e1c8222dfc65b5";
logging-data="1478696"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EWY7YRc+waoGoWGFMPXx4"
Cancel-Lock: sha1:n+1MmXaDgSgsGIRHkXjZcrNBMuE=
In-Reply-To: <c4b7c42e-a09e-48e6-8d91-988a27fd466en@googlegroups.com>
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Sun, 20 Aug 2023 14:38 UTC

On Sun, 20 Aug 2023 07:20:07 -0700 (PDT)
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> unsigned short checksum(const unsigned char *addr, int len)
> {
> unsigned short sum = 0;
> int i;
> for ( i = 0; i <len-1; i+ 2)

i += 2

> {
> sum += addr[i+1] * 256 + addr[i];
> }
> if (i < len)
> sum += addr[i];
>
> return sum & 0xFFFF;
> }

Regarding unsigned short sum = 0;

You want unsigned int not unsigned short ; the latter may be promoted
to int for the arithmetic in which case your code will likely cause
undefined behaviour. For the same reason you want

sum += (unsigned int) addr[i+1] * 256 + addr[i];

> The question is what to do for endiannness. Likely you really want the same
> result, regardless of the endianness of the platform. And adding the last
> byte as a low value implies little-endian.

Re: Explain function

<86edjx4w4p.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27975&group=comp.lang.c#27975

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 07:38:14 -0700
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <86edjx4w4p.fsf@linuxsc.com>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com> <ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6e4561071dd402c90a02333831e70b0b";
logging-data="1478717"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fstg/0970tuKyK//RvayDG4Fv7R9NFhk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:g65lSdr30VyGgdvcbUlAUtt0GLE=
sha1:+yoycaVMUfuRSZItr1DJaAhSjNE=
 by: Tim Rentsch - Sun, 20 Aug 2023 14:38 UTC

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;
}

Re: Explain function

<87pm3hu4zh.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27976&group=comp.lang.c#27976

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 16:07:46 +0100
Organization: A noiseless patient Spider
Lines: 96
Message-ID: <87pm3hu4zh.fsf@bsb.me.uk>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
<ubt6tj$1cngj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e5d8f68d80d0d28048106bb566360eff";
logging-data="1474759"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18V7IO7CBka8pEzuES2geOylf1l/2Oa3Bk="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:W8Pq2TxHuy73FQn83OyZdqw8OUQ=
sha1:qLoXv/EUJ5GvcfWfNf7B3qCMr08=
X-BSB-Auth: 1.781eedf219e1aaa0dc54.20230820160746BST.87pm3hu4zh.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 20 Aug 2023 15:07 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 20/08/2023 15:39, Ben Bacarisse wrote:
>> 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.
>
> I agree entirely - I just wanted to keep things simple at the start, and
> then see where the OP wanted to go from there.

I suspect it's a drive-by posting.

>> 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);
>> }
>>
>
> If someone is learning C, I'd assume they are interested in more practical
> C. Types like "uint_least16_t" have theoretical advantages over the fixed
> size types, but are almost never seen in real-world C code. If the OP is
> interested in C as a language and knowing more about it, then more detail
> will be appropriate. Even then, I'd question the appropriateness of
> returning uint_least16_t here. (And did you skip incrementing addr as a
> debugging exercise for the OP? :-) )

No, just tying off the top of my head.

> uint16_t checksum_16bit_le(const uint8_t *addr, size_t len)
> {
> uint16_t sum = 0;
> while (len > 1) {
> uint16_t tmp = *addr++;
> tmp += (*addr++) << 8;
> sum += tmp;
> len -= 2;
> }
> if (len) {
> sum += *addr;
> }
> return sum;
> }
>
>
> This version gives the same checksum on little-endian and big-endian
> processors, unlike the original one - but that is almost certainly what you
> want for a checksum, as the checksum algorithm is usually defined
> independently of the system it is run on.

There's no specification. It's all guesswork about what sort of
checksum this is. I agree they are often externally defined, but they
are never externally definedto be what the OP's code implements, so who
knows what's wanted?

--
Ben.

Re: Explain function

<ubtdi5$1e4oo$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27979&group=comp.lang.c#27979

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 18:04:21 +0200
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <ubtdi5$1e4oo$2@dont-email.me>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
<ubt6tj$1cngj$1@dont-email.me> <87pm3hu4zh.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 20 Aug 2023 16:04:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="28a5ce0b91d98d1cfdd2d8454cd084fc";
logging-data="1512216"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ApEUh0AMzMp/Xt5L6xvTXSrpq0axgEQU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:hC1KqWR0pur/9ih9/7qHuyc2Pm8=
Content-Language: en-GB
In-Reply-To: <87pm3hu4zh.fsf@bsb.me.uk>
 by: David Brown - Sun, 20 Aug 2023 16:04 UTC

On 20/08/2023 17:07, Ben Bacarisse wrote:

> There's no specification. It's all guesswork about what sort of
> checksum this is. I agree they are often externally defined, but they
> are never externally definedto be what the OP's code implements, so who
> knows what's wanted?
>

I guess we will just have to wait and see if he comes back - then we'll
hopefully know much more.

Re: Explain function

<878ra5trt5.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27988&group=comp.lang.c#27988

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Explain function
Date: Sun, 20 Aug 2023 20:52:22 +0100
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <878ra5trt5.fsf@bsb.me.uk>
References: <00808c30-b47e-4ee2-b1da-5811e242e6cbn@googlegroups.com>
<ubsvi8$1bhkn$1@dont-email.me> <877cppvnne.fsf@bsb.me.uk>
<ubt6tj$1cngj$1@dont-email.me> <87pm3hu4zh.fsf@bsb.me.uk>
<ubtdi5$1e4oo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e5d8f68d80d0d28048106bb566360eff";
logging-data="1607400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190aqnLS3PtDOJuPZEUkVPm6xf0Q8hSo00="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:hUrIzyDwBxKg76+0Dc74C4IJ+3A=
sha1:xnkBKmPX3Nx2GtY5HnBJai0qdDo=
X-BSB-Auth: 1.c2fc3c980b4545a1a242.20230820205222BST.878ra5trt5.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 20 Aug 2023 19:52 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 20/08/2023 17:07, Ben Bacarisse wrote:
>
>> There's no specification. It's all guesswork about what sort of
>> checksum this is. I agree they are often externally defined, but they
>> are never externally definedto be what the OP's code implements, so who
>> knows what's wanted?
>
> I guess we will just have to wait and see if he comes back - then we'll
> hopefully know much more.

I'd like to know the context. Is it "the teacher gave up this function
to use" or is it "I work for the MOD on missile control systems and I
have to write a checksum function"?

--
Ben.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor