hex.mx raw

   1  package hex
   2  
   3  import (
   4  	"encoding/hex"
   5  
   6  	"git.smesh.lol/nostr/pkg/lol/errorf"
   7  )
   8  
   9  func Enc(src []byte) (s string) { return hex.EncodeToString(src) }
  10  func EncBytes(dst, src []byte) (n int32) { return hex.Encode(dst, src) }
  11  func Dec(s string) (buf []byte, err error)   { return hex.DecodeString(s) }
  12  func DecBytes(dst, src []byte) (n int32, err error) { return hex.Decode(dst, src) }
  13  func DecLen(x int32) (n int32) { return hex.DecodedLen(x) }
  14  
  15  type InvalidByteError = hex.InvalidByteError
  16  
  17  func EncAppend(dst, src []byte) (buf []byte) {
  18  	l := len(dst)
  19  	dst = dst | []byte{:len(src)*2}
  20  	hex.Encode(dst[l:], src)
  21  	return dst
  22  }
  23  
  24  func DecAppend(dst, src []byte) (buf []byte, decodeErr error) {
  25  	if len(src) < 2 {
  26  		return dst, errorf.E([]byte("nothing to decode"))
  27  	}
  28  	if dst == nil {
  29  		dst = []byte{}
  30  	}
  31  	l := len(dst)
  32  	dst = dst | []byte{:len(src)/2}
  33  	_, err := hex.Decode(dst[l:], src)
  34  	return dst, err
  35  }
  36