<commit_before>package gosnmp

import (
	"fmt"
	"net"
)

type Varbind interface {
	//encodeValue returns the number of bytes written to the encoder
	encodeValue(encoder *berEncoder) (int, error)
	decodeValue(decoder *berDecoder, valueLength int) error
	getOid() ObjectIdentifier
	setOid(oid ObjectIdentifier)
}

func (encoder *berEncoder) encodeVarbind(vb Varbind) (int, error) {
	header := encoder.newHeader(SEQUENCE)
	oidLen, err := encoder.encodeObjectIdentifier(vb.getOid())
	if err != nil {
		return 0, err
	}
	valLen, err := vb.encodeValue(encoder)
	if err != nil {
		return 0, err
	}
	_, marshalledLen := header.setContentLength(oidLen + valLen)
	return marshalledLen, nil
}

type baseVarbind struct {
	oid ObjectIdentifier
}

func (vb *baseVarbind) getOid() ObjectIdentifier {
	return vb.oid
}

func (vb *baseVarbind) setOid(oid ObjectIdentifier) {
	vb.oid = oid
}

type IntegerVarbind struct { // type 0x02
	baseVarbind
	val int32
}

func NewIntegerVarbind(oid ObjectIdentifier, val int32) *IntegerVarbind {
	vb := new(IntegerVarbind)
	vb.oid = oid
	return vb
}

func (vb *IntegerVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeInteger(int64(vb.val)), nil
}

func (vb *IntegerVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeInt32(valueLength)
	return
}

// BitStringVarbind is the structure to use when you want an ASN.1 BIT STRING type. A bit string is padded up to the nearest byte in memory
// and the number of valid bits is recorded. Padding bits will be zero
type BitStringVarbind struct { // type 0x03
	baseVarbind
	val *BitString
}

func NewBitStringVarbind(oid ObjectIdentifier, val *BitString) *BitStringVarbind {
	vb := new(BitStringVarbind)
	vb.oid = oid
	return vb
}

func (vb *BitStringVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeBitString(vb.val), nil
}

func (vb *BitStringVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeBitString(valueLength)
	return
}

type OctetStringVarbind struct { // type 0x04
	baseVarbind
	val []byte
}

func NewOctetStringVarbind(oid ObjectIdentifier, val []byte) *OctetStringVarbind {
	vb := new(OctetStringVarbind)
	vb.oid = oid
	return vb
}

func (vb *OctetStringVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeOctetString(vb.val), nil
}

func (vb *OctetStringVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val = make([]byte, valueLength)
	if numRead, err := decoder.Read(vb.val); err != nil || numRead != valueLength {
		return fmt.Errorf("Couldn't decode octet string of length %d. Number of bytes read from stream: %d, err: %s", valueLength, numRead, err)
	}
	return
}

type NullVarbind struct { // type 0x05
	baseVarbind
}

func NewNullVarbind(oid ObjectIdentifier) *NullVarbind {
	vb := new(NullVarbind)
	vb.oid = oid
	return vb
}

func (vb *NullVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeNull(), nil
}

func (vb *NullVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	if valueLength != 0 {
		return fmt.Errorf("Non-zero value length found for NULL varbind: %d", valueLength)
	}
	return
}

type ObjectIdentifierVarbind struct { // type 0x06
	baseVarbind
	val ObjectIdentifier
}

func NewObjectIdentifierVarbind(oid ObjectIdentifier, val ObjectIdentifier) *ObjectIdentifierVarbind {
	vb := new(ObjectIdentifierVarbind)
	vb.oid = oid
	return vb
}

func (vb *ObjectIdentifierVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeObjectIdentifier(vb.val)
}

func (vb *ObjectIdentifierVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeObjectIdentifier(valueLength)
	return
}

type IPv4AddressVarbind struct { // type 0x40
	baseVarbind
	val net.IP
}

func NewIPv4AddressVarbind(oid ObjectIdentifier, val net.IP) *IPv4AddressVarbind {
	vb := new(IPv4AddressVarbind)
	vb.oid = oid

	return vb
}

func (vb *IPv4AddressVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeIPv4Address(vb.val)
}

func (vb *IPv4AddressVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeIPv4Address(valueLength)
	return
}

type Counter32Varbind struct { // type 0x41
	baseVarbind
	val uint32
}

