Character stream class.
Exemple #1
0
        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();
        }
Exemple #2
0
		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();
		}
Exemple #3
0
        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;
        }
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, Encoding.UTF8);
			string strContent = sr.ReadToEnd();
			sr.Close();

			if(strContent.Length == 0) return;

			CharStream cs = new CharStream(strContent);

			JsonObject jRoot = new JsonObject(cs);
			AddObject(pwStorage.RootGroup, jRoot, pwStorage, false);
			Debug.Assert(cs.PeekChar(true) == char.MinValue);
		}
Exemple #5
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, Encoding.UTF8);
			string strFileContents = sr.ReadToEnd();
			sr.Close();

			CharStream csSource = new CharStream(strFileContents);

			while(true)
			{
				if(ReadEntry(pwStorage, csSource) == false)
					break;
			}
		}
Exemple #6
0
		private static bool ReadEntry(PwDatabase pwStorage, CharStream csSource)
		{
			PwEntry pe = new PwEntry(true, true);

			string strTitle = ReadCsvField(csSource);
			if(strTitle == null) return false; // No entry available

			string strUser = ReadCsvField(csSource);
			if(strUser == null) throw new InvalidDataException();

			string strPassword = ReadCsvField(csSource);
			if(strPassword == null) throw new InvalidDataException();

			string strUrl = ReadCsvField(csSource);
			if(strUrl == null) throw new InvalidDataException();

			string strNotes = ReadCsvField(csSource);
			if(strNotes == null) throw new InvalidDataException();

			if((strTitle == "Account") && (strUser == "Login Name") &&
				(strPassword == "Password") && (strUrl == "Web Site") &&
				(strNotes == "Comments"))
			{
				return true; // Ignore header entry
			}

			pe.Strings.Set(PwDefs.TitleField, new ProtectedString(
				pwStorage.MemoryProtection.ProtectTitle, strTitle));
			pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(
				pwStorage.MemoryProtection.ProtectUserName, strUser));
			pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
				pwStorage.MemoryProtection.ProtectPassword, strPassword));
			pe.Strings.Set(PwDefs.UrlField, new ProtectedString(
				pwStorage.MemoryProtection.ProtectUrl, strUrl));
			pe.Strings.Set(PwDefs.NotesField, new ProtectedString(
				pwStorage.MemoryProtection.ProtectNotes, strNotes));

			pwStorage.RootGroup.AddEntry(pe, true);
			return true;
		}
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, StrUtil.Utf8);
			string strContent = sr.ReadToEnd();
			sr.Close();

			if(string.IsNullOrEmpty(strContent)) return;

			CharStream cs = new CharStream(strContent);

			Dictionary<string, List<string>> dTags =
				new Dictionary<string, List<string>>();
			List<PwEntry> lCreatedEntries = new List<PwEntry>();

			JsonObject jRoot = new JsonObject(cs);
			AddObject(pwStorage.RootGroup, jRoot, pwStorage, false, dTags,
				lCreatedEntries);
			Debug.Assert(cs.PeekChar(true) == char.MinValue);

			// Assign tags
			foreach(PwEntry pe in lCreatedEntries)
			{
				string strUri = pe.Strings.ReadSafe(PwDefs.UrlField);
				if(strUri.Length == 0) continue;

				foreach(KeyValuePair<string, List<string>> kvp in dTags)
				{
					foreach(string strTagUri in kvp.Value)
					{
						if(strUri.Equals(strTagUri, StrUtil.CaseIgnoreCmp))
							pe.AddTag(kvp.Key);
					}
				}
			}
		}
        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());
        }
Exemple #9
0
        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); }
        }
Exemple #10
0
 public CsvStreamReader(string strData, bool bAllowUnquoted)
 {
     m_sChars = new CharStream(strData);
     m_bAllowUnquoted = bAllowUnquoted;
 }
Exemple #11
0
 public CsvStreamReader(string strData)
 {
     m_sChars = new CharStream(strData);
     m_bAllowUnquoted = false;
 }
Exemple #12
0
        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;
        }
Exemple #13
0
		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); }
		}
Exemple #14
0
        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;
        }
		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;
		}
Exemple #16
0
        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);
            }
        }
Exemple #17
0
        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);
            }
        }
Exemple #18
0
        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;
        }
Exemple #19
0
		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);
		}
        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();
        }
Exemple #21
0
		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 override void Import(PwDatabase pwStorage, Stream sInput,
            IStatusLogger slLogger)
        {
            StreamReader sr = new StreamReader(sInput, Encoding.Default);
            string strDocument = sr.ReadToEnd();
            sr.Close();

            PwGroup pg = pwStorage.RootGroup;

            CharStream cs = new CharStream(strDocument);
            string[] vFields = new string[7];
            char[] vDateFieldSplitter = new char[]{ '/' };
            char[] vDateZeroTrim = new char[]{ '0' };

            bool bFirst = true;
            while(true)
            {
                bool bSubZero = false;
                for(int iField = 0; iField < vFields.Length; ++iField)
                {
                    vFields[iField] = ReadCsvField(cs);

                    if((iField > 0) && (vFields[iField] == null))
                        bSubZero = true;
                }
                if(vFields[0] == null) break; // Import successful
                else if(bSubZero) throw new FormatException();

                if(bFirst)
                {
                    bFirst = false; // Check first line once only

                    if((vFields[0] != "ServiceName") || (vFields[1] != "UserName") ||
                        (vFields[2] != "Password") || (vFields[3] != "Memo") ||
                        (vFields[4] != "Expire") || (vFields[5] != "StartDate") ||
                        (vFields[6] != "DaysToLive"))
                    {
                        Debug.Assert(false);
                        throw new FormatException();
                    }
                    else continue;
                }

                PwEntry pe = new PwEntry(true, true);
                pg.AddEntry(pe, true);

                pe.Strings.Set(PwDefs.TitleField, new ProtectedString(
                    pwStorage.MemoryProtection.ProtectTitle, vFields[0]));
                pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(
                    pwStorage.MemoryProtection.ProtectUserName, vFields[1]));
                pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
                    pwStorage.MemoryProtection.ProtectPassword, vFields[2]));
                pe.Strings.Set(PwDefs.NotesField, new ProtectedString(
                    pwStorage.MemoryProtection.ProtectNotes, vFields[3]));

                pe.Expires = (vFields[4] == "true");

                try
                {
                    string[] vDateParts = vFields[5].Split(vDateFieldSplitter);
                    DateTime dt = new DateTime(
                        int.Parse(vDateParts[2].TrimStart(vDateZeroTrim)),
                        int.Parse(vDateParts[0].TrimStart(vDateZeroTrim)),
                        int.Parse(vDateParts[1].TrimStart(vDateZeroTrim)));
                    pe.LastModificationTime = dt;
                    pe.LastAccessTime = dt;
                    pe.ExpiryTime = dt.AddDays(double.Parse(vFields[6]));
                }
                catch(Exception) { Debug.Assert(false); }

                pe.Strings.Set("Days To Live", new ProtectedString(false,
                    vFields[6]));
            }
        }
        private void Init(string strData, CsvOptions opt)
        {
            if(strData == null) throw new ArgumentNullException("strData");

            m_opt = (opt ?? new CsvOptions());

            string strInput = strData;

            // Normalize to Unix "\n" right now; the new lines are
            // converted back in the <c>AddField</c> method
            strInput = StrUtil.NormalizeNewLines(strInput, false);

            strInput = strInput.Trim(new char[] { (char)0 });

            m_sChars = new CharStream(strInput);
        }
        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();
        }