Skip to content

Commit

Permalink
add enum and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Aug 11, 2021
1 parent 071f95e commit ef65a62
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 183 deletions.
10 changes: 8 additions & 2 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (s *ObjectStream) AddReference(obj Object) {
s.handler++
}

func (s *ObjectStream) References() *orderedmap.OrderedMap {
return s.references
func (s *ObjectStream) GetReference(handler uint32) Object {
for pair := s.references.Oldest(); pair != nil; pair = pair.Next() {
if pair.Key == handler {
return pair.Value.(Object)
}
}

return nil
}
9 changes: 9 additions & 0 deletions classname_bag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package javaserialize

type ClassBag struct {
Classes []*TCClassDesc
}

func (bag *ClassBag) Add(classDesc *TCClassDesc) {
bag.Classes = append(bag.Classes, classDesc)
}
14 changes: 8 additions & 6 deletions example/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"encoding/binary"
"fmt"
"github.com/k0kubun/pp"
"testing"
)

func TestConvert(t *testing.T) {
var r rune = '你'
var bs = make([]byte, 4)
binary.BigEndian.PutUint32(bs, uint32(r))
fmt.Println(bs)
var s = []string {"1", "2"}
sample(s)
pp.Println(s)
}

func sample(s []string) {
s = append(s, "3")
}
8 changes: 3 additions & 5 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"encoding/hex"
"fmt"
"github.com/k0kubun/pp"
"github.com/phith0n/javaserialize"
"io/ioutil"
)

func main() {
data, err := ioutil.ReadFile("example/string.poc")
data, err := ioutil.ReadFile("example/object.poc")
if err != nil {
fmt.Println(err.Error())
return
Expand All @@ -21,7 +21,5 @@ func main() {
return
}

for _, o := range os.Contents {
fmt.Println(hex.EncodeToString(o.ToBytes()))
}
pp.Println(os)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/phith0n/javaserialize
go 1.16

require (
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/thoas/go-funk v0.9.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -35,6 +41,9 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
21 changes: 18 additions & 3 deletions tc_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package javaserialize
import (
"encoding/binary"
"fmt"
"strings"
)

type TCArray struct {
Expand All @@ -23,12 +24,11 @@ func (t *TCArray) ToBytes() []byte {
}

func readTCArray(stream *ObjectStream) (*TCArray, error) {
var classes []*TCClassDesc
var array = new(TCArray)
var err error

_, _ = stream.ReadN(1)
array.ClassPointer, err = readTCClassPointer(stream, classes)
array.ClassPointer, err = readTCClassPointer(stream, nil)
if err != nil {
return nil, err
}
Expand All @@ -39,9 +39,24 @@ func readTCArray(stream *ObjectStream) (*TCArray, error) {
return nil, fmt.Errorf("read JAVA_TC_ARRAY object failed on index %v", stream.CurrentIndex())
}

classDesc, err := array.ClassPointer.GetClassDesc(stream)
if err != nil {
return nil, err
}

className := string(classDesc.ClassName.data)
if !strings.HasPrefix(className, "[") || len(className) < 2 {
return nil, fmt.Errorf("JAVA_TC_ARRAY ClassName %v is error in %v", className, stream.CurrentIndex())
}

size := binary.BigEndian.Uint32(bs)
for i := uint32(0); i < size; i++ {
// TODO
value, err := readTCValue(stream, className[1:2])
if err != nil {
return nil, err
}

array.ArrayData = append(array.ArrayData, value)
}

return array, nil
Expand Down
3 changes: 1 addition & 2 deletions tc_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ func (c *TCClass) ToBytes() []byte {
}

func readTCClass(stream *ObjectStream) (*TCClass, error) {
var classes []*TCClassDesc // current unused
var class = new(TCClass)
var err error

_, _ = stream.ReadN(1)
class.ClassPointer, err = readTCClassPointer(stream, classes)
class.ClassPointer, err = readTCClassPointer(stream, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 15 additions & 2 deletions tc_classdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ type TCClassData struct {
}

func (cd *TCClassData) ToBytes() []byte {
return nil
var bs []byte
for _, value := range cd.FieldDatas {
bs = append(bs, value.ToBytes()...)
}

for _, content := range cd.ObjectAnnotation {
bs = append(bs, content.ToBytes()...)
}

if len(cd.ObjectAnnotation) > 0 {
bs = append(bs, JAVA_TC_ENDBLOCKDATA)
}

return bs
}

func readTCClassData(stream *ObjectStream, desc *TCClassDesc) (*TCClassData, error) {
Expand Down Expand Up @@ -35,5 +48,5 @@ func readTCClassData(stream *ObjectStream, desc *TCClassDesc) (*TCClassData, err
}

func readTCFieldData(stream *ObjectStream, field *TCFieldDesc) (*TCValue, error) {
return field.read(stream)
return readTCValue(stream, field.TypeCode)
}
9 changes: 6 additions & 3 deletions tc_classdesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ func (desc *TCClassDesc) HasFlag(flag byte) bool {
return (desc.ClassDescFlags & flag) == flag
}

func readTCClassDesc(stream *ObjectStream, classes []*TCClassDesc) (*TCClassDesc, error) {
func readTCClassDesc(stream *ObjectStream, bag *ClassBag) (*TCClassDesc, error) {
var err error
var classDesc = new(TCClassDesc)
classes = append(classes, classDesc)

if bag != nil {
bag.Add(classDesc)
}

// read JAVA_TC_CLASSDESC flag
_, _ = stream.ReadN(1)
Expand Down Expand Up @@ -82,7 +85,7 @@ func readTCClassDesc(stream *ObjectStream, classes []*TCClassDesc) (*TCClassDesc
}

// superClassDesc
classDesc.SuperClassPointer, err = readTCClassPointer(stream, classes)
classDesc.SuperClassPointer, err = readTCClassPointer(stream, bag)
if err != nil {
return nil, err
}
Expand Down
23 changes: 21 additions & 2 deletions tc_classpointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ func (cp *TCClassPointer) ToBytes() []byte {
return result
}

func readTCClassPointer(stream *ObjectStream, classes []*TCClassDesc) (*TCClassPointer, error) {
func (cp *TCClassPointer) GetClassDesc(stream *ObjectStream) (*TCClassDesc, error) {
if cp.Flag == JAVA_TC_NULL {
return nil, fmt.Errorf("JAVA_TC_NULL is not allowed here")
} else if cp.Flag == JAVA_TC_CLASSDESC {
return cp.ClassDesc, nil
} else {
obj := stream.GetReference(cp.Reference.Handler)
if obj == nil {
return nil, fmt.Errorf("JAVA_TC_REFERENCE handler not found")
}

if obj, ok := obj.(*TCClassDesc); ok {
return obj, nil
}

return nil, fmt.Errorf("JAVA_TC_REFERENCE handler not found")
}
}

func readTCClassPointer(stream *ObjectStream, bag *ClassBag) (*TCClassPointer, error) {
// read JAVA_TC_CLASSDESC Flag
flag, _ := stream.PeekN(1)
if flag[0] == JAVA_TC_NULL {
Expand All @@ -44,7 +63,7 @@ func readTCClassPointer(stream *ObjectStream, classes []*TCClassDesc) (*TCClassP
Reference: reference,
}, nil
} else if flag[0] == JAVA_TC_CLASSDESC {
desc, err := readTCClassDesc(stream, classes)
desc, err := readTCClassDesc(stream, bag)
if err != nil {
return nil, err
}
Expand Down
41 changes: 37 additions & 4 deletions tc_content.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
package javaserialize

type TCContent struct {
Flag byte
Object *TCObject
String *TCString
Array *TCArray
BlockData *TCBlockData
Class *TCClass
ClassDesc *TCClassDesc
Null *TCNull
Enum *TCEnum
Reference *TCReference
}

func (c *TCContent) ToBytes() []byte {
return nil
var bs []byte
switch c.Flag {
case JAVA_TC_STRING, JAVA_TC_LONGSTRING:
bs = c.String.ToBytes()
case JAVA_TC_BLOCKDATA, JAVA_TC_LONGSTRING:
bs = c.BlockData.ToBytes()
case JAVA_TC_CLASS:
bs = c.Class.ToBytes()
case JAVA_TC_OBJECT:
bs = c.Object.ToBytes()
case JAVA_TC_NULL:
bs = c.Null.ToBytes()
case JAVA_TC_REFERENCE:
bs = c.Reference.ToBytes()
case JAVA_TC_ENUM:
bs = c.Enum.ToBytes()
case JAVA_TC_ARRAY:
bs = c.Array.ToBytes()
case JAVA_TC_RESET:
bs = []byte{JAVA_TC_RESET}
}

return bs
}

func readTCContent(stream *ObjectStream) (*TCContent, error) {
var err error = nil
var content = new(TCContent)

switch next, _ := stream.PeekN(1); next[0] {
next, _ := stream.PeekN(1)
content.Flag = next[0]
switch next[0] {
case JAVA_TC_STRING, JAVA_TC_LONGSTRING:
content.String, err = readTCString(stream)
case JAVA_TC_BLOCKDATA:
case JAVA_TC_BLOCKDATALONG:
case JAVA_TC_BLOCKDATA, JAVA_TC_BLOCKDATALONG:
content.BlockData, err = readTCBlockData(stream)
case JAVA_TC_OBJECT:
content.Object, err = readTCObject(stream)
case JAVA_TC_CLASS:
content.Class, err = readTCClass(stream)
case JAVA_TC_CLASSDESC:
content.ClassDesc, err = readTCClassDesc(stream, nil)
case JAVA_TC_NULL:
content.Null = readTCNull(stream)
case JAVA_TC_REFERENCE:
content.Reference, err = readTCReference(stream)
case JAVA_TC_ARRAY:
content.Array, err = readTCArray(stream)
case JAVA_TC_ENUM:
content.Enum, err = readTCEnum(stream)
}

if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions tc_enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package javaserialize

type TCEnum struct {
ClassPointer *TCClassPointer
ConstantName *TCString
}

func (e *TCEnum) ToBytes() []byte {
var bs = []byte{JAVA_TC_ENUM}
bs = append(bs, e.ClassPointer.ToBytes()...)
bs = append(bs, e.ConstantName.ToBytes()...)
return bs
}

func readTCEnum(stream *ObjectStream) (*TCEnum, error) {
var enum = new(TCEnum)
var err error

_, _ = stream.ReadN(1)
enum.ClassPointer, err = readTCClassPointer(stream, nil)
if err != nil {
return nil, err
}

stream.AddReference(enum)
enum.ConstantName, err = readTCString(stream)
if err != nil {
return nil, err
}

return enum, nil
}
20 changes: 0 additions & 20 deletions tc_fielddata.go

This file was deleted.

Loading

0 comments on commit ef65a62

Please sign in to comment.