0

I am trying to use c++ api with objc native code in flutter.
https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-obj-c-tab
flutter documentation says Uint8List should be stored as FlutterStandardTypedData typedDataWithBytes: in objc do

send argument in flutter

var data = <String, Uint8List>{
      "key": byte,           //data type is Uint8List
      "value": byteBuffer,   //data type is Uint8List
    };
Uint8List? byteRes;
byteRes = await platform.invokeMethod('SeedDecrypt', data);

get argument in objc (AppDelegate.m)

NSData* key = call.arguments[@"key"];
NSData* value = call.arguments[@"value"];

NSUInteger keyLength = [key length];
NSUInteger valueLength = [value length];

Byte* byteKey = (Byte*)malloc(keyLength);
Byte* byteValue = (Byte*)malloc(valueLength);

memcpy(byteKey, [key bytes], keyLength);
memcpy(byteValue, [value bytes], byteLength);

DWORD roundKey[32];

//Call C++ API
//prototype : void SeedKey(DWORD* roundKey, BYTE* byteKey);
SeedKey(roundKey, byteKey);

//protoType : void Decrypt(BYTE* byteValue, DWORD* roundKey);
Decrypt(byteValue, roundKey);

NSData* res = [NSData dataWithBytes: byteValue length: sizeof(byteValue)];

result(res);

Store the argument as NSData* and copy the memory to a Byte* variable. After executing the C API, it is converted to NSData type. The problem is that when I run it, the device shuts down. I wrote this source referring to the article below. Do you know what my mistake is?

How to convert NSData to byte array in iPhone?

thanks.

matt
  • 515,959
  • 87
  • 875
  • 1,141
Jungwon
  • 1,038
  • 7
  • 20
  • Do not edit a question to include the answer. The Question field is for questions. To enter an answer, use the Answer field. – matt Jun 27 '22 at 00:25
  • I posted a workaround as a new answer. thanks for the good point – Jungwon Jun 27 '22 at 07:01

1 Answers1

0

Solved

NSNumber* keyLength = call.arguments[@"keyLen"];
NSNumber* valueLength = call.arguments[@"valueLen"];

NSUInteger keyLen = [keyLength integerValue];
NSUInteger valueLen = [valueLength integerValue];

FlutterStandardTypedData* key = call.arguments[@"key"];
FlutterStandardTypedData* value = call.arguments[@"value"];

Byte* byteKey = (Byte*)malloc(keyLen);
Byte* byteValye = (Byte*)malloc(valueLen);

memcpy(byteKey, [key.data bytes], keyLen);
memcpy(byteValue, [value.data bytes], valueLen);

DWORD roundKey[32];

//Call C++ API

NSData* res = [NSData dataWithBytes:keyValue length:keyLen];
FlutterStandardTypedData* rest = [FlutterStandardTypedData typedDataWithBytes: res];

free(byteKey);
free(byteValue);

result(rest);

See https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-obj-c-tab. After matching the data type, match the OBJC data type with the C data type and return the result.

Jungwon
  • 1,038
  • 7
  • 20