This discussion is archived
8 Replies Latest reply: Feb 8, 2013 2:47 AM by fgrieu RSS

Java card applet space

965207 Newbie
Currently Being Moderated
I am working on a project related to java card applet development. My project consists of 4 java classes which are used to store date of created objects, and a java card applet which contains method that enables me to de some kind of actions on the created objects, such as create a new object of a specified class or edited it and son on.

Now I have 2 classes done ; first one is called Odemeler which contains variables of type byte and the second one is called SadakatUygulamalar which contains   variables.

Inside the class I have defined 2 arrays, one of type Odemeler and the other of type SadakatUygulamalar and I set their size to be 200. I have tried to create 100 object of kind Odemeler and to store them in the array
but the applet is able to store up to 92 object, at the other hand after creating those objects and storing them I am not able to store objects of type SadakatUygulamalar.
Is this problem related to the applet size or what ?? any help please

The applet code is here :
package sTez_1;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;

public class TestApplet1 extends Applet {

     private static Odeme[] odemeArray;
     private static SadakatUygulamalar[] uygulamaArray;
     private static short index = 00;
     private static short index2 = 00;
     
     private static byte[] result;
     private static byte[] result2;
     private static short res_index =00;
     private static short res2_index =00;
     private static short FirmaIDFlag = 0;

     private static final byte ODEME_CLA = (byte) 0xB0;
     private static final byte UYGULAMA_CLA = (byte) 0xA0;
     private static final byte ODEME_INS_CREATE_PROFILE = (byte) 0x10;
     private static final byte UYGULAMA_INS_CREATE_PROFILE = (byte) 0x20;
     private static final byte ODEME_INS_GET_PROFILE = (byte) 0x30;
     private static final byte ODEME_INS_LIST_SOME = (byte) 0x35;
     private static final byte ODEME_INS_VIEW_PROFILE = (byte) 0x37;
     private static final byte ODEME_INS_GET_PUAN_VIA_FIRMA = (byte) 0x38;
     private static final byte ODEME_INS_GET_ALISVERIS_VIA_KAT = (byte) 0x39;
     private static final byte UYGULAMA_INS_GET_PROFILE = (byte) 0x40;
     private static final byte UYGULAMA_INS_LIST_ALL = (byte) 0x50;
     private static final byte UYGULAMA_INS_UPDATE_ID = (byte) 0x60;
     private static final byte UYGULAMA_INS_UPDATE_NAME = (byte) 0x80;
     private static final byte UYGULAMA_INS_SEARCH = (byte) 0x90;
     private static final byte SPLIT_PARTS = (byte) 0x2C;
     

     private static final short CLA_ERROR = (short) 0x13;
     private static final short INS_ERROR = (short) 0x14;
     private static final short NO_SUCH_INDEX = (short) 0x15;
     private static final short NO_DATA_FOUND = (short) 0x16;
     private static final short FIRMA_ID_IN_USE = (short) 0x9C;
     private static final short ODEME_ARRAY_FULL = (short) 0x18;
     
     
     private TestApplet1() {

          odemeArray = new Odeme[(short) 250];
          uygulamaArray = new SadakatUygulamalar[(short) 200];
          register();
     }

     public static void install(byte bArray[], short bOffset, byte bLength)
               throws ISOException {
          new TestApplet1();
     }

     public void process(APDU apdu) throws ISOException {
          // TODO Auto-generated method stub

          if (selectingApplet())
               return;
          byte[] buffer = apdu.getBuffer();
          byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
          switch (INS) {
          case ODEME_INS_CREATE_PROFILE:
               odemeCreateProfile(apdu);
               break;
          case UYGULAMA_INS_CREATE_PROFILE:
               uygulamaCreateProfile(apdu);
               break;
          case ODEME_INS_GET_PROFILE:
               odemeGetProfile(apdu);
               break;
          case ODEME_INS_LIST_SOME:
               odemeListSome(apdu);
               break;
          case ODEME_INS_VIEW_PROFILE:
               odemeViewProfile(apdu);
               break;
          case ODEME_INS_GET_PUAN_VIA_FIRMA:
               odemeGetPuanByFirma(apdu);
               break;
          case ODEME_INS_GET_ALISVERIS_VIA_KAT:
               odemeGetAlisverisByKat(apdu);
               break;
          case UYGULAMA_INS_GET_PROFILE:
               uygulamaGetProfile(apdu);
               break;
          case UYGULAMA_INS_UPDATE_ID:
               uygulamaUpdateID(apdu);
               break;
          case UYGULAMA_INS_UPDATE_NAME:
               uygulamaUpdateName(apdu);
               break;
          case UYGULAMA_INS_LIST_ALL:
               uygulamaListAll(apdu);
               break;     
          case UYGULAMA_INS_SEARCH:
               uygulamaSearch(apdu);
               break;

          default:
               ISOException.throwIt(INS_ERROR);

          }

     }

