Skip to content

Commit

Permalink
make printer struct internal
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Aug 16, 2021
1 parent e35b3a1 commit bf1b640
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 202 deletions.
32 changes: 4 additions & 28 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
package main

import (
"encoding/hex"
"fmt"
"github.com/phith0n/zkar"
"github.com/sanity-io/litter"
"io/ioutil"
"regexp"
)

func main() {
var filename = "testcases/ysoserial/URLDNS.ser"
// var filename = "example/object.poc"
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(err.Error())
return
}

ser, err := zkar.FromBytes(data)
if err != nil {
fmt.Println(err.Error())
return
}

sq := litter.Options{
StripPackageNames: false,
HidePrivateFields: true,
FieldExclusions: regexp.MustCompile(`^(XXX_.*)$`), // XXX_ is a prefix of fields generated by protoc-gen-go
Separator: " ",
HideZeroValues: true,
}
sq.Dump(ser)
// ioutil.WriteFile("testcases/ysoserial/C3P01.ser", ser.ToBytes(), fs.FileMode(755))
// fmt.Println(ser.ToString())
var s = "\x8c\xA3\x8B"
var bs = []byte(s)
fmt.Println(bs, s, hex.EncodeToString(bs))
}
12 changes: 6 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func FromBytes(data []byte) (*Serialization, error) {
}

func (ois *Serialization) ToString() string {
var b = NewPrinter()
b.Printf("@Magic - %s", Hexify(ois.MagicNumber))
b.Printf("@Version - %s", Hexify(ois.StreamVersion))
b.Printf("@Contents")
b.IncreaseIndent()
var b = newPrinter()
b.printf("@Magic - %s", Hexify(ois.MagicNumber))
b.printf("@Version - %s", Hexify(ois.StreamVersion))
b.printf("@Contents")
b.increaseIndent()
for _, content := range ois.Contents {
b.Print(content.ToString())
b.print(content.ToString())
}
return b.String()
}
Expand Down
34 changes: 15 additions & 19 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,40 @@ import (
"strings"
)

