public void TestDeepCopyBytes() { string filename = Path.Combine(Environment.SystemDirectory, "atl.dll"); Assert.IsTrue(File.Exists(filename)); VersionResource existingVersionResource = new VersionResource(); existingVersionResource.Language = ResourceUtil.USENGLISHLANGID; Console.WriteLine("Loading {0}", filename); existingVersionResource.LoadFrom(filename); DumpResource.Dump(existingVersionResource); VersionResource versionResource = new VersionResource(); versionResource.FileVersion = existingVersionResource.FileVersion; versionResource.ProductVersion = existingVersionResource.ProductVersion; StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"]; VarFileInfo existingVersionResourceVarFileInfo = (VarFileInfo)existingVersionResource["VarFileInfo"]; // copy string resources, data only StringFileInfo stringFileInfo = new StringFileInfo(); versionResource["StringFileInfo"] = stringFileInfo; { Dictionary<string, StringTable>.Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator(); while (enumerator.MoveNext()) { StringTable stringTable = new StringTable(enumerator.Current.Key); stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable); Dictionary<string, StringTableEntry>.Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator(); while (resourceEnumerator.MoveNext()) { StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key); stringResource.Value = resourceEnumerator.Current.Value.Value; stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource); } } } // copy var resources, data only VarFileInfo varFileInfo = new VarFileInfo(); versionResource["VarFileInfo"] = varFileInfo; { Dictionary<string, VarTable>.Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator(); while (enumerator.MoveNext()) { VarTable varTable = new VarTable(enumerator.Current.Key); varFileInfo.Vars.Add(enumerator.Current.Key, varTable); Dictionary<UInt16, UInt16>.Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator(); while (translationEnumerator.MoveNext()) { varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value); } } } ByteUtils.CompareBytes(existingVersionResource.WriteAndGetBytes(), versionResource.WriteAndGetBytes()); }
/// <summary> /// Read a string table. /// </summary> /// <param name="lpRes">Pointer to the beginning of the string table.</param> /// <returns>Pointer to the end of the string table.</returns> internal override IntPtr Read(IntPtr lpRes) { _strings.Clear(); IntPtr pChild = base.Read(lpRes); while (pChild.ToInt64() < (lpRes.ToInt64() + _header.wLength)) { StringTableEntry res = new StringTableEntry(pChild); _strings.Add(res.Key, res); pChild = ResourceUtil.Align(pChild.ToInt64() + res.Header.wLength); } return(new IntPtr(lpRes.ToInt64() + _header.wLength)); }
/// <summary> /// Read a string table. /// </summary> /// <param name="lpRes">Pointer to the beginning of the string table.</param> /// <returns>Pointer to the end of the string table.</returns> internal override IntPtr Read(IntPtr lpRes) { _strings.Clear(); IntPtr pChild = base.Read(lpRes); while (pChild.ToInt64() < (lpRes.ToInt64() + _header.wLength)) { StringTableEntry res = new StringTableEntry(pChild); _strings.Add(res.Key, res); pChild = ResourceUtil.Align(pChild.ToInt64() + res.Header.wLength); } return new IntPtr(lpRes.ToInt64() + _header.wLength); }
/// <summary> /// Returns an entry within the string table. /// </summary> /// <param name="key">Key.</param> /// <returns>An entry within the string table.</returns> public string this[string key] { get { return(_strings[key].Value); } set { StringTableEntry sr = null; if (!_strings.TryGetValue(key, out sr)) { sr = new StringTableEntry(key); _strings.Add(key, sr); } sr.Value = value; } }
public void TestDoubleNullTerminator() { StringTableEntry sr = new StringTableEntry("dummy"); string guid = Guid.NewGuid().ToString(); sr.Value = guid; Assert.AreEqual(guid + '\0', sr.Value); Assert.AreEqual(guid, sr.StringValue); Assert.AreEqual(guid.Length + 1, sr.Header.wValueLength); sr.Value = guid + '\0'; Assert.AreEqual(guid + '\0', sr.Value); Assert.AreEqual(guid, sr.StringValue); Assert.AreEqual(guid.Length + 1, sr.Header.wValueLength); sr.Value = guid + "\0\0"; Assert.AreEqual(guid + "\0\0", sr.Value); Assert.AreEqual(guid + '\0', sr.StringValue); Assert.AreEqual(guid.Length + 2, sr.Header.wValueLength); sr.Value = '\0' + guid; Assert.AreEqual('\0' + guid + '\0', sr.Value); Assert.AreEqual('\0' + guid, sr.StringValue); Assert.AreEqual(guid.Length + 2, sr.Header.wValueLength); }
public void TestDeleteDeepCopyAndSaveVersionResource() { string filename = Path.Combine(Environment.SystemDirectory, "atl.dll"); Assert.IsTrue(File.Exists(filename)); string targetFilename = Path.Combine(Path.GetTempPath(), "atl.dll"); File.Copy(filename, targetFilename, true); Console.WriteLine(targetFilename); VersionResource existingVersionResource = new VersionResource(); existingVersionResource.Language = ResourceUtil.USENGLISHLANGID; existingVersionResource.LoadFrom(targetFilename); DumpResource.Dump(existingVersionResource); existingVersionResource.DeleteFrom(targetFilename); VersionResource versionResource = new VersionResource(); versionResource.FileVersion = existingVersionResource.FileVersion; versionResource.ProductVersion = existingVersionResource.ProductVersion; StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"]; VarFileInfo existingVersionResourceVarFileInfo = (VarFileInfo)existingVersionResource["VarFileInfo"]; // copy string resources, data only StringFileInfo stringFileInfo = new StringFileInfo(); versionResource["StringFileInfo"] = stringFileInfo; { Dictionary<string, StringTable>.Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator(); while (enumerator.MoveNext()) { StringTable stringTable = new StringTable(enumerator.Current.Key); stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable); Dictionary<string, StringTableEntry>.Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator(); while (resourceEnumerator.MoveNext()) { StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key); stringResource.Value = resourceEnumerator.Current.Value.Value; stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource); } } } // copy var resources, data only VarFileInfo varFileInfo = new VarFileInfo(); versionResource["VarFileInfo"] = varFileInfo; { Dictionary<string, VarTable>.Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator(); while (enumerator.MoveNext()) { VarTable varTable = new VarTable(enumerator.Current.Key); varFileInfo.Vars.Add(enumerator.Current.Key, varTable); Dictionary<UInt16, UInt16>.Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator(); while (translationEnumerator.MoveNext()) { varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value); } } } versionResource.SaveTo(targetFilename); Console.WriteLine("Reloading {0}", targetFilename); VersionResource newVersionResource = new VersionResource(); newVersionResource.LoadFrom(targetFilename); DumpResource.Dump(newVersionResource); Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion); Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion); StringFileInfo testedStringFileInfo = (StringFileInfo)newVersionResource["StringFileInfo"]; foreach (KeyValuePair<string, StringTableEntry> stringResource in testedStringFileInfo.Default.Strings) { Assert.AreEqual(stringResource.Value.Value, stringFileInfo[stringResource.Key]); } VarFileInfo newVarFileInfo = (VarFileInfo)newVersionResource["VarFileInfo"]; foreach (KeyValuePair<UInt16, UInt16> varResource in newVarFileInfo.Default.Languages) { Assert.AreEqual(varResource.Value, varFileInfo[varResource.Key]); } }
public void TestDeepCopyBytes(string binaryName, int language) { Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase); string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName); Assert.IsTrue(File.Exists(filename)); VersionResource existingVersionResource = new VersionResource(); existingVersionResource.Language = (ushort)language; Console.WriteLine("Loading {0}", filename); existingVersionResource.LoadFrom(filename); DumpResource.Dump(existingVersionResource); VersionResource versionResource = new VersionResource(); versionResource.FileVersion = existingVersionResource.FileVersion; versionResource.ProductVersion = existingVersionResource.ProductVersion; StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"]; VarFileInfo existingVersionResourceVarFileInfo = (VarFileInfo)existingVersionResource["VarFileInfo"]; // copy string resources, data only StringFileInfo stringFileInfo = new StringFileInfo(); { Dictionary<string, StringTable>.Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator(); while (enumerator.MoveNext()) { StringTable stringTable = new StringTable(enumerator.Current.Key); stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable); Dictionary<string, StringTableEntry>.Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator(); while (resourceEnumerator.MoveNext()) { StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key); stringResource.Value = resourceEnumerator.Current.Value.Value; stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource); } } } // copy var resources, data only VarFileInfo varFileInfo = new VarFileInfo(); { Dictionary<string, VarTable>.Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator(); while (enumerator.MoveNext()) { VarTable varTable = new VarTable(enumerator.Current.Key); varFileInfo.Vars.Add(enumerator.Current.Key, varTable); Dictionary<UInt16, UInt16>.Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator(); while (translationEnumerator.MoveNext()) { varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value); } } } bool firstResourceIsStringFileInfo = existingVersionResource[0] == existingVersionResourceStringFileInfo; if (firstResourceIsStringFileInfo) { versionResource["StringFileInfo"] = stringFileInfo; versionResource["VarFileInfo"] = varFileInfo; } else { versionResource["VarFileInfo"] = varFileInfo; versionResource["StringFileInfo"] = stringFileInfo; } ByteUtils.CompareBytes(existingVersionResource.WriteAndGetBytes(), versionResource.WriteAndGetBytes()); }
public void TestDeleteDeepCopyAndSaveVersionResource(string binaryName, int language) { Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase); string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName); Assert.IsTrue(File.Exists(filename)); string targetFilename = Path.Combine(Path.GetTempPath(), binaryName); File.Copy(filename, targetFilename, true); Console.WriteLine(targetFilename); VersionResource existingVersionResource = new VersionResource(); existingVersionResource.Language = (ushort)language; existingVersionResource.LoadFrom(targetFilename); DumpResource.Dump(existingVersionResource); existingVersionResource.DeleteFrom(targetFilename); VersionResource versionResource = new VersionResource(); versionResource.FileVersion = existingVersionResource.FileVersion; versionResource.ProductVersion = existingVersionResource.ProductVersion; StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"]; VarFileInfo existingVersionResourceVarFileInfo = (VarFileInfo)existingVersionResource["VarFileInfo"]; // copy string resources, data only StringFileInfo stringFileInfo = new StringFileInfo(); versionResource["StringFileInfo"] = stringFileInfo; { Dictionary<string, StringTable>.Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator(); while (enumerator.MoveNext()) { StringTable stringTable = new StringTable(enumerator.Current.Key); stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable); Dictionary<string, StringTableEntry>.Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator(); while (resourceEnumerator.MoveNext()) { StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key); stringResource.Value = resourceEnumerator.Current.Value.Value; stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource); } } } // copy var resources, data only VarFileInfo varFileInfo = new VarFileInfo(); versionResource["VarFileInfo"] = varFileInfo; { Dictionary<string, VarTable>.Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator(); while (enumerator.MoveNext()) { VarTable varTable = new VarTable(enumerator.Current.Key); varFileInfo.Vars.Add(enumerator.Current.Key, varTable); Dictionary<UInt16, UInt16>.Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator(); while (translationEnumerator.MoveNext()) { varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value); } } } versionResource.SaveTo(targetFilename); Console.WriteLine("Reloading {0}", targetFilename); VersionResource newVersionResource = new VersionResource(); newVersionResource.LoadFrom(targetFilename); DumpResource.Dump(newVersionResource); AssertOneVersionResource(targetFilename); Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion); Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion); StringFileInfo testedStringFileInfo = (StringFileInfo)newVersionResource["StringFileInfo"]; foreach (KeyValuePair<string, StringTableEntry> stringResource in testedStringFileInfo.Default.Strings) { Assert.AreEqual(stringResource.Value.Value, stringFileInfo[stringResource.Key]); } VarFileInfo newVarFileInfo = (VarFileInfo)newVersionResource["VarFileInfo"]; foreach (KeyValuePair<UInt16, UInt16> varResource in newVarFileInfo.Default.Languages) { Assert.AreEqual(varResource.Value, varFileInfo[varResource.Key]); } }
/// <summary> /// Returns an entry within the string table. /// </summary> /// <param name="key">Key.</param> /// <returns>An entry within the string table.</returns> public string this[string key] { get { return _strings[key].Value; } set { StringTableEntry sr = null; if (!_strings.TryGetValue(key, out sr)) { sr = new StringTableEntry(key); _strings.Add(key, sr); } sr.Value = value; } }