feat: Add helper functions for field Ids

This commit is contained in:
PapaTutuWawa 2022-08-04 23:08:02 +02:00
parent a82b5d0990
commit b8d70f1b88
2 changed files with 12 additions and 4 deletions

View File

@ -9,7 +9,7 @@ class OmemoMessage {
var i = 0; var i = 0;
// required uint32 n = 1; // required uint32 n = 1;
if (data[0] != 8) { if (data[0] != fieldId(1, fieldTypeUint32)) {
throw Exception(); throw Exception();
} }
var decode = decodeVarint(data, 1); var decode = decodeVarint(data, 1);
@ -17,7 +17,7 @@ class OmemoMessage {
i += decode.length + 1; i += decode.length + 1;
// required uint32 pn = 2; // required uint32 pn = 2;
if (data[i] != 16) { if (data[i] != fieldId(2, fieldTypeUint32)) {
throw Exception(); throw Exception();
} }
decode = decodeVarint(data, i + 1); decode = decodeVarint(data, i + 1);
@ -25,7 +25,7 @@ class OmemoMessage {
i += decode.length + 1; i += decode.length + 1;
// required bytes dh_pub = 3; // required bytes dh_pub = 3;
if (data[i] != ((3 << 3) | 2)) { if (data[i] != fieldId(3, fieldTypeByteArray)) {
throw Exception(); throw Exception();
} }
final dhPub = data.sublist(i + 2, i + 2 + data[i + 1]); final dhPub = data.sublist(i + 2, i + 2 + data[i + 1]);
@ -34,7 +34,7 @@ class OmemoMessage {
// optional bytes ciphertext = 4; // optional bytes ciphertext = 4;
List<int>? ciphertext; List<int>? ciphertext;
if (i < data.length) { if (i < data.length) {
if (data[i] != ((4 << 3) | 2)) { if (data[i] != fieldId(4, fieldTypeByteArray)) {
throw Exception(); throw Exception();
} }

View File

@ -4,6 +4,14 @@ const lsb7Mask = 0x7F;
/// Constant for setting the MSB /// Constant for setting the MSB
const msb = 1 << 7; const msb = 1 << 7;
/// Field types
const fieldTypeUint32 = 0;
const fieldTypeByteArray = 2;
int fieldId(int number, int type) {
return (number << 3) | type;
}
class VarintDecode { class VarintDecode {
const VarintDecode(this.n, this.length); const VarintDecode(this.n, this.length);