intLiteral helper now accepts int (#76)

pull/77/head
pagran 4 years ago committed by GitHub
parent 4b73c37ed7
commit 14a19b3e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,10 +9,11 @@ import (
func ident(name string) *ast.Ident { func ident(name string) *ast.Ident {
return &ast.Ident{Name: name} return &ast.Ident{Name: name}
} }
func intLiteral(value string) *ast.BasicLit {
func intLiteral(value int) *ast.BasicLit {
return &ast.BasicLit{ return &ast.BasicLit{
Kind: token.INT, Kind: token.INT,
Value: value, Value: strconv.Itoa(value),
} }
} }
@ -62,10 +63,9 @@ func returnStmt(results ...ast.Expr) *ast.ReturnStmt {
// _ = data[pos] // _ = data[pos]
func boundsCheckData(pos int) *ast.AssignStmt { func boundsCheckData(pos int) *ast.AssignStmt {
posStr := strconv.Itoa(pos)
return &ast.AssignStmt{ return &ast.AssignStmt{
Lhs: []ast.Expr{ident("_")}, Lhs: []ast.Expr{ident("_")},
Tok: token.ASSIGN, Tok: token.ASSIGN,
Rhs: []ast.Expr{indexExpr("data", intLiteral(posStr))}, Rhs: []ast.Expr{indexExpr("data", intLiteral(pos))},
} }
} }

@ -200,7 +200,7 @@ func obfuscateByteArray(data []byte, length int64) *ast.CallExpr {
block := obfuscator.obfuscate(data) block := obfuscator.obfuscate(data)
arrayType := &ast.ArrayType{ arrayType := &ast.ArrayType{
Len: intLiteral(strconv.Itoa(int(length))), Len: intLiteral(int(length)),
Elt: ident("byte"), Elt: ident("byte"),
} }
@ -245,7 +245,7 @@ func obfuscateBool(data bool) *ast.BinaryExpr {
return &ast.BinaryExpr{ return &ast.BinaryExpr{
X: genObfuscateInt(dataUint64, intType), X: genObfuscateInt(dataUint64, intType),
Op: token.EQL, Op: token.EQL,
Y: intLiteral("1"), Y: intLiteral(1),
} }
} }

@ -122,19 +122,17 @@ func bytesToUint(bits int) ast.Expr {
var expr ast.Expr var expr ast.Expr
for i := 0; i < bytes; i++ { for i := 0; i < bytes; i++ {
posStr := strconv.Itoa(i)
if i == 0 { if i == 0 {
expr = callExpr(ident("uint"+bitsStr), indexExpr("data", intLiteral(posStr))) expr = callExpr(ident("uint"+bitsStr), indexExpr("data", intLiteral(i)))
continue continue
} }
shiftValue := strconv.Itoa(i * 8) shiftValue := i * 8
expr = &ast.BinaryExpr{ expr = &ast.BinaryExpr{
X: expr, X: expr,
Op: token.OR, Op: token.OR,
Y: &ast.BinaryExpr{ Y: &ast.BinaryExpr{
X: callExpr(ident("uint"+bitsStr), indexExpr("data", intLiteral(posStr))), X: callExpr(ident("uint"+bitsStr), indexExpr("data", intLiteral(i))),
Op: token.SHL, Op: token.SHL,
Y: intLiteral(shiftValue), Y: intLiteral(shiftValue),
}, },

@ -4,7 +4,6 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"math" "math"
"strconv"
) )
type swap struct{} type swap struct{}
@ -34,7 +33,7 @@ func positionsToSlice(data []int) *ast.CompositeLit {
Elts: []ast.Expr{}, Elts: []ast.Expr{},
} }
for _, data := range data { for _, data := range data {
arr.Elts = append(arr.Elts, intLiteral(strconv.Itoa(data))) arr.Elts = append(arr.Elts, intLiteral(data))
} }
return arr return arr
} }
@ -78,17 +77,17 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
Init: &ast.AssignStmt{ Init: &ast.AssignStmt{
Lhs: []ast.Expr{ident("i")}, Lhs: []ast.Expr{ident("i")},
Tok: token.DEFINE, Tok: token.DEFINE,
Rhs: []ast.Expr{intLiteral("0")}, Rhs: []ast.Expr{intLiteral(0)},
}, },
Cond: &ast.BinaryExpr{ Cond: &ast.BinaryExpr{
X: ident("i"), X: ident("i"),
Op: token.LSS, Op: token.LSS,
Y: intLiteral(strconv.Itoa(len(positions))), Y: intLiteral(len(positions)),
}, },
Post: &ast.AssignStmt{ Post: &ast.AssignStmt{
Lhs: []ast.Expr{ident("i")}, Lhs: []ast.Expr{ident("i")},
Tok: token.ADD_ASSIGN, Tok: token.ADD_ASSIGN,
Rhs: []ast.Expr{intLiteral("2")}, Rhs: []ast.Expr{intLiteral(2)},
}, },
Body: &ast.BlockStmt{ Body: &ast.BlockStmt{
List: []ast.Stmt{ List: []ast.Stmt{
@ -106,12 +105,12 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
Y: indexExpr("positions", &ast.BinaryExpr{ Y: indexExpr("positions", &ast.BinaryExpr{
X: ident("i"), X: ident("i"),
Op: token.ADD, Op: token.ADD,
Y: intLiteral("1"), Y: intLiteral(1),
}), }),
}), }),
}, },
Op: token.ADD, Op: token.ADD,
Y: intLiteral(strconv.Itoa(int(shiftKey))), Y: intLiteral(int(shiftKey)),
}, },
}, },
}, },
@ -121,7 +120,7 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ indexExpr("data", indexExpr("positions", &ast.BinaryExpr{
X: ident("i"), X: ident("i"),
Op: token.ADD, Op: token.ADD,
Y: intLiteral("1"), Y: intLiteral(1),
})), })),
}, },
Tok: token.ASSIGN, Tok: token.ASSIGN,
@ -130,7 +129,7 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
X: indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ X: indexExpr("data", indexExpr("positions", &ast.BinaryExpr{
X: ident("i"), X: ident("i"),
Op: token.ADD, Op: token.ADD,
Y: intLiteral("1"), Y: intLiteral(1),
})), })),
Op: token.XOR, Op: token.XOR,
Y: ident("localKey"), Y: ident("localKey"),

Loading…
Cancel
Save