//---------------------------------------------------------------------------------------- public static void decompFile() { global = cGlobal.INSTANCE; Trace.WriteLine("Decompressing"); Trace.Indent(); try { using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { bool b_result = TOLib.decomp(fs_in, ms_out); if (!b_result) throw new Exception(); global.writeFileToDisk(ms_out, global.DESTINATION); } } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
public static void compFile() { global = cGlobal.INSTANCE; Trace.WriteLine(string.Format("Compression of file {0}", global.SOURCE)); Trace.Indent(); try { using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (FileStream fs_out = new FileStream(global.DESTINATION, FileMode.Create)) { bool b_result = TOLib.comp(fs_in, fs_out, global.MODE); if (!b_result) throw new Exception(); } } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
public static void compFile(Stream as_in, Stream as_out) { global = cGlobal.INSTANCE; Trace.WriteLine(string.Format("Compression of file {0}", global.SOURCE)); Trace.Indent(); try { bool b_result = TOLib.comp(as_in, as_out, global.MODE); if (!b_result) throw new Exception(); as_out.Position = 0; } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
static void Main(string[] args) { global = cGlobal.INSTANCE; bool result = false; ConsoleTraceListener ctl_trace = new ConsoleTraceListener(); Trace.Listeners.Add(ctl_trace); global.ts_TypeTrace.Level = TraceLevel.Error; sw_watch = new Stopwatch(); try { global.readArguments(args); Trace.AutoFlush = true; sw_watch.Start(); switch (global.ACTION) { case "COMP": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); if (global.MODE == -1) throw new ToDException("The option -m is missing !"); using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { Compression.compFile(fs_in, ms_out); global.writeFileToDisk(ms_out, global.DESTINATION); } break; case "DECOMP": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); result = false; using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { Compression.decompFile(fs_in, ms_out); if (global.RECURSIVE) result = global.processFile(ms_out, global.DESTINATION, "", global.DESTINATION); } break; case "EXTRACT": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DIR_OUT == "") throw new ToDException("The option -d is missing !"); extractFromIso(); break; case "INSERT": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); insertToIso(); break; case "PACK": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); if (global.MODE == -1) throw new ToDException("The option -m is missing !"); Archive.packFile(); break; case "UNPACK": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) { Archive.unpackFile(fs_in, global.DIR_OUT, 1); } break; default: throw new ToDException(string.Format("Unknow action {0} !", global.ACTION)); } } catch (ToDException ex) { Trace.WriteLine(ex.Message + Environment.NewLine, "ERROR"); usage(); } catch (Exception ex) { Trace.WriteLine(ex.Message + Environment.NewLine, "ERROR"); Trace.WriteLine(ex.StackTrace); } finally { sw_watch.Stop(); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Terminated. Execution time : {0}", sw_watch.Elapsed)); } }
//---------------------------------------------------------------------------------------- public static void unpackFile(Stream as_in, string as_pathname, int mode) { global = cGlobal.INSTANCE; List<cGlobal.st_index> index = new List<cGlobal.st_index>(); byte[] b_file; string s_ext; int size; try { index = readIndex(as_in, mode); for (int i = 0; i < index.Count; i++) { if (i == index.Count - 1) size = (int)(as_in.Length - index[i].pos); else size = (int)(index[i + 1].pos - index[i].pos); b_file = new byte[size]; string s_name = string.Format("{0:0000}", index[i].id); s_ext = global.getTypeOfFile(as_in, (int)index[i].pos, size); as_in.Seek(index[i].pos, SeekOrigin.Begin); using (MemoryStream ms_file = new MemoryStream()) { ms_file.CopyFrom(as_in, size); ms_file.Position = 0; if (global.RECURSIVE) global.processFile(ms_file, as_pathname, s_name, as_pathname + s_name + s_ext); else global.writeFileToDisk(ms_file, as_pathname + s_name + s_ext); } b_file = null; } } catch (Exception ex) { throw ex; } }
public static void unpackBDat(MemoryStream ams_file, List<cGlobal.st_index> al_index, string as_path) { global = cGlobal.INSTANCE; byte[] b_file; string s_ext; Trace.Indent(); try { foreach (cGlobal.st_index file in al_index) { b_file = new byte[file.size]; string s_name = string.Format("{0:0000}", file.id); s_ext = global.getTypeOfFile(ams_file, (int)file.pos, (int)file.size); using (MemoryStream ms_file = new MemoryStream()) { ams_file.CopyTo(ms_file, file.size, file.pos); ms_file.Position = 0; if (global.RECURSIVE) global.processFile(ms_file, as_path, s_name, as_path + s_name + s_ext); else global.writeFileToDisk(ms_file, as_path + s_name + s_ext); } b_file = null; } } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
//---------------------------------------------------------------------------------------- public static void packFile() { global = cGlobal.INSTANCE; List<cGlobal.st_index> index_orig = new List<cGlobal.st_index>(); List<cGlobal.st_index> index_dest = new List<cGlobal.st_index>(); MemoryStream ms_dest = new MemoryStream(); cGlobal.st_index elem; byte[] b_file; string s_pathname; string s_name; int size; Trace.WriteLine(string.Format("Inserting file {0}", global.SOURCE)); Trace.Indent(); try { using (BinaryReader br_file = new BinaryReader(File.Open(global.SOURCE, FileMode.Open, FileAccess.Read))) { index_orig = readIndex(br_file, global.MODE); ms_dest.Position = global.MODE == 1 ? index_orig.Count * 4 : index_orig.Count * 4 + 4; for (int i = 0; i < index_orig.Count; i++) { if (i == index_orig.Count - 1) size = (int)(br_file.BaseStream.Length - index_orig[i].pos); else size = (int)(index_orig[i + 1].pos - index_orig[i].pos); s_name = string.Format("{0:0000}{1}", index_orig[i].id, global.getTypeOfFile(br_file, (int)index_orig[i].pos, size)); s_pathname = Path.GetDirectoryName(global.SOURCE) + "/" + Path.GetFileNameWithoutExtension(global.SOURCE) + "/" + s_name; elem.id = i; elem.pos = (UInt32)ms_dest.Position; if (File.Exists(s_pathname)) { Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Inserting file {0}", s_name)); using (BinaryReader br = new BinaryReader(File.Open(s_pathname, FileMode.Open, FileAccess.Read))) { b_file = br.ReadBytes((int)br.BaseStream.Length); elem.size = (UInt32)br.BaseStream.Length; } } else { Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Copying file {0}{1}", s_name)); br_file.BaseStream.Seek(index_orig[i].pos, SeekOrigin.Begin); b_file = new byte[size]; br_file.Read(b_file, 0, size); elem.size = (UInt32)size; } index_dest.Add(elem); ms_dest.Write(b_file, 0, size); global.padding(ms_dest, 4); b_file = null; } } ms_dest.Position = 0; if (global.MODE == 2) { byte[] b = new byte[4]; b = BitConverter.GetBytes(index_dest.Count); Array.Reverse(BitConverter.GetBytes(index_dest.Count)); ms_dest.Write(b, 0, 4); } writeIndex(ms_dest, index_dest); global.writeFileToDisk(ms_dest, global.DESTINATION); } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
public static MemoryStream packBDat(MemoryStream ams_orig, List<cGlobal.st_index> al_orig, string as_path) { global = cGlobal.INSTANCE; MemoryStream ms_new = new MemoryStream(); List<cGlobal.st_index> l_newIndex = new List<cGlobal.st_index>(); cGlobal.st_index elem; byte[] b_file; string s_ext; int i_num = 0; Trace.Indent(); try { foreach (cGlobal.st_index file in al_orig) { string s_name = string.Format("{0:0000}", file.id); string s_fullPath = global.DIR_OUT + as_path + s_name; Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Inserting file {0}", s_name)); s_ext = global.getTypeOfFile(ams_orig, (int)file.pos, (int)file.size); s_fullPath += s_ext; if (File.Exists(s_fullPath)) { using (BinaryReader br = new BinaryReader(File.Open(s_fullPath, FileMode.Open))) { b_file = br.ReadBytes((int)br.BaseStream.Length); elem.id = i_num++; elem.pos = (UInt32)ms_new.Position; elem.size = (UInt32)br.BaseStream.Length; l_newIndex.Add(elem); ms_new.Write(b_file, 0, (int)br.BaseStream.Length); } } else { b_file = new byte[file.size]; ams_orig.Seek(file.pos, SeekOrigin.Begin); ams_orig.Read(b_file, 0, (int)file.size); elem.id = i_num++; elem.pos = (UInt32)ms_new.Position; elem.size = file.size; l_newIndex.Add(elem); ms_new.Write(b_file, 0, (int)file.size); } b_file = null; } return ms_new; } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
public static bool isCompressed(BinaryReader abr_file, ref int mode, int ai_size = -1) { global = cGlobal.INSTANCE; mode = 0; UInt32 size_in = 0; UInt32 size_out = 0; long pos = abr_file.BaseStream.Position; abr_file.BaseStream.Position = 0; if (ai_size == -1) ai_size = (int)abr_file.BaseStream.Length; Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, "isCompressed - ENTER"); Trace.Indent(); try { Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, string.Format("Size : {0}", ai_size)); if (ai_size < 9) return false; mode = (int)abr_file.ReadByte(); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, string.Format("Mode : {0}", mode)); if (mode != 0 && mode != 1 && mode != 3) return false; size_in = abr_file.ReadUInt32() + 9; size_out = abr_file.ReadUInt32(); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, string.Format("Size IN : {0}", size_in)); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, string.Format("Size OUT : {0}", size_out)); while (size_in % 4 != 0 && size_in < ai_size) size_in++; if (mode == 0 && size_in != size_out) return false; if ((mode == 0x01 || mode == 0x03) && (size_out <= size_in || ai_size < size_in)) return false; return true; } catch (Exception ex) { throw ex; } finally { abr_file.BaseStream.Position = pos; Trace.Unindent(); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose && global.VERBOSE >= global.verboseFull, "isCompressed - EXIT"); } }
public static void decompFile(Stream as_in, Stream as_out) { global = cGlobal.INSTANCE; Trace.WriteLine("Decompressing"); Trace.Indent(); try { bool b_result = TOLib.decomp(as_in, as_out); if (!b_result) throw new Exception(); as_in.Position = 0; as_out.Position = 0; } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }