Skip to content

Commit

Permalink
added constant class, string field
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Aug 17, 2021
1 parent 5b9d4af commit 9312b0a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
6 changes: 5 additions & 1 deletion class/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (cf *ClassFile) readConstant(stream *commons.Stream) error {
case CONSTANT_LONG_INFO:
obj, err = readConstantLong(stream)
case CONSTANT_DOUBLE_INFO:

obj, err = readConstantDouble(stream)
case CONSTANT_CLASS_INFO:
obj, err = readConstantClass(stream)
case CONSTANT_STRING_INGFO:
obj, err = readConstantString(stream)
}

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

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
)

type ConstantClass struct {
NameOffset uint16
Reference *ConstantUTF8 // Reference must be a ConstantUTF8 object
}

func (c *ConstantClass) ToBytes() []byte {
var bs = []byte{CONSTANT_CLASS_INFO}
bs = append(bs, commons.NumberToBytes(c.NameOffset)...)
return bs
}

func readConstantClass(stream *commons.Stream) (*ConstantClass, error) {
_, _ = stream.ReadN(1)
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read constant class failed, no enough data in the stream")
}

var i = binary.BigEndian.Uint16(bs)
return &ConstantClass{
NameOffset: i,
// lack of Reference because ConstantPool have not been constructed yet
}, nil
}
16 changes: 15 additions & 1 deletion class/constant_double.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package class

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
"math"
)

type ConstantDouble struct {
Expand All @@ -14,6 +17,17 @@ func (c *ConstantDouble) ToBytes() []byte {
return bs
}

func readConstantDouble(stream *commons.Stream) (*ConstantDouble, error) {
_, _ = stream.ReadN(1)
bs, err := stream.ReadN(8)
if err != nil {
return nil, fmt.Errorf("read constant double failed, no enough data in the stream")
}

var i = binary.BigEndian.Uint64(bs)
var c = &ConstantDouble{
Double: math.Float64frombits(i),
}


return c, nil
}
5 changes: 5 additions & 0 deletions class/constant_fieldref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package class

type ConstantFieldRef struct {

}
32 changes: 32 additions & 0 deletions class/constant_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package class

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
)

type ConstantString struct {
StringOffset uint16
Reference *ConstantUTF8 // Reference must be a ConstantUTF8 object
}

func (c *ConstantString) ToBytes() []byte {
var bs = []byte{CONSTANT_STRING_INGFO}
bs = append(bs, commons.NumberToBytes(c.StringOffset)...)
return bs
}

func readConstantString(stream *commons.Stream) (*ConstantString, error) {
_, _ = stream.ReadN(1)
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read constant string failed, no enough data in the stream")
}

var i = binary.BigEndian.Uint16(bs)
return &ConstantString{
StringOffset: i,
// lack of Reference because ConstantPool have not been constructed yet
}, nil
}

0 comments on commit 9312b0a

Please sign in to comment.