Skip to content

Commit

Permalink
finished attr_module
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Apr 8, 2024
1 parent 9120100 commit 06da533
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions class/attr_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/phith0n/zkar/commons"
)

// AttrModule https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.25
type AttrModule struct {
*AttributeBase

Expand All @@ -29,6 +30,41 @@ func (a *AttrModule) readInfo(stream *commons.Stream) error {
a.ModuleName = binary.BigEndian.Uint16(bs[:2])
a.ModuleFlags = binary.BigEndian.Uint16(bs[2:4])
a.ModuleVersionIndex = binary.BigEndian.Uint16(bs[4:])

a.Requires, err = a.readRequires(stream)
if err != nil {
return err
}

a.Exports, err = a.readExports(stream)
if err != nil {
return err
}

a.Opens, err = a.readOpens(stream)
if err != nil {
return err
}

bs, err = stream.ReadN(2)
if err != nil {
return fmt.Errorf("read AttrModule UsesIndex failed, no enough data in the stream")
}

for i := uint16(0); i < binary.BigEndian.Uint16(bs); i++ {
data, err := stream.ReadN(2)
if err != nil {
return fmt.Errorf("read AttrModule UsesIndex[%d] failed, no enough data in the stream", i)
}

a.UsesIndex = append(a.UsesIndex, binary.BigEndian.Uint16(data))
}

a.Provides, err = a.readProvides(stream)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -105,7 +141,70 @@ type ModuleOpens struct {
OpensToIndex []uint16
}

func (a *AttrModule) readOpens(stream *commons.Stream) ([]*ModuleOpens, error) {
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read AttrModule Opens failed, no enough data in the stream")
}

var opens []*ModuleOpens
length := binary.BigEndian.Uint16(bs)
for i := uint16(0); i < length; i++ {
bs, err = stream.ReadN(6)
if err != nil {
return nil, fmt.Errorf("read AttrModule Opens[%d] failed, no enough data in the stream", i)
}

open := &ModuleOpens{
OpensIndex: binary.BigEndian.Uint16(bs[:2]),
OpensFlags: binary.BigEndian.Uint16(bs[2:4]),
}
for j := uint16(0); j < binary.BigEndian.Uint16(bs[4:]); j++ {
data, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read AttrModule Opens[%d] OpensToIndex[%d] failed, no enough data in the stream", i, j)
}

open.OpensToIndex = append(open.OpensToIndex, binary.BigEndian.Uint16(data))
}
opens = append(opens, open)
}

return opens, nil
}

type ModuleProvides struct {
ProvidesIndex uint16
ProvidesWithIndex []uint16
}

func (a *AttrModule) readProvides(stream *commons.Stream) ([]*ModuleProvides, error) {
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read AttrModule Provides failed, no enough data in the stream")
}

var provides []*ModuleProvides
length := binary.BigEndian.Uint16(bs)
for i := uint16(0); i < length; i++ {
bs, err = stream.ReadN(4)
if err != nil {
return nil, fmt.Errorf("read AttrModule Provides[%d] failed, no enough data in the stream", i)
}

provide := &ModuleProvides{
ProvidesIndex: binary.BigEndian.Uint16(bs[:2]),
}

for j := uint16(0); j < binary.BigEndian.Uint16(bs[2:]); j++ {
data, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read AttrModule Opens[%d] OpensToIndex[%d] failed, no enough data in the stream", i, j)
}

provide.ProvidesWithIndex = append(provide.ProvidesWithIndex, binary.BigEndian.Uint16(data))
}
provides = append(provides, provide)
}
return provides, nil
}

0 comments on commit 06da533

Please sign in to comment.