     public void odemeCreateProfile(APDU apdu) {
          
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];

          JCSystem.beginTransaction();
          short fieldIndex = 0;
          short field1counter = 0;
          short field2counter = 0;
          short field3counter = 0;
          short field4counter = 0;
          short field5counter = 0;
          short field6counter = 0;
          short field7counter = 0;
          short field8counter = 0;
          short field9counter = 0;
          short field10counter = 0;
          short field11counter = 0;
          short field12counter = 0;
          short field13counter = 0;
          short field14counter = 0;
          for (short i = 0; i < lc; i++) {
               fieldIndex++;
               field1counter++;
               if (buffer[(short) (5 + i)] == SPLIT_PARTS)
                    i = lc;

          }

          for (short j = fieldIndex; j < lc; j++) {
               fieldIndex++;
               field2counter++;
               if (buffer[(short) (5 + j)] == SPLIT_PARTS)
                    j = lc;
          }

          for (short k = fieldIndex; k < lc; k++) {
               fieldIndex++;
               field3counter++;
               if (buffer[(short) (5 + k)] == SPLIT_PARTS)
                    k = lc;
          }
          
          for (short h = fieldIndex; h < lc; h++) {
               fieldIndex++;
               field4counter++;
               if (buffer[(short) (5 + h)] == SPLIT_PARTS)
                    h = lc;
          }
          
          for (short u = fieldIndex; u < lc; u++) {
               fieldIndex++;
               field5counter++;
               if (buffer[(short) (5 + u)] == SPLIT_PARTS)
                    u = lc;
          }
          
          for (short u1 = fieldIndex; u1 < lc; u1++) {
               fieldIndex++;
               field6counter++;
               if (buffer[(short) (5 + u1)] == SPLIT_PARTS)
                    u1 = lc;
          }
          
          for (short u2 = fieldIndex; u2 < lc; u2++) {
               fieldIndex++;
               field7counter++;
               if (buffer[(short) (5 + u2)] == SPLIT_PARTS)
                    u2 = lc;
          }
          
          for (short u3 = fieldIndex; u3 < lc; u3++) {
               fieldIndex++;
               field8counter++;
               if (buffer[(short) (5 + u3)] == SPLIT_PARTS)
                    u3 = lc;
          }
          
          for (short u4 = fieldIndex; u4 < lc; u4++) {
               fieldIndex++;
               field9counter++;
               if (buffer[(short) (5 + u4)] == SPLIT_PARTS)
                    u4 = lc;
          }
          
          for (short u5 = fieldIndex; u5 < lc; u5++) {
               fieldIndex++;
               field10counter++;
               if (buffer[(short) (5 + u5)] == SPLIT_PARTS)
                    u5 = lc;
          }
          
          for (short u6 = fieldIndex; u6 < lc; u6++) {
               fieldIndex++;
               field11counter++;
               if (buffer[(short) (5 + u6)] == SPLIT_PARTS)
                    u6 = lc;
          }
          
          for (short u7 = fieldIndex; u7 < lc; u7++) {
               fieldIndex++;
               field12counter++;
               if (buffer[(short) (5 + u7)] == SPLIT_PARTS)
                    u7 = lc;
          }
          
          for (short u8 = fieldIndex; u8 < lc; u8++) {
               fieldIndex++;
               field13counter++;
               if (buffer[(short) (5 + u8)] == SPLIT_PARTS)
                    u8 = lc;
          }
          
          for (short u9 = fieldIndex; u9 < lc; u9++) {
               fieldIndex++;
               field14counter++;
               if (buffer[(short) (5 + u9)] == SPLIT_PARTS)
                    u9 = lc;
          }
          short cc = 5;

          byte[] _id = new byte[field1counter];
          for (short i1 = 0; i1 < (short) _id.length; i1++)
               _id[i1] = buffer[(short) (cc + i1)];
        cc = (short)(cc + field1counter);
        
          byte[] _tarih = new byte[field2counter];
          for (short j1 = 0; j1 < (short) _tarih.length; j1++)
               _tarih[j1] = buffer[(short) (cc + j1)];
          cc = (short)(cc + field2counter);
          
