简体   繁体   English

如何减小javacard小程序的大小

[英]How to reduce the size of javacard applet

I wrote an applet which has 19 KB size on disk.我写了一个在磁盘上有 19 KB 大小的小程序。 It has three classes.它有三个班级。 The first one is extended from Applet, the second one has static functions and third one is a class that i create an instance from it in my applet.第一个是从 Applet 扩展而来的,第二个有静态函数,第三个是我在我的小程序中从中创建实例的类。

I have three questions:我有三个问题:

  1. Is there any way to find out how much size is taken by my applet instance in my javacard?有什么方法可以找出我的 javacard 中的小程序实例占用了多少大小?
  2. Is there any tool to reduce the size of a javacard applet (.cap file)?是否有任何工具可以减小 javacard 小程序(.cap 文件)的大小?
  3. Can you explain rules that help me to reduce my applet size?你能解释一下帮助我减少小程序大小的规则吗?
  1. Is there any way to find out how much size is taken by my applet instance in my javacard?有什么方法可以找出我的 javacard 中的小程序实例占用了多少大小?
  • (AFAIK) There is no official way to do that (in GlobalPlatform / Java Card). (AFAIK)没有官方的方法来做到这一点(在 GlobalPlatform / Java Card 中)。

  • You can estimate the real memory usage from the difference in free memory before applet loading and after installation (and most likely after personalization -- as you probably will create some objects during the personalization).您可以根据小程序加载之前和安装之后(最有可能在个性化之后——因为您可能会在个性化期间创建一些对象)的可用内存差异来估计实际内存使用量。 Some ways to find out free memory information are:查找空闲内存信息的一些方法是:

    • Using JCSystem.getAvailableMemory() (see here ) which gives information for all memory types (if implemented).使用JCSystem.getAvailableMemory() (请参阅此处),它提供所有内存类型(如果已实现)的信息。

    • Using Extended Card Resources Information tag retrievable with GET DATA (see TS 102 226 ) (if implemented).使用可通过GET DATA检索的扩展卡资源信息标签(请参阅TS 102 226 )(如果已实施)。

    • Using proprietary command (ask you vendor).使用专有命令(询问您的供应商)。

  • You can have a look inside your .cap file and see the sizes of the parts that are loaded into the card -- this one is surely VERY INACCURATE as card OS is free to deal with the content at its own discretion.您可以查看您的.cap文件并查看加载到卡中的部分的大小——这个肯定是非常不准确的,因为卡操作系统可以自行决定处理内容。

  • I remember JCOP Tools have some special eclipse view which shows various statistics for the current applet -- might be informative as well.我记得JCOP 工具有一些特殊的 Eclipse 视图,它显示当前小程序的各种统计信息——也可能提供信息。

  • The Reference Implementation offers an option to get some resource consumption statistics -- might be useful as well (I have never used this, though).参考实现提供了一个选项来获取一些资源消耗统计信息——可能也很有用(虽然我从未使用过这个)。

  1. Is there any tool to reduce the size of a javacard applet (.cap file)?是否有任何工具可以减小 javacard 小程序(.cap 文件)的大小?
  • I used ProGuard in the past to improve applet performance (which in fact increased applet size as I used it mostly for method inlining) -- but it should work to reduce the applet size as well (eg eliminate dead code -- see shrinking options ).我过去使用ProGuard来提高小程序的性能(实际上增加了小程序的大小,因为我主要将它用于方法内联)——但它也应该可以减少小程序的大小(例如消除死代码——参见收缩选项) . There are many different optimizations as well -- just have a look, but do not expect miracles.还有许多不同的优化——看看吧,但不要指望奇迹。
  1. Can you explain rules that help me to reduce my applet size?你能解释一下帮助我减少小程序大小的规则吗?
  • I would emphasize good design and proper code re-use, but there are definitely many resources regarding generic optimization techniques -- I don't know any Java Card specific ones -- can't help here :(我会强调良好的设计和正确的代码重用,但肯定有很多关于通用优化技术的资源——我不知道任何特定于 Java Card 的技术——在这里无能为力:(

  • If you have more applets loaded into a single card you might place common code into a shared library.如果您在一张卡中加载了更多小程序,您可以将公共代码放入共享库中。


Some additional (random) notes:一些额外的(随机)注释:

  • It might be more practical to just get a card with a larger memory.买一张内存更大的卡可能更实用。

  • Free memory information given by the card might be inaccurate.卡提供的可用内存信息可能不准确。

  • I wonder you have problems with your applet size as usually there are problems with transient memory size (AFAIK).我想知道您的小程序大小有问题,因为通常存在瞬态内存大小 (AFAIK) 的问题。

  • Your applet might be simply leaking memory and thus use more and more memory.您的小程序可能只是在泄漏内存,从而使用越来越多的内存。

  • Do not sacrifice security for lesser applet size!不要为了较小的小程序大小而牺牲安全性!

Good luck!祝你好运!

To answer your 3rd Question回答你的第三个问题

3.Can you explain rules that help me to reduce my applet size? 3.你能解释一下帮助我减少小程序大小的规则吗?

Some basic Guidelines are :一些基本准则是:

  1. Keep the number of methods minimum as you know we have very limited resources for smart cards and method calling is an overhead so with minimum method calls,performance of the card will increase.Avoid using get/set methods.Recursive calls should also be avoided as the stack size in most of the cards is around 200 Bytes.保持最少的方法数量,因为您知道我们的智能卡资源非常有限,并且方法调用是一种开销,因此使用最少的方法调用,卡的性能将提高。避免使用 get/set 方法。还应避免递归调用,因为大多数卡片的堆栈大小约为 200 字节。

  2. Avoid using more than 3 parameters for virtual methods and 4 for static methods.避免为虚方法使用超过 3 个参数,为静态方法使用超过 4 个参数。 This way, the compiler will use a number or bytecode shortcuts, which reduces code size.这样,编译器将使用数字或字节码快捷方式,从而减少代码大小。

  3. To work on temporary data, one can use APDU buffer or transient arrays as writing on EEPROM is about 1,000 times slower than writing on RAM.要处理临时数据,可以使用 APDU 缓冲区或瞬态数组,因为在 EEPROM 上写入比在 RAM 上写入慢约 1,000 倍。

  4. Inheritance is also an overhead in javacard particularly when the hierarchy is complex.继承也是 javacard 的开销,特别是当层次结构复杂时。

  5. Accessing array elements is also an overhead to card.So, in situations where there is repeated accessing of an array element try to store the element in a local variable and use it.访问数组元素也是卡的开销。因此,在重复访问数组元素的情况下,尝试将元素存储在局部变量中并使用它。

Instead of doing this:而不是这样做:

if (arr[index1] == 1) do this;
OR
if (arr[index1] == 2) do this;
OR
if (arr[index1] == 3) do this;

Do this:做这个:

temp = arr[index1];
if (temp == 1) do this;
OR
if (temp == 2) do this;
OR
if (temp == 3) do this;
  1. Replace nested if-else statements with equivalent switch statements as switch executes faster and takes less memory.用等效的 switch 语句替换嵌套的 if-else 语句,因为 switch 执行速度更快,占用的内存更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM