56 lines
1.6 KiB
Dart
56 lines
1.6 KiB
Dart
import 'dart:ffi' as ffi;
|
|
import 'dart:io';
|
|
|
|
import 'package:ffi/ffi.dart';
|
|
|
|
import 'deezcryptor_bindings_generated.dart';
|
|
|
|
const String _libName = 'deezcryptor';
|
|
|
|
/// The dynamic library in which the symbols for [DeezcryptorBindings] can be found.
|
|
final ffi.DynamicLibrary _dylib = () {
|
|
if (Platform.isMacOS || Platform.isIOS) {
|
|
return ffi.DynamicLibrary.open('$_libName.framework/$_libName');
|
|
}
|
|
if (Platform.isAndroid || Platform.isLinux) {
|
|
return ffi.DynamicLibrary.open('lib$_libName.so');
|
|
}
|
|
if (Platform.isWindows) {
|
|
return ffi.DynamicLibrary.open('$_libName.dll');
|
|
}
|
|
|
|
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
|
|
}();
|
|
|
|
/// The bindings to the native functions in [_dylib].
|
|
final DeezcryptorBindings _bindings = DeezcryptorBindings(_dylib);
|
|
|
|
final decryptChunkRaw = _bindings.decryptChunk;
|
|
|
|
List<int> decryptChunk(List<int> data, List<int> key) {
|
|
final ptr = malloc<ffi.Char>(data.length);
|
|
final ptr1 = listIntToPointerChar(data);
|
|
final ptr2 = listIntToPointerChar(key);
|
|
final size = _bindings.decryptChunk(ptr, ptr1, ptr2);
|
|
malloc.free(ptr1);
|
|
malloc.free(ptr2);
|
|
final dec = pointerCharToListInt(ptr, size);
|
|
malloc.free(ptr);
|
|
return dec;
|
|
}
|
|
|
|
ffi.Pointer<ffi.Char> listIntToPointerChar<T>(List<int> list) {
|
|
final length = list.length;
|
|
final ptr = malloc<ffi.Char>(length);
|
|
for (int i = 0; i < length; i++) {
|
|
ptr[i] = list[i];
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
List<int> pointerCharToListInt(ffi.Pointer<ffi.Char> ptr, int size) {
|
|
return List<int>.unmodifiable(
|
|
Iterable<int>.generate(size, (index) => ptr[index]));
|
|
}
|