XmlUtils: Fix hex parsing in readThisByteArrayXml
hex 'a' is not correctly deserialized in readThisByteArrayXml.
Change readThisByteArrayXml & writeByteArrayXml to use HexEncoding for
hex conversions.
Bug: 138482990
Test: Was seeing parsing errors with ag/9135251.
Validated the fix with ag/9135251.
Change-Id: If9f86b5ed85a9f883130e4110f85cd2faa995439
diff --git a/core/java/com/android/internal/util/XmlUtils.java b/core/java/com/android/internal/util/XmlUtils.java
index 344d7ef..8799e3d 100644
--- a/core/java/com/android/internal/util/XmlUtils.java
+++ b/core/java/com/android/internal/util/XmlUtils.java
@@ -26,6 +26,8 @@
import android.util.Base64;
import android.util.Xml;
+import libcore.util.HexEncoding;
+
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@@ -396,16 +398,7 @@
final int N = val.length;
out.attribute(null, "num", Integer.toString(N));
- StringBuilder sb = new StringBuilder(val.length*2);
- for (int i=0; i<N; i++) {
- int b = val[i];
- int h = (b >> 4) & 0x0f;
- sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
- h = b & 0x0f;
- sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
- }
-
- out.text(sb.toString());
+ out.text(HexEncoding.encodeToString(val).toLowerCase());
out.endTag(null, "byte-array");
}
@@ -1032,7 +1025,9 @@
"Not a number in num attribute in byte-array");
}
- byte[] array = new byte[num];
+ // 0 len byte array does not have a text in the XML tag. So, initialize to 0 len array.
+ // For all other array lens, HexEncoding.decode() below overrides the array.
+ byte[] array = new byte[0];
int eventType = parser.getEventType();
do {
@@ -1043,16 +1038,7 @@
throw new XmlPullParserException(
"Invalid value found in byte-array: " + values);
}
- // This is ugly, but keeping it to mirror the logic in #writeByteArrayXml.
- for (int i = 0; i < num; i ++) {
- char nibbleHighChar = values.charAt(2 * i);
- char nibbleLowChar = values.charAt(2 * i + 1);
- int nibbleHigh = nibbleHighChar > 'a' ? (nibbleHighChar - 'a' + 10)
- : (nibbleHighChar - '0');
- int nibbleLow = nibbleLowChar > 'a' ? (nibbleLowChar - 'a' + 10)
- : (nibbleLowChar - '0');
- array[i] = (byte) ((nibbleHigh & 0x0F) << 4 | (nibbleLow & 0x0F));
- }
+ array = HexEncoding.decode(values);
}
} else if (eventType == parser.END_TAG) {
if (parser.getName().equals(endTag)) {
diff --git a/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java
index 2596ece..27f3596 100644
--- a/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java
+++ b/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java
@@ -16,13 +16,22 @@
package com.android.internal.util;
+import static org.junit.Assert.assertArrayEquals;
+
+import android.util.Xml;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
+
import junit.framework.TestCase;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlSerializer;
+
public class XmlUtilsTest extends TestCase {
// https://code.google.com/p/android/issues/detail?id=63717
@@ -38,4 +47,23 @@
assertEquals("nullValue", deserialized.get(null));
assertEquals("fooValue", deserialized.get("foo"));
}
+
+ public void testreadWriteXmlByteArrayValue() throws Exception {
+ byte[] testByteArray = {0x1 , 0xa, 0xb, 0x9, 0x34, (byte) 0xaa, (byte) 0xba, (byte) 0x99};
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+ XmlSerializer serializer = new FastXmlSerializer();
+ serializer.setOutput(baos, StandardCharsets.UTF_8.name());
+ serializer.startDocument(null, true);
+ XmlUtils.writeValueXml(testByteArray, "testByteArray", serializer);
+ serializer.endDocument();
+
+ InputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ XmlPullParser pullParser = Xml.newPullParser();
+ pullParser.setInput(bais, StandardCharsets.UTF_8.name());
+ String[] name = new String[1];
+ byte[] testByteArrayDeserialized = (byte[]) XmlUtils.readValueXml(pullParser, name);
+ assertEquals("testByteArray", name[0]);
+ assertArrayEquals(testByteArray, testByteArrayDeserialized);
+ }
}