Skip to content

Commit

Permalink
Refactoring some small things (#112)
Browse files Browse the repository at this point in the history
* Remove unnecessary parantheses

* Refactor inititialization

* Rename to ErrorCorrectionLevel
  • Loading branch information
robinvanpoppel committed Apr 11, 2020
1 parent 0f0138a commit 8ca6866
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 45 deletions.
70 changes: 32 additions & 38 deletions KeeTrayTOTP/Libraries/QRCoder/QRCodeGenerator.cs
Expand Up @@ -63,7 +63,7 @@ public QRCodeGenerator()
this.CreateAlignmentPatternTable();
}

public QRCodeData CreateQrCode(string plainText, ErrorCorrectionCode eccLevel = QRCodeGenerator.ErrorCorrectionCode.Q, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
public QRCodeData CreateQrCode(string plainText, ErrorCorrectionLevel eccLevel = QRCodeGenerator.ErrorCorrectionLevel.Q, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
{
EncodingMode encoding = GetEncodingFromPlaintext(plainText, forceUtf8);
var codedText = this.PlainTextToBinary(plainText, encoding, eciMode, utf8BOM, forceUtf8);
Expand Down Expand Up @@ -197,12 +197,12 @@ public QRCodeData CreateQrCode(string plainText, ErrorCorrectionCode eccLevel =
return qr;
}

private static string GetFormatString(ErrorCorrectionCode level, int maskVersion)
private static string GetFormatString(ErrorCorrectionLevel level, int maskVersion)
{
var generator = "10100110111";
var fStrMask = "101010000010010";

var fStr = (level == ErrorCorrectionCode.L) ? "01" : (level == ErrorCorrectionCode.M) ? "00" : (level == ErrorCorrectionCode.Q) ? "11" : "10";
var fStr = (level == ErrorCorrectionLevel.L) ? "01" : (level == ErrorCorrectionLevel.M) ? "00" : (level == ErrorCorrectionLevel.Q) ? "11" : "10";
fStr += DecToBin(maskVersion, 3);
var fStrEcc = fStr.PadRight(15, '0').TrimStart('0');
while (fStrEcc.Length > 10)
Expand Down Expand Up @@ -324,7 +324,7 @@ public static void PlaceFormat(ref QRCodeData qrCode, string formatStr)
}
}

public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangle> blockedModules, ErrorCorrectionCode eccLevel)
public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangle> blockedModules, ErrorCorrectionLevel eccLevel)
{
var patternName = string.Empty;
var patternScore = 0;
Expand Down Expand Up @@ -713,7 +713,7 @@ public static int Score(ref QRCodeData qrCode)
}
}

var percent = (blackModules / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count)) * 100;
var percent = blackModules / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count) * 100;
var prevMultipleOf5 = Math.Abs((int)Math.Floor(percent / 5) * 5 - 50) / 5;
var nextMultipleOf5 = Math.Abs((int)Math.Floor(percent / 5) * 5 - 45) / 5;
var score4 = Math.Min(prevMultipleOf5, nextMultipleOf5) * 10;
Expand Down Expand Up @@ -742,7 +742,7 @@ private List<string> CalculateECCWords(string bitString, ErrorCorrectionCodeInfo
}

var leadTermSource = messagePolynom;
for (var i = 0; (leadTermSource.PolyItems.Count > 0 && leadTermSource.PolyItems[leadTermSource.PolyItems.Count - 1].Exponent > 0); i++)
for (var i = 0; leadTermSource.PolyItems.Count > 0 && leadTermSource.PolyItems[leadTermSource.PolyItems.Count - 1].Exponent > 0; i++)
{
if (leadTermSource.PolyItems[0].Coefficient == 0)
{
Expand All @@ -767,9 +767,9 @@ private Polynom ConvertToAlphaNotation(Polynom poly)
{
newPoly.PolyItems.Add(
new PolynomItem(
(poly.PolyItems[i].Coefficient != 0
poly.PolyItems[i].Coefficient != 0
? this.GetAlphaExpFromIntVal(poly.PolyItems[i].Coefficient)
: 0), poly.PolyItems[i].Exponent));
: 0, poly.PolyItems[i].Exponent));
}

return newPoly;
Expand All @@ -786,14 +786,14 @@ private Polynom ConvertToDecNotation(Polynom poly)
return newPoly;
}

