annunarcist
annunarcist

Reputation: 1805

What is the JNI equivalent to an unsigned char pointer?

I have a function in C as:

mySource.c:

int trapdoor(unsigned char keywords[][MAX_SIZE], unsigned int *s, int slen, unsigned char *out, int outlen); 

and where keywords is a array of char arrays (with keywords of different sizes), s is an unsigned int of length slen(EDIT: actually 128-bit=>so value of slen would be 4) and out is the destination pointer where the result of the function is stored and of length outlen(EDIT: 256-bit=>so value of outlen is 32).

Note: Since this is a cryptographic function, I needed the datatypes to be of exact sizes that I mentioned.

And I have to call this function from java using JNI interface. Now what are the equivalent datatypes in java & JNI that I have to pass as parameters to call the trapdoor function in C?

mySource_jni.c:

JNIEXPORT ?? JNICALL Java_proj_trapdoor(JNIEnv *env, jobject obj, ??,  ??, ??, ??, ??, ??);

myJavaSource.java:

private native ?? trapdoor_extraction(??, ??, ??, ??, ??);

I found it from other posts that there is no equivalent to unsigned datatypes in java (correct me if I am wrong).

Upvotes: 3

Views: 7050

Answers (3)

JoshDM
JoshDM

Reputation: 5072

Write your Java Class with native method first, using whatever you expect your data to be when Java receives it.

Then call javah on that Class file.

This will generate header and stub files for use in C++ which you then extend to design your C++ class.


You want to be able to contain all data. Therefore, the largest smallest variable types should be used. As others have noted (I've upvoted them), unsigned int requires a long and unsigned char requires an int, so it breaks down like this:

C                                  | JAVA IN C                    | JAVA
unsigned char keywords[][MAX_SIZE] | jobjectArray of jintArrays   | int[][]
unsigned int *s                    | jlong                        | long
int slen                           | jint                         | int
unsigned char *out                 | jint                         | int
int outlen                         | jint                         | int

Untested prototype, fill-in-the-blanks, do the Java one and use javah:

C:

JNIEXPORT jint JNICALL Java_proj_trapdoor(JNIEnv *env, jobject obj, jobjectArray keywordz,  jlong s, jint slen, jint out, jint outlen);

Java:

private native int trapdoor_extraction(int[][] keywordz, long s, int slen, int out, int outlen);

Upvotes: 2

c.s.
c.s.

Reputation: 4826

It is true that Java does not use unsigned primitive types (except of char which is 16-bit) but you can always use a larger precision.

My suggestion is to produce a method that would make sense in Java no matter if the arguments are signed data types and cast them to unsigned inside your C method.

So I would try to use this first:

int trapdoor(char[][] keywords, int[] s, byte[] out);

Note: You don't need to pass the array lengths since you can get the length of an array in C side using env->GetArrayLength().

  • char[][] is unsigned so even if the Java side passes values greater then a single character you will only use the lower byte thus you will treat it as unsigned char. Otherwise you can use short.

  • int[] you don't mention what type of data it stores. E.g. if it is a key for encryption it would be ok to cast each int to unsigned int. Otherwise use a long[] for this.

  • byte[] I understand that your function will store the values here so this should not matter. Otherwise use a short or an int

I hope it helps

Upvotes: 4

Obicere
Obicere

Reputation: 3019

I am assuming that by '32-byte' you mean '32-bit' and likewise for 16. The reason is, because a number with 32 bytes would actually be a 256-bit number.

These are the bit-counts for each of the Java integers:

byte  ->  8-bit
short ->  16-bit
char  ->  16-bit
int   ->  32-bit
long  ->  64-bit

From this, I believe the Java equivalent would be:

char[][] keywords, int s, short slen, char out, int outlen

Upvotes: 2

Related Questions