Skip to content

Commit 49d5b35

Browse files
committed
Added additional checks for DataTooLongExceptions
Added checks for too huge payloads in case version was set manually/fix.
1 parent 8a18915 commit 49d5b35

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

QRCoder/Exceptions/DataTooLongException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ public class DataTooLongException : Exception
77
public DataTooLongException(string eccLevel, string encodingMode, int maxSizeByte) : base(
88
$"The given payload exceeds the maximum size of the QR code standard. The maximum size allowed for the choosen paramters (ECC level={eccLevel}, EncodingMode={encodingMode}) is {maxSizeByte} byte."
99
){}
10+
11+
public DataTooLongException(string eccLevel, string encodingMode, int version, int maxSizeByte) : base(
12+
$"The given payload exceeds the maximum size of the QR code standard. The maximum size allowed for the choosen paramters (ECC level={eccLevel}, EncodingMode={encodingMode}, FixedVersion={version}) is {maxSizeByte} byte."
13+
)
14+
{ }
1015
}
1116
}

QRCoder/QRCodeGenerator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
131131
{
132132
version = GetVersion(dataInputLength+(eciMode != EciMode.Default?2:0), encoding, eccLevel);
133133
}
134+
else
135+
{
136+
//Version was passed as fixed version via parameter. Thus let's check if chosen version is valid.
137+
var minVersion = GetVersion(dataInputLength + (eciMode != EciMode.Default ? 2 : 0), encoding, eccLevel);
138+
if (minVersion > version)
139+
{
140+
var maxSizeByte = capacityTable[version - 1].Details.First(x => x.ErrorCorrectionLevel == eccLevel).CapacityDict[encoding];
141+
throw new QRCoder.Exceptions.DataTooLongException(eccLevel.ToString(), encoding.ToString(), version, maxSizeByte);
142+
}
143+
}
134144

135145
string modeIndicator = String.Empty;
136146
if (eciMode != EciMode.Default)

0 commit comments

Comments
 (0)