
/* A simple, slow, CRC-16 function, functions using
   tables are recommended for higher speeds */

unsigned crc16(unsigned char * buf, int nbytes)
{
    unsigned carry;
    unsigned crc_val = 0xffff;


    while(nbytes--)
    {
        unsigned j = 8;
        crc_val ^= (unsigned int) *buf++;

        while(j--)
        {
            carry = crc_val & 0x0001;
            crc_val >>= 1;
            if (carry)
               crc_val ^= 0xa001;
        }
    }

    return( (crc_val >> 8) | (crc_val << 8) );
}

