/**
 * 
 */
package attack.firewall.serverfortypeconfusion;

import javacard.framework.AID;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.ISO7816;
import javacard.framework.APDU;
import javacard.framework.Shareable;
import javacard.framework.Util;
import attack.firewall.fortypeconfusion.SIForTypeConfusion;
import attack.firewall.fortypeconfusion.A;
import attack.firewall.fortypeconfusion.B;
/**
 * @author sauveron
 *
 */


public class ServerForTypeConfusion extends Applet implements SIForTypeConfusion {
	
	
	public byte[] OurbArray;
	
	public short[] OursArray;
	
	public A OurA;
	public A OurA1;
	public B OurB;
	
//	public static byte[] sOurbArray; 

	
/*	
	public void accessArray(short[] array) {
		OursArray = array;
	}
*/
//	public void accessArray(A a) {
//		OurA = a;
//	}
//	
//	public byte[] giveArray() {
//		return OurbArray;
//	}

//	public void accessObj(B obj) {
//		OurB = obj;
//	}

	public void accessObj(byte[] obj) {
		OurbArray = obj;
	}
	
	public A giveObj() {
		return OurA;
	}

	public ServerForTypeConfusion() {
//		 short size = SIForTypeConfusion.SIZE;
//		 OurbArray = new byte[size];
//		 for (short i = (short)0; i < size; i++)
//		 {
//			 OurbArray[i] = (byte)i;
//		 }
		OurA = new A();
		OurA1 = new A();
	}

	public static void install(byte[] bArray, short bOffset, byte bLength) {
		// GP-compliant JavaCard applet registration
		new ServerForTypeConfusion().register(bArray, (short) (bOffset + 1),
				bArray[bOffset]);
	}

	public void process(APDU apdu) {
		// Good practice: Return 9000 on SELECT
		if (selectingApplet()) {
			return;
		}

		byte[] buf = apdu.getBuffer();
		short size = SIForTypeConfusion.SIZE;
		switch (buf[ISO7816.OFFSET_INS]) {
		case (byte) 0x00:
			 for (short i = (short)0; i < size; i++)
			 {
				 Util.setShort(buf, (short)(2*i), OursArray[i]) ;
			 }
			 apdu.setOutgoingAndSend((short)0, (short)(2*size));
			break;
		case (byte) 0x02:
			 for (short i = (short)0; i < size; i++)
			 {
				 buf[(short)i] =  OurbArray[i] ;
			 }
			 apdu.setOutgoingAndSend((short)0, size);
			break;
		case (byte) 0x04:
			 for (short i = (short)0; i < (short)(size/2); i++)
			 {
				 Util.setShort(buf, (short)(2*i), OursArray[i]) ;
			 }
			 apdu.setOutgoingAndSend((short)0, (short)(size));
			break;
		case (byte) 0x06:
			 short offset = (short)(buf[ISO7816.OFFSET_P1]&0x00FF);
			 for (short i = (short)0; i < offset; i++)
			 {
				 Util.setShort(buf, (short)(2*i), OursArray[i]) ;
			 }
			 apdu.setOutgoingAndSend((short)0, (short)(offset*2));
			break;
		case (byte) 0x08:
			 OurA.theSize = (short)(buf[ISO7816.OFFSET_P1]&0x00FF);
			 for (short i = (short)0; i < OurA.theSize; i++)
			 {
				 buf[(short)i] =  OurbArray[i] ;
			 }
			 apdu.setOutgoingAndSend((short)0, OurA.theSize);
			break;
		case (byte) 0x10:
			 short a = (short)(buf[ISO7816.OFFSET_P1]&0x00FF);
			 short bOff = (short)0;
			 if (a>=(short)1)
				 bOff = Util.setShort(buf, bOff, OurB.s1) ;
			 if (a>=(short)2)
				 bOff = Util.setShort(buf, bOff, OurB.s2) ;
			 if (a>=(short)3)
				 bOff = Util.setShort(buf, bOff, OurB.s3) ;
			 if (a>=(short)4)
				 bOff = Util.setShort(buf, bOff, OurB.s4) ;
			 if (a>=(short)5)
				 bOff = Util.setShort(buf, bOff, OurB.s5) ;
			 if (a>=(short)6)
				 bOff = Util.setShort(buf, bOff, OurB.s6) ;
			 if (a>=(short)7)
				 bOff = Util.setShort(buf, bOff, OurB.s7) ;
			 if (a>=(short)8)
				 bOff = Util.setShort(buf, bOff, OurB.s8) ;
			 if (a>=(short)9)
				 bOff = Util.setShort(buf, bOff, OurB.s9) ;
			 if (a>=(short)10)
				 bOff = Util.setShort(buf, bOff, OurB.s10) ;
			 if (a>=(short)11)
				 bOff = Util.setShort(buf, bOff, OurB.s11) ;
			 if (a>=(short)12)
				 bOff = Util.setShort(buf, bOff, OurB.s12) ;
			 if (a>=(short)13)
				 bOff = Util.setShort(buf, bOff, OurB.s13) ;
			 if (a>=(short)14)
				 bOff = Util.setShort(buf, bOff, OurB.s14) ;
			 if (a>=(short)15)
				 bOff = Util.setShort(buf, bOff, OurB.s15) ;
			 if (a>=(short)16)
				 bOff = Util.setShort(buf, bOff, OurB.s16) ;
			 apdu.setOutgoingAndSend((short)0, bOff);
			break;
		case (byte) 0x12:
			 short x = (short)(buf[ISO7816.OFFSET_P1]&0x00FF);
			 for (short i = (short)0; i < x; i++)
			 {
				 buf[(short)i] =  OurbArray[i] ;
			 }
			 apdu.setOutgoingAndSend((short)0, x );
			break;
		default:
			// good practice: If you don't know the INStruction, say so:
			ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
		}
	}

	public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
		// No check;
		return this;
	}
	
	
}