[Pcsclite-cvs-commit] MCardApplet/src CardEdge.java,1.6,1.7 MemoryManager.java,1.2,1.3 ObjectManager.java,1.2,1.3
rousseau@quantz.debian.org
rousseau@quantz.debian.org
Wed, 19 Nov 2003 14:07:21 +0100
Update of /cvsroot/muscleplugins/MCardApplet/src
In directory quantz:/tmp/cvs-serv15355/src
Modified Files:
CardEdge.java MemoryManager.java ObjectManager.java
Log Message:
expand tabs in 4 spaces (Java Coding Conventions)
Index: CardEdge.java
===================================================================
RCS file: /cvsroot/muscleplugins/MCardApplet/src/CardEdge.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- CardEdge.java 12 Oct 2003 13:56:29 -0000 1.6
+++ CardEdge.java 19 Nov 2003 13:07:19 -0000 1.7
@@ -2,13 +2,17 @@
// Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
// Ludovic Rousseau <ludovic.rousseau@free.fr>
+//
// Package: CardEdgeApplet
+//
// Description: CardEdge implementation with JavaCard
+//
// Protocol Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
-// Modified:
[...3837 lines suppressed...]
- logged_ids = 0;
- getChallengeDone = false;
- randomData = null;
- STD_PUBLIC_ACL = new byte[6];
- for(byte i = 0; i < 6; i += 2)
- Util.setShort(STD_PUBLIC_ACL, i, (short)0);
+ logged_ids = 0;
+ getChallengeDone = false;
+ randomData = null;
+ STD_PUBLIC_ACL = new byte[6];
+ for(byte i = 0; i < 6; i += 2)
+ Util.setShort(STD_PUBLIC_ACL, i, (short)0);
- setupDone = true;
- }
+ setupDone = true;
+ }
}
Index: MemoryManager.java
===================================================================
RCS file: /cvsroot/muscleplugins/MCardApplet/src/MemoryManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- MemoryManager.java 7 Jan 2003 20:32:01 -0000 1.2
+++ MemoryManager.java 19 Nov 2003 13:07:19 -0000 1.3
@@ -2,13 +2,17 @@
// Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
// Ludovic Rousseau <ludovic.rousseau@free.fr>
+//
// Package: CardEdgeApplet
+//
// Description: CardEdge implementation with JavaCard
+//
// Protocol Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
-// Modified:
-// Eirik Herskedal <ehersked@cs.purdue.edu>
-// License: See LICENSE file
+//
+// Modified: Eirik Herskedal <ehersked@cs.purdue.edu>
+//
+// License: See LICENSE file
//
// $Id$
@@ -43,373 +47,372 @@
* @author Tommaso Cucinotta
* @author David Corcoran
* @author Ludovic Rousseau
- *
- * @version 0.9.10
+ * @version 0.9.9
*/
public class MemoryManager
{
- /**
- * Special offset value used as invalid offset
- */
- public static final short NULL_OFFSET = -1;
- private static final byte NODE_SIZE = 4;
- private byte ptr[];
- private short free_head;
+ /**
+ * Special offset value used as invalid offset
+ */
+ public static final short NULL_OFFSET = -1;
+ private static final byte NODE_SIZE = 4;
+ private byte ptr[];
+ private short free_head;
- /**
- * Constructor for the MemoryManager class
- *
- * @param mem_size Size of the memory are to be allocated
- */
- public MemoryManager(short mem_size)
- {
- ptr = null;
- free_head = NULL_OFFSET;
- Init(mem_size);
- }
+ /**
+ * Constructor for the MemoryManager class
+ *
+ * @param mem_size Size of the memory are to be allocated
+ */
+ public MemoryManager(short mem_size)
+ {
+ ptr = null;
+ free_head = NULL_OFFSET;
+ Init(mem_size);
+ }
- private void Init(short mem_size)
- {
- if(ptr != null)
- {
- return;
- } else
- {
- ptr = new byte[mem_size];
- Util.setShort(ptr, (short)0, mem_size);
- Util.setShort(ptr, (short)2, (short)NULL_OFFSET);
- free_head = 0;
- return;
- }
- }
+ private void Init(short mem_size)
+ {
+ if(ptr != null)
+ {
+ return;
+ } else
+ {
+ ptr = new byte[mem_size];
+ Util.setShort(ptr, (short)0, mem_size);
+ Util.setShort(ptr, (short)2, (short)NULL_OFFSET);
+ free_head = 0;
+ return;
+ }
+ }
- /**
- * Allocate memory
- *
- * Each allocation takes actually a 2 bytes overhead.
- *
- * @param size Size of the memory block
- * @return The offset at which allocated memory starts or
- * NULL_OFFSET if an error occurred.
- * @see #alloc(short)
- * @see #freemem()
- */
- public short alloc(short size)
- {
- short offset = free_head;
- short prev = NULL_OFFSET;
- size += 2;
- if(size < NODE_SIZE)
- size = NODE_SIZE;
- short next_offset;
- for(; offset != NULL_OFFSET; offset = next_offset)
- {
- short free_size = Util.getShort(ptr, offset);
- next_offset = Util.getShort(ptr, (short)(offset + 2));
- if(free_size >= size)
- {
- short remain = (short)(free_size - size);
- if(remain >= NODE_SIZE)
- {
- Util.setShort(ptr, offset, remain);
- } else
- {
- size = free_size;
- remain = 0;
- if(prev == NULL_OFFSET)
- free_head = next_offset;
- else
- Util.setShort(ptr, (short)(prev + 2), next_offset);
- }
- Util.setShort(ptr, (short)(offset + remain), size);
- return (short)(offset + remain + 2);
- }
- prev = offset;
- }
+ /**
+ * Allocate memory
+ *
+ * Each allocation takes actually a 2 bytes overhead.
+ *
+ * @param size Size of the memory block
+ * @return The offset at which allocated memory starts or
+ * NULL_OFFSET if an error occurred.
+ * @see #alloc(short)
+ * @see #freemem()
+ */
+ public short alloc(short size)
+ {
+ short offset = free_head;
+ short prev = NULL_OFFSET;
+ size += 2;
+ if(size < NODE_SIZE)
+ size = NODE_SIZE;
+ short next_offset;
+ for(; offset != NULL_OFFSET; offset = next_offset)
+ {
+ short free_size = Util.getShort(ptr, offset);
+ next_offset = Util.getShort(ptr, (short)(offset + 2));
+ if(free_size >= size)
+ {
+ short remain = (short)(free_size - size);
+ if(remain >= NODE_SIZE)
+ {
+ Util.setShort(ptr, offset, remain);
+ } else
+ {
+ size = free_size;
+ remain = 0;
+ if(prev == NULL_OFFSET)
+ free_head = next_offset;
+ else
+ Util.setShort(ptr, (short)(prev + 2), next_offset);
+ }
+ Util.setShort(ptr, (short)(offset + remain), size);
+ return (short)(offset + remain + 2);
+ }
+ prev = offset;
+ }
- return NULL_OFFSET;
- }
+ return NULL_OFFSET;
+ }
- /**
- * Free a memory block
- *
- * <p>Consecutive free blocks are recompacted. Recompaction happens on
- * free(). 4 cases are considered: don't recompact, recompact with
- * next only, with previous only and with both of them.</p>
- *
- * @param offset The offset at which the memory block starts; it was
- * returned from a previous call to {@link #alloc(short)}
- *
- * @see #alloc(short)
- * @see #freemem()
- */
- public void free(short offset)
- {
- offset -= 2;
- short size = Util.getShort(ptr, offset);
- short prev = NULL_OFFSET;
- short base = free_head;
- boolean found = false;
- short node_next = 0;
- for(; base != NULL_OFFSET; base = node_next)
- {
- node_next = Util.getShort(ptr, (short)(base + 2));
- if(offset < base)
- {
- found = true;
- break;
- }
- prev = base;
- }
+ /**
+ * Free a memory block
+ *
+ * <p>Consecutive free blocks are recompacted. Recompaction happens on
+ * free(). 4 cases are considered: don't recompact, recompact with
+ * next only, with previous only and with both of them.</p>
+ *
+ * @param offset The offset at which the memory block starts; it was
+ * returned from a previous call to {@link #alloc(short)}
+ *
+ * @see #alloc(short)
+ * @see #freemem()
+ */
+ public void free(short offset)
+ {
+ offset -= 2;
+ short size = Util.getShort(ptr, offset);
+ short prev = NULL_OFFSET;
+ short base = free_head;
+ boolean found = false;
+ short node_next = 0;
+ for(; base != NULL_OFFSET; base = node_next)
+ {
+ node_next = Util.getShort(ptr, (short)(base + 2));
+ if(offset < base)
+ {
+ found = true;
+ break;
+ }
+ prev = base;
+ }
- if(found && (short)(offset + size) == base)
- {
- size += Util.getShort(ptr, base);
- Util.setShort(ptr, offset, size);
- if(prev != NULL_OFFSET)
- Util.setShort(ptr, (short)(prev + 2), node_next);
- else
- free_head = node_next;
- base = node_next;
- }
- if(prev != NULL_OFFSET)
- {
- short prev_size = Util.getShort(ptr, prev);
- if((short)(prev + prev_size) == offset)
- {
- Util.setShort(ptr, prev, (short)(prev_size + size));
- } else
- {
- Util.setShort(ptr, (short)(offset + 2), base);
- Util.setShort(ptr, (short)(prev + 2), offset);
- }
- } else
- {
- Util.setShort(ptr, (short)(offset + 2), base);
- free_head = offset;
- }
- }
+ if(found && (short)(offset + size) == base)
+ {
+ size += Util.getShort(ptr, base);
+ Util.setShort(ptr, offset, size);
+ if(prev != NULL_OFFSET)
+ Util.setShort(ptr, (short)(prev + 2), node_next);
+ else
+ free_head = node_next;
+ base = node_next;
+ }
+ if(prev != NULL_OFFSET)
+ {
+ short prev_size = Util.getShort(ptr, prev);
+ if((short)(prev + prev_size) == offset)
+ {
+ Util.setShort(ptr, prev, (short)(prev_size + size));
+ } else
+ {
+ Util.setShort(ptr, (short)(offset + 2), base);
+ Util.setShort(ptr, (short)(prev + 2), offset);
+ }
+ } else
+ {
+ Util.setShort(ptr, (short)(offset + 2), base);
+ free_head = offset;
+ }
+ }
- /**
- * Get available free memory
- *
- * @return The total amount of available free memory, equal to the
- * sum of all free fragments' sizes.
- *
- * @see #free(short)
- * @see #alloc(short)
- */
- public short freemem()
- {
- short offset = free_head;
- short total = 0;
- for(; offset != NULL_OFFSET; offset = Util.getShort(ptr, (short)(offset + 2)))
- total = (short)((total + Util.getShort(ptr, offset)) - 2);
+ /**
+ * Get available free memory
+ *
+ * @return The total amount of available free memory, equal to the
+ * sum of all free fragments' sizes.
+ *
+ * @see #free(short)
+ * @see #alloc(short)
+ */
+ public short freemem()
+ {
+ short offset = free_head;
+ short total = 0;
+ for(; offset != NULL_OFFSET; offset = Util.getShort(ptr, (short)(offset + 2)))
+ total = (short)((total + Util.getShort(ptr, offset)) - 2);
- return total;
- }
+ return total;
+ }
- /**
- * Get the size of a memory block
- *
- * @param offset The offset at which the memory block starts
- */
- public short getBlockSize(short offset)
- {
- return (short)(Util.getShort(ptr, (short)(offset - 2)) - 2);
- }
+ /**
+ * Get the size of a memory block
+ *
+ * @param offset The offset at which the memory block starts
+ */
+ public short getBlockSize(short offset)
+ {
+ return (short)(Util.getShort(ptr, (short)(offset - 2)) - 2);
+ }
- /**
- * Retrieve the Java byte array containing all the memory contents.
- *
- * <p>To optimize, we don't use external buffers, but we directly
- * copy from the memory array.</p>
- *
- * <p><b>Use this function only if really required.</b></p>
- *
- * @return The Java byte array containing all memory contents
- */
- public byte[] getBuffer()
- {
- return ptr;
- }
+ /**
+ * Retrieve the Java byte array containing all the memory contents.
+ *
+ * <p>To optimize, we don't use external buffers, but we directly
+ * copy from the memory array.</p>
+ *
+ * <p><b>Use this function only if really required.</b></p>
+ *
+ * @return The Java byte array containing all memory contents
+ */
+ public byte[] getBuffer()
+ {
+ return ptr;
+ }
- /**
- * Read a byte value from memory
- *
- * @param base The complete memory location (offset) of the byte to
- * read
- * @return The byte value
- */
- public byte getByte(short base)
- {
- return ptr[base];
- }
+ /**
+ * Read a byte value from memory
+ *
+ * @param base The complete memory location (offset) of the byte to
+ * read
+ * @return The byte value
+ */
+ public byte getByte(short base)
+ {
+ return ptr[base];
+ }
- /**
- * Read a byte value from memory
- *
- * @param base The base memory location (offset) of the byte to read
- * @param offset The offset of the byte (is added to the base
- * parameter)
- * @return The byte value
- */
- public byte getByte(short base, short offset)
- {
- return ptr[(short)(base + offset)];
- }
+ /**
+ * Read a byte value from memory
+ *
+ * @param base The base memory location (offset) of the byte to read
+ * @param offset The offset of the byte (is added to the base
+ * parameter)
+ * @return The byte value
+ */
+ public byte getByte(short base, short offset)
+ {
+ return ptr[(short)(base + offset)];
+ }
- /**
- * Copy a byte sequence from memory
- *
- * @param dst_bytes[] The destination byte array
- * @param dst_offset The offset at which the sequence will be copied
- * in dst_bytes[]
- * @param src_base The base memory location (offset) of the source
- * byte sequence
- * @param src_offset The offset of the source byte sequence (is
- * added to the src_base parameter)
- * @param size The number of bytes to be copied
- */
- public void getBytes(byte dst_bytes[], short dst_offset, short src_base, short src_offset, short size)
- {
- Util.arrayCopy(ptr, (short)(src_base + src_offset), dst_bytes, dst_offset, size);
- }
+ /**
+ * Copy a byte sequence from memory
+ *
+ * @param dst_bytes[] The destination byte array
+ * @param dst_offset The offset at which the sequence will be copied
+ * in dst_bytes[]
+ * @param src_base The base memory location (offset) of the source
+ * byte sequence
+ * @param src_offset The offset of the source byte sequence (is
+ * added to the src_base parameter)
+ * @param size The number of bytes to be copied
+ */
+ public void getBytes(byte dst_bytes[], short dst_offset, short src_base, short src_offset, short size)
+ {
+ Util.arrayCopy(ptr, (short)(src_base + src_offset), dst_bytes, dst_offset, size);
+ }
- /**
- * Gets the size of the greatest chunk of available memory
- *
- * @return The size of the greatest free memory chunk, or zero if
- * there is no free mem left
- */
- public short getMaxSize()
- {
- short max_size = 2;
- for(short base = free_head; base != NULL_OFFSET; base = Util.getShort(ptr, (short)(base + 2)))
- {
- short size = Util.getShort(ptr, base);
- if(size > max_size)
- max_size = size;
- }
+ /**
+ * Gets the size of the greatest chunk of available memory
+ *
+ * @return The size of the greatest free memory chunk, or zero if
+ * there is no free mem left
+ */
+ public short getMaxSize()
+ {
+ short max_size = 2;
+ for(short base = free_head; base != NULL_OFFSET; base = Util.getShort(ptr, (short)(base + 2)))
+ {
+ short size = Util.getShort(ptr, base);
+ if(size > max_size)
+ max_size = size;
+ }
- return (short)(max_size - 2);
- }
+ return (short)(max_size - 2);
+ }
- /**
- * Read a short value from memory
- *
- * @param base The base memory location (offset) of the short to
- * read
- * @return The short value
- */
- public short getShort(short base)
- {
- return Util.getShort(ptr, base);
- }
+ /**
+ * Read a short value from memory
+ *
+ * @param base The base memory location (offset) of the short to
+ * read
+ * @return The short value
+ */
+ public short getShort(short base)
+ {
+ return Util.getShort(ptr, base);
+ }
- /**
- * Read a short value from memory
- *
- * @param base The base memory location (offset) of the short to
- * read
- * @param offset The offset of the short (is added to the base
- * parameter)
- * @return The short value
- */
- public short getShort(short base, short offset)
- {
- return Util.getShort(ptr, (short)(base + offset));
- }
+ /**
+ * Read a short value from memory
+ *
+ * @param base The base memory location (offset) of the short to
+ * read
+ * @param offset The offset of the short (is added to the base
+ * parameter)
+ * @return The short value
+ */
+ public short getShort(short base, short offset)
+ {
+ return Util.getShort(ptr, (short)(base + offset));
+ }
- /**
- * Resize (only clamping is supported) a previously allocated memory
- * chunk
- *
- * @param offset Memory offset as returned by alloc()
- * @param new_size ew size of the memory block
- * @return True if it was possible to realloc(), False otherwise
- *
- * @see #alloc(short)
- * @see #free(short)
- * @see #freemem()
- */
- public boolean realloc(short offset, short new_size)
- {
- short actual_size = Util.getShort(ptr, (short)(offset - 2));
- new_size += 2;
- if(new_size < 3 || (short)(actual_size - new_size) < NODE_SIZE)
- {
- return false;
- } else
- {
- Util.setShort(ptr, (short)(offset - 2), new_size);
- Util.setShort(ptr, (short)((offset + new_size) - 2), (short)(actual_size - new_size));
- free((short)(offset + new_size));
- return true;
- }
- }
+ /**
+ * Resize (only clamping is supported) a previously allocated memory
+ * chunk
+ *
+ * @param offset Memory offset as returned by alloc()
+ * @param new_size ew size of the memory block
+ * @return True if it was possible to realloc(), False otherwise
+ *
+ * @see #alloc(short)
+ * @see #free(short)
+ * @see #freemem()
+ */
+ public boolean realloc(short offset, short new_size)
+ {
+ short actual_size = Util.getShort(ptr, (short)(offset - 2));
+ new_size += 2;
+ if(new_size < 3 || (short)(actual_size - new_size) < NODE_SIZE)
+ {
+ return false;
+ } else
+ {
+ Util.setShort(ptr, (short)(offset - 2), new_size);
+ Util.setShort(ptr, (short)((offset + new_size) - 2), (short)(actual_size - new_size));
+ free((short)(offset + new_size));
+ return true;
+ }
+ }
- /**
- * Set a byte value into memory
- *
- * @param base The complete memory location (offset) of the byte to
- * set
- * @param b The new byte value
- */
- public void setByte(short base, byte b)
- {
- ptr[base] = b;
- }
+ /**
+ * Set a byte value into memory
+ *
+ * @param base The complete memory location (offset) of the byte to
+ * set
+ * @param b The new byte value
+ */
+ public void setByte(short base, byte b)
+ {
+ ptr[base] = b;
+ }
- /**
- * Set a byte value into memory
- *
- * @param base The base memory location (offset) of the byte to set
- * @param offset The offset of the byte (is added to the base
- * parameter)
- * @param b The new byte value
- */
- public void setByte(short base, short offset, byte b)
- {
- ptr[(short)(base + offset)] = b;
- }
+ /**
+ * Set a byte value into memory
+ *
+ * @param base The base memory location (offset) of the byte to set
+ * @param offset The offset of the byte (is added to the base
+ * parameter)
+ * @param b The new byte value
+ */
+ public void setByte(short base, short offset, byte b)
+ {
+ ptr[(short)(base + offset)] = b;
+ }
- /**
- * Copy a byte sequence into memory
- *
- * @param dst_base The base memory location (offset) of the
- * destination byte sequence
- * @param dst_offset The offset of the destination byte sequence (is
- * added to the dst_base parameter)
- * @param src_bytes[] The source byte array
- * @param src_offset The offset at which the source sequence starts
- * in src_bytes[]
- * @param size The number of bytes to be copied
- */
- public void setBytes(short dst_base, short dst_offset, byte src_bytes[], short src_offset, short size)
- {
- Util.arrayCopy(src_bytes, src_offset, ptr, (short)(dst_base + dst_offset), size);
- }
+ /**
+ * Copy a byte sequence into memory
+ *
+ * @param dst_base The base memory location (offset) of the
+ * destination byte sequence
+ * @param dst_offset The offset of the destination byte sequence (is
+ * added to the dst_base parameter)
+ * @param src_bytes[] The source byte array
+ * @param src_offset The offset at which the source sequence starts
+ * in src_bytes[]
+ * @param size The number of bytes to be copied
+ */
+ public void setBytes(short dst_base, short dst_offset, byte src_bytes[], short src_offset, short size)
+ {
+ Util.arrayCopy(src_bytes, src_offset, ptr, (short)(dst_base + dst_offset), size);
+ }
- /**
- * Set a short value into memory
- *
- * @param base The complete memory location (offset) of the short to
- * set
- * @param b The short value
- */
- public void setShort(short base, short b)
- {
- Util.setShort(ptr, base, b);
- }
+ /**
+ * Set a short value into memory
+ *
+ * @param base The complete memory location (offset) of the short to
+ * set
+ * @param b The short value
+ */
+ public void setShort(short base, short b)
+ {
+ Util.setShort(ptr, base, b);
+ }
- /**
- * Set a short value into memory
- */
- public void setShort(short base, short offset, short b)
- {
- Util.setShort(ptr, (short)(base + offset), b);
- }
+ /**
+ * Set a short value into memory
+ */
+ public void setShort(short base, short offset, short b)
+ {
+ Util.setShort(ptr, (short)(base + offset), b);
+ }
}
Index: ObjectManager.java
===================================================================
RCS file: /cvsroot/muscleplugins/MCardApplet/src/ObjectManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ObjectManager.java 7 Jan 2003 20:32:01 -0000 1.2
+++ ObjectManager.java 19 Nov 2003 13:07:19 -0000 1.3
@@ -2,13 +2,17 @@
// Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
// Ludovic Rousseau <ludovic.rousseau@free.fr>
+//
// Package: CardEdgeApplet
+//
// Description: CardEdge implementation with JavaCard
+//
// Protocol Authors: Tommaso Cucinotta <cucinotta@sssup.it>
// David Corcoran <corcoran@linuxnet.com>
-// Modified:
-// Eirik Herskedal <ehersked@cs.purdue.edu>
-// License: See LICENSE file
+//
+// Modified: Eirik Herskedal <ehersked@cs.purdue.edu>
+//
+// License: See LICENSE file
//
// $Id$
@@ -18,7 +22,7 @@
import javacard.framework.Util;
// Referenced classes of package com.sun.javacard.samples.CardEdge:
-// MemoryManager
+// MemoryManager
/**
* Object Manager Class
@@ -41,357 +45,357 @@
* @author Tommaso Cucinotta
* @author David Corcoran
* @author Ludovic Rousseau
+ * @version 0.9.9
*
- * @version 0.9.10
*/
public class ObjectManager
{
- public static final byte OBJ_ACL_SIZE = 6;
+ public static final byte OBJ_ACL_SIZE = 6;
- private static final byte OBJ_HEADER_SIZE = 14;
- private static final byte OBJ_H_NEXT = 0;
- private static final byte OBJ_H_CLASS = 2;
- private static final byte OBJ_H_ID = 4;
- private static final byte OBJ_H_ACL = 6;
- private static final byte OBJ_H_SIZE = 12;
- private static final byte OBJ_H_DATA = 14;
+ private static final byte OBJ_HEADER_SIZE = 14;
+ private static final byte OBJ_H_NEXT = 0;
+ private static final byte OBJ_H_CLASS = 2;
+ private static final byte OBJ_H_ID = 4;
+ private static final byte OBJ_H_ACL = 6;
+ private static final byte OBJ_H_SIZE = 12;
+ private static final byte OBJ_H_DATA = 14;
- /**
- * There have been memory problems on the card
- */
- public static final short SW_NO_MEMORY_LEFT = (short)0x9C01;
- public static final short SW_OBJECT_NOT_FOUND = (short)0x9C07;
+ /**
+ * There have been memory problems on the card
+ */
+ public static final short SW_NO_MEMORY_LEFT = (short)0x9C01;
+ public static final short SW_OBJECT_NOT_FOUND = (short)0x9C07;
- /**
- * Size of an Object Record filled by getFirstRecord() or
- * getNextRecord(): ID, Size, ACL
- */
- public static final short RECORD_SIZE = 14;
+ /**
+ * Size of an Object Record filled by getFirstRecord() or
+ * getNextRecord(): ID, Size, ACL
+ */
+ public static final short RECORD_SIZE = 14;
- /**
- * Iterator on objects.
- */
- private short it;
+ /**
+ * Iterator on objects.
+ */
+ private short it;
- /**
- * The Memory Manager object
- */
- private MemoryManager mem;
+ /**
+ * The Memory Manager object
+ */
+ private MemoryManager mem;
- /**
- * Head of the objects' list
- */
- private short obj_list_head;
+ /**
+ * Head of the objects' list
+ */
+ private short obj_list_head;
- /**
- * Constructor for the ObjectManager class.
- *
- * @param mem_ref The MemoryManager object to be used to allocate
- * objects' memory.
- */
- public ObjectManager(MemoryManager mem_ref)
- {
- mem = null;
- obj_list_head = -1;
- mem = mem_ref;
- obj_list_head = -1;
- }
+ /**
+ * Constructor for the ObjectManager class.
+ *
+ * @param mem_ref The MemoryManager object to be used to allocate
+ * objects' memory.
+ */
+ public ObjectManager(MemoryManager mem_ref)
+ {
+ mem = null;
+ obj_list_head = -1;
+ mem = mem_ref;
+ obj_list_head = -1;
+ }
- /**
- * Allow or unallow delete on object given the logged identities
- */
- public boolean authorizeDeleteFromAddress(short base, short logged_ids)
- {
- return authorizeOp(mem.getShort(base, (short)-4), logged_ids);
- }
+ /**
+ * Allow or unallow delete on object given the logged identities
+ */
+ public boolean authorizeDeleteFromAddress(short base, short logged_ids)
+ {
+ return authorizeOp(mem.getShort(base, (short)-4), logged_ids);
+ }
- /**
- * Check if logged in identities satisfy requirements for an
- * operation
- *
- * @param required_ids The required identities as from an ACL short
- * @param logged_ids The current logged in identities as stored in
- * CardEdge.logged_ids
- */
- private boolean authorizeOp(short required_ids, short logged_ids)
- {
- return required_ids != -1 && (short)(required_ids & logged_ids) == required_ids;
- }
+ /**
+ * Check if logged in identities satisfy requirements for an
+ * operation
+ *
+ * @param required_ids The required identities as from an ACL short
+ * @param logged_ids The current logged in identities as stored in
+ * CardEdge.logged_ids
+ */
+ private boolean authorizeOp(short required_ids, short logged_ids)
+ {
+ return required_ids != -1 && (short)(required_ids & logged_ids) == required_ids;
+ }
- /**
- * Allow or unallow read on object given the logged identities
- *
- * @param base The object base address as returned from
- * getBaseAddress()
- * @param logged_ids The current logged in identities as stored in
- * CardEdge.logged_ids
- */
- public boolean authorizeReadFromAddress(short base, short logged_ids)
- {
- return authorizeOp(mem.getShort(base, (short)-8), logged_ids);
- }
+ /**
+ * Allow or unallow read on object given the logged identities
+ *
+ * @param base The object base address as returned from
+ * getBaseAddress()
+ * @param logged_ids The current logged in identities as stored in
+ * CardEdge.logged_ids
+ */
+ public boolean authorizeReadFromAddress(short base, short logged_ids)
+ {
+ return authorizeOp(mem.getShort(base, (short)-8), logged_ids);
+ }
- /**
- * Allow or unallow write on object given the logged identities
- *
- * @param base The object base address as returned from
- * getBaseAddress()
- * @param logged_ids The current logged in identities as stored in
- * CardEdge.logged_ids
- */
- public boolean authorizeWriteFromAddress(short base, short logged_ids)
- {
- return authorizeOp(mem.getShort(base, (short)-6), logged_ids);
- }
+ /**
+ * Allow or unallow write on object given the logged identities
+ *
+ * @param base The object base address as returned from
+ * getBaseAddress()
+ * @param logged_ids The current logged in identities as stored in
+ * CardEdge.logged_ids
+ */
+ public boolean authorizeWriteFromAddress(short base, short logged_ids)
+ {
+ return authorizeOp(mem.getShort(base, (short)-6), logged_ids);
+ }
- /**
- * Clamps an object freeing the unused memory
- *
- * @throws SW_NO_MEMORY_LEFT exception if cannot allocate the
- * memory. Does not check if object exists.
- *
- * @param type Object Type
- * @param id Object ID (Type and ID form a generic 4 bytes
- * identifier)
- * @param new_size The new object size (must be less than current
- * size)
- *
- * @return True if clamp was possible, false otherwise
- */
- public boolean clampObject(short type, short id, short new_size)
- {
- short base = getEntry(type, id);
- if(base == -1)
- ISOException.throwIt((short)SW_OBJECT_NOT_FOUND);
- if(mem.realloc(base, (short)(new_size + RECORD_SIZE)))
- {
- mem.setShort(base, (short)OBJ_H_SIZE, new_size);
- return true;
- } else
- {
- return false;
- }
- }
+ /**
+ * Clamps an object freeing the unused memory
+ *
+ * @throws SW_NO_MEMORY_LEFT exception if cannot allocate the
+ * memory. Does not check if object exists.
+ *
+ * @param type Object Type
+ * @param id Object ID (Type and ID form a generic 4 bytes
+ * identifier)
+ * @param new_size The new object size (must be less than current
+ * size)
+ *
+ * @return True if clamp was possible, false otherwise
+ */
+ public boolean clampObject(short type, short id, short new_size)
+ {
+ short base = getEntry(type, id);
+ if(base == -1)
+ ISOException.throwIt((short)SW_OBJECT_NOT_FOUND);
+ if(mem.realloc(base, (short)(new_size + RECORD_SIZE)))
+ {
+ mem.setShort(base, (short)OBJ_H_SIZE, new_size);
+ return true;
+ } else
+ {
+ return false;
+ }
+ }
- /**
- * Compare an object's ACL with the provided ACL.
- *
- * @param base The object base address, as returned from
- * getBaseAddress()
- * @param acl The buffer containing the ACL
- *
- * @return True if the ACLs are equal
- */
- public boolean compareACLFromAddress(short base, byte acl[])
- {
- return Util.arrayCompare(mem.getBuffer(), (short)((base - 14) + OBJ_H_ACL), acl, (short)0, (short)OBJ_ACL_SIZE) == 0;
- }
+ /**
+ * Compare an object's ACL with the provided ACL.
+ *
+ * @param base The object base address, as returned from
+ * getBaseAddress()
+ * @param acl The buffer containing the ACL
+ *
+ * @return True if the ACLs are equal
+ */
+ public boolean compareACLFromAddress(short base, byte acl[])
+ {
+ return Util.arrayCompare(mem.getBuffer(), (short)((base - 14) + OBJ_H_ACL), acl, (short)0, (short)OBJ_ACL_SIZE) == 0;
+ }
- /**
- * Creates an object with specified parameters.
- *
- * @throws SW_NO_MEMORY_LEFT exception if cannot allocate the
- * memory. Does not check if object exists.
- *
- * @param type Object Type
- * @param id Object ID (Type and ID form a generic 4 bytes
- * identifier)
- * @param acl_buf Java byte array containing the ACL for the new object
- * @param acl_offset Offset at which the ACL starts in acl_buf[]
- *
- * @return The memory base address for the object. It can be used in
- * successive calls to xxxFromAddress() methods.
- *
- */
- public short createObject(short type, short id, short size, byte acl_buf[], short acl_offset)
- {
- short base = mem.alloc((short)(size + 14));
- if(base == -1)
- ISOException.throwIt((short)SW_NO_MEMORY_LEFT);
- mem.setShort(base, (short)OBJ_H_NEXT, obj_list_head);
- mem.setShort(base, (short)OBJ_H_CLASS, type);
- mem.setShort(base, (short)OBJ_H_ID, id);
- mem.setShort(base, (short)OBJ_H_SIZE, size);
- mem.setBytes(base, (short)OBJ_H_ACL, acl_buf, acl_offset, (short)OBJ_ACL_SIZE);
- obj_list_head = base;
- return (short)(base + OBJ_H_DATA);
- }
+ /**
+ * Creates an object with specified parameters.
+ *
+ * @throws SW_NO_MEMORY_LEFT exception if cannot allocate the
+ * memory. Does not check if object exists.
+ *
+ * @param type Object Type
+ * @param id Object ID (Type and ID form a generic 4 bytes
+ * identifier)
+ * @param acl_buf Java byte array containing the ACL for the new object
+ * @param acl_offset Offset at which the ACL starts in acl_buf[]
+ *
+ * @return The memory base address for the object. It can be used in
+ * successive calls to xxxFromAddress() methods.
+ *
+ */
+ public short createObject(short type, short id, short size, byte acl_buf[], short acl_offset)
+ {
+ short base = mem.alloc((short)(size + 14));
+ if(base == -1)
+ ISOException.throwIt((short)SW_NO_MEMORY_LEFT);
+ mem.setShort(base, (short)OBJ_H_NEXT, obj_list_head);
+ mem.setShort(base, (short)OBJ_H_CLASS, type);
+ mem.setShort(base, (short)OBJ_H_ID, id);
+ mem.setShort(base, (short)OBJ_H_SIZE, size);
+ mem.setBytes(base, (short)OBJ_H_ACL, acl_buf, acl_offset, (short)OBJ_ACL_SIZE);
+ obj_list_head = base;
+ return (short)(base + OBJ_H_DATA);
+ }
- /**
- * Creates an object with the maximum available size
- */
- public short createObjectMax(short type, short id, byte acl_buf[], short acl_offset)
- {
- short obj_size = mem.getMaxSize();
- if(obj_size == 0)
- ISOException.throwIt((short)SW_NO_MEMORY_LEFT);
- return createObject(type, id, (short)(obj_size - 14), acl_buf, acl_offset);
- }
+ /**
+ * Creates an object with the maximum available size
+ */
+ public short createObjectMax(short type, short id, byte acl_buf[], short acl_offset)
+ {
+ short obj_size = mem.getMaxSize();
+ if(obj_size == 0)
+ ISOException.throwIt((short)SW_NO_MEMORY_LEFT);
+ return createObject(type, id, (short)(obj_size - 14), acl_buf, acl_offset);
+ }
- /**
- * Destroy the specified object
- *
- * @param type Object Type
- * @param id Object ID (Type and ID form a generic 4 bytes
- * identifier)
- * @param secure If true, object memory is zeroed before being
- * released.
- */
- public void destroyObject(short type, short id, boolean secure)
- {
- short base = obj_list_head;
- short prev = -1;
- boolean found;
- for(found = false; !found && base != -1;)
- if(mem.getShort(base, (short)2) == type && mem.getShort(base, (short)4) == id)
- {
- found = true;
- } else
- {
- prev = base;
- base = mem.getShort(base, (short)0);
- }
+ /**
+ * Destroy the specified object
+ *
+ * @param type Object Type
+ * @param id Object ID (Type and ID form a generic 4 bytes
+ * identifier)
+ * @param secure If true, object memory is zeroed before being
+ * released.
+ */
+ public void destroyObject(short type, short id, boolean secure)
+ {
+ short base = obj_list_head;
+ short prev = -1;
+ boolean found;
+ for(found = false; !found && base != -1;)
+ if(mem.getShort(base, (short)2) == type && mem.getShort(base, (short)4) == id)
+ {
+ found = true;
+ } else
+ {
+ prev = base;
+ base = mem.getShort(base, (short)0);
+ }
- if(found)
- {
- if(prev != -1)
- mem.setShort(prev, (short)0, mem.getShort(base, (short)0));
- else
- obj_list_head = mem.getShort(base, (short)0);
- if(secure)
- Util.arrayFillNonAtomic(mem.getBuffer(), (short)(base + 14), mem.getShort(base, (short)12), (byte)0);
- mem.free(base);
- }
- }
+ if(found)
+ {
+ if(prev != -1)
+ mem.setShort(prev, (short)0, mem.getShort(base, (short)0));
+ else
+ obj_list_head = mem.getShort(base, (short)0);
+ if(secure)
+ Util.arrayFillNonAtomic(mem.getBuffer(), (short)(base + 14), mem.getShort(base, (short)12), (byte)0);
+ mem.free(base);
+ }
+ }
- /**
- * Checks if an object exists
- *
- * @param type The object type
- * @param id The object ID
- *
- * @return true if object exists
- */
- public boolean exists(short type, short id)
- {
- short base = getEntry(type, id);
- return base != -1;
- }
+ /**
+ * Checks if an object exists
+ *
+ * @param type The object type
+ * @param id The object ID
+ *
+ * @return true if object exists
+ */
+ public boolean exists(short type, short id)
+ {
+ short base = getEntry(type, id);
+ return base != -1;
+ }
- /**
- * Returns the data base address (offset) for an object.
- *
- * <p>The base address can be used for further calls to
- * xxxFromAddress() methods</p>
- *
- * <p>This function should only be used if performance issue arise.
- * setObjectData() and getObjectData() should be used, instead.</p>
- *
- * @param type Object Type
- * @param id Object ID (Type and ID form a generic 4 bytes
- * identifier)
- *
- * @return The starting offset of the object. At this location
- */
- public short getBaseAddress(short type, short id)
- {
- short base = getEntry(type, id);
- if(base == -1)
- return -1;
- else
- return (short)(base + 14);
- }
+ /**
+ * Returns the data base address (offset) for an object.
+ *
+ * <p>The base address can be used for further calls to
+ * xxxFromAddress() methods</p>
+ *
+ * <p>This function should only be used if performance issue arise.
+ * setObjectData() and getObjectData() should be used, instead.</p>
+ *
+ * @param type Object Type
+ * @param id Object ID (Type and ID form a generic 4 bytes
+ * identifier)
+ *
+ * @return The starting offset of the object. At this location
+ */
+ public short getBaseAddress(short type, short id)
+ {
+ short base = getEntry(type, id);
+ if(base == -1)
+ return -1;
+ else
+ return (short)(base + 14);
+ }
- /**
- * Returns the header base address (offset) for the specified
- * object.
- *
- * <p>Object header is found at the returned offset, while object
- * data starts right after the header.</p>
- *
- * <p>This performs a linear search, so performance issues could
- * arise as the number of objects grows If object is not found,
- * then returns NULL_OFFSET.</p>
- *
- * @param type Object Type
- * @param id Object ID (Type and ID form a generic 4 bytes
- * identifier)
- *
- * @return The starting offset of the object or NULL_OFFSET if the
- * object is not found.
- */
- private short getEntry(short type, short id)
- {
- for(short base = obj_list_head; base != -1; base = mem.getShort(base, (short)0))
- if(mem.getShort(base, (short)OBJ_H_CLASS) == type && mem.getShort(base, (short)OBJ_H_ID) == id)
- return base;
+ /**
+ * Returns the header base address (offset) for the specified
+ * object.
+ *
+ * <p>Object header is found at the returned offset, while object
+ * data starts right after the header.</p>
+ *
+ * <p>This performs a linear search, so performance issues could
+ * arise as the number of objects grows If object is not found,
+ * then returns NULL_OFFSET.</p>
+ *
+ * @param type Object Type
+ * @param id Object ID (Type and ID form a generic 4 bytes
+ * identifier)
+ *
+ * @return The starting offset of the object or NULL_OFFSET if the
+ * object is not found.
+ */
+ private short getEntry(short type, short id)
+ {
+ for(short base = obj_list_head; base != -1; base = mem.getShort(base, (short)0))
+ if(mem.getShort(base, (short)OBJ_H_CLASS) == type && mem.getShort(base, (short)OBJ_H_ID) == id)
+ return base;
- return -1;
- }
+ return -1;
+ }
- /**
- * Resets the objects iterator and retrieves the information record
- * of the first object, if any.
- *
- * @param buffer The byte array into which the record will be copied
- * @param offset The offset in buffer[] at which the record will be
- * copied
- *
- * @return True if an object was found. False if there are no
- * objects.
- *
- * @see #getNextRecord(byte[], short)
- */
- public boolean getFirstRecord(byte buffer[], short offset)
- {
- it = obj_list_head;
- return getNextRecord(buffer, offset);
- }
+ /**
+ * Resets the objects iterator and retrieves the information record
+ * of the first object, if any.
+ *
+ * @param buffer The byte array into which the record will be copied
+ * @param offset The offset in buffer[] at which the record will be
+ * copied
+ *
+ * @return True if an object was found. False if there are no
+ * objects.
+ *
+ * @see #getNextRecord(byte[], short)
+ */
+ public boolean getFirstRecord(byte buffer[], short offset)
+ {
+ it = obj_list_head;
+ return getNextRecord(buffer, offset);
+ }
- /**
- * Retrieves the information record of the next object, if any.
- *
- * @param buffer The byte array into which the record will be copied
- * @param offset The offset in buffer[] at which the record will be
- * copied
- *
- * @return True if an object was found. False if there are no more
- * objects to inspect.
- *
- * @see #getFirstRecord(byte[], short)
- */
- public boolean getNextRecord(byte buffer[], short offset)
- {
- if(it == -1)
- {
- return false;
- } else
- {
- Util.setShort(buffer, offset, mem.getShort(it, (short)2));
- Util.setShort(buffer, (short)(offset + 2), mem.getShort(it, (short)4));
- Util.setShort(buffer, (short)(offset + 4), (short)0);
- Util.setShort(buffer, (short)(offset + 6), mem.getShort(it, (short)12));
- Util.arrayCopyNonAtomic(mem.getBuffer(), (short)(it + 6), buffer, (short)(offset + 8), (short)6);
- it = mem.getShort(it, (short)0);
- return true;
- }
- }
+ /**
+ * Retrieves the information record of the next object, if any.
+ *
+ * @param buffer The byte array into which the record will be copied
+ * @param offset The offset in buffer[] at which the record will be
+ * copied
+ *
+ * @return True if an object was found. False if there are no more
+ * objects to inspect.
+ *
+ * @see #getFirstRecord(byte[], short)
+ */
+ public boolean getNextRecord(byte buffer[], short offset)
+ {
+ if(it == -1)
+ {
+ return false;
+ } else
+ {
+ Util.setShort(buffer, offset, mem.getShort(it, (short)2));
+ Util.setShort(buffer, (short)(offset + 2), mem.getShort(it, (short)4));
+ Util.setShort(buffer, (short)(offset + 4), (short)0);
+ Util.setShort(buffer, (short)(offset + 6), mem.getShort(it, (short)12));
+ Util.arrayCopyNonAtomic(mem.getBuffer(), (short)(it + 6), buffer, (short)(offset + 8), (short)6);
+ it = mem.getShort(it, (short)0);
+ return true;
+ }
+ }
- /**
- * Returns object size from the base address
- */
- public short getSizeFromAddress(short base)
- {
- return mem.getShort((short)((base - 14) + 12));
- }
+ /**
+ * Returns object size from the base address
+ */
+ public short getSizeFromAddress(short base)
+ {
+ return mem.getShort((short)((base - 14) + 12));
+ }
- /**
- * Set the object's ACL.
- */
- private void setACL(short type, short id, byte acl_buf[], short acl_offset)
- {
- short base = getEntry(type, id);
- mem.setBytes(base, (short)OBJ_H_ACL, acl_buf, acl_offset, (short)OBJ_ACL_SIZE);
- }
+ /**
+ * Set the object's ACL.
+ */
+ private void setACL(short type, short id, byte acl_buf[], short acl_offset)
+ {
+ short base = getEntry(type, id);
+ mem.setBytes(base, (short)OBJ_H_ACL, acl_buf, acl_offset, (short)OBJ_ACL_SIZE);
+ }
}