From 651209b597734d34768fbb69ebf982b483167fd5 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 31 May 2016 08:39:49 -0700 Subject: [PATCH] XmlUtils: Add missing readThisByteArrayXml method Add the missing XML to byte array conversion method. While there, 1. Fix writeByteArrayXml method to store the hex chars of the value. 2. Cleanup couple of error strings in |readThisIntArrayXml| method. BUG: 29039296 Change-Id: I6386f7df7c5c8b7bc3bc5a268196da617209cea9 TEST: Compiles & manual testing. --- .../com/android/internal/util/XmlUtils.java | 84 +++++++++++++++++-- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/core/java/com/android/internal/util/XmlUtils.java b/core/java/com/android/internal/util/XmlUtils.java index 992cb4e51f0f3..83d5ce3bada8b 100644 --- a/core/java/com/android/internal/util/XmlUtils.java +++ b/core/java/com/android/internal/util/XmlUtils.java @@ -392,10 +392,10 @@ public class XmlUtils { StringBuilder sb = new StringBuilder(val.length*2); for (int i=0; i>4; - sb.append(h >= 10 ? ('a'+h-10) : ('0'+h)); - h = b&0xff; - sb.append(h >= 10 ? ('a'+h-10) : ('0'+h)); + 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()); @@ -995,6 +995,73 @@ public class XmlUtils { "Document ended before " + endTag + " end tag"); } + /** + * Read a byte[] object from an XmlPullParser. The XML data could + * previously have been generated by writeByteArrayXml(). The XmlPullParser + * must be positioned after the tag that begins the list. + * + * @param parser The XmlPullParser from which to read the list data. + * @param endTag Name of the tag that will end the list, usually "list". + * @param name An array of one string, used to return the name attribute + * of the list's tag. + * + * @return Returns a newly generated byte[]. + * + * @see #writeByteArrayXml + */ + public static final byte[] readThisByteArrayXml(XmlPullParser parser, + String endTag, String[] name) + throws XmlPullParserException, java.io.IOException { + + int num; + try { + num = Integer.parseInt(parser.getAttributeValue(null, "num")); + } catch (NullPointerException e) { + throw new XmlPullParserException( + "Need num attribute in byte-array"); + } catch (NumberFormatException e) { + throw new XmlPullParserException( + "Not a number in num attribute in byte-array"); + } + + byte[] array = new byte[num]; + + int eventType = parser.getEventType(); + do { + if (eventType == parser.TEXT) { + if (num > 0) { + String values = parser.getText(); + if (values == null || values.length() != num * 2) { + 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)); + } + } + } else if (eventType == parser.END_TAG) { + if (parser.getName().equals(endTag)) { + return array; + } else { + throw new XmlPullParserException( + "Expected " + endTag + " end tag at: " + + parser.getName()); + } + } + eventType = parser.next(); + } while (eventType != parser.END_DOCUMENT); + + throw new XmlPullParserException( + "Document ended before " + endTag + " end tag"); + } + /** * Read an int[] object from an XmlPullParser. The XML data could * previously have been generated by writeIntArrayXml(). The XmlPullParser @@ -1018,10 +1085,10 @@ public class XmlUtils { num = Integer.parseInt(parser.getAttributeValue(null, "num")); } catch (NullPointerException e) { throw new XmlPullParserException( - "Need num attribute in byte-array"); + "Need num attribute in int-array"); } catch (NumberFormatException e) { throw new XmlPullParserException( - "Not a number in num attribute in byte-array"); + "Not a number in num attribute in int-array"); } parser.next(); @@ -1377,6 +1444,11 @@ public class XmlUtils { "Unexpected end of document in "); } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) { // all work already done by readThisPrimitiveValueXml + } else if (tagName.equals("byte-array")) { + res = readThisByteArrayXml(parser, "byte-array", name); + name[0] = valueName; + //System.out.println("Returning value for " + valueName + ": " + res); + return res; } else if (tagName.equals("int-array")) { res = readThisIntArrayXml(parser, "int-array", name); name[0] = valueName;