          byte[] _saat = new byte[field3counter];
          for (short j2 = 0; j2 < (short) _saat.length; j2++)
               _saat[j2] = buffer[(short) (cc + j2)];
          cc = (short)(cc + field3counter);   
          
          byte[] _firma = new byte[field4counter];
          for (short j3 = 0; j3 < (short) _firma.length; j3++)
               _firma[j3] = buffer[(short) (cc + j3)];
          cc = (short)(cc + field4counter); 
          
          byte[] _kategori = new byte[field5counter];
          for (short j4 = 0; j4 < (short) _kategori.length; j4++)
               _kategori[j4] = buffer[(short) (cc + j4)];
          cc = (short)(cc + field5counter); 
          
          byte[] _tutar = new byte[field6counter];
          for (short j5 = 0; j5 < (short) _tutar.length; j5++)
               _tutar[j5] = buffer[(short) (cc + j5)];
          cc = (short)(cc + field6counter); 
          
          byte[] _birim = new byte[field7counter];
          for (short j6 = 0; j6 < (short) _birim.length; j6++)
               _birim[j6] = buffer[(short) (cc + j6)];
          cc = (short)(cc + field7counter);
          
          byte[] _ulke = new byte[field8counter];
          for (short j7 = 0; j7 < (short) _ulke.length; j7++)
               _ulke[j7] = buffer[(short) (cc + j7)];
          cc = (short)(cc + field8counter); 
          
          byte[] _sehir = new byte[field9counter];
          for (short j8 = 0; j8 < (short) _sehir.length; j8++)
               _sehir[j8] = buffer[(short) (cc + j8)];
          cc = (short)(cc + field9counter); 
          
          byte[] _ilce = new byte[field10counter];
          for (short j9 = 0; j9 < (short) _ilce.length; j9++)
               _ilce[j9] = buffer[(short) (cc + j9)];
          cc = (short)(cc + field10counter); 
          
          byte[] _aid = new byte[field11counter];
          for (short j10 = 0; j10 < (short) _aid.length; j10++)
               _aid[j10] = buffer[(short) (cc + j10)];
          cc = (short)(cc + field11counter); 
          
          byte[] _banka = new byte[field12counter];
          for (short j11 = 0; j11 < (short) _banka.length; j11++)
               _banka[j11] = buffer[(short) (cc + j11)];
          cc = (short)(cc + field12counter); 
          
          byte[] _taksit = new byte[field13counter];
          for (short j12 = 0; j12 < (short) _taksit.length; j12++)
               _taksit[j12] = buffer[(short) (cc + j12)];
          cc = (short)(cc + field13counter); 
          
          byte[] _puan = new byte[field14counter];
          for (short j13 = 0; j13 < (short) _puan.length; j13++)
               _puan[j13] = buffer[(short) (cc + j13)];
          cc = (short)(cc + field3counter); 
          
          Odeme m = new Odeme(_id, _tarih, _saat, _firma, _kategori, _tutar, _birim, _ulke, _sehir, _ilce, _aid, _banka, _taksit, _puan);
          if(index2 >= (short)odemeArray.length)
               ISOException.throwIt(ODEME_ARRAY_FULL);
          odemeArray[index2] = m;
          index2++;
          
