Closed avetiso closed 1 year ago
If I use a type mapping like this:
type-map:
'native-types': # Targets native types.
'char':
'lib': 'pkg_ffi' # predefined import.
'c-type': 'Utf8'
'dart-type': 'Utf8'
Then I am able to call _masked_str = mxffi.mask_format(formatter, _str.toNativeUtf8()).toDartString();
just fine, but I still run into FormatException: Missing extension byte (the offset varies here, as well).
This is how I am getting the library:
var mxffi = mx.NativeLibrary(ffi.DynamicLibrary.executable());
var formatter = mxffi.mask_create_formatter(2);
_masked_str = mxffi.mask_format(formatter, _str.toNativeUtf8()).toDartString();
In this function in my C++ implementation, returning a string literal works as expected.
When I print the string before returning, it is also masked as expected.
const char* mask_format(void* formatter, const char* input) {
std::string str = input;
std::string masked = reinterpret_cast<MX::Utils::String::MaskFormatter*>(formatter)->format(str);
std::cout << masked << " <-here";
std::cout << masked.c_str() << " cstr";
//return masked.c_str();
//return reinterpret_cast<MX::Utils::String::MaskFormatter*>(formatter)->format(input).c_str();
return "string literal that always works";
}
Trying to return the string with .c_str() or copying it and then returning with .c_str() seems to cause issues/malformation.
Will open another issue, I think I've figured this one out.
Hi everyone,
I'm sure I'm making an error in understanding somewhere, so I would appreciate if someone could show me what it is.
I'm trying to use from C functions from our C++ banking library to mask some strings when the user is typing sensitive information.
Here is the signature of the function I am calling:
(
formatter
is a pointer to my string formatting class, it seems to construct just fine)ffigen runs on the whole header (there are 4 functions) with no errors and generates a bunch of signatures.
char
to Int8, and yet my run on ffigen this is what I get:Why do I get ffi.Char when almost all examples tell me that dart:ffi will give you Int8, and that pkg_ffi gives you Utf8?
Assuming that generation is correct, I can't call the function (I know the toString() should actually be toDartString() from Utf8, I was just trying things to try and understand what I'm doing wrong)
But everywhere in the examples that I see it tells me to call _str.toNativeUtf8(), seemingly with the implication that it'll map to both Utf8 or Int8 fine, but it doesn't seem to work with Char?
I've tried type-mapping using pkg_ffi so that Char maps to Utf8, and I can generate that just fine, too.
However, then when I try to call this line:
_masked_str = mxffi.mask_format(formatter, _str.toNativeUtf8()).toDartString();
I get an error of FormatException: unexpected extension byte (the offset changes)
What am I doing incorrectly? I'm trying to make this as out of the box as possible, so I'd prefer to just use what dart:ffi/ffigen generates without having to specify my own type mapping with pkg_ffi, but there seems to be type mismatches between what ffigen creates and all the examples of passing Dart strings to C and back.
How do I convert my Dart string into Pointer, and how do I convert the returning Pointer back into a Dart String? And if that's not possible because I have to use toNativeUtf8, why does ffigen generate it that way in the first place?
Full C header:
Default generated bindings from ffigen with no type mapping: