package sm2

import (
	"encoding/binary"

	"xdx.jelly/xgcl/sm"
)

type endian struct {
	isBigEndian bool
}

func (e *endian) PutUint32(b []byte, v uint32) {
	if e.isBigEndian {
		binary.BigEndian.PutUint32(b, v)
	} else {
		binary.LittleEndian.PutUint32(b, v)
	}
}

func (e *endian) Uint32(b []byte) uint32 {
	if e.isBigEndian {
		return binary.BigEndian.Uint32(b)
	}

	return binary.LittleEndian.Uint32(b)
}

var (
	// Endian is the endian when marshal a int, default is big endian
	Endian = endian{true}
)

// SetToBigEndian 将端序设置为大端序
func SetToBigEndian() {
	Endian.isBigEndian = true
}

// SetToLittleEndian 设置为小端序
func SetToLittleEndian() {
	Endian.isBigEndian = false
}

// SetToDefaultEndian 恢复默认端序-大端
func SetToDefaultEndian() {
	Endian.isBigEndian = true
}

func init() {
	sm.RegisterCallBack(SetToBigEndian, SetToLittleEndian, SetToDefaultEndian)
}
