encoding-asn1 0.1.0

asn1 crate based on pkg.go.dev/encoding/asn1
Documentation
package main

import (
	"fmt"
)

/*
type Message struct {
	Seq int
	//	Body asn1.RawValue
}

type Request struct {
	Num int
}

type Response struct {
	Ret int
}
*/

type Message struct {
	Seq int
}

func main() {
	a := 10
	aaa, _ := Marshal(a)
	fmt.Printf("bytes: %X\n", aaa)
	bbb, _ := Marshal(Message{
		Seq: 10,
	})
	fmt.Printf("m: %X\n", bbb)

	var m Message
	Unmarshal(bbb, &m)
	fmt.Printf("m: %v\n", m)
	return

	/*
		req, err := asn1.Marshal(Request{
			Num: 1,
		})
		if err != nil {
			return
		}

		requestMessage := Message{
			ID: 1,
			Body: asn1.RawValue{
				Class:      asn1.ClassContextSpecific,
				Tag:        3000,
				IsCompound: true,
				Bytes:      req,
				FullBytes:  append(AppendTagAndLength([]byte{}, asn1.ClassContextSpecific, 3000, len(req), true), req...),
			},
		}

		bytes, err := asn1.Marshal(requestMessage)
		if err != nil {
			return
		}

		fmt.Printf("bytes: %X\n", bytes)
	*/
}

/*
func AppendTagAndLength(dst []byte, class, tag, length int, isCompound bool) []byte {
	b := uint8(class) << 6
	if isCompound {
		b |= 0x20
	}
	if tag >= 31 {
		b |= 0x1f
		dst = append(dst, b)
		dst = appendBase128Int(dst, int64(tag))
	} else {
		b |= uint8(tag)
		dst = append(dst, b)
	}

	if length >= 128 {
		l := lengthLength(length)
		dst = append(dst, 0x80|byte(l))
		dst = appendLength(dst, length)
	} else {
		dst = append(dst, byte(length))
	}

	return dst
}

func appendBase128Int(dst []byte, n int64) []byte {
	l := base128IntLength(n)

	for i := l - 1; i >= 0; i-- {
		o := byte(n >> uint(i*7))
		o &= 0x7f
		if i != 0 {
			o |= 0x80
		}

		dst = append(dst, o)
	}

	return dst
}
func base128IntLength(n int64) int {
	if n == 0 {
		return 1
	}

	l := 0
	for i := n; i > 0; i >>= 7 {
		l++
	}

	return l
}

func lengthLength(i int) (numBytes int) {
	numBytes = 1
	for i > 255 {
		numBytes++
		i >>= 8
	}
	return
}

func appendLength(dst []byte, i int) []byte {
	n := lengthLength(i)

	for ; n > 0; n-- {
		dst = append(dst, byte(i>>uint((n-1)*8)))
	}

	return dst
}
*/