private int GetVersion(int length, EncodingMode encMode, ErrorCorrectionCode eccLevel)
private int GetVersion(int length, EncodingMode encMode, ErrorCorrectionLevel errorCorrectionLevel)
{
return this.capacityTable
.Where(x => x.Details.Any(y => y.ErrorCorrectionLevel == eccLevel && y.CapacityDict[encMode] >= Convert.ToInt32(length)))
.Where(x => x.Details.Any(y => y.ErrorCorrectionLevel == errorCorrectionLevel && y.CapacityDict[encMode] >= Convert.ToInt32(length)))
.Select(x => new
{
version = x.Version,
capacity = x.Details.Single(y => y.ErrorCorrectionLevel == eccLevel).CapacityDict[encMode]
capacity = x.Details.Single(y => y.ErrorCorrectionLevel == errorCorrectionLevel).CapacityDict[encMode]
}).Min(x => x.version);
}

Expand Down Expand Up @@ -936,7 +936,7 @@ private int GetDataLength(EncodingMode encoding, string plainText, string codedT

private bool IsUtf8(EncodingMode encoding, string plainText)
{
return (encoding == EncodingMode.Byte && !this.IsValidISO(plainText));
return encoding == EncodingMode.Byte && !this.IsValidISO(plainText);
}

private bool IsValidISO(string input)
Expand Down Expand Up @@ -1098,7 +1098,7 @@ private Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polynomMultip
var polItemRes = new PolynomItem
(
ShrinkAlphaExp(polItemBase.Coefficient + polItemMulti.Coefficient),
(polItemBase.Exponent + polItemMulti.Exponent)
polItemBase.Exponent + polItemMulti.Exponent
);
resultPolynom.PolyItems.Add(polItemRes);
}
Expand All @@ -1115,8 +1115,8 @@ private Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polynomMultip
}
resultPolynom.PolyItems.RemoveAll(x => toGlue.Contains(x.Exponent));
resultPolynom.PolyItems.AddRange(gluedPolynoms);
resultPolynom.PolyItems = resultPolynom.PolyItems.OrderByDescending(x => x.Exponent).ToList();
return resultPolynom;

return new Polynom(resultPolynom.PolyItems.OrderByDescending(x => x.Exponent));
}

