Exemple #1
0
    public Message(sbyte command, byte[] data) {

        this.command = command;
        ms = new MemoryStream(data);
        iss = new BinaryReaderEx(ms);
        dis = new DataInputStream(iss);
    }
Exemple #2
0
        public static List<Properties> ReadSampleBlock(Stream source)
        {
            BinaryReader sourceReader = new BinaryReader(source);
            byte[] sourceData = sourceReader.ReadBytes(0x2000);
            int count = 256;
            List<Properties> result = new List<Properties>();

            // load sample block
            using (MemoryStream tempMem = new MemoryStream(sourceData))
            {
                BinaryReaderEx dataReader = new BinaryReaderEx(tempMem);
                for (int i = 0; i < count; i++)
                {
                    Properties props = new Properties();
                    props.Channel = dataReader.ReadByte();
                    props.Flag01 = dataReader.ReadByte();
                    props.Frequency = dataReader.ReadUInt16S();
                    props.Volume = dataReader.ReadByte();
                    props.Panning = dataReader.ReadByte();
                    props.SampleOffset = dataReader.ReadInt24S() * 2;
                    props.SampleLength = ((dataReader.ReadInt24S() * 2) - props.SampleOffset);
                    props.Value0C = dataReader.ReadInt16S();
                    props.Flag0E = dataReader.ReadByte();
                    props.Flag0F = dataReader.ReadByte();
                    props.SizeInBlocks = dataReader.ReadInt16S();
                    result.Add(props);
                }
            }

            return result;
        }
Exemple #3
0
        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;
        }
Exemple #4
0
        static public void Decompress(Stream source, Stream target)
        {
            BinaryReaderEx reader = new BinaryReaderEx(source);

            // unsure what this is- probably decompressed size
            int decompressedSize = reader.ReadInt32S();

            // read length
            int length = reader.ReadInt32S();

            // decompress
            BemaniLZSS2.DecompressGCZ(source, target, length, decompressedSize);
        }
Exemple #5
0
        public static BemaniSSQ Read(Stream source, int measureUnit)
        {
            BinaryReaderEx reader = new BinaryReaderEx(source);
            BemaniSSQ result = new BemaniSSQ();
            Chunk tempoChunk = new Chunk();
            List<Chunk> chartChunks = new List<Chunk>();

            // parse all chunks in the file
            while (true)
            {
                int length = reader.ReadInt32();

                if (length <= 0)
                    break;

                Chunk chunk = Chunk.Read(source, length);

                switch (chunk.Type)
                {
                    case 0x0001: // tempo info
                        tempoChunk = chunk;
                        break;
                    case 0x0003: // chart
                        chartChunks.Add(chunk);
                        break;
                    default:
                        result.extraChunks.Add(chunk);
                        break;
                }
            }

            // assemble tempo information
            result.TempoEntries.AddRange(DecodeTempoChunk(tempoChunk, measureUnit));

            // convert charts
            foreach (Chunk chartChunk in chartChunks)
            {
                Chart chart = new Chart();
                chart.Entries.AddRange(DecodeStepChunk(chartChunk, measureUnit));
                chart.Tags["Panels"] = (chartChunk.Parameter & 0xF).ToString();
                chart.Tags["Difficulty"] = ((chartChunk.Parameter & 0xFF00) >> 8).ToString();
                result.charts.Add(chart);
            }

            return result;
        }
        public static bool DetectBemaniImage2DXAC(byte[] data)
        {
            if (data.Length < 8)
                return false;

            using (MemoryStream mem = new MemoryStream(data))
            {
                BinaryReaderEx reader = new BinaryReaderEx(mem);

                mem.Position = 4;

                int compressedSize = reader.ReadInt32S();
                if (compressedSize != (mem.Length - 8))
                    return false;
            }

            return true;
        }
Exemple #7
0
        static public bool DetectBemani1(byte[] data)
        {
            if (data.Length < 0x68)
                return false;

            using (MemoryStream mem = new MemoryStream(data))
            {
                BinaryReaderEx reader = new BinaryReaderEx(mem);
                mem.Position = mem.Length - 8;

                int endMarkerOffset = reader.ReadInt32();
                int endMarkerData = reader.ReadInt32();

                if (endMarkerData != 0x00000000 || endMarkerOffset != 0x7FFFFFFF)
                    return false;
            }

            return true;
        }
