aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolset.Dtf.Resources/VersionInfo.cs
blob: 38224d1258b650dce3f3af83559337daef23e359 (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
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
// 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.Dtf.Resources
{
    using System;
    using System.IO;
    using System.Text;
    using System.Reflection;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;

    internal class VersionInfo : ICollection<VersionInfo>
    {
        private string key;
        private bool isString;
        private byte[] data;
        private List<VersionInfo> children;

        public VersionInfo(string key)
            : base()
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            this.key = key;
            this.children = new List<VersionInfo>();
        }

        public string Key
        {
            get
            {
                return this.key;
            }

            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }

                this.key = value;
            }
        }

        public bool IsString
        {
            get
            {
                return this.isString;
            }
            
            set
            {
                this.isString = value;
            }
        }

        public byte[] Data
        {
            get
            {
                return this.data;
            }

            set
            {
                this.data = value;
                this.isString = false;
            }
        }

        public void Read(BinaryReader reader)
        {
            long basePosition = reader.BaseStream.Position;
            int verInfoSize = (int) reader.ReadUInt16();
            int valueSize = (int) reader.ReadUInt16();
            bool dataIsString = (reader.ReadUInt16() != 0);
            StringBuilder keyStringBuilder = new StringBuilder();
            char c;
            while ((c = (char) reader.ReadUInt16()) != 0)
            {
                keyStringBuilder.Append(c);
            }
            this.Key = keyStringBuilder.ToString();
            Pad(reader, basePosition);
            if (valueSize == 0)
            {
                this.data = null;
            }
            else
            {
                if (dataIsString) valueSize *= 2; // Count is # of chars instead of bytes
                this.data = reader.ReadBytes(valueSize);
                this.isString = dataIsString;
                Pad(reader, basePosition);
            }

            while (reader.BaseStream.Position - basePosition < verInfoSize)
            {
                Pad(reader, basePosition);
                VersionInfo childVerInfo = new VersionInfo("");
                childVerInfo.Read(reader);
                this.children.Add(childVerInfo);
            }
        }

        public void Write(BinaryWriter writer)
        {
            long basePosition = writer.BaseStream.Position;
            writer.Write((ushort) this.Length);
            byte[] valueBytes = this.data;
            writer.Write((ushort) ((valueBytes != null ? valueBytes.Length : 0) / (this.IsString ? 2 : 1)));
            writer.Write((ushort) (this.IsString ? 1 : 0));
            byte[] keyBytes = new byte[Encoding.Unicode.GetByteCount(this.Key) + 2];
            Encoding.Unicode.GetBytes(this.Key, 0, this.Key.Length, keyBytes, 0);
            writer.Write(keyBytes);
            Pad(writer, basePosition);
            if (valueBytes != null)
            {
                writer.Write(valueBytes);
                Pad(writer, basePosition);
            }

            foreach (VersionInfo childVersionInfo in this.children)
            {
                Pad(writer, basePosition);
                childVersionInfo.Write(writer);
            }
        }

        private static void Pad(BinaryReader reader, long basePosition)
        {
            long position = reader.BaseStream.Position;
            int diff = (int) (position - basePosition) % 4;
            if (diff > 0) while (diff++ < 4 && reader.BaseStream.Position < reader.BaseStream.Length) reader.ReadByte();
        }
        private static void Pad(BinaryWriter writer, long basePosition)
        {
            long position = writer.BaseStream.Position;
            int diff = (int) (position - basePosition) % 4;
            if (diff > 0) while (diff++ < 4) writer.Write((byte) 0);
        }

        private int Length
        {
            get
            {
                int len = 6 + Encoding.Unicode.GetByteCount(this.Key) + 2;
                if (len % 4 > 0) len += (4 - len % 4);
                len += (this.data != null ? this.data.Length : 0);
                if (len % 4 > 0) len += (4 - len % 4);
                foreach (VersionInfo childVersionInfo in this.children)
                {
                    if (len % 4 > 0) len += (4 - len % 4);
                    len += childVersionInfo.Length;
                }
                return len;
            }
        }

        public static explicit operator VersionInfo(byte[] bytesValue)
        {
            VersionInfo viValue = new VersionInfo("");
            using (BinaryReader reader = new BinaryReader(new MemoryStream(bytesValue, false)))
            {
                viValue.Read(reader);
            }
            return viValue;
        }

        public static explicit operator byte[](VersionInfo viValue)
        {
            byte[] bytesValue = new byte[viValue.Length];
            using (BinaryWriter writer = new BinaryWriter(new MemoryStream(bytesValue, true)))
            {
                viValue.Write(writer);
            }
            return bytesValue;
        }

        public VersionInfo this[string itemKey]
        {
            get
            {
                int index = this.IndexOf(itemKey);
                if (index < 0) return null;
                return this.children[index];
            }
        }

        public void Add(VersionInfo item)
        {
            this.children.Add(item);
        }

        public bool Remove(VersionInfo item)
        {
            return this.children.Remove(item);
        }

        public bool Remove(string itemKey)
        {
            int index = this.IndexOf(itemKey);
            if (index >= 0)
            {
                this.children.RemoveAt(index);
                return true;
            }
            else
            {
                return false;
            }
        }

        private int IndexOf(string itemKey)
        {
            for (int i = 0; i < this.children.Count; i++)
            {
                if (this.children[i].Key == itemKey) return i;
            }
            return -1;
        }

        public bool Contains(VersionInfo item)
        {
            return this.children.Contains(item);
        }

        public void CopyTo(VersionInfo[] array, int index)
        {
            this.children.CopyTo(array, index);
        }

        public void Clear()
        {
            this.children.Clear();
        }

        public int Count
        {
            get
            {
                return this.children.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public IEnumerator<VersionInfo> GetEnumerator()
        {
            return this.children.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}