public JsonObject(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); char chInit = csDataSource.ReadChar(true); if(chInit != '{') throw new JsonFormatException(); while(true) { string strName = (new JsonString(csDataSource)).Value; char chSeparator = csDataSource.ReadChar(true); if(chSeparator != ':') throw new JsonFormatException(); JsonValue jValue = new JsonValue(csDataSource); m_dict[strName] = jValue; char chNext = csDataSource.PeekChar(true); if(chNext == '}') break; else if(chNext == ',') csDataSource.ReadChar(true); else throw new JsonFormatException(); } char chTerminator = csDataSource.ReadChar(true); if(chTerminator != '}') throw new JsonFormatException(); }
public JsonArray(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); char chInit = csDataSource.ReadChar(true); if(chInit != '[') throw new JsonFormatException(); List<JsonValue> lValues = new List<JsonValue>(); while(true) { char chNext = csDataSource.PeekChar(true); if(chNext == ']') break; lValues.Add(new JsonValue(csDataSource)); chNext = csDataSource.PeekChar(true); if(chNext == ',') csDataSource.ReadChar(true); } char chTerminator = csDataSource.ReadChar(true); if(chTerminator != ']') throw new JsonFormatException(); m_values = lValues.ToArray(); }
public JsonNumber(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); StringBuilder sb = new StringBuilder(); while(true) { char ch = csDataSource.PeekChar(true); if(((ch >= '0') && (ch <= '9')) || (ch == 'e') || (ch == 'E') || (ch == '+') || (ch == '-') || (ch == '.')) { csDataSource.ReadChar(true); sb.Append(ch); } else break; } if(!double.TryParse(sb.ToString(), out m_value)) { Debug.Assert(false); } }
public JsonString(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); char chInit = csDataSource.ReadChar(true); if(chInit != '\"') throw new JsonFormatException(); StringBuilder sb = new StringBuilder(); while(true) { char ch = csDataSource.ReadChar(); if(ch == char.MinValue) throw new JsonFormatException(); if(ch == '\"') break; // End of string else if(ch == '\\') { char chNext = csDataSource.ReadChar(); if(chNext == char.MinValue) throw new JsonFormatException(); if((chNext == 'b') || (chNext == 'f')) { } // Ignore else if(chNext == 'r') sb.Append('\r'); else if(chNext == 'n') sb.Append('\n'); else if(chNext == 't') sb.Append('\t'); else if(chNext == 'u') { char ch1 = csDataSource.ReadChar(); char ch2 = csDataSource.ReadChar(); char ch3 = csDataSource.ReadChar(); char ch4 = csDataSource.ReadChar(); if(ch4 == char.MinValue) throw new JsonFormatException(); byte[] pbUni = MemUtil.HexStringToByteArray(new string( new char[] { ch1, ch2, ch3, ch4 })); if((pbUni != null) && (pbUni.Length == 2)) sb.Append((char)(((ushort)pbUni[0] << 8) | (ushort)pbUni[1])); else throw new JsonFormatException(); } else sb.Append(chNext); } else sb.Append(ch); } m_str = sb.ToString(); }
public JsonValue(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); char chNext = csDataSource.PeekChar(true); if(chNext == '\"') m_value = (new JsonString(csDataSource)).Value; else if(chNext == '{') m_value = new JsonObject(csDataSource); else if(chNext == '[') m_value = new JsonArray(csDataSource); else if(chNext == 't') { for(int i = 0; i < 4; ++i) csDataSource.ReadChar(true); m_value = true; } else if(chNext == 'f') { for(int i = 0; i < 5; ++i) csDataSource.ReadChar(true); m_value = false; } else if(chNext == 'n') { for(int i = 0; i < 4; ++i) csDataSource.ReadChar(true); m_value = null; } else m_value = new JsonNumber(csDataSource); }
public static void MakeList() { string strData = File.ReadAllText("MostPopularPasswords.txt", StrUtil.Utf8); strData += " "; CharStream cs = new CharStream(strData); List<string> vPasswords = new List<string>(); StringBuilder sbPassword = new StringBuilder(); while (true) { char ch = cs.ReadChar(); if (ch == char.MinValue) break; if (char.IsWhiteSpace(ch)) { string strPassword = sbPassword.ToString(); strPassword = strPassword.ToLower(); if (strPassword.Length > 3) { if (vPasswords.IndexOf(strPassword) < 0) vPasswords.Add(strPassword); } sbPassword = new StringBuilder(); } else { Debug.Assert(!char.IsControl(ch) && !char.IsHighSurrogate(ch) && !char.IsLowSurrogate(ch) && !char.IsSurrogate(ch)); Debug.Assert(IsPopularChar(ch)); sbPassword.Append(ch); } } ulong[] vTable = new ulong[PpcTableSize / 64]; Array.Clear(vTable, 0, vTable.Length); long lBitsInTable = 0; foreach (string strPassword in vPasswords) { byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strPassword); int[] vIndices = GetTableIndices(pbUtf8, PpcTableSize); foreach (int i in vIndices) { if (SetTableBit(vTable, i)) ++lBitsInTable; } } StringBuilder sb = new StringBuilder(); sb.Append("\t\t\t"); for (int i = 0; i < vTable.Length; ++i) { if (i > 0) { if ((i % 3) == 0) { sb.AppendLine(","); sb.Append("\t\t\t"); } else sb.Append(", "); } sb.Append("0x"); sb.Append(vTable[i].ToString("X16")); sb.Append("UL"); } sb.AppendLine(); sb.AppendLine(); sb.AppendLine("Bits set: " + lBitsInTable.ToString() + " of " + PpcTableSize.ToString()); int cHashFn = GetTableIndices(StrUtil.Utf8.GetBytes("Dummy"), PpcTableSize).Length; sb.AppendLine("Hash functions: " + cHashFn.ToString()); double dblPhi = Math.Pow(1.0 - ((double) cHashFn / PpcTableSize), (double) vPasswords.Count); sb.AppendLine("Phi (bits unset ratio) estimation: " + dblPhi.ToString(CultureInfo.InvariantCulture)); dblPhi = ((double) (PpcTableSize - lBitsInTable) / (double) PpcTableSize); sb.AppendLine("Exact Phi: " + dblPhi.ToString(CultureInfo.InvariantCulture)); sb.AppendLine("False positives ratio: " + Math.Pow(1.0 - dblPhi, (double) cHashFn).ToString(CultureInfo.InvariantCulture)); File.WriteAllText("Table.txt", sb.ToString()); }
public JsonNumber(CharStream csDataSource) { if(csDataSource == null) throw new ArgumentNullException("csDataSource"); StringBuilder sb = new StringBuilder(); while(true) { char ch = csDataSource.PeekChar(true); if(((ch >= '0') && (ch <= '9')) || (ch == 'e') || (ch == 'E') || (ch == '+') || (ch == '-') || (ch == '.')) { csDataSource.ReadChar(true); sb.Append(ch); } else break; } const NumberStyles ns = (NumberStyles.Integer | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent); if(!double.TryParse(sb.ToString(), ns, NumberFormatInfo.InvariantInfo, out m_value)) { Debug.Assert(false); } }
public static PwgError Generate(ProtectedString psOutBuffer, PwProfile pwProfile, CryptoRandomStream crsRandomSource) { LinkedList<char> vGenerated = new LinkedList<char>(); PwCharSet pcsCurrent = new PwCharSet(); PwCharSet pcsCustom = new PwCharSet(); PwCharSet pcsUsed = new PwCharSet(); bool bInCharSetDef = false; string strPattern = ExpandPattern(pwProfile.Pattern); if(strPattern.Length == 0) return PwgError.Success; CharStream csStream = new CharStream(strPattern); char ch = csStream.ReadChar(); while(ch != char.MinValue) { pcsCurrent.Clear(); bool bGenerateChar = false; if(ch == '\\') { ch = csStream.ReadChar(); if(ch == char.MinValue) // Backslash at the end { vGenerated.AddLast('\\'); break; } if(bInCharSetDef) pcsCustom.Add(ch); else { vGenerated.AddLast(ch); pcsUsed.Add(ch); } } else if(ch == '[') { pcsCustom.Clear(); bInCharSetDef = true; } else if(ch == ']') { pcsCurrent.Add(pcsCustom.ToString()); bInCharSetDef = false; bGenerateChar = true; } else if(bInCharSetDef) { if(pcsCustom.AddCharSet(ch) == false) pcsCustom.Add(ch); } else if(pcsCurrent.AddCharSet(ch) == false) { vGenerated.AddLast(ch); pcsUsed.Add(ch); } else bGenerateChar = true; if(bGenerateChar) { PwGenerator.PrepareCharSet(pcsCurrent, pwProfile); if(pwProfile.NoRepeatingCharacters) pcsCurrent.Remove(pcsUsed.ToString()); char chGen = PwGenerator.GenerateCharacter(pwProfile, pcsCurrent, crsRandomSource); if(chGen == char.MinValue) return PwgError.TooFewCharacters; vGenerated.AddLast(chGen); pcsUsed.Add(chGen); } ch = csStream.ReadChar(); } if(vGenerated.Count == 0) return PwgError.Success; char[] vArray = new char[vGenerated.Count]; vGenerated.CopyTo(vArray, 0); if(pwProfile.PatternPermutePassword) PwGenerator.ShufflePassword(vArray, crsRandomSource); byte[] pbUtf8 = Encoding.UTF8.GetBytes(vArray); psOutBuffer.SetString(Encoding.UTF8.GetString(pbUtf8, 0, pbUtf8.Length)); Array.Clear(pbUtf8, 0, pbUtf8.Length); Array.Clear(vArray, 0, vArray.Length); vGenerated.Clear(); return PwgError.Success; }
private static string ReadCsvField(CharStream cs) { StringBuilder sbValue = new StringBuilder(); char ch; while(true) { ch = cs.ReadChar(); if(ch == char.MinValue) return null; else if(ch == '\"') break; } while(true) { ch = cs.ReadChar(); if(ch == char.MinValue) return null; else if(ch == '\r') continue; else if(ch == '\"') { char chSucc = cs.ReadChar(); if(chSucc == '\"') sbValue.Append('\"'); else break; } else if(ch == '\n') sbValue.Append(MessageService.NewLine); else sbValue.Append(ch); } return sbValue.ToString(); }
private static void ReadParenthesized(CharStream csIn, StringBuilder sbBuffer) { sbBuffer.Append('('); while(true) { char ch = csIn.ReadChar(); if((ch == char.MinValue) || (ch == '}')) throw new FormatException(); else if(ch == ')') { sbBuffer.Append(ch); break; } else if(ch == '(') ReadParenthesized(csIn, sbBuffer); else if(ch == '{') ReadBraced(csIn, sbBuffer); else sbBuffer.Append(ch); } }
private static List<string> SplitKeySequence(string strKeys) { List<string> vParts = new List<string>(); if(string.IsNullOrEmpty(strKeys)) return vParts; CharStream cs = new CharStream(strKeys); StringBuilder sbRawText = new StringBuilder(); while(true) { char ch = cs.ReadChar(); if(ch == char.MinValue) break; switch(ch) { case ')': case '}': throw new FormatException(); case '(': case '{': case '+': case '^': case '%': case ' ': case '\t': string strBuf = sbRawText.ToString(); if(strBuf.IndexOfAny(new char[]{ '+', '^', '%', ' ', '\t' }) < 0) { if(strBuf.Length > 0) vParts.Add(strBuf); sbRawText.Remove(0, sbRawText.Length); } if(ch == '(') { ReadParenthesized(cs, sbRawText); if(sbRawText.Length > 0) vParts.Add(sbRawText.ToString()); sbRawText.Remove(0, sbRawText.Length); } else if(ch == '{') { ReadBraced(cs, sbRawText); if(sbRawText.Length > 0) vParts.Add(sbRawText.ToString()); sbRawText.Remove(0, sbRawText.Length); } else if(ch == ' ') { vParts.Add(" "); sbRawText.Remove(0, sbRawText.Length); } else if(ch == '\t') { vParts.Add("\t"); sbRawText.Remove(0, sbRawText.Length); } else sbRawText.Append(ch); break; default: sbRawText.Append(ch); break; } } if(sbRawText.Length > 0) vParts.Add(sbRawText.ToString()); return vParts; }
private static void ReadBraced(CharStream csIn, StringBuilder sbBuffer) { sbBuffer.Append('{'); char chFirst = csIn.ReadChar(); if(chFirst == char.MinValue) throw new FormatException(); char chSecond = csIn.ReadChar(); if(chSecond == char.MinValue) throw new FormatException(); if((chFirst == '{') && (chSecond == '}')) { sbBuffer.Append(@"{}"); return; } else if((chFirst == '}') && (chSecond == '}')) { sbBuffer.Append(@"}}"); return; } else if(chSecond == '}') { sbBuffer.Append(chFirst); sbBuffer.Append(chSecond); return; } sbBuffer.Append(chFirst); sbBuffer.Append(chSecond); while(true) { char ch = csIn.ReadChar(); if((ch == char.MinValue) || (ch == ')')) throw new FormatException(); else if(ch == '(') ReadParenthesized(csIn, sbBuffer); else if(ch == '{') ReadBraced(csIn, sbBuffer); else if(ch == '}') { sbBuffer.Append(ch); break; } else sbBuffer.Append(ch); } }
private static List<SiEvent> ParseSpecial(CharStream cs) { // Skip leading white space while(true) { char ch = cs.PeekChar(); if(ch == char.MinValue) { Debug.Assert(false); return null; } if(!char.IsWhiteSpace(ch)) break; cs.ReadChar(); } // First char is *always* part of the name (support for "{{}", etc.) char chFirst = cs.ReadChar(); if(chFirst == char.MinValue) { Debug.Assert(false); return null; } int iPart = 0; StringBuilder sbName = new StringBuilder(), sbParams = new StringBuilder(); sbName.Append(chFirst); while(true) { char ch = cs.ReadChar(); if(ch == char.MinValue) { Debug.Assert(false); return null; } if(ch == '}') break; if(iPart == 0) { if(char.IsWhiteSpace(ch)) ++iPart; else sbName.Append(ch); } else sbParams.Append(ch); } string strName = sbName.ToString(); string strParams = sbParams.ToString().Trim(); uint? ouParam = null; if(strParams.Length > 0) { uint uParamTry; if(uint.TryParse(strParams, out uParamTry)) ouParam = uParamTry; } List<SiEvent> l = new List<SiEvent>(); if(strName.Equals("DELAY", StrUtil.CaseIgnoreCmp)) { if(!ouParam.HasValue) { Debug.Assert(false); return null; } SiEvent si = new SiEvent(); si.Type = SiEventType.Delay; si.Delay = ouParam.Value; l.Add(si); return l; } if(strName.StartsWith("DELAY=", StrUtil.CaseIgnoreCmp)) { SiEvent si = new SiEvent(); si.Type = SiEventType.SetDefaultDelay; string strDelay = strName.Substring(6).Trim(); uint uDelay; if(uint.TryParse(strDelay, out uDelay)) si.Delay = uDelay; else { Debug.Assert(false); return null; } l.Add(si); return l; } if(strName.Equals("VKEY", StrUtil.CaseIgnoreCmp) || strName.Equals("VKEY-NX", StrUtil.CaseIgnoreCmp) || strName.Equals("VKEY-EX", StrUtil.CaseIgnoreCmp)) { if(!ouParam.HasValue) { Debug.Assert(false); return null; } SiEvent si = new SiEvent(); si.Type = SiEventType.Key; si.VKey = (int)ouParam.Value; if(strName.EndsWith("-NX", StrUtil.CaseIgnoreCmp)) si.ExtendedKey = false; else if(strName.EndsWith("-EX", StrUtil.CaseIgnoreCmp)) si.ExtendedKey = true; l.Add(si); return l; } if(strName.Equals("APPACTIVATE", StrUtil.CaseIgnoreCmp)) { SiEvent si = new SiEvent(); si.Type = SiEventType.AppActivate; si.Text = strParams; l.Add(si); return l; } SiCode siCode = SiCodes.Get(strName); SiEvent siTmpl = new SiEvent(); if(siCode != null) { siTmpl.Type = SiEventType.Key; siTmpl.VKey = siCode.VKey; siTmpl.ExtendedKey = siCode.ExtKey; } else if(strName.Length == 1) { siTmpl.Type = SiEventType.Char; siTmpl.Char = strName[0]; } else { throw new FormatException(KPRes.AutoTypeUnknownPlaceholder + MessageService.NewLine + @"{" + strName + @"}"); } uint uRepeat = 1; if(ouParam.HasValue) uRepeat = ouParam.Value; for(uint u = 0; u < uRepeat; ++u) { SiEvent si = new SiEvent(); si.Type = siTmpl.Type; si.VKey = siTmpl.VKey; si.ExtendedKey = siTmpl.ExtendedKey; si.Char = siTmpl.Char; l.Add(si); } return l; }
private static List<SiEvent> Parse(string strSequence) { CharStream cs = new CharStream(strSequence); List<SiEvent> l = new List<SiEvent>(); string strError = KPRes.AutoTypeSequenceInvalid; Keys kCurKbMods = Keys.None; List<Keys> lMods = new List<Keys>(); lMods.Add(Keys.None); while(true) { char ch = cs.ReadChar(); if(ch == char.MinValue) break; if((ch == '+') || (ch == '^') || (ch == '%')) { if(lMods.Count == 0) { Debug.Assert(false); break; } else if(ch == '+') lMods[lMods.Count - 1] |= Keys.Shift; else if(ch == '^') lMods[lMods.Count - 1] |= Keys.Control; else if(ch == '%') lMods[lMods.Count - 1] |= Keys.Alt; else { Debug.Assert(false); } continue; } else if(ch == '(') { lMods.Add(Keys.None); continue; } else if(ch == ')') { if(lMods.Count >= 2) { lMods.RemoveAt(lMods.Count - 1); lMods[lMods.Count - 1] = Keys.None; } else throw new FormatException(strError); continue; } Keys kEffMods = Keys.None; foreach(Keys k in lMods) kEffMods |= k; EnsureKeyModifiers(kEffMods, ref kCurKbMods, l); if(ch == '{') { List<SiEvent> lSub = ParseSpecial(cs); if(lSub == null) throw new FormatException(strError); l.AddRange(lSub); } else if(ch == '}') throw new FormatException(strError); else if(ch == '~') { SiEvent si = new SiEvent(); si.Type = SiEventType.Key; si.VKey = (int)Keys.Enter; l.Add(si); } else { SiEvent si = new SiEvent(); si.Type = SiEventType.Char; si.Char = ch; l.Add(si); } lMods[lMods.Count - 1] = Keys.None; } EnsureKeyModifiers(Keys.None, ref kCurKbMods, l); return l; }
internal static Dictionary<string, string> ParseRepl(string str) { Dictionary<string, string> d = new Dictionary<string, string>(); if(str == null) { Debug.Assert(false); return d; } CharStream cs = new CharStream(str + ",,"); StringBuilder sb = new StringBuilder(); string strKey = string.Empty; bool bValue = false; while(true) { char ch = cs.ReadChar(); if(ch == char.MinValue) break; if(ch == ',') { if(!bValue) { strKey = sb.ToString(); sb.Remove(0, sb.Length); } if(strKey.Length > 0) d[strKey] = sb.ToString(); sb.Remove(0, sb.Length); bValue = false; } else if(ch == '>') { strKey = sb.ToString(); sb.Remove(0, sb.Length); bValue = true; } else if(ch == '\\') { char chSub = cs.ReadChar(); if(chSub == 'n') sb.Append('\n'); else if(chSub == 'r') sb.Append('\r'); else if(chSub == 't') sb.Append('\t'); else sb.Append(chSub); } else sb.Append(ch); } return d; }
private static string ReadCsvField(CharStream csSource) { StringBuilder sb = new StringBuilder(); bool bInField = false; while(true) { char ch = csSource.ReadChar(); if(ch == char.MinValue) return null; if((ch == '\"') && !bInField) bInField = true; else if((ch == '\"') && bInField) break; else if(ch == '\\') { char chSub = csSource.ReadChar(); if(chSub == char.MinValue) throw new InvalidDataException(); sb.Append(chSub); } else if(bInField) sb.Append(ch); } return sb.ToString(); }