[Dibbler] dibbler-server an debian-armel

Christian Andersen lists at milindur.de
Tue Aug 19 09:52:05 CEST 2008


Hallo,

sorry, parts of my last mail were nonsense, it has been a bit too late
yesterday ;-).

> > Well, my proposal was quicker:
> >
> >     unsigned short code   = buf[pos]*256 + buf[pos+1];
> >     pos+=2;
> >     unsigned short length = buf[pos]*256 + buf[pos+1];
> >     pos+=2;
> >
> > Didn't it work? As far as I understand, reading a byte is always
> > successful. Byte multipled by 256 is automatically cast to short.

This works for me and it would naturally work for little and big endian
architectures.

> Yes, that worked, too. But this only works for little endian systems,
> not
> for big endian. Better would be:
> 
>     unsigned short code   = ntohs(buf[pos] + buf[pos+1]*256);
>     pos+=2;
>     unsigned short length = ntohs(buf[pos] + buf[pos+1]*256);
>     pos+=2;

And this idea of mine was nonsense.

> But I think that also the code where data is written into byte-buffers
> has
> to be modified, e.g.:
> 
>     *((u_short*)DUID)=htons(this->DUIDType);
>     *((u_short*)(DUID+2))=htons((short)macType);
> 
> Such code only works on ARM (and similar) if it is guaranteed that the
> DUID, DUID+2 and so on are properly aligned for the pointer-type they
> are
> casted to. In this case (uint16_t) it has to be a multiple of 2, for
> uint32_t a multiple of 4.

This problem remains.

So I change my idea:
 
> Maybe are functions like these a solution (untested):

uint16_t getUShort(const char *buf)
{
    return buf[0] * 256 + buf[1];
}

void putUShort(char *buf, uint16_t val)
{
    buf[0] = (unsigned char)((val & 0xff00) >> 8);
    buf[1] = (unsigned char)(val & 0xff);
}

I hope that I am now well rested and this is right...

Regards,
Christian



More information about the Dibbler mailing list