Don't like ads? PRO users don't see any ads ;-)
Guest

2much code

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 25.75 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Diagnostics;
  6. using Ionic.Zip;
  7. using Microsoft.Win32;
  8. using System.Security.Cryptography;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         string MW3Directory = String.Empty;
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             OpenFileDialog openImage = new OpenFileDialog();
  24.             openImage.Filter = "Compatible formats|*.tga;*.bmp;*.gif;*.ppm;*.jpg;*.tif;*.cel;*.dds;*.png;*.psd;*.rgb;*.bw;*.rgba";
  25.             DialogResult result = openImage.ShowDialog();
  26.             if (result == DialogResult.OK) { filePath.Text = openImage.FileName; }
  27.         }
  28.  
  29.         private void button2_Click(object sender, EventArgs e)
  30.         {
  31.             MW3Directory = MW3Path();
  32.             string iwiFile = textBox1.Text;
  33.             string settings = String.Empty;
  34.             string ddsFormat = String.Empty;
  35.             string[] iwiDB = File.ReadAllLines("iwidb.idf");
  36.             string iwdFile = guessIWD(iwiDB, iwiFile);
  37.             using (ZipFile zip1 = ZipFile.Read(MW3Directory + iwdFile))
  38.             {
  39.                 ZipEntry iwiInZip = zip1[@"images\" + iwiFile];
  40.                 iwiInZip.Extract("temp", ExtractExistingFileAction.OverwriteSilently);
  41.             }
  42.             Crc32 crc32 = new Crc32();
  43.             String hash = String.Empty;
  44.             using (FileStream fs = File.Open(@"temp\images\" + iwiFile, FileMode.Open))
  45.                 foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
  46.             byte[] iwiFileBytes = File.ReadAllBytes(@"temp\images\" + iwiFile);
  47.             byte[] unknown1 = { iwiFileBytes[4], iwiFileBytes[5], iwiFileBytes[6], iwiFileBytes[7] };
  48.             byte unknown2 = iwiFileBytes[9];
  49.             byte[] unknown3 = { iwiFileBytes[14], iwiFileBytes[15] };
  50.             if (checkBox2.Checked)
  51.             {
  52.                 File.Copy(@"temp\images\" + iwiFile, iwiFile.Replace(".iwi", ".bak"), true);
  53.             }
  54.             if (Directory.Exists("temp")) { Directory.Delete("temp", true); }
  55.             byte ddsType = 0x00;
  56.             if (radioButton1.Checked) { ddsType = iwiFileBytes[8]; }
  57.             else
  58.             {
  59.                 if (comboBox1.SelectedIndex == 0) { ddsType = 0x01; }
  60.                 else if (comboBox1.SelectedIndex == 1) { ddsType = 0x02; }
  61.                 else if (comboBox1.SelectedIndex == 2) { ddsType = 0x03; }
  62.                 else if (comboBox1.SelectedIndex == 3) { ddsType = 0x04; }
  63.                 else if (comboBox1.SelectedIndex == 4) { ddsType = 0x0B; }
  64.                 else if (comboBox1.SelectedIndex == 5) { ddsType = 0x0C; }
  65.                 else if (comboBox1.SelectedIndex == 6) { ddsType = 0x0D; }
  66.             }
  67.             if (radioButton1.Checked)
  68.             {
  69.                 if (iwiFileBytes[8] == 0x01) { ddsFormat = "-u8888"; }
  70.                 else if (iwiFileBytes[8] == 0x02) { ddsFormat = "-u888"; }
  71.                 else if (iwiFileBytes[8] == 0x03) { ddsFormat = "-A8L8"; }
  72.                 else if (iwiFileBytes[8] == 0x04) { ddsFormat = "-a8"; }
  73.                 else if (iwiFileBytes[8] == 0x0B) { ddsFormat = "-dxt1c"; }
  74.                 else if (iwiFileBytes[8] == 0x0C) { ddsFormat = "-dxt3"; }
  75.                 else if (iwiFileBytes[8] == 0x0D) { ddsFormat = "-dxt5"; }
  76.             }
  77.             else
  78.             {
  79.                 if (comboBox1.SelectedIndex == 0) { ddsFormat = "-u8888"; }
  80.                 else if (comboBox1.SelectedIndex == 1) { ddsFormat = "-u888"; }
  81.                 else if (comboBox1.SelectedIndex == 2) { ddsFormat = "-A8L8"; }
  82.                 else if (comboBox1.SelectedIndex == 3) { ddsFormat = "-a8"; }
  83.                 else if (comboBox1.SelectedIndex == 4) { ddsFormat = "-dxt1c"; }
  84.                 else if (comboBox1.SelectedIndex == 5) { ddsFormat = "-dxt3"; }
  85.                 else if (comboBox1.SelectedIndex == 6) { ddsFormat = "-dxt5"; }
  86.             }
  87.             if (File.Exists("batchddsprocess_iwi.bat")) { File.Delete("batchddsprocess_iwi.bat"); }
  88.             settings = "nvdxt.exe -file \"" + filePath.Text + "\" " + ddsFormat + " -nmips 1 -outdir \"" + Directory.GetCurrentDirectory() + "\"";
  89.             if (checkBox1.Checked) { settings += " -prescale " + resolution1.Text + " " + resolution2.Text; }
  90.             StreamWriter batch = new StreamWriter("batchddsprocess_iwi.bat");
  91.             batch.WriteLine("@echo off");
  92.             batch.WriteLine(settings);
  93.             batch.Close();
  94.             ProcessStartInfo batchInfo = new ProcessStartInfo("batchddsprocess_iwi.bat");
  95.             Process batchProcess = new Process();
  96.             batchProcess.StartInfo = batchInfo;
  97.             batchProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  98.             batchProcess.Start();
  99.             batchProcess.WaitForExit();
  100.             if (File.Exists("batchddsprocess_iwi.bat")) { File.Delete("batchddsprocess_iwi.bat"); }
  101.             byte[] oldArray = File.ReadAllBytes(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds");
  102.             byte[] newArray = new byte[oldArray.Length - 96];
  103.             Buffer.BlockCopy(oldArray, 96, newArray, 0, newArray.Length);
  104.             string res1string = String.Empty;
  105.             string res2string = String.Empty;
  106.             if (!checkBox1.Checked)
  107.             {
  108.                 byte[] res2hex = { oldArray[12], oldArray[13] };
  109.                 byte[] res1hex = { oldArray[16], oldArray[17] };
  110.                 Array.Reverse(res1hex);
  111.                 Array.Reverse(res2hex);
  112.                 res1string = BitConverter.ToString(res1hex).Replace("-", "");
  113.                 res2string = BitConverter.ToString(res2hex).Replace("-", "");
  114.                 res1string = hex2Decimal(res1string);
  115.                 res2string = hex2Decimal(res2string);
  116.             }
  117.             else
  118.             {
  119.                 res1string = resolution1.Text;
  120.                 res2string = resolution2.Text;
  121.             }
  122.             byte[] iwiCookie = { 0x49, 0x57, 0x69, 0x08 };
  123.             byte[] res1IWI = { 0x00, 0x00 };
  124.             byte[] res2IWI = { 0x00, 0x00 };
  125.             Array.Copy(BitConverter.GetBytes(Int16.Parse(res1string)), res1IWI, 2);
  126.             Array.Copy(BitConverter.GetBytes(Int16.Parse(res2string)), res2IWI, 2);
  127.             byte[] secondLine = { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };
  128.             int fileSize = newArray.Length;
  129.             int count = 0;
  130.             for (int i = 0; i < 4; i++)
  131.             {
  132.                 Array.Copy(BitConverter.GetBytes(fileSize), 0, secondLine, count, 4);
  133.                 count += 4;
  134.             }
  135.             byte[] iwiHeader = { iwiCookie[0], iwiCookie[1], iwiCookie[2], iwiCookie[3], unknown1[0], unknown1[1], unknown1[2], unknown1[3], ddsType, unknown2, res1IWI[0], res1IWI[1], res2IWI[0], res2IWI[1], unknown3[0], unknown3[1], secondLine[0], secondLine[1], secondLine[2], secondLine[3], secondLine[4], secondLine[5], secondLine[6], secondLine[7], secondLine[8], secondLine[9], secondLine[10], secondLine[11], secondLine[12], secondLine[13], secondLine[14], secondLine[15] };
  136.             iwiHeader.CopyTo(newArray, 0);
  137.             File.WriteAllBytes(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi", newArray);
  138.             if (File.Exists(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds")) { File.Delete(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds"); }
  139.             File.Copy(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi", iwiFile, true);
  140.             if (File.Exists(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi")) { File.Delete(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi"); }
  141.             Crc32Fix.FixChecksum(newArray, newArray.Length, newArray.Length - 4, Convert.ToUInt32(hash, 16));
  142.             File.WriteAllBytes(iwiFile, newArray);
  143.             using (var zip = ZipFile.Read(MW3Directory + iwdFile))
  144.             {
  145.                 zip.RemoveEntry(@"images\" + iwiFile);
  146.                 zip.AddFile(iwiFile, @"images");
  147.                 zip.Save();
  148.             }
  149.             if (File.Exists(iwiFile)) { File.Delete(iwiFile); }
  150.         }
  151.  
  152.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  153.         {
  154.             if (resolution1.Enabled) { resolution1.Enabled = false; }
  155.             else { resolution1.Enabled = true; }
  156.             if (resolution2.Enabled) { resolution2.Enabled = false; }
  157.             else { resolution2.Enabled = true; }
  158.             if (label2.Enabled) { label2.Enabled = false; }
  159.             else { label2.Enabled = true; }
  160.         }
  161.  
  162.         public static string hex2Decimal(string hexvalue)
  163.         {
  164.             string binaryval = String.Empty;
  165.             binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 10);
  166.             return binaryval;
  167.         }
  168.  
  169.         private void radioButton1_CheckedChanged(object sender, EventArgs e)
  170.         {
  171.             if (checkBox2.Enabled) { checkBox2.Enabled = false; }
  172.             else { checkBox2.Enabled = true; }
  173.             if (comboBox1.Enabled) { comboBox1.Enabled = false; }
  174.             else { comboBox1.Enabled = true; }
  175.             if (label3.Enabled) { label3.Enabled = false; }
  176.             else { label3.Enabled = true; }
  177.         }
  178.  
  179.         private void checkBox2_CheckedChanged(object sender, EventArgs e)
  180.         {
  181.  
  182.         }
  183.  
  184.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  185.         {
  186.             Process.Start("http://www.itsmods.com/forum/index.php");
  187.         }
  188.  
  189.         private void textBox1_TextChanged(object sender, EventArgs e)
  190.         {
  191.  
  192.         }
  193.  
  194.         private void Form1_Load(object sender, EventArgs e)
  195.         {
  196.             try
  197.             {
  198.                 comboBox1.SelectedIndex = 0;
  199.                 if (File.Exists("iwidb.idf"))
  200.                 {
  201.                     string[] iwiDataBase = File.ReadAllLines("iwidb.idf");
  202.                     int count = 0;
  203.                     AutoCompleteStringCollection data = new AutoCompleteStringCollection();
  204.                     while (count != iwiDataBase.Length)
  205.                     {
  206.                         if (iwiDataBase[count].Contains(".iwi"))
  207.                         {
  208.                             data.Add(iwiDataBase[count]);
  209.                         }
  210.                         count++;
  211.                     }
  212.                     textBox1.AutoCompleteCustomSource = data;
  213.                 }
  214.                 else
  215.                 {
  216.                     DialogResult result = MessageBox.Show("Cant find the IWI database! Do you want to create it?", "Error", MessageBoxButtons.YesNo);
  217.                     if (result == DialogResult.Yes)
  218.                     {
  219.                         createIWIDB();
  220.                         string[] iwiDataBase = File.ReadAllLines("iwidb.idf");
  221.                         int count = 0;
  222.                         AutoCompleteStringCollection data = new AutoCompleteStringCollection();
  223.                         while (count != iwiDataBase.Length)
  224.                         {
  225.                             if (iwiDataBase[count].Contains(".iwi"))
  226.                             {
  227.                                 data.Add(iwiDataBase[count]);
  228.                             }
  229.                             count++;
  230.                         }
  231.                         textBox1.AutoCompleteCustomSource = data;
  232.                     }
  233.                 }
  234.             }
  235.             catch (Exception x)
  236.             {
  237.                 MessageBox.Show(x.ToString());
  238.             }
  239.             Activate();
  240.         }
  241.  
  242.         void createIWIDB()
  243.         {
  244.             try
  245.             {
  246.                 MW3Directory = MW3Path();
  247.                 if (MW3Directory != null)
  248.                 {
  249.                     if (Directory.Exists(MW3Directory))
  250.                     {
  251.                         string[] filePaths = Directory.GetFiles(MW3Directory, "*.iwd");
  252.                         using (StreamWriter sw = new StreamWriter("iwidb.idf"))
  253.                         {
  254.                             sw.WriteLine("[iwi database file]");
  255.                             sw.WriteLine("[do not modify unless you want corrupt shit]");
  256.                             sw.WriteLine();
  257.                             sw.WriteLine();
  258.                             for (int i = 0; i != filePaths.Length; i++)
  259.                             {
  260.                                 sw.WriteLine("[[" + Path.GetFileName(filePaths[i]) + "]]");
  261.                                 sw.WriteLine("");
  262.                                 using (var zip = ZipFile.Read(filePaths[i]))
  263.                                 {
  264.                                     foreach (ZipEntry zipEntry in zip.Entries)
  265.                                     {
  266.                                         if (zipEntry.FileName != "fileSysCheck.cfg")
  267.                                         {
  268.                                             string file = zipEntry.FileName;
  269.                                             if (file.EndsWith(".iwi")) { sw.WriteLine(file.Replace("images/", "")); }
  270.                                         }
  271.                                     }
  272.                                 }
  273.                             }
  274.                         }
  275.                     }
  276.                     else { MessageBox.Show("You have selected the wrong folder. IWI database not created", "Error", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); }
  277.                 }
  278.                 else { MessageBox.Show("You didnt select any folder. IWI database not created", "Error", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); }
  279.             }
  280.             catch (Exception x)
  281.             {
  282.                 MessageBox.Show(x.ToString());
  283.             }
  284.         }
  285.  
  286.         private string MW3Path()
  287.         {
  288.             try
  289.             {
  290.                 bool cfgCorrupt = false;
  291.                 RegistryKey regKey = Registry.CurrentUser;
  292.                 regKey = regKey.OpenSubKey(@"Software\Valve\Steam");
  293.                 bool canFindSteamPath = false;
  294.                 string steamInstallPath;
  295.                 string MW3Directory = String.Empty;
  296.                 if (regKey != null)
  297.                 {
  298.                     steamInstallPath = regKey.GetValue("SteamPath").ToString();
  299.                     MW3Directory = steamInstallPath + "/steamapps/common/call of duty modern warfare 3";
  300.                     if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { canFindSteamPath = true; }
  301.                     else { canFindSteamPath = false; }
  302.                 }
  303.                 else { canFindSteamPath = false; }
  304.                 if (!canFindSteamPath)
  305.                 {
  306.                     if (File.Exists("settings.cfg"))
  307.                     {
  308.                         string cfgPath = String.Empty;
  309.                         try { cfgPath = GetStringFromTo(File.ReadAllLines("settings.cfg"), "MW3 Directory = ", " ;"); }
  310.                         catch (Exception)
  311.                         {
  312.                             cfgCorrupt = true;
  313.                             if (File.Exists("settings.cfg")) { File.Delete("settings.cfg"); }
  314.                             MessageBox.Show("File \"settings.cfg\" corrupt, please select the MW3 folder");
  315.                             FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
  316.                             DialogResult result = MW3Folder.ShowDialog();
  317.                             if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
  318.                             else { MW3Directory = null; }
  319.                             if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
  320.                         }
  321.                         if (File.Exists("settings.cfg")) { if (File.Exists(cfgPath + "/main/iw_00.iwd")) { MW3Directory = cfgPath; } }
  322.                         if (File.Exists("settings.cfg"))
  323.                         {
  324.                             if (!File.Exists(cfgPath + "/main/iw_00.iwd"))
  325.                             {
  326.                                 if (!cfgCorrupt)
  327.                                 {
  328.                                     if (File.Exists("settings.cfg")) { File.Delete("settings.cfg"); }
  329.                                     MessageBox.Show("The folder in \"settings.cfg\" is wrong, please select the MW3 folder");
  330.                                     FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
  331.                                     DialogResult result = MW3Folder.ShowDialog();
  332.                                     if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
  333.                                     else { MW3Directory = null; }
  334.                                     if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
  335.                                 }
  336.                             }
  337.                         }
  338.                     }
  339.                     else
  340.                     {
  341.                         MessageBox.Show("Please select the MW3 folder");
  342.                         FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
  343.                         DialogResult result = MW3Folder.ShowDialog();
  344.                         if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
  345.                         else { MW3Directory = null; }
  346.                         if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
  347.                     }
  348.                 }
  349.                 if (MW3Directory != null) { MW3Directory += "/main/"; }
  350.                 return MW3Directory;
  351.             }
  352.             catch (Exception x)
  353.             {
  354.                 MessageBox.Show(x.ToString());
  355.                 return null;
  356.             }
  357.         }
  358.  
  359.         public static string GetStringFromTo(string[] source, string from, string to)
  360.         {
  361.             string result;
  362.             int line = 0;
  363.             bool lineReached = false;
  364.             while (line != source.Length - 1)
  365.             {
  366.                 if (lineReached == false)
  367.                 {
  368.                     if (source[line].Contains(from) && source[line].Contains(to)) { lineReached = true; }
  369.                     else { line++; }
  370.                 }
  371.             }
  372.             result = source[line];
  373.             result = result.Replace(source[line].Substring(source[line].IndexOf(to)), "");
  374.             result = result.Substring(result.IndexOf(from)).Replace(from, "");
  375.             return result;
  376.         }
  377.  
  378.         string guessIWD(string[] iwiDB, string iwiToFind)
  379.         {
  380.             string iwdFile;
  381.             int strNumber = findStringInArray(iwiDB, iwiToFind);
  382.             if (iwiDB[strNumber].EndsWith(".iwi"))
  383.             {
  384.                 while (!iwiDB[strNumber].StartsWith("[["))
  385.                 {
  386.                     strNumber--;
  387.                 }
  388.                 iwdFile = iwiDB[strNumber].Replace("[", "");
  389.                 iwdFile = iwdFile.Replace("]", "");
  390.                 return iwdFile;
  391.             }
  392.             else { return null; }
  393.         }
  394.  
  395.         int findStringInArray(string[] stringArray, string stringToFind)
  396.         {
  397.             int strNumber;
  398.             int strIndex = 0;
  399.             for (strNumber = 0; strNumber < stringArray.Length; strNumber++)
  400.             {
  401.                 strIndex = stringArray[strNumber].IndexOf(stringToFind);
  402.                 if (strIndex >= 0)
  403.                     break;
  404.             }
  405.             return strNumber;
  406.         }
  407.  
  408.         public class Crc32Fix
  409.         {
  410.             public const uint poly = 0xedb88320;
  411.             public const uint startxor = 0xffffffff;
  412.             public static uint[] table = null;
  413.             public static uint[] revtable = null;
  414.             public static byte[] FixChecksum(byte[] bytes, int length, int fixpos, uint wantcrc)
  415.             {
  416.                 try
  417.                 {
  418.                     if (fixpos + 4 > length) return null;
  419.  
  420.                     if (table == null)
  421.                     {
  422.                         table = new uint[256];
  423.                         revtable = new uint[256];
  424.  
  425.                         uint fwd, rev;
  426.                         for (int i = 0; i < table.Length; i++)
  427.                         {
  428.                             fwd = (uint)i;
  429.                             rev = (uint)(i) << (3 * 8);
  430.                             for (int j = 8; j > 0; j--)
  431.                             {
  432.                                 if ((fwd & 1) == 1)
  433.                                 {
  434.                                     fwd = (uint)((fwd >> 1) ^ poly);
  435.                                 }
  436.                                 else
  437.                                 {
  438.                                     fwd >>= 1;
  439.                                 }
  440.  
  441.                                 if ((rev & 0x80000000) != 0)
  442.                                 {
  443.                                     rev = ((rev ^ poly) << 1) | 1;
  444.                                 }
  445.                                 else
  446.                                 {
  447.                                     rev <<= 1;
  448.                                 }
  449.                             }
  450.                             table[i] = fwd;
  451.                             revtable[i] = rev;
  452.                         }
  453.                     }
  454.  
  455.                     uint crc = startxor;
  456.                     for (int i = 0; i < fixpos; i++)
  457.                     {
  458.                         crc = (crc >> 8) ^ table[(crc ^ bytes[i]) & 0xff];
  459.                     }
  460.                     Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
  461.                     crc = wantcrc ^ startxor;
  462.                     for (int i = length - 1; i >= fixpos; i--)
  463.                     {
  464.                         crc = (crc << 8) ^ revtable[crc >> (3 * 8)] ^ bytes[i];
  465.                     }
  466.                     Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
  467.                     return BitConverter.GetBytes(crc);
  468.                 }
  469.                 catch (Exception x)
  470.                 {
  471.                     MessageBox.Show(x.ToString());
  472.                     return null;
  473.                 }
  474.             }
  475.         }
  476.  
  477.         public class Crc32 : HashAlgorithm
  478.         {
  479.             public const UInt32 DefaultPolynomial = 0xedb88320;
  480.             public const UInt32 DefaultSeed = 0xffffffff;
  481.  
  482.             private UInt32 hash;
  483.             private UInt32 seed;
  484.             private UInt32[] table;
  485.             private static UInt32[] defaultTable;
  486.  
  487.             public Crc32()
  488.             {
  489.                 table = InitializeTable(DefaultPolynomial);
  490.                 seed = DefaultSeed;
  491.                 Initialize();
  492.             }
  493.  
  494.             public Crc32(UInt32 polynomial, UInt32 seed)
  495.             {
  496.                 table = InitializeTable(polynomial);
  497.                 this.seed = seed;
  498.                 Initialize();
  499.             }
  500.  
  501.             public override void Initialize()
  502.             {
  503.                 hash = seed;
  504.             }
  505.  
  506.             protected override void HashCore(byte[] buffer, int start, int length)
  507.             {
  508.                 hash = CalculateHash(table, hash, buffer, start, length);
  509.             }
  510.  
  511.             protected override byte[] HashFinal()
  512.             {
  513.                 byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
  514.                 this.HashValue = hashBuffer;
  515.                 return hashBuffer;
  516.             }
  517.  
  518.             public override int HashSize
  519.             {
  520.                 get { return 32; }
  521.             }
  522.  
  523.             public static UInt32 Compute(byte[] buffer)
  524.             {
  525.                 return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
  526.             }
  527.  
  528.             public static UInt32 Compute(UInt32 seed, byte[] buffer)
  529.             {
  530.                 return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
  531.             }
  532.  
  533.             public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  534.             {
  535.                 return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  536.             }
  537.  
  538.             private static UInt32[] InitializeTable(UInt32 polynomial)
  539.             {
  540.                 if (polynomial == DefaultPolynomial && defaultTable != null)
  541.                     return defaultTable;
  542.  
  543.                 UInt32[] createTable = new UInt32[256];
  544.                 for (int i = 0; i < 256; i++)
  545.                 {
  546.                     UInt32 entry = (UInt32)i;
  547.                     for (int j = 0; j < 8; j++)
  548.                         if ((entry & 1) == 1)
  549.                             entry = (entry >> 1) ^ polynomial;
  550.                         else
  551.                             entry = entry >> 1;
  552.                     createTable[i] = entry;
  553.                 }
  554.  
  555.                 if (polynomial == DefaultPolynomial)
  556.                     defaultTable = createTable;
  557.  
  558.                 return createTable;
  559.             }
  560.  
  561.             private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
  562.             {
  563.                 UInt32 crc = seed;
  564.                 for (int i = start; i < size; i++)
  565.                     unchecked
  566.                     {
  567.                         crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
  568.                     }
  569.                 return crc;
  570.             }
  571.  
  572.             private byte[] UInt32ToBigEndianBytes(UInt32 x)
  573.             {
  574.                 return new byte[] {
  575.                         (byte)((x >> 24) & 0xff),
  576.                         (byte)((x >> 16) & 0xff),
  577.                         (byte)((x >> 8) & 0xff),
  578.                         (byte)(x & 0xff)
  579.                 };
  580.             }
  581.         }
  582.     }
  583. }