forked from Nexus/nexus
Now this feature is stable, we should use it. I might have missed some usecases, but these can be added in future commits.
36 lines
902 B
Dart
36 lines
902 B
Dart
import "dart:convert";
|
|
import "dart:ffi";
|
|
import "dart:typed_data";
|
|
import "package:ffi/ffi.dart";
|
|
import "package:nexus/src/third_party/gomuks.g.dart";
|
|
|
|
extension GomuksOwnedBufferToX on GomuksOwnedBuffer {
|
|
Uint8List toBytes() {
|
|
try {
|
|
if (base == nullptr || length <= 0) return .new(0);
|
|
return .fromList(base.asTypedList(length));
|
|
} finally {
|
|
calloc.free(base);
|
|
}
|
|
}
|
|
|
|
dynamic toJson() => jsonDecode(utf8.decode(toBytes()));
|
|
}
|
|
|
|
extension JsonToGomuksBuffer on Map<String, dynamic> {
|
|
Pointer<GomuksBorrowedBuffer> toGomuksBufferPtr() {
|
|
final jsonString = json.encode(this);
|
|
final bytes = utf8.encode(jsonString);
|
|
|
|
final dataPtr = calloc<Uint8>(bytes.length);
|
|
dataPtr.asTypedList(bytes.length).setAll(0, bytes);
|
|
|
|
final ptr = calloc<GomuksBorrowedBuffer>();
|
|
|
|
ptr.ref
|
|
..base = dataPtr
|
|
..length = bytes.length;
|
|
|
|
return ptr;
|
|
}
|
|
}
|