LCOV - code coverage report
Current view: top level - lib/cwtch - ffi.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 515 0.0 %
Date: 2024-08-22 16:58:37 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:collection';
       2             : import 'dart:convert';
       3             : import 'dart:ffi';
       4             : import 'dart:io';
       5             : import 'dart:isolate';
       6             : import 'package:cwtch/cwtch/cwtchNotifier.dart';
       7             : import 'package:path/path.dart' as path;
       8             : 
       9             : import 'package:ffi/ffi.dart';
      10             : import 'package:cwtch/cwtch/cwtch.dart';
      11             : 
      12             : import '../config.dart';
      13             : 
      14             : import "package:path/path.dart" show dirname, join;
      15             : 
      16             : /////////////////////
      17             : ///   Cwtch API   ///
      18             : /////////////////////
      19             : 
      20             : typedef start_cwtch_function = Int8 Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      21             : typedef StartCwtchFn = int Function(Pointer<Utf8> dir, int len, Pointer<Utf8> tor, int torLen);
      22             : 
      23             : typedef void_from_void_funtion = Void Function();
      24             : typedef VoidFromVoidFunction = void Function();
      25             : 
      26             : typedef free_function = Void Function(Pointer<Utf8>);
      27             : typedef FreeFn = void Function(Pointer<Utf8>);
      28             : 
      29             : typedef void_from_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      30             : typedef VoidFromStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      31             : typedef VoidFromStringFn = void Function(Pointer<Utf8>, int);
      32             : 
      33             : typedef void_from_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      34             : typedef VoidFromStringStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      35             : 
      36             : typedef void_from_string_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      37             : typedef VoidFromStringStringStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      38             : 
      39             : // DownloadFile
      40             : typedef void_from_string_int_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      41             : typedef VoidFromStringIntStringStringStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      42             : 
      43             : typedef void_from_string_string_int_int_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int64, Int64);
      44             : typedef VoidFromStringStringIntIntFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int, int);
      45             : 
      46             : typedef void_from_string_bool_bool_bool = Void Function(Pointer<Utf8>, Int32, Bool, Bool, Bool);
      47             : typedef VoidFromStringBoolBoolBool = void Function(Pointer<Utf8>, int, bool, bool, bool);
      48             : 
      49             : typedef void_from_string_string_byte_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int8);
      50             : typedef VoidFromStringStringByteFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
      51             : 
      52             : typedef string_from_string_string_byte_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int8);
      53             : typedef StringFromStringStringByteFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
      54             : 
      55             : typedef string_to_void_function = Void Function(Pointer<Utf8> str, Int32 length);
      56             : typedef StringFn = void Function(Pointer<Utf8> dir, int);
      57             : 
      58             : typedef string_string_to_void_function = Void Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      59             : typedef StringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      60             : 
      61             : typedef string_string_to_string_function = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      62             : typedef StringFromStringStringFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      63             : 
      64             : typedef string_int_to_void_function = Void Function(Pointer<Utf8> str, Int32 length, Int32 handle);
      65             : typedef VoidFromStringIntFn = void Function(Pointer<Utf8>, int, int);
      66             : 
      67             : typedef get_json_blob_string_function = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
      68             : typedef GetJsonBlobStringFn = Pointer<Utf8> Function(Pointer<Utf8> str, int len);
      69             : 
      70             : typedef get_json_blob_from_string_int_string_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
      71             : typedef GetJsonBlobFromStrIntStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int);
      72             : 
      73             : //func GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char {
      74             : typedef get_json_blob_from_str_str_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int32);
      75             : typedef GetJsonBlobFromStrStrIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
      76             : 
      77             : typedef get_json_blob_from_str_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32);
      78             : typedef GetJsonBlobFromStrIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int);
      79             : 
      80             : typedef get_json_blob_from_str_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      81             : typedef GetJsonBlobFromStrStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      82             : 
      83             : typedef get_json_blob_from_str_int_int_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32, Pointer<Utf8>, Int32);
      84             : typedef GetJsonBlobFromStrIntIntStrFn = Pointer<Utf8> Function(
      85             :   Pointer<Utf8>,
      86             :   int,
      87             :   int,
      88             :   int,
      89             :   Pointer<Utf8>,
      90             :   int,
      91             : );
      92             : 
      93             : typedef get_json_blob_from_str_int_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32);
      94             : typedef GetJsonBlobFromStrIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, int);
      95             : 
      96             : typedef get_json_blob_from_str_int_int_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32, Int32);
      97             : typedef GetJsonBlobFromStrIntIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, int, int);
      98             : 
      99             : typedef get_json_blob_from_str_int_string_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
     100             : typedef GetJsonBlobFromStrIntStringFn = Pointer<Utf8> Function(
     101             :   Pointer<Utf8>,
     102             :   int,
     103             :   int,
     104             :   Pointer<Utf8>,
     105             :   int,
     106             : );
     107             : 
     108             : // func c_GetMessagesByContentHash(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, contenthash_ptr *C.char, contenthash_len C.int) *C.char
     109             : typedef get_json_blob_from_str_str_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     110             : typedef GetJsonBlobFromStrStrStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     111             : 
     112             : typedef void_from_string_int_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
     113             : typedef VoidFromStringIntStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int);
     114             : 
     115             : typedef void_from_string_int_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     116             : typedef VoidFromStringIntStringStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     117             : 
     118             : typedef void_from_string_int_int_int_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     119             : typedef VoidFromStringIntIntIntStringStringFn = void Function(Pointer<Utf8>, int, int, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     120             : 
     121             : typedef void_from_string_int_int_function = Void Function(Pointer<Utf8>, Int32, Int32, Int32);
     122             : typedef VoidFromStringIntIntFn = void Function(Pointer<Utf8>, int, int, int);
     123             : 
     124             : typedef appbus_events_function = Pointer<Utf8> Function();
     125             : typedef AppbusEventsFn = Pointer<Utf8> Function();
     126             : 
     127             : typedef void_to_string = Pointer<Utf8> Function();
     128             : typedef StringFromVoid = Pointer<Utf8> Function();
     129             : 
     130             : const String UNSUPPORTED_OS = "unsupported-os";
     131             : 
     132             : class CwtchFfi implements Cwtch {
     133             :   late DynamicLibrary library;
     134             :   late CwtchNotifier cwtchNotifier;
     135             :   late Isolate cwtchIsolate;
     136             :   ReceivePort _receivePort = ReceivePort();
     137             :   bool _isL10nInit = false;
     138             :   String _assetsDir = path.join(Directory.current.path, "data", "flutter_assets");
     139             :   String _cwtchDir = "";
     140             : 
     141           0 :   static String getLibraryPath() {
     142           0 :     if (Platform.isWindows) {
     143             :       return "libCwtch.dll";
     144           0 :     } else if (Platform.isLinux) {
     145             :       return "libCwtch.so";
     146           0 :     } else if (Platform.isMacOS) {
     147           0 :       if (Abi.current() == Abi.macosX64) {
     148             :         return "libCwtch.x64.dylib";
     149             :       } else {
     150             :         return "libCwtch.arm64.dylib";
     151             :       }
     152             :     } else {
     153             :       return UNSUPPORTED_OS;
     154             :     }
     155             :   }
     156             : 
     157           0 :   CwtchFfi(CwtchNotifier _cwtchNotifier) {
     158           0 :     String libraryPath = getLibraryPath();
     159           0 :     if (libraryPath == UNSUPPORTED_OS) {
     160           0 :       print("OS ${Platform.operatingSystem} not supported by cwtch/ffi");
     161             :       // emergency, ideally the app stays on splash and just posts the error till user closes
     162           0 :       exit(0);
     163             :     }
     164           0 :     library = DynamicLibrary.open(libraryPath);
     165           0 :     cwtchNotifier = _cwtchNotifier;
     166           0 :     cwtchNotifier.setMessageSeenCallback((String profile, int conversation, DateTime time) => {this.SetConversationAttribute(profile, conversation, LastMessageSeenTimeKey, time.toIso8601String())});
     167             :   }
     168             : 
     169             :   // ignore: non_constant_identifier_names
     170           0 :   Future<void> Start() async {
     171             :     String home = "";
     172             :     String bundledTor = "";
     173           0 :     Map<String, String> envVars = Platform.environment;
     174           0 :     if (Platform.isLinux) {
     175           0 :       home = envVars['HOME'] ?? "";
     176             :       if (EnvironmentConfig.TEST_MODE) {
     177           0 :         _cwtchDir = envVars['CWTCH_HOME']!;
     178             :       } else {
     179           0 :         _cwtchDir = envVars['CWTCH_HOME'] ?? path.join(envVars['HOME']!, ".cwtch");
     180             :       }
     181             : 
     182           0 :       if (await File("linux/Tor/tor").exists()) {
     183             :         bundledTor = "linux/Tor/tor";
     184           0 :       } else if (await File("lib/Tor/tor").exists()) {
     185             :         bundledTor = "lib/Tor/tor";
     186           0 :       } else if (await File(path.join(home, ".local/lib/cwtch/Tor/tor")).exists()) {
     187           0 :         bundledTor = path.join(home, ".local/lib/cwtch/Tor/tor");
     188           0 :         _assetsDir = path.join(home, ".local", "share", "cwtch", "data", "flutter_assets");
     189           0 :       } else if (await File("/usr/lib/cwtch/Tor/tor").exists()) {
     190             :         bundledTor = "/usr/lib/cwtch/Tor/tor";
     191           0 :         _assetsDir = path.join("usr", "share", "cwtch", "data", "flutter_assets");
     192             :       } else {
     193             :         bundledTor = "tor";
     194             :       }
     195           0 :     } else if (Platform.isWindows) {
     196           0 :       _cwtchDir = envVars['CWTCH_DIR'] ?? path.join(envVars['UserProfile']!, ".cwtch");
     197           0 :       String currentTor = path.join(Directory.current.absolute.path, "Tor\\Tor\\tor.exe");
     198           0 :       if (await File(currentTor).exists()) {
     199             :         bundledTor = currentTor;
     200           0 :         _assetsDir = path.join(Directory.current.absolute.path, "data", "flutter_assets");
     201             :       } else {
     202           0 :         String exeDir = path.dirname(Platform.resolvedExecutable);
     203           0 :         bundledTor = path.join(exeDir, "Tor\\Tor\\tor.exe");
     204           0 :         _assetsDir = path.join(exeDir, "data", "flutter_assets");
     205             :       }
     206           0 :     } else if (Platform.isMacOS) {
     207           0 :       _cwtchDir = envVars['CWTCH_HOME'] ?? path.join(envVars['HOME']!, "Library/Application Support/Cwtch");
     208           0 :       _assetsDir = "/Applications/Cwtch.app/Contents/Frameworks/App.framework/Versions/Current/Resources/flutter_assets/";
     209           0 :       if (await File("Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     210             :         bundledTor = "Cwtch.app/Contents/MacOS/Tor/tor";
     211           0 :         _assetsDir = "Cwtch.app/Contents/Frameworks/App.framework/Versions/Current/Resources/flutter_assets/";
     212           0 :       } else if (await File("/Applications/Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     213             :         bundledTor = "/Applications/Cwtch.app/Contents/MacOS/Tor/tor";
     214           0 :       } else if (await File("/Volumes/Cwtch/Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     215             :         bundledTor = "/Volumes/Cwtch/Cwtch.app/Contents/MacOS/Tor/tor";
     216           0 :       } else if (await File("/Applications/Tor Browser.app/Contents/MacOS/Tor/tor").exists()) {
     217             :         bundledTor = "/Applications/Tor Browser.app/Contents/MacOS/Tor/tor";
     218           0 :         print("We couldn't find Tor in the Cwtch app directory, however we can fall back to the Tor Browser binary");
     219             :       } else {
     220           0 :         var splitPath = path.split(dirname(Platform.script.path));
     221           0 :         if (splitPath[0] == "/" && splitPath[1] == "Applications") {
     222           0 :           var appName = splitPath[2];
     223           0 :           print("We're running in /Applications in a non standard app name: $appName");
     224           0 :           if (await File("/Applications/$appName/Contents/MacOS/Tor/tor").exists()) {
     225           0 :             bundledTor = "/Applications/$appName/Contents/MacOS/Tor/tor";
     226             :           }
     227             :         }
     228             :       }
     229             :     }
     230             : 
     231             :     // the first Cwtch MacOS release (1.2) accidently was a dev build
     232             :     // we need to temporarily remedy this for a release or two then delete
     233             :     // if macOs and release build and no profile and is dev profile
     234             :     // copy dev profile to release profile
     235           0 :     if (Platform.isMacOS && EnvironmentConfig.BUILD_VER != dev_version) {
     236           0 :       var devProfileExists = await Directory(path.join(_cwtchDir, "dev", "profiles")).exists();
     237           0 :       var releaseProfileExists = await Directory(path.join(_cwtchDir, "profiles")).exists();
     238             :       if (devProfileExists && !releaseProfileExists) {
     239           0 :         print("MacOS one time dev -> release profile migration...");
     240           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "profiles"), _cwtchDir]);
     241           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "SALT"), _cwtchDir]);
     242           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "ui.globals"), _cwtchDir]);
     243             :       }
     244             :     }
     245             : 
     246           0 :     if (EnvironmentConfig.BUILD_VER == dev_version) {
     247           0 :       _cwtchDir = path.join(_cwtchDir, "dev");
     248             :     }
     249             : 
     250           0 :     print("StartCwtch( cwtchdir: $_cwtchDir, torPath: $bundledTor )");
     251             : 
     252           0 :     var startCwtchC = library.lookup<NativeFunction<start_cwtch_function>>("c_StartCwtch");
     253             :     // ignore: non_constant_identifier_names
     254             :     final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
     255             : 
     256           0 :     final utf8CwtchDir = _cwtchDir.toNativeUtf8();
     257           0 :     StartCwtch(utf8CwtchDir, utf8CwtchDir.length, bundledTor.toNativeUtf8(), bundledTor.length);
     258           0 :     malloc.free(utf8CwtchDir);
     259             : 
     260             :     // Spawn an isolate to listen to events from libcwtch-go and then dispatch them when received on main thread to cwtchNotifier
     261           0 :     cwtchIsolate = await Isolate.spawn(_checkAppbusEvents, _receivePort.sendPort);
     262           0 :     _receivePort.listen((message) {
     263           0 :       var env = jsonDecode(message);
     264           0 :       cwtchNotifier.handleMessage(env["EventType"], env["Data"]);
     265             :     });
     266             :   }
     267             : 
     268           0 :   String getAssetsDir() {
     269           0 :     return _assetsDir;
     270             :   }
     271             : 
     272           0 :   Future<String> getCwtchDir() async {
     273           0 :     return _cwtchDir;
     274             :   }
     275             : 
     276             :   // Called on object being disposed to (presumably on app close) to close the isolate that's listening to libcwtch-go events
     277           0 :   @override
     278             :   void dispose() {
     279           0 :     EnvironmentConfig.debugLog("tearing down cwtch FFI isolate");
     280           0 :     library.close();
     281           0 :     cwtchIsolate.kill(priority: Isolate.immediate);
     282             :   }
     283             : 
     284             :   // Entry point for an isolate to listen to a stream of events pulled from libcwtch-go and return them on the sendPort
     285           0 :   static void _checkAppbusEvents(SendPort sendPort) async {
     286           0 :     var stream = pollAppbusEvents();
     287           0 :     await for (var value in stream) {
     288           0 :       sendPort.send(value);
     289             :     }
     290           0 :     print("checkAppBusEvents finished...");
     291             :   }
     292             : 
     293             :   // Steam of appbus events. Call blocks in libcwtch-go GetAppbusEvent.  Static so the isolate can use it
     294           0 :   static Stream<String> pollAppbusEvents() async* {
     295             :     late DynamicLibrary library = DynamicLibrary.open(getLibraryPath());
     296             : 
     297           0 :     var getAppbusEventC = library.lookup<NativeFunction<appbus_events_function>>("c_GetAppBusEvent");
     298             :     // ignore: non_constant_identifier_names
     299             :     final GetAppbusEvent = getAppbusEventC.asFunction<AppbusEventsFn>();
     300             : 
     301             :     // Embedded Version of _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved
     302           0 :     var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
     303             :     final Free = free.asFunction<FreeFn>();
     304             : 
     305             :     // ignore: non_constant_identifier_names
     306           0 :     final GetAppBusEvent = () {
     307             :       // ignore: non_constant_identifier_names
     308           0 :       Pointer<Utf8> result = GetAppbusEvent();
     309           0 :       String event = result.toDartString();
     310           0 :       Free(result);
     311             :       return event;
     312             :     };
     313             : 
     314             :     while (true) {
     315           0 :       final event = GetAppBusEvent();
     316             : 
     317           0 :       if (event.startsWith("{\"EventType\":\"Shutdown\"")) {
     318           0 :         print("Shutting down isolate thread: $event");
     319             :         return;
     320             :       }
     321             :       yield event;
     322             :     }
     323             :   }
     324             : 
     325             :   // ignore: non_constant_identifier_names
     326           0 :   void CreateProfile(String nick, String pass, bool autostart) {
     327           0 :     var createProfileC = library.lookup<NativeFunction<string_from_string_string_byte_function>>("c_CreateProfile");
     328             :     // ignore: non_constant_identifier_names
     329             :     final CreateProfile = createProfileC.asFunction<StringFromStringStringByteFn>();
     330           0 :     final utf8nick = nick.toNativeUtf8();
     331           0 :     final ut8pass = pass.toNativeUtf8();
     332           0 :     var profileHandleBytes = CreateProfile(utf8nick, utf8nick.length, ut8pass, ut8pass.length, autostart ? 1 : 0);
     333           0 :     String profileHandle = profileHandleBytes.toDartString();
     334           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(profileHandleBytes);
     335           0 :     malloc.free(utf8nick);
     336           0 :     malloc.free(ut8pass);
     337             :   }
     338             : 
     339             :   // ignore: non_constant_identifier_names
     340           0 :   void ActivatePeerEngine(String profile) {
     341           0 :     var activatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_ActivatePeerEngine");
     342             :     final ActivatePeerEngine = activatePeerEngineC.asFunction<StringFn>();
     343           0 :     final ut8profile = profile.toNativeUtf8();
     344           0 :     ActivatePeerEngine(ut8profile, ut8profile.length);
     345           0 :     malloc.free(ut8profile);
     346             :   }
     347             : 
     348             :   // ignore: non_constant_identifier_names
     349           0 :   void DeactivatePeerEngine(String profile) {
     350           0 :     var deactivatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_DeactivatePeerEngine");
     351             :     final DeactivatePeerEngine = deactivatePeerEngineC.asFunction<StringFn>();
     352           0 :     final ut8profile = profile.toNativeUtf8();
     353           0 :     DeactivatePeerEngine(ut8profile, ut8profile.length);
     354           0 :     malloc.free(ut8profile);
     355             :   }
     356             : 
     357             :   // ignore: non_constant_identifier_names
     358           0 :   void LoadProfiles(String pass) {
     359           0 :     var loadProfileC = library.lookup<NativeFunction<string_to_void_function>>("c_LoadProfiles");
     360             :     // ignore: non_constant_identifier_names
     361             :     final LoadProfiles = loadProfileC.asFunction<StringFn>();
     362           0 :     final ut8pass = pass.toNativeUtf8();
     363           0 :     LoadProfiles(ut8pass, ut8pass.length);
     364           0 :     malloc.free(ut8pass);
     365             :   }
     366             : 
     367             :   // ignore: non_constant_identifier_names
     368           0 :   Future<String> GetMessage(String profile, int handle, int index) async {
     369           0 :     var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessage");
     370             :     // ignore: non_constant_identifier_names
     371             :     final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
     372           0 :     final utf8profile = profile.toNativeUtf8();
     373           0 :     Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
     374           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     375           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     376           0 :     malloc.free(utf8profile);
     377             :     return jsonMessage;
     378             :   }
     379             : 
     380             :   // ignore: non_constant_identifier_names
     381           0 :   Future<dynamic> GetMessages(String profile, int handle, int index, int count) async {
     382           0 :     var getMessagesC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_int_function>>("c_GetMessages");
     383             :     // ignore: non_constant_identifier_names
     384             :     final GetMessages = getMessagesC.asFunction<GetJsonBlobFromStrIntIntIntFn>();
     385           0 :     final utf8profile = profile.toNativeUtf8();
     386           0 :     Pointer<Utf8> jsonMessageBytes = GetMessages(utf8profile, utf8profile.length, handle, index, count);
     387           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     388           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     389           0 :     malloc.free(utf8profile);
     390             : 
     391             :     return jsonMessage;
     392             :   }
     393             : 
     394           0 :   @override
     395             :   // ignore: non_constant_identifier_names
     396             :   void AcceptContact(String profileOnion, int contactHandle) {
     397           0 :     var acceptContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_AcceptConversation");
     398             :     // ignore: non_constant_identifier_names
     399             :     final AcceptContact = acceptContact.asFunction<VoidFromStringIntFn>();
     400           0 :     final u1 = profileOnion.toNativeUtf8();
     401           0 :     AcceptContact(u1, u1.length, contactHandle);
     402           0 :     malloc.free(u1);
     403             :   }
     404             : 
     405           0 :   @override
     406             :   // ignore: non_constant_identifier_names
     407             :   void BlockContact(String profileOnion, int contactHandle) {
     408           0 :     var blockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_BlockConversation");
     409             :     // ignore: non_constant_identifier_names
     410             :     final BlockContact = blockContact.asFunction<VoidFromStringIntFn>();
     411           0 :     final u1 = profileOnion.toNativeUtf8();
     412           0 :     BlockContact(u1, u1.length, contactHandle);
     413           0 :     malloc.free(u1);
     414             :   }
     415             : 
     416           0 :   @override
     417             :   // ignore: non_constant_identifier_names
     418             :   void UnblockContact(String profileOnion, int contactHandle) {
     419           0 :     var unblockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_UnblockConversation");
     420             :     // ignore: non_constant_identifier_names
     421             :     final UnblockContact = unblockContact.asFunction<VoidFromStringIntFn>();
     422           0 :     final u1 = profileOnion.toNativeUtf8();
     423           0 :     UnblockContact(u1, u1.length, contactHandle);
     424           0 :     malloc.free(u1);
     425             :   }
     426             : 
     427           0 :   @override
     428             :   // ignore: non_constant_identifier_names
     429             :   Future<dynamic> SendMessage(String profileOnion, int contactHandle, String message) async {
     430           0 :     var sendMessage = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_SendMessage");
     431             :     // ignore: non_constant_identifier_names
     432             :     final SendMessage = sendMessage.asFunction<GetJsonBlobFromStrIntStrFn>();
     433           0 :     final u1 = profileOnion.toNativeUtf8();
     434           0 :     final u3 = message.toNativeUtf8();
     435           0 :     Pointer<Utf8> jsonMessageBytes = SendMessage(u1, u1.length, contactHandle, u3, u3.length);
     436           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     437           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     438           0 :     malloc.free(u1);
     439           0 :     malloc.free(u3);
     440             :     return jsonMessage;
     441             :   }
     442             : 
     443           0 :   @override
     444             :   // ignore: non_constant_identifier_names
     445             :   Future<dynamic> SendInvitation(String profileOnion, int contactHandle, int target) async {
     446           0 :     var sendInvitation = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_SendInviteMessage");
     447             :     // ignore: non_constant_identifier_names
     448             :     final SendInvitation = sendInvitation.asFunction<GetJsonBlobFromStrIntIntFn>();
     449           0 :     final u1 = profileOnion.toNativeUtf8();
     450           0 :     Pointer<Utf8> jsonMessageBytes = SendInvitation(u1, u1.length, contactHandle, target);
     451           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     452           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     453           0 :     malloc.free(u1);
     454             :     return jsonMessage;
     455             :   }
     456             : 
     457           0 :   @override
     458             :   // ignore: non_constant_identifier_names
     459             :   Future<dynamic> ShareFile(String profileOnion, int contactHandle, String filepath) async {
     460           0 :     var shareFile = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_ShareFile");
     461             :     // ignore: non_constant_identifier_names
     462             :     final ShareFile = shareFile.asFunction<GetJsonBlobFromStrIntStrFn>();
     463           0 :     final u1 = profileOnion.toNativeUtf8();
     464           0 :     final u3 = filepath.toNativeUtf8();
     465           0 :     Pointer<Utf8> jsonMessageBytes = ShareFile(u1, u1.length, contactHandle, u3, u3.length);
     466           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     467           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     468           0 :     malloc.free(u1);
     469           0 :     malloc.free(u3);
     470             :     return jsonMessage;
     471             :   }
     472             : 
     473           0 :   @override
     474             :   // ignore: non_constant_identifier_names
     475             :   void DownloadFile(String profileOnion, int contactHandle, String filepath, String manifestpath, String filekey) {
     476           0 :     var dlFile = library.lookup<NativeFunction<void_from_string_int_string_string_string_function>>("c_DownloadFileDefaultLimit");
     477             :     // ignore: non_constant_identifier_names
     478             :     final DownloadFile = dlFile.asFunction<VoidFromStringIntStringStringStringFn>();
     479           0 :     final u1 = profileOnion.toNativeUtf8();
     480           0 :     final u3 = filepath.toNativeUtf8();
     481           0 :     final u4 = manifestpath.toNativeUtf8();
     482           0 :     final u5 = filekey.toNativeUtf8();
     483           0 :     DownloadFile(u1, u1.length, contactHandle, u3, u3.length, u4, u4.length, u5, u5.length);
     484           0 :     malloc.free(u1);
     485           0 :     malloc.free(u3);
     486           0 :     malloc.free(u4);
     487           0 :     malloc.free(u5);
     488             :   }
     489             : 
     490           0 :   @override
     491             :   // ignore: non_constant_identifier_names
     492             :   void CreateDownloadableFile(String profileOnion, int contactHandle, String filenameSuggestion, String filekey, String manifestPath) {
     493             :     // android only - do nothing
     494             :   }
     495             : 
     496             :   // ignore: non_constant_identifier_names
     497           0 :   void ExportPreviewedFile(String sourceFile, String suggestion) {
     498             :     // android only - do nothing
     499             :   }
     500             : 
     501           0 :   @override
     502             :   // ignore: non_constant_identifier_names
     503             :   void CheckDownloadStatus(String profileOnion, String fileKey) {
     504           0 :     var checkDownloadStatus = library.lookup<NativeFunction<string_string_to_void_function>>("c_CheckDownloadStatus");
     505             :     // ignore: non_constant_identifier_names
     506             :     final CheckDownloadStatus = checkDownloadStatus.asFunction<VoidFromStringStringFn>();
     507           0 :     final u1 = profileOnion.toNativeUtf8();
     508           0 :     final u2 = fileKey.toNativeUtf8();
     509           0 :     CheckDownloadStatus(u1, u1.length, u2, u2.length);
     510           0 :     malloc.free(u1);
     511           0 :     malloc.free(u2);
     512             :   }
     513             : 
     514           0 :   @override
     515             :   // ignore: non_constant_identifier_names
     516             :   void VerifyOrResumeDownload(String profileOnion, int contactHandle, String filekey) {
     517           0 :     var fn = library.lookup<NativeFunction<void_from_string_int_string_function>>("c_VerifyOrResumeDownloadDefaultLimit");
     518             :     // ignore: non_constant_identifier_names
     519             :     final VerifyOrResumeDownload = fn.asFunction<VoidFromStringIntStringFn>();
     520           0 :     final u1 = profileOnion.toNativeUtf8();
     521           0 :     final u3 = filekey.toNativeUtf8();
     522           0 :     VerifyOrResumeDownload(u1, u1.length, contactHandle, u3, u3.length);
     523           0 :     malloc.free(u1);
     524           0 :     malloc.free(u3);
     525             :   }
     526             : 
     527           0 :   @override
     528             :   // ignore: non_constant_identifier_names
     529             :   void ResetTor() {
     530           0 :     var resetTor = library.lookup<NativeFunction<Void Function()>>("c_ResetTor");
     531             :     // ignore: non_constant_identifier_names
     532             :     final ResetTor = resetTor.asFunction<void Function()>();
     533           0 :     ResetTor();
     534             :   }
     535             : 
     536           0 :   @override
     537             :   // ignore: non_constant_identifier_names
     538             :   Future<dynamic> ImportBundle(String profileOnion, String bundle) async {
     539           0 :     var importBundle = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportBundle");
     540             :     // ignore: non_constant_identifier_names
     541             :     final ImportBundle = importBundle.asFunction<StringFromStringStringFn>();
     542           0 :     final u1 = profileOnion.toNativeUtf8();
     543           0 :     final u2 = bundle.toNativeUtf8();
     544           0 :     Pointer<Utf8> responsePtr = ImportBundle(u1, u1.length, u2, u2.length);
     545           0 :     String response = responsePtr.toDartString();
     546           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(responsePtr);
     547           0 :     malloc.free(u1);
     548           0 :     malloc.free(u2);
     549             :     return response;
     550             :   }
     551             : 
     552           0 :   @override
     553             :   // ignore: non_constant_identifier_names
     554             :   void CreateGroup(String profileOnion, String server, String groupName) {
     555           0 :     var createGroup = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_StartGroup");
     556             :     // ignore: non_constant_identifier_names
     557             :     final CreateGroup = createGroup.asFunction<VoidFromStringStringStringFn>();
     558           0 :     final u1 = profileOnion.toNativeUtf8();
     559           0 :     final u3 = server.toNativeUtf8();
     560           0 :     final u2 = groupName.toNativeUtf8();
     561           0 :     CreateGroup(u1, u1.length, u2, u2.length, u3, u3.length);
     562             : 
     563           0 :     malloc.free(u1);
     564           0 :     malloc.free(u2);
     565           0 :     malloc.free(u3);
     566             :   }
     567             : 
     568           0 :   @override
     569             :   // ignore: non_constant_identifier_names
     570             :   void ArchiveConversation(String profileOnion, int handle) {
     571           0 :     var archiveConversation = library.lookup<NativeFunction<string_int_to_void_function>>("c_ArchiveConversation");
     572             :     // ignore: non_constant_identifier_names
     573             :     final ArchiveConversation = archiveConversation.asFunction<VoidFromStringIntFn>();
     574           0 :     final u1 = profileOnion.toNativeUtf8();
     575           0 :     ArchiveConversation(u1, u1.length, handle);
     576           0 :     malloc.free(u1);
     577             :   }
     578             : 
     579           0 :   @override
     580             :   // ignore: non_constant_identifier_names
     581             :   void DeleteContact(String profileOnion, int handle) {
     582           0 :     var deleteContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_DeleteConversation");
     583             :     // ignore: non_constant_identifier_names
     584             :     final DeleteContact = deleteContact.asFunction<VoidFromStringIntFn>();
     585           0 :     final u1 = profileOnion.toNativeUtf8();
     586           0 :     DeleteContact(u1, u1.length, handle);
     587           0 :     malloc.free(u1);
     588             :   }
     589             : 
     590           0 :   @override
     591             :   // ignore: non_constant_identifier_names
     592             :   void DeleteProfile(String onion, String currentPassword) {
     593           0 :     var deleteprofile = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteProfile");
     594             :     // ignore: non_constant_identifier_names
     595             :     final DeleteProfile = deleteprofile.asFunction<VoidFromStringStringFn>();
     596           0 :     final u1 = onion.toNativeUtf8();
     597           0 :     final u2 = currentPassword.toNativeUtf8();
     598           0 :     DeleteProfile(u1, u1.length, u2, u2.length);
     599           0 :     malloc.free(u1);
     600           0 :     malloc.free(u2);
     601             :   }
     602             : 
     603           0 :   @override
     604             :   // ignore: non_constant_identifier_names
     605             :   void SetProfileAttribute(String profile, String key, String val) {
     606           0 :     var setProfileAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetProfileAttribute");
     607             :     // ignore: non_constant_identifier_names
     608             :     final SetProfileAttribute = setProfileAttribute.asFunction<VoidFromStringStringStringFn>();
     609           0 :     final u1 = profile.toNativeUtf8();
     610           0 :     final u2 = key.toNativeUtf8();
     611           0 :     final u3 = val.toNativeUtf8();
     612           0 :     SetProfileAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
     613           0 :     malloc.free(u1);
     614           0 :     malloc.free(u2);
     615           0 :     malloc.free(u3);
     616             :   }
     617             : 
     618           0 :   @override
     619             :   // ignore: non_constant_identifier_names
     620             :   void SetConversationAttribute(String profile, int contact, String key, String val) {
     621           0 :     var setContactAttribute = library.lookup<NativeFunction<void_from_string_int_string_string_function>>("c_SetConversationAttribute");
     622             :     // ignore: non_constant_identifier_names
     623             :     final SetContactAttribute = setContactAttribute.asFunction<VoidFromStringIntStringStringFn>();
     624           0 :     final u1 = profile.toNativeUtf8();
     625           0 :     final u3 = key.toNativeUtf8();
     626           0 :     final u4 = val.toNativeUtf8();
     627           0 :     SetContactAttribute(u1, u1.length, contact, u3, u3.length, u4, u4.length);
     628           0 :     malloc.free(u1);
     629           0 :     malloc.free(u3);
     630           0 :     malloc.free(u4);
     631             :   }
     632             : 
     633           0 :   @override
     634             :   // ignore: non_constant_identifier_names
     635             :   void SetMessageAttribute(String profile, int conversation, int channel, int message, String key, String val) {
     636           0 :     var setMessageAttribute = library.lookup<NativeFunction<void_from_string_int_int_int_string_string_function>>("c_UpdateMessageAttribute");
     637             :     // ignore: non_constant_identifier_names
     638             :     final SetMessageAttribute = setMessageAttribute.asFunction<VoidFromStringIntIntIntStringStringFn>();
     639           0 :     final u1 = profile.toNativeUtf8();
     640           0 :     final u3 = key.toNativeUtf8();
     641           0 :     final u4 = val.toNativeUtf8();
     642           0 :     SetMessageAttribute(u1, u1.length, conversation, channel, message, u3, u3.length, u4, u4.length);
     643           0 :     malloc.free(u1);
     644           0 :     malloc.free(u3);
     645           0 :     malloc.free(u4);
     646             :   }
     647             : 
     648           0 :   @override
     649             :   // ignore: non_constant_identifier_names
     650             :   void LoadServers(String password) {
     651           0 :     var loadServers = library.lookup<NativeFunction<string_to_void_function>>("c_LoadServers");
     652             :     // ignore: non_constant_identifier_names
     653             :     final LoadServers = loadServers.asFunction<StringFn>();
     654           0 :     final u1 = password.toNativeUtf8();
     655           0 :     LoadServers(u1, u1.length);
     656           0 :     malloc.free(u1);
     657             :   }
     658             : 
     659           0 :   @override
     660             :   // ignore: non_constant_identifier_names
     661             :   void CreateServer(String password, String description, bool autostart) {
     662           0 :     var createServer = library.lookup<NativeFunction<void_from_string_string_byte_function>>("c_CreateServer");
     663             :     // ignore: non_constant_identifier_names
     664             :     final CreateServer = createServer.asFunction<VoidFromStringStringByteFn>();
     665           0 :     final u1 = password.toNativeUtf8();
     666           0 :     final u2 = description.toNativeUtf8();
     667           0 :     CreateServer(u1, u1.length, u2, u2.length, autostart ? 1 : 0);
     668           0 :     malloc.free(u1);
     669           0 :     malloc.free(u2);
     670             :   }
     671             : 
     672           0 :   @override
     673             :   // ignore: non_constant_identifier_names
     674             :   void DeleteServer(String serverOnion, String password) {
     675           0 :     var deleteServer = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteServer");
     676             :     // ignore: non_constant_identifier_names
     677             :     final DeleteServer = deleteServer.asFunction<VoidFromStringStringFn>();
     678           0 :     final u1 = serverOnion.toNativeUtf8();
     679           0 :     final u2 = password.toNativeUtf8();
     680           0 :     DeleteServer(u1, u1.length, u2, u2.length);
     681           0 :     malloc.free(u1);
     682           0 :     malloc.free(u2);
     683             :   }
     684             : 
     685           0 :   @override
     686             :   // ignore: non_constant_identifier_names
     687             :   void LaunchServers() {
     688           0 :     var launchServers = library.lookup<NativeFunction<Void Function()>>("c_LaunchServers");
     689             :     // ignore: non_constant_identifier_names
     690             :     final LaunchServers = launchServers.asFunction<void Function()>();
     691           0 :     LaunchServers();
     692             :   }
     693             : 
     694           0 :   @override
     695             :   // ignore: non_constant_identifier_names
     696             :   void LaunchServer(String serverOnion) {
     697           0 :     var launchServer = library.lookup<NativeFunction<string_to_void_function>>("c_LaunchServer");
     698             :     // ignore: non_constant_identifier_names
     699             :     final LaunchServer = launchServer.asFunction<StringFn>();
     700           0 :     final u1 = serverOnion.toNativeUtf8();
     701           0 :     LaunchServer(u1, u1.length);
     702           0 :     malloc.free(u1);
     703             :   }
     704             : 
     705           0 :   @override
     706             :   // ignore: non_constant_identifier_names
     707             :   void StopServer(String serverOnion) {
     708           0 :     var shutdownServer = library.lookup<NativeFunction<string_to_void_function>>("c_StopServer");
     709             :     // ignore: non_constant_identifier_names
     710             :     final ShutdownServer = shutdownServer.asFunction<StringFn>();
     711           0 :     final u1 = serverOnion.toNativeUtf8();
     712           0 :     ShutdownServer(u1, u1.length);
     713           0 :     malloc.free(u1);
     714             :   }
     715             : 
     716           0 :   @override
     717             :   // ignore: non_constant_identifier_names
     718             :   void StopServers() {
     719           0 :     var shutdownServers = library.lookup<NativeFunction<Void Function()>>("c_StopServers");
     720             :     // ignore: non_constant_identifier_names
     721             :     final ShutdownServers = shutdownServers.asFunction<void Function()>();
     722           0 :     ShutdownServers();
     723             :   }
     724             : 
     725           0 :   @override
     726             :   // ignore: non_constant_identifier_names
     727             :   void DestroyServers() {
     728           0 :     var destroyServers = library.lookup<NativeFunction<Void Function()>>("c_DestroyServers");
     729             :     // ignore: non_constant_identifier_names
     730             :     final DestroyServers = destroyServers.asFunction<void Function()>();
     731           0 :     DestroyServers();
     732             :   }
     733             : 
     734           0 :   @override
     735             :   // ignore: non_constant_identifier_names
     736             :   void SetServerAttribute(String serverOnion, String key, String val) {
     737           0 :     var setServerAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetServerAttribute");
     738             :     // ignore: non_constant_identifier_names
     739             :     final SetServerAttribute = setServerAttribute.asFunction<VoidFromStringStringStringFn>();
     740           0 :     final u1 = serverOnion.toNativeUtf8();
     741           0 :     final u2 = key.toNativeUtf8();
     742           0 :     final u3 = val.toNativeUtf8();
     743           0 :     SetServerAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
     744           0 :     malloc.free(u1);
     745           0 :     malloc.free(u2);
     746           0 :     malloc.free(u3);
     747             :   }
     748             : 
     749           0 :   @override
     750             :   // ignore: non_constant_identifier_names
     751             :   Future<void> Shutdown() async {
     752           0 :     var shutdown = library.lookup<NativeFunction<void_from_void_funtion>>("c_ShutdownCwtch");
     753             :     // ignore: non_constant_identifier_names
     754             : 
     755             :     // Shutdown Cwtch + Tor...
     756             :     // ignore: non_constant_identifier_names
     757             :     final Shutdown = shutdown.asFunction<VoidFromVoidFunction>();
     758           0 :     Shutdown();
     759             : 
     760             :     // Kill our Isolate
     761           0 :     cwtchIsolate.kill(priority: Isolate.immediate);
     762           0 :     print("Isolate killed");
     763             : 
     764           0 :     _receivePort.close();
     765           0 :     print("Receive Port Closed");
     766             :   }
     767             : 
     768           0 :   @override
     769             :   // ignore: non_constant_identifier_names
     770             :   Future GetMessageByContentHash(String profile, int handle, String contentHash) async {
     771           0 :     var getMessagesByContentHashC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetMessageByContentHash");
     772             :     // ignore: non_constant_identifier_names
     773             :     final GetMessagesByContentHash = getMessagesByContentHashC.asFunction<GetJsonBlobFromStrIntStringFn>();
     774           0 :     final utf8profile = profile.toNativeUtf8();
     775           0 :     final utf8contentHash = contentHash.toNativeUtf8();
     776           0 :     Pointer<Utf8> jsonMessageBytes = GetMessagesByContentHash(utf8profile, utf8profile.length, handle, utf8contentHash, utf8contentHash.length);
     777           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     778             : 
     779           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     780           0 :     malloc.free(utf8profile);
     781           0 :     malloc.free(utf8contentHash);
     782             :     return jsonMessage;
     783             :   }
     784             : 
     785             :   // ignore: non_constant_identifier_names
     786             :   // Incredibly dangerous function which invokes a free in libCwtch, should only be used
     787             :   // as documented in `MEMORY.md` in libCwtch repo.
     788           0 :   void _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(Pointer<Utf8> ptr) {
     789           0 :     var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
     790             :     final Free = free.asFunction<FreeFn>();
     791           0 :     Free(ptr);
     792             :   }
     793             : 
     794           0 :   @override
     795             :   String? defaultDownloadPath() {
     796           0 :     Map<String, String> envVars = Platform.environment;
     797           0 :     String nominalPath = path.join(envVars[Platform.isWindows ? 'UserProfile' : 'HOME']!, "Downloads");
     798           0 :     if (Directory(nominalPath).existsSync() == false) {
     799           0 :       return Directory.current.path;
     800             :     }
     801             :     return nominalPath;
     802             :   }
     803             : 
     804           0 :   @override
     805             :   // ignore: non_constant_identifier_names
     806             :   Future<String> GetMessageByID(String profile, int handle, int index) async {
     807           0 :     var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessageById");
     808             :     // ignore: non_constant_identifier_names
     809             :     final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
     810           0 :     final utf8profile = profile.toNativeUtf8();
     811           0 :     Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
     812           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     813           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     814           0 :     malloc.free(utf8profile);
     815             :     return jsonMessage;
     816             :   }
     817             : 
     818           0 :   @override
     819             :   // ignore: non_constant_identifier_names
     820             :   void ChangePassword(String profile, String pass, String newpass, String newpassAgain) {
     821           0 :     var changePasswordC = library.lookup<NativeFunction<void_from_string_string_string_string_function>>("c_ChangePassword");
     822             :     // ignore: non_constant_identifier_names
     823             :     final ChangePasswordFn = changePasswordC.asFunction<VoidFromStringStringStringStringFn>();
     824           0 :     final utf8profile = profile.toNativeUtf8();
     825           0 :     final utf8pass = pass.toNativeUtf8();
     826           0 :     final utf8newpass = newpass.toNativeUtf8();
     827           0 :     final utf8newpasssagain = newpassAgain.toNativeUtf8();
     828           0 :     ChangePasswordFn(utf8profile, utf8profile.length, utf8pass, utf8pass.length, utf8newpass, utf8newpass.length, utf8newpasssagain, utf8newpasssagain.length);
     829           0 :     malloc.free(utf8profile);
     830           0 :     malloc.free(utf8pass);
     831           0 :     malloc.free(utf8newpass);
     832           0 :     malloc.free(utf8newpasssagain);
     833             :   }
     834             : 
     835           0 :   @override
     836             :   bool isL10nInit() {
     837           0 :     return _isL10nInit;
     838             :   }
     839             : 
     840           0 :   @override
     841             :   void l10nInit(String notificationSimple, String notificationConversationInfo) {
     842           0 :     cwtchNotifier.l10nInit(notificationSimple, notificationConversationInfo);
     843           0 :     _isL10nInit = true;
     844             :   }
     845             : 
     846           0 :   @override
     847             :   // ignore: non_constant_identifier_names
     848             :   void ExportProfile(String profile, String file) {
     849           0 :     final utf8profile = profile.toNativeUtf8();
     850           0 :     final utf8file = file.toNativeUtf8();
     851           0 :     var exportProfileC = library.lookup<NativeFunction<void_from_string_string_function>>("c_ExportProfile");
     852             :     // ignore: non_constant_identifier_names
     853             :     final ExportProfileFn = exportProfileC.asFunction<VoidFromStringStringFn>();
     854           0 :     ExportProfileFn(utf8profile, utf8profile.length, utf8file, utf8file.length);
     855           0 :     malloc.free(utf8profile);
     856           0 :     malloc.free(utf8file);
     857             :   }
     858             : 
     859           0 :   @override
     860             :   // ignore: non_constant_identifier_names
     861             :   Future<String> ImportProfile(String file, String pass) async {
     862           0 :     final utf8pass = pass.toNativeUtf8();
     863           0 :     final utf8file = file.toNativeUtf8();
     864           0 :     var exportProfileC = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportProfile");
     865             :     // ignore: non_constant_identifier_names
     866             :     final ExportProfileFn = exportProfileC.asFunction<StringFromStringStringFn>();
     867           0 :     Pointer<Utf8> result = ExportProfileFn(utf8file, utf8file.length, utf8pass, utf8pass.length);
     868           0 :     String importResult = result.toDartString();
     869           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
     870           0 :     malloc.free(utf8pass);
     871           0 :     malloc.free(utf8file);
     872             :     return importResult;
     873             :   }
     874             : 
     875           0 :   @override
     876             :   Future<String> GetDebugInfo() async {
     877           0 :     var getDebugInfo = library.lookup<NativeFunction<void_to_string>>("c_GetDebugInfo");
     878             :     final GetDebugInfo = getDebugInfo.asFunction<StringFromVoid>();
     879           0 :     Pointer<Utf8> result = GetDebugInfo();
     880           0 :     String debugResult = result.toDartString();
     881           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
     882             :     return debugResult;
     883             :   }
     884             : 
     885           0 :   @override
     886             :   Future<String> GetSharedFiles(String profile, int handle) async {
     887           0 :     var getSharedFiles = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_GetSharedFiles");
     888             :     final GetSharedFiles = getSharedFiles.asFunction<GetJsonBlobFromStrIntFn>();
     889           0 :     final utf8profile = profile.toNativeUtf8();
     890           0 :     Pointer<Utf8> jsonMessageBytes = GetSharedFiles(utf8profile, utf8profile.length, handle);
     891           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     892           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     893           0 :     malloc.free(utf8profile);
     894             :     return jsonMessage;
     895             :   }
     896             : 
     897           0 :   @override
     898             :   void RestartSharing(String profile, String filekey) {
     899           0 :     var restartSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_RestartFileShare");
     900             :     // ignore: non_constant_identifier_names
     901             :     final RestartSharing = restartSharingC.asFunction<VoidFromStringStringFn>();
     902           0 :     final utf8profile = profile.toNativeUtf8();
     903           0 :     final ut8filekey = filekey.toNativeUtf8();
     904           0 :     RestartSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
     905           0 :     malloc.free(utf8profile);
     906           0 :     malloc.free(ut8filekey);
     907             :   }
     908             : 
     909           0 :   @override
     910             :   void StopSharing(String profile, String filekey) {
     911           0 :     var stopSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_StopFileShare");
     912             :     // ignore: non_constant_identifier_names
     913             :     final StopSharing = stopSharingC.asFunction<VoidFromStringStringFn>();
     914           0 :     final utf8profile = profile.toNativeUtf8();
     915           0 :     final ut8filekey = filekey.toNativeUtf8();
     916           0 :     StopSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
     917           0 :     malloc.free(utf8profile);
     918           0 :     malloc.free(ut8filekey);
     919             :   }
     920             : 
     921           0 :   @override
     922             :   void DeleteServerInfo(String profile, String handle) {
     923           0 :     var deleteServerInfoC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DeleteServerInfo");
     924             :     // ignore: non_constant_identifier_names
     925             :     final StopSharing = deleteServerInfoC.asFunction<VoidFromStringStringFn>();
     926           0 :     final utf8profile = profile.toNativeUtf8();
     927           0 :     final ut8handle = handle.toNativeUtf8();
     928           0 :     StopSharing(utf8profile, utf8profile.length, ut8handle, ut8handle.length);
     929           0 :     malloc.free(utf8profile);
     930           0 :     malloc.free(ut8handle);
     931             :   }
     932             : 
     933           0 :   @override
     934             :   void UpdateSettings(String json) {
     935           0 :     var updateSettings = library.lookup<NativeFunction<string_to_void_function>>("c_UpdateSettings");
     936             :     // ignore: non_constant_identifier_names
     937             :     final UpdateSettingsFn = updateSettings.asFunction<VoidFromStringFn>();
     938           0 :     final u1 = json.toNativeUtf8();
     939           0 :     UpdateSettingsFn(u1, u1.length);
     940           0 :     malloc.free(u1);
     941             :   }
     942             : 
     943           0 :   @override
     944             :   bool IsServersCompiled() {
     945           0 :     return library.providesSymbol("c_LoadServers");
     946             :   }
     947             : 
     948           0 :   @override
     949             :   Future<String> SummarizeConversation(String profile, int conversation) async {
     950           0 :     if (!library.providesSymbol("c_Summarize")) {
     951           0 :       return Future.value("");
     952             :     }
     953           0 :     var summarize = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_Summarize");
     954             :     // ignore: non_constant_identifier_names
     955             :     final SummarizeFn = summarize.asFunction<GetJsonBlobFromStrIntFn>();
     956           0 :     final utf8profile = profile.toNativeUtf8();
     957           0 :     Pointer<Utf8> jsonMessageBytes = SummarizeFn(utf8profile, utf8profile.length, conversation);
     958           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     959           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     960           0 :     malloc.free(utf8profile);
     961             :     return jsonMessage;
     962             :   }
     963             : 
     964           0 :   @override
     965             :   Future<String> TranslateMessage(String profile, int conversation, int message, String language) async {
     966           0 :     if (!library.providesSymbol("c_Translate")) {
     967           0 :       return Future.value("");
     968             :     }
     969           0 :     var translate = library.lookup<NativeFunction<get_json_blob_from_str_int_int_str_function>>("c_Translate");
     970             :     // ignore: non_constant_identifier_names
     971             :     final TranslateFn = translate.asFunction<GetJsonBlobFromStrIntIntStrFn>();
     972           0 :     final utf8profile = profile.toNativeUtf8();
     973           0 :     final utf8lang = language.toNativeUtf8();
     974           0 :     Pointer<Utf8> jsonMessageBytes = TranslateFn(
     975             :       utf8profile,
     976           0 :       utf8profile.length,
     977             :       conversation,
     978             :       message,
     979             :       utf8lang,
     980           0 :       utf8lang.length,
     981             :     );
     982           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     983           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     984           0 :     malloc.free(utf8profile);
     985           0 :     malloc.free(utf8lang);
     986             :     return jsonMessage;
     987             :   }
     988             : 
     989           0 :   @override
     990             :   bool IsBlodeuweddSupported() {
     991           0 :     if (library.providesSymbol("c_Translate")) {
     992             :       return true;
     993             :     }
     994             :     return false;
     995             :   }
     996             : 
     997           0 :   @override
     998             :   Future<String?> GetProfileAttribute(String profile, String key) {
     999           0 :     var getProfileAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_str_function>>("c_GetProfileAttribute");
    1000             :     // ignore: non_constant_identifier_names
    1001             :     final GetProfileAttribute = getProfileAttributeC.asFunction<GetJsonBlobFromStrStrFn>();
    1002           0 :     final utf8profile = profile.toNativeUtf8();
    1003           0 :     final utf8key = key.toNativeUtf8();
    1004           0 :     Pointer<Utf8> jsonMessageBytes = GetProfileAttribute(utf8profile, utf8profile.length, utf8key, utf8key.length);
    1005           0 :     String jsonMessage = jsonMessageBytes.toDartString();
    1006           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
    1007           0 :     malloc.free(utf8profile);
    1008           0 :     malloc.free(utf8key);
    1009             : 
    1010             :     try {
    1011           0 :       dynamic attributeResult = json.decode(jsonMessage);
    1012           0 :       if (attributeResult["Exists"]) {
    1013           0 :         return Future.value(attributeResult["Value"]);
    1014             :       }
    1015             :     } catch (e) {
    1016           0 :       EnvironmentConfig.debugLog("error getting profile attribute: $e");
    1017             :     }
    1018             : 
    1019           0 :     return Future.value(null);
    1020             :   }
    1021             : 
    1022           0 :   @override
    1023             :   Future<String?> GetConversationAttribute(String profile, int conversation, String key) {
    1024           0 :     var getConversationAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetConversationAttribute");
    1025             :     // ignore: non_constant_identifier_names
    1026             :     final GetConversationAttribute = getConversationAttributeC.asFunction<GetJsonBlobFromStrIntStringFn>();
    1027           0 :     final utf8profile = profile.toNativeUtf8();
    1028           0 :     final utf8key = key.toNativeUtf8();
    1029           0 :     Pointer<Utf8> jsonMessageBytes = GetConversationAttribute(utf8profile, utf8profile.length, conversation, utf8key, utf8key.length);
    1030           0 :     String jsonMessage = jsonMessageBytes.toDartString();
    1031           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
    1032           0 :     malloc.free(utf8profile);
    1033           0 :     malloc.free(utf8key);
    1034             : 
    1035             :     try {
    1036           0 :       dynamic attributeResult = json.decode(jsonMessage);
    1037           0 :       if (attributeResult["Exists"]) {
    1038           0 :         return Future.value(attributeResult["Value"]);
    1039             :       }
    1040             :     } catch (e) {
    1041           0 :       EnvironmentConfig.debugLog("error getting profile attribute: $e");
    1042             :     }
    1043             : 
    1044           0 :     return Future.value(null);
    1045             :   }
    1046             : 
    1047           0 :   @override
    1048             :   void AttemptReconnection(String profile, String onion) {
    1049             :     // ignore: non_constant_identifier_names
    1050           0 :     var peerWithOnionC = library.lookup<NativeFunction<void_from_string_string_function>>("c_PeerWithOnion");
    1051             :     final PeerWithOnionF = peerWithOnionC.asFunction<VoidFromStringStringFn>();
    1052           0 :     final utf8profile = profile.toNativeUtf8();
    1053           0 :     final utf8onion = onion.toNativeUtf8();
    1054           0 :     PeerWithOnionF(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1055           0 :     malloc.free(utf8profile);
    1056           0 :     malloc.free(utf8onion);
    1057             :   }
    1058             : 
    1059           0 :   @override
    1060             :   void AttemptReconnectionServer(String profile, String onion) {
    1061             :     // ignore: non_constant_identifier_names
    1062           0 :     var queueJoinServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_QueueJoinServer");
    1063             :     final QueueJoinServerC = queueJoinServerC.asFunction<VoidFromStringStringFn>();
    1064           0 :     final utf8profile = profile.toNativeUtf8();
    1065           0 :     final utf8onion = onion.toNativeUtf8();
    1066           0 :     QueueJoinServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1067           0 :     malloc.free(utf8profile);
    1068           0 :     malloc.free(utf8onion);
    1069             :   }
    1070             : 
    1071           0 :   @override
    1072             :   void DisconnectFromPeer(String profile, String onion) {
    1073             :     // ignore: non_constant_identifier_names
    1074           0 :     var disconnectFromPeerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromPeer");
    1075             :     final DisconnectFromPeerC = disconnectFromPeerC.asFunction<VoidFromStringStringFn>();
    1076           0 :     final utf8profile = profile.toNativeUtf8();
    1077           0 :     final utf8onion = onion.toNativeUtf8();
    1078           0 :     DisconnectFromPeerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1079           0 :     malloc.free(utf8profile);
    1080           0 :     malloc.free(utf8onion);
    1081             :   }
    1082             : 
    1083           0 :   @override
    1084             :   void DisconnectFromServer(String profile, String onion) {
    1085             :     // ignore: non_constant_identifier_names
    1086           0 :     var disconnectFromServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromServer");
    1087             :     final DisconnectFromServerC = disconnectFromServerC.asFunction<VoidFromStringStringFn>();
    1088           0 :     final utf8profile = profile.toNativeUtf8();
    1089           0 :     final utf8onion = onion.toNativeUtf8();
    1090           0 :     DisconnectFromServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1091           0 :     malloc.free(utf8profile);
    1092           0 :     malloc.free(utf8onion);
    1093             :   }
    1094             : 
    1095           0 :   @override
    1096             :   Future<String> SearchConversations(String profile, String pattern) async {
    1097           0 :     var searchConversationsC = library.lookup<NativeFunction<string_string_to_string_function>>("c_SearchConversations");
    1098             :     // ignore: non_constant_identifier_names
    1099             :     final SearchConversations = searchConversationsC.asFunction<StringFromStringStringFn>();
    1100           0 :     final utf8profile = profile.toNativeUtf8();
    1101           0 :     final utf8pattern = pattern.toNativeUtf8();
    1102           0 :     EnvironmentConfig.debugLog("Searching for $profile $pattern");
    1103           0 :     Pointer<Utf8> searchIDRaw = SearchConversations(utf8profile, utf8profile.length, utf8pattern, utf8pattern.length);
    1104           0 :     String searchID = searchIDRaw.toDartString();
    1105           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(searchIDRaw);
    1106           0 :     malloc.free(utf8profile);
    1107           0 :     malloc.free(utf8pattern);
    1108             :     return searchID;
    1109             :   }
    1110             : 
    1111           0 :   @override
    1112             :   Future<HashMap<String, String>> PlatformChannelInfo() {
    1113           0 :     return Future.value(HashMap<String, String>());
    1114             :   }
    1115             : 
    1116           0 :   @override
    1117             :   Future<void> ConfigureConnections(String profile, bool listen, bool peers, bool servers) async {
    1118           0 :     var configureConnections = library.lookup<NativeFunction<void_from_string_bool_bool_bool>>("c_ConfigureConnections");
    1119             :     // ignore: non_constant_identifier_names
    1120             :     final ConfigureConnections = configureConnections.asFunction<VoidFromStringBoolBoolBool>();
    1121           0 :     final utf8profile = profile.toNativeUtf8();
    1122           0 :     ConfigureConnections(utf8profile, utf8profile.length, listen, peers, servers);
    1123           0 :     malloc.free(utf8profile);
    1124             :     return;
    1125             :   }
    1126             : 
    1127           0 :   @override
    1128             :   void PublishServerUpdate(String profile) {
    1129           0 :     var publishServerUpdate = library.lookup<NativeFunction<string_to_void_function>>("c_PublishServerUpdate");
    1130             :     // ignore: non_constant_identifier_names
    1131             :     final PublishServerUpdate = publishServerUpdate.asFunction<StringFn>();
    1132           0 :     final utf8profile = profile.toNativeUtf8();
    1133           0 :     PublishServerUpdate(utf8profile, utf8profile.length);
    1134           0 :     malloc.free(utf8profile);
    1135             :   }
    1136             : 
    1137           0 :   @override
    1138             :   bool IsLoaded() {
    1139           0 :     bool check = library.providesSymbol("c_UpdateSettings");
    1140           0 :     EnvironmentConfig.debugLog("Checking that the FFI Interface is Correctly Loaded... $check");
    1141             :     return check;
    1142             :   }
    1143             : }

Generated by: LCOV version 1.14