private int GetIntValFromAlphaExp(int exp)
Expand Down Expand Up @@ -1188,7 +1188,7 @@ private void CreateCapacityECCTable()
{
new ErrorCorrectionCodeInfo(
(i+24) / 24,
ErrorCorrectionCode.L,
ErrorCorrectionLevel.L,
this.capacityECCBaseValues[i],
this.capacityECCBaseValues[i+1],
this.capacityECCBaseValues[i+2],
Expand All @@ -1198,7 +1198,7 @@ private void CreateCapacityECCTable()
new ErrorCorrectionCodeInfo
(
version: (i + 24) / 24,
errorCorrectionLevel: ErrorCorrectionCode.M,
errorCorrectionLevel: ErrorCorrectionLevel.M,
totalDataCodewords: this.capacityECCBaseValues[i+6],
eccPerBlock: this.capacityECCBaseValues[i+7],
blocksInGroup1: this.capacityECCBaseValues[i+8],
Expand All @@ -1209,7 +1209,7 @@ private void CreateCapacityECCTable()
new ErrorCorrectionCodeInfo
(
version: (i + 24) / 24,
errorCorrectionLevel: ErrorCorrectionCode.Q,
errorCorrectionLevel: ErrorCorrectionLevel.Q,
totalDataCodewords: this.capacityECCBaseValues[i+12],
eccPerBlock: this.capacityECCBaseValues[i+13],
blocksInGroup1: this.capacityECCBaseValues[i+14],
Expand All @@ -1220,7 +1220,7 @@ private void CreateCapacityECCTable()
new ErrorCorrectionCodeInfo
(
version: (i + 24) / 24,
errorCorrectionLevel: ErrorCorrectionCode.H,
errorCorrectionLevel: ErrorCorrectionLevel.H,
totalDataCodewords: this.capacityECCBaseValues[i+18],
eccPerBlock: this.capacityECCBaseValues[i+19],
blocksInGroup1: this.capacityECCBaseValues[i+20],
Expand All @@ -1243,7 +1243,7 @@ private void CreateCapacityTable()
new List<VersionInfoDetails>
{
new VersionInfoDetails(
ErrorCorrectionCode.L,
ErrorCorrectionLevel.L,
new Dictionary<EncodingMode,int>(){
{ EncodingMode.Numeric, this.capacityBaseValues[i] },
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+1] },
Expand All @@ -1252,7 +1252,7 @@ private void CreateCapacityTable()
}
),
new VersionInfoDetails(
ErrorCorrectionCode.M,
ErrorCorrectionLevel.M,
new Dictionary<EncodingMode,int>(){
{ EncodingMode.Numeric, this.capacityBaseValues[i+4] },
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+5] },
Expand All @@ -1261,7 +1261,7 @@ private void CreateCapacityTable()
}
),
new VersionInfoDetails(
ErrorCorrectionCode.Q,
ErrorCorrectionLevel.Q,
new Dictionary<EncodingMode,int>(){
{ EncodingMode.Numeric, this.capacityBaseValues[i+8] },
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+9] },
Expand All @@ -1270,7 +1270,7 @@ private void CreateCapacityTable()
}
),
new VersionInfoDetails(
ErrorCorrectionCode.H,
ErrorCorrectionLevel.H,
new Dictionary<EncodingMode,int>(){
{ EncodingMode.Numeric, this.capacityBaseValues[i+12] },
{ EncodingMode.Alphanumeric, this.capacityBaseValues[i+13] },
Expand Down Expand Up @@ -1312,7 +1312,7 @@ private void CreateAntilogTable()
/// <summary>
/// Error correction level. These define the tolerance levels for how much of the code can be lost before the code cannot be recovered.
/// </summary>
public enum ErrorCorrectionCode
public enum ErrorCorrectionLevel
{
/// <summary>
/// 7% may be lost before recovery is not possible
Expand Down Expand Up @@ -1373,7 +1373,7 @@ private struct CodewordBlock

private struct ErrorCorrectionCodeInfo
{
public ErrorCorrectionCodeInfo(int version, ErrorCorrectionCode errorCorrectionLevel, int totalDataCodewords, int eccPerBlock, int blocksInGroup1,
public ErrorCorrectionCodeInfo(int version, ErrorCorrectionLevel errorCorrectionLevel, int totalDataCodewords, int eccPerBlock, int blocksInGroup1,
int codewordsInGroup1, int blocksInGroup2, int codewordsInGroup2)
: this()
{
Expand All @@ -1387,7 +1387,7 @@ private struct ErrorCorrectionCodeInfo
this.CodewordsInGroup2 = codewordsInGroup2;
}
public int Version { get; private set; }
public ErrorCorrectionCode ErrorCorrectionLevel { get; private set; }
public ErrorCorrectionLevel ErrorCorrectionLevel { get; private set; }
public int TotalDataCodewords { get; private set; }
public int ECCPerBlock { get; private set; }
public int BlocksInGroup1 { get; private set; }
Expand All @@ -1410,14 +1410,14 @@ public VersionInfo(int version, List<VersionInfoDetails> versionInfoDetails)

private struct VersionInfoDetails
{
public VersionInfoDetails(ErrorCorrectionCode errorCorrectionLevel, Dictionary<EncodingMode, int> capacityDict)
public VersionInfoDetails(ErrorCorrectionLevel errorCorrectionLevel, Dictionary<EncodingMode, int> capacityDict)
: this()
{
this.ErrorCorrectionLevel = errorCorrectionLevel;
this.CapacityDict = capacityDict;
}

public ErrorCorrectionCode ErrorCorrectionLevel { get; private set; }
public ErrorCorrectionLevel ErrorCorrectionLevel { get; private set; }
public Dictionary<EncodingMode, int> CapacityDict { get; private set; }
}

Expand Down Expand Up @@ -1453,18 +1453,12 @@ public Polynom()
this.PolyItems = new List<PolynomItem>();
}

public List<PolynomItem> PolyItems { get; set; }

public override string ToString()
public Polynom(IEnumerable<PolynomItem> items)
{
var sb = new StringBuilder();
foreach (var polyItem in this.PolyItems)
{
sb.Append("a^" + polyItem.Coefficient + "*x^" + polyItem.Exponent + " + ");
}

return sb.ToString().TrimEnd(new[] { ' ', '+' });
this.PolyItems = new List<PolynomItem>(items);
}

public List<PolynomItem> PolyItems { get; private set; }
}

private class Point
Expand Down
2 changes: 1 addition & 1 deletion KeeTrayTOTP/Libraries/TOTPProvider.cs
Expand Up @@ -154,7 +154,7 @@ private byte[] GetBytes(ulong n)
b[4] = (byte)(n >> 24);
b[5] = (byte)(n >> 16);
b[6] = (byte)(n >> 8);
b[7] = (byte)(n);
b[7] = (byte)n;

return b;
}
Expand Down
2 changes: 1 addition & 1 deletion KeeTrayTOTP/TrayTOTP_CustomColumn.cs
Expand Up @@ -89,7 +89,7 @@ public override string GetCellData(string columnName, PwEntry pe)
}
return Localization.Strings.ErrorBadSettings;
}
return (_plugin.SettingsCheck(pe) || _plugin.SeedCheck(pe) ? Localization.Strings.ErrorStorage : string.Empty);
return _plugin.SettingsCheck(pe) || _plugin.SeedCheck(pe) ? Localization.Strings.ErrorStorage : string.Empty;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions KeeTrayTOTP/TrayTOTP_Plugin.cs
Expand Up @@ -425,7 +425,7 @@ private void OnNotifyMenuOpening(object sender, CancelEventArgs e)
var context = new SprContext(entry, PluginHost.MainWindow.ActiveDatabase, SprCompileFlags.All, false, false);
var entryUsername = SprEngine.Compile(entry.Strings.ReadSafe(PwDefs.UserNameField), context);
string trayTitle;
if ((trimTrayText && entryTitle.Length + entryUsername.Length > setstat_trim_text_length) || (string.IsNullOrEmpty(entryUsername)))
if ((trimTrayText && entryTitle.Length + entryUsername.Length > setstat_trim_text_length) || string.IsNullOrEmpty(entryUsername))
{
trayTitle = entryTitle.ExtWithSpaceAfter();
}
Expand Down Expand Up @@ -565,7 +565,7 @@ private void OnNotifyMenuTOTPClick(object sender, EventArgs e)
/// <param name="e"></param>
private void OnTimerTick(object sender, EventArgs e)
{
if ((PluginHost.MainWindow.ActiveDatabase.IsOpen) && (PluginHost.MainWindow.Visible))
if (PluginHost.MainWindow.ActiveDatabase.IsOpen && PluginHost.MainWindow.Visible)
{
if (KeePass.Program.Config.MainWindow.EntryListColumns.Count != _liColumnsCount)
{
Expand Down Expand Up @@ -597,7 +597,7 @@ private void OnTimerTick(object sender, EventArgs e)
}
}

if ((_liColumnTotpVisible) && (_liColumnTotpContains)) //Tests if displayed entries have totps that require refreshing.
if (_liColumnTotpVisible && _liColumnTotpContains) //Tests if displayed entries have totps that require refreshing.
{
var currentSeconds = DateTime.Now.Second;
if (_liRefreshTimerPreviousCounter != currentSeconds)
Expand Down
4 changes: 2 additions & 2 deletions KeeTrayTOTP/TrayTOTP_TimeCorrectionCollection.cs
Expand Up @@ -164,9 +164,9 @@ internal ListViewItem[] ToLvi()
{
//Create new Listviewitem to appear in Time Correction Settings Listview.
var lvi = new ListViewItem(tc.Url) { ImageIndex = tc.LastUpdateSucceeded ? 0 : 2 };
lvi.SubItems.Add((tc.LastUpdateSucceeded ? tc.TimeCorrection.ToString() : Localization.Strings.ConnectionFailed));
lvi.SubItems.Add(tc.LastUpdateSucceeded ? tc.TimeCorrection.ToString() : Localization.Strings.ConnectionFailed);
lvi.Tag = tc;
lvi.ToolTipText = (tc.LastUpdateSucceeded ? string.Empty : tc.LastUpdateDateTime.ToString());
lvi.ToolTipText = tc.LastUpdateSucceeded ? string.Empty : tc.LastUpdateDateTime.ToString();
lvIs.Add(lvi);
}
return lvIs.ToArray();
Expand Down

0 comments on commit 8ca6866

Please sign in to comment.