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

2
lib/src/kdbx_entry.dart

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

6
lib/src/kdbx_format.dart

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

2
lib/src/utils/print_utils.dart

@ -34,7 +34,7 @@ class KdbxPrintUtils {
.join('\n')); .join('\n'));
} }
buf.writeln(entry.binaryEntries 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')); .join('\n'));
} }
} }

6
test/kdbx_binaries_test.dart

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

Loading…
Cancel
Save