jdk/src/share/classes/javax/smartcardio/ATR.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A Smart Card's answer-to-reset bytes. A Card's ATR object can be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * by calling {@linkplain Card#getATR}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class does not attempt to verify that the ATR encodes a semantically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * valid structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>Instances of this class are immutable. Where data is passed in or out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * via byte arrays, defensive cloning is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see Card#getATR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class ATR implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final long serialVersionUID = 6695383790847736493L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] atr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private transient int startHistorical, nHistorical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Constructs an ATR from a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param atr the byte array containing the answer-to-reset bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @throws NullPointerException if <code>atr</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public ATR(byte[] atr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.atr = atr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private void parse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (atr.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if ((atr[0] != 0x3b) && (atr[0] != 0x3f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int t0 = (atr[1] & 0xf0) >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int n = atr[1] & 0xf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int i = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        while ((t0 != 0) && (i < atr.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if ((t0 & 1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if ((t0 & 2) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if ((t0 & 4) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if ((t0 & 8) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (i >= atr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                t0 = (atr[i++] & 0xf0) >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                t0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        int k = i + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if ((k == atr.length) || (k == atr.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            startHistorical = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            nHistorical = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns a copy of the bytes in this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return a copy of the bytes in this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return atr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns a copy of the historical bytes in this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * If this ATR does not contain historical bytes, an array of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * zero is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return a copy of the historical bytes in this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public byte[] getHistoricalBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        byte[] b = new byte[nHistorical];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.arraycopy(atr, startHistorical, b, 0, nHistorical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns a string representation of this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return a String representation of this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return "ATR: " + atr.length + " bytes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Compares the specified object with this ATR for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns true if the given object is also an ATR and its bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * identical to the bytes in this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param obj the object to be compared for equality with this ATR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return true if the specified object is equal to this ATR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (obj instanceof ATR == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ATR other = (ATR)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return Arrays.equals(this.atr, other.atr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns the hash code value for this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the hash code value for this ATR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return Arrays.hashCode(atr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        atr = (byte[])in.readUnshared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}