public static Sound Read(Stream source) { BinaryReaderEx reader = new BinaryReaderEx(source); Sound result = new Sound(); int id = reader.ReadInt32(); if (id == 0x00394453) { int headerLength = reader.ReadInt32(); int sampleLength = reader.ReadInt32(); headerLength -= 12; if (headerLength > 0) reader.ReadBytes(headerLength); byte[] wavData = reader.ReadBytes(sampleLength); using (MemoryStream wavDataMem = new MemoryStream(wavData)) { using (WaveStream wavStream = new WaveFileReader(wavDataMem)) { byte[] rawWaveData = new byte[wavStream.Length]; wavStream.Read(rawWaveData, 0, (int)wavStream.Length); result.SetSound(rawWaveData, wavStream.WaveFormat); } } } return result; }
public static Sound Read(Stream source, Properties properties) { BinaryReaderEx reader = new BinaryReaderEx(source); Sound result = new Sound(); bool stereo = (properties.Flag0F & 0x80) != 0; byte[] data = reader.ReadBytes(properties.SampleLength); int dataLength = data.Length; byte[] newData = new byte[dataLength * (stereo ? 1 : 2)]; // translate the "signed" data int newDataPtr = 0; for (int i = 0; i < dataLength; i += 2) { int sample = ((int)data[i] << 8) | data[i + 1]; if (sample >= 0x8000) { sample = -(sample & 0x7FFF); } newData[newDataPtr] = (byte)(sample & 0xFF); newData[newDataPtr + 1] = (byte)((sample >> 8) & 0xFF); if (!stereo) { newData[newDataPtr + 2] = newData[newDataPtr]; newData[newDataPtr + 3] = newData[newDataPtr + 1]; newDataPtr += 2; } newDataPtr += 2; } // 16-bit stereo format result.Format = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, properties.Frequency, 2, properties.Frequency * 4, 4, 16); result.Data = newData; // determine volume result.Volume = VolumeTable[properties.Volume]; // determine panning result.Panning = (float)(properties.Panning - 1) / (float)0x7E; // return the final result return result; }
public static BemaniSSP Read(Stream source) { BinaryReaderEx reader = new BinaryReaderEx(source); BemaniSSP result = new BemaniSSP(); reader.ReadBytes(16); // name of archive int length = reader.ReadInt32(); int count = (length - 18) / 4; for (int i = 0; i < count; i++) { int offset = reader.ReadInt32(); if (offset >= length && offset < reader.BaseStream.Length) { long currentOffset = reader.BaseStream.Position; reader.BaseStream.Position = offset; result.sounds.Add(BemaniSD9.Read(reader.BaseStream)); reader.BaseStream.Position = currentOffset; } } return result; }
public static void Decompress(Stream source, Stream target, int length, int decompLength, BemaniLZSS2Properties props) { byte[] ring = new byte[props.ringBufferSize]; int ring_pos = props.ringBufferOffset; int chunk_offset; int chunk_length; int control_word = 1; int controlBitsLeft = 0; int controlBitMask = 0x1; byte cmd1; byte cmd2; byte data; if (decompLength <= 0) decompLength = int.MaxValue; BinaryReaderEx sourceReader = new BinaryReaderEx(source); BinaryWriterEx writer = new BinaryWriterEx(target); using (MemoryStream mem = new MemoryStream(sourceReader.ReadBytes(length))) { BinaryReaderEx reader = new BinaryReaderEx(mem); while (decompLength > 0 && length > 0) { if (controlBitsLeft == 0) { /* Read a control byte */ control_word = reader.ReadByte(); length--; controlBitsLeft = 8; } /* Decode a byte according to the current control byte bit */ if ((control_word & controlBitMask) != 0) { /* Straight copy, store into history ring */ data = reader.ReadByte(); length--; writer.Write(data); ring[ring_pos] = data; ring_pos = (ring_pos + 1) % props.ringBufferSize; decompLength--; } else { /* Reference to data in ring buffer */ switch (props.type) { case BemaniLZSS2Type.Firebeat: cmd1 = reader.ReadByte(); cmd2 = reader.ReadByte(); length -= 2; chunk_length = (cmd1 & 0x0F) + 3; chunk_offset = (((int)cmd1 & 0xF0) << 4) + (int)cmd2; chunk_offset = ring_pos - chunk_offset; while (chunk_offset < 0) chunk_offset += props.ringBufferSize; break; case BemaniLZSS2Type.GCZ: cmd1 = reader.ReadByte(); cmd2 = reader.ReadByte(); length -= 2; chunk_length = (cmd2 & 0x0F) + 3; chunk_offset = (((int)cmd2 & 0xF0) << 4) | cmd1; break; default: return; } for ( ; chunk_length > 0 && length > 0 ; chunk_length--) { /* Copy historical data to output AND current ring pos */ writer.Write(ring[chunk_offset]); ring[ring_pos] = ring[chunk_offset]; /* Update counters */ chunk_offset = (chunk_offset + 1) % props.ringBufferSize; ring_pos = (ring_pos + 1) % props.ringBufferSize; decompLength--; } } /* Get next control bit */ control_word >>= 1; controlBitsLeft--; } } }
private static Entry[] DecodeStepChunk(Chunk source, int measureUnit) { using (BinaryReaderEx reader = new BinaryReaderEx(new MemoryStream(source.Data))) { int count = reader.ReadInt32(); int[] metric = new int[count]; List<Entry> result = new List<Entry>(); for (int i = 0; i < count; i++) metric[i] = reader.ReadInt32(); byte[] step = reader.ReadBytes(count); byte[] freeze = reader.ReadBytes((int)reader.BaseStream.Length - (4 + (5 * count))); int freezeIndex = 0; int freezeCount = 0; while (freezeIndex < freeze.Length && freeze[freezeIndex] == 0) freezeIndex++; for (int i = 0; i < count; i++) { if (step[i] == 0) freezeCount++; } for (int i = 0; i < count; i++) { bool isShock = false; bool isFreeze = false; byte stepData = step[i]; int column = 0; if (stepData == 0) { isFreeze = true; stepData = freeze[freezeIndex]; freezeIndex++; freezeIndex++; // freeze type, ignored for now } else if ((stepData & 0xF) == 0xF) { isShock = true; } while (stepData > 0) { if ((stepData & 1) != 0) { Entry entry = new Entry(); entry.MetricMeasure = metric[i] / measureUnit; entry.MetricOffset = new Fraction(metric[i] % measureUnit, measureUnit); entry.Column = column; if (isFreeze) entry.Freeze = true; if (isShock) entry.Type = EntryType.Mine; else entry.Type = EntryType.Marker; result.Add(entry); } stepData >>= 1; column++; } } return result.ToArray(); } }
public static Chunk Read(Stream source, int length) { Chunk result = new Chunk(); BinaryReaderEx reader = new BinaryReaderEx(source); length -= 8; result.Type = reader.ReadInt16(); result.Parameter = reader.ReadInt16(); result.Data = reader.ReadBytes(length); return result; }
public static BemaniIFS Read(Stream source) { BemaniIFS result = new BemaniIFS(); // read header Header header = Header.Read(source); byte[] propertyPageData = new byte[header.BodyStart - Header.Size]; source.Read(propertyPageData, 0, propertyPageData.Length); // read property page #if (false) List<byte[]> dataList = new List<byte[]>(); BinaryReaderEx reader = new BinaryReaderEx(source); BemaniIFS result = new BemaniIFS(); // header length is 0x28 bytes reader.ReadInt32S(); // identifier Int16 headerMetaLength = reader.ReadInt16S(); // header meta amount? reader.ReadInt16S(); // bitwise xor 0xFFFF of previously read value reader.ReadInt32S(); reader.ReadInt32S(); Int32 headerLength = reader.ReadInt32S(); reader.ReadInt32S(); for (int i = 1; i < headerMetaLength; i++) { reader.ReadInt32S(); reader.ReadInt32S(); } Console.WriteLine("Header length: " + headerLength.ToString()); // read table A Int32 tableALength = reader.ReadInt32S(); Console.WriteLine("Table A length: " + tableALength.ToString()); MemoryStream tableAMem = new MemoryStream(reader.ReadBytes(tableALength)); // read table B Int32 tableBLength = reader.ReadInt32S(); Console.WriteLine("Table B length: " + tableBLength.ToString()); MemoryStream tableBMem = new MemoryStream(reader.ReadBytes(tableBLength)); // read padding int headerPadding = headerLength - (0x10 + (headerMetaLength * 8) + 4 + tableALength + 4 + tableBLength); if (headerPadding > 0) reader.ReadBytes(headerPadding); // a bit of a hack to get the info we need (it's probably not accurate) BinaryReaderEx tableAReader = new BinaryReaderEx(tableAMem); BinaryReaderEx tableBReader = new BinaryReaderEx(tableBMem); tableAReader.BaseStream.Position = 0x18; tableBReader.BaseStream.Position = 0x14; int dataLength = tableBReader.ReadInt32S(); MemoryStream dataChunk = new MemoryStream(reader.ReadBytes(dataLength)); BinaryReaderEx dataReader = new BinaryReaderEx(dataChunk); // process tables int chunkIndex = 0; bool processTable = true; while (processTable) { Console.Write("A:" + Util.ConvertToHexString((int)tableAReader.BaseStream.Position, 8) + " B:" + Util.ConvertToHexString((int)tableBReader.BaseStream.Position, 8) + " "); byte chunkType = tableAReader.ReadByte(); Console.Write("Op:" + Util.ConvertToHexString(chunkType, 2) + " "); switch (chunkType) { case 0x06: // directory { byte subType = tableAReader.ReadByte(); switch (subType) { case 0x03: tableAReader.ReadBytes(3); break; case 0x06: break; default: break; } Int32 fileModified = tableBReader.ReadInt32S(); // modified date? Console.WriteLine("*" + Util.ConvertToHexString(fileModified, 8)); } continue; case 0x1E: // file tableAReader.ReadByte(); { Int32 fileOffset = tableBReader.ReadInt32S(); // offset Int32 fileLength = tableBReader.ReadInt32S(); // length Int32 fileModified = tableBReader.ReadInt32S(); // modified date? Console.WriteLine(Util.ConvertToHexString(fileOffset, 8) + ":" + Util.ConvertToHexString(fileLength, 8) + ", *" + Util.ConvertToHexString(fileModified, 8)); dataReader.BaseStream.Position = fileOffset; dataList.Add(dataReader.ReadBytes(fileLength)); } break; case 0x94: // filename Console.WriteLine("FileID: " + Util.ConvertToHexString(tableAReader.ReadInt32S(), 8)); continue; case 0xFE: // end of entry Console.WriteLine("End of entry."); break; case 0xFF: // end of list processTable = false; Console.WriteLine("End of list."); continue; default: // for types we don't know, skip the whole line for now Console.WriteLine("UNKNOWN."); break; } while (chunkType != 0xFE) { chunkType = tableAReader.ReadByte(); } chunkIndex++; } result.files = dataList; #endif return result; }
public SHNFile(string path) { try { columns.Clear(); this.Path = path; if (System.IO.Path.GetFileNameWithoutExtension(path).ToLower().Contains("textdata")) isTextData = true; BinaryReaderEx r = new BinaryReaderEx(File.OpenRead(path)); if (path.EndsWith(".shn")) { this.CryptHeader = r.ReadBytes(0x20); data = r.ReadBytes(r.ReadInt32() - 0x24); } else data = r.ReadBytes((int)r.Length); r.Close(); if (Properties.Settings.Default.isNewCrypto == true) this.Decrypt(data, 0, data.Length, true); else this.Decrypt(data, 0, data.Length); r = new BinaryReaderEx(new MemoryStream(data)); this.Header = r.ReadUInt32(); //FileStream stream = new FileStream(System.IO.Path.GetDirectoryName(path) + "\\TestFile.dat", FileMode.Create); //stream.Write(data, 0, data.Length); //stream.Close(); //return; if (!((this.Header == 0xcdcdcdcd) | (this.Header == 0))) { //bleh, why check, unk.dat is useless, outspark uses other enc } //Parse columns this.RecordCount = r.ReadUInt32(); this.DefaultRecordLength = r.ReadUInt32(); this.ColumnCount = r.ReadUInt32(); this.ColumnNames = new string[this.ColumnCount]; this.ColumnTypes = new uint[this.ColumnCount]; this.ColumnLengths = new int[this.ColumnCount]; int num2 = 2; int unkCols = 0; for (uint i = 0; i < this.ColumnCount; i++) { string str = r.ReadString(0x30); // if (str.Length < 2) MessageBox.Show(str.Length.ToString()); uint num4 = r.ReadUInt32(); int num5 = r.ReadInt32(); SHNColumn col = new SHNColumn(); if (str.Length == 0) { str = "UnkCol" + unkCols.ToString(); unkCols++; } col.name = str; col.Type = num4; col.Lenght = num5; columns.Add(col); this.ColumnNames[i] = str; this.ColumnTypes[i] = num4; this.ColumnLengths[i] = num5; num2 += num5; } if (num2 != this.DefaultRecordLength) { throw new Exception("Wrong record lenght!"); } //generate columns this.GenerateColumns(table, columns); //add data into rows this.ReadRows(r, table); } catch (Exception e) { Stream X = new FileStream("unk.dat", FileMode.OpenOrCreate); BinaryWriter lol = new BinaryWriter(X); lol.Write(data, 0, data.Length); lol.Close(); throw new Exception("Unknown File Type -- dec to unk.dat Reason: " + e.Message); } }
public void LoadMe(string path) { BinaryReaderEx r = new BinaryReaderEx(File.OpenRead(path)); if (path.EndsWith(".shn")) { this.CryptHeader = r.ReadBytes(0x20); data = r.ReadBytes(r.ReadInt32() - 0x24); } else { data = r.ReadBytes((int)r.Length); } r.Close(); if (Properties.Settings.Default.isNewCrypto == true) this.Decrypt(data, 0, data.Length, true); else this.Decrypt(data, 0, data.Length); }