The Class of Util Sample Code

Applets Development Guide

Moderator: product

User avatar
Posts: 253
Joined: Thu Apr 30, 2015 12:00 pm
Points: 2221

The Class of Util Sample Code

Postby JavaCardOS » Sat Jun 25, 2016 5:55 am

The following code is the Class of Util Sample Code in JavaCard API Specification.

You can copy and use it directly or you can download the JCIDE project from the attachment and build it.

Code: Select all

 * @file
 * @brief The Class of Util Sample Code in JavaCard API Specification
 * @comment The purpose of this example is only used to show the usage of API functions and there is no practical significance.
 * @copyright Copyright(C) 2016 JavaCardOS Technologies Co., Ltd. All rights reserved.
package JavaCardOS.Sample.Utility;

import javacard.framework.*;

public class Utility extends Applet
   public static final byte INS_ARRAY_COPY_APDU     = 0x01;
   public static final byte INS_ARRAY_COMPARE_APDU  = 0x02;
   public static final byte INS_SHORT_VALUE_APDU     = 0x03;
   //Redefine the SW1,SW2 error codes
   short SW_DATA_CONVERT_ERROR = 0x6F80;
   public static void install(byte[] bArray, short bOffset, byte bLength)
      new Utility().register(bArray, (short) (bOffset + 1), bArray[bOffset]);

   public void process(APDU apdu)
      if (selectingApplet())
      //Get the apdu buffer datas
      byte[] buf = apdu.getBuffer();
      byte ins = buf[ISO7816.OFFSET_INS];
      switch (ins)
      case (byte)INS_ARRAY_COPY_APDU:
            //Create a transient byte array with the specified array length
            byte[] tmpMemory = JCSystem.makeTransientByteArray((short)10, JCSystem.CLEAR_ON_DESELECT);
            //Fill the byte array 'tmpMemory' (non-atomically) with the value 0x66, beginning from the 0 position, the filling length is 10.
            Util.arrayFillNonAtomic(tmpMemory, (short)0, (short)10, (byte)0x66);
            //Copy an array from the 'tmpMemory' array to APDU Buffer.
            Util.arrayCopy(tmpMemory, (short)0, buf, (short)0, (short)10);
            //Send the length of 10 bytes from the APDU buffer
         apdu.setOutgoingAndSend((short)0, (short)10);
         //Calling this method indicates that this APDU has incoming data.
         //Get the incoming data length(Lc).
         short lc = apdu.getIncomingLength();
            if (lc != 0x04)
            //Define a byte array named 'cmpArray'
         byte[] cmpArray = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04};
         //Compare an array from the specified source array,
         byte cmpResult = Util.arrayCompare(cmpArray, (short)0, buf, (short)ISO7816.OFFSET_CDATA, (short)4);
         //if the comparison of the results is same, set the value of 'buf' byte array to "01", if not, set to "02"
         if (cmpResult == (byte)0)
            buf[0] = (byte)1;
            buf[0] = (byte)2;
         apdu.setOutgoingAndSend((short)0, (short)1);
         //Get the incoming data length(Lc).
         short len = apdu.getIncomingLength();
            if (len != 0x02)
            //Concatenates the two parameter bytes to form a short value.
            short short_value1 = Util.makeShort(buf[ISO7816.OFFSET_CDATA], buf[ISO7816.OFFSET_CDATA+1]);
            //Concatenates two bytes in a byte array to form a short value.
            short short_value2 = Util.getShort(buf, ISO7816.OFFSET_CDATA);
            //compare the result value that two methods converted
            if (short_value1 != short_value2)
            //Deposits the short value as two successive bytes at the specified offset in the byte array.
            Util.setShort(buf, (short)0, short_value1);
            apdu.setOutgoingAndSend((short)0, (short)2);

The purpose of this example is only used to show the usage of API functions and there is no practical significance.
You do not have the required permissions to view the files attached to this post. Please login first.

Return to “Applets Development Guide”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

JavaCard OS : Disclaimer