func NewCounter32Varbind(oid ObjectIdentifier) *Counter32Varbind {
	vb := new(Counter32Varbind)
	vb.oid = oid<commit_msg>fixing value assignment in varbind initializers<commit_after>package gosnmp

import (
	"fmt"
	"net"
)

type Varbind interface {
	//encodeValue returns the number of bytes written to the encoder
	encodeValue(encoder *berEncoder) (int, error)
	decodeValue(decoder *berDecoder, valueLength int) error
	getOid() ObjectIdentifier
	setOid(oid ObjectIdentifier)
}

func (encoder *berEncoder) encodeVarbind(vb Varbind) (int, error) {
	header := encoder.newHeader(SEQUENCE)
	oidLen, err := encoder.encodeObjectIdentifier(vb.getOid())
	if err != nil {
		return 0, err
	}
	valLen, err := vb.encodeValue(encoder)
	if err != nil {
		return 0, err
	}
	_, marshalledLen := header.setContentLength(oidLen + valLen)
	return marshalledLen, nil
}

type baseVarbind struct {
	oid ObjectIdentifier
}

func (vb *baseVarbind) getOid() ObjectIdentifier {
	return vb.oid
}

func (vb *baseVarbind) setOid(oid ObjectIdentifier) {
	vb.oid = oid
}

type IntegerVarbind struct { // type 0x02
	baseVarbind
	val int32
}

func NewIntegerVarbind(oid ObjectIdentifier, val int32) *IntegerVarbind {
	vb := new(IntegerVarbind)
	vb.oid = oid
	vb.val = val
	return vb
}

func (vb *IntegerVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeInteger(int64(vb.val)), nil
}

func (vb *IntegerVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeInt32(valueLength)
	return
}

// BitStringVarbind is the structure to use when you want an ASN.1 BIT STRING type. A bit string is padded up to the nearest byte in memory
// and the number of valid bits is recorded. Padding bits will be zero
type BitStringVarbind struct { // type 0x03
	baseVarbind
	val *BitString
}

func NewBitStringVarbind(oid ObjectIdentifier, val *BitString) *BitStringVarbind {
	vb := new(BitStringVarbind)
	vb.oid = oid
	vb.val = val
	return vb
}

func (vb *BitStringVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeBitString(vb.val), nil
}

func (vb *BitStringVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeBitString(valueLength)
	return
}

type OctetStringVarbind struct { // type 0x04
	baseVarbind
	val []byte
}

func NewOctetStringVarbind(oid ObjectIdentifier, val []byte) *OctetStringVarbind {
	vb := new(OctetStringVarbind)
	vb.oid = oid
	vb.val = val
	return vb
}

func (vb *OctetStringVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeOctetString(vb.val), nil
}

func (vb *OctetStringVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val = make([]byte, valueLength)
	if numRead, err := decoder.Read(vb.val); err != nil || numRead != valueLength {
		return fmt.Errorf("Couldn't decode octet string of length %d. Number of bytes read from stream: %d, err: %s", valueLength, numRead, err)
	}
	return
}

type NullVarbind struct { // type 0x05
	baseVarbind
}

func NewNullVarbind(oid ObjectIdentifier) *NullVarbind {
	vb := new(NullVarbind)
	vb.oid = oid
	return vb
}

func (vb *NullVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeNull(), nil
}

func (vb *NullVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	if valueLength != 0 {
		return fmt.Errorf("Non-zero value length found for NULL varbind: %d", valueLength)
	}
	return
}

type ObjectIdentifierVarbind struct { // type 0x06
	baseVarbind
	val ObjectIdentifier
}

func NewObjectIdentifierVarbind(oid ObjectIdentifier, val ObjectIdentifier) *ObjectIdentifierVarbind {
	vb := new(ObjectIdentifierVarbind)
	vb.oid = oid
	vb.val = val
	return vb
}

func (vb *ObjectIdentifierVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeObjectIdentifier(vb.val)
}

func (vb *ObjectIdentifierVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeObjectIdentifier(valueLength)
	return
}

type IPv4AddressVarbind struct { // type 0x40
	baseVarbind
	val net.IP
}

func NewIPv4AddressVarbind(oid ObjectIdentifier, val net.IP) *IPv4AddressVarbind {
	vb := new(IPv4AddressVarbind)
	vb.oid = oid
	vb.val = val
	return vb
}

func (vb *IPv4AddressVarbind) encodeValue(encoder *berEncoder) (int, error) {
	return encoder.encodeIPv4Address(vb.val)
}

func (vb *IPv4AddressVarbind) decodeValue(decoder *berDecoder, valueLength int) (err error) {
	vb.val, err = decoder.decodeIPv4Address(valueLength)
	return
}

type Counter32Varbind struct { // type 0x41
	baseVarbind
	val uint32
}

func NewCounter32Varbind(oid ObjectIdentifier) *Counter32Varbind {
	vb := new(Counter32Varbind)
	vb.oid = oid
{'fraction_non_alphanumeric': 0.1012212564638574, 'fraction_numerical': 0.01122235669490593, 'mean_word_length': 5.192098092643052, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4753871', 'n_tokens_mistral': 3155, 'n_tokens_neox': 3041, 'n_words': 870}