[Dibbler] dibbler-server an debian-armel

Christian Andersen lists at milindur.de
Tue Aug 19 02:15:39 CEST 2008


Hallo,

> 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.

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;

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.

Maybe are functions like these a solution (untested):

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

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

These functions are untested, I could check them on my ARM-device.

Regards,
Christian Andersen



More information about the Dibbler mailing list