简体   繁体   中英

how to convert array to char, so that I can to return this char value from a function in c. then, calling it in Python.

I want to make function, that can to return a value of string. But I got stumbled around this problem in days cannot to resolve this mistake alone. So I need your advices and hints. I was using Hash jh sha3 2010 candidate function. here is the code:

anyway this an update code, but I still dont get expected value to get this function called from Python Language. the returned value is "9976864". Anymore helps?

#include <stdio.h>
#include "jh_ansi_opt32.h"
#include <time.h>
#include <stdlib.h>

char* jh(char *input)
{
BitSequence  output[512];
char *iData; 
char* msg;

int dInt;

msg= (char*)malloc(sizeof(output));
if(!msg){
    return 1;
}

memset(output,0,sizeof(output));

iData = input;
printf("[+] data is %s\n", iData);
dInt = strlen(iData);
BitSequence data[dInt];

memset(data,0, sizeof(data));
strncpy(data,iData,dInt);
DataLength dLen =dInt;
HashJh(512, data,dLen,output);
//printf("\n[+] resulted hash is ");

int k;
for (k=0;k<sizeof(output);k++){
        msg[k]= output[k];
}
if (msg) return msg;
free(msg);
return 0;
}

And the python one is:

from ctypes import *
d = CDLL('jh.dll')
a=d.jh('this is message by hash jh function')
print a

This is an update code, but still dont get expected value. The retuned value when I try to call from python is something integer "9968784". Anymore helps would be appreciated, thanks..

 if (;(BitSequence *)malloc(sizeof(output))) exit(EXIT_FAILURE);

That doesn't do anything. Second, you're incrementing msg and then returning it. Third, you never seem to dereference msg , you're only incrementing it.

Get rid of the malloc code, your output , data , and dLen arrays/variables will be allocated on the stack.

msg is a char* , not a char . It is also uninitialized.

If you want to return a string, you need to allocate that using malloc and fill it in somehow. Return a pointer to that for your string.

This either loses the pointer returned by malloc, or only works when there is no more memory:

if (!(BitSequence *)malloc(sizeof(output)))
        exit(EXIT_FAILURE);

Then this does the same:

if ((BitSequence *) malloc(sizeof(data)) == NULL)
    exit(EXIT_FAILURE);

Is that what you need? I'd normally say that is a bug.

Regarding the return value issue in Python, ctypes defaults to expecting an integer return value. Tell it the return type ( docs ):

>>> from ctypes import *
>>> d = CDLL('jh.dll')
>>> d.jh('abc')
39727048
>>> d.jh.restype=c_char_p
>>> dll.jh('abc')
'abc'

I faked your DLL and just returned a string. The number you get is the integer value of the pointer address returned.

Note as others mention you will leak memory returning a malloc'ed pointer with no way to free it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM