37 lines
929 B
Dart
37 lines
929 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 toBytesAndFree() {
|
|
try {
|
|
if (base == nullptr || length <= 0) return .new(0);
|
|
return .fromList(base.asTypedList(length));
|
|
} finally {
|
|
GomuksFreeBuffer(this);
|
|
}
|
|
}
|
|
|
|
dynamic toJsonAndFree() => jsonDecode(utf8.decode(toBytesAndFree()));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|