/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common.apdu;

import iso.std.iso_iec._24727.tech.schema.Transmit;
import java.util.ArrayList;
import org.openecard.common.apdu.common.CardCommandAPDU;
import org.openecard.common.util.ByteUtils;
import org.openecard.common.util.ShortUtils;


/**
 *
 * @author Moritz Horsch <horsch@cdc.informatik.tu-darmstadt.de>
 * @author Johannes Schmoelz <johannes.schmoelz@ecsec.de>
 * @author Tobias Wich <tobias.wich@ecsec.de>
 */
public class ReadBinary extends CardCommandAPDU {

    /**
     * READ BINARY command instruction byte. INS = 0xB0
     */
    private static final byte READ_BINARY_INS_1 = (byte) 0xB0;
    /**
     * READ BINARY command instruction byte. INS = 0xB1
     */
    private static final byte READ_BINARY_INS_2 = (byte) 0xB1;

    /**
     * Creates an new READ BINARY APDU.
     */
    public ReadBinary() {
	super(x00, READ_BINARY_INS_1, x00, xFF);
    }

    /**
     * Creates an new READ BINARY APDU.
     *
     * @param offset Offset
     */
    public ReadBinary(byte offset) {
	super(x00, READ_BINARY_INS_1, offset, xFF);
    }

    /**
     * Creates an new READ BINARY APDU.
     *
     * @param offset Offset
     * @param length Expected length
     */
    public ReadBinary(byte offset, byte length) {
	super(x00, READ_BINARY_INS_1, offset, length);
    }

    /**
     * Creates an new READ BINARY APDU.
     * Bit 8 of P1 is set to 1, bits 7 and 6 of P1 are set to 00 (RFU),
     * bits 5 to 1 of P1 encode a short EF identifier and P2 (eight bits)
     * encodes an offset from zero to 255.
     *
     * @param shortFileID Short file identifier
     * @param offset Offset
     * @param length Expected length
     */
    public ReadBinary(byte shortFileID, byte offset, short length) {
	super(x00, READ_BINARY_INS_1, (byte) ((0x1F & shortFileID) | 0x80), offset);
	setLE(length);
    }

    /**
     * Creates an new READ BINARY APDU.
     * Bit 8 of P1 is set to 0, and P1-P2 (fifteen bits) encodes an
     * offset from zero to 32 767.
     *
     * @param offset Offset
     * @param length Expected length
     */
    public ReadBinary(short offset, byte length) {
	setINS(READ_BINARY_INS_1);
	setP1((byte) (((byte) (offset >> 8)) & 0x7F));
	setP2((byte) (offset & xFF));
	setLE(length);
    }

    /**
     * Creates an new READ BINARY APDU.
     * P1-P2 set to '0000' identifies the current EF.
     * The offset data object with tag '54' is encoded in the command data field.
     *
     * @param fileID File identifier
     * @param offset Offset
     * @param length Expected length
     */
    public ReadBinary(short fileID, short offset, short length) {
	super(x00, READ_BINARY_INS_2, x00, x00);

	byte[] content = ShortUtils.toByteArray(offset);
	content = ByteUtils.concatenate((byte) 0x54, content);

	setP1P2(ShortUtils.toByteArray(fileID));
	setData(content);
	setLE(length);
    }

    /**
     * Creates a new Transmit message.
     *
     * @param slotHandle Slot handle
     * @return Transmit
     */
    @Override
    public Transmit makeTransmit(byte[] slotHandle) {
	ArrayList<byte[]> defaultResponses = new ArrayList<byte[]>() {
	    {
		add(new byte[]{(byte) 0x90, (byte) 0x00});
		add(new byte[]{(byte) 0x62, (byte) 0x82});
	    }
	};
	return makeTransmit(slotHandle, defaultResponses);
    }

}

@@ -3143,32 +3143,445 @@
 length);%0A    %7D%0A%0A
+    /**%0A     * Creates an new READ BINARY APDU.%0A     * Bit 8 of P1 is set to 0, and P1-P2 (fifteen bits) encodes an%0A     * offset from zero to 32 767.%0A     *%0A     * @param offset Offset%0A     * @param length Expected length%0A     */%0A    public ReadBinary(short offset, short length) %7B%0A%09setINS(READ_BINARY_INS_1);%0A%09setP1((byte) (((byte) (offset %3E%3E 8)) & 0x7F));%0A%09setP2((byte) (offset & xFF));%0A%09setLE(length);%0A    %7D%0A%0A
     /**%0A     * C

Add constructor which consumes a offset as short value and a length as short value