github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/binary/writer.go (about) 1 package binary 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 ) 8 9 // Writer 写入 10 type Writer bytes.Buffer 11 12 func NewWriterF(f func(writer *Writer)) []byte { 13 w := SelectWriter() 14 f(w) 15 b := make([]byte, len(w.Bytes())) 16 copy(b, w.Bytes()) 17 w.put() 18 return b 19 } 20 21 // OpenWriterF must call func cl to close 22 func OpenWriterF(f func(*Writer)) (b []byte, cl func()) { 23 w := SelectWriter() 24 f(w) 25 return w.Bytes(), w.put 26 } 27 28 func (w *Writer) FillUInt16() (pos int) { 29 pos = w.Len() 30 (*bytes.Buffer)(w).Write([]byte{0, 0}) 31 return 32 } 33 34 func (w *Writer) WriteUInt16At(pos int, v uint16) { 35 newdata := (*bytes.Buffer)(w).Bytes()[pos:] 36 binary.BigEndian.PutUint16(newdata, v) 37 } 38 39 func (w *Writer) FillUInt32() (pos int) { 40 pos = w.Len() 41 (*bytes.Buffer)(w).Write([]byte{0, 0, 0, 0}) 42 return 43 } 44 45 func (w *Writer) WriteUInt32At(pos int, v uint32) { 46 newdata := (*bytes.Buffer)(w).Bytes()[pos:] 47 binary.BigEndian.PutUint32(newdata, v) 48 } 49 50 func (w *Writer) Write(b []byte) { 51 (*bytes.Buffer)(w).Write(b) 52 } 53 54 func (w *Writer) WriteHex(h string) { 55 b, _ := hex.DecodeString(h) 56 w.Write(b) 57 } 58 59 func (w *Writer) WriteByte(b byte) { 60 (*bytes.Buffer)(w).WriteByte(b) 61 } 62 63 func (w *Writer) WriteUInt16(v uint16) { 64 b := make([]byte, 2) 65 binary.BigEndian.PutUint16(b, v) 66 w.Write(b) 67 } 68 69 func (w *Writer) WriteUInt32(v uint32) { 70 b := make([]byte, 4) 71 binary.BigEndian.PutUint32(b, v) 72 w.Write(b) 73 } 74 75 func (w *Writer) WriteUInt64(v uint64) { 76 b := make([]byte, 8) 77 binary.BigEndian.PutUint64(b, v) 78 w.Write(b) 79 } 80 81 func (w *Writer) WriteString(v string) { 82 w.WriteUInt32(uint32(len(v) + 4)) 83 (*bytes.Buffer)(w).WriteString(v) 84 } 85 86 func (w *Writer) WriteStringShort(v string) { 87 w.WriteUInt16(uint16(len(v))) 88 (*bytes.Buffer)(w).WriteString(v) 89 } 90 91 func (w *Writer) WriteBool(b bool) { 92 if b { 93 w.WriteByte(0x01) 94 } else { 95 w.WriteByte(0x00) 96 } 97 } 98 99 func (w *Writer) EncryptAndWrite(key []byte, data []byte) { 100 w.Write(NewTeaCipher(key).Encrypt(data)) 101 } 102 103 func (w *Writer) WriteIntLvPacket(offset int, f func(*Writer)) { 104 pos := w.FillUInt32() 105 f(w) 106 w.WriteUInt32At(pos, uint32(w.Len()+offset-pos-4)) 107 } 108 109 func (w *Writer) WriteBytesShort(data []byte) { 110 w.WriteUInt16(uint16(len(data))) 111 w.Write(data) 112 } 113 114 func (w *Writer) WriteTlvLimitedSize(data []byte, limit int) { 115 if len(data) <= limit { 116 w.WriteBytesShort(data) 117 return 118 } 119 w.WriteBytesShort(data[:limit]) 120 } 121 122 func (w *Writer) Len() int { 123 return (*bytes.Buffer)(w).Len() 124 } 125 126 func (w *Writer) Bytes() []byte { 127 return (*bytes.Buffer)(w).Bytes() 128 } 129 130 func (w *Writer) Reset() { 131 (*bytes.Buffer)(w).Reset() 132 } 133 134 func (w *Writer) Grow(n int) { 135 (*bytes.Buffer)(w).Grow(n) 136 } 137 138 func (w *Writer) put() { 139 PutWriter(w) 140 }