/*
 * Copyright (c) 2016 Martin Paljak
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package apdu4j;

public class HexUtils {
	// This code has been taken from Apache commons-codec 1.7 (License: Apache 2.0)
	private static final char[] LOWER_HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	@Deprecated
	public static String encodeHexString(final byte[] data) {
		return encodeHexString_imp(data);
	}
	public static String encodeHexString_imp(final byte[] data) {

		final int l = data.length;
		final char[] out = new char[l << 1];
		// two characters form the hex value.
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = LOWER_HEX[(0xF0 & data[i]) >>> 4];
			out[j++] = LOWER_HEX[0x0F & data[i]];
		}
		return new String(out);
	}

	@Deprecated
	public static byte[] decodeHexString(String str) {
		return decodeHexString_imp(str);
	}
	public static byte[] decodeHexString_imp(String str) {
		char data[] = str.toCharArray();
		final int len = data.length;
		if ((len & 0x01) != 0) {
			throw new IllegalArgumentException("Odd number of characters: " + str);
		}
		final byte[] out = new byte[len >> 1];
		// two characters form the hex value.
		for (int i = 0, j = 0; j < len; i++) {
			int f = Character.digit(data[j], 16) << 4;
			j++;
			f = f | Character.digit(data[j], 16);
			j++;
			out[i] = (byte) (f & 0xFF);
		}
		return out;
	}
	// End of copied code from commons-codec
	public static byte[] hex2bin(final String hex) {
		return decodeHexString_imp(hex);
	}

	public static String bin2hex(final byte []bin) {
		return encodeHexString_imp(bin);
	}

	public static byte[] stringToBin(String s) {
		s = s.toLowerCase().replaceAll(" ", "").replaceAll(":", "");
		s = s.replaceAll("0x", "").replaceAll("\n", "").replaceAll("\t", "");
		s = s.replaceAll(";", "");
		return decodeHexString_imp(s);
	}
}

@@ -1262,19 +1262,19 @@
  char%5B%5D 
-LOW
+UPP
 ER_HEX =
@@ -1688,27 +1688,27 @@
 %09out%5Bj++%5D = 
-LOW
+UPP
 ER_HEX%5B(0xF0
@@ -1745,11 +1745,11 @@
 %5D = 
-LOW
+UPP
 ER_H
@@ -2308,53 +2308,229 @@
 %0A%09%09%09
-j++;%0A%09%09%09f = f %7C Character.digit(data%5Bj%5D, 16);
+if (f %3C 0) %7B%0A%09%09%09%09throw new IllegalArgumentException(%22Illegal hex: %22 + data%5Bj%5D);%0A%09%09%09%7D%0A%09%09%09j++;%0A%09%09%09f = f %7C Character.digit(data%5Bj%5D, 16);%0A%09%09%09if (f %3C 0) %7B%0A%09%09%09%09throw new IllegalArgumentException(%22Illegal hex: %22 + data%5Bj%5D);%0A%09%09%09%7D
 %0A%09%09%09
@@ -2866,11 +2866,11 @@
 s.to
-Low
+Upp
 erCa

Reject invalid hex