          JCSystem.commitTransaction();
          buffer[0] = (byte)index2;
          apdu.setOutgoingAndSend((short) 0, (short) 1);
     }

     public void odemeGetProfile(APDU apdu) {
          
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          byte[] sent_id = new byte[lc];
          for (short i5 = 0; i5 < lc; i5++)
               sent_id[i5] = buffer[(short) (5 + i5)];
          
          for (short h = 0; h < index2; h++) {
               Odeme m = odemeArray[h];
               byte[] _id = m.getID();
               if (equals(sent_id, _id)) {
                    byte[] banka = m.getBanka();
                    byte[] sehir = m.getSehir();
                    byte[] firma = m.getFirma();
                    short counter = 0;

                    for (short i = 0; i < (short) banka.length; i++)
                         buffer[i] = banka;
                    counter = (short) banka.length;

                    for (short j = 0; j < (short) sehir.length; j++)
                         buffer[(short) (counter + j)] = sehir[j];
                    counter = (short) (counter + sehir.length);

                    for (short j = 0; j < (short) firma.length; j++)
                         buffer[(short) (counter + j)] = firma[j];
                    counter = (short) (counter + firma.length);
                    
                    apdu.setOutgoingAndSend((short) 0, counter);

               }
          }
     }
     
     public void odemeGetPuanByFirma(APDU apdu)
     {
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          byte[] puan = null;
          byte[] res = new byte[200];
          byte[] fname = new byte[lc];
          for(short t = 0; t < lc; t++)
               fname[t] = buffer[(short)(t + 5)];
          short counter = 0;
          for(short z = 0; z < index2 ; z++)
          {
               Odeme m = odemeArray[z];
               byte[] _name = m.getFirma();
               if (equals(fname, _name)) {
                    puan = m.getKazanilanpuan();
                    Util.arrayCopy(puan, (short)0, res, counter, (short)puan.length);
                    counter = (short)(counter + puan.length);
                    res[counter] = (byte)0x7C;
                    counter++;
               }
               
          }
          
          Util.arrayCopy(res, (short)0, buffer, (short)0, (short)counter);
          apdu.setOutgoingAndSend((short)0, (short)counter);
          
     }
     
     public void odemeGetAlisverisByKat(APDU apdu)
     {
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          byte[] alisveris = null;
          byte[] res = new byte[200];
          byte[] kat = new byte[lc];
          for(short t = 0; t < lc; t++)
               kat[t] = buffer[(short)(t + 5)];
          short counter = 0;
          for(short z = 0; z < index2 ; z++)
          {
               Odeme m = odemeArray[z];
               byte[] _kategori = m.getKategori();
               if (equals(kat, _kategori)) {
                    alisveris = m.getTutar();
                    Util.arrayCopy(alisveris, (short)0, res, counter, (short)alisveris.length);
                    counter = (short)(counter + alisveris.length);
                    res[counter] = (byte)0x7C;
                    counter++;
               }
               
          }
          
          Util.arrayCopy(res, (short)0, buffer, (short)0, (short)counter);
          apdu.setOutgoingAndSend((short)0, (short)counter);
          
     }
     
     public void odemeListSome(APDU apdu)
     {
          
          byte[] buffer = apdu.getBuffer();
          result2 = new byte[(short) 200];
          byte[] id = null, sehir = null, _banka = null;
          if(index2 == 0)
               ISOException.throwIt(NO_DATA_FOUND);
          for(short r = 0; r < index2; r++)
          {
               Odeme m = odemeArray[r];
               _id = m.getID();
               _sehir = m.getSehir();
               _banka = m.getBanka();
               copyData2(_id, sehir, banka);
               
          }
          Util.arrayCopy(result2, (short)0, buffer, (short)0, res2_index);
          result2 = null;
          short len = res2_index;
          res2_index = 00;
          apdu.setOutgoingAndSend((short)0, len);
          
     }
     
     public void odemeViewProfile(APDU apdu)
     {
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          byte[] id = new byte[lc];
          for (short j3 = 0; j3 < lc; j3++)
               id[j3] = buffer[(short) (5 + j3)];
          for (short h = 0; h < index2; h++) {
               Odeme m = odemeArray[h];
               byte[] H_id = m.getID();
               if (equals(id, H_id)) {

                    byte[] tarih = m.getTarih();
                    byte[] saat = m.getSaat();
                    byte[] firma = m.getFirma();
                    byte[] kategori = m.getKategori();
                    byte[] tutar = m.getTutar();
                    byte[] birim = m.getParabirimi();
                    byte[] ulke = m.getUlke();
                    byte[] sehir = m.getSehir();
                    byte[] ilce = m.getIlce();
                    byte[] aid = m.getAID();
                    byte[] banka = m.getBanka();
                    byte[] taksit = m.getTaksitsayisi();
                    byte[] puan = m.getKazanilanpuan();

                    short counter1 = 0;
                    Util.arrayCopy(tarih, (short)0, buffer, (short)0, (short)tarih.length);
                    counter1 = (short)(counter1 + tarih.length);
               
                    Util.arrayCopy(saat, (short)0, buffer, counter1, (short)saat.length);
                    counter1 = (short)(counter1 + saat.length);
                    
                    Util.arrayCopy(firma, (short)0, buffer, counter1, (short)firma.length);
                    counter1 = (short)(counter1 + firma.length);
                    
                    Util.arrayCopy(kategori, (short)0, buffer, counter1, (short)kategori.length);
                    counter1 = (short)(counter1 + kategori.length);
                    
                    Util.arrayCopy(tutar, (short)0, buffer, counter1, (short)tutar.length);
                    counter1 = (short)(counter1 + tutar.length);
                    
                    Util.arrayCopy(birim, (short)0, buffer, counter1, (short)birim.length);
                    counter1 = (short)(counter1 + birim.length);
                    
                    Util.arrayCopy(ulke, (short)0, buffer, counter1, (short)ulke.length);
                    counter1 = (short)(counter1 + ulke.length);
                    
                    Util.arrayCopy(sehir, (short)0, buffer, counter1, (short)sehir.length);
                    counter1 = (short)(counter1 + sehir.length);
                    
                    Util.arrayCopy(ilce, (short)0, buffer, counter1, (short)ilce.length);
                    counter1 = (short)(counter1 + ilce.length);
                    
                    Util.arrayCopy(aid, (short)0, buffer, counter1, (short)aid.length);
                    counter1 = (short)(counter1 + aid.length);
                    
                    Util.arrayCopy(banka, (short)0, buffer, counter1, (short)banka.length);
                    counter1 = (short)(counter1 + banka.length);
                    
                    Util.arrayCopy(taksit, (short)0, buffer, counter1, (short)taksit.length);
                    counter1 = (short)(counter1 + taksit.length);
                    
Util.arrayCopy(puan, (short)0, buffer, counter1, (short)puan.length);
counter1 = (short)(counter1 + puan.length);

                    apdu.setOutgoingAndSend((short) 0, counter1);

               }
          }
     }

     public void uygulamaCreateProfile(APDU apdu) {
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          // JCSystem.beginTransaction();

          JCSystem.beginTransaction();
          short fieldIndex = 0;
          short field1counter = 0;
          short field2counter = 0;
          short field3counter = 0;
          // byte test;
          for (short i = 0; i < lc; i++) {
               fieldIndex++;
               field1counter++;
               if (buffer[(short) (5 + i)] == SPLIT_PARTS)
                    i = lc;

          }

          for (short j = fieldIndex; j < lc; j++) {
               fieldIndex++;
               field2counter++;
               if (buffer[(short) (5 + j)] == SPLIT_PARTS)
                    j = lc;
          }

          for (short k = fieldIndex; k < lc; k++) {
               fieldIndex++;
               field3counter++;
               if (buffer[(short) (5 + k)] == SPLIT_PARTS)
                    k = lc;
          }

          byte[] uygulama_id = new byte[field1counter];
          for (short i1 = 0; i1 < (short) uygulama_id.length; i1++)
               uygulama_id[i1] = buffer[(short) (5 + i1)];

          byte[] firma_id = new byte[field2counter];
          for (short j1 = 0; j1 < (short) firma_id.length; j1++)
               firma_id[j1] = buffer[(short) (5 + field1counter + j1)];
getFirmaIDFlag(firma_id);
if(FirmaIDFlag == 1)
     ISOException.throwIt(FIRMA_ID_IN_USE);
          
          byte[] firma_ismi = new byte[field3counter];
          for (short j2 = 0; j2 < (short) firma_ismi.length; j2++)
               firma_ismi[j2] = buffer[(short) (5 + field1counter + field2counter + j2)];

          SadakatUygulamalar su = new SadakatUygulamalar(uygulama_id, firma_id,
                    firma_ismi);
          uygulamaArray[index] = su;
          index++;

          JCSystem.commitTransaction();
          buffer[0] = (byte) index;
          apdu.setOutgoingAndSend((short) 0, (short) 1);

     }

     public void uygulamaGetProfile(APDU apdu) {
          byte[] buffer = apdu.getBuffer();
          short p1 = (short) (buffer[ISO7816.OFFSET_P1]);
          if (p1 >= index)
               ISOException.throwIt(NO_SUCH_INDEX);
          SadakatUygulamalar su = uygulamaArray[p1];
          byte[] uygulama_id = su.getUygulama_id();
          byte[] firma_id = su.getFirma_id();
          byte[] firma_ismi = su.getFirma_ismi();
          short counter = 0;

          for (short i = 0; i < (short) uygulama_id.length; i++)
               buffer[i] = uygulama_id[i];
          counter = (short) uygulama_id.length;

          for (short j = 0; j < (short) firma_id.length; j++)
               buffer[(short) (counter + j)] = firma_id[j];
          counter = (short) (counter + firma_id.length);

          for (short j = 0; j < (short) firma_ismi.length; j++)
               buffer[(short) (counter + j)] = firma_ismi[j];
          counter = (short) (counter + firma_ismi.length);

          apdu.setOutgoingAndSend((short) 0, counter);

     }

     public void uygulamaUpdateID(APDU apdu) {
          byte[] buffer = apdu.getBuffer();
          short p1 = (short) (buffer[ISO7816.OFFSET_P1]);
          if (p1 >= index)
               ISOException.throwIt(NO_SUCH_INDEX);
          JCSystem.beginTransaction();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          SadakatUygulamalar su = uygulamaArray[p1];
          byte[] newFirmaID = new byte[lc];
          for (short i = 0; i < lc; i++)
               newFirmaID[i] = buffer[(short) (5 + i)];
          su.setFirma_id(newFirmaID);
          JCSystem.commitTransaction();

          byte[] currentFirmaID = su.getFirma_id();
          for (short k = 0; k < (short) currentFirmaID.length; k++)
               buffer[k] = currentFirmaID[k];
          apdu.setOutgoingAndSend((short) 0, (short) currentFirmaID.length);
     }

     public void uygulamaUpdateName(APDU apdu) {
          byte[] buffer = apdu.getBuffer();
          short p1 = (short) (buffer[ISO7816.OFFSET_P1]);
          if (p1 >= index)
               ISOException.throwIt(NO_SUCH_INDEX);
          JCSystem.beginTransaction();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          SadakatUygulamalar su = uygulamaArray[p1];
          byte[] newFirmaName = new byte[lc];
          for (short i = 0; i < lc; i++)
               newFirmaName[i] = buffer[(short) (5 + i)];
          su.setFirma_ismi(newFirmaName);
          JCSystem.commitTransaction();

          byte[] currentFirmaName = su.getFirma_ismi();
          for (short k = 0; k < (short) currentFirmaName.length; k++)
               buffer[k] = currentFirmaName[k];
          apdu.setOutgoingAndSend((short) 0, (short) currentFirmaName.length);

     }

     public static boolean equals(byte[] b1, byte[] b2) {
          if (b1 == null && b2 == null) {
               return true;
          }
          if (b1 == null || b2 == null) {
               return false;
          }
          if ((short) b1.length != (short) b2.length) {
               return false;
          }
          for (short i = 0; i < (short) b1.length; i++) {
               if (b1[i] != b2[i]) {
                    return false;
               }
          }
          return true;
     }

     public void uygulamaSearch(APDU apdu) {
          byte[] buffer = apdu.getBuffer();
          short lc = (short) buffer[ISO7816.OFFSET_LC];
          byte[] f_id = new byte[lc];
          for (short i3 = 0; i3 < lc; i3++)
               f_id[i3] = buffer[(short) (5 + i3)];
          for (short h = 0; h < index; h++) {
               SadakatUygulamalar su = uygulamaArray[h];
               byte[] H_Firma_id = su.getFirma_id();
               if (equals(f_id, H_Firma_id)) {

                    byte[] H_Uygulama_id = su.getUygulama_id();
                    byte[] H_Firma_ismi = su.getFirma_ismi();

                    short counter1 = 0;

                    for (short k1 = 0; k1 < (short) H_Uygulama_id.length; k1++)
                         buffer[k1] = H_Uygulama_id[k1];
                    counter1 = (short) H_Uygulama_id.length;

                    for (short j2 = 0; j2 < (short) H_Firma_ismi.length; j2++)
                         buffer[(short) (counter1 + j2)] = H_Firma_ismi[j2];
                    counter1 = (short) (counter1 + H_Firma_ismi.length);
                    apdu.setOutgoingAndSend((short) 0, counter1);

               }
          }

     }
     
     public void uygulamaListAll(APDU apdu)
     {
          byte[] buffer = apdu.getBuffer();
          result = new byte[(short) 200];
          byte[] u_id = null, f_id = null, f_isim = null;
          for(short r = 0; r < index; r++)
          {
               SadakatUygulamalar su = uygulamaArray[r];
               u_id = su.getUygulama_id();
               f_id = su.getFirma_id();
               f_isim = su.getFirma_ismi();
               copyData(u_id, f_id, f_isim);
               
          }
          Util.arrayCopy(result, (short)0, buffer, (short)0, res_index);
          result = null;
          short len = res_index;
          res_index = 00;
          apdu.setOutgoingAndSend((short)0, len);
     }
     
     public void copyData(byte[] u_id, byte[] f_id, byte[] f_isim)
     {
          Util.arrayCopy(u_id, (short)0, result, res_index, (short)u_id.length);
          res_index = (short)(res_index + u_id.length);
          Util.arrayCopy(f_id, (short)0, result, res_index, (short)f_id.length);
          res_index = (short)(res_index + f_id.length);
          Util.arrayCopy(f_isim, (short)0, result, res_index, (short)f_isim.length);
          res_index = (short)(res_index + f_isim.length);
          result[res_index] = (byte)0x7C;
          res_index = (short)(res_index + 1);
          
     }
     
     public void copyData2(byte[] id, byte[] sehir, byte[] _banka)
     {
          Util.arrayCopy(_id, (short)0, result2, res2_index, (short)_id.length);
          res2_index = (short)(res2_index + _id.length);
          Util.arrayCopy(_banka, (short)0, result2, res2_index, (short)_sehir.length);
          res2_index = (short)(res2_index + _sehir.length);
          Util.arrayCopy(_banka, (short)0,result2, res2_index, (short)_banka.length);
          res2_index = (short)(res2_index + _banka.length);
          result2[res2_index] = (byte)0x7C;
          res2_index = (short)(res2_index + 1);
          
     }
     
     public void getFirmaIDFlag(byte[] f_id)
     {
          for(short g = 0; g < index; g++)
          {
               SadakatUygulamalar su = uygulamaArray[g];
               byte[] firma_no = su.getFirma_id();
               if(equals(f_id, firma_no))
                    FirmaIDFlag = 1;
          }
     }

}

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 1. Re: Java card applet space
    HanaBizhani Newbie
    Currently Being Moderated
    Hi,

    You should refer to Javacard 2.2 Virtual Machine (or 2.1 if you are using that version),
    some of limitations are as follows:

    Package References: A package can reference at most 128 other packages.

    Package Name: The fully qualified name of a package may contain a maximum of 255 characters. The package name size is further limited if it contains one or more characters which, when represented in UTF-8 format, requires multiple bytes.

    Classes in a Package: A package can contain at most 255 classes and interfaces.

    Interfaces: A class can implement at most 15 interfaces, including interfaces implemented by superclasses. An interface can inherit from at most 14 superinterfaces.

    Static Fields: A class in an applet package can have at most 256 public or protected static non-final fields. A class in a library package can have at most 255 public or protected static non-final fields. There is no limit on the number of static final fields (constants) declared in a class.

    Static Methods: A class in an applet package can have at most 256 public or protected static methods. A class in a library package can have at most 255 public or protected static methods.

    Methods: A class can implement a maximum of 128 public or protected instance methods, and a maximum of 128 instance methods with package visibility. These limits include inherited methods.

    Class Instances: Class instances can contain a maximum of 255 fields, where an int data type is counted as occupying two fields.

    Arrays: Arrays can hold a maximum of 32767 fields.


    Regards,
    Hana
  • 2. Re: Java card applet space
    safarmer Expert
    Currently Being Moderated
    Your problem is most likely related to the fact that you are calling byte[] foo = new byte[size] a lot in your code. There is no garbage collector so you are probably running out of space on your card.

    You need to allocate your memory up front in your applet constructor. This way you will find out at install time if you have enough space to store everything. You will also not have to worry about resources at runtime then.

    - Shane
  • 3. Re: Java card applet space
    860435 Newbie
    Currently Being Moderated
    Shane,
    in most cases, we don't know how much files will be created.in this case, if we allocate all memory in the constructor function,we should allocate a huge memory,but it may be a big waste.so sometimes we still need to dynamic allocate memory even if it is at the risk of run out memroy.anyway,it is not easy to choose.


    simon
  • 4. Re: Java card applet space
    fgrieu Newbie
    Currently Being Moderated
    Although that might not be the direct cause of the problem observed, this code is an extreme example of overly repetitive coding. That coding style is passable on a computer where its cost will not blatantly show, and is actually encouraged by usual productivity metrics. It is not good in a resource-constrained environment like a Smart Card.

    For example:
    The 15 counters in odemeCreateProfile are clear candidates for arrays and an overall loop.
    The constructor for Odeme should be re-factored to accept most of its parameters concatenated in a single array (perhaps as concatenation of TLV).
    The at least 13 getTarih..getKazanilanpuan methods should be just one, returning the concatenation of the desired fields (all of them, or perhaps a selection given in parameters, e.g. as a bitmap).

    My advice is to generalize what the code does until the nature of the data items manipulated (Tarih..Kazanilanpuan) never appear in a method or variable name, except as the name of a final static variable (that is, as a constant).

    When that's done, I guess the number of methods, variables and objects will have been be reduced by nearly a decimal ordrer of magnitude, and the code size also reduced. It will be much easier to determine which limit of the runtime platform is exceeded, should there remain any, and correct that.

    As an aside, one Java Card runtime I have been exposed to happened to have undocumented limits on the number of local variables a method can have, and at least odemeCreateProfile exceeds that (BTW, non-imbricated loops can use the same loop variable, and I there's a javac/converter/settings combo that manage to overlay different variables to the same stack location when that's possible, I want to know).

    Also, defensive Java Card coding requires to allocate all objects at applet installation, for multiple reasons including because that's the only sure way to avoid out-of-memory conditions later, under the assumption that another applet either gets installed, or does not follow that convention. There is currently no consideration for that in the code.
  • 5. Re: Java card applet space
    965207 Newbie
    Currently Being Moderated
    I agree with you,,
    But because I am a newbie to java card subject I did not find any way to seperate between variables when sending them to the applet, for example inside the Odeme class I have 14 variables, and I do not know how to seperate between them
    so the solution I have found is to add a special character such as comma after each variable to distinguish between them.

    But here you are talking about TLV, I have serched alot over the internet about this subject but I do not find any thing related to it, So could you please explain to me how to create a new object using TLV instead of using 14 counters in the code above ?

    thank you in advance
  • 6. Re: Java card applet space
    fgrieu Newbie
    Currently Being Moderated
    TLV stands for Tag (or Type) Length Value. Simplifying just a bit, the idea is to have a single byte[] array holding several data elements, each starting with a tag byte stating its type, a length byte stating its data length, and its data. It is then possible to manipulate (read, write..) an element designated by its tag, using the same code, accepting the tag as a parameter.

    Some pack tag and length in the same byte. There is at least one standard allowing tag and length to have a variable number of bytes. There is even built-in support for that in modern Java Card Classic, see
    JCDK3.0.4_ClassicEdition/docs/api/javacardx/framework/tlv/BERTLV.html
    Caveat: it might not be available on a given card (euphemism). But it is not too hard to write a small library or class to do this.
  • 7. Re: Java card applet space
    965207 Newbie
    Currently Being Moderated
    Thank you fgrieu for your clear explanation but really I am not able to rewrite the code using the BERTLV class. The issue is that when I send the command APDU through android activity I take date from user and then but tho─▒se date in arrays and send them to the applet,
    When I did it, I did not have any other ideas related to this, otherwise I will not be able to retreive the correct data.

    Again, could you rewrite the createOdemeProfie() method code using TLV ??
  • 8. Re: Java card applet space
    fgrieu Newbie
    Currently Being Moderated
    Again, could you rewrite the createOdemeProfie() method code using TLV ??
    I guess you are referring to<tt> odemeCreateProfile </tt>in the original applet.

    I could* rewrite it in a cleaner way. But
    - Why should I* do that?
    - You did not give the spec of the applet and its APDU, and I'd hate to write code without specs, instead having to rely on the assumption that the original code is valid, which is dubious. For a tiny example, how could one guess if<tt> short lc = (short) buffer[ISO7816.OFFSET_LC]; </tt>is correct, or if rather the canonical<tt> short lc = (short)(buffer[ISO7816.OFFSET_LC]&0xFF); </tt>is required (that is, if the incoming data can exceed 127 bytes)?
    - the original sin lies in the design of new<tt> Odeme(_id, tarih, saat, firma, kategori, tutar, birim, ulke, sehir, ilce, aid, banka, taksit, _puan) </tt>needing so many explicit parameters.

    Without fixing the very definition of<tt> Odeme </tt>, the code will remain ugly. One possible design would be to have a method to set a field designated by its tag, and another to get the value of a field designated by its tag. Another design would be a method to set any number of fields passed as an array of bytes containing a concatenation of TLVs, and another to get any number of fields designated by an array of tag bytes, as an array of bytes containing a concatenation of the TLVs.

    Again, you* want to generalize what the code does until the nature of the data items manipulated never appear in a method or variable name, except as the name of a finalstatic variable (that is, as a constant), at least in utility methods like<tt> odemeCreateProfile </tt>, and in the constructor and accessors of <tt> Odeme </tt>. Think of it a building a tiny database engine, then using it. Of course when performing actual computation as opposed to mere data storage, it helps to name the variables, e.g. when adding a traveled distance to an odometer, or computing a cost from this or that per some formula (but beware that in Java Card, especially on platforms without<tt> int </tt>, straight formulas are seldom the right way to perform any but the simplest computations required in a real application).

    Edited by: fgrieu on Feb 8, 2013 11:46 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points