// TAHファイルをstreamに書き込む.成功ならtrue,失敗ならfalseを返す. public bool Write(Stream tostream) { try { int index = 0; // ファイル先頭に移動する. tostream.Seek(0, SeekOrigin.Begin); using (BufferedStream stream = new BufferedStream(tostream)) using (BinaryWriter binarywriter = new BinaryWriter(stream)) { // ヘッダー情報を構築する. BuildHeader(); // ヘッダーを書き込む. header.Write(binarywriter); long entryposition = stream.Position; // offset値は0のままでエントリを一度書き込む. foreach (TAHContent content in contents) { content.Entry.Write(binarywriter); } // ディレクトリを構築する BuildDirectory(); // ディレクトリを書き込む. directory.Write(binarywriter); // データを書き込む foreach (TAHContent content in contents) { // 実際にデータを読み込む. Byte[] data = Data(delegateFileList[index++]); // ヘッダーを更新する. content.Data = data; content.Entry.Length = data.Length; content.Entry.DataOffset = (uint)stream.Position; UInt32 length = (UInt32)content.Entry.Length; binarywriter.Write(length); Byte[] cryptdata = TAHUtil.Encrypt(content.Data); binarywriter.Write(cryptdata); // 参照を解除する. content.Data = null; data = null; } // エントリを書き直す. stream.Seek(entryposition, SeekOrigin.Begin); foreach (TAHContent content in contents) { content.Entry.Write(binarywriter); } // バッファをフラッシュして完了. binarywriter.Flush(); binarywriter.Close(); //stream.Flush(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); return(false); } return(true); }
public static void ReadExternalFileList() { external_files = new ext_file_list(); if (System.IO.File.Exists("names.txt")) { System.IO.StreamReader reader = new System.IO.StreamReader(System.IO.File.OpenRead("names.txt")); List <string> known_files = new List <string>(); string line; while ((line = reader.ReadLine()) != null) { known_files.Add(line); } //map a list of hash keys to the file... external_files.files = known_files.ToArray(); if (external_files.files != null) { external_files.hashkeys = new UInt32[external_files.files.Length]; for (int i = 0; i < external_files.files.Length; i++) { external_files.hashkeys[i] = TAHUtil.CalcHash(external_files.files[i]); //Console.WriteLine(external_files.hashkeys[i].ToString("X8") + "\t" + external_files.files[i]); } } //sorting for faster look up... Array.Sort(external_files.hashkeys, external_files.files); } }
private static TAHEntryInfo GetEntry(string filename) { uint hash = 0; // ファイル名からハッシュ値を求める. if (Path.GetDirectoryName(filename) == "") { // dddddddd_xxxxxxxx.eee形式をハッシュ値に戻す if (filename.Length >= 17) { string hashcode = filename.Substring(9, 8); try { // 16進数からハッシュ値に. hash = UInt32.Parse(hashcode, System.Globalization.NumberStyles.HexNumber); } catch (Exception) { // 判んない時は適当につける. hash = TAHUtil.CalcHash(filename); } } else { hash = TAHUtil.CalcHash(filename); } } else { hash = TAHUtil.CalcHash(filename); } return(hashentry[hash]); }
public static byte[] ReadEntryData(BinaryReader br, TAHEntry e) { br.BaseStream.Seek(e.DataOffset, SeekOrigin.Begin); byte[] output = new byte[br.ReadInt32()]; byte[] input = br.ReadBytes(e.Length - 4); TAHUtil.Decrypt(input, output); return(output); }
private static void CreateTahFiles(string tahname, uint version) { string basefilename = Path.GetFileNameWithoutExtension(tahname); int count = 0; foreach (List <string> tahfilelist in tahfilelists) { if (tahfilelist.Count > 0) { string destname; if (count == 0) { destname = tahname; } else { tahname = Path.Combine(Path.GetDirectoryName(tahname), basefilename + "-" + count.ToString() + ".tah"); } count++; TAHWriter writer = new TAHWriter(); writer.Version = version; foreach (string file in tahfilelist) { writer.Add(file); } Console.WriteLine("tah file " + tahname); using (FileStream fs = File.OpenWrite(tahname)) { int filecount = 1, filemax = tahfilelist.Count; // データ取得delegate writer.RawData += delegate(string filename) { TAHRawData rawdata = new TAHRawData(); TAHEntryInfo fileinfo = GetEntry(filename); TAHFile tahfile = fileinfo.file; uint length; rawdata.data = TAHUtil.ReadRawEntryData(tahfile.Reader, fileinfo.entry, out length); rawdata.length = length; //Console.Write(tahname + " " + filecount++.ToString() + "/" + filemax.ToString() + "\r"); return(rawdata); }; writer.RawWrite(fs); Console.Write("\n"); } } } }
// TAHにファイルを加える. public void Add(string filename) { // オリジナルのファイル名を保存しておく. delegateFileList.Add(filename); // TAHに格納可能なファイル名に変換する. string regularfile = filename.Replace('\\', '/'); TAHEntry tahentry = new TAHEntry(); tahentry.DataOffset = 0; tahentry.FileName = regularfile; tahentry.Length = 0; if (Path.GetDirectoryName(regularfile) == "") { // dddddddd_xxxxxxxx.eee形式をハッシュ値に戻す if (regularfile.Length >= 17) { string hashcode = regularfile.Substring(9, 8); try { // 16進数からハッシュ値に. tahentry.Hash = UInt32.Parse(hashcode, System.Globalization.NumberStyles.HexNumber); // 暫定ファイル名は削除. tahentry.FileName = null; } catch (Exception) { // 判んない時は適当につける. tahentry.Hash = TAHUtil.CalcHash(regularfile); } } else { // 判んない時は適当につける. tahentry.Hash = TAHUtil.CalcHash(regularfile); } } else { tahentry.Hash = TAHUtil.CalcHash(regularfile); } TAHContent content = new TAHContent(tahentry, null); contents.Add(content); }
public void Write(BinaryWriter bw) { MemoryStream ms = new MemoryStream(); BinaryWriter bw2 = new BinaryWriter(ms); foreach (string i in Files) { if (i.EndsWith("/")) { TAHUtil.WriteString(bw2, i); } else { TAHUtil.WriteString(bw2, Path.GetFileName(i)); } } bw2.Flush(); byte[] encrypted = TAHUtil.Encrypt(ms.ToArray()); bw.Write((uint)ms.Length); bw.Write(encrypted); }
public TAHContent LoadContent(BinaryReader br, TAHEntry e) { return(new TAHContent(e, TAHUtil.ReadEntryData(br, e))); }
public void Read(BinaryReader br, TAHFile file) { // ディレクトリデータの読み込み Files = new List <string>(file.Header.NumEntries); int output_length = br.ReadInt32(); int input_length = (int)(file.EntrySet[0].DataOffset - br.BaseStream.Position); //- 16 - 8 * file.Header.NumEntries; byte[] input = br.ReadBytes(input_length); byte[] output = new byte[output_length]; if (output.Length == 0) { return; } // add konoa:tahdecryptorのバグ回避. if (input.Length == 0) { return; } TAHUtil.Decrypt(input, output); //TAHdecrypt.Decrypter.decrypt(ref input, (uint)input.Length, ref output, (uint)output.Length); MemoryStream ms = new MemoryStream(output); BinaryReader br2 = new BinaryReader(ms); try { string dir = ""; while (ms.Position < ms.Length) { string name = TAHUtil.ReadString(br2); if (name.Length == 0) { } else if (name.EndsWith("/")) { dir = name; //DbgPrint("Directory: " + dir); } else { name = dir + name; uint hash = TAHUtil.CalcHash(name); TAHEntry ent; //DbgPrint(hash.ToString("X").PadLeft(8, '0')); if (file.EntrySet.TryGetValue(hash, out ent)) { ent.FileName = name; //DbgPrint(": Found: " + file); } else { //DbgPrint(": Not Found: " + file); System.Diagnostics.Debug.Assert(false); } //EntryMap[hash].FileName = FileName; } Files.Add(name); } } catch (EndOfStreamException) { } }