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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
//
// System.Resources/Win32Resources.cs
//
// Author:
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) 2003 Ximian, Inc. http://www.ximian.com
//
// An incomplete set of classes for manipulating Win32 resources
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace System.Resources {
internal enum Win32ResourceType {
RT_CURSOR = 1,
RT_FONT = 8,
RT_BITMAP = 2,
RT_ICON = 3,
RT_MENU = 4,
RT_DIALOG = 5,
RT_STRING = 6,
RT_FONTDIR = 7,
RT_ACCELERATOR = 9,
RT_RCDATA = 10,
RT_MESSAGETABLE = 11,
RT_GROUP_CURSOR = 12,
RT_GROUP_ICON = 14,
RT_VERSION = 16,
RT_DLGINCLUDE = 17,
RT_PLUGPLAY = 19,
RT_VXD = 20,
RT_ANICURSOR = 21,
RT_ANIICON = 22,
RT_HTML = 23,
}
internal class NameOrId {
string name;
int id;
public NameOrId (string name) {
this.name = name;
}
public NameOrId (int id) {
this.id = id;
}
public bool IsName {
get {
return name != null;
}
}
public string Name {
get {
return name;
}
}
public int Id {
get {
return id;
}
}
public override string ToString () {
if (name != null)
return "Name(" + name + ")";
else
return "Id(" + id + ")";
}
}
internal abstract class Win32Resource {
NameOrId type;
NameOrId name;
int language;
internal Win32Resource (NameOrId type, NameOrId name, int language) {
this.type = type;
this.name = name;
this.language = language;
}
internal Win32Resource (Win32ResourceType type, int name, int language) {
this.type = new NameOrId ((int)type);
this.name = new NameOrId (name);
this.language = language;
}
public Win32ResourceType ResourceType {
get {
if (type.IsName)
return (Win32ResourceType)(-1);
else
return (Win32ResourceType)type.Id;
}
}
public NameOrId Name {
get {
return name;
}
}
public NameOrId Type {
get {
return type;
}
}
public int Language {
get {
return language;
}
}
public abstract void WriteTo (Stream s);
public override string ToString () {
return "Win32Resource (Kind=" + ResourceType + ", Name=" + name + ")";
}
}
//
// This class represents a Win32 resource in encoded format
//
internal class Win32EncodedResource : Win32Resource {
byte[] data;
internal Win32EncodedResource (NameOrId type, NameOrId name, int language, byte[] data) : base (type, name, language) {
this.data = data;
}
public byte[] Data {
get {
return data;
}
}
public override void WriteTo (Stream s) {
s.Write (data, 0, data.Length);
}
}
//
// This class represents a Win32 ICON resource
//
internal class Win32IconResource : Win32Resource {
ICONDIRENTRY icon;
public Win32IconResource (int id, int language, ICONDIRENTRY icon) : base (Win32ResourceType.RT_ICON, id, language) {
this.icon = icon;
}
public ICONDIRENTRY Icon {
get {
return icon;
}
}
public override void WriteTo (Stream s) {
s.Write (icon.image, 0, icon.image.Length);
}
}
internal class Win32GroupIconResource : Win32Resource {
Win32IconResource[] icons;
public Win32GroupIconResource (int id, int language, Win32IconResource[] icons) : base (Win32ResourceType.RT_GROUP_ICON, id, language) {
this.icons = icons;
}
public override void WriteTo (Stream s) {
using (BinaryWriter w = new BinaryWriter (s)) {
w.Write ((short)0);
w.Write ((short)1);
w.Write ((short)icons.Length);
for (int i = 0; i < icons.Length; ++i) {
Win32IconResource icon = icons [i];
ICONDIRENTRY entry = icon.Icon;
w.Write (entry.bWidth);
w.Write (entry.bHeight);
w.Write (entry.bColorCount);
w.Write ((byte)0);
w.Write (entry.wPlanes);
w.Write (entry.wBitCount);
w.Write ((int)entry.image.Length);
w.Write ((short)icon.Name.Id);
}
}
}
}
//
// This class represents a Win32 VERSION resource
//
internal class Win32VersionResource : Win32Resource {
public string[] WellKnownProperties = {
"Comments",
"CompanyName",
"FileDescription",
"FileVersion",
"InternalName",
"LegalCopyright",
"LegalTrademarks",
"OriginalFilename",
"ProductName",
"ProductVersion"
};
long signature;
int struct_version;
long file_version;
long product_version;
int file_flags_mask;
int file_flags;
int file_os;
int file_type;
int file_subtype;
long file_date;
int file_lang;
int file_codepage;
Hashtable properties;
public Win32VersionResource (int id, int language) : base (Win32ResourceType.RT_VERSION, id, language) {
// Initialize non-public members to the usual values used in
// resources
signature = 0xfeef04bd;
struct_version = 1 << 16; /* 1.0 */
file_flags_mask = 63;
file_flags = 0;
file_os = 4; /* VOS_WIN32 */
file_type = 2;
file_subtype = 0;
file_date = 0;
file_lang = 0x7f;
file_codepage = 1200;
properties = new Hashtable ();
// Well known properties
foreach (string s in WellKnownProperties)
// The value of properties can't be empty
properties [s] = " ";
}
public string Version {
get {
return
"" + (file_version >> 48) +
"." + ((file_version >> 32) & 0xffff) +
"." + ((file_version >> 16) & 0xffff) +
"." + ((file_version >> 0) & 0xffff);
}
set {
long[] ver = new long [4] { 0, 0, 0, 0 };
if (value != null) {
string[] parts = value.Split ('.');
for (int i = 0; i < parts.Length; ++i) {
try {
if (i < ver.Length)
ver [i] = Int32.Parse (parts [i]);
}
catch (FormatException) {
}
}
}
file_version = (ver [0] << 48) | (ver [1] << 32) | (ver [2] << 16) + ver [3];
}
}
public virtual string this [string key] {
set {
properties [key] = value;
}
}
// Accessors for well known properties
public virtual string Comments {
get {
return (string)properties ["Comments"];
}
set {
properties ["Comments"] = value == String.Empty ? " " : value;
}
}
public virtual string CompanyName {
get {
return (string)properties ["CompanyName"];
}
set {
properties ["CompanyName"] = value == String.Empty ? " " : value;
}
}
public virtual string LegalCopyright {
get {
return (string)properties ["LegalCopyright"];
}
set {
properties ["LegalCopyright"] = value == String.Empty ? " " : value;
}
}
public virtual string LegalTrademarks {
get {
return (string)properties ["LegalTrademarks"];
}
set {
properties ["LegalTrademarks"] = value == String.Empty ? " " : value;
}
}
public virtual string OriginalFilename {
get {
return (string)properties ["OriginalFilename"];
}
set {
properties ["OriginalFilename"] = value == String.Empty ? " " : value;
}
}
public virtual string ProductName {
get {
return (string)properties ["ProductName"];
}
set {
properties ["ProductName"] = value == String.Empty ? " " : value;
}
}
public virtual string ProductVersion {
get {
return (string)properties ["ProductVersion"];
}
set {
properties ["ProductVersion"] = value == String.Empty ? " " : value;
}
}
public virtual string InternalName {
get {
return (string)properties ["InternalName"];
}
set {
properties ["InternalName"] = value == String.Empty ? " " : value;
}
}
public virtual string FileDescription {
get {
return (string)properties ["FileDescription"];
}
set {
properties ["FileDescription"] = value == String.Empty ? " " : value;
}
}
public virtual int FileLanguage {
get {
return file_lang;
}
set {
file_lang = value;
}
}
public virtual string FileVersion {
get {
return (string)properties ["FileVersion"];
}
set {
properties ["FileVersion"] = value == String.Empty ? " " : value;
}
}
private void emit_padding (BinaryWriter w) {
Stream ms = w.BaseStream;
if ((ms.Position % 4) != 0)
w.Write ((short)0);
}
private void patch_length (BinaryWriter w, long len_pos) {
Stream ms = w.BaseStream;
long pos = ms.Position;
ms.Position = len_pos;
w.Write ((short)(pos - len_pos));
ms.Position = pos;
}
public override void WriteTo (Stream ms)
{
using (BinaryWriter w = new BinaryWriter (ms, Encoding.Unicode)) {
//
// See the documentation for the VS_VERSIONINFO structure and
// its children on MSDN
//
// VS_VERSIONINFO
w.Write ((short)0);
w.Write ((short)0x34);
w.Write ((short)0);
w.Write ("VS_VERSION_INFO".ToCharArray ());
w.Write ((short)0);
emit_padding (w);
// VS_FIXEDFILEINFO
w.Write ((uint)signature);
w.Write ((int)struct_version);
w.Write ((int)(file_version >> 32));
w.Write ((int)((file_version & 0xffffffff)));
w.Write ((int)(product_version >> 32));
w.Write ((int)(product_version & 0xffffffff));
w.Write ((int)file_flags_mask);
w.Write ((int)file_flags);
w.Write ((int)file_os);
w.Write ((int)file_type);
w.Write ((int)file_subtype);
w.Write ((int)(file_date >> 32));
w.Write ((int)(file_date & 0xffffffff));
emit_padding (w);
// VarFileInfo
long var_file_info_pos = ms.Position;
w.Write ((short)0);
w.Write ((short)0);
w.Write ((short)1);
w.Write ("VarFileInfo".ToCharArray ());
w.Write ((short)0);
if ((ms.Position % 4) != 0)
w.Write ((short)0);
// Var
long var_pos = ms.Position;
w.Write ((short)0);
w.Write ((short)4);
w.Write ((short)0);
w.Write ("Translation".ToCharArray ());
w.Write ((short)0);
if ((ms.Position % 4) != 0)
w.Write ((short)0);
w.Write ((short)file_lang);
w.Write ((short)file_codepage);
patch_length (w, var_pos);
patch_length (w, var_file_info_pos);
// StringFileInfo
long string_file_info_pos = ms.Position;
w.Write ((short)0);
w.Write ((short)0);
w.Write ((short)1);
w.Write ("StringFileInfo".ToCharArray ());
emit_padding (w);
// StringTable
long string_table_pos = ms.Position;
w.Write ((short)0);
w.Write ((short)0);
w.Write ((short)1);
w.Write (String.Format ("{0:x4}{1:x4}", file_lang, file_codepage).ToCharArray ());
emit_padding (w);
// Strings
foreach (string key in properties.Keys) {
string value = (string)properties [key];
long string_pos = ms.Position;
w.Write ((short)0);
w.Write ((short)(value.ToCharArray ().Length + 1));
w.Write ((short)1);
w.Write (key.ToCharArray ());
w.Write ((short)0);
emit_padding (w);
w.Write (value.ToCharArray ());
w.Write ((short)0);
emit_padding (w);
patch_length (w, string_pos);
}
patch_length (w, string_table_pos);
patch_length (w, string_file_info_pos);
patch_length (w, 0);
}
}
}
internal class Win32ResFileReader {
Stream res_file;
public Win32ResFileReader (Stream s) {
res_file = s;
}
int read_int16 () {
int b1 = res_file.ReadByte ();
int b2 = res_file.ReadByte ();
if ((b1 == -1) || (b2 == -1))
return -1;
else
return b1 | (b2 << 8);
}
int read_int32 () {
int w1 = read_int16 ();
int w2 = read_int16 ();
if ((w1 == -1) || (w2 == -1))
return -1;
return w1 | (w2 << 16);
}
private void read_padding () {
while ((res_file.Position % 4) != 0)
read_int16 ();
}
NameOrId read_ordinal () {
int i = read_int16 ();
if ((i & 0xffff) != 0) {
int j = read_int16 ();
return new NameOrId (j);
}
else {
byte[] chars = new byte [16];
int pos = 0;
while (true) {
int j = read_int16 ();
if (j == 0)
break;
if (pos == chars.Length) {
byte[] new_chars = new byte [chars.Length * 2];
Array.Copy (chars, new_chars, chars.Length);
chars = new_chars;
}
chars [pos] = (byte)(j >> 8);
chars [pos + 1] = (byte)(j & 0xff);
pos += 2;
}
return new NameOrId (new String (Encoding.Unicode.GetChars (chars, 0, pos)));
}
}
public ICollection ReadResources () {
ArrayList resources = new ArrayList ();
/*
* We can't use a BinaryReader since we have to keep track of the
* stream position for padding.
*/
while (true) {
read_padding ();
int data_size = read_int32 ();
if (data_size == -1)
/* EOF */
break;
//int header_size =
read_int32 ();
NameOrId type = read_ordinal ();
NameOrId name = read_ordinal ();
read_padding ();
//int data_version =
read_int32 ();
//int memory_flags =
read_int16 ();
int language_id = read_int16 ();
//int version =
read_int32 ();
//int characteristics =
read_int32 ();
if (data_size == 0)
/* Empty resource entry */
continue;
byte[] data = new byte [data_size];
res_file.Read (data, 0, data_size);
resources.Add (new Win32EncodedResource (type, name, language_id, data));
}
return resources;
}
}
//
// This class represents one icon image in an .ico file
//
internal class ICONDIRENTRY {
public byte bWidth;
public byte bHeight;
public byte bColorCount;
public byte bReserved;
public Int16 wPlanes;
public Int16 wBitCount;
public Int32 dwBytesInRes;
public Int32 dwImageOffset;
public byte[] image;
public override string ToString () {
return "ICONDIRENTRY (" + bWidth + "x" + bHeight + " " + wBitCount + " bpp)";
}
}
//
// This class represents a Reader for Win32 .ico files
//
internal class Win32IconFileReader {
Stream iconFile;
public Win32IconFileReader (Stream s) {
iconFile = s;
}
public ICONDIRENTRY[] ReadIcons () {
ICONDIRENTRY[] icons = null;
using (BinaryReader r = new BinaryReader (iconFile)) {
int idReserved = r.ReadInt16 ();
int idType = r.ReadInt16 ();
if ((idReserved != 0) || (idType != 1))
throw new Exception ("Invalid .ico file format");
long nitems = r.ReadInt16 ();
icons = new ICONDIRENTRY [nitems];
for (int i = 0; i < nitems; ++i) {
ICONDIRENTRY entry = new ICONDIRENTRY ();
entry.bWidth = r.ReadByte ();
entry.bHeight = r.ReadByte ();
entry.bColorCount = r.ReadByte ();
entry.bReserved = r.ReadByte ();
entry.wPlanes = r.ReadInt16 ();
entry.wBitCount = r.ReadInt16 ();
int dwBytesInRes = r.ReadInt32 ();
int dwImageOffset = r.ReadInt32 ();
/* Read image */
entry.image = new byte [dwBytesInRes];
long pos = iconFile.Position;
iconFile.Position = dwImageOffset;
iconFile.Read (entry.image, 0, dwBytesInRes);
iconFile.Position = pos;
/*
* The wPlanes and wBitCount members in the ICONDIRENTRY
* structure can be 0, so we set them from the BITMAPINFOHEADER
* structure that follows
*/
if (entry.wPlanes == 0)
entry.wPlanes = (short)(entry.image [12] | (entry.image [13] << 8));
if (entry.wBitCount == 0)
entry.wBitCount = (short)(entry.image [14] | (entry.image [15] << 8));
icons [i] = entry;
}
return icons;
}
}
}
}
|