Hi Aswarth...
Note Sure about your 211PK card, but 4K is easily acheivable on the Oberthur
cards
You can allocate the memory after or during the install with something like
the following code snippets.
Note: you cannot reasonably expect to free the memory, so manage it for the
life
of the applet.
Eric
-----Code sample start ------
ba = new byte[heap_size]; // allocate heap stack from eeprom
-----Code sample end ------
I had my memory allocated when the applet is installed. The following shows
my applets install
and constructor:
-----Code sample start ------
/** installation method */
public static void install( byte[] bArray, short bOffset, byte bLength )
{
new mlbb_applet( bArray, bOffset, bLength );
}
/** constructor */
private mlbb_applet( byte[] bArray, short bOffset, byte bLength )
{
short ptr;
ptr = bOffset; // pointer to aid length
ptr = (short)(ptr + 1 + (short)( bArray[ptr] )); // pointer to priv length
ptr = (short)(ptr + 1 + (short)( bArray[ptr] )); // pointer to length of
params
if ( bArray[ptr] >= 3 && bArray[ptr+1] == MLBB_MAGIC ) // len >= 3 and
magic byte
{
db = new data_base( Util.getShort( bArray, (short)(ptr+2) ) );
}
else
{
db = new data_base( DATA_BASE_SIZE );
}
sreg = new short[NUM_SREGS];
}
-----Code sample end ------
Here's a low level byte heap class which manages alloc's and free from a
heap.
-----Code sample end ------
/** provies a malloc/free memory management of pre-allocated eeprom */
public class byte_heap extends eeprom_list
{
private short total_size;
private final short F_NEXT = (short)0;
private final short F_LENGTH = (short)2;
private final short MIN_FREE = (short)4;
/** constructor of a byte heap within byte array */
byte_heap ( short heap_size )
{
ba = new byte[heap_size]; // allocate heap stack from eeprom
root = 0;
total_size = heap_size;
set_node( (short)0, EOF, total_size );
}
/** return the amount of free memory */
public short get_free ( )
{
short ptr = root;
short free;
free = get( ptr, F_LENGTH );
while ( get( ptr, F_NEXT ) != EOF )
{
ptr = get_next( ptr );
free += get_length( ptr );
}
return free;
}
/** return all memory to the heap */
public void erase_all ( )
{
root = 0; // create root node ptr
set_node( root, EOF, total_size ); // and fill in default
}
/** allocate a given number of bytes */
public short alloc ( short size )
{
short ptr = root;
short diff;
short last;
if ( get_length( ptr ) >= ( size + MIN_FREE ) ) // alway leave first block
{
diff = (short)(get_length( ptr ) - size);
set_node( ptr, get_next( ptr ), diff);
return ( (short)(ptr + diff) );
}
while( get_next( ptr ) != EOF )
{
last = ptr;
ptr = get_next( ptr );
if ( get_length( ptr ) >= size )
{
diff = (short)(get_length( ptr ) - size);
if ( diff == 0 ) // first block is exact fit
{
set_node( last, get_next( ptr ), get_length( last ) );
return ( ptr );
}
if ( diff >= 4 ) // re-size the block
{
set_node( ptr, get_next( ptr ), diff);
return ( (short)(ptr + diff));
}
}
}
return( EOF ); // else return null
}
/** free up the given number of bytes - which must have been alloc'ed */
public void free ( short index, short size )
{
short ptr = root;
short last;
if ( index + size < ptr ) { // add before 1st free block - disjoint
set_node( index, ptr, size );
root = index;
return;
}
if (( index + size ) == ptr ) // add before 1st free block - adjacent
{
set_node( index, get_next( ptr ), (short)(size + get_length(ptr )));
root = index;
return;
}
while( get_next( ptr ) != EOF )
{
last = ptr;
ptr = get_next( ptr );
if ( index + size < ptr )
{
if ( index == (short)(last + get_length( last )))
{
// data lines up with end of last, but not next
set_node( last, ptr, (short)(get_length( last ) + size));
return;
}
else
{
// data is in the middle
set_node( index, ptr, size );
set_node( last, index, get_length( last ) );
return;
}
}
if ( (index + size) == ptr )
{
if ( index == last + get_length( last ) )
{
// data lines up with end of last, and beginning of next
set_node( last, get_next( ptr),
(short)(get_length( last ) + get_length( ptr) + size));
return;
}
else
{
// data lines up with the end of last
set_node( index, get_next( ptr),
(short)(get_length( ptr ) + size) );
set_node( last, index, get_length( last ) );
return;
}
}
}
if( get_length( ptr ) + ptr == index ) // block is after last free block -
adjacent
{
set_node( ptr, get_next( ptr ), (short)(get_length( ptr ) + size) );
return;
}
if( get_length( ptr ) + ptr < index ) // block is after last free block -
disjoint
{
set_node( index, get_next( ptr ), size );
set_node( ptr, index, get_length( ptr ));
return;
}
// must never happen
}
private void set_node ( short index, short next, short length )
{
set( index, F_NEXT, next );
set( index, F_LENGTH, length );
}
private short get_next ( short index )
{
return get( index, F_NEXT );
}
private short get_length ( short index )
{
return get( index, F_LENGTH );
}
/**
*
* Unit Test Block
*
* this file can be discarded by eraseing the classname$Test.class files
* It is only useful for testing the subroutine, which has no javacard
* dependancies
*
*/
/*
public static final class Test
{
public static void main( String[] args )
{
short[] ptr = new short[100];
short start_size = 8192;
short i;
short size;
System.out.println("Hello world");
System.out.println("Create Byte heap of size " + start_size );
byte_heap heap = new byte_heap( start_size );
System.out.println("get_free( ) = " + heap.get_free() );
for( i = 0; i < 20; i ++ )
{
size = (short)(i * 10+ 10);
ptr[i] = heap.alloc( size );
System.out.println("alloc size = " + size + " ptr = " + ptr[i] );
}
System.out.println("get_free( ) = " + heap.get_free() );
// force an adjacent end exit
size = (short)(19 * 10+ 10);
System.out.println("free size = " + size + " ptr = " + ptr[19] );
heap.free( ptr[19], size );
ptr[i] = EOF;
for( i = 0; i < 20; i+=2 )
{
size = (short)(i * 10+ 10);
System.out.println("free size = " + size + " ptr = " + ptr[i] );
heap.free( ptr[i], size );
ptr[i] = EOF;
}
System.out.println("get_free( ) = " + heap.get_free() );
for( i = 0; i < 20; i+=2 )
{
size = (short)(i * 11+ 10);
ptr[i] = heap.alloc( size );
System.out.println("alloc size = " + size + " ptr = " + ptr[i] );
}
System.out.println("get_free( ) = " + heap.get_free() );
for( i = 1; i < 19; i+=2 )
{
size = (short)(i * 10+ 10);
System.out.println("free size = " + size + " ptr = " + ptr[i] );
heap.free( ptr[i], size );
ptr[i] = EOF;
}
System.out.println("get_free( ) = " + heap.get_free() );
for( i = 0; i < 20; i+=2 )
{
size = (short)(i * 11+ 10);
System.out.println("free size = " + size + " ptr = " + ptr[i] );
heap.free( ptr[i], size );
ptr[i] = EOF;
}
System.out.println("Should have original data free");
System.out.println("get_free( ) = " + heap.get_free() );
ptr[0] = heap.alloc( (short)4096);
System.out.println("alloc size = " + 4096 + " ptr = " + ptr[0] );
System.out.println("get_free( ) = " + heap.get_free() );
ptr[1] = heap.alloc( (short)4092);
System.out.println("alloc size = " + 4092 + " ptr = " + ptr[1] );
System.out.println("get_free( ) = " + heap.get_free() );
System.out.println("free size = " + 4096 + " ptr = " + ptr[0] );
heap.free( ptr[0], (short)4096);
System.out.println("get_free( ) = " + heap.get_free() );
ptr[0] = heap.alloc( (short)4096);
System.out.println("alloc size = " + 4096 + " ptr = " + ptr[0] );
System.out.println("get_free( ) = " + heap.get_free() );
System.out.println("free size = " + 4096 + " ptr = " + ptr[0] );
heap.free( ptr[0], (short)4096);
System.out.println("get_free( ) = " + heap.get_free() );
ptr[0] = heap.alloc( (short)4092);
System.out.println("alloc size = " + 4092 + " ptr = " + ptr[0] );
System.out.println("get_free( ) = " + heap.get_free() );
ptr[2] = heap.alloc( (short)4);
System.out.println("alloc size = " + 4 + " ptr = " + ptr[2] );
System.out.println("get_free( ) = " + heap.get_free() );
heap.erase_all();
System.out.println("heap erase all");
System.out.println("Should have original data free");
System.out.println("get_free( ) = " + heap.get_free() );
heap.erase_all();
System.out.println("heap erase all");
System.out.println("Should have original data free");
System.out.println("get_free( ) = " + heap.get_free() );
}
}
*/
}
-----Code sample end ------
I hope my reply was quick enough
Eric Pearson
---- Original Message -----
From: "Aswarth" <[EMAIL PROTECTED]>
To: "OCF" <[EMAIL PROTECTED]>
Sent: Wednesday, October 16, 2002 1:29 PM
Subject: [OCF] How to store a 4kb image in a Java card
> Dear All,
>
> Can any one let me know if I can store a 4 kb image as a variable in
> GemXpresso 211 PK IS Java Cards?
> If possible
> 1. Is is achieved at the time of Installing the applet?
> 2. Is the variable set after the applet has already been installed?
> please let me know how I could achieve this
>
> Appreciating your quick reply
>
> Thanks and Regards
> --------------------------------------------------------------------
> Aswartha Narayana R.
> Associate Engineer R&D
> VISIC, CMC Limited
> Hyderabad - 500 019
>
> Ph: (O) 91- 040 - 3000401/501 ext : 2166
> http://www.cmcltd.com
> --------------------------------------------------------------------
>
>
> ---
> > Visit the OpenCard web site at http://www.opencard.org/ for more
> > information on OpenCard---binaries, source code, documents.
> > This list is being archived at http://www.opencard.org/archive/opencard/
>
> ! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
> ! to
> ! [EMAIL PROTECTED]
> ! containing the word
> ! unsubscribe
> ! in the body.
>
---
> Visit the OpenCard web site at http://www.opencard.org/ for more
> information on OpenCard---binaries, source code, documents.
> This list is being archived at http://www.opencard.org/archive/opencard/
! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
! to
! [EMAIL PROTECTED]
! containing the word
! unsubscribe
! in the body.