Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / corlib / System.Resources / Win32Resources.cs
index bfe5ba879ec55394fe0192990003bed47433840f..ad58ce8823fc9092687e547878d7a6eaffed78f3 100644 (file)
@@ -34,6 +34,7 @@
 
 using System;
 using System.Collections;
+using System.Globalization;
 using System.IO;
 using System.Text;
 
@@ -235,10 +236,8 @@ internal class Win32VersionResource : Win32Resource {
        public string[] WellKnownProperties = {
                "Comments",
                "CompanyName",
-               "FileDescription",
                "FileVersion",
                "InternalName",
-               "LegalCopyright",
                "LegalTrademarks",
                "OriginalFilename",
                "ProductName",
@@ -261,7 +260,7 @@ internal class Win32VersionResource : Win32Resource {
 
        Hashtable properties;
 
-       public Win32VersionResource (int id, int language) : base (Win32ResourceType.RT_VERSION, id, language) {
+       public Win32VersionResource (int id, int language, bool compilercontext) : base (Win32ResourceType.RT_VERSION, id, language) {
                // Initialize non-public members to the usual values used in
                // resources
                signature = 0xfeef04bd;
@@ -273,18 +272,23 @@ internal class Win32VersionResource : Win32Resource {
                file_subtype = 0;
                file_date = 0;
 
-               file_lang = 0x7f;
+               file_lang = compilercontext ? 0x00 : 0x7f;
                file_codepage = 1200;
 
                properties = new Hashtable ();
 
+               string defaultvalue = compilercontext ? string.Empty : " ";
+
                // Well known properties
                foreach (string s in WellKnownProperties)
                        // The value of properties can't be empty
-                       properties [s] = " ";
+                       properties [s] = defaultvalue;
+
+               LegalCopyright = " ";
+               FileDescription = " ";
        }
 
-       public string FileVersion {
+       public string Version {
                get {
                        return 
                                "" + (file_version >> 48) + 
@@ -297,18 +301,18 @@ internal class Win32VersionResource : Win32Resource {
                        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 {
+
+                               try {
+                                       for (int i = 0; i < parts.Length; ++i) {
                                                if (i < ver.Length)
                                                        ver [i] = Int32.Parse (parts [i]);
                                        }
-                                       catch (FormatException) {
-                                       }
+                               } catch (FormatException) {
                                }
                        }
 
                        file_version = (ver [0] << 48) | (ver [1] << 32) | (ver [2] << 16) + ver [3];
+                       properties ["FileVersion"] = Version;
                }
        }
 
@@ -379,7 +383,23 @@ internal class Win32VersionResource : Win32Resource {
                        return (string)properties ["ProductVersion"];
                }
                set {
-                       properties ["ProductVersion"] = value == String.Empty ? " " : value;
+                       if (value == null || value.Length == 0)
+                               value = " ";
+
+                       long [] ver = new long [4] { 0, 0, 0, 0 };
+
+                       string [] parts = value.Split ('.');
+
+                       try {
+                               for (int i = 0; i < parts.Length; ++i) {
+                                       if (i < ver.Length)
+                                               ver [i] = Int32.Parse (parts [i]);
+                               }
+                       } catch (FormatException) {
+                       }
+
+                       properties ["ProductVersion"] = value;
+                       product_version = (ver [0] << 48) | (ver [1] << 32) | (ver [2] << 16) + ver [3];
                }
        }
 
@@ -402,11 +422,31 @@ internal class Win32VersionResource : Win32Resource {
        }
 
        public virtual int FileLanguage {
+               get { return file_lang; }
+               set { file_lang = value; }
+       }
+
+       public virtual string FileVersion {
                get {
-                       return file_lang;
+                       return (string)properties ["FileVersion"];
                }
                set {
-                       file_lang = value;
+                       if (value == null || value.Length == 0)
+                               value = " ";
+
+                       long[] ver = new long [4] { 0, 0, 0, 0 };
+                       string[] parts = value.Split ('.');
+
+                       try {
+                               for (int i = 0; i < parts.Length; ++i) {
+                                       if (i < ver.Length)
+                                               ver [i] = Int32.Parse (parts [i]);
+                               }
+                       } catch (FormatException) {
+                       }
+
+                       properties ["FileVersion"] = value;
+                       file_version = (ver [0] << 48) | (ver [1] << 32) | (ver [2] << 16) + ver [3];
                }
        }
 
@@ -548,26 +588,33 @@ internal class Win32ResFileReader {
 
        int read_int16 () {
                int b1 = res_file.ReadByte ();
-               int b2 = res_file.ReadByte ();
+               if (b1 == -1)
+                       return -1;
 
-               if ((b1 == -1) || (b2 == -1))
+               int b2 = res_file.ReadByte ();
+               if (b2 == -1)
                        return -1;
-               else
-                       return b1 | (b2 << 8);
+
+               return b1 | (b2 << 8);
        }
 
        int read_int32 () {
                int w1 = read_int16 ();
+               if (w1 == -1)
+                       return -1;
                int w2 = read_int16 ();
-
-               if ((w1 == -1) || (w2 == -1))
+               if (w2 == -1)
                        return -1;
+
                return w1 | (w2 << 16);
        }
 
-       private void read_padding () {
-               while ((res_file.Position % 4) != 0)
-                       read_int16 ();
+       private bool read_padding () {
+               while ((res_file.Position % 4) != 0){
+                       if (read_int16 () == -1)
+                               return false;
+               }
+               return true;
        }
 
        NameOrId read_ordinal () {
@@ -608,8 +655,9 @@ internal class Win32ResFileReader {
 
                while (true) {
 
-                       read_padding ();
-
+                       if (!read_padding ())
+                               break;
+                       
                        int data_size = read_int32 ();
 
                        if (data_size == -1)
@@ -621,8 +669,9 @@ internal class Win32ResFileReader {
                        NameOrId type = read_ordinal ();
                        NameOrId name = read_ordinal ();
 
-                       read_padding ();
-
+                       if (!read_padding ())
+                               break;
+                       
                        //int data_version = 
                        read_int32 ();
                        //int memory_flags =
@@ -638,7 +687,8 @@ internal class Win32ResFileReader {
                                continue;
 
                        byte[] data = new byte [data_size];
-                       res_file.Read (data, 0, data_size);
+                       if (res_file.Read (data, 0, data_size) != data_size)
+                               break;
 
                        resources.Add (new Win32EncodedResource (type, name, language_id, data));
                }
@@ -652,6 +702,7 @@ internal class Win32ResFileReader {
 //
 internal class ICONDIRENTRY {
 
+#pragma warning disable 649
        public byte bWidth;
        public byte bHeight;
        public byte bColorCount;
@@ -660,7 +711,7 @@ internal class ICONDIRENTRY {
        public Int16 wBitCount;
        public Int32 dwBytesInRes;
        public Int32 dwImageOffset;
-
+#pragma warning restore 649
        public byte[] image;
 
        public override string ToString () {