~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
  Copyright (C) 2010-2012 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using IKVM.Reflection.Reader;

namespace IKVM.Reflection.Writer
{
	sealed class ResourceSection
	{
		private const int RT_ICON = 3;
		private const int RT_GROUP_ICON = 14;
		private const int RT_VERSION = 16;
		private const int RT_MANIFEST = 24;
		private ResourceDirectoryEntry root = new ResourceDirectoryEntry(new OrdinalOrName("root"));
		private ByteBuffer bb;
		private List<int> linkOffsets;

		internal void AddVersionInfo(ByteBuffer versionInfo)
		{
			root[new OrdinalOrName(RT_VERSION)][new OrdinalOrName(1)][new OrdinalOrName(0)].Data = versionInfo;
		}

		internal void AddIcon(byte[] iconFile)
		{
			BinaryReader br = new BinaryReader(new MemoryStream(iconFile));
			ushort idReserved = br.ReadUInt16();
			ushort idType = br.ReadUInt16();
			ushort idCount = br.ReadUInt16();
			if (idReserved != 0 || idType != 1)
			{
				throw new ArgumentException("The supplied byte array is not a valid .ico file.");
			}
			ByteBuffer group = new ByteBuffer(6 + 14 * idCount);
			group.Write(idReserved);
			group.Write(idType);
			group.Write(idCount);
			for (int i = 0; i < idCount; i++)
			{
				byte bWidth = br.ReadByte();
				byte bHeight = br.ReadByte();
				byte bColorCount = br.ReadByte();
				byte bReserved = br.ReadByte();
				ushort wPlanes = br.ReadUInt16();
				ushort wBitCount = br.ReadUInt16();
				uint dwBytesInRes = br.ReadUInt32();
				uint dwImageOffset = br.ReadUInt32();

				// we start the icon IDs at 2
				ushort id = (ushort)(2 + i);

				group.Write(bWidth);
				group.Write(bHeight);
				group.Write(bColorCount);
				group.Write(bReserved);
				group.Write(wPlanes);
				group.Write(wBitCount);
				group.Write(dwBytesInRes);
				group.Write(id);

				byte[] icon = new byte[dwBytesInRes];
				Buffer.BlockCopy(iconFile, (int)dwImageOffset, icon, 0, icon.Length);
				root[new OrdinalOrName(RT_ICON)][new OrdinalOrName(id)][new OrdinalOrName(0)].Data = ByteBuffer.Wrap(icon);
			}
			root[new OrdinalOrName(RT_GROUP_ICON)][new OrdinalOrName(32512)][new OrdinalOrName(0)].Data = group;
		}

		internal void AddManifest(byte[] manifest, ushort resourceID)
		{
			root[new OrdinalOrName(RT_MANIFEST)][new OrdinalOrName(resourceID)][new OrdinalOrName(0)].Data = ByteBuffer.Wrap(manifest);
		}

		internal void ExtractResources(byte[] buf)
		{
			ByteReader br = new ByteReader(buf, 0, buf.Length);
			while (br.Length >= 32)
			{
				br.Align(4);
				RESOURCEHEADER hdr = new RESOURCEHEADER(br);
				if (hdr.DataSize != 0)
				{
					root[hdr.TYPE][hdr.NAME][new OrdinalOrName(hdr.LanguageId)].Data = ByteBuffer.Wrap(br.ReadBytes(hdr.DataSize));
				}
			}
		}

		internal void Finish()
		{
			if (bb != null)
			{
				throw new InvalidOperationException();
			}
			bb = new ByteBuffer(1024);
			linkOffsets = new List<int>();
			root.Write(bb, linkOffsets);
			root = null;
		}

		internal int Length
		{
			get { return bb.Length; }
		}

		internal void Write(MetadataWriter mw, uint rva)
		{
			foreach (int offset in linkOffsets)
			{
				bb.Position = offset;
				bb.Write(bb.GetInt32AtCurrentPosition() + (int)rva);
			}
			mw.Write(bb);
		}
	}

	sealed class ResourceDirectoryEntry
	{
		internal readonly OrdinalOrName OrdinalOrName;
		internal ByteBuffer Data;
		private int namedEntries;
		private readonly List<ResourceDirectoryEntry> entries = new List<ResourceDirectoryEntry>();

		internal ResourceDirectoryEntry(OrdinalOrName id)
		{
			this.OrdinalOrName = id;
		}

		internal ResourceDirectoryEntry this[OrdinalOrName id]
		{
			get
			{
				foreach (ResourceDirectoryEntry entry in entries)
				{
					if (entry.OrdinalOrName.IsEqual(id))
					{
						return entry;
					}
				}
				// the entries must be sorted
				ResourceDirectoryEntry newEntry = new ResourceDirectoryEntry(id);
				if (id.Name == null)
				{
					for (int i = namedEntries; i < entries.Count; i++)
					{
						if (entries[i].OrdinalOrName.IsGreaterThan(id))
						{
							entries.Insert(i, newEntry);
							return newEntry;
						}
					}
					entries.Add(newEntry);
					return newEntry;
				}
				else
				{
					for (int i = 0; i < namedEntries; i++)
					{
						if (entries[i].OrdinalOrName.IsGreaterThan(id))
						{
							entries.Insert(i, newEntry);
							namedEntries++;
							return newEntry;
						}
					}
					entries.Insert(namedEntries++, newEntry);
					return newEntry;
				}
			}
		}

		private int DirectoryLength
		{
			get
			{
				if (Data != null)
				{
					return 16;
				}
				else
				{
					int length = 16 + entries.Count * 8;
					foreach (ResourceDirectoryEntry entry in entries)
					{
						length += entry.DirectoryLength;
					}
					return length;
				}
			}
		}

		internal void Write(ByteBuffer bb, List<int> linkOffsets)
		{
			if (entries.Count != 0)
			{
				int stringTableOffset = this.DirectoryLength;
				Dictionary<string, int> strings = new Dictionary<string, int>();
				ByteBuffer stringTable = new ByteBuffer(16);
				int offset = 16 + entries.Count * 8;
				for (int pass = 0; pass < 3; pass++)
				{
					Write(bb, pass, 0, ref offset, strings, ref stringTableOffset, stringTable);
				}
				// the pecoff spec says that the string table is between the directory entries and the data entries,
				// but the windows linker puts them after the data entries, so we do too.
				stringTable.Align(4);
				offset += stringTable.Length;
				WriteResourceDataEntries(bb, linkOffsets, ref offset);
				bb.Write(stringTable);
				WriteData(bb);
			}
		}

		private void WriteResourceDataEntries(ByteBuffer bb, List<int> linkOffsets, ref int offset)
		{
			foreach (ResourceDirectoryEntry entry in entries)
			{
				if (entry.Data != null)
				{
					linkOffsets.Add(bb.Position);
					bb.Write(offset);
					bb.Write(entry.Data.Length);
					bb.Write(0);	// code page
					bb.Write(0);	// reserved
					offset += (entry.Data.Length + 3) & ~3;
				}
				else
				{
					entry.WriteResourceDataEntries(bb, linkOffsets, ref offset);
				}
			}
		}

		private void WriteData(ByteBuffer bb)
		{
			foreach (ResourceDirectoryEntry entry in entries)
			{
				if (entry.Data != null)
				{
					bb.Write(entry.Data);
					bb.Align(4);
				}
				else
				{
					entry.WriteData(bb);
				}
			}
		}

		private void Write(ByteBuffer bb, int writeDepth, int currentDepth, ref int offset, Dictionary<string, int> strings, ref int stringTableOffset, ByteBuffer stringTable)
		{
			if (currentDepth == writeDepth)
			{
				// directory header
				bb.Write(0);	// Characteristics
				bb.Write(0);	// Time/Date Stamp
				bb.Write(0);	// Version (Major / Minor)
				bb.Write((ushort)namedEntries);
				bb.Write((ushort)(entries.Count - namedEntries));
			}
			foreach (ResourceDirectoryEntry entry in entries)
			{
				if (currentDepth == writeDepth)
				{
					entry.WriteEntry(bb, ref offset, strings, ref stringTableOffset, stringTable);
				}
				else
				{
					entry.Write(bb, writeDepth, currentDepth + 1, ref offset, strings, ref stringTableOffset, stringTable);
				}
			}
		}

		private void WriteEntry(ByteBuffer bb, ref int offset, Dictionary<string, int> strings, ref int stringTableOffset, ByteBuffer stringTable)
		{
			WriteNameOrOrdinal(bb, OrdinalOrName, strings, ref stringTableOffset, stringTable);
			if (Data == null)
			{
				bb.Write(0x80000000U | (uint)offset);
			}
			else
			{
				bb.Write(offset);
			}
			offset += 16 + entries.Count * 8;
		}

		private static void WriteNameOrOrdinal(ByteBuffer bb, OrdinalOrName id, Dictionary<string, int> strings, ref int stringTableOffset, ByteBuffer stringTable)
		{
			if (id.Name == null)
			{
				bb.Write((int)id.Ordinal);
			}
			else
			{
				int stringOffset;
				if (!strings.TryGetValue(id.Name, out stringOffset))
				{
					stringOffset = stringTableOffset;
					strings.Add(id.Name, stringOffset);
					stringTableOffset += id.Name.Length * 2 + 2;
					stringTable.Write((ushort)id.Name.Length);
					foreach (char c in id.Name)
					{
						stringTable.Write((short)c);
					}
				}
				bb.Write(0x80000000U | (uint)stringOffset);
			}
		}
	}

	struct OrdinalOrName
	{
		internal readonly ushort Ordinal;
		internal readonly string Name;

		internal OrdinalOrName(ushort value)
		{
			Ordinal = value;
			Name = null;
		}

		internal OrdinalOrName(string value)
		{
			Ordinal = 0xFFFF;
			Name = value;
		}

		internal bool IsGreaterThan(OrdinalOrName other)
		{
			return this.Name == null
				? this.Ordinal > other.Ordinal
				: String.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) > 0;
		}

		internal bool IsEqual(OrdinalOrName other)
		{
			return this.Name == null
				? this.Ordinal == other.Ordinal
				: String.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) == 0;
		}
	}

	struct RESOURCEHEADER
	{
		internal int DataSize;
		internal int HeaderSize;
		internal OrdinalOrName TYPE;
		internal OrdinalOrName NAME;
		internal int DataVersion;
		internal ushort MemoryFlags;
		internal ushort LanguageId;
		internal int Version;
		internal int Characteristics;

		internal RESOURCEHEADER(ByteReader br)
		{
			DataSize = br.ReadInt32();
			HeaderSize = br.ReadInt32();
			TYPE = ReadOrdinalOrName(br);
			NAME = ReadOrdinalOrName(br);
			br.Align(4);
			DataVersion = br.ReadInt32();
			MemoryFlags = br.ReadUInt16();
			LanguageId = br.ReadUInt16();
			Version = br.ReadInt32();
			Characteristics = br.ReadInt32();
		}

		private static OrdinalOrName ReadOrdinalOrName(ByteReader br)
		{
			char c = br.ReadChar();
			if (c == 0xFFFF)
			{
				return new OrdinalOrName(br.ReadUInt16());
			}
			else
			{
				StringBuilder sb = new StringBuilder();
				while (c != 0)
				{
					sb.Append(c);
					c = br.ReadChar();
				}
				return new OrdinalOrName(sb.ToString());
			}
		}
	}
}