Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/ndef.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ List<NDEFRecord> decodeRawNdefMessage(
while (!stream.isEnd()) {
var record = NDEFRecord.decodeStream(stream, typeFactory);
if (records.isEmpty) {
assert(record.flags.MB == true, "MB flag is not set in first record");
if (record.flags.MB != true) {
throw FormatException("MB flag is not set in first record");
}
} else {
assert(record.flags.MB == false, "MB flag is set in middle record");
if (record.flags.MB != false) {
throw FormatException("MB flag is set in middle record");
}
}
records.add(record);
}
assert(records.last.flags.ME == true, "ME flag is not set in last record");
assert(records.last.flags.CF == false, "CF flag is set in last record");
if (records.last.flags.ME != true) {
throw FormatException("ME flag is not set in last record");
}
if (records.last.flags.CF != false) {
throw FormatException("CF flag is set in last record");
}
return records;
}

Expand Down
20 changes: 13 additions & 7 deletions lib/record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class NDEFRecordFlags {

/// Encodes the flags into a single byte.
int encode() {
assert(0 <= TNF && TNF <= 7);
if (TNF < 0 || TNF > 7) {
throw RangeError.range(TNF, 0, 7, "TNF");
}
return (MB.toInt() << 7) |
(ME.toInt() << 6) |
(CF.toInt() << 5) |
Expand Down Expand Up @@ -336,15 +338,19 @@ class NDEFRecord {
idLength = stream.readByte();
}

if ([0, 5, 6].contains(flags.TNF)) {
assert(typeLength == 0, "TYPE_LENGTH must be 0 when TNF is 0,5,6");
if ([0, 5, 6].contains(flags.TNF) && typeLength != 0) {
throw FormatException("TYPE_LENGTH must be 0 when TNF is 0,5,6");
}
if (flags.TNF == 0) {
assert(idLength == 0, "ID_LENGTH must be 0 when TNF is 0");
assert(payloadLength == 0, "PAYLOAD_LENGTH must be 0 when TNF is 0");
if (idLength != 0) {
throw FormatException("ID_LENGTH must be 0 when TNF is 0");
}
if (payloadLength != 0) {
throw FormatException("PAYLOAD_LENGTH must be 0 when TNF is 0");
}
}
if ([1, 2, 3, 4].contains(flags.TNF)) {
assert(typeLength > 0, "TYPE_LENGTH must be > 0 when TNF is 1,2,3,4");
if ([1, 2, 3, 4].contains(flags.TNF) && typeLength <= 0) {
throw FormatException("TYPE_LENGTH must be > 0 when TNF is 1,2,3,4");
}

var type = stream.readBytes(typeLength);
Expand Down
Loading