2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
buffer.go 21.90 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-01-29 09:51 . 去掉多余的转换
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
package cstruct
import (
"encoding/binary"
"io"
"reflect"
"unsafe"
)
type Buffer struct {
buf []byte
index int
}
func NewBuffer(e []byte) *Buffer {
return &Buffer{buf: e}
}
func (p *Buffer) Reset() {
p.buf = p.buf[:0]
p.index = 0
}
// Pack
func (p *Buffer) Marshal(obj IStruct) error {
t, base, err := getbase(obj)
if structPointer_IsNil(base) {
return ErrNil
}
if err == nil {
props := GetProperties(t.Elem())
s := p.size_struct(props, base)
p.buf = make([]byte, s)
err = p.enc_struct(props, base)
}
return err
}
func getbase(obj IStruct) (t reflect.Type, b structPointer, err error) {
if obj == nil {
err = ErrNil
return
}
t = reflect.TypeOf(obj)
value := reflect.ValueOf(obj)
b = toStructPointer(value)
return
}
func (o *Buffer) size_struct(prop *StructProperties, base structPointer) int {
ret := prop.fixedSize
for _, p := range prop.Prop {
if p.siz != nil {
ret += p.siz(o, p, base)
}
}
return ret
}
func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
for _, p := range prop.Prop {
if p.enc != nil {
if err := p.enc(o, p, base); err != nil {
return err
}
}
}
return nil
}
// Unmarshal
func (p *Buffer) Unmarshal(obj IStruct) error {
typ, base, err := getbase(obj)
if err != nil {
return err
}
return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), base)
}
func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, base structPointer) error {
for _, p := range prop.Prop {
if p.dec != nil {
if err := p.dec(o, p, base); err != nil {
return err
}
}
}
return nil
}
// bool
func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
v := structPointer_BoolVal(base, p.field)
x := 0
if *v {
x = 1
}
o.buf[o.index] = uint8(x)
o.index++
return nil
}
func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
i := o.index + 1
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
u := uint8(o.buf[i-1])
v := structPointer_BoolVal(base, p.field)
*v = (u != 0)
return nil
}
// uint8
func (o *Buffer) enc_uint8(p *Properties, base structPointer) error {
v := (*uint8)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
o.buf[o.index] = *v
o.index++
return nil
}
func (o *Buffer) dec_uint8(p *Properties, base structPointer) error {
i := o.index + 1
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
u := uint8(o.buf[i-1])
v := (*uint8)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
*v = u
return nil
}
// uint16
func (o *Buffer) enc_uint16(p *Properties, base structPointer) error {
v := (*uint16)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
binary.LittleEndian.PutUint16(o.buf[o.index:], *v)
o.index += 2
return nil
}
func (o *Buffer) dec_uint16(p *Properties, base structPointer) error {
u, err := o.readUInt16()
if err != nil {
return err
}
v := (*uint16)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
*v = u
return nil
}
// uint32
func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
v := (*uint32)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
binary.LittleEndian.PutUint32(o.buf[o.index:], *v)
o.index += 4
return nil
}
func (o *Buffer) dec_uint32(p *Properties, base structPointer) error {
v := (*uint32)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
i := o.index + 4
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
*v = binary.LittleEndian.Uint32(o.buf[i-4:])
return nil
}
// uint64
func (o *Buffer) enc_uint64(p *Properties, base structPointer) error {
v := (*uint64)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
binary.LittleEndian.PutUint64(o.buf[o.index:], *v)
o.index += 8
return nil
}
func (o *Buffer) dec_uint64(p *Properties, base structPointer) error {
v := (*uint64)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
i := o.index + 8
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
*v = binary.LittleEndian.Uint64(o.buf[i-8:])
return nil
}
// string
func (o *Buffer) enc_string(p *Properties, base structPointer) error {
v := structPointer_StringVal(base, p.field)
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
if ln > 0 {
copy(o.buf[o.index:], *v)
o.index += ln
}
return nil
}
func (o *Buffer) dec_string(p *Properties, base structPointer) error {
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
buf := o.buf[o.index:end]
o.index = end
v := structPointer_StringVal(base, p.field)
*v = string(buf)
return nil
}
func (o *Buffer) size_string(p *Properties, base structPointer) int {
v := structPointer_StringVal(base, p.field)
return len(*v)
}
// []byte
func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
v := structPointer_Bytes(base, p.field)
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
if ln > 0 {
copy(o.buf[o.index:], *v)
o.index += ln
}
return nil
}
func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
v := structPointer_Bytes(base, p.field)
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
*v = append((*v)[:0], o.buf[o.index:end]...)
o.index = end
return nil
}
func (o *Buffer) size_slice_byte(p *Properties, base structPointer) int {
v := structPointer_Bytes(base, p.field)
return len(*v)
}
// cstruct ptr
func (o *Buffer) enc_substruct_ptr(p *Properties, base structPointer) error {
v := structPointer_GetStructPointer(base, p.field)
if v == nil {
o.buf[o.index] = uint8(0)
o.index++
return nil
} else {
o.buf[o.index] = uint8(1)
o.index++
return o.enc_struct(p.sprop, v)
}
}
func (o *Buffer) dec_substruct_ptr(p *Properties, base structPointer) error {
bas := structPointer_GetStructPointer(base, p.field)
i := o.index + 1
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
flag := uint8(o.buf[i-1])
if flag == 0 {
return nil
}
if structPointer_IsNil(bas) {
bas = toStructPointer(reflect.New(p.stype))
structPointer_SetStructPointer(base, p.field, bas)
}
return o.unmarshalType(p.stype, p.sprop, bas)
}
func (o *Buffer) size_substruct_ptr(p *Properties, base structPointer) int {
v := structPointer_GetStructPointer(base, p.field)
if v == nil {
return 1
}
return o.size_struct(p.sprop, v) + 1
}
// cstruct
func (o *Buffer) enc_substruct(p *Properties, base structPointer) error {
return o.enc_struct(p.sprop, structPointer(unsafe.Pointer(uintptr(base)+uintptr(p.field))))
}
func (o *Buffer) dec_substruct(p *Properties, base structPointer) error {
bas := structPointer(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
return o.unmarshalType(p.stype, p.sprop, bas)
}
func (o *Buffer) size_substruct(p *Properties, base structPointer) int {
return o.size_struct(p.sprop, structPointer(unsafe.Pointer(uintptr(base)+uintptr(p.field))))
}
// []bool
func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
v := structPointer_BoolSlice(base, p.field)
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
x := 0
if (*v)[i] {
x = 1
}
o.buf[o.index] = uint8(x)
o.index += 1
}
return nil
}
func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
v := structPointer_BoolSlice(base, p.field)
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
*v = make([]bool, int(nb))
for i := 0; i < int(nb); i++ {
u := uint8(o.buf[o.index+i])
(*v)[i] = (u != 0)
}
o.index = end
return nil
}
func (o *Buffer) size_slice_bool(p *Properties, base structPointer) int {
v := structPointer_BoolSlice(base, p.field)
return len(*v)
}
// []uint16
func (o *Buffer) enc_slice_uint16(p *Properties, base structPointer) error {
v := (*[]uint16)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint16(o.buf[o.index:], (*v)[i])
o.index += 2
}
return nil
}
func (o *Buffer) dec_slice_uint16(p *Properties, base structPointer) error {
v := (*[]uint16)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)*2
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
*v = make([]uint16, int(nb))
for i := 0; i < int(nb); i++ {
(*v)[i] = binary.LittleEndian.Uint16(o.buf[o.index+i*2:])
}
o.index = end
return nil
}
func (o *Buffer) size_slice_uint16(p *Properties, base structPointer) int {
v := (*[]uint16)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
return len(*v) * 2
}
// []uint32
func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
v := (*[]uint32)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint32(o.buf[o.index:], (*v)[i])
o.index += 4
}
return nil
}
func (o *Buffer) dec_slice_uint32(p *Properties, base structPointer) error {
v := (*[]uint32)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)*4
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
*v = make([]uint32, int(nb))
for i := 0; i < int(nb); i++ {
(*v)[i] = binary.LittleEndian.Uint32(o.buf[o.index+i*4:])
}
o.index = end
return nil
}
func (o *Buffer) size_slice_uint32(p *Properties, base structPointer) int {
v := (*[]uint32)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
return len(*v) * 4
}
// []uint64
func (o *Buffer) enc_slice_uint64(p *Properties, base structPointer) error {
v := (*[]uint64)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint64(o.buf[o.index:], (*v)[i])
o.index += 8
}
return nil
}
func (o *Buffer) dec_slice_uint64(p *Properties, base structPointer) error {
v := (*[]uint64)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)*8
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
*v = make([]uint64, int(nb))
for i := 0; i < int(nb); i++ {
(*v)[i] = binary.LittleEndian.Uint64(o.buf[o.index+i*8:])
}
o.index = end
return nil
}
func (o *Buffer) size_slice_uint64(p *Properties, base structPointer) int {
v := (*[]uint64)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
return len(*v) * 8
}
// []string
func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
v := (*[]string)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
ln2 := len((*v)[i])
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln2))
o.index += 2
if ln2 > 0 {
copy(o.buf[o.index:], (*v)[i])
o.index += ln2
}
}
return nil
}
func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
v := (*[]string)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
nb0, err0 := o.readUInt16()
if err0 != nil {
return err0
}
*v = make([]string, int(nb0))
for i := 0; i < int(nb0); i++ {
nb, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb)
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
(*v)[i] = string(o.buf[o.index:end])
o.index = end
}
return nil
}
func (o *Buffer) size_slice_string(p *Properties, base structPointer) int {
v := (*[]string)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
ret := 0
ln := len(*v)
for i := 0; i < ln; i++ {
ret += len((*v)[i]) + 2
}
return ret
}
// []cstruct
func (o *Buffer) enc_slice_substruct(p *Properties, base structPointer) error {
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
var ln = sliceHeader.Len
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
itemsize := int(p.stype.Size())
for i := 0; i < ln; i++ {
sv := (structPointer)(unsafe.Pointer(sliceHeader.Data + uintptr(i*itemsize)))
o.enc_struct(p.sprop, sv)
}
return nil
}
func (o *Buffer) dec_slice_substruct(p *Properties, base structPointer) error {
nb, err := o.readUInt16()
if err != nil {
return err
}
if nb == 0 {
return nil
}
itemsize := int(p.stype.Size())
data := reflect.MakeSlice(p.t, int(nb), int(nb))
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
sliceHeader.Cap = int(nb)
sliceHeader.Len = int(nb)
sliceHeader.Data = data.Pointer()
for i := 0; i < int(nb); i++ {
data := (structPointer)(unsafe.Pointer(sliceHeader.Data + uintptr(i*itemsize)))
o.unmarshalType(p.stype, p.sprop, data)
}
return nil
}
func (o *Buffer) size_slice_substruct(p *Properties, base structPointer) int {
ret := 0
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(uintptr(base) + uintptr(p.field)))
var ln = sliceHeader.Len
itemsize := int(p.stype.Size())
for i := 0; i < ln; i++ {
sv := (structPointer)(sliceHeader.Data + uintptr(i*itemsize))
ret += o.size_struct(p.sprop, sv)
}
return ret
}
// []struct_ptr
func (o *Buffer) enc_slice_substruct_ptr(p *Properties, base structPointer) error {
v := structPointer_StructPointerSlice(base, p.field)
ln := v.Len()
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
sv := (*v)[i]
if sv == nil {
o.buf[o.index] = uint8(0)
o.index++
} else {
o.buf[o.index] = uint8(1)
o.index++
o.enc_struct(p.sprop, sv)
}
}
return nil
}
func (o *Buffer) dec_slice_substruct_ptr(p *Properties, base structPointer) error {
v := structPointer_StructPointerSlice(base, p.field)
nb, err := o.readUInt16()
if err != nil {
return err
}
for j := 0; j < int(nb); j++ {
i := o.index + 1
if i < 0 || i > len(o.buf) {
return io.ErrUnexpectedEOF
}
o.index = i
flag := uint8(o.buf[i-1])
if flag == 0 {
v.Append(nil)
} else {
bas := toStructPointer(reflect.New(p.stype))
o.unmarshalType(p.stype, p.sprop, bas)
v.Append(bas)
}
}
return nil
}
func (o *Buffer) size_slice_substruct_ptr(p *Properties, base structPointer) int {
ret := 0
v := structPointer_StructPointerSlice(base, p.field)
ln := v.Len()
for i := 0; i < ln; i++ {
sv := (*v)[i]
if sv == nil {
ret += 1
} else {
ret += o.size_struct(p.sprop, sv) + 1
}
}
return ret
}
// []struct_ptr ignore nil
func (o *Buffer) enc_slice_substruct_ptr_ignore_nil(p *Properties, base structPointer) error {
v := structPointer_StructPointerSlice(base, p.field)
ln := v.Len()
len_index := o.index
real_ln := 0
o.index += 2
for i := 0; i < ln; i++ {
sv := (*v)[i]
if sv != nil {
real_ln++
o.enc_struct(p.sprop, sv)
}
}
binary.LittleEndian.PutUint16(o.buf[len_index:], uint16(real_ln))
return nil
}
func (o *Buffer) dec_slice_substruct_ptr_ignore_nil(p *Properties, base structPointer) error {
v := structPointer_StructPointerSlice(base, p.field)
nb, err := o.readUInt16()
if err != nil {
return err
}
for j := 0; j < int(nb); j++ {
bas := toStructPointer(reflect.New(p.stype))
v.Append(bas)
o.unmarshalType(p.stype, p.sprop, bas)
}
return nil
}
func (o *Buffer) size_slice_substruct_ptr_ignore_nil(p *Properties, base structPointer) int {
ret := 0
v := structPointer_StructPointerSlice(base, p.field)
ln := v.Len()
for i := 0; i < ln; i++ {
sv := (*v)[i]
if sv != nil {
ret += o.size_struct(p.sprop, sv)
}
}
return ret
}
// [][]byte
func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
v := structPointer_BytesSlice(base, p.field)
ln := len(*v)
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln))
o.index += 2
for i := 0; i < ln; i++ {
ln2 := len((*v)[i])
binary.LittleEndian.PutUint16(o.buf[o.index:], uint16(ln2))
o.index += 2
if ln2 > 0 {
copy(o.buf[o.index:], (*v)[i])
o.index += ln2
}
}
return nil
}
func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
v := structPointer_BytesSlice(base, p.field)
nb, err := o.readUInt16()
if err != nil {
return err
}
for i := 0; i < int(nb); i++ {
nb2, err := o.readUInt16()
if err != nil {
return err
}
end := o.index + int(nb2)
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
buf := o.buf[o.index:end]
o.index = end
*v = append(*v, buf)
}
return nil
}
func (o *Buffer) size_slice_slice_byte(p *Properties, base structPointer) int {
v := structPointer_BytesSlice(base, p.field)
ret := 0
ln := len(*v)
for i := 0; i < ln; i++ {
ret += len((*v)[i]) + 2
}
return ret
}
func (o *Buffer) readUInt16() (uint16, error) {
i := o.index + 2
if i < 0 || i > len(o.buf) {
return 0, io.ErrUnexpectedEOF
}
o.index = i
return binary.LittleEndian.Uint16(o.buf[i-2:]), nil
}
// [n]byte [n]uint8 [n]int8 [n]bool
func (o *Buffer) enc_array_byte(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
var data []byte
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
copy(o.buf[o.index:], data)
o.index += ln
}
return nil
}
func (o *Buffer) dec_array_byte(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
end := o.index + ln
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
var data []byte
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
copy(data, o.buf[o.index:end])
o.index = end
}
return nil
}
func (o *Buffer) size_array_byte(p *Properties, base structPointer) int {
return p.t.Len()
}
// [n]uint16 [n]int16
func (o *Buffer) enc_array_uint16(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
var data []uint16
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint16(o.buf[o.index:], data[i])
o.index += 2
}
}
return nil
}
func (o *Buffer) dec_array_uint16(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
end := o.index + ln*2
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
var data []uint16
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
data[i] = binary.LittleEndian.Uint16(o.buf[o.index+i*2:])
}
o.index = end
}
return nil
}
func (o *Buffer) size_array_uint16(p *Properties, base structPointer) int {
return p.t.Len() * 2
}
// [n]uint32 [n]int32 [n]float32
func (o *Buffer) enc_array_uint32(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
var data []uint32
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint32(o.buf[o.index:], data[i])
o.index += 4
}
}
return nil
}
func (o *Buffer) dec_array_uint32(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
end := o.index + ln*4
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
var data []uint32
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
data[i] = binary.LittleEndian.Uint32(o.buf[o.index+i*4:])
}
o.index = end
}
return nil
}
func (o *Buffer) size_array_uint32(p *Properties, base structPointer) int {
return p.t.Len() * 4
}
// [n]uint64 [n]int64 [n]float64
func (o *Buffer) enc_array_uint64(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
var data []uint64
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
binary.LittleEndian.PutUint64(o.buf[o.index:], data[i])
o.index += 8
}
}
return nil
}
func (o *Buffer) dec_array_uint64(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
end := o.index + ln*8
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
var data []uint64
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = ln
sliceHeader.Len = ln
sliceHeader.Data = uintptr(base) + uintptr(p.field)
for i := 0; i < ln; i++ {
data[i] = binary.LittleEndian.Uint64(o.buf[o.index+i*8:])
}
o.index = end
}
return nil
}
func (o *Buffer) size_array_uint64(p *Properties, base structPointer) int {
return p.t.Len() * 8
}
// [n]cstruct
func (o *Buffer) enc_array_substruct(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
itemsize := int(p.stype.Size())
for i := 0; i < ln; i++ {
data := (structPointer)(unsafe.Pointer(uintptr(base) + uintptr(p.field) + uintptr(i*itemsize)))
o.enc_struct(p.sprop, data)
}
}
return nil
}
func (o *Buffer) dec_array_substruct(p *Properties, base structPointer) error {
ln := p.t.Len()
if ln > 0 {
itemsize := o.size_substruct(p, base)
end := o.index + ln*itemsize
if end < o.index || end > len(o.buf) {
return io.ErrUnexpectedEOF
}
for i := 0; i < ln; i++ {
data := (structPointer)(unsafe.Pointer(uintptr(base) + uintptr(p.field) + uintptr(i*int(p.stype.Size()))))
o.unmarshalType(p.stype, p.sprop, data)
}
o.index = end
}
return nil
}
func (o *Buffer) size_array_substruct(p *Properties, base structPointer) int {
return p.t.Len() * o.size_substruct(p, base)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.16.4

搜索帮助