Serialization to KeePass KDB files.
Exemplo n.º 1
0
		public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
			IStatusLogger slLogger)
		{
			Kdb4File kdb4 = new Kdb4File(pwExportInfo.ContextDatabase);
			kdb4.Save(sOutput, pwExportInfo.DataGroup, Kdb4Format.Default, slLogger);
			return true;
		}
Exemplo n.º 2
0
        /// <summary>
        /// Write entries to a stream.
        /// </summary>
        /// <param name="msOutput">Output stream to which the entries will be written.</param>
        /// <param name="pwDatabase">Source database.</param>
        /// <param name="vEntries">Entries to serialize.</param>
        /// <returns>Returns <c>true</c>, if the entries were written successfully to the stream.</returns>
        public static bool WriteEntries(Stream msOutput, PwDatabase pwDatabase, PwEntry[] vEntries)
        {
            Kdb4File f = new Kdb4File(pwDatabase);
            f.m_format = Kdb4Format.PlainXml;

            XmlTextWriter xtw = null;
            try { xtw = new XmlTextWriter(msOutput, Encoding.UTF8); }
            catch(Exception) { Debug.Assert(false); return false; }
            if(xtw == null) { Debug.Assert(false); return false; }

            f.m_xmlWriter = xtw;

            xtw.Formatting = Formatting.Indented;
            xtw.IndentChar = '\t';
            xtw.Indentation = 1;

            xtw.WriteStartDocument(true);
            xtw.WriteStartElement(ElemRoot);

            foreach(PwEntry pe in vEntries)
                f.WriteEntry(pe, false);

            xtw.WriteEndElement();
            xtw.WriteEndDocument();

            xtw.Flush();
            xtw.Close();
            return true;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="pwDatabase">Source database.</param>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        public static List<PwEntry> ReadEntries(PwDatabase pwDatabase, Stream msData)
        {
            Kdb4File f = new Kdb4File(pwDatabase);
            f.m_format = Kdb4Format.PlainXml;

            XmlDocument doc = new XmlDocument();
            doc.Load(msData);

            XmlElement el = doc.DocumentElement;
            if(el.Name != ElemRoot) throw new FormatException();

            List<PwEntry> vEntries = new List<PwEntry>();

            foreach(XmlNode xmlChild in el.ChildNodes)
            {
                if(xmlChild.Name == ElemEntry)
                {
                    PwEntry pe = f.ReadEntry(xmlChild);
                    pe.Uuid = new PwUuid(true);

                    foreach(PwEntry peHistory in pe.History)
                        peHistory.Uuid = pe.Uuid;

                    vEntries.Add(pe);
                }
                else { Debug.Assert(false); }
            }

            return vEntries;
        }
Exemplo n.º 4
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			Kdb4File kdb4 = new Kdb4File(pwStorage);
			// CappedByteStream s = new CappedByteStream(sInput, 64);

			kdb4.RepairMode = true;

			try { kdb4.Load(sInput, Kdb4Format.Default, slLogger); }
			catch(Exception) { }
		}
Exemplo n.º 5
0
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        public static List <PwEntry> ReadEntries(Stream msData)
        {
            /* Kdb4File f = new Kdb4File(pwDatabase);
             * f.m_format = Kdb4Format.PlainXml;
             *
             * XmlDocument doc = new XmlDocument();
             * doc.Load(msData);
             *
             * XmlElement el = doc.DocumentElement;
             * if(el.Name != ElemRoot) throw new FormatException();
             *
             * List<PwEntry> vEntries = new List<PwEntry>();
             *
             * foreach(XmlNode xmlChild in el.ChildNodes)
             * {
             *      if(xmlChild.Name == ElemEntry)
             *      {
             *              PwEntry pe = f.ReadEntry(xmlChild);
             *              pe.Uuid = new PwUuid(true);
             *
             *              foreach(PwEntry peHistory in pe.History)
             *                      peHistory.Uuid = pe.Uuid;
             *
             *              vEntries.Add(pe);
             *      }
             *      else { Debug.Assert(false); }
             * }
             *
             * return vEntries; */

            PwDatabase pd = new PwDatabase();
            Kdb4File   f  = new Kdb4File(pd);

            f.Load(msData, Kdb4Format.PlainXml, null);

            List <PwEntry> vEntries = new List <PwEntry>();

            foreach (PwEntry pe in pd.RootGroup.Entries)
            {
                pe.SetUuid(new PwUuid(true), true);
                vEntries.Add(pe);
            }

            return(vEntries);
        }
Exemplo n.º 6
0
		public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
			IStatusLogger slLogger)
		{
			string strFilter = UIUtil.CreateFileTypeFilter("xsl", KPRes.XslFileType, true);
			OpenFileDialog dlgXsl = UIUtil.CreateOpenFileDialog(KPRes.XslSelectFile,
				strFilter, 1, "xsl", false, false);

			if(dlgXsl.ShowDialog() != DialogResult.OK) return false;

			string strXslFile = dlgXsl.FileName;
			XslCompiledTransform xsl = new XslCompiledTransform();

			try { xsl.Load(strXslFile); }
			catch(Exception exXsl)
			{
				throw new NotSupportedException(strXslFile + MessageService.NewParagraph +
					KPRes.NoXslFile + MessageService.NewParagraph + exXsl.Message);
			}

			MemoryStream msDataXml = new MemoryStream();

			PwDatabase pd = (pwExportInfo.ContextDatabase ?? new PwDatabase());
			Kdb4File kdb = new Kdb4File(pd);
			kdb.Save(msDataXml, pwExportInfo.DataGroup, Kdb4Format.PlainXml, slLogger);

			byte[] pbData = msDataXml.ToArray();
			MemoryStream msDataRead = new MemoryStream(pbData, false);
			XmlReader xmlDataReader = XmlReader.Create(msDataRead);

			XmlWriterSettings xws = new XmlWriterSettings();
			xws.CheckCharacters = false;
			xws.Encoding = new UTF8Encoding(false);
			xws.NewLineChars = MessageService.NewLine;
			xws.NewLineHandling = NewLineHandling.None;
			xws.OmitXmlDeclaration = true;
			xws.ConformanceLevel = ConformanceLevel.Auto;

			XmlWriter xmlWriter = XmlWriter.Create(sOutput, xws);
			xsl.Transform(xmlDataReader, xmlWriter);
			xmlWriter.Close();

			Array.Clear(pbData, 0, pbData.Length);
			return true;
		}
Exemplo n.º 7
0
        /// <summary>
        /// Write entries to a stream.
        /// </summary>
        /// <param name="msOutput">Output stream to which the entries will be written.</param>
        /// <param name="vEntries">Entries to serialize.</param>
        /// <returns>Returns <c>true</c>, if the entries were written successfully
        /// to the stream.</returns>
        public static bool WriteEntries(Stream msOutput, PwEntry[] vEntries)
        {
            /* Kdb4File f = new Kdb4File(pwDatabase);
             *             f.m_format = Kdb4Format.PlainXml;
             *
             *             XmlTextWriter xtw = null;
             *             try { xtw = new XmlTextWriter(msOutput, StrUtil.Utf8); }
             *             catch(Exception) { Debug.Assert(false); return false; }
             *             if(xtw == null) { Debug.Assert(false); return false; }
             *
             *             f.m_xmlWriter = xtw;
             *
             *             xtw.Formatting = Formatting.Indented;
             *             xtw.IndentChar = '\t';
             *             xtw.Indentation = 1;
             *
             *             xtw.WriteStartDocument(true);
             *             xtw.WriteStartElement(ElemRoot);
             *
             *             foreach(PwEntry pe in vEntries)
             *                     f.WriteEntry(pe, false);
             *
             *             xtw.WriteEndElement();
             *             xtw.WriteEndDocument();
             *
             *             xtw.Flush();
             *             xtw.Close();
             *             return true; */

            PwDatabase pd = new PwDatabase();

            pd.New(new IOConnectionInfo(), new CompositeKey());

            foreach (PwEntry peCopy in vEntries)
            {
                pd.RootGroup.AddEntry(peCopy.CloneDeep(), true);
            }

            Kdb4File f = new Kdb4File(pd);

            f.Save(msOutput, null, Kdb4Format.PlainXml, null);
            return(true);
        }
Exemplo n.º 8
0
		public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
			IStatusLogger slLogger)
		{
			PwDatabase pd = (pwExportInfo.ContextDatabase ?? new PwDatabase());

			PwObjectList<PwDeletedObject> vDel = null;
			if(pwExportInfo.ExportDeletedObjects == false)
			{
				vDel = pd.DeletedObjects.CloneShallow();
				pd.DeletedObjects.Clear();
			}

			Kdb4File kdb = new Kdb4File(pd);
			kdb.Save(sOutput, pwExportInfo.DataGroup, Kdb4Format.PlainXml, slLogger);

			// Restore deleted objects list
			if(vDel != null) pd.DeletedObjects.Add(vDel);

			return true;
		}
Exemplo n.º 9
0
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="pwDatabase">Source database.</param>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        public static List <PwEntry> ReadEntries(PwDatabase pwDatabase, Stream msData)
        {
            Kdb4File f = new Kdb4File(pwDatabase);

            f.m_format = Kdb4Format.PlainXml;

            XmlDocument doc = new XmlDocument();

            doc.Load(msData);

            XmlElement el = doc.DocumentElement;

            if (el.Name != ElemRoot)
            {
                throw new FormatException();
            }

            List <PwEntry> vEntries = new List <PwEntry>();

            foreach (XmlNode xmlChild in el.ChildNodes)
            {
                if (xmlChild.Name == ElemEntry)
                {
                    PwEntry pe = f.ReadEntry(xmlChild);
                    pe.Uuid = new PwUuid(true);

                    foreach (PwEntry peHistory in pe.History)
                    {
                        peHistory.Uuid = pe.Uuid;
                    }

                    vEntries.Add(pe);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            return(vEntries);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Write entries to a stream.
        /// </summary>
        /// <param name="msOutput">Output stream to which the entries will be written.</param>
        /// <param name="pwDatabase">Source database.</param>
        /// <param name="vEntries">Entries to serialize.</param>
        /// <returns>Returns <c>true</c>, if the entries were written successfully to the stream.</returns>
        public static bool WriteEntries(Stream msOutput, PwDatabase pwDatabase, PwEntry[] vEntries)
        {
            Kdb4File f = new Kdb4File(pwDatabase);

            f.m_format = Kdb4Format.PlainXml;

            XmlTextWriter xtw = null;

            try { xtw = new XmlTextWriter(msOutput, new UTF8Encoding(false)); }
            catch (Exception) { Debug.Assert(false); return(false); }
            if (xtw == null)
            {
                Debug.Assert(false); return(false);
            }

            f.m_xmlWriter = xtw;

            xtw.Formatting  = Formatting.Indented;
            xtw.IndentChar  = '\t';
            xtw.Indentation = 1;

            xtw.WriteStartDocument(true);
            xtw.WriteStartElement(ElemRoot);

            foreach (PwEntry pe in vEntries)
            {
                f.WriteEntry(pe, false);
            }

            xtw.WriteEndElement();
            xtw.WriteEndDocument();

            xtw.Flush();
            xtw.Close();
            return(true);
        }
Exemplo n.º 11
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			Kdb4File kdb4 = new Kdb4File(pwStorage);
			kdb4.Load(sInput, Kdb4Format.PlainXml, slLogger);
		}
Exemplo n.º 12
0
        /// <summary>
        /// Save the currently opened database. The file is written to the location
        /// it has been opened from.
        /// </summary>
        /// <param name="slLogger">Logger that recieves status information.</param>
        public void Save(IStatusLogger slLogger)
        {
            bool bMadeUnhidden = UrlUtil.UnhideFile(m_ioSource.Path);

            Stream s = IOConnection.OpenWrite(m_ioSource);

            Kdb4File kdb = new Kdb4File(this);
            kdb.Save(s, null, Kdb4Format.Default, slLogger);

            if(bMadeUnhidden) UrlUtil.HideFile(m_ioSource.Path, true); // Hide again

            m_pbHashOfLastIO = kdb.HashOfFileOnDisk;
            m_pbHashOfFileOnDisk = kdb.HashOfFileOnDisk;
            Debug.Assert(m_pbHashOfFileOnDisk != null);

            m_bModified = false;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Open a database. The URL may point to any supported data source.
        /// </summary>
        /// <param name="ioSource">IO connection to load the database from.</param>
        /// <param name="pwKey">Key used to open the specified database.</param>
        /// <param name="slLogger">Logger, which gets all status messages.</param>
        public void Open(IOConnectionInfo ioSource, CompositeKey pwKey,
            IStatusLogger slLogger)
        {
            Debug.Assert(ioSource != null);
            if(ioSource == null) throw new ArgumentNullException("ioSource");
            Debug.Assert(pwKey != null);
            if(pwKey == null) throw new ArgumentNullException("pwKey");

            this.Close();

            try
            {
                m_pgRootGroup = new PwGroup(true, true, UrlUtil.StripExtension(
                    UrlUtil.GetFileName(ioSource.Path)), PwIcon.FolderOpen);
                m_pgRootGroup.IsExpanded = true;

                m_pwUserKey = pwKey;

                m_bModified = false;

                Kdb4File kdb4 = new Kdb4File(this);
                Stream s = IOConnection.OpenRead(ioSource);
                kdb4.Load(s, Kdb4Format.Default, slLogger);
                s.Close();

                m_pbHashOfLastIO = kdb4.HashOfFileOnDisk;
                m_pbHashOfFileOnDisk = kdb4.HashOfFileOnDisk;
                Debug.Assert(m_pbHashOfFileOnDisk != null);

                m_bDatabaseOpened = true;
                m_ioSource = ioSource;
            }
            catch(Exception)
            {
                this.Clear();
                throw;
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Write entries to a stream.
        /// </summary>
        /// <param name="msOutput">Output stream to which the entries will be written.</param>
        /// <param name="vEntries">Entries to serialize.</param>
        /// <returns>Returns <c>true</c>, if the entries were written successfully
        /// to the stream.</returns>
        public static bool WriteEntries(Stream msOutput, PwEntry[] vEntries)
        {
            /* Kdb4File f = new Kdb4File(pwDatabase);
            f.m_format = Kdb4Format.PlainXml;

            XmlTextWriter xtw = null;
            try { xtw = new XmlTextWriter(msOutput, StrUtil.Utf8); }
            catch(Exception) { Debug.Assert(false); return false; }
            if(xtw == null) { Debug.Assert(false); return false; }

            f.m_xmlWriter = xtw;

            xtw.Formatting = Formatting.Indented;
            xtw.IndentChar = '\t';
            xtw.Indentation = 1;

            xtw.WriteStartDocument(true);
            xtw.WriteStartElement(ElemRoot);

            foreach(PwEntry pe in vEntries)
                f.WriteEntry(pe, false);

            xtw.WriteEndElement();
            xtw.WriteEndDocument();

            xtw.Flush();
            xtw.Close();
            return true; */

            PwDatabase pd = new PwDatabase();
            pd.New(new IOConnectionInfo(), new CompositeKey());

            foreach(PwEntry peCopy in vEntries)
                pd.RootGroup.AddEntry(peCopy.CloneDeep(), true);

            Kdb4File f = new Kdb4File(pd);
            f.Save(msOutput, null, Kdb4Format.PlainXml, null);
            return true;
        }