type Printer struct {
type printer struct {
strings.Builder
currentIndent int
}

func (p *Printer) Printf(msg string, args ...interface{}) {
func (p *printer) printf(msg string, args ...interface{}) {
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
p.indent(msg)
p.print(msg)
}

func (p *Printer) Print(data string) {
p.indent(data)
func (p *printer) print(data string) {
var indent = strings.Repeat(" ", p.currentIndent)
var blocks = strings.Split(data, "\n")
for _, block := range blocks {
if block != "" {
_, _ = p.WriteString(indent + block + "\n")
}
}
}

func (p *Printer) IncreaseIndent() {
func (p *printer) increaseIndent() {
p.currentIndent += 2
}

func (p *Printer) DecreaseIndent() {
func (p *printer) decreaseIndent() {
if p.currentIndent >= 2 {
p.currentIndent -= 2
}
}

func (p *Printer) indent(data string) {
var indent = strings.Repeat(" ", p.currentIndent)
var blocks = strings.Split(data, "\n")
for _, block := range blocks {
if block != "" {
_, _ = p.WriteString(indent + block + "\n")
}
}
}

func NewPrinter() *Printer {
return &Printer{
func newPrinter() *printer {
return &printer{
currentIndent: 0,
}
}
24 changes: 12 additions & 12 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
)

func TestPrinterIndent(t *testing.T) {
var b = NewPrinter()
b.IncreaseIndent()
b.Printf("a: 1\nb: 2")
var b = newPrinter()
b.increaseIndent()
b.printf("a: 1\nb: 2")
require.Equal(t, " a: 1\n b: 2\n", b.String())
}

func TestPrinterNoNewline(t *testing.T) {
var b = NewPrinter()
b.Printf("no newline")
var b = newPrinter()
b.printf("no newline")
require.Equal(t, "no newline\n", b.String())
}

func TestPrinterNewline(t *testing.T) {
var b = NewPrinter()
b.Printf("\n")
var b = newPrinter()
b.printf("\n")
require.Equal(t, "", b.String())
}

func TestPrinterNewlines(t *testing.T) {
var b = NewPrinter()
b.Printf("\n\n\n\n\n")
var b = newPrinter()
b.printf("\n\n\n\n\n")
require.Equal(t, "", b.String())
}

func TestPrinterIndentLines(t *testing.T) {
var b = NewPrinter()
b.IncreaseIndent()
b.Printf("\n\n\n")
var b = newPrinter()
b.increaseIndent()
b.printf("\n\n\n")
require.Equal(t, "", b.String())
}
24 changes: 12 additions & 12 deletions tc_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func (t *TCArray) ToBytes() []byte {
}

func (t *TCArray) ToString() string {
var b = NewPrinter()
b.Printf("TC_ARRAY - %s", Hexify(JAVA_TC_ARRAY))
b.IncreaseIndent()
b.Print(t.ClassPointer.ToString())
b.Printf("@Handler - %v", t.Handler)
b.Printf("@ArraySize - %d - %s", len(t.ArrayData), Hexify(uint32(len(t.ArrayData))))
b.Printf("[]Values")
b.IncreaseIndent()
var b = newPrinter()
b.printf("TC_ARRAY - %s", Hexify(JAVA_TC_ARRAY))
b.increaseIndent()
b.print(t.ClassPointer.ToString())
b.printf("@Handler - %v", t.Handler)
b.printf("@ArraySize - %d - %s", len(t.ArrayData), Hexify(uint32(len(t.ArrayData))))
b.printf("[]Values")
b.increaseIndent()

// check if Array is a bytes, then hexdump the byte array
var className = ""
Expand All @@ -49,10 +49,10 @@ func (t *TCArray) ToString() string {
}

for index, data := range t.ArrayData {
b.Printf("Index %d", index)
b.IncreaseIndent()
b.Print(data.ToString())
b.DecreaseIndent()
b.printf("Index %d", index)
b.increaseIndent()
b.print(data.ToString())
b.decreaseIndent()
}

return b.String()
Expand Down
10 changes: 5 additions & 5 deletions tc_blockdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (bd *TCBlockData) ToBytes() []byte {
}

func (bd *TCBlockData) ToString() string {
var b = NewPrinter()
var b = newPrinter()
if len(bd.Data) > 0xFF {
b.Printf("TC_BLOCKDATALONG - %s", Hexify(JAVA_TC_BLOCKDATALONG))
b.printf("TC_BLOCKDATALONG - %s", Hexify(JAVA_TC_BLOCKDATALONG))
} else {
b.Printf("TC_BLOCKDATA - %s", Hexify(JAVA_TC_BLOCKDATA))
b.printf("TC_BLOCKDATA - %s", Hexify(JAVA_TC_BLOCKDATA))
}
b.IncreaseIndent()
b.Printf("@Blockdata - %s", Hexify(bd.Data))
b.increaseIndent()
b.printf("@Blockdata - %s", Hexify(bd.Data))
return b.String()
}

Expand Down
10 changes: 5 additions & 5 deletions tc_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func (c *TCClass) ToBytes() []byte {
}

func (c *TCClass) ToString() string {
var b = NewPrinter()
b.Printf("TC_CLASS - %s", Hexify(JAVA_TC_CLASS))
b.IncreaseIndent()
b.Print(c.ClassPointer.ToString())
b.Printf("@Handler - %v", c.Handler)
var b = newPrinter()
b.printf("TC_CLASS - %s", Hexify(JAVA_TC_CLASS))
b.increaseIndent()
b.print(c.ClassPointer.ToString())
b.printf("@Handler - %v", c.Handler)
return b.String()
}

Expand Down
26 changes: 13 additions & 13 deletions tc_classdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ func (cd *TCClassData) ToBytes() []byte {
}

func (cd *TCClassData) ToString() string {
var b = NewPrinter()
b.Printf("@ClassName - %s", cd.ReferenceClass.ClassName)
b.IncreaseIndent()
b.Print("{}Attributes")
b.IncreaseIndent()
var b = newPrinter()
b.printf("@ClassName - %s", cd.ReferenceClass.ClassName)
b.increaseIndent()
b.print("{}Attributes")
b.increaseIndent()
for i := 0; i < len(cd.FieldDatas); i++ {
b.Printf("%s", cd.ReferenceClass.Attributes[i])
b.IncreaseIndent()
b.Print(cd.FieldDatas[i].ToString())
b.DecreaseIndent()
b.printf("%s", cd.ReferenceClass.Attributes[i])
b.increaseIndent()
b.print(cd.FieldDatas[i].ToString())
b.decreaseIndent()
}
b.DecreaseIndent()
b.decreaseIndent()

if !cd.HasAnnotation {
return b.String()
}

b.Print("@ObjectAnnotation")
b.IncreaseIndent()
b.print("@ObjectAnnotation")
b.increaseIndent()
for _, content := range cd.ObjectAnnotation {
b.Print(content.ToString())
b.print(content.ToString())
}

return b.String()
Expand Down
12 changes: 6 additions & 6 deletions tc_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func (e *TCEnum) ToBytes() []byte {
}

func (e *TCEnum) ToString() string {
var b = NewPrinter()
b.Printf("TC_ENUM - %s", Hexify(JAVA_TC_ENUM))
b.IncreaseIndent()
b.Print(e.ClassPointer.ToString())
b.Printf("@Handler - %v", e.Handler)
b.Print(e.ConstantName.ToString())
var b = newPrinter()
b.printf("TC_ENUM - %s", Hexify(JAVA_TC_ENUM))
b.increaseIndent()
b.print(e.ClassPointer.ToString())
b.printf("@Handler - %v", e.Handler)
b.print(e.ConstantName.ToString())
return b.String()
}

Expand Down
18 changes: 9 additions & 9 deletions tc_fielddesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func (f *TCFieldDesc) ToBytes() []byte {
}

func (f *TCFieldDesc) ToString() string {
var b = NewPrinter()
b.Printf("%s - %s - %s", typecodeVerbose[f.TypeCode], f.TypeCode, Hexify(f.TypeCode))
b.Print("@FieldName")
b.IncreaseIndent()
b.Print(f.FieldName.ToString())
b.DecreaseIndent()
var b = newPrinter()
b.printf("%s - %s - %s", typecodeVerbose[f.TypeCode], f.TypeCode, Hexify(f.TypeCode))
b.print("@FieldName")
b.increaseIndent()
b.print(f.FieldName.ToString())
b.decreaseIndent()
if f.TypeCode == "L" || f.TypeCode == "[" {
b.Print("@ClassName")
b.IncreaseIndent()
b.Print(f.ClassName.ToString())
b.print("@ClassName")
b.increaseIndent()
b.print(f.ClassName.ToString())
}

return b.String()
Expand Down
Loading

0 comments on commit bf1b640

Please sign in to comment.