// Checksum calculates the checksum (as defined in RFC 1071) of the bytes in the
// given byte array.
//
// The initial checksum must have been computed on an even number of bytes.
549821
func Checksum(buf []byte, initial uint16) uint16 {
	return calculateChecksum(buf, uint32(initial))
}
204796
func (s *PutSlotTypeOutput) SetChecksum(v string) *PutSlotTypeOutput {
	s.Checksum = &v
	return s
}
972884
func (cr *ChecksumReader) ReadChecksum() ([]byte, error) {
	if cr.datanodes == nil {
		locs := cr.Block.GetLocs()
		datanodes := make([]string, len(locs))
		for i, loc := range locs {
			dn := loc.GetId()
			datanodes[i] = getDatanodeAddress(dn, cr.UseDatanodeHostname)
		}

		cr.datanodes = newDatanodeFailover(datanodes)
	}

	for cr.datanodes.numRemaining() > 0 {
		address := cr.datanodes.next()
		checksum, err := cr.readChecksum(address)
		if err != nil {
			cr.datanodes.recordFailure(err)
			continue
		}

		return checksum, nil
	}

	err := cr.datanodes.lastError()
	if err != nil {
		err = errors.New("No available datanodes for block.")
	}

	return nil, err
}
1351375
func (l LabelMap) checksum() int32 {
	var checksum uint32
	lb := byteBufferPool.Get().(*bytes.Buffer)
	// make sure the buffer is empty after getting a buffer from our pool
	lb.Reset()
	for key, value := range l {
		// The bytes used for checksumming are the following:
		// <4 bytes: length of key>
		binary.Write(lb, binary.BigEndian, int32(len(key)))
		// <n bytes: key>
		lb.WriteString(key)
		// <4 bytes: length of value>
		binary.Write(lb, binary.BigEndian, int32(len(value)))
		// <n bytes: value>
		lb.WriteString(value)

		// The checksum is calculated by xorring the checksums of all individual
		// labels. This makes the checksum of the labels order independant. This
		// is easier compared to sorting the labels by their key because of two
		// reasons.
		// 1. It saves memory allocations to have the keys in a slice.
		// 2. This method is guaranteed to be locale independant where sorting
		//    of strings might be different on different locale settings. This
		//    would cause indefinite fullsync storms because two ringpops would
		//    never agree on membership checksums.
		checksum = checksum ^ farm.Fingerprint32(lb.Bytes())

		lb.Reset()
	}
	// give the buffer back to the pool
	byteBufferPool.Put(lb)

	var signedChecksum int32
	// This line converts an unsigned integer to a signed integer (32 bits).
	// It is needed to be able to calculate the same value in nodejs for
	// ringpop-node and the integration tests in ringpop-common.
	signedChecksum = int32(checksum>>1)<<1 | int32(checksum&uint32(1))
	return signedChecksum
}
682195
func (l LabelMap) checksumString(b *bytes.Buffer) {
	checksum := l.checksum()

	if checksum == 0 {
		// we don't write the checksum of the labels if the value of the checksum
		// is 0 (zero) to be backwards compatible with ringpop applications on
		// an older version. This only works if the newer version does not use
		// labels
		return
	}

	// write #labels<checksum> to the buffer which will be appended to the
	// checksum string for the node.
	b.WriteString("#labels")
	b.WriteString(strconv.Itoa(int(checksum)))
}
60166
func (p *Packet) computeIPChecksum() uint16 {

	// IP packet checksum is computed with the checksum value set to zero
	p.ipHdr.Buffer[ipv4ChecksumPos] = 0
	p.ipHdr.Buffer[ipv4ChecksumPos+1] = 0

	// Compute checksum, over IP header only
	sum := checksum(p.ipHdr.Buffer[:p.ipHdr.ipHeaderLen])

	// Restore the previous checksum (whether correct or not, as this function doesn't change it)
	binary.BigEndian.PutUint16(p.ipHdr.Buffer[ipv4ChecksumPos:ipv4ChecksumPos+2], p.ipHdr.ipChecksum)

	return sum
}
643243
func checksum(buf []byte) uint16 {
	sum32 := checksumDelta(0, buf)
	sum16 := csumConvert32To16bit(sum32)

	csum := ^sum16
	return csum
}
277093
func ChecksumVV(vv buffer.VectorisedView, initial uint16) uint16 {
	var odd bool
	sum := initial
	for _, v := range vv.Views() {
		if len(v) == 0 {
			continue
		}
		s := uint32(sum)
		if odd {
			s += uint32(v[0])
			v = v[1:]
		}
		odd = len(v)&1 != 0
		sum = calculateChecksum(v, s)
	}
	return sum
}
1196377
func (s *CreateSlotTypeVersionOutput) SetChecksum(v string) *CreateSlotTypeVersionOutput {
	s.Checksum = &v
	return s
}
772909
func chksum(p []byte, csum []byte) {
	cklen := len(p)
	s := uint32(0)
	for i := 0; i < (cklen - 1); i += 2 {
		s += uint32(p[i+1])<<8 | uint32(p[i])
	}
	if cklen&1 == 1 {
		s += uint32(p[cklen-1])
	}
	s = (s >> 16) + (s & 0xffff)
	s = s + (s >> 16)
	s = ^s

	csum[0] = uint8(s & 0xff)
	csum[1] = uint8(s >> 8)
}
1149684
func verifyChecksum(path, expectedSum string) (bool, error) {
	md5sum, err := checksum(path)
	if err != nil {
		return false, err
	}

	log.Debug("expected MD5 sum:", expectedSum)
	log.Debug("MD5 sum found:", md5sum)

	return expectedSum == md5sum, nil
}
32422
func (s *CreateIntentVersionInput) SetChecksum(v string) *CreateIntentVersionInput {
	s.Checksum = &v
	return s
}
4152
func checksumok(packet, checksumBuf []byte) bool {
	if packet[0] != '$' {
		return false
	}

	sum := checksum(packet)
	tgt, err := strconv.ParseUint(string(checksumBuf), 16, 8)
	if err != nil {
		return false
	}
	return sum == uint8(tgt)
}
824265
func PseudoHeaderChecksum(protocol TransportProtocolNumber, srcAddr net.IP, dstAddr net.IP) uint16 {
	xsum := Checksum([]byte(srcAddr), 0)
	xsum = Checksum([]byte(dstAddr), xsum)
	return Checksum([]byte{0, uint8(protocol)}, xsum)
}
250636
func GenerateChecksum(data []byte) byte {
	var offset int
	ret := data[offset]
	for i := offset + 1; i < len(data)-1; i++ {
		// Xor bytes
		ret ^= data[i]
	}
	// Not result
	ret = (byte)(^ret)
	return ret
}
818190
func CheckEncode(input []byte, version byte) string {
	b := make([]byte, 0, 1+len(input)+4)
	b = append(b, version)
	b = append(b, input[:]...)
	cksum := checksum(b)
	b = append(b, cksum[:]...)
	return Encode(b)
}
640314
func getChecksum(data interface{}) uint64 {
	hash := fnv.New32a()
	hashutil.DeepHashObject(hash, data)
	return uint64(hash.Sum32())
}