Add instruction for storing data

This commit is contained in:
Manuel Thalmann 2023-10-12 20:13:26 +02:00
parent 8f7e657a9c
commit a673b7ecd0
2 changed files with 48 additions and 27 deletions

View file

@ -25,4 +25,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>com.oracle.javacard.jcdk.core.projectbuild.JCDKNature</nature>
</natures>
<filteredResources>
<filter>
<id>1697133027601</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View file

@ -3,7 +3,6 @@
*
*/
package hwb1;
import javacard.framework.*;
@ -16,22 +15,23 @@ import static hwb1.MyAppletStrings.*;
* @author <user>
*/
@StringPool(value = {
@StringDef(name = "Package", value = "hwb1"),
@StringDef(name = "AppletName", value = "MyApplet"),
@StringDef(name = "AuthorName", value = "Manuel")},
// Insert your strings here
name = "MyAppletStrings")
@StringDef(name = "Package", value = "hwb1"),
@StringDef(name = "AppletName", value = "MyApplet"),
@StringDef(name = "AuthorName", value = "Manuel") },
// Insert your strings here
name = "MyAppletStrings")
public class MyApplet extends Applet {
private byte[] storage = null;
/**
* Installs this applet.
*
* @param bArray
* the array containing installation parameters
* the array containing installation parameters
* @param bOffset
* the starting offset in bArray
* the starting offset in bArray
* @param bLength
* the length in bytes of the parameter data in bArray
* the length in bytes of the parameter data in bArray
*/
public static void install(byte[] bArray, short bOffset, byte bLength) {
new MyApplet();
@ -49,26 +49,36 @@ public class MyApplet extends Applet {
*
* @see APDU
* @param apdu
* the incoming APDU
* the incoming APDU
*/
@Override
public void process(APDU apdu) {
short responseLength;
byte[] buffer = apdu.getBuffer();
short length;
byte[] buffer = apdu.getBuffer();
byte instruction = buffer[ISO7816.OFFSET_INS];
byte instruction = buffer[ISO7816.OFFSET_INS];
switch (instruction) {
case 0x00:
responseLength = apdu.setOutgoing();
switch (instruction) {
case 0x00:
length = apdu.setOutgoing();
if (responseLength > MyAppletStrings.AuthorName.length) {
responseLength = (short)MyAppletStrings.AuthorName.length;
}
if (length > AuthorName.length) {
length = (short)AuthorName.length;
}
apdu.setOutgoingLength(responseLength);
apdu.sendBytesLong(MyAppletStrings.AuthorName, (short)0, responseLength);
break;
}
apdu.setOutgoingLength(length);
apdu.sendBytesLong(AuthorName, (short)0, length);
break;
case 0x02:
length = apdu.setIncomingAndReceive();
if (length > 20) {
// ToDo: Handle too much data.
}
else {
Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, storage, (short)0, length);
}
break;
}
}
}