001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.pki;
028:
029: import java.io.IOException;
030: import com.sun.satsa.util.*;
031:
032: /**
033: * This class provides interface to WIM file system.
034: */
035: public class WimFileSystem extends FileSystemAbstract {
036:
037: /** Constant defines size of the card buffer */
038: private int CARD_BUFFER = 240;
039: /** Constant defines parameters P1, P2 in the apdu command */
040: private int P1_P2 = 0x0;
041:
042: /**
043: * Constructs new WimFileSystem object.
044: * @param apdu connection to be used by this object.
045: */
046: public WimFileSystem(Connection apdu) {
047: super (apdu);
048: apdu.setCLAbyte((byte) 0x80);
049: }
050:
051: /**
052: * Selects file by ID.
053: * @param id file ID
054: * @throws IOException if IOError occurs
055: */
056: public void select(short id) throws IOException {
057:
058: byte[] data = apdu.resetCommand().putShort(id).sendCommand(
059: INS_SELECT, P1_P2);
060:
061: isEFSelected = false;
062: currentFileSize = 0;
063:
064: if (data.length == 2) {
065: return; // FCI is empty
066: }
067:
068: TLV t; // parse FCI
069: try {
070: t = new TLV(data, 0);
071: } catch (TLVException e) {
072: throw new IOException();
073: }
074:
075: t = t.child;
076: while (t != null) {
077: if (t.type == 0x80) {
078: isEFSelected = true;
079: currentFileSize = Utils.getU2(data, t.valueOffset);
080: return;
081: }
082: t = t.next;
083: }
084: return;
085: }
086:
087: /**
088: * Reads part of selected file.
089: * @param offset the offset into data buffer where data should be
090: * placed
091: * @param length data length
092: * @param fileOffset file data offset
093: * @throws IOException if IO error occurs
094: * @return data byte array of the data
095: */
096: public byte[] readData(int offset, int length, int fileOffset)
097: throws IOException {
098: byte[] data = new byte[length + offset];
099: while (length != 0) {
100: int len = length > CARD_BUFFER ? CARD_BUFFER : length;
101: byte[] result = apdu.resetCommand().sendCommand(INS_READ,
102: fileOffset, len, true);
103: System.arraycopy(result, 0, data, offset, len);
104: offset += len;
105: fileOffset += len;
106: length -= len;
107: }
108: return data;
109: }
110:
111: /**
112: * Writes data into selected file.
113: * @param data buffer that contains data
114: * @param offset offset of data in the buffer
115: * @param length length of data in the buffer
116: * @param fileOffset offset of updated data in the file
117: * @throws IOException if I/O error occurs
118: */
119: public void writeData(byte[] data, int offset, int length,
120: int fileOffset) throws IOException {
121:
122: while (length != 0) {
123: int len = length > CARD_BUFFER ? CARD_BUFFER : length;
124: apdu.resetCommand().putBytes(data, offset, len)
125: .sendCommand(INS_UPDATE, fileOffset, len, true);
126: offset += len;
127: fileOffset += len;
128: length -= len;
129: }
130: }
131:
132: }
|