From 70f106e0963a88ef5844f6343380a9316b863d59 Mon Sep 17 00:00:00 2001 From: Mohsen <56779182+mrtnetwork@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:50:43 +0330 Subject: [PATCH] Resolve issue with CBOR encoding for large negative integers. --- lib/cbor/types/int.dart | 10 ++++++++-- test/cbor_test.dart | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/cbor/types/int.dart b/lib/cbor/types/int.dart index 939d1c3..8f2d26d 100644 --- a/lib/cbor/types/int.dart +++ b/lib/cbor/types/int.dart @@ -19,8 +19,14 @@ class CborIntValue implements CborNumeric { @override List encode() { final bytes = CborBytesTracker(); - bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt, - value.isNegative ? ~value : value); + if (value.bitLength > 31 && value.isNegative) { + final value = (~BigInt.parse(this.value.toString())).toInt(); + bytes.pushInt(MajorTags.negInt, value); + } else { + // print("is here lower!"); + bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt, + value.isNegative ? ~value : value); + } return bytes.toBytes(); } diff --git a/test/cbor_test.dart b/test/cbor_test.dart index 1972854..ecc8113 100644 --- a/test/cbor_test.dart +++ b/test/cbor_test.dart @@ -106,7 +106,6 @@ void _decodeDateTime() { } void main() { - // decode test("cbor decode", () { _decodeInt(); _decodeFloat();