Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

The Class of Util Sample Code

Applets Development Guide

Moderator: product

User avatar
JavaCardOS
Posts: 273
Joined: Thu Apr 30, 2015 12:00 pm
Points :2403
Contact:

The Class of Util Sample Code

Post by 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  Utility.java
 * @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())
      {
         return;
      }
      //Get the apdu buffer datas
      byte[] buf = apdu.getBuffer();
      byte ins = buf[ISO7816.OFFSET_INS];
      
      switch (ins)
      {
      case (byte)INS_ARRAY_COPY_APDU:
         apdu.setIncomingAndReceive();
            //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);
         break;
         
      case INS_ARRAY_COMPARE_APDU:
         //Calling this method indicates that this APDU has incoming data.
         apdu.setIncomingAndReceive();
      
         //Get the incoming data length(Lc).
         short lc = apdu.getIncomingLength();
         
            if (lc != 0x04)
            {
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
            }
            //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;
         }
         else
         {
            buf[0] = (byte)2;
         }
         apdu.setOutgoingAndSend((short)0, (short)1);
         break;
         
      case INS_SHORT_VALUE_APDU:
         apdu.setIncomingAndReceive();
         //Get the incoming data length(Lc).
         short len = apdu.getIncomingLength();
         
            if (len != 0x02)
            {
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
            }
            //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)
            {
               ISOException.throwIt(SW_DATA_CONVERT_ERROR);
            }
           
            //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);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }
}




Note:
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.

Who is online

Users browsing this forum: No registered users and 8 guests

JavaCard OS : Disclaimer