private void WriteEntries(KdbManager mgr, Dictionary <PwGroup, uint> dictGroups, PwGroup pgRoot) { bool bWarnedOnce = false; uint uGroupCount, uEntryCount, uEntriesSaved = 0; pgRoot.GetCounts(true, out uGroupCount, out uEntryCount); DateTime dtNeverExpire = KdbManager.GetNeverExpireTime(); EntryHandler eh = delegate(PwEntry pe) { KdbEntry e = new KdbEntry(); e.Uuid.Set(pe.Uuid.UuidBytes); if (pe.ParentGroup != pgRoot) { e.GroupId = dictGroups[pe.ParentGroup]; } else { e.GroupId = 1; if ((m_slLogger != null) && !bWarnedOnce) { m_slLogger.SetText(KdbPrefix + KPRes.FormatNoRootEntries, LogStatusType.Warning); bWarnedOnce = true; } if (dictGroups.Count == 0) { throw new Exception(KPRes.FormatNoSubGroupsInRoot); } } e.ImageId = (uint)pe.IconId; e.Title = pe.Strings.ReadSafe(PwDefs.TitleField); e.UserName = pe.Strings.ReadSafe(PwDefs.UserNameField); e.Password = pe.Strings.ReadSafe(PwDefs.PasswordField); e.Url = pe.Strings.ReadSafe(PwDefs.UrlField); string strNotes = pe.Strings.ReadSafe(PwDefs.NotesField); ExportCustomStrings(pe, ref strNotes); ExportAutoType(pe, ref strNotes); ExportUrlOverride(pe, ref strNotes); e.Additional = strNotes; e.PasswordLength = (uint)e.Password.Length; Debug.Assert(TimeUtil.PwTimeLength == 7); e.CreationTime.Set(pe.CreationTime); e.LastModificationTime.Set(pe.LastModificationTime); e.LastAccessTime.Set(pe.LastAccessTime); if (pe.Expires) { e.ExpirationTime.Set(pe.ExpiryTime); } else { e.ExpirationTime.Set(dtNeverExpire); } IntPtr hBinaryData = IntPtr.Zero; if (pe.Binaries.UCount >= 1) { foreach (KeyValuePair <string, ProtectedBinary> kvp in pe.Binaries) { e.BinaryDescription = kvp.Key; byte[] pbAttached = kvp.Value.ReadData(); e.BinaryDataLength = (uint)pbAttached.Length; if (e.BinaryDataLength > 0) { hBinaryData = Marshal.AllocHGlobal((int)e.BinaryDataLength); Marshal.Copy(pbAttached, 0, hBinaryData, (int)e.BinaryDataLength); e.BinaryData = hBinaryData; } break; } if ((pe.Binaries.UCount > 1) && (m_slLogger != null)) { m_slLogger.SetText(KdbPrefix + KPRes.FormatOnlyOneAttachment + "\r\n\r\n" + KPRes.Entry + ":\r\n" + KPRes.Title + ": " + e.Title + "\r\n" + KPRes.UserName + ": " + e.UserName, LogStatusType.Warning); } } bool bResult = mgr.AddEntry(ref e); Marshal.FreeHGlobal(hBinaryData); hBinaryData = IntPtr.Zero; if (!bResult) { Debug.Assert(false); throw new InvalidOperationException(); } ++uEntriesSaved; if (m_slLogger != null) { if (!m_slLogger.SetProgress((100 * uEntriesSaved) / uEntryCount)) { return(false); } } return(true); }; if (!pgRoot.TraverseTree(TraversalMethod.PreOrder, null, eh)) { throw new InvalidOperationException(); } }
private void ReadEntries(KdbManager mgr, Dictionary <UInt32, PwGroup> dictGroups) { DateTime dtNeverExpire = KdbManager.GetNeverExpireTime(); uint uEntryCount = mgr.EntryCount; for (uint uEntry = 0; uEntry < uEntryCount; ++uEntry) { KdbEntry e = mgr.GetEntry(uEntry); PwGroup pgContainer; if (!dictGroups.TryGetValue(e.GroupId, out pgContainer)) { Debug.Assert(false); continue; } PwEntry pe = new PwEntry(false, false); pe.SetUuid(new PwUuid(e.Uuid.ToByteArray()), false); pgContainer.AddEntry(pe, true, true); pe.IconId = (e.ImageId < (uint)PwIcon.Count) ? (PwIcon)e.ImageId : PwIcon.Key; pe.Strings.Set(PwDefs.TitleField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectTitle, e.Title)); pe.Strings.Set(PwDefs.UserNameField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUserName, e.UserName)); pe.Strings.Set(PwDefs.PasswordField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectPassword, e.Password)); pe.Strings.Set(PwDefs.UrlField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUrl, e.Url)); string strNotes = e.Additional; ImportAutoType(ref strNotes, pe); ImportUrlOverride(ref strNotes, pe); pe.Strings.Set(PwDefs.NotesField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectNotes, strNotes)); pe.CreationTime = e.CreationTime.ToDateTime(); pe.LastModificationTime = e.LastModificationTime.ToDateTime(); pe.LastAccessTime = e.LastAccessTime.ToDateTime(); pe.ExpiryTime = e.ExpirationTime.ToDateTime(); pe.Expires = (pe.ExpiryTime != dtNeverExpire); if ((e.BinaryDataLength > 0) && (e.BinaryData != IntPtr.Zero)) { byte[] pbData = KdbManager.ReadBinary(e.BinaryData, e.BinaryDataLength); Debug.Assert(pbData.Length == e.BinaryDataLength); string strDesc = e.BinaryDescription; if (string.IsNullOrEmpty(strDesc)) { strDesc = "Attachment"; } pe.Binaries.Set(strDesc, new ProtectedBinary(false, pbData)); } if (m_slLogger != null) { if (!m_slLogger.SetProgress((100 * uEntry) / uEntryCount)) { throw new Exception(KPRes.Cancel); } } } }