// Perf optimization - Don't use Reflection for most cases with // our .resources files. This makes our code run faster and we can avoid // creating a ResourceReader via Reflection. This would incur // a security check (since the link-time check on the constructor that // takes a String is turned into a full demand with a stack walk) // and causes partially trusted localized apps to fail. private bool CanUseDefaultResourceClasses(string readerTypeName, string resSetTypeName) { Debug.Assert(readerTypeName != null, "readerTypeName shouldn't be null; check caller"); Debug.Assert(resSetTypeName != null, "resSetTypeName shouldn't be null; check caller"); if (_mediator.UserResourceSet != null) { return(false); } // Ignore the actual version of the ResourceReader and // RuntimeResourceSet classes. Let those classes deal with // versioning themselves. AssemblyName mscorlib = new AssemblyName(ResourceManager.MscorlibName); if (readerTypeName != null) { if (!ResourceManager.CompareNames(readerTypeName, ResourceManager.ResReaderTypeName, mscorlib)) { return(false); } } if (resSetTypeName != null) { if (!ResourceManager.CompareNames(resSetTypeName, ResourceManager.ResSetTypeName, mscorlib)) { return(false); } } return(true); }
private bool CanUseDefaultResourceClasses(string readerTypeName, string resSetTypeName) { if (this._mediator.UserResourceSet != (Type)null) { return(false); } AssemblyName asmName2 = new AssemblyName(ResourceManager.MscorlibName); return((readerTypeName == null || ResourceManager.CompareNames(readerTypeName, ResourceManager.ResReaderTypeName, asmName2)) && (resSetTypeName == null || ResourceManager.CompareNames(resSetTypeName, ResourceManager.ResSetTypeName, asmName2))); }
private void InitSafeToDeserializeArray() { this._safeToDeserialize = new bool[this._typeTable.Length]; for (int index = 0; index < this._typeTable.Length; ++index) { long position = this._store.BaseStream.Position; string typeName; try { this._store.BaseStream.Position = (long)this._typeNamePositions[index]; typeName = this._store.ReadString(); } finally { this._store.BaseStream.Position = position; } RuntimeType runtimeType = (RuntimeType)Type.GetType(typeName, false); AssemblyName asmName2; string typeName2; if (runtimeType == (RuntimeType)null) { asmName2 = (AssemblyName)null; typeName2 = typeName; } else { if (runtimeType.BaseType == typeof(Enum)) { this._safeToDeserialize[index] = true; continue; } typeName2 = runtimeType.FullName; asmName2 = new AssemblyName(); RuntimeAssembly runtimeAssembly = (RuntimeAssembly)runtimeType.Assembly; asmName2.Init(runtimeAssembly.GetSimpleName(), runtimeAssembly.GetPublicKey(), (byte[])null, (Version)null, runtimeAssembly.GetLocale(), AssemblyHashAlgorithm.None, AssemblyVersionCompatibility.SameMachine, (string)null, AssemblyNameFlags.PublicKey, (StrongNameKeyPair)null); } foreach (string asmTypeName1 in ResourceReader.TypesSafeForDeserialization) { if (ResourceManager.CompareNames(asmTypeName1, typeName2, asmName2)) { this._safeToDeserialize[index] = true; } } } }
private void InitSafeToDeserializeArray() { this._safeToDeserialize = new bool[this._typeTable.Length]; for (int i = 0; i < this._typeTable.Length; i++) { string str; AssemblyName name; string fullName; long position = this._store.BaseStream.Position; try { this._store.BaseStream.Position = this._typeNamePositions[i]; str = this._store.ReadString(); } finally { this._store.BaseStream.Position = position; } RuntimeType type = (RuntimeType)Type.GetType(str, false); if (type == null) { name = null; fullName = str; } else { if (type.BaseType == typeof(Enum)) { this._safeToDeserialize[i] = true; continue; } fullName = type.FullName; name = new AssemblyName(); RuntimeAssembly assembly = (RuntimeAssembly)type.Assembly; name.Init(assembly.GetSimpleName(), assembly.GetPublicKey(), null, null, assembly.GetLocale(), AssemblyHashAlgorithm.None, AssemblyVersionCompatibility.SameMachine, null, AssemblyNameFlags.PublicKey, null); } foreach (string str3 in TypesSafeForDeserialization) { if (ResourceManager.CompareNames(str3, fullName, name)) { this._safeToDeserialize[i] = true; } } } }
private bool CanUseDefaultResourceClasses(string readerTypeName, string resSetTypeName) { if (this._mediator.UserResourceSet != null) { return(false); } AssemblyName name = new AssemblyName(ResourceManager.MscorlibName); if ((readerTypeName != null) && !ResourceManager.CompareNames(readerTypeName, ResourceManager.ResReaderTypeName, name)) { return(false); } if ((resSetTypeName != null) && !ResourceManager.CompareNames(resSetTypeName, ResourceManager.ResSetTypeName, name)) { return(false); } return(true); }
public override Type BindToType(string assemblyName, string typeName) { AssemblyName asmName2 = new AssemblyName(assemblyName); bool flag = false; foreach (string asmTypeName1 in ResourceReader.TypesSafeForDeserialization) { if (ResourceManager.CompareNames(asmTypeName1, typeName, asmName2)) { flag = true; break; } } if (this.ObjectReader.FastBindToType(assemblyName, typeName).IsEnum) { flag = true; } if (flag) { return((Type)null); } throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResType&SerBlobMismatch", (object)this._typeToDeserialize.FullName, (object)typeName)); }
// Reads in the header information for a .resources file. Verifies some // of the assumptions about this resource set, and builds the class table // for the default resource file format. private void ReadResources() { BCLDebug.Assert(_store != null, "ResourceReader is closed!"); BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File | StreamingContextStates.Persistence)); _objFormatter = bf; try { // Read ResourceManager header // Check for magic number int magicNum = _store.ReadInt32(); if (magicNum != ResourceManager.MagicNumber) { throw new ArgumentException(Environment.GetResourceString("Resources_StreamNotValid")); } // Assuming this is ResourceManager header V1 or greater, hopefully // after the version number there is a number of bytes to skip // to bypass the rest of the ResMgr header. int resMgrHeaderVersion = _store.ReadInt32(); if (resMgrHeaderVersion > 1) { int numBytesToSkip = _store.ReadInt32(); BCLDebug.Log("RESMGRFILEFORMAT", LogLevel.Status, "ReadResources: Unexpected ResMgr header version: {0} Skipping ahead {1} bytes.", resMgrHeaderVersion, numBytesToSkip); BCLDebug.Assert(numBytesToSkip >= 0, "numBytesToSkip in ResMgr header should be positive!"); _store.BaseStream.Seek(numBytesToSkip, SeekOrigin.Current); } else { BCLDebug.Log("RESMGRFILEFORMAT", "ReadResources: Parsing ResMgr header v1."); SkipInt32(); // We don't care about numBytesToSkip. // Read in type name for a suitable ResourceReader // Note ResourceWriter & InternalResGen use different Strings. String readerType = _store.ReadString(); AssemblyName mscorlib = new AssemblyName(ResourceManager.MscorlibName); if (!ResourceManager.CompareNames(readerType, ResourceManager.ResReaderTypeName, mscorlib)) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_WrongResourceReader_Type", readerType)); } // Skip over type name for a suitable ResourceSet SkipString(); } // Read RuntimeResourceSet header // Do file version check int version = _store.ReadInt32(); if (version != RuntimeResourceSet.Version && version != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_ResourceFileUnsupportedVersion", RuntimeResourceSet.Version, version)); } _version = version; #if RESOURCE_FILE_FORMAT_DEBUG // Look for ***DEBUG*** to see if this is a debuggable file. long oldPos = _store.BaseStream.Position; _debug = false; try { String debugString = _store.ReadString(); _debug = String.Equals("***DEBUG***", debugString); } catch (IOException) { } catch (OutOfMemoryException) { } if (_debug) { Console.WriteLine("ResourceReader is looking at a debuggable .resources file, version {0}", _version); } else { _store.BaseStream.Position = oldPos; } #endif _numResources = _store.ReadInt32(); BCLDebug.Log("RESMGRFILEFORMAT", "ReadResources: Expecting " + _numResources + " resources."); #if _DEBUG if (ResourceManager.DEBUG >= 4) { Console.WriteLine("ResourceReader::ReadResources - Reading in " + _numResources + " resources"); } #endif // Read type positions into type positions array. // But delay initialize the type table. int numTypes = _store.ReadInt32(); _typeTable = new Type[numTypes]; _typeNamePositions = new int[numTypes]; for (int i = 0; i < numTypes; i++) { _typeNamePositions[i] = (int)_store.BaseStream.Position; // Skip over the Strings in the file. Don't create types. SkipString(); } #if _DEBUG if (ResourceManager.DEBUG >= 5) { Console.WriteLine("ResourceReader::ReadResources - Reading in " + numTypes + " type table entries"); } #endif // Prepare to read in the array of name hashes // Note that the name hashes array is aligned to 8 bytes so // we can use pointers into it on 64 bit machines. (4 bytes // may be sufficient, but let's plan for the future) // Skip over alignment stuff. All public .resources files // should be aligned No need to verify the byte values. long pos = _store.BaseStream.Position; int alignBytes = ((int)pos) & 7; if (alignBytes != 0) { for (int i = 0; i < 8 - alignBytes; i++) { _store.ReadByte(); } } // Read in the array of name hashes #if RESOURCE_FILE_FORMAT_DEBUG // Skip over "HASHES->" if (_debug) { _store.BaseStream.Position += 8; } #endif if (_ums == null) { _nameHashes = new int[_numResources]; for (int i = 0; i < _numResources; i++) { _nameHashes[i] = _store.ReadInt32(); } } else { unsafe { _nameHashesPtr = (int *)_ums.PositionPointer; // Skip over the array of nameHashes. _ums.Seek(4 * _numResources, SeekOrigin.Current); // get the position pointer once more to check that the whole table is within the stream byte *junk = _ums.PositionPointer; } } // Read in the array of relative positions for all the names. #if RESOURCE_FILE_FORMAT_DEBUG // Skip over "POS---->" if (_debug) { _store.BaseStream.Position += 8; } #endif if (_ums == null) { _namePositions = new int[_numResources]; for (int i = 0; i < _numResources; i++) { _namePositions[i] = _store.ReadInt32(); } } else { unsafe { _namePositionsPtr = (int *)_ums.PositionPointer; // Skip over the array of namePositions. _ums.Seek(4 * _numResources, SeekOrigin.Current); // get the position pointer once more to check that the whole table is within the stream byte *junk = _ums.PositionPointer; } } // Read location of data section. _dataSectionOffset = _store.ReadInt32(); // Store current location as start of name section _nameSectionOffset = _store.BaseStream.Position; BCLDebug.Log("RESMGRFILEFORMAT", String.Format(CultureInfo.InvariantCulture, "ReadResources: _nameOffset = 0x{0:x} _dataOffset = 0x{1:x}", _nameSectionOffset, _dataSectionOffset)); } catch (EndOfStreamException eof) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted"), eof); } catch (IndexOutOfRangeException e) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted"), e); } }
private unsafe void _ReadResources() { if (this._store.ReadInt32() != ResourceManager.MagicNumber) { throw new ArgumentException(Environment.GetResourceString("Resources_StreamNotValid")); } int num1 = this._store.ReadInt32(); int num2 = this._store.ReadInt32(); if (num2 < 0 || num1 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } if (num1 > 1) { this._store.BaseStream.Seek((long)num2, SeekOrigin.Current); } else { string asmTypeName1 = this._store.ReadString(); AssemblyName asmName2 = new AssemblyName(ResourceManager.MscorlibName); if (!ResourceManager.CompareNames(asmTypeName1, ResourceManager.ResReaderTypeName, asmName2)) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_WrongResourceReader_Type", (object)asmTypeName1)); } this.SkipString(); } int num3 = this._store.ReadInt32(); switch (num3) { case 2: case 1: this._version = num3; this._numResources = this._store.ReadInt32(); if (this._numResources < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } int length = this._store.ReadInt32(); if (length < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._typeTable = new RuntimeType[length]; this._typeNamePositions = new int[length]; for (int index = 0; index < length; ++index) { this._typeNamePositions[index] = (int)this._store.BaseStream.Position; this.SkipString(); } int num4 = (int)this._store.BaseStream.Position & 7; if (num4 != 0) { for (int index = 0; index < 8 - num4; ++index) { int num5 = (int)this._store.ReadByte(); } } if (this._ums == null) { this._nameHashes = new int[this._numResources]; for (int index = 0; index < this._numResources; ++index) { this._nameHashes[index] = this._store.ReadInt32(); } } else { int num5 = 4 * this._numResources; if (num5 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._nameHashesPtr = (int *)this._ums.PositionPointer; this._ums.Seek((long)num5, SeekOrigin.Current); byte *positionPointer = this._ums.PositionPointer; } if (this._ums == null) { this._namePositions = new int[this._numResources]; for (int index = 0; index < this._numResources; ++index) { int num5 = this._store.ReadInt32(); if (num5 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._namePositions[index] = num5; } } else { int num5 = 4 * this._numResources; if (num5 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._namePositionsPtr = (int *)this._ums.PositionPointer; this._ums.Seek((long)num5, SeekOrigin.Current); byte *positionPointer = this._ums.PositionPointer; } this._dataSectionOffset = (long)this._store.ReadInt32(); if (this._dataSectionOffset < 0L) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._nameSectionOffset = this._store.BaseStream.Position; if (this._dataSectionOffset >= this._nameSectionOffset) { break; } throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); default: throw new ArgumentException(Environment.GetResourceString("Arg_ResourceFileUnsupportedVersion", (object)2, (object)num3)); } }
private unsafe void _ReadResources() { if (this._store.ReadInt32() != ResourceManager.MagicNumber) { throw new ArgumentException(Environment.GetResourceString("Resources_StreamNotValid")); } int num2 = this._store.ReadInt32(); int num3 = this._store.ReadInt32(); if ((num3 < 0) || (num2 < 0)) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } if (num2 > 1) { this._store.BaseStream.Seek((long)num3, SeekOrigin.Current); } else { string str = this._store.ReadString(); AssemblyName name = new AssemblyName(ResourceManager.MscorlibName); if (!ResourceManager.CompareNames(str, ResourceManager.ResReaderTypeName, name)) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_WrongResourceReader_Type", new object[] { str })); } this.SkipString(); } int num4 = this._store.ReadInt32(); if ((num4 != 2) && (num4 != 1)) { throw new ArgumentException(Environment.GetResourceString("Arg_ResourceFileUnsupportedVersion", new object[] { 2, num4 })); } this._version = num4; this._numResources = this._store.ReadInt32(); if (this._numResources < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } int num5 = this._store.ReadInt32(); if (num5 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._typeTable = new RuntimeType[num5]; this._typeNamePositions = new int[num5]; for (int i = 0; i < num5; i++) { this._typeNamePositions[i] = (int)this._store.BaseStream.Position; this.SkipString(); } int num8 = ((int)this._store.BaseStream.Position) & 7; if (num8 != 0) { for (int j = 0; j < (8 - num8); j++) { this._store.ReadByte(); } } if (this._ums == null) { this._nameHashes = new int[this._numResources]; for (int k = 0; k < this._numResources; k++) { this._nameHashes[k] = this._store.ReadInt32(); } } else { int num11 = 4 * this._numResources; if (num11 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._nameHashesPtr = (int *)this._ums.PositionPointer; this._ums.Seek((long)num11, SeekOrigin.Current); byte *positionPointer = this._ums.PositionPointer; } if (this._ums == null) { this._namePositions = new int[this._numResources]; for (int m = 0; m < this._numResources; m++) { int num13 = this._store.ReadInt32(); if (num13 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._namePositions[m] = num13; } } else { int num14 = 4 * this._numResources; if (num14 < 0) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._namePositionsPtr = (int *)this._ums.PositionPointer; this._ums.Seek((long)num14, SeekOrigin.Current); byte *numPtr2 = this._ums.PositionPointer; } this._dataSectionOffset = this._store.ReadInt32(); if (this._dataSectionOffset < 0L) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } this._nameSectionOffset = this._store.BaseStream.Position; if (this._dataSectionOffset < this._nameSectionOffset) { throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ResourcesHeaderCorrupted")); } }