public static string GetDereferencedString(PwEntry pe, string s) { if (pe == null) { return(s); } if (!s.ToLowerInvariant().Contains("{ref:")) { return(s); } KeePass.Util.Spr.SprContext ctx = new KeePass.Util.Spr.SprContext(pe, Program.MainForm.DocumentManager.FindContainerOf(pe), KeePass.Util.Spr.SprCompileFlags.Deref); return(KeePass.Util.Spr.SprEngine.Compile(s, ctx)); }
private string GetEntryField(PwEntry entry, string FieldName) { const string StrRefStart = @"{REF:"; const string StrRefEnd = @"}"; const StringComparison ScMethod = StringComparison.OrdinalIgnoreCase; string input = entry.Strings.ReadSafe(FieldName); //replace field references //See also KeePass source SprEngine.cs function FillRefPlaceholders if (input.IndexOf(StrRefStart, 0, ScMethod) < 0) { //nothing to do return(input); } KeePass.Util.Spr.SprContext ctx = new KeePass.Util.Spr.SprContext(entry, KeePassHost.MainWindow.DocumentManager.SafeFindContainerOf(entry), KeePass.Util.Spr.SprCompileFlags.Deref); int nOffset = 0; int maxTries = 100; while (true) { int nStart = input.IndexOf(StrRefStart, nOffset, ScMethod); if (nStart < 0) { break; } int nEnd = input.IndexOf(StrRefEnd, nStart + 1, ScMethod); if (nEnd <= nStart) { break; } string strFullRef = input.Substring(nStart, nEnd - nStart + 1); char chScan, chWanted; PwEntry peFound = KeePass.Util.Spr.SprEngine.FindRefTarget(strFullRef, ctx, out chScan, out chWanted); if (peFound != null) { string strInsData; if (chWanted == 'T') { strInsData = peFound.Strings.ReadSafe(PwDefs.TitleField); } else if (chWanted == 'U') { strInsData = peFound.Strings.ReadSafe(PwDefs.UserNameField); } else if (chWanted == 'A') { strInsData = peFound.Strings.ReadSafe(PwDefs.UrlField); } else if (chWanted == 'P') { strInsData = peFound.Strings.ReadSafe(PwDefs.PasswordField); } else if (chWanted == 'N') { strInsData = peFound.Strings.ReadSafe(PwDefs.NotesField); } else if (chWanted == 'I') { strInsData = peFound.Uuid.ToHexString(); } else { nOffset = nStart + 1; continue; } input = input.Substring(0, nStart) + strInsData + input.Substring(nEnd + StrRefEnd.Length); maxTries--; if (maxTries <= 0) { break; } } else { nOffset = nStart + 1; continue; } } return(input); }