diff --git a/example/gobe_generated.go b/example/gobe_generated.go index 6008675..3a30753 100644 --- a/example/gobe_generated.go +++ b/example/gobe_generated.go @@ -1,5 +1,9 @@ package main +import ( + ns25529 "unsafe" +) + func (ns25519 *Player) SizeGOBE() uint64 { var ns25520 uint64 @@ -86,13 +90,14 @@ func (ns25527 *Player) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { if uint64(len(src)) < offset+ns25528 { return } - ns25527.Name = string(src[offset : offset+ns25528]) + var ns25530 []byte = src[offset : offset+ns25528] + ns25527.Name = *(*string)(ns25529.Pointer(&ns25530)) offset += ns25528 // ZZ: (./example.CustomUint8)(ns25527.Health) - ns25529, ns25530 := ns25527.Health.ZZUnmarshalGOBE(src[offset:]) - offset += ns25529 - if !ns25530 { + ns25531, ns25532 := ns25527.Health.ZZUnmarshalGOBE(src[offset:]) + offset += ns25531 + if !ns25532 { return } @@ -101,52 +106,52 @@ func (ns25527 *Player) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { return } _ = src[offset+7] - var ns25531 uint64 = uint64(src[offset]) | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56 + var ns25533 uint64 = uint64(src[offset]) | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56 offset += 8 - if uint64(cap(ns25527.Weapons)) < ns25531 { - if ns25531*uint64(18) <= 1<<15 { - ns25527.Weapons = make([]Weapon, ns25531) - for ns25532 := uint64(0); ns25532 < ns25531; ns25532++ { - - // ZZ: (./example.Weapon)(ns25527.Weapons[ns25532]) - ns25534, ns25535 := ns25527.Weapons[ns25532].UnmarshalGOBE(src[offset:]) - offset += ns25534 - if !ns25535 { + if uint64(cap(ns25527.Weapons)) < ns25533 { + if ns25533*uint64(18) <= 1<<15 { + ns25527.Weapons = make([]Weapon, ns25533) + for ns25534 := uint64(0); ns25534 < ns25533; ns25534++ { + + // ZZ: (./example.Weapon)(ns25527.Weapons[ns25534]) + ns25536, ns25537 := ns25527.Weapons[ns25534].UnmarshalGOBE(src[offset:]) + offset += ns25536 + if !ns25537 { return } } } else { // Slice too large, Using Append ns25527.Weapons = ns25527.Weapons[:0] - for ns25532 := uint64(0); ns25532 < ns25531; ns25532++ { - var ns25533 Weapon + for ns25534 := uint64(0); ns25534 < ns25533; ns25534++ { + var ns25535 Weapon - // ZZ: (./example.Weapon)(ns25533) - ns25536, ns25537 := ns25533.UnmarshalGOBE(src[offset:]) - offset += ns25536 - if !ns25537 { + // ZZ: (./example.Weapon)(ns25535) + ns25538, ns25539 := ns25535.UnmarshalGOBE(src[offset:]) + offset += ns25538 + if !ns25539 { return } - ns25527.Weapons = append(ns25527.Weapons, ns25533) + ns25527.Weapons = append(ns25527.Weapons, ns25535) } } } else { - ns25527.Weapons = ns25527.Weapons[:ns25531] - for ns25532 := uint64(0); ns25532 < ns25531; ns25532++ { + ns25527.Weapons = ns25527.Weapons[:ns25533] + for ns25534 := uint64(0); ns25534 < ns25533; ns25534++ { - // ZZ: (./example.Weapon)(ns25527.Weapons[ns25532]) - ns25538, ns25539 := ns25527.Weapons[ns25532].UnmarshalGOBE(src[offset:]) - offset += ns25538 - if !ns25539 { + // ZZ: (./example.Weapon)(ns25527.Weapons[ns25534]) + ns25540, ns25541 := ns25527.Weapons[ns25534].UnmarshalGOBE(src[offset:]) + offset += ns25540 + if !ns25541 { return } } } // ZZ: (./example.Position)(ns25527.Position) - ns25540, ns25541 := ns25527.Position.UnmarshalGOBE(src[offset:]) - offset += ns25540 - if !ns25541 { + ns25542, ns25543 := ns25527.Position.UnmarshalGOBE(src[offset:]) + offset += ns25542 + if !ns25543 { return } @@ -154,95 +159,95 @@ func (ns25527 *Player) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { return } -func (ns25542 *Position) SizeGOBE() uint64 { - var ns25543 uint64 - - // ZZ: (struct{X uint64; Y uint64; Z uint64})(ns25542) - - // ZZ: (uint64)(ns25542.X) - ns25543 += 8 - - // ZZ: (uint64)(ns25542.Y) - ns25543 += 8 - - // ZZ: (uint64)(ns25542.Z) - ns25543 += 8 - - return ns25543 -} - -func (ns25544 *Position) MarshalGOBE(dst []byte) uint64 { +func (ns25544 *Position) SizeGOBE() uint64 { var ns25545 uint64 // ZZ: (struct{X uint64; Y uint64; Z uint64})(ns25544) // ZZ: (uint64)(ns25544.X) - _ = dst[ns25545+7] - dst[ns25545+0] = byte(ns25544.X >> 0) - dst[ns25545+1] = byte(ns25544.X >> 8) - dst[ns25545+2] = byte(ns25544.X >> 16) - dst[ns25545+3] = byte(ns25544.X >> 24) - dst[ns25545+4] = byte(ns25544.X >> 32) - dst[ns25545+5] = byte(ns25544.X >> 40) - dst[ns25545+6] = byte(ns25544.X >> 48) - dst[ns25545+7] = byte(ns25544.X >> 56) ns25545 += 8 // ZZ: (uint64)(ns25544.Y) - _ = dst[ns25545+7] - dst[ns25545+0] = byte(ns25544.Y >> 0) - dst[ns25545+1] = byte(ns25544.Y >> 8) - dst[ns25545+2] = byte(ns25544.Y >> 16) - dst[ns25545+3] = byte(ns25544.Y >> 24) - dst[ns25545+4] = byte(ns25544.Y >> 32) - dst[ns25545+5] = byte(ns25544.Y >> 40) - dst[ns25545+6] = byte(ns25544.Y >> 48) - dst[ns25545+7] = byte(ns25544.Y >> 56) ns25545 += 8 // ZZ: (uint64)(ns25544.Z) - _ = dst[ns25545+7] - dst[ns25545+0] = byte(ns25544.Z >> 0) - dst[ns25545+1] = byte(ns25544.Z >> 8) - dst[ns25545+2] = byte(ns25544.Z >> 16) - dst[ns25545+3] = byte(ns25544.Z >> 24) - dst[ns25545+4] = byte(ns25544.Z >> 32) - dst[ns25545+5] = byte(ns25544.Z >> 40) - dst[ns25545+6] = byte(ns25544.Z >> 48) - dst[ns25545+7] = byte(ns25544.Z >> 56) ns25545 += 8 return ns25545 } -func (ns25546 *Position) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { +func (ns25546 *Position) MarshalGOBE(dst []byte) uint64 { + var ns25547 uint64 // ZZ: (struct{X uint64; Y uint64; Z uint64})(ns25546) // ZZ: (uint64)(ns25546.X) + _ = dst[ns25547+7] + dst[ns25547+0] = byte(ns25546.X >> 0) + dst[ns25547+1] = byte(ns25546.X >> 8) + dst[ns25547+2] = byte(ns25546.X >> 16) + dst[ns25547+3] = byte(ns25546.X >> 24) + dst[ns25547+4] = byte(ns25546.X >> 32) + dst[ns25547+5] = byte(ns25546.X >> 40) + dst[ns25547+6] = byte(ns25546.X >> 48) + dst[ns25547+7] = byte(ns25546.X >> 56) + ns25547 += 8 + + // ZZ: (uint64)(ns25546.Y) + _ = dst[ns25547+7] + dst[ns25547+0] = byte(ns25546.Y >> 0) + dst[ns25547+1] = byte(ns25546.Y >> 8) + dst[ns25547+2] = byte(ns25546.Y >> 16) + dst[ns25547+3] = byte(ns25546.Y >> 24) + dst[ns25547+4] = byte(ns25546.Y >> 32) + dst[ns25547+5] = byte(ns25546.Y >> 40) + dst[ns25547+6] = byte(ns25546.Y >> 48) + dst[ns25547+7] = byte(ns25546.Y >> 56) + ns25547 += 8 + + // ZZ: (uint64)(ns25546.Z) + _ = dst[ns25547+7] + dst[ns25547+0] = byte(ns25546.Z >> 0) + dst[ns25547+1] = byte(ns25546.Z >> 8) + dst[ns25547+2] = byte(ns25546.Z >> 16) + dst[ns25547+3] = byte(ns25546.Z >> 24) + dst[ns25547+4] = byte(ns25546.Z >> 32) + dst[ns25547+5] = byte(ns25546.Z >> 40) + dst[ns25547+6] = byte(ns25546.Z >> 48) + dst[ns25547+7] = byte(ns25546.Z >> 56) + ns25547 += 8 + + return ns25547 +} + +func (ns25548 *Position) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { + + // ZZ: (struct{X uint64; Y uint64; Z uint64})(ns25548) + + // ZZ: (uint64)(ns25548.X) if uint64(len(src)) < offset+8 { return } _ = src[offset+7] - ns25546.X = uint64( + ns25548.X = uint64( uint64(src[offset+0])<<0 | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56) offset += 8 - // ZZ: (uint64)(ns25546.Y) + // ZZ: (uint64)(ns25548.Y) if uint64(len(src)) < offset+8 { return } _ = src[offset+7] - ns25546.Y = uint64( + ns25548.Y = uint64( uint64(src[offset+0])<<0 | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56) offset += 8 - // ZZ: (uint64)(ns25546.Z) + // ZZ: (uint64)(ns25548.Z) if uint64(len(src)) < offset+8 { return } _ = src[offset+7] - ns25546.Z = uint64( + ns25548.Z = uint64( uint64(src[offset+0])<<0 | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56) offset += 8 @@ -250,71 +255,72 @@ func (ns25546 *Position) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { return } -func (ns25547 *Weapon) SizeGOBE() uint64 { - var ns25548 uint64 - - // ZZ: (struct{Name string; Damage uint16})(ns25547) - - // ZZ: (string)(ns25547.Name) - ns25548 += 8 + uint64(len(ns25547.Name)) - - // ZZ: (uint16)(ns25547.Damage) - ns25548 += 2 - - return ns25548 -} - -func (ns25549 *Weapon) MarshalGOBE(dst []byte) uint64 { +func (ns25549 *Weapon) SizeGOBE() uint64 { var ns25550 uint64 // ZZ: (struct{Name string; Damage uint16})(ns25549) // ZZ: (string)(ns25549.Name) - var ns25551 uint64 = uint64(len(ns25549.Name)) - _ = dst[ns25550+7] - dst[ns25550+0] = byte(ns25551 >> 0) - dst[ns25550+1] = byte(ns25551 >> 8) - dst[ns25550+2] = byte(ns25551 >> 16) - dst[ns25550+3] = byte(ns25551 >> 24) - dst[ns25550+4] = byte(ns25551 >> 32) - dst[ns25550+5] = byte(ns25551 >> 40) - dst[ns25550+6] = byte(ns25551 >> 48) - dst[ns25550+7] = byte(ns25551 >> 56) - copy(dst[ns25550+8:], ns25549.Name) - ns25550 += 8 + ns25551 + ns25550 += 8 + uint64(len(ns25549.Name)) // ZZ: (uint16)(ns25549.Damage) - _ = dst[ns25550+1] - dst[ns25550+0] = byte(ns25549.Damage >> 0) - dst[ns25550+1] = byte(ns25549.Damage >> 8) ns25550 += 2 return ns25550 } -func (ns25552 *Weapon) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { +func (ns25551 *Weapon) MarshalGOBE(dst []byte) uint64 { + var ns25552 uint64 + + // ZZ: (struct{Name string; Damage uint16})(ns25551) + + // ZZ: (string)(ns25551.Name) + var ns25553 uint64 = uint64(len(ns25551.Name)) + _ = dst[ns25552+7] + dst[ns25552+0] = byte(ns25553 >> 0) + dst[ns25552+1] = byte(ns25553 >> 8) + dst[ns25552+2] = byte(ns25553 >> 16) + dst[ns25552+3] = byte(ns25553 >> 24) + dst[ns25552+4] = byte(ns25553 >> 32) + dst[ns25552+5] = byte(ns25553 >> 40) + dst[ns25552+6] = byte(ns25553 >> 48) + dst[ns25552+7] = byte(ns25553 >> 56) + copy(dst[ns25552+8:], ns25551.Name) + ns25552 += 8 + ns25553 + + // ZZ: (uint16)(ns25551.Damage) + _ = dst[ns25552+1] + dst[ns25552+0] = byte(ns25551.Damage >> 0) + dst[ns25552+1] = byte(ns25551.Damage >> 8) + ns25552 += 2 + + return ns25552 +} + +func (ns25554 *Weapon) UnmarshalGOBE(src []byte) (offset uint64, ok bool) { - // ZZ: (struct{Name string; Damage uint16})(ns25552) + // ZZ: (struct{Name string; Damage uint16})(ns25554) - // ZZ: (string)(ns25552.Name) + // ZZ: (string)(ns25554.Name) if uint64(len(src)) < offset+8 { return } _ = src[offset+7] - var ns25553 uint64 = uint64(src[offset]) | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56 + var ns25555 uint64 = uint64(src[offset]) | uint64(src[offset+1])<<8 | uint64(src[offset+2])<<16 | uint64(src[offset+3])<<24 | uint64(src[offset+4])<<32 | uint64(src[offset+5])<<40 | uint64(src[offset+6])<<48 | uint64(src[offset+7])<<56 offset += 8 - if uint64(len(src)) < offset+ns25553 { + if uint64(len(src)) < offset+ns25555 { return } - ns25552.Name = string(src[offset : offset+ns25553]) - offset += ns25553 + var ns25556 []byte = src[offset : offset+ns25555] + ns25554.Name = *(*string)(ns25529.Pointer(&ns25556)) + offset += ns25555 - // ZZ: (uint16)(ns25552.Damage) + // ZZ: (uint16)(ns25554.Damage) if uint64(len(src)) < offset+2 { return } _ = src[offset+1] - ns25552.Damage = uint16( + ns25554.Damage = uint16( uint16(src[offset+0])<<0 | uint16(src[offset+1])<<8) offset += 2 diff --git a/main.go b/main.go index ad8fbdb..6d8dee9 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,7 @@ var config = struct { Version: flag.Bool("version", false, "Print version and exit"), } -const VERSION = "0.0.5" +const VERSION = "0.0.6" const VERSION_STRING = "gobe version v" + VERSION + " " + runtime.GOOS + "/" + runtime.GOARCH func main() { @@ -1656,20 +1656,48 @@ func generateUnmarshalBody(ctx *GenerateContext, name string, rt *types.Named, t __slen, ) - // if uint64(len(src)) < offset+__slen { - // return - // } - // name = string(src[offset:offset+__slen]) - // offset += __slen - ctx.Generated[rt] = fmt.Appendf( - ctx.Generated[rt], - " if uint64(len(src)) < offset+%s {\n"+ - " return\n"+ - " }\n"+ - " %s = %s(src[offset:offset+%s])\n"+ - " offset += %s\n", - __slen, name, Type, __slen, __slen, - ) + if *config.AllowZeroCopyString { + if !*config.AllowUnsafe { + panic("unsafe is not allowed!, please use --allow-unsafe to enable unsafe feature") + } + + if _, ok := ctx.LibAlias["unsafe"]; !ok { + ctx.LibAlias["unsafe"] = ctx.nextName() + } + __unsafe := ctx.LibAlias["unsafe"] + __str := ctx.nextName() + // if uint64(len(src)) < offset+__slen { + // return + // } + // var __str []byte = src[offset:offset+__slen] + // name = *(*Type)(unsafe.Pointer(&__str)) + // offset += __slen + ctx.Generated[rt] = fmt.Appendf( + ctx.Generated[rt], + " if uint64(len(src)) < offset+%s {\n"+ + " return\n"+ + " }\n"+ + " var %s []byte = src[offset:offset+%s]\n"+ + " %s = *(*%s)(%s.Pointer(&%s))\n"+ + " offset += %s\n", + __slen, __str, __slen, name, Type, __unsafe, __str, __slen, + ) + } else { + // if uint64(len(src)) < offset+__slen { + // return + // } + // name = string(src[offset:offset+__slen]) + // offset += __slen + ctx.Generated[rt] = fmt.Appendf( + ctx.Generated[rt], + " if uint64(len(src)) < offset+%s {\n"+ + " return\n"+ + " }\n"+ + " %s = %s(src[offset:offset+%s])\n"+ + " offset += %s\n", + __slen, name, Type, __slen, __slen, + ) + } } } }