bencode

package
v0.0.0-...-f0dcbf7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BENCODE_TAG      = "bencode" // 解析结构体上的 TAG 标签
	IGNORE_TAG_VALUE = "-"       // 忽略结构体上的字段
)

Variables

View Source
var (
	ErrBType                  = errors.New("error BObject type")               // 非法 BObject 类型
	ErrInvalidBObject         = errors.New("invalid BObject encoding")         // 非法 BObject 编码
	ErrInvalidStringFormat    = errors.New("invalid string format")            // 非法字符串格式
	ErrInvalidIntFormat       = errors.New("invalid int format")               // 非法整数格式
	ErrMissingDigits          = errors.New("missing digits after sign")        // 正负号后缺少数字
	ErrStringLength           = errors.New("invalid string length prefix")     // 字符串长度非法
	ErrNoPtr                  = errors.New("dest must be a pointer")           // 传递参数非指针类型
	ErrSrcMustBeStructOrSlice = errors.New("src code must be struct or slice") // 被绑定的必须是结构体或者切片
	ErrDestMustBeSlice        = errors.New("dest must be pointer to slice")    // 目标必须是切片指针
	ErrType                   = errors.New("error type")                       // 类型错误
)

Functions

func DecodeInt

func DecodeInt(r io.Reader) (int, error)

解码读出一个整数

func DecodeString

func DecodeString(r io.Reader) (string, error)

解码读出一个字符串

func EncodeInt

func EncodeInt(w io.Writer, val int) (int, error)

编码写入一个整数 写入123 -> i123e

func EncodeString

func EncodeString(w io.Writer, val string) (int, error)

编码写入一个字符串 写入"Hello World" -> "11:Hello World"

func GetValue

func GetValue[T allowedTypes](o *BObject, dest *T) error

Get 根据泛型类型 T 返回对应的值,

func Marshal

func Marshal(w io.Writer, s any) (int, error)

func Unmarshal

func Unmarshal(r io.Reader, s any) error

从 io.Reader 读数据绑定在结构体上

func UnmarshalBObject

func UnmarshalBObject(o *BObject, s any) error

从 BObject 中读取数据绑定在结构体上

Types

type BObject

type BObject struct {
	// contains filtered or unexported fields
}

func GetBObject

func GetBObject[T allowedTypes](v T) *BObject

func Parse

func Parse(r io.Reader) (*BObject, error)

func (*BObject) Bencode

func (o *BObject) Bencode(w io.Writer) (int, error)

将 BObject 编码写入 Writer 中,返回写入的字节长度

func (*BObject) GetBType

func (o *BObject) GetBType() BType

func (*BObject) GetDictKeyDay

func (bObj *BObject) GetDictKeyDay(keyName string) ([]byte, error)

type BType

type BType uint8
const (
	BSTR BType = iota
	BINT
	BLIST
	BDICT
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL