I have 2 very similar C functions being called from python and Java. How can I combine the 2 libraries into 1 that can be called from both languages?

房东的猫 提交于 2019-12-25 07:27:35

问题


Basically I have 2 implementations of a C function "encrypt" which I call from python using ctypes and java using JNI. I've been told to take the two dynamic libraries that were used for java and python, and combine them into a static library that can be called from both. To clarify, here's the C function implementation for java using JNI:

#include "jniTest.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_jniTest_passBytes
  (JNIEnv *env, jclass cls, jbyteArray array) {
    unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
    jsize size = (*env)->GetArrayLength(env,array);

    for(int i=0; i<size; i++){
        buffer[i] += 1;
        printf("%c",buffer[i]);
    }
    (*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}

So the function takes in a byte array and increments each byte by 1 then returns each element of the new byte array. Here's the java side:

class jniTest{
    public static native void passBytes(byte[] bytes);
    static{
        System.loadLibrary("encrypter");
    {
    public static void main(String[] args){
        Tester tester = new Tester();
        byte[] b = "hello";
        tester.passBytes(b);
    }
}

The python implementation of the C function is the exact same, just not using all that JNI syntax:

#include <stdio.h>

void encrypt(int size, unsigned char *buffer);

void encrypt(int size, unsigned char *buffer){
    for(int i=0; i < size; i++){
        buffer[i]+=1; 
        printf("%c", buffer[i]);
    }
}

And the python side:

import ctypes

encryptPy = ctypes.CDLL('/home/aradhak/Documents/libencrypt.so')
hello = "hello"
byteArr = bytearray(hello)
rawBytes = (ctyes_c.ubyte*len(byteArr)(*(byteArr))
encryptPy.encrypt(len(byteArr), rawBytes)

As you can see, the C function for python and java do the exact same thing (few minor differences). They take in a byte array, increment each byte by 1, and print each incremented byte. I just need to combine these two C libraries and combine them into a single static library which can be accessed from both python AND java. Is this possible? Thanks.


回答1:


You can call the python version from the java one. This avoids the duplicate code. You’ll still have to do the to/from JNI marshalling though:

void encrypt(int size, unsigned char *buffer){
    for(int i=0; i < size; i++){
        buffer[i]+=1; 
        printf("%c", buffer[i]);
    }
}

JNIEXPORT void JNICALL Java_jniTest_passBytes
  (JNIEnv *env, jclass cls, jbyteArray array) {
    unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
    jsize size = (*env)->GetArrayLength(env,array);

    encrypt(size, buffer);

    (*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}


来源:https://stackoverflow.com/questions/37489128/i-have-2-very-similar-c-functions-being-called-from-python-and-java-how-can-i-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!