<commit_before>package stun

import (
  "bytes"
  "encoding/binary"
  "errors"
  "net"
)

type XorMappedAddressAttribute struct {
  Family  uint16
  Port    uint16
  Address net.IP
}

func (h *XorMappedAddressAttribute) Type() (StunAttributeType) {
  return XorMappedAddress
}

func (h *XorMappedAddressAttribute) Encode(msg *StunMessage) ([]byte, error) {
  buf := new(bytes.Buffer)
  err := binary.Write(buf, binary.BigEndian, attributeHeader(StunAttribute(h)))
  err = binary.Write(buf, binary.BigEndian, h.Family)
  err = binary.Write(buf, binary.BigEndian, h.Port)
  err = binary.Write(buf, binary.BigEndian, h.Address)

  if err != nil {
    return nil, err
  }
  return buf.Bytes(), nil
}

func (h *XorMappedAddressAttribute) Decode(data []byte, _ uint16, header *Header) (error) {
  if data[0] != 0 && data[1] != 1 && data[0] != 2 {
    return errors.New("Incorrect Mapped Address Family.")
  }
  h.Family = uint16(data[1])
  if (h.Family == 1 && len(data) < 8) || (h.Family == 2 && len(data) < 20) {
    return errors.New("Mapped Address Attribute unexpectedly Truncated.")
  }
  h.Port = uint16(data[2]) << 8 + uint16(data[3])
  if h.Family == 1 {
    h.Address = data[4:8]
  } else {
    h.Address = data[4:20]
  }
  return nil
}

func (h *XorMappedAddressAttribute) Length() (uint16) {
  if h.Family == 1 {
    return 8
  } else {
    return 20
  }
}<commit_msg>xor mapping does xor on decode<commit_after>package stun

import (
  "bytes"
  "encoding/binary"
  "errors"
  "net"
)

type XorMappedAddressAttribute struct {
  Family  uint16
  Port    uint16
  Address net.IP
}

func (h *XorMappedAddressAttribute) Type() (StunAttributeType) {
  return XorMappedAddress
}

func (h *XorMappedAddressAttribute) Encode(msg *StunMessage) ([]byte, error) {
  buf := new(bytes.Buffer)
  err := binary.Write(buf, binary.BigEndian, attributeHeader(StunAttribute(h)))
  err = binary.Write(buf, binary.BigEndian, h.Family)
  xport := h.Port ^ uint16(magicCookie >> 16)
  err = binary.Write(buf, binary.BigEndian, xport)
  err = binary.Write(buf, binary.BigEndian, h.Address)

  if err != nil {
    return nil, err
  }
  return buf.Bytes(), nil
}

func (h *XorMappedAddressAttribute) Decode(data []byte, _ uint16, header *Header) (error) {
  if data[0] != 0 && data[1] != 1 && data[0] != 2 {
    return errors.New("Incorrect Mapped Address Family.")
  }
  h.Family = uint16(data[1])
  if (h.Family == 1 && len(data) < 8) || (h.Family == 2 && len(data) < 20) {
    return errors.New("Mapped Address Attribute unexpectedly Truncated.")
  }
  h.Port = uint16(data[2]) << 8 + uint16(data[3])
  // X-port is XOR'ed with the 16 most significant bits of the magic Cookie
  h.Port ^= uint16(magicCookie >> 16)

  var xoraddress []byte
  if h.Family == 1 {
    xoraddress = make([]byte, 4)
    binary.BigEndian.PutUint32(xoraddress, magicCookie)
    h.Address = data[4:8]
  } else {
    xoraddress = make([]byte, 16)
    binary.BigEndian.PutUint32(xoraddress, magicCookie)
    copy(xoraddress[4:16], header.Id[:])
    h.Address = data[4:20]
  }
  for i, _ := range xoraddress {
    h.Address[i] ^= xoraddress[i]
  }
  return nil
}

func (h *XorMappedAddressAttribute) Length() (uint16) {
  if h.Family == 1 {
    return 8
  } else {
    return 20
  }
}
{'fraction_non_alphanumeric': 0.14876543209876544, 'fraction_numerical': 0.030246913580246913, 'mean_word_length': 3.6102418207681364, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4305724', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1184, 'n_words': 290}