Skip to content

Commit b2b4130

Browse files
committed
Added illegal string adapter and added iso encoding to modifiable string
1 parent e908817 commit b2b4130

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@
8989
<artifactId>jaxb-runtime</artifactId>
9090
<version>2.3.3</version>
9191
</dependency>
92+
<dependency>
93+
<groupId>org.apache.commons</groupId>
94+
<artifactId>commons-lang3</artifactId>
95+
<version>3.11</version>
96+
<type>jar</type>
97+
</dependency>
98+
</dependency>
9299
</dependencies>
93100
<build>
94101
<plugins>

src/main/java/de/rub/nds/modifiablevariable/string/ModifiableString.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
import de.rub.nds.modifiablevariable.ModifiableVariable;
2020
import de.rub.nds.modifiablevariable.VariableModification;
21+
import de.rub.nds.modifiablevariable.util.IllegalStringAdapter;
2122
import java.io.Serializable;
23+
import java.nio.charset.StandardCharsets;
2224
import javax.xml.bind.annotation.XmlAccessType;
2325
import javax.xml.bind.annotation.XmlAccessorType;
2426
import javax.xml.bind.annotation.XmlRootElement;
27+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
2528

2629
/**
2730
*
@@ -30,6 +33,7 @@
3033
@XmlAccessorType(XmlAccessType.FIELD)
3134
public class ModifiableString extends ModifiableVariable<String> implements Serializable {
3235

36+
@XmlJavaTypeAdapter(IllegalStringAdapter.class)
3337
private String originalValue;
3438

3539
public ModifiableString() {
@@ -55,7 +59,7 @@ public boolean isOriginalValueModified() {
5559
}
5660

5761
public byte[] getByteArray(int size) {
58-
return getValue().getBytes();
62+
return getValue().getBytes(StandardCharsets.ISO_8859_1);
5963
}
6064

6165
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ModifiableVariable - A Variable Concept for Runtime Modifications
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
10+
package de.rub.nds.modifiablevariable.util;
11+
12+
import javax.xml.bind.annotation.adapters.XmlAdapter;
13+
import org.apache.commons.lang.StringEscapeUtils;
14+
15+
/**
16+
*
17+
*/
18+
public class IllegalStringAdapter extends XmlAdapter<String, String> {
19+
20+
@Override
21+
public String unmarshal(String value) {
22+
return StringEscapeUtils.unescapeJava(value);
23+
}
24+
25+
@Override
26+
public String marshal(String value) {
27+
return StringEscapeUtils.escapeJava(value);
28+
}
29+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2021 ic0ns.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.rub.nds.modifiablevariable.util;
18+
19+
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.Arrays;
22+
import org.apache.commons.lang.StringEscapeUtils;
23+
import org.junit.After;
24+
import org.junit.AfterClass;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
*
32+
* @author ic0ns
33+
*/
34+
public class IllegalStringAdapterTest {
35+
36+
public IllegalStringAdapterTest() {
37+
}
38+
39+
@BeforeClass
40+
public static void setUpClass() {
41+
}
42+
43+
@AfterClass
44+
public static void tearDownClass() {
45+
}
46+
47+
@Before
48+
public void setUp() {
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
}
54+
55+
/**
56+
* Test of unmarshal method, of class IllegalStringAdapter.
57+
*/
58+
@Test
59+
public void testUnmarshal() {
60+
IllegalStringAdapter instance = new IllegalStringAdapter();
61+
byte[] data = new byte[256];
62+
for (int i = 0; i < data.length; i++) {
63+
data[i] = (byte) i;
64+
}
65+
// System.out.println("Plain: " + new String(data, StandardCharsets.US_ASCII));
66+
System.out.println(Arrays.toString(new String(data, StandardCharsets.ISO_8859_1)
67+
.getBytes(StandardCharsets.ISO_8859_1)));
68+
String marshal = instance.marshal(new String(data, StandardCharsets.ISO_8859_1));
69+
System.out.println("Marshal: " + marshal);
70+
String unmarshal = instance.unmarshal(marshal);
71+
System.out.println(StringEscapeUtils.unescapeXml(unmarshal));
72+
System.out.println("Unmarshal: " + unmarshal);
73+
assertArrayEquals(data, unmarshal.getBytes(StandardCharsets.ISO_8859_1));
74+
}
75+
}

0 commit comments

Comments
 (0)