1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using KeePass.App;
using KeePassLib;
using KeePassLib.Utility;
namespace KeePass.Util.Spr
{
public static class SprSyntax
{
private static readonly string[] m_vDynSepPlh = new string[] {
@"{NEWPASSWORD:", @"{T-REPLACE-RX:", @"{T-CONV:",
@"{CMD:"
};
private static readonly SprStyle SprStyleOK = new SprStyle(
Color.FromArgb(0, 128, 0));
// Color.FromArgb(212, 255, 212));
private static readonly SprStyle SprStyleRaw = new SprStyle(
Color.FromArgb(0, 0, 224));
// Color.FromArgb(214, 226, 255));
private static readonly SprStyle SprStyleWarning = new SprStyle(
Color.FromArgb(255, 106, 0));
// AppDefs.ColorEditError);
private static readonly SprStyle SprStyleError = new SprStyle(
Color.FromArgb(224, 0, 0));
// AppDefs.ColorEditError);
private sealed class SprStyle : IEquatable<SprStyle> // Immutable
{
private readonly Color? m_clr;
public Color? Color { get { return m_clr; } }
public SprStyle(Color? clr)
{
m_clr = clr;
}
public bool Equals(SprStyle other)
{
if(other == null) return false;
if(m_clr.HasValue != other.m_clr.HasValue) return false;
if(m_clr.HasValue)
{
if(m_clr.Value != other.m_clr.Value) return false;
}
return true;
}
}
private sealed class SprPart // Immutable
{
private readonly string m_str;
public string Text { get { return m_str; } }
private readonly int m_iStart;
public int Start { get { return m_iStart; } }
public SprPart(string str, int iStart)
{
if(str == null) throw new ArgumentNullException("str");
m_str = str;
m_iStart = iStart;
}
public SprPart GetPart(int iOffset)
{
return GetPart(iOffset, m_str.Length - iOffset);
}
public SprPart GetPart(int iOffset, int nLength)
{
if(iOffset < 0) throw new ArgumentOutOfRangeException("iOffset");
if(nLength < 0) throw new ArgumentOutOfRangeException("nLength");
if((iOffset + nLength) > m_str.Length) throw new ArgumentException();
SprPart pSub = new SprPart(m_str.Substring(iOffset, nLength),
m_iStart + iOffset);
return pSub;
}
}
public static void Highlight(RichTextBox rtb, SprContext ctx)
{
try { HighlightPriv(rtb, ctx); }
catch(Exception) { Debug.Assert(false); }
}
private static void HighlightPriv(RichTextBox rtb, SprContext ctx)
{
if(rtb == null) { Debug.Assert(false); return; }
int iSelStart = rtb.SelectionStart;
int iSelLen = rtb.SelectionLength;
string strText = rtb.Text;
rtb.SelectAll();
// rtb.SelectionBackColor = SystemColors.Window;
rtb.SelectionColor = SystemColors.ControlText;
List<SprStyle> l = GetHighlight(strText, ctx);
l.Add(null); // Sentinel
SprStyle sFrom = null;
int pFrom = 0;
for(int i = 0; i < l.Count; ++i)
{
SprStyle s = l[i];
if(sFrom != null)
{
if((s == null) || !sFrom.Equals(s))
{
if(sFrom.Color.HasValue)
{
rtb.Select(pFrom, i - pFrom);
rtb.SelectionColor = sFrom.Color.Value;
// rtb.SelectionBackColor = sFrom.Color.Value;
}
sFrom = s;
pFrom = i;
}
}
else
{
sFrom = s;
pFrom = i;
}
}
rtb.Select(iSelStart, iSelLen);
}
private static List<SprStyle> GetHighlight(string str, SprContext ctx)
{
if(str == null) { Debug.Assert(false); return new List<SprStyle>(); }
List<SprStyle> l = new List<SprStyle>(str.Length + 1); // 1 for sentinel
for(int i = 0; i < str.Length; ++i) l.Add(null);
Stack<SprPart> sToDo = new Stack<SprPart>();
sToDo.Push(new SprPart(str, 0));
while(sToDo.Count > 0)
{
SprPart p = sToDo.Pop();
Debug.Assert(p.Start >= 0);
Debug.Assert((p.Start + p.Text.Length) <= l.Count);
Debug.Assert(str.Substring(p.Start, p.Text.Length) == p.Text);
HighlightPart(p, l, sToDo, ctx);
}
return l;
}
private static void HighlightPart(SprPart p, List<SprStyle> lStyles,
Stack<SprPart> sToDo, SprContext ctx)
{
if(p.Text.Length == 0) return;
if(HighlightDynSepPlh(p, lStyles, sToDo)) return;
if(HighlightRegularPlh(p, lStyles, sToDo, ctx)) return;
HighlightInvalid(p, lStyles, sToDo, ctx);
}
private static bool HighlightDynSepPlh(SprPart pPart, List<SprStyle> lStyles,
Stack<SprPart> sToDo)
{
string str = pPart.Text;
int iStart = -1, p = -1;
foreach(string strPlh in m_vDynSepPlh)
{
iStart = str.IndexOf(strPlh, StrUtil.CaseIgnoreCmp);
if(iStart >= 0)
{
p = iStart + strPlh.Length;
break;
}
}
if(iStart < 0) return false;
try
{
if(p >= str.Length) throw new FormatException();
char chSep = str[p];
while(true)
{
if((p + 1) >= str.Length) throw new FormatException();
if(str[p + 1] == '}') break;
int q = str.IndexOf(chSep, p + 1);
if(q < 0) throw new FormatException();
p = q;
}
Debug.Assert(str[p + 1] == '}');
if((p + 2) < str.Length)
sToDo.Push(pPart.GetPart(p + 2));
SetStyle(lStyles, pPart, iStart, (p + 1) - iStart + 1, SprStyleRaw);
}
catch(Exception)
{
SetStyle(lStyles, pPart, iStart, str.Length - iStart, SprStyleError);
}
if(iStart > 0) sToDo.Push(pPart.GetPart(0, iStart));
return true;
}
private static bool HighlightRegularPlh(SprPart pPart, List<SprStyle> lStyles,
Stack<SprPart> sToDo, SprContext ctx)
{
string str = pPart.Text;
int iStart = str.IndexOf('{');
if(iStart < 0) return false;
if((iStart + 2) >= str.Length)
SetStyle(lStyles, pPart, iStart, str.Length - iStart, SprStyleError);
else
{
int iOpen = str.IndexOf('{', iStart + 2);
int iClose = str.IndexOf('}', iStart + 2);
bool bAT = ((ctx != null) && ctx.EncodeAsAutoTypeSequence);
if(iClose < 0)
SetStyle(lStyles, pPart, iStart, str.Length - iStart, SprStyleError);
else if((iOpen >= 0) && (iOpen < iClose))
{
sToDo.Push(pPart.GetPart(iOpen));
SetStyle(lStyles, pPart, iStart, iOpen - iStart, SprStyleError);
}
else if(bAT && ((str[iStart + 1] == '{') || (str[iStart + 1] == '}')) &&
(iClose != (iStart + 2)))
{
sToDo.Push(pPart.GetPart(iClose + 1));
SetStyle(lStyles, pPart, iStart, iClose - iStart + 1, SprStyleError);
}
else
{
sToDo.Push(pPart.GetPart(iClose + 1));
int iErrLvl = 0;
string strPlh = str.Substring(iStart + 1, iClose - iStart - 1);
if(strPlh.Length == 0) { Debug.Assert(false); iErrLvl = 2; }
else if(char.IsWhiteSpace(strPlh[0])) iErrLvl = 2;
else if(strPlh.StartsWith("S:", StrUtil.CaseIgnoreCmp) &&
(ctx != null) && (ctx.Entry != null))
{
string strField = strPlh.Substring(2);
List<string> lFields = PwDefs.GetStandardFields();
lFields.AddRange(ctx.Entry.Strings.GetKeys());
bool bFound = false;
foreach(string strAvail in lFields)
{
if(strField.Equals(strAvail, StrUtil.CaseIgnoreCmp))
{
bFound = true;
break;
}
}
if(!bFound) iErrLvl = 1;
}
SprStyle s = SprStyleOK;
if(iErrLvl == 1) s = SprStyleWarning;
else if(iErrLvl > 1) s = SprStyleError;
SetStyle(lStyles, pPart, iStart, iClose - iStart + 1, s);
}
}
if(iStart > 0) sToDo.Push(pPart.GetPart(0, iStart));
return true;
}
private static bool HighlightInvalid(SprPart pPart, List<SprStyle> lStyles,
Stack<SprPart> sToDo, SprContext ctx)
{
bool bAT = ((ctx != null) && ctx.EncodeAsAutoTypeSequence);
if(!bAT) return false;
string str = pPart.Text;
int p = str.IndexOf('}');
if(p >= 0)
{
Debug.Assert(str.IndexOf('{') < 0); // Must be called after regular
sToDo.Push(pPart.GetPart(p + 1));
SetStyle(lStyles, pPart, 0, p + 1, SprStyleError);
return true;
}
return false;
}
private static void SetStyle(List<SprStyle> l, SprPart p,
int iPartOffset, int nLength, SprStyle s)
{
int px = p.Start + iPartOffset;
if(px < 0) { Debug.Assert(false); return; }
if(nLength < 0) { Debug.Assert(false); return; }
if((px + nLength) > l.Count) { Debug.Assert(false); return; }
for(int i = 0; i < nLength; ++i)
{
Debug.Assert(l[px + i] == null);
l[px + i] = s;
}
}
}
}
|