Reputation: 697
Below, you see a program that is written to return the EEPROM available memory on reception of any APDU command :
public class HelloWorld extends Applet {
public static void install(byte[] bArray, short bOffset, byte bLength) {
new helloWorld.HelloWorld().register(bArray, (short) (bOffset + 1),
bArray[bOffset]);
}
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
getAvailableMem(apdu);
}
public void getAvailableMem(APDU apdu){
byte[] buffer = apdu.getBuffer();
short[] AvailableMem = {};
UtilX.getAvailableMemoryInfo(AvailableMem, (short) 0,
UtilX.MEMORY_TYPE_PERSISTENT);
for(short i=0;i<(short)AvailableMem.length;i++){
buffer[i]=(byte)AvailableMem[i];
}
apdu.setOutgoingAndSend((short)0, (short)(2*AvailableMem.length));
}
}
But when I send an APDU command to the applet, it returns 0x6F00
:
OSC: opensc-tool -s 00a4040006010203040506 -s 00000000
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 06
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 00 00
Received (SW1=0x6F, SW2=0x00)
Note that I tried the above program with the below loop also :
for(byte i=0;i<(byte)AvailableMem.length;i++){
buffer[i]=(byte)AvailableMem[i];
}
It compiled successfully also! Shouldn't the compiler restrict the type of array indexes to byte
only or short
only? Why it accepts both types for indexes?
-This second program has the same output in OpenSCTool. What's wrong?
Upvotes: 2
Views: 314
Reputation: 2647
Example code taken out of one of my applets:
Declarations in the beginning of my class
short[] mem = new short[2];
From inside the process tree:
case Cmd.GET_FREE_MEM:
UtilX.getAvailableMemoryInfo(mem, (short) 0, UtilX.MEMORY_TYPE_PERSISTENT);
Util.setShort(apdu.getBuffer(), (short) 0, mem[0]);
Util.setShort(apdu.getBuffer(), (short) 2, mem[1]);
UtilX.getAvailableMemoryInfo(mem, (short) 0, UtilX.MEMORY_TYPE_TRANSIENT_RESET);
Util.setShort(apdu.getBuffer(), (short) 4, mem[0]);
Util.setShort(apdu.getBuffer(), (short) 6, mem[1]);
apdu.setOutgoingAndSend((short) 0, (short) 8);
break;
Upvotes: 0
Reputation: 5651
You have to store the result into an existing array of non-zero length:
class MyApplet extends Applet {
short[] shorts = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_RESET);
public void process(APDU apdu) {
//...
UtilX.getAvailableMemoryInfo(shorts, (short)0, UtilX.MEMORY_TYPE_PERSISTENT);
//...
}
Your status code 6F00
is an uncaught instance of ArrayIndexOutOfBoundsException
.
You can use both integer types as an index: byte
or short
, there is no real difference (for arrays not longer than 0x7F, of course).
Upvotes: 2