Skip to content

Commit

Permalink
Add string concaternate operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
LMRupp committed Apr 19, 2024
1 parent 7814f76 commit beb2dbf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/de/usd/cstchef/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import de.usd.cstchef.operations.string.Suffix;
import de.usd.cstchef.operations.string.Uppercase;
import de.usd.cstchef.operations.string.Lowercase;
import de.usd.cstchef.operations.string.Concaternate;
import de.usd.cstchef.operations.utils.GetVariable;
import de.usd.cstchef.operations.utils.NoOperation;
import de.usd.cstchef.operations.utils.RandomNumber;
Expand Down Expand Up @@ -228,7 +229,7 @@ public static Class<? extends Operation>[] getOperationsDev() {
Suffix.class, Sum.class, StringContains.class, StringMatch.class, Tiger.class,
ToBase64.class, ToHex.class, UnixTimestamp.class, UrlDecode.class, UrlEncode.class,
Whirlpool.class, WriteFile.class, XmlFullSignature.class, XmlMultiSignature.class,
Xor.class, SoapMultiSignature.class
Xor.class, SoapMultiSignature.class, Concaternate.class
};
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/de/usd/cstchef/operations/string/Concaternate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.usd.cstchef.operations.string;

import de.usd.cstchef.VariableStore;
import de.usd.cstchef.operations.Operation;
import de.usd.cstchef.operations.OperationCategory;
import de.usd.cstchef.operations.Operation.OperationInfos;
import de.usd.cstchef.view.ui.VariableTextArea;


@OperationInfos(name = "Concaternate", category = OperationCategory.STRING, description = "Concaternate multiple string values or variables. Separate multiple strings or variables with ';'.")
public class Concaternate extends Operation {

private VariableTextArea text;

@Override
protected byte[] perform(byte[] input) throws Exception {

String[] components = text.getText().split(";");
String trimed;
byte[][] value = new byte[components.length][];
for (int i = 0; i < components.length; i++) {
trimed = components[i].trim();

if (trimed.startsWith("$")) {
value[i] = VariableStore.getInstance().getVariable(trimed);
} else {
value[i] = trimed.getBytes();
}
}

return flatten_array(value);
}

public void createUI() {
this.text = new VariableTextArea();
this.addUIElement("Values to concaternate", this.text);
}

private byte[] flatten_array(byte[][] arrays){

// Calculate the total length of the concatenated array
int totalLength = 0;
for (byte[] array : arrays) {
totalLength += array.length;
}

// Create the concatenated array
byte[] result = new byte[totalLength];
int currentIndex = 0;
for (byte[] array : arrays) {
System.arraycopy(array, 0, result, currentIndex, array.length);
currentIndex += array.length;
}

return result;
}

}

0 comments on commit beb2dbf

Please sign in to comment.