diff --git a/src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java b/src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java index 1e7349bc..d9334ecb 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java +++ b/src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java @@ -7,32 +7,12 @@ */ package de.rub.nds.modifiablevariable; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerAddModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerExplicitValueModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerInteractiveModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerMultiplyModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftLeftModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftRightModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerSubtractModification; -import de.rub.nds.modifiablevariable.biginteger.BigIntegerXorModification; +import de.rub.nds.modifiablevariable.biginteger.*; import de.rub.nds.modifiablevariable.bool.BooleanExplicitValueModification; import de.rub.nds.modifiablevariable.bool.BooleanToggleModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayDeleteModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayDuplicateModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayExplicitValueModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayInsertModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayShuffleModification; -import de.rub.nds.modifiablevariable.bytearray.ByteArrayXorModification; -import de.rub.nds.modifiablevariable.integer.IntegerAddModification; -import de.rub.nds.modifiablevariable.integer.IntegerExplicitValueModification; -import de.rub.nds.modifiablevariable.integer.IntegerShiftLeftModification; -import de.rub.nds.modifiablevariable.integer.IntegerShiftRightModification; -import de.rub.nds.modifiablevariable.integer.IntegerSubtractModification; -import de.rub.nds.modifiablevariable.integer.IntegerXorModification; -import de.rub.nds.modifiablevariable.longint.LongAddModification; -import de.rub.nds.modifiablevariable.longint.LongExplicitValueModification; -import de.rub.nds.modifiablevariable.longint.LongSubtractModification; -import de.rub.nds.modifiablevariable.longint.LongXorModification; +import de.rub.nds.modifiablevariable.bytearray.*; +import de.rub.nds.modifiablevariable.integer.*; +import de.rub.nds.modifiablevariable.longint.*; import de.rub.nds.modifiablevariable.singlebyte.ByteAddModification; import de.rub.nds.modifiablevariable.singlebyte.ByteExplicitValueModification; import de.rub.nds.modifiablevariable.singlebyte.ByteSubtractModification; @@ -88,6 +68,15 @@ public abstract class ModifiableVariable implements Serializable { @XmlElement( type = BigIntegerMultiplyModification.class, name = "BigIntegerMultiplyModification"), + @XmlElement( + type = BigIntegerAppendValueModification.class, + name = "BigIntegerAppendValueModification"), + @XmlElement( + type = BigIntegerInsertValueModification.class, + name = "BigIntegerInsertValueModification"), + @XmlElement( + type = BigIntegerPrependValueModification.class, + name = "BigIntegerPrependValueModification"), @XmlElement( type = BooleanToggleModification.class, name = "BooleanToggleModification"), @@ -101,8 +90,14 @@ public abstract class ModifiableVariable implements Serializable { type = ByteArrayShuffleModification.class, name = "ByteArrayShuffleModification"), @XmlElement( - type = ByteArrayInsertModification.class, - name = "ByteArrayInsertModification"), + type = ByteArrayAppendValueModification.class, + name = "ByteArrayAppendValueModification"), + @XmlElement( + type = ByteArrayInsertValueModification.class, + name = "ByteArrayInsertValueModification"), + @XmlElement( + type = ByteArrayPrependValueModification.class, + name = "ByteArrayPrependValueModification"), @XmlElement( type = ByteArrayExplicitValueModification.class, name = "ByteArrayExplicitValueModification"), @@ -116,6 +111,9 @@ public abstract class ModifiableVariable implements Serializable { @XmlElement( type = IntegerSubtractModification.class, name = "IntegerSubtractModification"), + @XmlElement( + type = IntegerMultiplyModification.class, + name = "IntegerMultiplyModification"), @XmlElement( type = IntegerShiftRightModification.class, name = "IntegerShiftRightModification"), @@ -126,6 +124,15 @@ public abstract class ModifiableVariable implements Serializable { type = IntegerExplicitValueModification.class, name = "IntegerExplicitValueModification"), @XmlElement(type = IntegerAddModification.class, name = "IntegerAddModification"), + @XmlElement( + type = IntegerAppendValueModification.class, + name = "IntegerAppendValueModification"), + @XmlElement( + type = IntegerInsertValueModification.class, + name = "IntegerInsertValueModification"), + @XmlElement( + type = IntegerPrependValueModification.class, + name = "IntegerPrependValueModification"), @XmlElement(type = LongXorModification.class, name = "LongXorModification"), @XmlElement( type = LongSubtractModification.class, @@ -133,6 +140,24 @@ public abstract class ModifiableVariable implements Serializable { @XmlElement( type = LongExplicitValueModification.class, name = "LongExplicitValueModification"), + @XmlElement( + type = LongAppendValueModification.class, + name = "LongAppendValueModification"), + @XmlElement( + type = LongInsertValueModification.class, + name = "LongInsertValueModification"), + @XmlElement( + type = LongPrependValueModification.class, + name = "LongPrependValueModification"), + @XmlElement( + type = LongMultiplyModification.class, + name = "LongMultiplyModification"), + @XmlElement( + type = LongShiftLeftModification.class, + name = "LongShiftLeftModification"), + @XmlElement( + type = LongShiftRightModification.class, + name = "LongShiftRightModification"), @XmlElement(type = LongAddModification.class, name = "LongAddModification"), @XmlElement(type = ByteXorModification.class, name = "ByteXorModification"), @XmlElement( diff --git a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerAppendValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerAppendValueModification.java new file mode 100644 index 00000000..5b1c385c --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerAppendValueModification.java @@ -0,0 +1,76 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.biginteger; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.math.BigInteger; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"appendValue", "modificationFilter"}) +@XmlAccessorType(XmlAccessType.FIELD) +public class BigIntegerAppendValueModification extends VariableModification { + + private static final int MAX_APPEND_LENGTH = 8; + + private BigInteger appendValue; + + public BigIntegerAppendValueModification() {} + + public BigIntegerAppendValueModification(BigInteger bi) { + this.appendValue = bi; + } + + @Override + protected BigInteger modifyImplementationHook(BigInteger input) { + if (input == null) { + input = BigInteger.ZERO; + } + return input.shiftLeft(appendValue.bitLength()).add(appendValue); + } + + public BigInteger getAppendValue() { + return appendValue; + } + + public void setAppendValue(BigInteger appendValue) { + this.appendValue = appendValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new BigIntegerAppendValueModification(appendValue.add(new BigInteger(MAX_APPEND_LENGTH, new Random()))); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 59 * hash + Objects.hashCode(this.appendValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final BigIntegerAppendValueModification other = (BigIntegerAppendValueModification) obj; + return Objects.equals(this.appendValue, other.appendValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerInsertValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerInsertValueModification.java new file mode 100644 index 00000000..9fa713b7 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerInsertValueModification.java @@ -0,0 +1,120 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.biginteger; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.math.BigInteger; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"insertValue", "startPosition", "modificationFilter"}) +@XmlAccessorType(XmlAccessType.FIELD) +public class BigIntegerInsertValueModification extends VariableModification { + + private static final int MAX_INSERT_LENGTH = 8; + private static final int MAX_POSITION_MODIFIER = 32; + + private BigInteger insertValue; + private int startPosition; + + public BigIntegerInsertValueModification() {} + + public BigIntegerInsertValueModification(BigInteger bi, int startPosition) { + this.insertValue = bi; + this.startPosition = startPosition; + } + + @Override + protected BigInteger modifyImplementationHook(BigInteger input) { + if (input == null) { + input = BigInteger.ZERO; + } + int originalValueLength = input.bitLength(); + int insertValueLength = insertValue.bitLength(); + int insertPosition = startPosition; + if (startPosition > originalValueLength) { + insertPosition = originalValueLength; + } else if (startPosition < 0) { + insertPosition = 0; + } + BigInteger mask = BigInteger.valueOf((1L << insertPosition) - 1); + + return input.shiftRight(insertPosition) + .shiftLeft(insertValueLength) + .and(insertValue) + .shiftLeft(insertPosition) + .add(mask.and(input)); + } + + public BigInteger getInsertValue() { + return insertValue; + } + + public void setInsertValue(BigInteger insertValue) { + this.insertValue = insertValue; + } + + public int getStartPosition() { + return startPosition; + } + + public void setStartPosition(int startPosition) { + this.startPosition = startPosition; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + if (r.nextBoolean()) { + return new BigIntegerInsertValueModification( + insertValue.add(new BigInteger(MAX_INSERT_LENGTH, r)), startPosition); + } else { + int modifier = r.nextInt(MAX_POSITION_MODIFIER); + if (r.nextBoolean()) { + modifier *= -1; + } + modifier = startPosition + modifier; + if (modifier <= 0) { + modifier = 1; + } + return new BigIntegerInsertValueModification(insertValue, modifier); + } + } + + @Override + public int hashCode() { + int hash = 7; + hash = 58 * hash + Objects.hashCode(this.insertValue); + hash = 58 * hash + Objects.hashCode(this.startPosition); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final BigIntegerInsertValueModification other = (BigIntegerInsertValueModification) obj; + if (this.startPosition != other.startPosition) { + return false; + } + return Objects.equals(this.insertValue, other.insertValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerModificationFactory.java b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerModificationFactory.java index 75158ce8..ebb2c1e3 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerModificationFactory.java +++ b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerModificationFactory.java @@ -11,6 +11,7 @@ import de.rub.nds.modifiablevariable.VariableModification; import de.rub.nds.modifiablevariable.bytearray.ByteArrayModificationFactory; import de.rub.nds.modifiablevariable.integer.IntegerModificationFactory; +import de.rub.nds.modifiablevariable.longint.LongModificationFactory; import de.rub.nds.modifiablevariable.util.RandomHelper; import java.io.BufferedReader; import java.io.IOException; @@ -25,15 +26,33 @@ public class BigIntegerModificationFactory { private enum ModificationType { - ADD, SUBTRACT, XOR, EXPLICIT, SHIFT_LEFT, SHIFT_RIGHT, EXPLICIT_FROM_FILE + ADD, + SUBTRACT, + MULTIPLY, + XOR, + EXPLICIT, + SHIFT_LEFT, + SHIFT_RIGHT, + EXPLICIT_FROM_FILE, + APPEND, + INSERT, + PREPEND } private static final int MODIFICATION_COUNT = ModificationType.values().length; private static final int MAX_MODIFICATION_VALUE = 320000; + private static final int MAX_FILE_ENTRIES = 200; + private static final int MAX_MODIFICATION_SHIFT_VALUE = 50; + private static final int MAX_MODIFICATION_MULTIPLY_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_POSITION_VALUE = 50; + private static List> modificationsFromFile; public static BigIntegerAddModification add(final String summand) { @@ -94,6 +113,19 @@ public static VariableModification explicitValueFromFile(int value) return modifications.get(pos); } + public static VariableModification appendValue(final BigInteger value) { + return new BigIntegerAppendValueModification(value); + } + + public static VariableModification insertValue( + final BigInteger value, final int startPosition) { + return new BigIntegerInsertValueModification(value, startPosition); + } + + public static VariableModification prependValue(final BigInteger value) { + return new BigIntegerPrependValueModification(value); + } + /* * Interactive modification */ @@ -137,7 +169,7 @@ public static synchronized List> modificationsF modificationsFromFile = new LinkedList<>(); ClassLoader classLoader = ByteArrayModificationFactory.class.getClassLoader(); InputStream is = - classLoader.getResourceAsStream(IntegerModificationFactory.FILE_NAME); + classLoader.getResourceAsStream(LongModificationFactory.FILE_NAME); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { @@ -158,12 +190,17 @@ public static VariableModification createRandomModification() { Random random = RandomHelper.getRandom(); ModificationType randomType = ModificationType.values()[random.nextInt(MODIFICATION_COUNT)]; BigInteger modification = BigInteger.valueOf(random.nextInt(MAX_MODIFICATION_VALUE)); + BigInteger insert_modification = + BigInteger.valueOf(random.nextInt(MAX_MODIFICATION_INSERT_VALUE)); int shiftModification = random.nextInt(MAX_MODIFICATION_SHIFT_VALUE); switch (randomType) { case ADD: return new BigIntegerAddModification(modification); case SUBTRACT: return new BigIntegerSubtractModification(modification); + case MULTIPLY: + return new BigIntegerMultiplyModification( + BigInteger.valueOf(random.nextInt(MAX_MODIFICATION_MULTIPLY_VALUE))); case XOR: return new BigIntegerXorModification(modification); case EXPLICIT: @@ -173,7 +210,15 @@ public static VariableModification createRandomModification() { case SHIFT_RIGHT: return new BigIntegerShiftRightModification(shiftModification); case EXPLICIT_FROM_FILE: - return explicitValueFromFile(MAX_MODIFICATION_VALUE); + return explicitValueFromFile(MAX_FILE_ENTRIES); + case APPEND: + return new BigIntegerAppendValueModification(insert_modification); + case INSERT: + return new BigIntegerInsertValueModification( + insert_modification, + random.nextInt(MAX_MODIFICATION_INSERT_POSITION_VALUE)); + case PREPEND: + return new BigIntegerPrependValueModification(insert_modification); default: throw new IllegalStateException("Unexpected modification type: " + randomType); } diff --git a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerMultiplyModification.java b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerMultiplyModification.java index 97376ea8..2f68fcb4 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerMultiplyModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerMultiplyModification.java @@ -33,10 +33,7 @@ public BigIntegerMultiplyModification(BigInteger bi) { @Override protected BigInteger modifyImplementationHook(BigInteger input) { - if (input == null) { - input = BigInteger.ZERO; - } - return input.multiply(factor); + return (input == null) ? BigInteger.ZERO: input.multiply(factor); } public BigInteger getFactor() { diff --git a/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerPrependValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerPrependValueModification.java new file mode 100644 index 00000000..92f7f772 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerPrependValueModification.java @@ -0,0 +1,76 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.biginteger; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.math.BigInteger; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"prependValue", "modificationFilter"}) +@XmlAccessorType(XmlAccessType.FIELD) +public class BigIntegerPrependValueModification extends VariableModification { + + private static final int MAX_PREPEND_LENGTH = 8; + + private BigInteger prependValue; + + public BigIntegerPrependValueModification() {} + + public BigIntegerPrependValueModification(BigInteger bi) { + this.prependValue = bi; + } + + @Override + protected BigInteger modifyImplementationHook(BigInteger input) { + if (input == null) { + input = BigInteger.ZERO; + } + return prependValue.shiftLeft(input.bitLength()).add(input); + } + + public BigInteger getPrependValue() { + return prependValue; + } + + public void setPrependValue(BigInteger prependValue) { + this.prependValue = prependValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new BigIntegerPrependValueModification(prependValue.add(new BigInteger(MAX_PREPEND_LENGTH, new Random()))); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 60 * hash + Objects.hashCode(this.prependValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final BigIntegerPrependValueModification other = (BigIntegerPrependValueModification) obj; + return Objects.equals(this.prependValue, other.prependValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayAppendValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayAppendValueModification.java new file mode 100644 index 00000000..e80a5bb4 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayAppendValueModification.java @@ -0,0 +1,91 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.bytearray; + +import de.rub.nds.modifiablevariable.VariableModification; +import de.rub.nds.modifiablevariable.util.ArrayConverter; +import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.Arrays; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"bytesToAppend", "modificationFilter"}) +@XmlAccessorType(XmlAccessType.FIELD) +public class ByteArrayAppendValueModification extends VariableModification { + + private static final int MAX_EXPLICIT_VALUE = 256; + + @XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class) + private byte[] bytesToAppend; + + public ByteArrayAppendValueModification() {} + + public ByteArrayAppendValueModification(byte[] bytesToAppend) { + this.bytesToAppend = bytesToAppend; + } + + @Override + protected byte[] modifyImplementationHook(byte[] input) { + if (input == null) { + input = new byte[0]; + } + + return ArrayConverter.concatenate(input, bytesToAppend); + } + + public byte[] getBytesToAppend() { + return bytesToAppend; + } + + public void setBytesToAppend(byte[] bytesToAppend) { + this.bytesToAppend = bytesToAppend; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + int index = r.nextInt(bytesToAppend.length); + byte[] newValue = Arrays.copyOf(bytesToAppend, bytesToAppend.length); + newValue[index] = (byte) r.nextInt(MAX_EXPLICIT_VALUE); + return new ByteArrayAppendValueModification(newValue); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 86 * hash + Arrays.hashCode(this.bytesToAppend); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final ByteArrayAppendValueModification other = (ByteArrayAppendValueModification) obj; + return Arrays.equals(this.bytesToAppend, other.bytesToAppend); + } + + @Override + public String toString() { + return String.format("ByteArrayInsertModification{bytesToAppend=%s}", + ArrayConverter.bytesToHexString(bytesToAppend)); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertModification.java b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertValueModification.java similarity index 81% rename from src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertModification.java rename to src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertValueModification.java index c98b92ea..2a871621 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertValueModification.java @@ -21,20 +21,20 @@ @XmlRootElement @XmlType(propOrder = {"bytesToInsert", "startPosition", "modificationFilter"}) @XmlAccessorType(XmlAccessType.FIELD) -public class ByteArrayInsertModification extends VariableModification { +public class ByteArrayInsertValueModification extends VariableModification { private static final int MAX_EXPLICIT_VALUE = 256; - private static final int MAX_INSERT_MODIFIER = 32; + private static final int MAX_POSITION_MODIFIER = 32; @XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class) private byte[] bytesToInsert; private int startPosition; - public ByteArrayInsertModification() {} + public ByteArrayInsertValueModification() {} - public ByteArrayInsertModification(byte[] bytesToInsert, int startPosition) { + public ByteArrayInsertValueModification(byte[] bytesToInsert, int startPosition) { this.bytesToInsert = bytesToInsert; this.startPosition = startPosition; } @@ -49,13 +49,11 @@ protected byte[] modifyImplementationHook(byte[] input) { if (start < 0) { start += input.length; if (start < 0) { - LOGGER.debug("Trying to insert from too negative Startposition. start = {}", startPosition); - return input; + start = 0; } } if (start > input.length) { - LOGGER.debug("Trying to insert behind the Array. ArraySize:{} Insert Position:{}", input.length, startPosition); - return input; + start = input.length; } byte[] ret1 = Arrays.copyOf(input, start); byte[] ret3 = null; @@ -89,10 +87,10 @@ public VariableModification getModifiedCopy() { int index = r.nextInt(bytesToInsert.length); byte[] newValue = Arrays.copyOf(bytesToInsert, bytesToInsert.length); newValue[index] = (byte) r.nextInt(MAX_EXPLICIT_VALUE); - return new ByteArrayInsertModification(newValue, startPosition); + return new ByteArrayInsertValueModification(newValue, startPosition); } else { byte[] newValue = Arrays.copyOf(bytesToInsert, bytesToInsert.length); - int modifier = r.nextInt(MAX_INSERT_MODIFIER); + int modifier = r.nextInt(MAX_POSITION_MODIFIER); if (r.nextBoolean()) { modifier *= -1; } @@ -100,7 +98,7 @@ public VariableModification getModifiedCopy() { if (modifier <= 0) { modifier = 1; } - return new ByteArrayInsertModification(newValue, modifier); + return new ByteArrayInsertValueModification(newValue, modifier); } } @@ -123,7 +121,7 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) { return false; } - final ByteArrayInsertModification other = (ByteArrayInsertModification) obj; + final ByteArrayInsertValueModification other = (ByteArrayInsertValueModification) obj; if (this.startPosition != other.startPosition) { return false; } diff --git a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayModificationFactory.java b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayModificationFactory.java index c87c5e55..f32e4153 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayModificationFactory.java +++ b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayModificationFactory.java @@ -22,13 +22,13 @@ public class ByteArrayModificationFactory { private enum ModificationType { - XOR, INSERT, DELETE, EXPLICIT, DUPLICATE, EXPLICIT_FROM_FILE, SHUFFLE + XOR, APPEND, INSERT, PREPEND, DELETE, EXPLICIT, DUPLICATE, EXPLICIT_FROM_FILE, SHUFFLE } private static final int MODIFICATION_COUNT = ModificationType.values().length; - private static final int MAX_CONFIG_PARAMETER = 200; + private static final int MAX_BYTE_ARRAY_LENGTH = 200; - private static final int EXPLICIT_VALUE_RANDOM = 1000; + private static final int MAX_FILE_ENTRIES = 1000; private static final int MODIFIED_ARRAY_LENGTH_ESTIMATION = 50; @@ -48,13 +48,23 @@ public static VariableModification xor(final byte[] xor, final int start /** * * * - * @param bytesToInsert bytes to xor + * @param bytesToInsert bytes to insert * @param startPosition negative numbers mean that the position is taken from the end * @return variable modification */ - public static VariableModification insert( + public static VariableModification insertValue( final byte[] bytesToInsert, final int startPosition) { - return new ByteArrayInsertModification(bytesToInsert, startPosition); + return new ByteArrayInsertValueModification(bytesToInsert, startPosition); + } + + public static VariableModification appendValue( + final byte[] bytesToAppend) { + return new ByteArrayAppendValueModification(bytesToAppend); + } + + public static VariableModification prependValue( + final byte[] bytesToPrepend) { + return new ByteArrayPrependValueModification(bytesToPrepend); } /** @@ -132,44 +142,40 @@ public static VariableModification createRandomModification(byte[] origi randomType = ModificationType.EXPLICIT; } } + modificationArrayLength = random.nextInt(MAX_BYTE_ARRAY_LENGTH - 1) + 1; switch (randomType) { case XOR: - modificationArrayLength = random.nextInt(modifiedArrayLength); - if (modificationArrayLength == 0) { - modificationArrayLength++; - } byte[] xor = new byte[modificationArrayLength]; random.nextBytes(xor); startPosition = random.nextInt(modifiedArrayLength - modificationArrayLength); return new ByteArrayXorModification(xor, startPosition); + case APPEND: + byte[] bytesToAppend = new byte[modificationArrayLength]; + random.nextBytes(bytesToAppend); + return new ByteArrayAppendValueModification(bytesToAppend); case INSERT: - modificationArrayLength = random.nextInt(MAX_CONFIG_PARAMETER); - if (modificationArrayLength == 0) { - modificationArrayLength++; - } byte[] bytesToInsert = new byte[modificationArrayLength]; random.nextBytes(bytesToInsert); - int insertPosition = random.nextInt(modifiedArrayLength); - return new ByteArrayInsertModification(bytesToInsert, insertPosition); + return new ByteArrayInsertValueModification(bytesToInsert, random.nextInt(modifiedArrayLength)); + case PREPEND: + byte[] bytesToPrepend = new byte[modificationArrayLength]; + random.nextBytes(bytesToPrepend); + return new ByteArrayPrependValueModification(bytesToPrepend); case DELETE: startPosition = random.nextInt(modifiedArrayLength - 1); int count = random.nextInt(modifiedArrayLength - startPosition); count++; return new ByteArrayDeleteModification(startPosition, count); case EXPLICIT: - modificationArrayLength = random.nextInt(MAX_CONFIG_PARAMETER); - if (modificationArrayLength == 0) { - modificationArrayLength++; - } byte[] explicitValue = new byte[modificationArrayLength]; random.nextBytes(explicitValue); return new ByteArrayExplicitValueModification(explicitValue); case DUPLICATE: return new ByteArrayDuplicateModification(); case EXPLICIT_FROM_FILE: - return explicitValueFromFile(random.nextInt(EXPLICIT_VALUE_RANDOM)); + return explicitValueFromFile(random.nextInt(MAX_FILE_ENTRIES)); case SHUFFLE: - int shuffleSize = random.nextInt(MAX_CONFIG_PARAMETER); + int shuffleSize = random.nextInt(MAX_BYTE_ARRAY_LENGTH); byte[] shuffle = new byte[shuffleSize]; random.nextBytes(shuffle); return new ByteArrayShuffleModification(shuffle); diff --git a/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayPrependValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayPrependValueModification.java new file mode 100644 index 00000000..f1bfc264 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayPrependValueModification.java @@ -0,0 +1,91 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.bytearray; + +import de.rub.nds.modifiablevariable.VariableModification; +import de.rub.nds.modifiablevariable.util.ArrayConverter; +import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.Arrays; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"bytesToPrepend", "modificationFilter"}) +@XmlAccessorType(XmlAccessType.FIELD) +public class ByteArrayPrependValueModification extends VariableModification { + + private static final int MAX_EXPLICIT_VALUE = 256; + + @XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class) + private byte[] bytesToPrepend; + + public ByteArrayPrependValueModification() {} + + public ByteArrayPrependValueModification(byte[] bytesToPrepend) { + this.bytesToPrepend = bytesToPrepend; + } + + @Override + protected byte[] modifyImplementationHook(byte[] input) { + if (input == null) { + input = new byte[0]; + } + + return ArrayConverter.concatenate(bytesToPrepend, input); + } + + public byte[] getBytesToPrepend() { + return bytesToPrepend; + } + + public void setBytesToPrepend(byte[] bytesToPrepend) { + this.bytesToPrepend = bytesToPrepend; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + int index = r.nextInt(bytesToPrepend.length); + byte[] newValue = Arrays.copyOf(bytesToPrepend, bytesToPrepend.length); + newValue[index] = (byte) r.nextInt(MAX_EXPLICIT_VALUE); + return new ByteArrayPrependValueModification(newValue); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 87 * hash + Arrays.hashCode(this.bytesToPrepend); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final ByteArrayPrependValueModification other = (ByteArrayPrependValueModification) obj; + return Arrays.equals(this.bytesToPrepend, other.bytesToPrepend); + } + + @Override + public String toString() { + return String.format("ByteArrayInsertModification{bytesToPrepend=%s}", + ArrayConverter.bytesToHexString(bytesToPrepend)); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAddModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAddModification.java index 1b4a9c44..a44ed33a 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAddModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAddModification.java @@ -23,8 +23,8 @@ public class IntegerAddModification extends VariableModification { public IntegerAddModification() {} - public IntegerAddModification(Integer bi) { - this.summand = bi; + public IntegerAddModification(Integer summand) { + this.summand = summand; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAppendValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAppendValueModification.java new file mode 100644 index 00000000..067410f6 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerAppendValueModification.java @@ -0,0 +1,73 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.integer; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"appendValue", "modificationFilter"}) +public class IntegerAppendValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + + private Integer appendValue; + + public IntegerAppendValueModification() {} + + public IntegerAppendValueModification(Integer appendValue) { + this.appendValue = appendValue; + } + + @Override + protected Integer modifyImplementationHook(Integer input) { + if (input == null) { + input = 0; + } + return (input << (Integer.SIZE - Integer.numberOfLeadingZeros((appendValue)))) + appendValue; + } + + public Integer getAppendValue() { + return appendValue; + } + + public void setAppendValue(Integer appendValue) { + this.appendValue = appendValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new IntegerAppendValueModification( + appendValue + new Random().nextInt(MAX_VALUE_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 38 * hash + Objects.hashCode(this.appendValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final IntegerAppendValueModification other = (IntegerAppendValueModification) obj; + return Objects.equals(this.appendValue, other.appendValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerExplicitValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerExplicitValueModification.java index 32affcac..0ad99c1a 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerExplicitValueModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerExplicitValueModification.java @@ -23,8 +23,8 @@ public class IntegerExplicitValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + + private static final int MAX_POSITION_MODIFIER = 32; + + private Integer insertValue; + + private int startPosition; + + public IntegerInsertValueModification() {} + + public IntegerInsertValueModification(Integer insertValue, int startPosition) { + this.insertValue = insertValue; + this.startPosition = startPosition; + } + + @Override + protected Integer modifyImplementationHook(Integer input) { + if (input == null) { + input = 0; + } + int originalValueLength = Integer.SIZE - Integer.numberOfLeadingZeros((input)); + int insertValueLength = Integer.SIZE - Integer.numberOfLeadingZeros((insertValue)); + int insertPosition = startPosition; + if (startPosition > originalValueLength) { + insertPosition = originalValueLength; + } else if (startPosition < 0) { + insertPosition = 0; + } + int mask = ((1 << insertPosition) - 1); + + return (((input >> insertPosition) << insertValueLength) + insertValue) << insertPosition + (mask & input); + } + + public Integer getInsertValue() { + return insertValue; + } + + public void setInsertValue(Integer insertValue) { + this.insertValue = insertValue; + } + + public int getStartPosition() { + return startPosition; + } + + public void setStartPosition(int startPosition) { + this.startPosition = startPosition; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + if (r.nextBoolean()) { + return new IntegerInsertValueModification( + insertValue + r.nextInt(MAX_VALUE_MODIFIER), startPosition); + } else { + int modifier = r.nextInt(MAX_POSITION_MODIFIER); + if (r.nextBoolean()) { + modifier *= -1; + } + modifier = startPosition + modifier; + if (modifier <= 0) { + modifier = 1; + } + return new IntegerInsertValueModification(insertValue, modifier); + } + } + + @Override + public int hashCode() { + int hash = 7; + hash = 37 * hash + Objects.hashCode(this.insertValue); + hash = 37 * hash + this.startPosition; + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final IntegerInsertValueModification other = (IntegerInsertValueModification) obj; + if (this.startPosition != other.startPosition) { + return false; + } + return Objects.equals(this.insertValue, other.insertValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerModificationFactory.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerModificationFactory.java index 7b65d624..dd596e93 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerModificationFactory.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerModificationFactory.java @@ -21,14 +21,33 @@ public class IntegerModificationFactory { private enum ModificationType { - ADD, SUBTRACT, XOR, EXPLICIT, SHIFT_LEFT, SHIFT_RIGHT, EXPLICIT_FROM_FILE + ADD, + SUBTRACT, + MULTIPLY, + XOR, + EXPLICIT, + SHIFT_LEFT, + SHIFT_RIGHT, + EXPLICIT_FROM_FILE, + APPEND, + INSERT, + PREPEND } + private static final int MODIFICATION_COUNT = ModificationType.values().length; private static final int MAX_MODIFICATION_VALUE = 32000; + private static final int MAX_FILE_ENTRIES = 200; + private static final int MAX_MODIFICATION_SHIFT_VALUE = 20; + private static final int MAX_MODIFICATION_MULTIPLY_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_POSITION_VALUE = 32; + private static List> modificationsFromFile; public static final String FILE_NAME = "de/rub/nds/modifiablevariable/explicit/integer.vec"; @@ -87,6 +106,23 @@ public static VariableModification explicitValueFromFile(int value) { return modifications.get(pos); } + public static VariableModification appendValue(final Integer value) { + return new IntegerAppendValueModification(value); + } + + public static VariableModification insertValue( + final Integer value, final int position) { + return new IntegerInsertValueModification(value, position); + } + + public static VariableModification prependValue(final Integer value) { + return new IntegerPrependValueModification(value); + } + + public static VariableModification multiply(final Integer value) { + return new IntegerMultiplyModification(value); + } + public static synchronized List> modificationsFromFile() { try { if (modificationsFromFile == null) { @@ -111,12 +147,16 @@ public static VariableModification createRandomModification() { Random random = RandomHelper.getRandom(); ModificationType randomType = ModificationType.values()[random.nextInt(MODIFICATION_COUNT)]; int modification = random.nextInt(MAX_MODIFICATION_VALUE); + int insert_modification = random.nextInt(MAX_MODIFICATION_INSERT_VALUE); int shiftModification = random.nextInt(MAX_MODIFICATION_SHIFT_VALUE); switch (randomType) { case ADD: return new IntegerAddModification(modification); case SUBTRACT: return new IntegerSubtractModification(modification); + case MULTIPLY: + return new IntegerSubtractModification( + random.nextInt(MAX_MODIFICATION_MULTIPLY_VALUE)); case XOR: return new IntegerXorModification(modification); case EXPLICIT: @@ -126,7 +166,15 @@ public static VariableModification createRandomModification() { case SHIFT_RIGHT: return new IntegerShiftRightModification(shiftModification); case EXPLICIT_FROM_FILE: - return explicitValueFromFile(random.nextInt(MAX_MODIFICATION_VALUE)); + return explicitValueFromFile(random.nextInt(MAX_FILE_ENTRIES)); + case APPEND: + return new IntegerAppendValueModification(insert_modification); + case INSERT: + return new IntegerInsertValueModification( + insert_modification, + random.nextInt(MAX_MODIFICATION_INSERT_POSITION_VALUE)); + case PREPEND: + return new IntegerPrependValueModification(insert_modification); default: throw new IllegalStateException("Unexpected modification type: " + randomType); } diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerMultiplyModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerMultiplyModification.java new file mode 100644 index 00000000..431b97af --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerMultiplyModification.java @@ -0,0 +1,72 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.integer; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"factor", "modificationFilter"}) +public class IntegerMultiplyModification extends VariableModification { + + private static final int MAX_FACTOR_MODIFIER = 256; + + private Integer factor; + + public IntegerMultiplyModification() {} + + public IntegerMultiplyModification(Integer factor) { + this.factor = factor; + } + + @Override + protected Integer modifyImplementationHook(Integer input) { + return (input == null) ? 0 : input * factor; + } + + public Integer getFactor() { + return factor; + } + + public void setFactor(Integer factor) { + this.factor = factor; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + return new IntegerMultiplyModification( + factor + r.nextInt(MAX_FACTOR_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 40 * hash + Objects.hashCode(this.factor); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final IntegerMultiplyModification other = (IntegerMultiplyModification) obj; + return Objects.equals(this.factor, other.factor); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerPrependValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerPrependValueModification.java new file mode 100644 index 00000000..10612063 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerPrependValueModification.java @@ -0,0 +1,74 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.integer; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"prependValue", "modificationFilter"}) +public class IntegerPrependValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + + private Integer prependValue; + + public IntegerPrependValueModification() {} + + public IntegerPrependValueModification(Integer prependValue) { + this.prependValue = prependValue; + } + + @Override + protected Integer modifyImplementationHook(Integer input) { + if (input == null) { + input = 0; + } + + return (prependValue << (Integer.SIZE - Integer.numberOfLeadingZeros((input)))) + input; + } + + public Integer getPrependValue() { + return prependValue; + } + + public void setPrependValue(Integer prependValue) { + this.prependValue = prependValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new IntegerPrependValueModification( + prependValue + new Random().nextInt(MAX_VALUE_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 39 * hash + Objects.hashCode(this.prependValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final IntegerPrependValueModification other = (IntegerPrependValueModification) obj; + return Objects.equals(this.prependValue, other.prependValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftLeftModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftLeftModification.java index 31332f5b..b422e87e 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftLeftModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftLeftModification.java @@ -28,7 +28,7 @@ public IntegerShiftLeftModification(int shift) { @Override protected Integer modifyImplementationHook(Integer input) { - return (input == null) ? 0 : input << shift; + return (input == null) ? 0 : input << shift % MAX_SHIFT_MODIFIER; } public int getShift() { diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftRightModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftRightModification.java index 4d0467fa..fb503ec5 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftRightModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerShiftRightModification.java @@ -28,7 +28,7 @@ public IntegerShiftRightModification(int shift) { @Override protected Integer modifyImplementationHook(final Integer input) { - return (input == null) ? 0 : input >> shift; + return (input == null) ? 0 : input >> shift % MAX_SHIFT_MODIFIER; } public int getShift() { diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerSubtractModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerSubtractModification.java index 0d23a33b..b71c9cea 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerSubtractModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerSubtractModification.java @@ -23,8 +23,8 @@ public class IntegerSubtractModification extends VariableModification { public IntegerSubtractModification() {} - public IntegerSubtractModification(Integer bi) { - this.subtrahend = bi; + public IntegerSubtractModification(Integer subtrahend) { + this.subtrahend = subtrahend; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerXorModification.java b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerXorModification.java index 07338a89..7931f109 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerXorModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/integer/IntegerXorModification.java @@ -23,8 +23,8 @@ public class IntegerXorModification extends VariableModification { public IntegerXorModification() {} - public IntegerXorModification(Integer bi) { - this.xor = bi; + public IntegerXorModification(Integer xor) { + this.xor = xor; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongAddModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongAddModification.java index afde2330..781d17c3 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/longint/LongAddModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongAddModification.java @@ -23,8 +23,8 @@ public class LongAddModification extends VariableModification { public LongAddModification() {} - public LongAddModification(Long bi) { - this.summand = bi; + public LongAddModification(Long summand) { + this.summand = summand; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongAppendValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongAppendValueModification.java new file mode 100644 index 00000000..cf163e93 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongAppendValueModification.java @@ -0,0 +1,72 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"appendValue", "modificationFilter"}) +public class LongAppendValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + + private Long appendValue; + + public LongAppendValueModification() {} + + public LongAppendValueModification(Long appendValue) { + this.appendValue = appendValue; + } + + @Override + protected Long modifyImplementationHook(Long input) { + if (input == null) { + input = 0L; + } + return (input << (Long.SIZE - Long.numberOfLeadingZeros((appendValue)))) + appendValue; + } + + public Long getAppendValue() { + return appendValue; + } + + public void setAppendValue(Long appendValue) { + this.appendValue = appendValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new LongAppendValueModification(appendValue + new Random().nextInt(MAX_VALUE_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 45 * hash + Objects.hashCode(this.appendValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongAppendValueModification other = (LongAppendValueModification) obj; + return Objects.equals(this.appendValue, other.appendValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongExplicitValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongExplicitValueModification.java index b7422adf..765476b8 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/longint/LongExplicitValueModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongExplicitValueModification.java @@ -23,8 +23,8 @@ public class LongExplicitValueModification extends VariableModification { public LongExplicitValueModification() {} - public LongExplicitValueModification(Long bi) { - this.explicitValue = bi; + public LongExplicitValueModification(Long explicitValue) { + this.explicitValue = explicitValue; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongInsertValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongInsertValueModification.java new file mode 100644 index 00000000..524f6613 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongInsertValueModification.java @@ -0,0 +1,113 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import de.rub.nds.modifiablevariable.integer.IntegerInsertValueModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"insertValue", "startPosition", "modificationFilter"}) +public class LongInsertValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + private static final int MAX_POSITION_MODIFIER = 32; + + private Long insertValue; + private int startPosition; + + public LongInsertValueModification() {} + + public LongInsertValueModification(Long insertValue, int startPosition) { + this.insertValue = insertValue; + this.startPosition = startPosition; + } + + @Override + protected Long modifyImplementationHook(Long input) { + if (input == null) { + input = 0L; + } + int originalValueLength = Long.SIZE - Long.numberOfLeadingZeros((input)); + int insertValueLength = Long.SIZE - Long.numberOfLeadingZeros((insertValue)); + int insertPosition = startPosition; + if (startPosition > originalValueLength) { + insertPosition = originalValueLength; + } else if (startPosition < 0) { + insertPosition = 0; + } + long mask = ((1L << insertPosition) - 1); + + return (((input >> insertPosition) << insertValueLength) + insertValue) << insertPosition + (mask & input); + } + + public Long getInsertValue() { + return insertValue; + } + + public void setInsertValue(Long insertValue) { + this.insertValue = insertValue; + } + + public int getStartPosition() { + return startPosition; + } + + public void setStartPosition(int startPosition) { + this.startPosition = startPosition; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + + if (r.nextBoolean()) { + return new LongInsertValueModification( + insertValue + r.nextInt(MAX_VALUE_MODIFIER), startPosition); + } else { + int modifier = r.nextInt(MAX_POSITION_MODIFIER); + if (r.nextBoolean()) { + modifier *= -1; + } + modifier = startPosition + modifier; + if (modifier <= 0) { + modifier = 1; + } + return new LongInsertValueModification(insertValue, modifier); + } + } + + @Override + public int hashCode() { + int hash = 7; + hash = 47 * hash + Objects.hashCode(this.insertValue); + hash = 47 * hash + Objects.hashCode(this.startPosition); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongInsertValueModification other = (LongInsertValueModification) obj; + if (this.startPosition != other.startPosition) { + return false; + } + return Objects.equals(this.insertValue, other.insertValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongModificationFactory.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongModificationFactory.java index ca97a66a..afc6322c 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/longint/LongModificationFactory.java +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongModificationFactory.java @@ -22,14 +22,31 @@ public class LongModificationFactory { private enum ModificationType { - ADD, SUBTRACT, XOR, EXPLICIT, EXPLICIT_FROM_FILE + ADD, SUBTRACT, MULTIPLY, XOR, EXPLICIT, + SHIFT_LEFT, + SHIFT_RIGHT, EXPLICIT_FROM_FILE, + APPEND, + INSERT, + PREPEND } private static final int MODIFICATION_COUNT = ModificationType.values().length; private static final int MAX_MODIFICATION_VALUE = 32000; + private static final int MAX_FILE_ENTRIES = 200; + + private static final int MAX_MODIFICATION_SHIFT_VALUE = 40; + + private static final int MAX_MODIFICATION_MULTIPLY_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_VALUE = 256; + + private static final int MAX_MODIFICATION_INSERT_POSITION_VALUE = 64; + private static List> modificationsFromFile; + public static final String FILE_NAME = "de/rub/nds/modifiablevariable/explicit/long.vec"; + public static LongAddModification add(final String summand) { return add(Long.parseLong(summand)); } @@ -68,13 +85,38 @@ public static VariableModification explicitValueFromFile(int value) { return modifications.get(pos); } + public static VariableModification appendValue(final Long value) { + return new LongAppendValueModification(value); + } + + public static VariableModification insertValue(final Long value, final int position) { + return new LongInsertValueModification(value, position); + } + + public static VariableModification prependValue(final Long value) { + return new LongPrependValueModification(value); + } + + public static VariableModification multiply(final Long factor) { + return new LongMultiplyModification(factor); + } + + public static VariableModification shiftLeft(final int shift) { + return new LongShiftLeftModification(shift); + } + + public static VariableModification shiftRight(final int shift) { + return new LongShiftRightModification(shift); + } + + public static synchronized List> modificationsFromFile() { try { if (modificationsFromFile == null) { modificationsFromFile = new LinkedList<>(); ClassLoader classLoader = IntegerModificationFactory.class.getClassLoader(); InputStream is = - classLoader.getResourceAsStream(IntegerModificationFactory.FILE_NAME); + classLoader.getResourceAsStream(FILE_NAME); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { @@ -93,17 +135,31 @@ public static VariableModification createRandomModification() { Random random = RandomHelper.getRandom(); ModificationType randomType = ModificationType.values()[random.nextInt(MODIFICATION_COUNT)]; long modification = random.nextInt(MAX_MODIFICATION_VALUE); + long insert_modification = random.nextInt( MAX_MODIFICATION_INSERT_VALUE); + int shiftModification = random.nextInt(MAX_MODIFICATION_SHIFT_VALUE); switch (randomType) { case ADD: return new LongAddModification(modification); case SUBTRACT: return new LongSubtractModification(modification); + case MULTIPLY: + return new LongMultiplyModification((long)random.nextInt(MAX_MODIFICATION_MULTIPLY_VALUE)); case XOR: return new LongXorModification(modification); case EXPLICIT: return new LongExplicitValueModification(modification); + case SHIFT_LEFT: + return new LongShiftLeftModification(shiftModification); + case SHIFT_RIGHT: + return new LongShiftRightModification(shiftModification); case EXPLICIT_FROM_FILE: - return explicitValueFromFile(random.nextInt(MAX_MODIFICATION_VALUE)); + return explicitValueFromFile(random.nextInt(MAX_FILE_ENTRIES)); + case APPEND: + return new LongAppendValueModification(insert_modification); + case INSERT: + return new LongInsertValueModification(insert_modification, random.nextInt(MAX_MODIFICATION_INSERT_POSITION_VALUE)); + case PREPEND: + return new LongPrependValueModification(insert_modification); default: throw new IllegalStateException("Unexpected modification type: " + randomType); } diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongMultiplyModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongMultiplyModification.java new file mode 100644 index 00000000..1c9388ed --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongMultiplyModification.java @@ -0,0 +1,69 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"factor", "modificationFilter"}) +public class LongMultiplyModification extends VariableModification { + + private static final int MAX_FACTOR_MODIFIER = 256; + + private Long factor; + + public LongMultiplyModification() {} + + public LongMultiplyModification(Long factor) { + this.factor = factor; + } + + @Override + protected Long modifyImplementationHook(final Long input) { + return (input == null) ? 0L : input * factor; + } + + public Long getFactor() { + return factor; + } + + public void setFactor(Long factor) { + this.factor = factor; + } + + @Override + public VariableModification getModifiedCopy() { + return new LongMultiplyModification(factor + new Random().nextInt(MAX_FACTOR_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 48 * hash + Objects.hashCode(this.factor); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongMultiplyModification other = (LongMultiplyModification) obj; + return Objects.equals(this.factor, other.factor); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongPrependValueModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongPrependValueModification.java new file mode 100644 index 00000000..1d035dc9 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongPrependValueModification.java @@ -0,0 +1,72 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"prependValue", "modificationFilter"}) +public class LongPrependValueModification extends VariableModification { + + private static final int MAX_VALUE_MODIFIER = 256; + + private Long prependValue; + + public LongPrependValueModification() {} + + public LongPrependValueModification(Long prependValue) { + this.prependValue = prependValue; + } + + @Override + protected Long modifyImplementationHook(Long input) { + if (input == null) { + input = 0L; + } + return (prependValue << (Long.SIZE - Long.numberOfLeadingZeros((input)))) + input; + } + + public Long getPrependValue() { + return prependValue; + } + + public void setPrependValue(Long prependValue) { + this.prependValue = prependValue; + } + + @Override + public VariableModification getModifiedCopy() { + return new LongPrependValueModification(prependValue + new Random().nextInt(MAX_VALUE_MODIFIER)); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 46 * hash + Objects.hashCode(this.prependValue); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongPrependValueModification other = (LongPrependValueModification) obj; + return Objects.equals(this.prependValue, other.prependValue); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftLeftModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftLeftModification.java new file mode 100644 index 00000000..2cb69884 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftLeftModification.java @@ -0,0 +1,82 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"shift", "modificationFilter"}) +public class LongShiftLeftModification extends VariableModification { + + private static final int MAX_SHIFT_MODIFIER = 64; + + private int shift; + + public LongShiftLeftModification() {} + + public LongShiftLeftModification(int shift) { + this.shift = shift; + } + + @Override + protected Long modifyImplementationHook(final Long input) { + return (input == null) ? 0L : input << shift % MAX_SHIFT_MODIFIER; + } + + public int getShift() { + return shift; + } + + public void setShift(int shift) { + this.shift = shift; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + int newShift; + if (r.nextBoolean()) { + newShift = shift + r.nextInt(MAX_SHIFT_MODIFIER); + } else { + newShift = shift - r.nextInt(MAX_SHIFT_MODIFIER); + } + if (newShift < 0) { + newShift = MAX_SHIFT_MODIFIER - 1; + } else if (newShift > MAX_SHIFT_MODIFIER - 1) { + newShift = 0; + } + + return new LongShiftLeftModification(newShift); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 49 * hash + Objects.hashCode(this.shift); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongShiftLeftModification other = (LongShiftLeftModification) obj; + return Objects.equals(this.shift, other.shift); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftRightModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftRightModification.java new file mode 100644 index 00000000..1b2b9a08 --- /dev/null +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongShiftRightModification.java @@ -0,0 +1,82 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.longint; + +import de.rub.nds.modifiablevariable.VariableModification; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import java.util.Objects; +import java.util.Random; + +@XmlRootElement +@XmlType(propOrder = {"shift", "modificationFilter"}) +public class LongShiftRightModification extends VariableModification { + + private static final int MAX_SHIFT_MODIFIER = 64; + + private int shift; + + public LongShiftRightModification() {} + + public LongShiftRightModification(int shift) { + this.shift = shift; + } + + @Override + protected Long modifyImplementationHook(final Long input) { + return (input == null) ? 0L : input >> shift % MAX_SHIFT_MODIFIER; + } + + public int getShift() { + return shift; + } + + public void setShift(int shift) { + this.shift = shift; + } + + @Override + public VariableModification getModifiedCopy() { + Random r = new Random(); + int newShift; + if (r.nextBoolean()) { + newShift = shift + r.nextInt(MAX_SHIFT_MODIFIER); + } else { + newShift = shift - r.nextInt(MAX_SHIFT_MODIFIER); + } + if (newShift < 0) { + newShift = MAX_SHIFT_MODIFIER - 1; + } else if (newShift > MAX_SHIFT_MODIFIER - 1) { + newShift = 0; + } + + return new LongShiftRightModification(newShift); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 50 * hash + Objects.hashCode(this.shift); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LongShiftRightModification other = (LongShiftRightModification) obj; + return Objects.equals(this.shift, other.shift); + } +} diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongSubtractModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongSubtractModification.java index 7b554e0d..6c617018 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/longint/LongSubtractModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongSubtractModification.java @@ -23,8 +23,8 @@ public class LongSubtractModification extends VariableModification { public LongSubtractModification() {} - public LongSubtractModification(Long bi) { - this.subtrahend = bi; + public LongSubtractModification(Long subtrahend) { + this.subtrahend = subtrahend; } @Override diff --git a/src/main/java/de/rub/nds/modifiablevariable/longint/LongXorModification.java b/src/main/java/de/rub/nds/modifiablevariable/longint/LongXorModification.java index fd73e4d2..0cc8b3ba 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/longint/LongXorModification.java +++ b/src/main/java/de/rub/nds/modifiablevariable/longint/LongXorModification.java @@ -23,8 +23,8 @@ public class LongXorModification extends VariableModification { public LongXorModification() {} - public LongXorModification(Long bi) { - this.xor = bi; + public LongXorModification(Long xor) { + this.xor = xor; } @Override @@ -44,9 +44,9 @@ public void setXor(Long xor) { public VariableModification getModifiedCopy() { Random r = new Random(); if (r.nextBoolean()) { - return new LongXorModification(xor + new Random().nextInt(MAX_XOR_MODIFIER)); + return new LongXorModification(xor + r.nextInt(MAX_XOR_MODIFIER)); } else { - return new LongXorModification(xor - new Random().nextInt(MAX_XOR_MODIFIER)); + return new LongXorModification(xor - r.nextInt(MAX_XOR_MODIFIER)); } } diff --git a/src/main/java/de/rub/nds/modifiablevariable/string/ModifiableString.java b/src/main/java/de/rub/nds/modifiablevariable/string/ModifiableString.java index 88076b21..05dd42a9 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/string/ModifiableString.java +++ b/src/main/java/de/rub/nds/modifiablevariable/string/ModifiableString.java @@ -30,7 +30,7 @@ public ModifiableString() {} @Override protected void createRandomModification() { - VariableModification vm = StringModificationFactory.createRandomModification(); + VariableModification vm = StringModificationFactory.createRandomModification(null); setModification(vm); } diff --git a/src/main/java/de/rub/nds/modifiablevariable/util/Modifiable.java b/src/main/java/de/rub/nds/modifiablevariable/util/Modifiable.java index d87dd421..e261f2c8 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/util/Modifiable.java +++ b/src/main/java/de/rub/nds/modifiablevariable/util/Modifiable.java @@ -77,10 +77,47 @@ private static ModifiableString getModifiableStringWithModification( return modifiableString; } + public static ModifiableBigInteger prepend(BigInteger i) { + return getModifiableBigIntegerWithModification( + BigIntegerModificationFactory.prependValue(i)); + } + + public static ModifiableByteArray prepend(byte[] b) { + return getModifiableByteArrayWithModification( + ByteArrayModificationFactory.prependValue(b)); + } + + public static ModifiableInteger prepend(Integer i) { + return getModifiableIntegerWithModification(IntegerModificationFactory.prependValue(i)); + } + + public static ModifiableLong prepend(Long l) { + return getModifiableLongWithModification(LongModificationFactory.prependValue(l)); + } + public static ModifiableString prepend(final String s) { return getModifiableStringWithModification(StringModificationFactory.prependValue(s)); } + + public static ModifiableBigInteger append(BigInteger i) { + return getModifiableBigIntegerWithModification( + BigIntegerModificationFactory.appendValue(i)); + } + + public static ModifiableByteArray append(byte[] b) { + return getModifiableByteArrayWithModification( + ByteArrayModificationFactory.appendValue(b)); + } + + public static ModifiableInteger append(Integer i) { + return getModifiableIntegerWithModification(IntegerModificationFactory.appendValue(i)); + } + + public static ModifiableLong append(Long l) { + return getModifiableLongWithModification(LongModificationFactory.appendValue(l)); + } + public static ModifiableString append(final String s) { return getModifiableStringWithModification(StringModificationFactory.appendValue(s)); } @@ -114,6 +151,26 @@ public static ModifiableBoolean explicit(Boolean b) { public static ModifiableString explicit(String s) { return getModifiableStringWithModification(StringModificationFactory.explicitValue(s)); } + + + public static ModifiableBigInteger insert(BigInteger i, int position) { + return getModifiableBigIntegerWithModification( + BigIntegerModificationFactory.insertValue(i, position)); + } + + public static ModifiableByteArray insert(byte[] b, int position) { + return getModifiableByteArrayWithModification( + ByteArrayModificationFactory.insertValue(b, position)); + } + + public static ModifiableInteger insert(Integer i, int position) { + return getModifiableIntegerWithModification(IntegerModificationFactory.insertValue(i, position)); + } + + public static ModifiableLong insert(Long l, int position) { + return getModifiableLongWithModification(LongModificationFactory.insertValue(l, position)); + } + public static ModifiableString insert(String s, int position) { return getModifiableStringWithModification(StringModificationFactory.insertValue(s, position)); } @@ -172,10 +229,6 @@ public static ModifiableLong sub(Long l) { return getModifiableLongWithModification(LongModificationFactory.sub(l)); } - public static ModifiableByteArray insert(byte[] b, int position) { - return getModifiableByteArrayWithModification( - ByteArrayModificationFactory.insert(b, position)); - } public static ModifiableByteArray delete(int startPosition, int count) { return getModifiableByteArrayWithModification( @@ -203,6 +256,10 @@ public static ModifiableInteger shiftLeft(Integer i) { return getModifiableIntegerWithModification(IntegerModificationFactory.shiftLeft(i)); } + public static ModifiableLong shiftLeftLong(Integer i) { + return getModifiableLongWithModification(LongModificationFactory.shiftLeft(i)); + } + public static ModifiableBigInteger shiftRightBigInteger(Integer i) { return getModifiableBigIntegerWithModification(BigIntegerModificationFactory.shiftRight(i)); } @@ -211,7 +268,20 @@ public static ModifiableBigInteger multiplyBigInteger(BigInteger i) { return getModifiableBigIntegerWithModification(BigIntegerModificationFactory.multiply(i)); } + + public static ModifiableInteger multiply(Integer i) { + return getModifiableIntegerWithModification(IntegerModificationFactory.multiply(i)); + } + + public static ModifiableLong multiply(Long l) { + return getModifiableLongWithModification(LongModificationFactory.multiply(l)); + } + public static ModifiableInteger shiftRight(Integer i) { return getModifiableIntegerWithModification(IntegerModificationFactory.shiftRight(i)); } + + public static ModifiableLong shiftRightLong(Integer i) { + return getModifiableLongWithModification(LongModificationFactory.shiftRight(i)); + } } diff --git a/src/main/resources/de/rub/nds/modifiablevariable/explicit/long.vec b/src/main/resources/de/rub/nds/modifiablevariable/explicit/long.vec new file mode 100644 index 00000000..da14a614 --- /dev/null +++ b/src/main/resources/de/rub/nds/modifiablevariable/explicit/long.vec @@ -0,0 +1,31 @@ +-128 /* Overflow signed 8-bit when decremented */ +-1 /* */ +0 /* */ +1 /* */ +16 /* One-off with common buffer size */ +32 /* One-off with common buffer size */ +64 /* One-off with common buffer size */ +100 /* One-off with common buffer size */ +127 /* Overflow signed 8-bit when incremented */ +-32768 /* Overflow signed 16-bit when decremented */ +-129 /* Overflow signed 8-bit */ +128 /* Overflow signed 8-bit */ +255 /* Overflow unsig 8-bit when incremented */ +256 /* Overflow unsig 8-bit */ +512 /* One-off with common buffer size */ +1000 /* One-off with common buffer size */ +1024 /* One-off with common buffer size */ +4096 /* One-off with common buffer size */ +32767 /* Overflow signed 16-bit when incremented */ +-2147483648 /* Overflow signed 32-bit when decremented */ +-9223372036854775808 /* Overflow signed 64-bit when decremented */ +-100663046 /* Large negative number (endian-agnostic) */ +-32769 /* Overflow signed 16-bit */ +32768 /* Overflow signed 16-bit */ +65535 /* Overflow unsig 16-bit when incremented */ +65536 /* Overflow unsig 16 bit */ +100663045 /* Large positive number (endian-agnostic) */ +2147483647 /* Overflow signed 32-bit when incremented */ +9223372036854775807 /* Overflow signed 64-bit when incremented */ +4294967295 /* Overflow unsig 32-bit when incremented */ +4294967296 /* Overflow unsig 32-bit */ \ No newline at end of file diff --git a/src/test/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArrayTest.java b/src/test/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArrayTest.java index c0305224..00961cf7 100644 --- a/src/test/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArrayTest.java +++ b/src/test/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArrayTest.java @@ -123,7 +123,7 @@ public void testPrependBytes() { } VariableModification modifier = - ByteArrayModificationFactory.insert(modification1, 0); + ByteArrayModificationFactory.insertValue(modification1, 0); start.setModification(modifier); LOGGER.debug("Expected: {}", expResult); @@ -146,7 +146,7 @@ public void testAppendBytes() { } VariableModification modifier = - ByteArrayModificationFactory.insert(modification1, originalValue.length); + ByteArrayModificationFactory.insertValue(modification1, originalValue.length); start.setModification(modifier); LOGGER.debug("Expected: {}", expResult); @@ -246,21 +246,21 @@ public void testInsertBytes() { assumeTrue(modification1.length < originalValue.length); LOGGER.debug("Inserting negative Position"); VariableModification modifier = - ByteArrayModificationFactory.insert(modification1, -2 * originalValue.length); + ByteArrayModificationFactory.insertValue(modification1, -2 * originalValue.length); start.setModification(modifier); assertArrayEquals(start.getValue(), originalValue); start = new ModifiableByteArray(); start.setOriginalValue(originalValue); LOGGER.debug("Inserting empty Array"); byte[] emptyArray = new byte[0]; - modifier = ByteArrayModificationFactory.insert(emptyArray, 0); + modifier = ByteArrayModificationFactory.insertValue(emptyArray, 0); start.setModification(modifier); assertArrayEquals(originalValue, start.getValue()); start = new ModifiableByteArray(); start.setOriginalValue(originalValue); LOGGER.debug("Inserting to big Start position"); - modifier = ByteArrayModificationFactory.insert(modification1, originalValue.length * 2); + modifier = ByteArrayModificationFactory.insertValue(modification1, originalValue.length * 2); start.setModification(modifier); assertArrayEquals(originalValue, start.getValue()); } diff --git a/src/test/java/de/rub/nds/modifiablevariable/serialization/ByteArraySerializationTest.java b/src/test/java/de/rub/nds/modifiablevariable/serialization/ByteArraySerializationTest.java index 47e58ca0..9aeba5c7 100644 --- a/src/test/java/de/rub/nds/modifiablevariable/serialization/ByteArraySerializationTest.java +++ b/src/test/java/de/rub/nds/modifiablevariable/serialization/ByteArraySerializationTest.java @@ -54,7 +54,7 @@ public void setUp() throws JAXBException { ModifiableByteArray.class, ByteArrayDeleteModification.class, ByteArrayExplicitValueModification.class, - ByteArrayInsertModification.class, + ByteArrayInsertValueModification.class, ByteArrayXorModification.class); m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); @@ -81,7 +81,7 @@ public void testSerializeDeserializeSimple() throws Exception { @Test public void testSerializeDeserializeWithModification() throws Exception { VariableModification modifier = - ByteArrayModificationFactory.insert(new byte[] {1, 2}, 0); + ByteArrayModificationFactory.insertValue(new byte[] {1, 2}, 0); start.setModification(modifier); m.marshal(start, writer);