Simplify QRCode library for bitcoin addresses
MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/CharCountIndicatorTable.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/CharCountIndicatorTable.cs
@@ -5,30 +5,14 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
public static class CharCountIndicatorTable
{
/// <remarks>ISO/IEC 18004:2000 Table 3 Page 18</remarks>
- public static int[] GetCharCountIndicatorSet(Mode mode)
+ public static int[] GetCharCountIndicatorSet()
{
- switch (mode)
- {
- case Mode.Numeric:
- return new int[] { 10, 12, 14 };
-
- case Mode.Alphanumeric:
- return new int[] { 9, 11, 13 };
-
- case Mode.EightBitByte:
return new int[] { 8, 16, 16 };
-
- case Mode.Kanji:
- return new int[] { 8, 10, 12 };
-
- default:
- throw new System.InvalidOperationException($"Unexpected Mode: {mode.ToString()}");
- }
} //
- public static int GetBitCountInCharCountIndicator(Mode mode, int version)
+ public static int GetBitCountInCharCountIndicator(int version)
{
- int[] charCountIndicatorSet = GetCharCountIndicatorSet(mode);
+ int[] charCountIndicatorSet = GetCharCountIndicatorSet();
int versionGroup = GetVersionGroup(version);
return charCountIndicatorSet[versionGroup];

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/DataEncode.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/DataEncode.cs
@@ -14,14 +14,14 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
internal static EncodationStruct Encode(string content, ErrorCorrectionLevel ecLevel)
{
RecognitionStruct recognitionResult = InputRecognise.Recognise(content);
- EncoderBase encoderBase = CreateEncoder(recognitionResult.Mode, recognitionResult.EncodingName);
+ EncoderBase encoderBase = CreateEncoder(recognitionResult.EncodingName);
BitList encodeContent = encoderBase.GetDataBits(content);
int encodeContentLength = encodeContent.Count;
VersionControlStruct vcStruct =
- VersionControl.InitialSetup(encodeContentLength, recognitionResult.Mode, ecLevel, recognitionResult.EncodingName);
+ VersionControl.InitialSetup(encodeContentLength, ecLevel, recognitionResult.EncodingName);
BitList dataCodewords = new BitList();
//Eci header
@@ -29,7 +29,7 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
dataCodewords.Add(vcStruct.ECIHeader);
//Header
dataCodewords.Add(encoderBase.GetModeIndicator());
- int numLetter = recognitionResult.Mode == Mode.EightBitByte ? encodeContentLength >> 3 : content.Length;
+ int numLetter = encodeContentLength >> 3;
dataCodewords.Add(encoderBase.GetCharCountIndicator(numLetter, vcStruct.VersionDetail.Version));
//Data
dataCodewords.Add(encodeContent);
@@ -46,7 +46,6 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
var encStruct = new EncodationStruct(vcStruct)
{
- Mode = recognitionResult.Mode,
DataCodewords = dataCodewords
};
return encStruct;
@@ -54,14 +53,14 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
internal static EncodationStruct Encode(IEnumerable<byte> content, ErrorCorrectionLevel eclevel)
{
- EncoderBase encoderBase = CreateEncoder(Mode.EightBitByte, QRCodeConstantVariable.DefaultEncoding);
+ EncoderBase encoderBase = CreateEncoder(QRCodeConstantVariable.DefaultEncoding);
BitList encodeContent = new BitList(content);
int encodeContentLength = encodeContent.Count;
VersionControlStruct vcStruct =
- VersionControl.InitialSetup(encodeContentLength, Mode.EightBitByte, eclevel, QRCodeConstantVariable.DefaultEncoding);
+ VersionControl.InitialSetup(encodeContentLength, eclevel, QRCodeConstantVariable.DefaultEncoding);
BitList dataCodewords = new BitList();
//Eci header
@@ -86,31 +85,14 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
var encStruct = new EncodationStruct(vcStruct)
{
- Mode = Mode.EightBitByte,
DataCodewords = dataCodewords
};
return encStruct;
}
- private static EncoderBase CreateEncoder(Mode mode, string encodingName)
- {
- switch (mode)
+ private static EncoderBase CreateEncoder(string encodingName)
{
- case Mode.Numeric:
- return new NumericEncoder();
-
- case Mode.Alphanumeric:
- return new AlphanumericEncoder();
-
- case Mode.EightBitByte:
return new EightBitByteEncoder(encodingName);
-
- case Mode.Kanji:
- return new KanjiEncoder();
-
- default:
- throw new ArgumentOutOfRangeException(nameof(mode), $"Doesn't contain encoder for {mode}");
- }
}
}
}

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/EightBitByteEncoder.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/EightBitByteEncoder.cs
@@ -32,8 +32,6 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
Encoding = _defaultEncoding;
}
- internal override Mode Mode => Mode.EightBitByte;
-
protected byte[] EncodeContent(string content, string encoding) => System.Text.Encoding.GetEncoding(encoding).GetBytes(content);
/// <summary>
@@ -76,6 +74,6 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
return dataBits;
}
- protected override int GetBitCountInCharCountIndicator(int version) => CharCountIndicatorTable.GetBitCountInCharCountIndicator(Mode.EightBitByte, version);
+ protected override int GetBitCountInCharCountIndicator(int version) => CharCountIndicatorTable.GetBitCountInCharCountIndicator(version);
}
}

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/EncodationStruct.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/EncodationStruct.cs
@@ -5,7 +5,6 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation
internal struct EncodationStruct
{
internal VersionDetail VersionDetail { get; set; }
- internal Mode Mode { get; set; }
internal BitList DataCodewords { get; set; }
internal EncodationStruct(VersionControlStruct vcStruct) : this()

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/InputRecognise.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/InputRecognise.cs
@@ -7,24 +7,8 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition
{
public static RecognitionStruct Recognise(string content)
{
- int contentLength = content.Length;
-
- int tryEncodePos = ModeEncodeCheck.TryEncodeKanji(content, contentLength);
-
- if (tryEncodePos == -2)
- return new RecognitionStruct(Mode.EightBitByte, QRCodeConstantVariable.UTF8Encoding);
- else if (tryEncodePos == -1)
- return new RecognitionStruct(Mode.Kanji, QRCodeConstantVariable.DefaultEncoding);
-
- tryEncodePos = ModeEncodeCheck.TryEncodeAlphaNum(content, 0, contentLength);
-
- if (tryEncodePos == -2)
- return new RecognitionStruct(Mode.Numeric, QRCodeConstantVariable.DefaultEncoding);
- else if (tryEncodePos == -1)
- return new RecognitionStruct(Mode.Alphanumeric, QRCodeConstantVariable.DefaultEncoding);
-
- string encodingName = EightBitByteRecognision(content, tryEncodePos, contentLength);
- return new RecognitionStruct(Mode.EightBitByte, encodingName);
+ string encodingName = EightBitByteRecognision(content, 0, content.Length);
+ return new RecognitionStruct(encodingName);
}
private static string EightBitByteRecognision(string content, int startPos, int contentLength)

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/ModeEncodeCheck.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/ModeEncodeCheck.cs
@@ -4,72 +4,9 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition
{
public static class ModeEncodeCheck
{
- public static bool IsModeEncodeValid(Mode mode, string encoding, string content)
+ public static bool IsModeEncodeValid(string encoding, string content)
{
- switch (mode)
- {
- case Mode.Numeric:
- return NumericCheck(content);
-
- case Mode.Alphanumeric:
- return AlphaNumCheck(content);
-
- case Mode.EightBitByte:
return EightBitByteCheck(encoding, content);
-
- case Mode.Kanji:
- return KanjiCheck(content);
-
- default:
- throw new InvalidOperationException($"System does not contain mode: {mode.ToString()}");
- }
- }
-
- private static bool NumericCheck(string content)
- {
- int tryEncodePos = TryEncodeAlphaNum(content, 0, content.Length);
- return tryEncodePos == -2 ? true : false;
- }
-
- private static bool AlphaNumCheck(string content)
- {
- int tryEncodePos = TryEncodeAlphaNum(content, 0, content.Length);
- return tryEncodePos == -1 ? true : false;
- }
-
- /// <summary>
- /// Check char from startPos for string content.
- /// </summary>
- /// <param name="content">input string content</param>
- /// <param name="startPos">start check position</param>
- /// <returns>-2 Numeric encode, -1 AlphaNum encode, Index of failed check pos</returns>
- internal static int TryEncodeAlphaNum(string content, int startPos, int contentLength)
- {
- if (string.IsNullOrEmpty(content))
- throw new IndexOutOfRangeException("Input content should not be Null or empty");
-
- //True numeric check, False alphaNum check
- bool checkOption = true;
-
- int num = new int();
-
- for (int index = startPos; index < contentLength; index++)
- {
- if (checkOption)
- {
- num = content[index] - '0';
- if (num < 0 || num > 9)
- checkOption = false;
- }
- if (!checkOption)
- {
- if (!AlphanumericTable.Contains(content[index]))
- {
- return index;
- }
- }
- }
- return checkOption ? -2 : -1;
}
/// <summary>
@@ -134,51 +71,5 @@ namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition
return -1;
}
-
- private static bool KanjiCheck(string content)
- {
- int tryEncodePos = TryEncodeKanji(content, content.Length);
- return tryEncodePos == -1 ? true : false;
- }
-
- /// <summary>
- /// Check input string content. Whether it can apply Kanji encode or not.
- /// </summary>
- /// <param name="content">String input content</param>
- /// <returns>-1 if it can apply Kanji encode, -2 should use utf8 encode, 0 check for other encode.</returns>
- internal static int TryEncodeKanji(string content, int contentLength)
- {
- if (string.IsNullOrEmpty(content))
- throw new IndexOutOfRangeException("Input content should not be Null or empty");
- System.Text.Encoding encoding;
- try
- {
- encoding = System.Text.Encoding.GetEncoding("Shift_JIS");
- }
- catch (ArgumentException)
- {
- return 0;
- }
-
- char[] currentChar = new char[1];
- byte[] bytes;
-
- for (int index = 0; index < contentLength; index++)
- {
- currentChar[0] = content[index];
- bytes = encoding.GetBytes(currentChar);
- int length = bytes.Length;
- byte mostSignificantByte = bytes[0];
-
- if (length != 2)
- return index == 0 ? 0 : -2;
- else if ((mostSignificantByte < 0x81 || mostSignificantByte > 0x9F) && (mostSignificantByte < 0xE0 || mostSignificantByte > 0xEB))
- {
- return index == 0 ? 0 : -2;
- }
- }
-
- return -1;
- }
}
}

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/RecognitionStruct.cs
WalletWasabi/Gma/QrCodeNet/Encoding/DataEncodation/InputRecognition/RecognitionStruct.cs
{
public struct RecognitionStruct
{
- public Mode Mode { get; private set; }
-
public string EncodingName { get; private set; }
- public RecognitionStruct(Mode mode, string encodingName)
+ public RecognitionStruct(string encodingName)
: this()
{
- Mode = mode;
EncodingName = encodingName;
}
}

MODIFY
WalletWasabi/Gma/QrCodeNet/Encoding/Versions/VersionControl.cs
WalletWasabi/Gma/QrCodeNet/Encoding/Versions/VersionControl.cs
@@ -14,7 +14,7 @@ namespace Gma.QrCodeNet.Encoding.Versions
/// <param name="dataBitsLength">Number of bits for encoded content</param>
/// <param name="encodingName">Encoding name for EightBitByte</param>
/// <returns>VersionDetail and ECI</returns>
- internal static VersionControlStruct InitialSetup(int dataBitsLength, Mode mode, ErrorCorrectionLevel level, string encodingName)
+ internal static VersionControlStruct InitialSetup(int dataBitsLength, ErrorCorrectionLevel level, string encodingName)
{
int totalDataBits = dataBitsLength;
@@ -22,9 +22,6 @@ namespace Gma.QrCodeNet.Encoding.Versions
BitList eciHeader = new BitList();
- //Check ECI header
- if (mode == Mode.EightBitByte)
- {
if (encodingName != DEFAULT_ENCODING && encodingName != QRCodeConstantVariable.UTF8Encoding)
{
ECISet eciSet = new ECISet(ECISet.AppendOption.NameToValue);
@@ -34,11 +31,11 @@ namespace Gma.QrCodeNet.Encoding.Versions
eciHeader = eciSet.GetECIHeader(encodingName);
containECI = true;
}
- }
+
//Determine which version group it belong to
- int searchGroup = DynamicSearchIndicator(totalDataBits, level, mode);
+ int searchGroup = DynamicSearchIndicator(totalDataBits, level);
- int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet(mode);
+ int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet();
totalDataBits += (NUM_BITS_MODE_INDICATOR + charCountIndicator[searchGroup]);
@@ -89,11 +86,10 @@ namespace Gma.QrCodeNet.Encoding.Versions
/// </summary>
/// <param name="numBits">number of bits for bitlist where it contain DataBits encode from input content and ECI header</param>
/// <param name="level">Error correction level</param>
- /// <param name="mode">Mode</param>
/// <returns>Version group index for VERSION_GROUP</returns>
- private static int DynamicSearchIndicator(int numBits, ErrorCorrectionLevel level, Mode mode)
+ private static int DynamicSearchIndicator(int numBits, ErrorCorrectionLevel level)
{
- int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet(mode);
+ int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet();
int totalBits = 0;
int loopLength = VERSION_GROUP.Length;
for (int i = 0; i < loopLength; i++)

Simplify QRCode library for bitcoin addresses