I'm wrapping a C library using Swig to be able to use it in PHP. The library uses u_char *
instead of unsigned char *
. Swig treats the u_char as a structure that it doesn't know about.
This results in the generated code doing a check to for the argument type, which obviously fails when I pass in a string as char *
data.
How do I tell Swig to that u_char pointers are just unsigned char pointers?
i.e. this C function
int c_library_function(u_char *data, size_t data_size);
has this check inside of the generated code:
if(SWIG_ConvertPtr(*args[0], (void **) &arg1, SWIGTYPE_p_unsigned_char, 0) < 0) {
SWIG_PHP_Error(E_ERROR, "Type error in argument 1 of c_library_function. Expected SWIGTYPE_p_unsigned_char");
}
Which is where it fails.
I've tried adding %typemap(ctype) u_char "unsigned char"
to the Swig template, and it seems to have no effect.
Edit To clarify one thing, the u_char is definitely a unsigned char. The C files in the library are including sys/types.h
which includes bits/types.h
which has the entry:
typedef unsigned char __u_char;
Also, just writing a function to wrap the call to C function, to be able to cast the type, works as expected:
void libraryFunction(u_char *foo) {
//Does stuff with foo
}
void castLibraryFunction(char *foo) {
libraryFunction((u_char *)foo);
}
But I don't want to have to write a wrapper for every function that expects a u_char* parameter.
Even more obviously, the type that Swig is checking for is SWIGTYPE_p_unsigned_char
i.e. it's understood that the data type is 'unsigned char' but not using an 'unsigned char' pointer, but instead a pointer to a custom structure.