Exemple #8
0
        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;
        }
        static public bool DetectBemani2DXArchive(byte[] data)
        {
            if (data.Length < 0x4C)
                return false;

            using (MemoryStream mem = new MemoryStream(data))
            {
                BinaryReaderEx reader = new BinaryReaderEx(mem);
                mem.Position = 0x48;

                int offset = reader.ReadInt32();
                if ((offset < 0) || ((offset + 4) > data.Length))
                    return false;

                mem.Position = offset;
                if (new string(reader.ReadChars(4)) != "2DX9")
                    return false;

                return true;
            }
        }
Exemple #10
0
        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;
        }
Exemple #11
0
        /// <summary>
        /// Load LabelFeatureData file - static function for both CLabelFeatureData and CLabelFeatureDataCoded
        /// load in the file and output a LabelFeatureData object that has the specified Type (type = {LabelFeatureData, LabelFeatureDataCode}
        /// </summary>
        /// <param name="inFileName">the name of the file: xxx.tsv == tsv file formation; xxx.bin == binary uncoded data format; xxx.dp == binary coded data format</param>
        /// <param name="featureParser">parser that understand the feature values</param>
        /// <param name="labelParser">parser that understand the label values</param>
        /// <param name="dataGroupBoundary">data group boundaries</param>
        /// <param name="outDatatype">the output data type LabelFeatureData or LabelFeatureDataCoded</param>
        /// <param name="activeFeatureNames">only these feature values are loaded</param>
        /// <param name="cThreads">number of threads used to code the original data</param>
        /// <returns>the desired LabelFeatureData if no errors in loading; otherwise, null</returns>
        static public LabelFeatureData Load(string inFileName, IParser<float> featureParser, IParser<float> labelParser, IGroupBoundary dataGroupBoundary,
                                            Type outDataType, string[] activeFeatureNames, int cThreads, bool fCacheCodedFeature, bool fSparseCoded)
        {
            if (inFileName == null)
            {
                return null;
            }

            string[] fields = inFileName.Split('.');
            if (fields.Length <= 0)
            {
                return null;
            }

            CLabelFeatureData labelFeatureData = null;

            string sufix = fields[fields.Length-1];
            
            if (string.Compare(sufix, "bin", true) == 0 || string.Compare(sufix, "dp", true) == 0)
            {
                BinaryReaderEx binReaderEx = new BinaryReaderEx(inFileName);
                Type t = binReaderEx.Read<Type>();
                labelFeatureData = (CLabelFeatureData)binReaderEx.Read(t);
                binReaderEx.Close();
                
                labelFeatureData.SetActiveFeatures(activeFeatureNames);
            }
            else // "tsv" or null
            {
                TsvFileLoader tsvFileLoader = new TsvFileLoader(inFileName, null, labelParser, featureParser, dataGroupBoundary);

                labelFeatureData = new CLabelFeatureData(tsvFileLoader.FeatureName, tsvFileLoader.Labels, tsvFileLoader.GroupId, tsvFileLoader.Feature);
            }

            if (outDataType.Equals(typeof(CLabelFeatureDataCoded)))
            {
                if (labelFeatureData.GetType().Equals(typeof(CLabelFeatureDataCoded)))
                {
                    if (fCacheCodedFeature)
                    {
                        ((CLabelFeatureDataCoded)labelFeatureData).EncodeFeatureValues(cThreads, fSparseCoded);
                    }
                }
                else
                {
                    //need to upgrade to coded                    
                    labelFeatureData = new CLabelFeatureDataCoded(labelFeatureData, cThreads, fCacheCodedFeature, fSparseCoded);
                }
            }           

            return labelFeatureData;
        }
Exemple #12
0
        //deserialize the object
        public CLabelFeatureCore(BinaryReaderEx binReaderEx)
        {                                             
            this.labels = binReaderEx.ReadSingleArray();// Read<float>();                                   
            this.groupId = binReaderEx.ReadInt32Array();
            this.featureNames = binReaderEx.ReadStringArray();

            bool factiveFeatureNamesExist = binReaderEx.ReadBoolean();
            if (factiveFeatureNamesExist)
            {
                this.activeFeatureNames = binReaderEx.ReadStringArray();
            }

            bool factiveFeatureIdxExist = binReaderEx.ReadBoolean();
            if (factiveFeatureIdxExist)
            {
                this.activeFeatureIdx = binReaderEx.ReadInt32Array();
            }            
        }
Exemple #13
0
        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--;
                }
            }
        }
Exemple #14
0
            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;
            }
Exemple #15
0
        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 virtual void Decompress(BinaryReaderEx reader, BinaryWriter writer, StreamElement element)
 {
     int count = Math.Min(this.NumValues, element.NumElements);
     FloatStreamDecompressor decompressor = StreamDecompressors.GetFloatDecompressor(element.CompressionType);
     for (var i = 0; i < count; i++)
     {
         float val = decompressor(reader);
         writer.Write(val);
     }
 }
