aboutsummaryrefslogtreecommitdiff
path: root/src/api/burn/WixToolset.Mba.Core/StrUtil.cs
blob: 5daef8e3d2750d2b565d79421ed69a42d943e930 (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Mba.Core
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Text;

    internal static class StrUtil
    {
        [DllImport("mbanative.dll", ExactSpelling = true)]
        internal static extern void StrFree(
            IntPtr scz
            );

        internal sealed class StrHandle : SafeHandleZeroIsDefaultAndInvalid
        {
            protected override bool ReleaseHandle()
            {
                StrFree(this.handle);
                return true;
            }

            public string ToUniString()
            {
                return Marshal.PtrToStringUni(this.handle);
            }

            public SecureString ToSecureString()
            {
                if (this.handle == IntPtr.Zero)
                {
                    return null;
                }

                SecureString value = new SecureString();
                char c;
                for (int charIndex = 0; ; charIndex++)
                {
                    c = (char)Marshal.ReadInt16(this.handle, charIndex * UnicodeEncoding.CharSize);
                    if (c == '\0')
                    {
                        break;
                    }

                    value.AppendChar(c);
                }

                return value;
            }
        }
    }
}