// Path: core/src/main/java/apdu4j/core/HexUtils.java
// public final class HexUtils {
//     // This code has been taken from Apache commons-codec 1.7 (License: Apache 2.0)
//     private static final char[] UPPER_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++] = UPPER_HEX[(0xF0 & data[i]) >>> 4];
//             out[j++] = UPPER_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.length());
//         }
//         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;
//             if (f < 0) {
//                 throw new IllegalArgumentException("Illegal hex: " + data[j]);
//             }
//             j++;
//             f = f | Character.digit(data[j], 16);
//             if (f < 0) {
//                 throw new IllegalArgumentException("Illegal hex: " + data[j]);
//             }
//             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.toUpperCase().replaceAll(" ", "").replaceAll(":", "");
//         s = s.replaceAll("0X", "").replaceAll("\n", "").replaceAll("\t", "");
//         s = s.replaceAll(";", "");
//         return decodeHexString_imp(s);
//     }
// }


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import apdu4j.core.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
 * Copyright (c) 2015-present Martin Paljak
 * <p>
 * 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:
 * <p>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p>
 * 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.pcsc;



// Construct and parse necessary PC/SC CCID pinpad blocks
public final class PinPadTerminal {
    private static final Logger logger = LoggerFactory.getLogger(PinPadTerminal.class);
    private final Map<FEATURE, Integer> features;
    private boolean display = false;

    // Parse the features into FEATURE -> control code map
    private static Map<FEATURE, Integer> tokenize(byte[] tlv) {
        HashMap<FEATURE, Integer> m = new HashMap<>();

        if (tlv.length % 6 != 0) {
            throw new IllegalArgumentException("Bad response length: " + tlv.length);
        }
        for (int i = 0; i < tlv.length; i += 6) {
            int ft = tlv[i + 0] & 0xFF;
            FEATURE f = FEATURE.fromValue(ft);
            byte[] c = Arrays.copyOfRange(tlv, i + 2, i + 6);
            ByteBuffer buffer = ByteBuffer.wrap(c);
            buffer.order(ByteOrder.BIG_ENDIAN);
            int ci = buffer.getInt();
            m.put(f, ci);
        }
        return m;
    }

    private static void parse_tlv_properties(byte[] tlv) {
        for (int i = 0; i + 2 < tlv.length; ) {
            int t = tlv[i + 0] & 0xFF;
            int l = tlv[i + 1] & 0xFF;
            byte[] v = Arrays.copyOfRange(tlv, i + 2, i + 2 + l);
            i += v.length + 2;
// Path: core/src/main/java/apdu4j/core/HexUtils.java
// public final class HexUtils {
//     // This code has been taken from Apache commons-codec 1.7 (License: Apache 2.0)
//     private static final char[] UPPER_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++] = UPPER_HEX[(0xF0 & data[i]) >>> 4];
//             out[j++] = UPPER_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.length());
//         }
//         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;
//             if (f < 0) {
//                 throw new IllegalArgumentException("Illegal hex: " + data[j]);
//             }
//             j++;
//             f = f | Character.digit(data[j], 16);
//             if (f < 0) {
//                 throw new IllegalArgumentException("Illegal hex: " + data[j]);
//             }
//             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.toUpperCase().replaceAll(" ", "").replaceAll(":", "");
//         s = s.replaceAll("0X", "").replaceAll("\n", "").replaceAll("\t", "");
//         s = s.replaceAll(";", "");
//         return decodeHexString_imp(s);
//     }
// }


// Path: pcsc/src/main/java/apdu4j/pcsc/PinPadTerminal.java
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import apdu4j.core.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Copyright (c) 2015-present Martin Paljak
 * <p>
 * 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:
 * <p>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p>
 * 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.pcsc;



// Construct and parse necessary PC/SC CCID pinpad blocks
public final class PinPadTerminal {
    private static final Logger logger = LoggerFactory.getLogger(PinPadTerminal.class);
    private final Map<FEATURE, Integer> features;
    private boolean display = false;

    // Parse the features into FEATURE -> control code map
    private static Map<FEATURE, Integer> tokenize(byte[] tlv) {
        HashMap<FEATURE, Integer> m = new HashMap<>();

        if (tlv.length % 6 != 0) {
            throw new IllegalArgumentException("Bad response length: " + tlv.length);
        }
        for (int i = 0; i < tlv.length; i += 6) {
            int ft = tlv[i + 0] & 0xFF;
            FEATURE f = FEATURE.fromValue(ft);
            byte[] c = Arrays.copyOfRange(tlv, i + 2, i + 6);
            ByteBuffer buffer = ByteBuffer.wrap(c);
            buffer.order(ByteOrder.BIG_ENDIAN);
            int ci = buffer.getInt();
            m.put(f, ci);
        }
        return m;
    }

    private static void parse_tlv_properties(byte[] tlv) {
        for (int i = 0; i + 2 < tlv.length; ) {
            int t = tlv[i + 0] & 0xFF;
            int l = tlv[i + 1] & 0xFF;
            byte[] v = Arrays.copyOfRange(tlv, i + 2, i + 2 + l);
            i += v.length + 2;
            logger.trace("{}={}", Integer.toHexString(t), HexUtils.bin2hex(v));