Exemplo n.º 1
0
        /// <summary> tamir:
        /// Computes the one's sum on a byte array.
        /// Based TCP/IP Illustrated Vol. 2(1995) by Gary R. Wright and W. Richard
        /// Stevens. Page 236. And on http://www.cs.utk.edu/~cs594np/unp/checksum.html
        /// </summary>
        protected internal virtual int _OnesSum(byte[] bytes, int start, int len)
        {
            int sum = 0;             /* assume 32 bit long, 16 bit short */
            int i   = start;

            len = start + len;

            while (i < len - 1)
            {
                sum += ArrayHelper.extractInteger(bytes, i, 2);
                //if ((sum & unchecked((int)0x80000000)) != 0)
                if ((sum & 0x80000000) != 0)
                {
                    /* if high order bit set, fold */
                    sum = (sum & 0xFFFF) + (sum >> 16);
                }
                i += 2;
            }

            if (i < len)
            {
                /* take care of left over byte */
                sum += ArrayHelper.extractInteger(bytes, start, 2);
            }

            while (sum >> 16 != 0)
            {
                sum = (sum & 0xFFFF) + (sum >> 16);
            }

            return(sum & 0xFFFF);
        }
Exemplo n.º 2
0
 /// <summary> Fetch the header checksum.</summary>
 public virtual int GetTransportLayerChecksum(int pos)
 {
     return(ArrayHelper.extractInteger(_bytes, pos, 2));
 }