Exemple #17
0
 public DataInputStream(BinaryReaderEx _clientInput) {
     ClientInput = _clientInput;
 }
        public static Chart Read(Stream source)
        {
            BinaryReaderEx reader = new BinaryReaderEx(source);

            Chart result = new Chart();
            result.TickRate = new Fraction(1, 58);

            int[] lastSample = new int[16];
            int eventParameter = 0;
            int eventValue = 0;
            int eventType = 0;
            int eventOffset = 0;
            bool notecountMode = true;
            bool defaultBPMSet = false;

            while (true)
            {
                eventOffset = reader.ReadUInt16();
                eventType = reader.ReadByte();
                eventParameter = (eventType >> 4);
                eventType &= 0xF;
                eventValue = reader.ReadByte();

                // end of chart?
                if (eventOffset == 0x7FFF)
                    break;

                // ignore events in note count mode
                if (notecountMode)
                {
                    if (eventType != 0 || eventOffset > 0)
                        notecountMode = false;
                }

                // process events
                if (!notecountMode)
                {
                    Entry entry = new Entry();

                    entry.LinearOffset = new Fraction(eventOffset, 1);

                    switch (eventType)
                    {
                        case 0x0: // marker
                            switch (eventParameter)
                            {
                                case 0x0: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 0; entry.Value = new Fraction(lastSample[0x0], 1); break; //key0
                                case 0x1: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 0; entry.Value = new Fraction(lastSample[0x1], 1); break;
                                case 0x2: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 1; entry.Value = new Fraction(lastSample[0x2], 1); break; //key1
                                case 0x3: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 1; entry.Value = new Fraction(lastSample[0x3], 1); break;
                                case 0x4: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 2; entry.Value = new Fraction(lastSample[0x4], 1); break; //key2
                                case 0x5: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 2; entry.Value = new Fraction(lastSample[0x5], 1); break;
                                case 0x6: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 3; entry.Value = new Fraction(lastSample[0x6], 1); break; //key3
                                case 0x7: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 3; entry.Value = new Fraction(lastSample[0x7], 1); break;
                                case 0x8: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 4; entry.Value = new Fraction(lastSample[0x8], 1); break; //key4
                                case 0x9: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 4; entry.Value = new Fraction(lastSample[0x9], 1); break;
                                case 0xA: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 7; entry.Value = new Fraction(lastSample[0xA], 1); break; //scratch
                                case 0xB: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 7; entry.Value = new Fraction(lastSample[0xB], 1); break;
                                case 0xC: entry.Type = EntryType.Measure; entry.Player = 1; break;
                                case 0xD: entry.Type = EntryType.Measure; entry.Player = 2; break;
                                case 0xE: entry.Type = EntryType.Marker; entry.Player = 1; entry.Column = 8; entry.Value = new Fraction(lastSample[0xA], 1); break; //freescratch
                                case 0xF: entry.Type = EntryType.Marker; entry.Player = 2; entry.Column = 8; entry.Value = new Fraction(lastSample[0xB], 1); break;
                            }
                            break;
                        case 0x1: // sample
                            switch (eventParameter)
                            {
                                case 0x0: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 0; lastSample[0x0] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x1: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 0; lastSample[0x1] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x2: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 1; lastSample[0x2] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x3: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 1; lastSample[0x3] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x4: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 2; lastSample[0x4] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x5: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 2; lastSample[0x5] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x6: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 3; lastSample[0x6] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x7: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 3; lastSample[0x7] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x8: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 4; lastSample[0x8] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0x9: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 4; lastSample[0x9] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0xA: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 7; lastSample[0xA] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0xB: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 7; lastSample[0xB] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0xE: entry.Type = EntryType.Sample; entry.Player = 1; entry.Column = 8; lastSample[0xA] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                                case 0xF: entry.Type = EntryType.Sample; entry.Player = 2; entry.Column = 8; lastSample[0xB] = eventValue; entry.Value = new Fraction(eventValue, 1); break;
                            }
                            break;
                        case 0x2: // tempo
                            entry.Type = EntryType.Tempo;
                            entry.Value = new Fraction((eventParameter * 256) + eventValue, 1);
                            if (!defaultBPMSet)
                            {
                                defaultBPMSet = true;
                                result.DefaultBPM = entry.Value;
                            }
                            break;
                        case 0x4: // end of song
                            entry.Type = EntryType.EndOfSong;
                            break;
                        case 0x5: // bgm
                            entry.Type = EntryType.Marker;
                            entry.Player = 0;
                            entry.Value = new Fraction(eventValue, 1);
                            break;
                        case 0x6: // judgement
                            entry.Type = EntryType.Judgement;
                            entry.Value = new Fraction(eventValue, 1);
                            entry.Parameter = eventParameter;
                            break;
                    }

                    if (entry.Type != EntryType.Invalid)
                        result.Entries.Add(entry);
                }
            }

            if (result.Entries.Count > 0)
            {
                result.Entries.Sort();
                result.CalculateMetricOffsets();
            }
            else
            {
                result = null;
            }

            return result;
        }
