github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/binary/utils.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"compress/zlib"
     7  	binary2 "encoding/binary"
     8  	"encoding/hex"
     9  	"net"
    10  
    11  	"github.com/Mrs4s/MiraiGo/utils"
    12  )
    13  
    14  type GzipWriter struct {
    15  	w   *gzip.Writer
    16  	buf *bytes.Buffer
    17  }
    18  
    19  func (w *GzipWriter) Write(p []byte) (int, error) {
    20  	return w.w.Write(p)
    21  }
    22  
    23  func (w *GzipWriter) Close() error {
    24  	return w.w.Close()
    25  }
    26  
    27  func (w *GzipWriter) Bytes() []byte {
    28  	return w.buf.Bytes()
    29  }
    30  
    31  func ZlibUncompress(src []byte) []byte {
    32  	b := bytes.NewReader(src)
    33  	var out bytes.Buffer
    34  	r, _ := zlib.NewReader(b)
    35  	defer r.Close()
    36  	_, _ = out.ReadFrom(r)
    37  	return out.Bytes()
    38  }
    39  
    40  func ZlibCompress(data []byte) []byte {
    41  	zw := acquireZlibWriter()
    42  	_, _ = zw.w.Write(data)
    43  	_ = zw.w.Close()
    44  	ret := make([]byte, len(zw.buf.Bytes()))
    45  	copy(ret, zw.buf.Bytes())
    46  	releaseZlibWriter(zw)
    47  	return ret
    48  }
    49  
    50  func GZipCompress(data []byte) []byte {
    51  	gw := AcquireGzipWriter()
    52  	_, _ = gw.Write(data)
    53  	_ = gw.Close()
    54  	ret := make([]byte, len(gw.buf.Bytes()))
    55  	copy(ret, gw.buf.Bytes())
    56  	ReleaseGzipWriter(gw)
    57  	return ret
    58  }
    59  
    60  func GZipUncompress(src []byte) []byte {
    61  	b := bytes.NewReader(src)
    62  	var out bytes.Buffer
    63  	r, _ := gzip.NewReader(b)
    64  	defer r.Close()
    65  	_, _ = out.ReadFrom(r)
    66  	return out.Bytes()
    67  }
    68  
    69  func CalculateImageResourceId(md5 []byte) string {
    70  	id := make([]byte, 36+6)[:0]
    71  	id = append(id, '{')
    72  	AppendUUID(id[1:], md5)
    73  	id = id[:37]
    74  	id = append(id, "}.png"...)
    75  	return utils.B2S(bytes.ToUpper(id))
    76  }
    77  
    78  func GenUUID(uuid []byte) []byte {
    79  	return AppendUUID(nil, uuid)
    80  }
    81  
    82  func AppendUUID(dst []byte, uuid []byte) []byte {
    83  	_ = uuid[15]
    84  	if cap(dst) > 36 {
    85  		dst = dst[:36]
    86  		dst[8] = '-'
    87  		dst[13] = '-'
    88  		dst[18] = '-'
    89  		dst[23] = '-'
    90  	} else { // Need Grow
    91  		dst = append(dst, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"...)
    92  	}
    93  	hex.Encode(dst[0:], uuid[0:4])
    94  	hex.Encode(dst[9:], uuid[4:6])
    95  	hex.Encode(dst[14:], uuid[6:8])
    96  	hex.Encode(dst[19:], uuid[8:10])
    97  	hex.Encode(dst[24:], uuid[10:16])
    98  	return dst
    99  }
   100  
   101  func UInt32ToIPV4Address(i uint32) string {
   102  	ip := net.IP{0, 0, 0, 0}
   103  	binary2.LittleEndian.PutUint32(ip, i)
   104  	return ip.String()
   105  }
   106  
   107  func ToBytes(i any) []byte {
   108  	return NewWriterF(func(w *Writer) {
   109  		// TODO: more types
   110  		switch t := i.(type) {
   111  		case int16:
   112  			w.WriteUInt16(uint16(t))
   113  		case int32:
   114  			w.WriteUInt32(uint32(t))
   115  		}
   116  	})
   117  }