Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added padding function #75

Merged
merged 4 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func main() {
| xor(sequences ...strings/bytes) | Perform xor on sequences of same length | `xor('abc','def')` | `50705` in hex |
| zlib(input string) string | Compresses the input using Zlib | `base64(zlib("Hello"))` | `eJzySM3JyQcEAAD//wWMAfU=` |
| zlib_decode(input string) string | Decompresses the input using Zlib | `zlib_decode(hex_decode("789cf248cdc9c907040000ffff058c01f5"))` | `Hello` |
| padding(input string, padding string,length int) string | Adds padding char to input string until it reaches desired length | `padding("A","b",3)` | `Abb` |

**Supported encodings:**

Expand Down
56 changes: 56 additions & 0 deletions dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/projectdiscovery/mapcidr"
jarm "github.com/projectdiscovery/utils/crypto/jarm"
errors "github.com/projectdiscovery/utils/errors"
errorutil "github.com/projectdiscovery/utils/errors"
maputils "github.com/projectdiscovery/utils/maps"
randint "github.com/projectdiscovery/utils/rand"
stringsutil "github.com/projectdiscovery/utils/strings"
Expand Down Expand Up @@ -818,6 +819,61 @@ func init() {
result := constraint.Check(firstParsed)
return result, nil
}))
MustAddFunction(NewWithPositionalArgs("padding", 3, false, func(args ...interface{}) (interface{}, error) {
// padding('Test String','A',50) // will pad "Test String" up to 50 characters with "A" as padding byte.
bLen := 0
switch value := args[2].(type) {
case float64:
bLen = int(value)
case int:
bLen = value
default:
strLen := toString(args[2])
floatVal, err := strconv.ParseFloat(strLen, 64)
if err != nil {
return nil, err
}
bLen = int(floatVal)
}
if bLen == 0 {
return nil, errorutil.New("invalid padding length")
}
bByte := []byte(toString(args[1]))
if len(bByte) == 0 {
return nil, errorutil.New("invalid padding byte")
}
bData := []byte(toString(args[0]))
dataLen := len(bData)
if dataLen >= bLen {
return toString(bData), nil // Note: if given string is longer than the desired length, it will not be truncated
}
if dataLen == 0 {
// If the initial string is empty, simply create a padded array with the specified length
paddedData := make([]byte, bLen)
for i := 0; i < bLen; i++ {
paddedData[i] = bByte[i%len(bByte)]
}
return toString(paddedData), nil
}

// Calculate the number of bytes needed for padding
paddingLen := (bLen - (dataLen % bLen)) % bLen

// Create a new byte array with the desired length
paddedData := make([]byte, dataLen+paddingLen)

// Copy the original data into the padded array
copy(paddedData, bData)

// Add padding bytes with the specified padding byte
for i := dataLen; i < len(paddedData); i++ {
paddedData[i] = bByte[i%len(bByte)]
}

return toString(paddedData), nil

}))

MustAddFunction(NewWithSingleSignature("print_debug",
"(args ...interface{})",
false,
Expand Down
2 changes: 2 additions & 0 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
md5(arg1 interface{}) interface{}
mmh3(arg1 interface{}) interface{}
oct_to_dec(arg1 interface{}) interface{}
padding(arg1, arg2, arg3 interface{}) interface{}
print_debug(args ...interface{})
public_ip() string
rand_base(length uint, optionalCharSet string) string
Expand Down Expand Up @@ -252,6 +253,7 @@ func TestDslExpressions(t *testing.T) {
"line_ends_with('Hii\nHello', 'ii')": true, // back quotes do not support escape sequences
`regex("H([a-z]+)o", "Hello")`: true,
`wait_for(1)`: nil,
`padding("A","b",3)`: "Abb",
`print_debug(1+2, "Hello")`: nil,
`to_number('4')`: float64(4),
`to_string(4)`: "4",
Expand Down
Loading