Exemple #19
0
 public static Stat Read(Stream source)
 {
     BinaryReaderEx reader = new BinaryReaderEx(source);
     Stat result = new Stat();
     result.Offset = reader.ReadInt32S();
     result.Length = reader.ReadInt32S();
     result.TimeStamp = reader.ReadInt32S();
     return result;
 }
Exemple #20
0
		/// <summary>
		/// Create new PE Object from strean
		/// </summary>
		/// <param name="stream"></param>
		public PE(Stream stream, bool load_full = false)
		{
			pe = new BinaryReaderEx(stream);
			
			try {
				pe.ReadStruct<IMAGE_DOS_HEADER>(out this.dosHeader);

				if (dosHeader.e_magic != 0x5A4D) // MZ
					throw new Exception("Invalid DOS header");

				pe.Seek(dosHeader.e_lfanew);

				UInt32 nt_sign = pe.ReadUInt32();

				if (nt_sign != 0x4550) // PE
					throw new Exception("Invalid PE signature");

				pe.ReadStruct<IMAGE_FILE_HEADER>(out fileHeader);

				ushort magic = pe.ReadUInt16();
				pe.Seek(-2, SeekOrigin.Current);
				
				switch (magic) {
					case (ushort)PeType.PE32:
						this.peType = PeType.PE32;
						pe.ReadStruct<IMAGE_OPTIONAL_HEADER32>(out this.optionalHeader32);
						break;

					case (ushort)PeType.PE64:
						this.peType = PeType.PE64;
						pe.ReadStruct<IMAGE_OPTIONAL_HEADER64>(out this.optionalHeader64);
						break;

					case (ushort)PeType.ROM:
						this.peType = PeType.ROM;
						break;

					default:
						throw new Exception("Invalid IMAGE_FILE_HEADER.Magic value");
				}
				
				if (this.peType != PeType.ROM) {
					this.imageSectionHeaders = pe.ReadStructArray<IMAGE_SECTION_HEADER>(fileHeader.NumberOfSections);
					
					this.loadSections();
					
					if (load_full)
						this.LoadFull();
				}
			
			} catch (Exception e) {
				pe = null;
				throw e;
			}
		}
        public override void Decompress(BinaryReaderEx reader, BinaryWriter writer, StreamElement element)
        {
            FloatStreamDecompressor decompressor = StreamDecompressors.GetFloatDecompressor(element.CompressionType);
            float x = decompressor(reader);
            float y = decompressor(reader);
            float z = decompressor(reader);

            writer.Write(x);
            writer.Write(y);
            writer.Write(z);
        }
 public override void Decompress(BinaryReaderEx reader, BinaryWriter writer, StreamElement element)
 {
     var decompressor = StreamDecompressors.GetUShortDecompressor(element.CompressionType);
     var val = decompressor(reader);
     writer.Write(val);
 }
 public override void Decompress(BinaryReaderEx reader, BinaryWriter writer, StreamElement element)
 {
     var decompressor = StreamDecompressors.GetIntDecompressor(element.CompressionType);
     int count = Math.Min(StreamDataType.MaxBonesPerVert, element.NumElements);
     for (var i = 0; i < count; i++)
     {
         var val = decompressor(reader);
         writer.Write(val);
     }
 }
Exemple #24
0
 //deserialize the object
 public CLabelFeatureData(BinaryReaderEx binReaderEx)
     : base(binReaderEx)
 {                       
     this.feature = binReaderEx.Read<DataMatrixSerialized<float>>();
 }		    
Exemple #25
0
        //deserialize the object
        public CLabelFeatureDataCoded(BinaryReaderEx binReaderEx)
            : base(binReaderEx)
        {
            this.codeBook = binReaderEx.Read<CodeBook>();

            bool featureCodedExist = binReaderEx.ReadBoolean();
            if (featureCodedExist)
            {                
                this.featureCoded = binReaderEx.Read<DataMatrixSerialized<ushort>>();
            }
        }
