问题描述
我有一个Java应用程序,该应用程序调用c库来执行加密功能.这是一个用c实现的自定义库,我们需要在某些Java程序中使用它.我需要一种定义SWIG类型图的方法,该方法将允许我调用从Java传递字节数组的函数,并将其视为C函数中的无符号字符指针,其中C函数填充数据并将其返回给Java
I have a Java Application that calls a c library for performing crypto functions.This is a custom library implemented in c that we need to use from some Java programs.I need a way to define a SWIG typemap that will allow me call a function passing bytearray from Java and treat it as an unsigned character pointer in C function where c function fills data and returns it to java
我目前不满意的界面文件摘录如下
Excerpt of my present unhappy interface file is as follows
%module CryptoFacade
%pointer_functions(int, intp);
%pointer_functions(unsigned char, unsigned_charp);
int enCrypt(char* clearText, int clearLen,unsigned char* retCipherText, int *retCipherLen);
我不高兴的Java代码摘录如下.在下面的代码中,我希望对enCrypt函数的调用将为我提供一个缓冲区,但根据生成的代码,它给了我短"字. (请参阅代码中的注释)
And Excerpt from my unhappy Java code is as follows. In the code below I expected that the call to enCrypt function will give me a buffer but it gives me a "short" as per generated code. (See comments in the code)
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,然后实现一个类型映射,该类型映射会将C程序中无符号字符指针指向的内容复制到Java字节数组.
I guess I should change my interface definition to something like below where I make my third parameter a jbytearray and then I got to implement a typemap that will copy the contents pointed to by unsigned character pointer in C program to a java bytearray.
如果必须将内容的长度指定为256个字节,我就很好了,因为处理任意长度可能很棘手.
I am perfectly fine if I have to specify the length of content to 256 bytes, because handling arbitrary lengths may be tricky.
有人可以指出我可以找到这种类型图的地方吗(我是SWIG的新手,没有编写类型图的经验)
Can someone point me to place where I can find such a typemap (I am new to SWIG and have no experience in writing typemaps)
%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
类似,但对于整个数组,而不仅仅是单个元素.我使用此头文件作为测试为您整理了一个完整的示例:
The easiest way to do work with byte arrays in Java is to use %array_class
(or %array_functions
) which is like %pointer_functions
but for an entire array, not just a single element. I put together a complete example for you, using this header file as a test:
inline void foo(unsigned char *bytearr) {
bytearr[0] = 1;
bytearr[1] = 2;
bytearr[2] = 3;
bytearr[3] = 100;
}
我们可以使用SWIG将其包装:
We can wrap this with SWIG with:
%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来行使此功能:
I also put together some Java to exercise this function:
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));
}
}
已编译并运行.请注意,C或C ++中的unsigned char
在Java中表示为short
-Java没有任何无符号类型,因此适合范围0-255的最小类型是简短的. byte
默认情况下不覆盖它. (您可以通过其他方式玩将unsigned char
映射到byte
的游戏,但这远非直观,因此默认设置不是.)
which compiled and ran. Note that unsigned char
in C or C++ is represented in Java as short
- Java doesn't have any unsigned types, so the smallest type which fits the range 0-255 is a short. byte
doesn't cover it by default. (You could play games re-mapping unsigned char
to byte
in other ways but that's far from intuitive so the default is not to).
如果需要,您可以执行更高级的操作.例如,如果您知道数组的大小,则可以使用arrays_java.i
. 此示例使用JNI创建类型映射以返回数组unsigned char
. 此示例显示了如何使用JNI类型映射将数组传递给函数. (它使用的是long
而不是byte
,但这实际上是一个搜索并替换以更改它).
You can do more advanced things if you want. For example if you know the size of the array you can use arrays_java.i
. This example creates typemaps using JNI for returning an array of unsigned char
. This example shows how to pass an array in to a function using JNI typemaps. (It's using long
instead of byte
, but the it's literally a search and replace to change that).
这篇关于如何定义Swig类型映射以将未签名的char *返回给Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!