2023-01-19 381
我有一个Java应用程序,该应用程序调用C库用于执行加密函数.
这是C中实现的自定义库,我们需要从某些Java程序中使用.
我需要一种定义swig typemap的方法,该swig typemap允许我调用来自java的函数,并将其视为c函数中的无符号字符指针,其中c函数填充数据并将其返回到java
我目前的不愉快界面文件的摘录如下
%module CryptoFacade
%pointer_functions(int, intp);
%pointer_functions(unsigned char, unsigned_charp);
int enCrypt(char* clearText, int clearLen,unsigned char* retCipherText, int *retCipherLen);
和我不快乐的Java代码中的摘录如下.在下面的代码中,我期望加密函数的呼叫将为我提供缓冲区,但根据生成的代码,它给了我”短”. (请参阅代码中的评论)
class MainLoader {
static {
System.loadLibrary("dccasecuJ"); //Load my crypto library
}
public static void main(String[] args) {
// Define the parameters to be passed by reference
SWIGTYPE_p_int retCipherLen=CryptoFacade.new_intp();
SWIGTYPE_p_unsigned_char retCipherText =
CryptoFacade.new_unsigned_charp();
CryptoFacade myFacade=new CryptoFacade();
// Call crypto library function. First two are value parameters, next two are return
myFacade.enCrypt("STRING-TO-ENCRYPT", 17, retCipherText, retCipherLen);
// The length I get back in fourth parameter is just fine
int gotLen= CryptoFacade.intp_value(retCipherLen);
//The value I get for the Ciphertext though is a "short" ... no good
// I need a byte[] in java that has the ciphertext
short gotText= CryptoFacade.unsigned_charp_value(retCipherText);
我想我应该将接口定义更改为下面的类似的内容,在该内容中我将第三个参数作为jbytearray,然后我必须实现一个Typemap,将C Proginal中的无符号字符指针指向的内容复制到Java bytearray.
如果我必须指定内容的长度为256个字节,我就很好,因为处理任意长度可能很棘手.
有人可以将我指向我可以找到这样的打字的地方(我是Swig的新手,没有编写类型的经验)
%module CryptoFacade
%pointer_functions(int, intp);
%pointer_functions(unsigned char, unsigned_charp);
int enCrypt(char* clearText, int clearLen, jbyteArray retCipherText, int *retCipherLen);
使用Java中字节数组的最简单方法是使用%array_class(或%array_functions),就像%pointer_functions一样,但对于整个数组,而不仅仅是一个元素.我为您提供了一个完整的示例,使用此标头文件作为测试:
inline void foo(unsigned char *bytearr) {
bytearr[0] = 1;
bytearr[1] = 2;
bytearr[2] = 3;
bytearr[3] = 100;
}
我们可以用:
将其包裹起来
%module test
%{
#include "test.h"
%}
%include <carrays.i>
%array_class(unsigned char,ByteArr);
%include "test.h"
// Emit Java code to automatically load the shared library
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("test");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}
我还将一些Java组合在一起以行使此功能:
public class run {
public static void main(String[] argv) {
ByteArr arr = new ByteArr(4); // Initial size 4
// You could set some values before passing in if you wanted to.
test.foo(arr.cast());
System.out.println(arr.getitem(0) + ", " + arr.getitem(1) + ", " + arr.getitem(2) + ", " + arr.getitem(3));
}
}
编译并运行.请注意,unsigned char在java中表示为short -Java没有任何无符号类型,因此适合0-255范围的最小类型是简短的. byte默认情况下不会覆盖它. (您可以以其他方式玩游戏unsigned char unsigned char unsigned char,但这远非直观,因此默认不了).
如果您愿意,您可以做更多的高级事情.例如,如果您知道阵列的大小,则可以使用arrays_java.i . unsigned char. 此示例显示了如何使用JNI Typemaps将数组传递到函数. (它使用long而不是byte,但实际上是搜索并更换的).
以上所述是小编给大家介绍的如何定义SWIG TypeMap以将无符号Char *返回给Java,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/25878.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态