Exemple #26
0
        private static Entry[] DecodeTempoChunk(Chunk source, int measureUnit)
        {
            using (BinaryReaderEx reader = new BinaryReaderEx(new MemoryStream(source.Data)))
            {
                int count = reader.ReadInt32();
                int[] metric = new int[count];
                int[] linear = new int[count];

                for (int i = 0; i < count; i++)
                    metric[i] = reader.ReadInt32();
                for (int i = 0; i < count; i++)
                    linear[i] = reader.ReadInt32();

                Entry[] result = new Entry[count - 1];

                for (int i = 1; i < count; i++)
                {
                    Entry entry = new Entry();
                    int metricDiff = metric[i] - metric[i - 1];
                    int linearDiff = linear[i] - linear[i - 1];
                    Fraction metricValue = new Fraction(metricDiff * 60, measureUnit / 4);
                    Fraction linearValue = new Fraction(linearDiff, source.Parameter);
                    Fraction bpmValue = metricValue / linearValue;
                    entry.MetricMeasure = metric[i - 1] / measureUnit;
                    entry.MetricOffset = new Fraction(metric[i - 1] % measureUnit, measureUnit);
                    entry.LinearOffset = new Fraction(linear[i - 1], source.Parameter);
                    entry.Type = EntryType.Tempo;
                    entry.Value = bpmValue;
                    result[i - 1] = entry;
                }

                return result;
            }
        }
Exemple #27
0
 public CHD(Stream baseFile)
 {
     baseStream = baseFile;
     reader = new BinaryReaderEx(baseStream);
 }
Exemple #28
0
        public static byte[] ReadRaw(Stream source, Properties prop)
        {
            BinaryReaderEx reader = new BinaryReaderEx(source);
            byte[] tailMarker = { };
            int bytesPerSample = 1;

            switch (prop.SampleType & 0xC)
            {
                case 0x0:
                    tailMarker = tailMarker8Bit;
                    break;
                case 0x4:
                    tailMarker = tailMarker16Bit;
                    bytesPerSample = 2;
                    break;
                case 0x8:
                    tailMarker = tailMarker4Bit;
                    break;
                default:
                    throw new Exception("Unable to determine sample type.");
            }

            using (MemoryStream mem = new MemoryStream())
            {
                int tailMarkerLength = tailMarker.Length;
                byte[] buffer = new byte[tailMarkerLength];
                bool finished = false;
                int bufferPad = tailMarkerLength;

                while (!finished)
                {
                    // read in the next sample
                    for (int j = 0; j < bytesPerSample; j++)
                    {
                        if (bufferPad > 0)
                            bufferPad--;
                        else
                            mem.WriteByte(buffer[0]);

                        for (int i = 1; i < tailMarkerLength; i++)
                            buffer[i - 1] = buffer[i];

                        int inByte = source.ReadByte();
                        if (inByte < 0 || inByte > 255)
                            return new byte[] { };

                        buffer[tailMarkerLength - 1] = (byte)inByte;
                    }

                    // check the buffer against the tail marker
                    for (int i = 0; i <= tailMarkerLength; i++)
                    {
                        if (i == tailMarkerLength)
                        {
                            finished = true;
                            break;
                        }
                        if (buffer[i] != tailMarker[i])
                            break;
                    }
                }

                return mem.ToArray();
            }
        }
Exemple #29
0
            /*
            public static bool operator ==(Properties a, Properties b)
            {
                return (a.Channel == b.Channel &&
                    a.Flags == b.Flags &&
                    a.Frequency == b.Frequency &&
                    a.Offset == b.Offset &&
                    a.Panning == b.Panning &&
                    a.ReverbVolume == b.ReverbVolume &&
                    a.SampleType == b.SampleType &&
                    a.Volume == b.Volume
                    );
            }

            public static bool operator !=(Properties a, Properties b)
            {
                return (a.Channel != b.Channel ||
                    a.Flags != b.Flags ||
                    a.Frequency != b.Frequency ||
                    a.Offset != b.Offset ||
                    a.Panning != b.Panning ||
                    a.ReverbVolume != b.ReverbVolume ||
                    a.SampleType != b.SampleType ||
                    a.Volume != b.Volume
                    );
            }
            */
            public static Properties Read(Stream source)
            {
                BinaryReaderEx reader = new BinaryReaderEx(source);
                Properties result = new Properties();

                result.Channel = reader.ReadByte();
                result.Frequency = reader.ReadUInt16();
                result.ReverbVolume = reader.ReadByte();
                result.Volume = reader.ReadByte();
                result.Panning = reader.ReadByte();
                result.Offset = reader.ReadUInt24();
                result.SampleType = reader.ReadByte();
                result.Flags = reader.ReadByte();

                return result;
            }
Exemple #30
0
        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;
        }