// // System.Resources.ResourceReader.cs // // Authors: // Duncan Mak // Nick Drochak // Dick Porter // Marek Safar // Atsushi Enomoto // Larry Ewing // // (C) 2001, 2002 Ximian Inc, http://www.ximian.com // Copyright (C) 2004-2005,2007-2008 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.Collections; using System.Resources; using System.IO; using System.Text; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Permissions; using System.Collections.Generic; namespace System.Resources { internal enum PredefinedResourceType { Null = 0, String = 1, Bool = 2, Char = 3, Byte = 4, SByte = 5, Int16 = 6, UInt16 = 7, Int32 = 8, UInt32 = 9, Int64 = 10, UInt64 = 11, Single = 12, Double = 13, Decimal = 14, DateTime = 15, TimeSpan = 16, ByteArray = 32, Stream = 33, FistCustom = 64 } [System.Runtime.InteropServices.ComVisible (true)] public sealed class ResourceReader : IResourceReader, IEnumerable, IDisposable { struct ResourceInfo { public readonly long ValuePosition; public readonly string ResourceName; public readonly int TypeIndex; public ResourceInfo (string resourceName, long valuePosition, int type_index) { ValuePosition = valuePosition; ResourceName = resourceName; TypeIndex = type_index; } } struct ResourceCacheItem { public readonly string ResourceName; public readonly object ResourceValue; public ResourceCacheItem (string name, object value) { ResourceName = name; ResourceValue = value; } } BinaryReader reader; object readerLock = new object (); IFormatter formatter; internal int resourceCount = 0; int typeCount = 0; string[] typeNames; int[] hashes; ResourceInfo[] infos; int dataSectionOffset; long nameSectionOffset; int resource_ver; ResourceCacheItem[] cache; object cache_lock = new object (); // Constructors [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)] public ResourceReader (Stream stream) { if (stream == null) throw new ArgumentNullException ("stream"); if (!stream.CanRead) throw new ArgumentException ("Stream was not readable."); reader = new BinaryReader(stream, Encoding.UTF8); formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File|StreamingContextStates.Persistence)); ReadHeaders(); } public ResourceReader (string fileName) { reader = new BinaryReader (new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)); formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File|StreamingContextStates.Persistence)); ReadHeaders(); } /* Read the ResourceManager header and the * ResourceReader header. */ private void ReadHeaders() { try { int manager_magic = reader.ReadInt32(); if(manager_magic != ResourceManager.MagicNumber) throw new ArgumentException(String.Format ("Stream is not a valid .resources file, magic=0x{0:x}", manager_magic)); int manager_ver = reader.ReadInt32(); int manager_len = reader.ReadInt32(); /* We know how long the header is, even if * the version number is too new */ if(manager_ver > ResourceManager.HeaderVersionNumber) { reader.BaseStream.Seek(manager_len, SeekOrigin.Current); } else { string reader_class=reader.ReadString(); if(!reader_class.StartsWith("System.Resources.ResourceReader")) { throw new NotSupportedException("This .resources file requires reader class " + reader_class); } string set_class=reader.ReadString(); if(!set_class.StartsWith(typeof(ResourceSet).FullName) && !set_class.StartsWith("System.Resources.RuntimeResourceSet")) { throw new NotSupportedException("This .resources file requires set class " + set_class); } } /* Now read the ResourceReader header */ resource_ver = reader.ReadInt32(); if(resource_ver != 1 && resource_ver != 2) { throw new NotSupportedException("This .resources file requires unsupported set class version: " + resource_ver.ToString()); } resourceCount = reader.ReadInt32(); typeCount = reader.ReadInt32(); typeNames=new string[typeCount]; for(int i=0; i 0 ) { int x = reader.Read (bytes, 0, (int)Math.Min (bytes.Length, slen)); if (x == 0) throw new FormatException ("The resource data is corrupt. Resource stream ended"); ms.Write (bytes, 0, x); slen -= x; } ms.Seek (0, SeekOrigin.Begin); return ms; } } } } public void Close () { Dispose(true); } public IDictionaryEnumerator GetEnumerator () { if (reader == null){ throw new InvalidOperationException("ResourceReader is closed."); } else { return new ResourceEnumerator (this); } } IEnumerator IEnumerable.GetEnumerator () { return ((IResourceReader) this).GetEnumerator(); } public void GetResourceData (string resourceName, out string resourceType, out byte [] resourceData) { if (resourceName == null) throw new ArgumentNullException ("resourceName"); ResourceEnumerator en = new ResourceEnumerator (this); while (en.MoveNext ()) if ((string) en.Key == resourceName) { GetResourceDataAt (en.Index, out resourceType, out resourceData); return; } throw new ArgumentException (String.Format ("Specified resource not found: {0}", resourceName)); } private void GetResourceDataAt (int index, out string resourceType, out byte [] data) { ResourceInfo ri = infos [index]; int type_index = ri.TypeIndex; if (type_index == -1) throw new FormatException ("The resource data is corrupt"); lock (readerLock) { reader.BaseStream.Seek (ri.ValuePosition, SeekOrigin.Begin); long pos2 = reader.BaseStream.Position; // Simply read data, and seek back to the original position if (resource_ver == 2) { if (type_index >= (int) PredefinedResourceType.FistCustom) { int typenameidx = type_index - (int)PredefinedResourceType.FistCustom; if (typenameidx >= typeNames.Length) throw new FormatException ("The resource data is corrupt. Invalid index to types"); resourceType = typeNames[typenameidx]; } else resourceType = "ResourceTypeCode." + (PredefinedResourceType) type_index; ReadValueVer2 (type_index); } else { // resource ver 1 == untyped resourceType = "ResourceTypeCode.Null"; ReadValueVer1 (Type.GetType (typeNames [type_index], true)); } // FIXME: the data size is wrong. int datalen = (int) (reader.BaseStream.Position - pos2); reader.BaseStream.Seek (-datalen, SeekOrigin.Current); data = new byte [datalen]; reader.BaseStream.Read (data, 0, datalen); } } void IDisposable.Dispose () { Dispose(true); } private void Dispose (bool disposing) { if(disposing) { if(reader!=null) { reader.Close(); } } reader=null; hashes=null; infos=null; typeNames=null; cache = null; } internal sealed class ResourceEnumerator : IDictionaryEnumerator { private ResourceReader reader; private int index = -1; private bool finished; internal ResourceEnumerator(ResourceReader readerToEnumerate) { reader = readerToEnumerate; FillCache (); } public int Index { get { return index; } } public DictionaryEntry Entry { get { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); if (index < 0) throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); return new DictionaryEntry(Key, Value); } } public object Key { get { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); if (index < 0) throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); return reader.cache [index].ResourceName; } } public object Value { get { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); if (index < 0) throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); return reader.cache [index].ResourceValue; } } public UnmanagedMemoryStream ValueAsStream { get { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); if (index < 0) throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); return(reader.ResourceValueAsStream((string) Key, index)); } } public object Current { get { /* Entry does the checking, no * need to repeat it here */ return Entry; } } public bool MoveNext () { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); if (finished) { return false; } if (++index < reader.resourceCount){ return true; } finished=true; return false; } public void Reset () { if (reader.reader == null) throw new InvalidOperationException("ResourceReader is closed."); index = -1; finished = false; } void FillCache () { if (reader.cache != null) return; lock (reader.cache_lock) { if (reader.cache != null) return; ResourceCacheItem[] resources = new ResourceCacheItem [reader.resourceCount]; reader.LoadResourceValues (resources); reader.cache = resources; } } } // internal class ResourceEnumerator } // public sealed class ResourceReader } // namespace System.Resources