~ubuntu-branches/ubuntu/wily/keepass2/wily-proposed

« back to all changes in this revision

Viewing changes to KeePass/Util/Spr/SprEngine.cs

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2014-08-17 15:55:03 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20140817155503-snriix2rogldinw7
Tags: upstream-2.27+dfsg
ImportĀ upstreamĀ versionĀ 2.27+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
282
282
                                }
283
283
                        }
284
284
 
285
 
                        str = EntryUtil.FillPlaceholders(str, ctx);
 
285
                        if((ctx.Flags & SprCompileFlags.Env) != SprCompileFlags.None)
 
286
                                str = FillUriSpecial(str, ctx, @"{BASE", (ctx.Base ?? string.Empty),
 
287
                                        ctx.BaseIsEncoded, uRecursionLevel);
 
288
 
 
289
                        str = EntryUtil.FillPlaceholders(str, ctx, uRecursionLevel);
286
290
 
287
291
                        if((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
288
292
                                str = ReplacePickChars(str, ctx, uRecursionLevel);
407
411
                        return str;
408
412
                }
409
413
 
410
 
                private const string UrlSpecialRmvScm = @"{URL:RMVSCM}";
411
 
                private const string UrlSpecialScm = @"{URL:SCM}";
412
 
                private const string UrlSpecialHost = @"{URL:HOST}";
413
 
                private const string UrlSpecialPort = @"{URL:PORT}";
414
 
                private const string UrlSpecialPath = @"{URL:PATH}";
415
 
                private const string UrlSpecialQuery = @"{URL:QUERY}";
416
414
                private static string FillEntryStringsSpecial(string str, SprContext ctx,
417
415
                        uint uRecursionLevel)
418
416
                {
419
 
                        if((str.IndexOf(UrlSpecialRmvScm, SprEngine.ScMethod) >= 0) ||
420
 
                                (str.IndexOf(UrlSpecialScm, SprEngine.ScMethod) >= 0) ||
421
 
                                (str.IndexOf(UrlSpecialHost, SprEngine.ScMethod) >= 0) ||
422
 
                                (str.IndexOf(UrlSpecialPort, SprEngine.ScMethod) >= 0) ||
423
 
                                (str.IndexOf(UrlSpecialPath, SprEngine.ScMethod) >= 0) ||
424
 
                                (str.IndexOf(UrlSpecialQuery, SprEngine.ScMethod) >= 0))
 
417
                        return FillUriSpecial(str, ctx, @"{URL", ctx.Entry.Strings.ReadSafe(
 
418
                                PwDefs.UrlField), false, uRecursionLevel);
 
419
                }
 
420
 
 
421
                private static string FillUriSpecial(string strText, SprContext ctx,
 
422
                        string strPlhInit, string strData, bool bDataIsEncoded,
 
423
                        uint uRecursionLevel)
 
424
                {
 
425
                        Debug.Assert(strPlhInit.StartsWith(@"{") && !strPlhInit.EndsWith(@"}"));
 
426
                        Debug.Assert(strData != null);
 
427
 
 
428
                        string[] vPlhs = new string[] {
 
429
                                strPlhInit + @"}",
 
430
                                strPlhInit + @":RMVSCM}",
 
431
                                strPlhInit + @":SCM}",
 
432
                                strPlhInit + @":HOST}",
 
433
                                strPlhInit + @":PORT}",
 
434
                                strPlhInit + @":PATH}",
 
435
                                strPlhInit + @":QUERY}",
 
436
                                strPlhInit + @":USERINFO}",
 
437
                                strPlhInit + @":USERNAME}",
 
438
                                strPlhInit + @":PASSWORD}"
 
439
                        };
 
440
 
 
441
                        string str = strText;
 
442
                        string strDataCmp = null;
 
443
                        Uri uri = null;
 
444
                        for(int i = 0; i < vPlhs.Length; ++i)
425
445
                        {
426
 
                                SprContext ctxRaw = ctx.WithoutContentTransformations();
427
 
                                string strUrl = SprEngine.FillIfExists(@"{URL}", @"{URL}",
428
 
                                        ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctxRaw, uRecursionLevel);
429
 
 
430
 
                                str = SprEngine.FillPlaceholder(str, UrlSpecialRmvScm,
431
 
                                        UrlUtil.RemoveScheme(strUrl), ctx);
432
 
 
433
 
                                try
434
 
                                {
435
 
                                        Uri uri = new Uri(strUrl);
436
 
 
437
 
                                        str = SprEngine.FillPlaceholder(str, UrlSpecialScm,
438
 
                                                uri.Scheme, ctx);
439
 
                                        str = SprEngine.FillPlaceholder(str, UrlSpecialHost,
440
 
                                                uri.Host, ctx);
441
 
                                        str = SprEngine.FillPlaceholder(str, UrlSpecialPort,
442
 
                                                uri.Port.ToString(NumberFormatInfo.InvariantInfo), ctx);
443
 
                                        str = SprEngine.FillPlaceholder(str, UrlSpecialPath,
444
 
                                                uri.AbsolutePath, ctx);
445
 
                                        str = SprEngine.FillPlaceholder(str, UrlSpecialQuery,
446
 
                                                uri.Query, ctx);
447
 
                                }
448
 
                                catch(Exception) { } // Invalid URI
 
446
                                string strPlh = vPlhs[i];
 
447
                                if(str.IndexOf(strPlh, SprEngine.ScMethod) < 0) continue;
 
448
 
 
449
                                if(strDataCmp == null)
 
450
                                {
 
451
                                        SprContext ctxData = (bDataIsEncoded ?
 
452
                                                ctx.WithoutContentTransformations() : ctx);
 
453
                                        strDataCmp = SprEngine.CompileInternal(strData, ctxData,
 
454
                                                uRecursionLevel + 1);
 
455
                                }
 
456
 
 
457
                                string strRep = null;
 
458
                                if(i == 0) strRep = strDataCmp;
 
459
                                else if(i == 1) strRep = UrlUtil.RemoveScheme(strDataCmp);
 
460
                                else
 
461
                                {
 
462
                                        try
 
463
                                        {
 
464
                                                if(uri == null) uri = new Uri(strDataCmp);
 
465
 
 
466
                                                int t;
 
467
                                                switch(i)
 
468
                                                {
 
469
                                                        case 2: strRep = uri.Scheme; break;
 
470
                                                        case 3: strRep = uri.Host; break;
 
471
                                                        case 4:
 
472
                                                                strRep = uri.Port.ToString(
 
473
                                                                        NumberFormatInfo.InvariantInfo);
 
474
                                                                break;
 
475
                                                        case 5: strRep = uri.AbsolutePath; break;
 
476
                                                        case 6: strRep = uri.Query; break;
 
477
                                                        case 7: strRep = uri.UserInfo; break;
 
478
                                                        case 8:
 
479
                                                                strRep = uri.UserInfo;
 
480
                                                                t = strRep.IndexOf(':');
 
481
                                                                if(t >= 0) strRep = strRep.Substring(0, t);
 
482
                                                                break;
 
483
                                                        case 9:
 
484
                                                                strRep = uri.UserInfo;
 
485
                                                                t = strRep.IndexOf(':');
 
486
                                                                if(t < 0) strRep = string.Empty;
 
487
                                                                else strRep = strRep.Substring(t + 1);
 
488
                                                                break;
 
489
                                                        default: Debug.Assert(false); break;
 
490
                                                }
 
491
                                        }
 
492
                                        catch(Exception) { } // Invalid URI
 
493
                                }
 
494
                                if(strRep == null) strRep = string.Empty; // No assert
 
495
 
 
496
                                str = StrUtil.ReplaceCaseInsensitive(str, strPlh, strRep);
449
497
                        }
450
498
 
451
499
                        return str;
554
602
 
555
603
                        SearchParameters sp = SearchParameters.None;
556
604
                        sp.SearchString = strRef.Substring(4);
 
605
                        sp.RespectEntrySearchingDisabled = false;
 
606
 
557
607
                        if(chScan == 'T') sp.SearchInTitles = true;
558
608
                        else if(chScan == 'U') sp.SearchInUserNames = true;
559
609
                        else if(chScan == 'A') sp.SearchInUrls = true;
626
676
                /// Parse and remove a placeholder of the form
627
677
                /// <c>{PLH:/Param1/Param2/.../}</c>.
628
678
                /// </summary>
629
 
                private static bool ParseAndRemovePlhWithParams(ref string str,
 
679
                internal static bool ParseAndRemovePlhWithParams(ref string str,
630
680
                        SprContext ctx, uint uRecursionLevel, string strPlhStart,
631
681
                        out int iStart, out List<string> lParams, bool bSprCmpParams)
632
682
                {
662
712
                        }
663
713
                        catch(Exception)
664
714
                        {
665
 
                                str = str.Substring(0, strPlhStart.Length);
 
715
                                str = str.Substring(0, iStart);
666
716
                        }
667
717
 
668
718
                        if(bSprCmpParams && (ctx != null))
723
773
                                        }
724
774
                                        else if(strCmd == "uri")
725
775
                                                strNew = Uri.EscapeDataString(strNew);
 
776
                                        else if(strCmd == "uri-dec")
 
777
                                                strNew = Uri.UnescapeDataString(strNew);
726
778
 
727
779
                                        strNew = TransformContent(strNew, ctx);
728
780
                                        str = str.Insert(iStart, strNew);