<commit_before>package netlink

import "os"
import "bytes"
import "encoding/binary"

type AttributeType uint16

func (self *AttributeType)Unmarshal(in []byte)( err os.Error){
  var mt uint16
  err = Unmarshal(in, &mt)
  if err == nil { *self = AttributeType(mt) }
  return
}


type Attribute interface {
  AttributeType()(AttributeType)
  Body()([]byte)
}

func (self AttributeType)Marshal()([]byte, os.Error){
  return Marshal(uint16(self))
}

type attr struct {
  _type AttributeType
  _body []byte
}

func (self attr)AttributeType()(AttributeType){ return self._type }
func (self attr)Body()([]byte){ return self._body}

func (self *attr)Unmarshal(in []byte)(err os.Error){
  if len(in) < 2 {
    return os.NewError("Cannot be valid attribute")
  }
  err = Unmarshal(in[0:2], &self._type)
  if err == nil {
    self._body = in[2:]
  }
  return
}

func UnmarshalAttribute(in []byte)(out Attribute, err os.Error){
  a := &attr{}
  err = a.Unmarshal(in)
  if err == nil { out = a }
  return
}

func UnmarshalAttributes(in []byte, padding int)(out []Attribute, err os.Error){
  pos := 0
  for pos < len(in) {
    l := binary.LittleEndian.Uint16(in[pos:pos+2])
    if int(l) > len(in) - pos {
      err = os.NewError("Can't parse attribute (too long)")
      break
    }
    if l > 4 {
      t := binary.LittleEndian.Uint16(in[pos+2:pos+4])
      out = append(out, attr{_type: AttributeType(t), _body:in[pos+4:pos + int(l)]})
      pos += int(l)
      if padding > 0 && pos % padding != 0 {
        pos += padding - (pos % padding)
      }
    } else {
      err = os.NewError("Invalid attribute length.")
      break
    }
  }
  return
}

func MarshalAttributes(in []Attribute, padding int)(out []byte, err os.Error){
  buff := bytes.NewBuffer(nil)
  for i := range(in){
    ahdr := [4]byte{}
    body := in[i].Body()
    binary.LittleEndian.PutUint16(ahdr[2:4], uint16(in[i].AttributeType()))
    binary.LittleEndian.PutUint16(ahdr[0:2], uint16(len(body)) + 4)
    buff.Write(ahdr[0:])
    buff.Write(body)
    if padding > 0 && len(body) % padding != 0 {
      buff.Write(make([]byte, padding - (len(body) % padding)))
    }
  }
  out = buff.Bytes()
  return
}
<commit_msg>Misc fixes, remove old marshalling calls<commit_after>package netlink

import "fmt"
import "os"
import "bytes"
import "encoding/binary"

type AttributeType uint16

type Attribute interface {
  AttributeType()(AttributeType)
  Body()([]byte)
}

type attr struct {
  _type AttributeType
  _body []byte
}

func (self attr)AttributeType()(AttributeType){ return self._type }
func (self attr)Body()([]byte){ return self._body}

func UnmarshalAttributes(in []byte, padding int)(out []Attribute, err os.Error){
  pos := 0
  for pos < len(in) {
    l := binary.LittleEndian.Uint16(in[pos:pos+2])
    if int(l) > len(in) - pos {
      err = os.NewError("Can't parse attribute (too long)")
      break
    }
    if l > 4 {
      t := binary.LittleEndian.Uint16(in[pos+2:pos+4])
      out = append(out, attr{_type: AttributeType(t), _body:in[pos+4:pos + int(l)]})
      pos = Reposition(pos + int(l), padding)
    } else {
      err = os.NewError(fmt.Sprintf("Invalid attribute (Len: %d):", l))
      break
    }
  }
  return
}

func MarshalAttributes(in []Attribute, padding int)(out []byte, err os.Error){
  buff := bytes.NewBuffer(nil)
  for i := range(in){
    ahdr := [4]byte{}
    body := in[i].Body()
    binary.LittleEndian.PutUint16(ahdr[2:4], uint16(in[i].AttributeType()))
    binary.LittleEndian.PutUint16(ahdr[0:2], uint16(len(body)) + 4)
    buff.Write(ahdr[0:])
    buff.Write(body)
  }
  out = PadBytes(buff.Bytes(), padding)
  return
}

{'fraction_non_alphanumeric': 0.16371681415929204, 'fraction_numerical': 0.01825221238938053, 'mean_word_length': 3.5439698492462313, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, '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': '6391325', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1278, 'n_words': 306}