Skip to content

Commit 823aa1c

Browse files
support different-length inputs in xor() (#300)
* fix typo in parameter name and add typos config * add tests for xor() with different-length inputs
1 parent da351f6 commit 823aa1c

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

_typos.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[default.extend-words]
2+
# intentional test data
3+
comparitive = "comparitive"
4+
examplify = "examplify"
5+
6+
[default.extend-identifiers]
7+
TrUe = "TrUe"
8+
9+
[files]
10+
extend-exclude = ["go.sum"]

dsl.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,8 @@ func init() {
14131413
return nil, errors.New("at least two arguments needed")
14141414
}
14151415

1416-
n := -1
1416+
inputs := make([][]byte, 0, len(args))
1417+
maxLen := 0
14171418
for _, arg := range args {
14181419
var b []byte
14191420
switch v := arg.(type) {
@@ -1424,21 +1425,19 @@ func init() {
14241425
default:
14251426
return nil, fmt.Errorf("invalid argument type %T", arg)
14261427
}
1427-
if n == -1 {
1428-
n = len(b)
1429-
} else if len(b) != n {
1430-
return nil, errors.New("all arguments must have the same length")
1428+
if len(b) == 0 {
1429+
return nil, errors.New("empty arguments are not allowed")
1430+
}
1431+
inputs = append(inputs, b)
1432+
if len(b) > maxLen {
1433+
maxLen = len(b)
14311434
}
14321435
}
14331436

1434-
result := make([]byte, n)
1435-
for i := 0; i < n; i++ {
1436-
for _, arg := range args {
1437-
b, ok := arg.([]byte)
1438-
if !ok {
1439-
b = []byte(arg.(string))
1440-
}
1441-
result[i] ^= b[i]
1437+
result := make([]byte, maxLen)
1438+
for i := 0; i < maxLen; i++ {
1439+
for _, b := range inputs {
1440+
result[i] ^= b[i%len(b)]
14421441
}
14431442
}
14441443

@@ -1739,8 +1738,8 @@ func AddMultiSignatureHelperFunction(key string, signatureparts []string, cachea
17391738
return AddFunction(function)
17401739
}
17411740

1742-
func GetFunctionNames(heperFunctions map[string]govaluate.ExpressionFunction) []string {
1743-
return maputils.GetKeys(heperFunctions)
1741+
func GetFunctionNames(helperFunctions map[string]govaluate.ExpressionFunction) []string {
1742+
return maputils.GetKeys(helperFunctions)
17441743
}
17451744

17461745
// GetPrintableDslFunctionSignatures returns the function signatures for the

dsl_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ func TestDslExpressions(t *testing.T) {
462462
`ip_format('127.0.1.0', '11')`: "127.0.256",
463463
"unpack('>I', '\xac\xd7\t\xd0')": -272646673,
464464
"xor('\x01\x02', '\x02\x01')": []uint8([]byte{0x3, 0x3}),
465+
"xor('\x01\x02\x03\x04', '\x02\x01')": []uint8([]byte{0x3, 0x3, 0x1, 0x5}),
466+
"xor('\x05', '\x01\x02\x03')": []uint8([]byte{0x4, 0x7, 0x6}),
465467
`count("projectdiscovery", "e")`: 2,
466468
`concat(to_title("pRoJeCt"), to_title("diScOvErY"))`: "ProjectDiscovery",
467469
`concat(to_title("welcome "), "to", to_title(" watch"), to_title("mojo"))`: "Welcome to WatchMojo",

0 commit comments

Comments
 (0)