Browse Source

fix nullability for KdbxBinary

pull/5/head
Herbert Poul 3 years ago
parent
commit
5cf6b8db93
  1. 24
      lib/src/kdbx_binary.dart
  2. 2
      lib/src/kdbx_entry.dart
  3. 6
      lib/src/kdbx_format.dart
  4. 2
      lib/src/utils/print_utils.dart
  5. 6
      test/kdbx_binaries_test.dart

24
lib/src/kdbx_binary.dart

@ -9,10 +9,14 @@ import 'package:quiver/core.dart';
import 'package:xml/xml.dart';
class KdbxBinary {
KdbxBinary({this.isInline, this.isProtected, this.value});
final bool? isInline;
final bool? isProtected;
final Uint8List? value;
KdbxBinary({
required this.isInline,
required this.isProtected,
required this.value,
});
final bool isInline;
final bool isProtected;
final Uint8List value;
int? _valueHashCode;
static KdbxBinary readBinaryInnerHeader(InnerHeaderField field) {
@ -26,16 +30,16 @@ class KdbxBinary {
);
}
int get valueHashCode => _valueHashCode ??= hashObjects(value!);
int get valueHashCode => _valueHashCode ??= hashObjects(value);
bool valueEqual(KdbxBinary other) =>
valueHashCode == other.valueHashCode && ByteUtils.eq(value!, value!);
valueHashCode == other.valueHashCode && ByteUtils.eq(value, other.value);
InnerHeaderField writeToInnerHeader() {
final writer = WriterHelper();
final flags = isProtected! ? 0x01 : 0x00;
final flags = isProtected ? 0x01 : 0x00;
writer.writeUint8(flags);
writer.writeBytes(value!);
writer.writeBytes(value);
return InnerHeaderField(
InnerHeaderFields.Binary, writer.output.takeBytes());
}
@ -56,8 +60,8 @@ class KdbxBinary {
}
void saveToXml(XmlElement valueNode) {
final content = base64.encode(gzip.encode(value!));
valueNode.addAttributeBool(KdbxXml.ATTR_PROTECTED, isProtected!);
final content = base64.encode(gzip.encode(value));
valueNode.addAttributeBool(KdbxXml.ATTR_PROTECTED, isProtected);
valueNode.addAttributeBool(KdbxXml.ATTR_COMPRESSED, true);
valueNode.children.add(XmlText(content));
}

2
lib/src/kdbx_entry.dart

@ -267,7 +267,7 @@ class KdbxEntry extends KdbxObject {
final key = binaryEntry.key;
final binary = binaryEntry.value;
final value = XmlElement(XmlName(KdbxXml.NODE_VALUE));
if (binary.isInline!) {
if (binary.isInline) {
binary.saveToXml(value);
} else {
final binaryIndex = ctx.findBinaryId(binary);

6
lib/src/kdbx_format.dart

@ -90,11 +90,11 @@ class KdbxReadWriteContext {
/// finds the ID of the given binary.
/// if it can't be found, [KdbxCorruptedFileException] is thrown.
int findBinaryId(KdbxBinary binary) {
assert(!binary.isInline!);
assert(!binary.isInline);
final id = _binaries.indexOf(binary);
if (id < 0) {
throw KdbxCorruptedFileException('Unable to find binary.'
' (${binary.value!.length},${binary.isInline})');
' (${binary.value.length},${binary.isInline})');
}
return id;
}
@ -231,7 +231,7 @@ class KdbxBody extends KdbxNode {
if (ctx.findBinaryByValue(binary) == null) {
ctx.addBinary(binary);
mergeContext.trackChange(this,
debug: 'adding new binary ${binary.value!.length}');
debug: 'adding new binary ${binary.value.length}');
}
}
meta.merge(other.meta);

2
lib/src/utils/print_utils.dart

@ -34,7 +34,7 @@ class KdbxPrintUtils {
.join('\n'));
}
buf.writeln(entry.binaryEntries
.map((b) => '$indent `- file: ${b.key} - ${b.value.value!.length}')
.map((b) => '$indent `- file: ${b.key} - ${b.value.value.length}')
.join('\n'));
}
}

6
test/kdbx_binaries_test.dart

@ -55,10 +55,10 @@ void main() {
for (final binary in binaries) {
switch (binary.key.key) {
case 'example1.txt':
expect(utf8.decode(binary.value.value!), 'content1 example\n\n');
expect(utf8.decode(binary.value.value), 'content1 example\n\n');
break;
case 'example2.txt':
expect(utf8.decode(binary.value.value!), 'content2 example\n\n');
expect(utf8.decode(binary.value.value), 'content2 example\n\n');
break;
case 'keepasslogo.jpeg':
expect(binary.value.value, hasLength(7092));
@ -126,7 +126,7 @@ void main() {
final entry = file.body.rootGroup.entries.first;
for (final name in ['a', 'b', 'c', 'd', 'e']) {
expect(
utf8.decode(entry.getBinary(KdbxKey('$name.txt'))!.value!).trim(),
utf8.decode(entry.getBinary(KdbxKey('$name.txt'))!.value).trim(),
name,
);
}

Loading…
Cancel
Save