@@ -93,7 +93,7 @@ public QRCodeData CreateQrCode(byte[] binaryData, ECCLevel eccLevel)
9393 /// <returns>Returns the raw QR code data which can be used for rendering.</returns>
9494 public static QRCodeData GenerateQrCode ( PayloadGenerator . Payload payload )
9595 {
96- return GenerateQrCode ( payload . ToString ( ) , payload . EccLevel ?? ECCLevel . M , false , false , payload . EciMode , payload . Version ) ;
96+ return GenerateQrCode ( payload . ToString ( ) , payload . EccLevel , false , false , payload . EciMode , payload . Version ) ;
9797 }
9898
9999 /// <summary>
@@ -105,8 +105,10 @@ public static QRCodeData GenerateQrCode(PayloadGenerator.Payload payload)
105105 /// <returns>Returns the raw QR code data which can be used for rendering.</returns>
106106 public static QRCodeData GenerateQrCode ( PayloadGenerator . Payload payload , ECCLevel eccLevel )
107107 {
108- if ( payload . EccLevel . HasValue && eccLevel != payload . EccLevel . Value )
109- throw new ArgumentException ( $ "The provided payload requires a ECC level of { eccLevel } .", nameof ( eccLevel ) ) ;
108+ if ( eccLevel == ECCLevel . Default )
109+ eccLevel = payload . EccLevel ;
110+ else if ( payload . EccLevel != ECCLevel . Default && eccLevel != payload . EccLevel )
111+ throw new ArgumentOutOfRangeException ( nameof ( eccLevel ) , $ "The provided payload requires a ECC level of { eccLevel } .") ;
110112 return GenerateQrCode ( payload . ToString ( ) , eccLevel , false , false , payload . EciMode , payload . Version ) ;
111113 }
112114
@@ -123,6 +125,7 @@ public static QRCodeData GenerateQrCode(PayloadGenerator.Payload payload, ECCLev
123125 /// <returns>Returns the raw QR code data which can be used for rendering.</returns>
124126 public static QRCodeData GenerateQrCode ( string plainText , ECCLevel eccLevel , bool forceUtf8 = false , bool utf8BOM = false , EciMode eciMode = EciMode . Default , int requestedVersion = - 1 )
125127 {
128+ eccLevel = ValidateECCLevel ( eccLevel ) ;
126129 EncodingMode encoding = GetEncodingFromPlaintext ( plainText , forceUtf8 ) ;
127130 var codedText = PlainTextToBinary ( plainText , encoding , eciMode , utf8BOM , forceUtf8 ) ;
128131 var dataInputLength = GetDataLength ( encoding , plainText , codedText , forceUtf8 ) ;
@@ -167,7 +170,6 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
167170 return GenerateQrCode ( completeBitArray , eccLevel , version ) ;
168171 }
169172
170-
171173 /// <summary>
172174 /// Calculates the QR code data which than can be used in one of the rendering classes to generate a graphical representation.
173175 /// </summary>
@@ -177,6 +179,7 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
177179 /// <returns>Returns the raw QR code data which can be used for rendering.</returns>
178180 public static QRCodeData GenerateQrCode ( byte [ ] binaryData , ECCLevel eccLevel )
179181 {
182+ eccLevel = ValidateECCLevel ( eccLevel ) ;
180183 int version = GetVersion ( binaryData . Length , EncodingMode . Byte , eccLevel ) ;
181184
182185 int countIndicatorLen = GetCountIndicatorLength ( version , EncodingMode . Byte ) ;
@@ -189,6 +192,27 @@ public static QRCodeData GenerateQrCode(byte[] binaryData, ECCLevel eccLevel)
189192 return GenerateQrCode ( bitArray , eccLevel , version ) ;
190193 }
191194
195+ /// <summary>
196+ /// Validates the specified error correction level.
197+ /// Returns the provided level if it is valid, or the level M if the provided level is Default.
198+ /// Throws an exception if an invalid level is provided.
199+ /// </summary>
200+ private static ECCLevel ValidateECCLevel ( ECCLevel eccLevel )
201+ {
202+ switch ( eccLevel )
203+ {
204+ case ECCLevel . L :
205+ case ECCLevel . M :
206+ case ECCLevel . Q :
207+ case ECCLevel . H :
208+ return eccLevel ;
209+ case ECCLevel . Default :
210+ return ECCLevel . M ;
211+ default :
212+ throw new ArgumentOutOfRangeException ( nameof ( eccLevel ) , eccLevel , "Invalid error correction level." ) ;
213+ }
214+ }
215+
192216 private static readonly BitArray _repeatingPattern = new BitArray (
193217 new [ ] { true , true , true , false , true , true , false , false , false , false , false , true , false , false , false , true } ) ;
194218
0 commit comments