Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f27d080d authored by Dan Cashman's avatar Dan Cashman
Browse files

ByteStringUtils: fix hex parsing to include lowercase input.

fromHexToByteArray() should be able to handle any hex input, not
just input specified as upper-case.

Bug: 73001469
Test: Builds, boots.
Change-Id: I89dcbaeb95d6fc540b532d897ff27fb5f40381c7
parent 1c12690f
Loading
Loading
Loading
Loading
+47 −45
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@ package android.util;
 * @hide
 */
public final class ByteStringUtils {
  private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    private static final char[] HEX_LOWERCASE_ARRAY = "0123456789abcdef".toCharArray();
    private static final char[] HEX_UPPERCASE_ARRAY = "0123456789ABCDEF".toCharArray();

    private ByteStringUtils() {
    /* hide constructor */
@@ -44,8 +45,8 @@ public final class ByteStringUtils {

        for (int i = 0; i < byteLength; i++) {
            final int byteHex = bytes[i] & 0xFF;
      chars[i * 2] = HEX_ARRAY[byteHex >>> 4];
      chars[i * 2 + 1] = HEX_ARRAY[byteHex & 0x0F];
            chars[i * 2] = HEX_UPPERCASE_ARRAY[byteHex >>> 4];
            chars[i * 2 + 1] = HEX_UPPERCASE_ARRAY[byteHex & 0x0F];
        }
        return new String(chars);
    }
@@ -66,14 +67,15 @@ public final class ByteStringUtils {

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] =
          (byte)(((getIndex(chars[i * 2]) << 4) & 0xF0) | (getIndex(chars[i * 2 + 1]) & 0x0F));
                    (byte) (((getIndex(chars[i * 2]) << 4) & 0xF0)
                            | (getIndex(chars[i * 2 + 1]) & 0x0F));
        }
        return bytes;
    }

    private static int getIndex(char c) {
    for (int i = 0; i < HEX_ARRAY.length; i++) {
      if (HEX_ARRAY[i] == c) {
        for (int i = 0; i < HEX_UPPERCASE_ARRAY.length; i++) {
            if (HEX_UPPERCASE_ARRAY[i] == c || HEX_LOWERCASE_ARRAY[i] == c) {
                return i;
            }
        }