SWIG 将 unsigned char* 转换为 20 字节缓冲区 Java 结构 [英] SWIG Convert unsigned char* to 20 byte buffer Java structure

查看:38
本文介绍了SWIG 将 unsigned char* 转换为 20 字节缓冲区 Java 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C 函数 (composeKey),它有一个输入 unsigned char* 参数(key").在 C 端,key"需要是一个空的 20 字节缓冲区结构.我假设大小为 20 的 Java 短数组将是传递 composeKey 的key"参数的正确 Java 结构,但我不确定.也许一个字节 [20] 是我需要的.如果这是正确的,那么需要修改什么 SWIG 接口文件来生成以 short[] 作为key"参数输入的 composeKey Java 方法?

I have a C function (composeKey) that has an input unsigned char* parameter ("key"). On the C side, "key" needs to be an empty 20 byte buffer structure. I'm assuming that a Java short array with a size of 20 would be the correct Java structure to pass composeKey's "key" parameter, but I'm unsure. Maybe a byte[20] is what i need. If that is correct, what SWIG Interface file modification is needed to generate the composeKey Java method with a short[] as input for the "key" parameter?

C Function:
int composeKey(const void* secret, int secret_len, unsigned char* key, int length);

推荐答案

解决您的具体问题

Java 在其类型系统中并没有真正区分 short[20] 和(例如)short[21].不过,您可以通过使 key 的长度对于 SWIG 而言始终为 20 的事实来做一些非常明智的事情:

Solution to your specific problem

Java doesn't really distinguish between short[20] and (e.g.) short[21] in its type system. You can do something that's pretty sensible quite simply though, by making the fact that the length of key is always 20 obvious to SWIG:

%module test

%include "arrays_java.i"

int func(unsigned char key[20]);

即使不直接更改函数的实际签名,这也可以工作 - SWIG 可以包装它,但让包装的代码调用一个仍然非常明智地使用 unsigned char* 的函数:

This can work even without changing the actual signature of your function directly - SWIG can wrap that, but have the wrapped code call a function that still takes unsigned char* quite sensibly:

%module test

%{
#include "header.h"
// fine even if it's func(unsigned char *key) in the header.
%}

%include "arrays_java.i"
int func(unsigned char key[20]);

如果您从 Java 中使用大小不合适的数组调用 func,您将收到由 SWIG 生成的代码自动为您抛出的 IndexOutOfBoundsException 异常.

If you call func from Java with an inappropriately sized array you'll get an IndexOutOfBoundsException exception thrown for you automatically by the code that SWIG generates.

在这种特定情况下,"arrays_java.i" 已经提供了合适的类型映射.在更一般的情况下,这是通过为 SWIG 提供 unsigned char [ANY] 的类型映射(字面意思是用 SWIG 类型映射语法编写 ANY )来实现的.然后为特定值实例化它而不是 ANY(有点像 C++ 中的模板),然后您可以使用 访问类型映射中 ANY 的特定值>$1_size 并提供填充尺寸的代码(为简洁起见省略了一些 JNI 细节)大致如下:

In this specific case "arrays_java.i" provides a suitable typemap already. In the more general case this works by providing SWIG with a typemap for unsigned char [ANY] (literally write ANY in SWIG typemap syntax). This then gets instantiated for specific values in place of ANY (sort of like a template in C++), you can then access the specific value of ANY in your typemap using $1_size and supply code that the sizes gets filled in to look (some JNI details omitted for brevity) roughly like:

if (GetArrayLength(arg) != $1_size) {
    // Code to throw a Java Exception ...

然后在生成的包装器中变成:

Which then in the generated wrapper becomes:

if (GetArrayLength(arg) != 20) {
    // Code to throw a Java Exception ...

这篇关于SWIG 将 unsigned char* 转换为 20 字节缓冲区 Java 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