Skip to content

Commit

Permalink
Added additional checks for DataTooLongExceptions
Browse files Browse the repository at this point in the history
Added checks for too huge payloads in case version was set manually/fix.
  • Loading branch information
codebude committed Nov 24, 2020
1 parent 8a18915 commit 49d5b35
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions QRCoder/Exceptions/DataTooLongException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class DataTooLongException : Exception
public DataTooLongException(string eccLevel, string encodingMode, int maxSizeByte) : base(
$"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."
){}

public DataTooLongException(string eccLevel, string encodingMode, int version, int maxSizeByte) : base(
$"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."
)
{ }
}
}
10 changes: 10 additions & 0 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
{
version = GetVersion(dataInputLength+(eciMode != EciMode.Default?2:0), encoding, eccLevel);
}
else
{
//Version was passed as fixed version via parameter. Thus let's check if chosen version is valid.
var minVersion = GetVersion(dataInputLength + (eciMode != EciMode.Default ? 2 : 0), encoding, eccLevel);
if (minVersion > version)
{
var maxSizeByte = capacityTable[version - 1].Details.First(x => x.ErrorCorrectionLevel == eccLevel).CapacityDict[encoding];
throw new QRCoder.Exceptions.DataTooLongException(eccLevel.ToString(), encoding.ToString(), version, maxSizeByte);
}
}

string modeIndicator = String.Empty;
if (eciMode != EciMode.Default)
Expand Down

0 comments on commit 49d5b35

Please sign in to comment.