<commit_before>package packets

import (
	"fmt"
	"reflect"
)

// ReflectBytes uses reflection to create a byte array out of a packet.
// This can then be written down a net.Conn, or into a reader for later replay.
func ReflectBytes(p Packet) []byte {
	b := NewPacketWrapper([]byte{})
	vp := reflect.ValueOf(p)
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {
		case reflect.Uint8: // byte
			b.WriteByte(byte(v.Uint()))
		case reflect.Int8: // sbyte
			b.WriteSByte(int8(v.Int()))
		case reflect.Int16: // short
			b.WriteShort(int16(v.Int()))
		case reflect.Int32: // int
			b.WriteInt(int32(v.Int()))
		case reflect.String: // string
			b.WriteString(v.String())
		case reflect.Slice: // []byte
			b.WriteBytes(v.Bytes())
		}
	}
	return b.Buffer.Bytes()
}

// ReflectRead unmarshals the bytes from b into v. Similar to encoding/json.Umarshal
//   var p MyPacket
//   err := ReflectRead(b, &p)
func ReflectRead(b []byte, v Packet) error {
	buf := NewPacketWrapper(b)
	vp := reflect.ValueOf(v).Elem()
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {
		case reflect.Uint8: // byte
			if val, err := buf.ReadByte(); err != nil {
				return err
			} else {
				v.SetUint(uint64(val))
			}
		case reflect.Int8: // sbyte
			if val, err := buf.ReadSByte(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.Int16: // short
			if val, err := buf.ReadShort(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.Int32: // int
			if val, err := buf.ReadInt(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.String: // string
			if val, err := buf.ReadString(); err != nil {
				return err
			} else {
				v.SetString(val)
			}
		case reflect.Slice:
			if val, err := buf.ReadBytes(); err != nil {
				return err
			} else {
				v.SetBytes(val)
			}
		}
	}
	if buf.Buffer.Len() != 0 {
		fmt.Printf("expected 0 leftover, had %d\n", buf.Buffer.Len())
		//return fmt.Errorf("kyubu: expected 0 leftover bytes, but had %d", buf.Buffer.Len())
	}
	return nil
}

// ReflectSize uses reflection to figure out the size of a given packet based
// on the fields it defines.
func ReflectSize(p Packet) (size int) {
	vp := reflect.ValueOf(p).Elem()
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {<commit_msg>Dodgy debugging stuff I should probably remove.<commit_after>package packets

import (
	"fmt"
	"reflect"
)

// ReflectBytes uses reflection to create a byte array out of a packet.
// This can then be written down a net.Conn, or into a reader for later replay.
func ReflectBytes(p Packet) []byte {
	b := NewPacketWrapper([]byte{})
	vp := reflect.ValueOf(p)
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {
		case reflect.Uint8: // byte
			b.WriteByte(byte(v.Uint()))
		case reflect.Int8: // sbyte
			b.WriteSByte(int8(v.Int()))
		case reflect.Int16: // short
			b.WriteShort(int16(v.Int()))
		case reflect.Int32: // int
			b.WriteInt(int32(v.Int()))
		case reflect.String: // string
			b.WriteString(v.String())
		case reflect.Slice: // []byte
			b.WriteBytes(v.Bytes())
		}
	}
	return b.Buffer.Bytes()
}

// ReflectRead unmarshals the bytes from b into v. Similar to encoding/json.Umarshal
//   var p MyPacket
//   err := ReflectRead(b, &p)
func ReflectRead(b []byte, v Packet) error {
	buf := NewPacketWrapper(b)
	vp := reflect.ValueOf(v).Elem()
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {
		case reflect.Uint8: // byte
			if val, err := buf.ReadByte(); err != nil {
				return err
			} else {
				v.SetUint(uint64(val))
			}
		case reflect.Int8: // sbyte
			if val, err := buf.ReadSByte(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.Int16: // short
			if val, err := buf.ReadShort(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.Int32: // int
			if val, err := buf.ReadInt(); err != nil {
				return err
			} else {
				v.SetInt(int64(val))
			}
		case reflect.String: // string
			if val, err := buf.ReadString(); err != nil {
				return err
			} else {
				v.SetString(val)
			}
		case reflect.Slice:
			if val, err := buf.ReadBytes(); err != nil {
				return err
			} else {
				v.SetBytes(val)
			}
		}
	}
	if buf.Buffer.Len() != 0 {
		fmt.Printf("kyubu: ReflectRead(): expected 0 leftover, had %d\n", buf.Buffer.Len())
		//return fmt.Errorf("kyubu: expected 0 leftover bytes, but had %d", buf.Buffer.Len())
	}
	return nil
}

// ReflectSize uses reflection to figure out the size of a given packet based
// on the fields it defines.
func ReflectSize(p Packet) (size int) {
	vp := reflect.ValueOf(p).Elem()
	for i := 0; i < vp.NumField(); i++ {
		v := vp.Field(i)
		switch v.Kind() {
{'fraction_non_alphanumeric': 0.16250520183104453, 'fraction_numerical': 0.012900540990428632, 'mean_word_length': 2.796998420221169, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '5130142', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1760, 'n_words': 500}