Example #1
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x0);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x4 + (i * 0x2C)), // Offset
                        data.ReadUInt(0x8 + (i * 0x2C)), // Length
                        data.ReadString(0xC + (i * 0x2C), 36) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                return null;
            }
        }
Example #2
0
 /* Checks to see if the input stream is a Storybook archive */
 public override bool Check(ref Stream input, string filename)
 {
     try
     {
         return (input.ReadUInt(0x4).SwapEndian() == 0x10 &&
            (input.ReadUInt(0xC).SwapEndian() == 0xFFFFFFFF ||
             input.ReadUInt(0xC).SwapEndian() == 0x00000000));
     }
     catch
     {
         return false;
     }
 }
Example #3
0
 /* Checks to see if the input stream is a MRG archive */
 public override bool Check(ref Stream input, string filename)
 {
     try
     {
         return (input.ReadString(0x0, 4) == ArchiveHeader.TEX &&
             input.ReadUInt(0x4) != input.Length &&
             input.ReadUInt(0x4) != input.Length - 4);
     }
     catch
     {
         return false;
     }
 }
Example #4
0
 public static HashBlock[] Destream(Stream inputStream)
 {
     uint count = inputStream.ReadUInt();
     var hashBlocks = new HashBlock[count];
     for (int i = 0; i < count; ++i)
     {
         hashBlocks[i] = new HashBlock {Hash = new byte[16]};
         inputStream.Read(hashBlocks[i].Hash, 0, 16);
         hashBlocks[i].Length = inputStream.ReadInt();
         hashBlocks[i].Offset = inputStream.ReadLong();
         hashBlocks[i].Checksum = inputStream.ReadUInt();
     }
     return hashBlocks;
 }
Example #5
0
 protected override void VideoHandler(Stream packet)
 {
     var time = packet.ReadUInt();
     var length = (uint)packet.GetAvaliableByteCounts();
     if (_numberLostFragments > 0)
         _firstKeyFrame = false;
     if ((packet.ReadByte() & 0xF0) == 0x10)
         _firstKeyFrame = true;
     packet.Position--;
     if (!_firstKeyFrame)
     {
         //丢失关键帧
         return;
     }
     _numberLostFragments = 0;
     if (_publisher != null && _publisher.PublisherId == StreamId)
     {
         //_publication.PushVideoPacket(packet.ReadUInt32(),packet,_numberLostFragments);
         _publisher.FeedData(packet, length, 0, length, time, false);
     }
     else if (OutStream != null)
     {
         TotalBytes += length;
         OutStream.FeedData(packet, length, 0, length, time, false);
     }
 }
Example #6
0
 protected override void AudioHandler(Stream packet)
 {
     if (_publisher != null && _publisher.PublisherId == StreamId)
     {
         var time = packet.ReadUInt();
         var length = (uint)packet.GetAvaliableByteCounts();
        // _publication.PushAudioPacket(packet.ReadUInt32(), packet, _numberLostFragments);
         _publisher.FeedData(packet, length, 0, length, time, true);
         _numberLostFragments = 0;
     }else if (OutStream != null)
     {
         var time = packet.ReadUInt();
         var length = (uint)packet.GetAvaliableByteCounts();
         TotalBytes += length;
         OutStream.FeedData(packet, length, 0, length, time, true);
     }
 }
Example #7
0
 public virtual bool LoadFrom(Stream stream)
 {
     uint count = stream.ReadUInt();
     for (int i = 0; i < count; i++)
     {
         var key = stream.ReadString();
         var val = stream.ReadString();
         KeyValues.Add(key, val);
     }
     return true;
 }
Example #8
0
 public override void SendStream(Stream stream,int length)
 {
     var marker = stream.ReadByte() | 0xF0;
     var echoTime = marker == (Target == null ? 0xFE : 0xFD);
     stream.ReadUShort();
     if (echoTime) stream.ReadUShort();
     var type = stream.ReadByte();
     if (type == 0x10)
     {
         var sizePos = stream.Position;
         var size = stream.ReadUShort();
         var flags = stream.ReadByte();
         var idFlow = stream.Read7BitLongValue();
         var stage = stream.Read7BitLongValue();
         if (idFlow == 2 && stage == 1)
         {
             var deltaNAck = stream.Read7BitLongValue();
             var len = (ushort) stream.ReadByte();
             stream.Position += len;
             stream.ReadByte();
             stream.ReadByte();//type
             stream.ReadUInt();//timestamp
             var amfReader = new AMF0Reader(stream);
             var str = amfReader.ReadShortString(true);
             var num = amfReader.ReadAMFDouble(true);
             var pos = stream.Position;
             var connectionInfo = amfReader.ReadVariant();
             connectionInfo["tcUrl"] = MiddleSession.QueryUrl;
             connectionInfo["app"] = MiddleSession.QueryUrl.Split('/').Last();
             stream.Position = pos;
             var amfWriter = new AMF0Writer(stream);
             amfWriter.WriteObject(connectionInfo, true);
             length = (int) stream.Position;
             len = (ushort) (stream.Position - sizePos-2);
             stream.Position = sizePos;
             stream.Write(len);
         }
     }
     stream.Position = 6;
     base.SendStream(stream,length);
 }
Example #9
0
        public Bitmap DecodeFrame(int frameIndex, Stream inStream)
        {
            if (inStream == null) return null;

            try
            {
                // Check the frame header:
                if (inStream.ReadUInt() != MAGIC_FRAME) return null;

                int encodedFrameIndex = inStream.ReadSShort();
                if (encodedFrameIndex != frameIndex) return null;

                BitmapData currentData = currentFrame.LockBits(
                    new Rectangle(0, 0, frameWidth, frameHeight),
                    ImageLockMode.ReadOnly, currentFrame.PixelFormat);
                BitmapData previousData = previousFrame.LockBits(
                    new Rectangle(0, 0, frameWidth, frameHeight),
                    ImageLockMode.ReadOnly, previousFrame.PixelFormat);

                if (VisualizeMCBlockTypes)
                {
                    debugFrame = new Bitmap(frameWidth, frameHeight, PixelFormat.Format24bppRgb);
                    debugFrameData = debugFrame.LockBits(
                        new Rectangle(0, 0, frameWidth, frameHeight),
                        ImageLockMode.ReadOnly, debugFrame.PixelFormat);
                }

                int pixelBytes = GetBytesPerPixel(currentFrame.PixelFormat);

                // TODO: handle bad value
                FrameType frameType = (FrameType)inStream.ReadUByte();
                switch (frameType)
                {
                    case FrameType.Intra:
                        DecodeIntraFrame(inStream, currentData, pixelBytes);
                        break;
                    case FrameType.Predicted:
                        DecodePredictedFrame(inStream, currentData, previousData, pixelBytes);
                        break;
                }

                currentFrame.UnlockBits(currentData);
                previousFrame.UnlockBits(previousData);

                if (VisualizeMCBlockTypes)
                {
                    debugFrame.UnlockBits(debugFrameData);
                    debugFrame.Save(String.Format("debug{0:000000}.png", frameIndex), ImageFormat.Png);
                    debugFrame.Dispose();
                }
            }
            catch (EndOfStreamException ex)
            {
                Log("Exception: {0} {1}", ex.Message, ex.StackTrace);
                return null;
            }

            Bitmap result = currentFrame;
            // double buffering
            // save the current bitmap to act as previous one when decoding the next frame
            SwapBitmaps(ref previousFrame, ref currentFrame);
            //currentFrame.Dispose();

            return result;
        }
Example #10
0
        /* Decompress */
        public override MemoryStream Decompress(ref Stream data)
        {
            try
            {
                // Compressed & Decompressed Data Information
                uint CompressedSize   = data.ReadUInt(0x4);
                uint DecompressedSize = data.ReadUInt(0x8);

                byte[] CompressedData   = data.ToByteArray();
                byte[] DecompressedData = new byte[DecompressedSize];
                byte[] DestBuffer       = new byte[0x1000];

                uint SourcePointer = 0x10;
                uint DestPointer   = 0x0;
                uint BufferPointer = 0xFEE;

                // Start Decompression
                while (SourcePointer < CompressedSize && DestPointer < DecompressedSize)
                {
                    byte Flag = CompressedData[SourcePointer]; // Compression Flag
                    SourcePointer++;

                    for (int i = 0; i < 8; i++)
                    {
                        if ((Flag & (1 << i)) > 0) // Data is not compressed
                        {
                            DecompressedData[DestPointer] = CompressedData[SourcePointer];
                            DestBuffer[BufferPointer]     = DecompressedData[DestPointer];
                            SourcePointer++;
                            DestPointer++;
                            BufferPointer = (BufferPointer + 1) & 0xFFF;
                        }
                        else // Data is compressed
                        {
                            int Offset = ((((CompressedData[SourcePointer + 1] >> 4) & 0xF) << 8) | CompressedData[SourcePointer]);
                            int Amount = (CompressedData[SourcePointer + 1] & 0xF) + 3;
                            SourcePointer += 2;

                            for (int j = 0; j < Amount; j++)
                            {
                                DecompressedData[DestPointer + j] = DestBuffer[(Offset + j) & 0xFFF];
                                DestBuffer[BufferPointer]         = DecompressedData[DestPointer + j];
                                BufferPointer = (BufferPointer + 1) & 0xFFF;
                            }
                            DestPointer += (uint)Amount;
                        }

                        // Check for out of range
                        if (SourcePointer >= CompressedSize || DestPointer >= DecompressedSize)
                            break;
                    }
                }

                return new MemoryStream(DecompressedData);
            }
            catch
            {
                return null; // An error occured while decompressing
            }
        }
Example #11
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                ushort files = data.ReadUShort(0x0);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (int i = 0; i < files; i++)
                {
                    /* Get the filename */
                    string filename = data.ReadString(0xA + (i * 0x24), 28);

                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x2 + (i * 0x24)), // Offset
                        data.ReadUInt(0x6 + (i * 0x24)), // Length
                        (filename == String.Empty ? String.Empty : filename + (filename.IsAllUpperCase() ? ".PVR" : ".pvr")) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #12
0
        public override bool LoadFrom(Stream stream)
        {
            base.LoadFrom(stream);
            Attribute.LoadFrom(stream);
            BaseTypeName = stream.ReadString();

            //types
            uint typeCount = stream.ReadUInt();
            for (int i = 0; i < typeCount; i++)
            {
                byte isClass = (byte)stream.ReadByte();
                if (isClass == 1)
                {
                    SirenCustomClass type = new SirenCustomClass();
                    type.LoadFrom(stream);
                    type.Parent = this;
                    Types.Add(type.Name, type);
                }
                else
                {
                    SirenCustomEnum type = new SirenCustomEnum();
                    type.LoadFrom(stream);
                    type.Parent = this;
                    Types.Add(type.Name, type);
                }
            }
            //fields
            uint fieldCount = stream.ReadUInt();
            for (int i = 0; i < fieldCount; i++)
            {
                SirenField field = new SirenField();
                field.LoadFrom(stream);
                field.ParentType = this;
                FieldNameDict.Add(field.Name, field);
                field.Index = (ushort)(FieldNameDict.Count - 1);
            }

            return true;
        }
Example #13
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x30);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* See if the archive contains filenames */
                bool containsFilenames = (files > 0 && data.ReadUInt(0x3C + (files * 0x14)) + 0x20 != 0x3C + (files * 0x1C) && data.ReadString(0x3C + (files * 0x1C), 4) == "FLST");

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    /* Get the offset & length */
                    uint offset = data.ReadUInt(0x40 + (files * 0x14) + (i * 0x8)) + 0x20;
                    uint length = data.ReadUInt(0x3C + (files * 0x14) + (i * 0x8));

                    /* Check for filenames */
                    string filename = String.Empty;
                    if (containsFilenames)
                        filename = data.ReadString(0x40 + (files * 0x1C) + (i * 0x40), 64);

                    /* GIM files can also contain their original filename in the footer */
                    if (filename == string.Empty && length > 40 && data.ReadString(offset, 12, false) == GraphicHeader.MIG)
                    {
                        uint filenameOffset = data.ReadUInt(offset + 0x24) + 0x30;
                        if (filenameOffset < length)
                            filename = Path.GetFileNameWithoutExtension(data.ReadString(offset + filenameOffset, (int)(length - filenameOffset)));

                        if (filename != String.Empty)
                            filename += (filename.IsAllUpperCase() ? ".GIM" : ".gim");
                    }

                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        offset,  // Offset
                        length,  // Length
                        filename // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #14
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x4).SwapEndian();

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    string filename = data.ReadString(0x10 + (i * 0x28), 32);

                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x08 + (i * 0x28)).SwapEndian(), // Offset
                        data.ReadUInt(0x0C + (i * 0x28)).SwapEndian(), // Length
                        (filename == String.Empty ? String.Empty : filename + (filename.IsAllUpperCase() ? ".GVR" : ".gvr")) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #15
0
        /* To simplify the process greatly, we are going to convert
         * the GVM to a new format */
        public override MemoryStream TranslateData(ref Stream stream)
        {
            try
            {
                /* Get the number of files, and format type in the stream */
                ushort files = stream.ReadUShort(0xA).SwapEndian();
                byte formatType = stream.ReadByte(0x9);

                /* Now let's see what information is contained inside the metadata */
                bool containsFilename    = (formatType & (1 << 3)) > 0;
                bool containsPixelFormat = (formatType & (1 << 2)) > 0;
                bool containsDimensions  = (formatType & (1 << 1)) > 0;
                bool containsGlobalIndex = (formatType & (1 << 0)) > 0;

                /* Let's figure out the metadata size */
                int size_filename = 0, size_pixelFormat = 0, size_dimensions = 0, size_globalIndex = 0;
                if (containsFilename)    size_filename = 28;
                if (containsPixelFormat) size_pixelFormat = 2;
                if (containsDimensions)  size_dimensions = 2;
                if (containsGlobalIndex) size_globalIndex = 4;
                int metaDataSize = 2 + size_filename + size_pixelFormat + size_dimensions + size_globalIndex;

                /* Now create the header */
                MemoryStream data = new MemoryStream();
                data.Write(files);

                /* Ok, try to find out data */
                uint sourceOffset = stream.ReadUInt(0x4) + 0x8;

                /* Write each file in the header */
                uint offset = 0x2 + ((uint)files * 0x24);
                for (int i = 0; i < files; i++)
                {
                    /* Ok, get the size of the GVR file */
                    uint length = stream.ReadUInt(sourceOffset + 0x4) + 8;

                    /* Make sure this is a valid file length */
                    if (sourceOffset + length > stream.Length)
                        length -= 16; // For some reason some GVR files are like this.
                    if (sourceOffset + length > stream.Length)
                        throw new Exception();

                    /* Write the offset, file length, and filename */
                    data.Write(offset);      // Offset
                    data.Write(length + 16); // Length

                    if (containsFilename)
                        data.Write(stream.ReadString(0xE + (i * metaDataSize), 28), 28); // Filename
                    else
                        data.Position += 28;

                    /* Add the GBIX header */
                    data.Position = offset;
                    data.Write(GraphicHeader.GBIX);
                    data.Write((int)0x8);

                    /* Copy the global index */
                    if (containsGlobalIndex)
                        data.Write(stream.ReadUInt(0xE + size_filename + size_pixelFormat + size_dimensions + (i * metaDataSize)));
                    else
                        data.Position += 4;

                    /* Write out the 0x0 in the header */
                    data.Write(new byte[] { 0x0, 0x0, 0x0, 0x0 });

                    /* Now copy the file */
                    data.Write(stream, sourceOffset, length);
                    data.Position = 0x26 + (i * 0x24);

                    sourceOffset += length.RoundUp(16);

                    /* Increment the offset */
                    offset += length + 16;
                }

                return data;
            }
            catch
            {
                /* Something went wrong, so send as blank stream */
                return new MemoryStream();
            }
        }
Example #16
0
        // Get file list containing the entries in the archive
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x4).SwapEndian();

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* See if the archive contains filenames */
                bool containsFilenames = (files > 0 && data.ReadUInt(0x8) != 0x8 + (files * 0x8) && data.ReadString(0x8 + (files * 0x8), 4) == "FLST");

                /* Now we can get the file offsets, lengths, and filenames */
                for (int i = 0; i < files; i++)
                {
                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x8 + (i * 0x8)).SwapEndian(), // Offset
                        data.ReadUInt(0xC + (i * 0x8)).SwapEndian(), // Length
                        (containsFilenames ? data.ReadString(0xC + (files * 0x8) + (i * 0x40), 64) : string.Empty) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #17
0
        /* To simplify the process greatly, we are going to convert
         * the Storybook Archive to a new format */
        public override MemoryStream TranslateData(ref Stream stream)
        {
            try
            {
                /* Get the number of files */
                uint files = stream.ReadUInt(0x0).SwapEndian();

                /* Now create the header */
                MemoryStream data = new MemoryStream();
                data.Write(files);

                /* Write each file in the header */
                uint offset = 0xC + (files * 0x2C);
                for (int i = 0; i < files; i++)
                {
                    uint length = stream.ReadUInt(0x3C + (i * 0x30)).SwapEndian();

                    data.Write(offset); // Offset
                    data.Write(length); // Length
                    data.Write(stream.ReadString(0x10 + (i * 0x30), 36), 36); // Filename

                    /* Let's write the decompressed data */
                    uint sourceOffset     = stream.ReadUInt(0x34 + (i * 0x30)).SwapEndian();
                    uint sourceLength     = stream.ReadUInt(0x38 + (i * 0x30)).SwapEndian();
                    Stream compressedData = stream.Copy(sourceOffset, sourceLength);

                    /* Decompress the data */
                    PRS decompressor = new PRS();
                    MemoryStream decompressedData = decompressor.Decompress(ref compressedData, length);
                    if (decompressedData == null)
                        throw new Exception();

                    /* Write the data */
                    data.Position = offset;
                    data.Write(decompressedData);
                    data.Position = 0x30 + (i * 0x2C);
                    decompressedData.Close();

                    offset += length;
                }

                return data;
            }
            catch
            {
                return new MemoryStream();
            }
        }
Example #18
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x0);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (int i = 0; i < files; i++)
                {
                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x14   + (i * 0x18)) * 0x800, // Offset
                        data.ReadUInt(0x18   + (i * 0x18)),         // Length
                        data.ReadString(0x04 + (i * 0x18), 16)      // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #19
0
 public override bool LoadFrom(Stream stream)
 {
     Mode = (SirenFieldGenerateMode)stream.ReadUInt();
     return true;
 }
Example #20
0
 public override bool LoadFrom(Stream stream)
 {
     base.LoadFrom(stream);
     Attribute.LoadFrom(stream);
     UnderlyType = stream.ReadString();
     uint count = stream.ReadUInt();
     for (int i = 0; i < count; i++)
     {
         FieldNames.Add(stream.ReadString());
         FieldValues.Add(stream.ReadInt());
     }
     return true;
 }
Example #21
0
 public override bool LoadFrom(Stream stream)
 {
     Mode = (SirenClassGenerateMode)stream.ReadUInt();
     Dir = stream.ReadString();
     return true;
 }
Example #22
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the offset of each section of the NARC file */
                uint offset_fatb = data.ReadUShort(0xC);
                uint offset_fntb = offset_fatb + data.ReadUInt(offset_fatb + 0x4);
                uint offset_fimg = offset_fntb + data.ReadUInt(offset_fntb + 0x4);

                /* Stuff for filenames */
                bool containsFilenames = (data.ReadUInt(offset_fntb + 0x8) == 8);
                uint offset_filename   = offset_fntb + 0x10;

                /* Get the number of files */
                uint files = data.ReadUInt(offset_fatb + 0x8);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    /* Get the offset & length */
                    uint offset = data.ReadUInt(offset_fatb + 0x0C + (i * 0x8));
                    uint length = data.ReadUInt(offset_fatb + 0x10 + (i * 0x8)) - offset;

                    /* Get the filename, if the NARC contains filenames */
                    string filename = String.Empty;
                    if (containsFilenames)
                    {
                        /* Ok, since the NARC contains filenames, let's go grab it now */
                        byte filename_length = data.ReadByte(offset_filename);
                        filename             = data.ReadString(offset_filename + 1, filename_length);
                        offset_filename     += (uint)(filename_length + 1);
                    }

                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        offset + offset_fimg + 0x8, // Offset
                        length,  // Length
                        filename // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #23
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x4);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Find the metadata location */
                uint metadataLocation = data.ReadUInt((files * 0x8) + 0x8);
                if (metadataLocation == 0x0)
                    metadataLocation = data.ReadUInt(data.ReadUInt(0x8) - 0x8);

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x8 + (i * 0x8)), // Offset
                        data.ReadUInt(0xC + (i * 0x8)), // Length
                        (metadataLocation == 0x0 ? String.Empty : data.ReadString(metadataLocation + (i * 0x30), 32)) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
Example #24
0
        /* Decompress */
        public override MemoryStream Decompress(ref Stream data)
        {
            try
            {
                // Compressed & Decompressed Data Information
                uint CompressedSize   = (uint)data.Length;
                uint DecompressedSize = data.ReadUInt(0x0) >> 8;

                uint SourcePointer = 0x4;
                uint DestPointer   = 0x0;

                if (DecompressedSize == 0) // Next 4 bytes are the decompressed size
                {
                    DecompressedSize = data.ReadUInt(0x4);
                    SourcePointer += 0x4;
                }

                byte[] CompressedData   = data.ToByteArray();
                byte[] DecompressedData = new byte[DecompressedSize];

                // Start Decompression
                while (SourcePointer < CompressedSize && DestPointer < DecompressedSize)
                {
                    byte Flag = CompressedData[SourcePointer]; // Compression Flag
                    SourcePointer++;

                    for (int i = 7; i >= 0; i--)
                    {
                        if ((Flag & (1 << i)) == 0) // Data is not compressed
                        {
                            DecompressedData[DestPointer] = CompressedData[SourcePointer];
                            SourcePointer++;
                            DestPointer++;
                        }
                        else // Data is compressed
                        {
                            int Distance;
                            int Amount;

                            // Let's determine how many bytes the distance & length pair take up
                            switch (CompressedData[SourcePointer] >> 4)
                            {
                                case 0: // 3 bytes
                                    Distance = (((CompressedData[SourcePointer + 1] & 0xF) << 8) | CompressedData[SourcePointer + 2]) + 1;
                                    Amount   = (((CompressedData[SourcePointer] & 0xF) << 4) | (CompressedData[SourcePointer + 1] >> 4)) + 17;
                                    SourcePointer += 3;
                                    break;

                                case 1: // 4 bytes
                                    Distance = (((CompressedData[SourcePointer + 2] & 0xF) << 8) | CompressedData[SourcePointer + 3]) + 1;
                                    Amount   = (((CompressedData[SourcePointer] & 0xF) << 12) | (CompressedData[SourcePointer + 1] << 4) | (CompressedData[SourcePointer + 2] >> 4)) + 273;
                                    SourcePointer += 4;
                                    break;

                                default: // 2 bytes
                                    Distance = (((CompressedData[SourcePointer] & 0xF) << 8) | CompressedData[SourcePointer + 1]) + 1;
                                    Amount   = (CompressedData[SourcePointer] >> 4) + 1;
                                    SourcePointer += 2;
                                    break;
                            }

                            // Copy the data
                            for (int j = 0; j < Amount; j++)
                                DecompressedData[DestPointer + j] = DecompressedData[DestPointer - Distance + j];
                            DestPointer += (uint)Amount;
                        }

                        // Check for out of range
                        if (SourcePointer >= CompressedSize || DestPointer >= DecompressedSize)
                            break;
                    }
                }

                return new MemoryStream(DecompressedData);
            }
            catch
            {
                return null; // An error occured while decompressing
            }
        }
Example #25
0
        /* Get the offsets, lengths, and filenames of all the files */
        public override ArchiveFileList GetFileList(ref Stream data)
        {
            try
            {
                /* Get the number of files */
                uint files = data.ReadUInt(0x4);

                /* Create the array of files now */
                ArchiveFileList fileList = new ArchiveFileList(files);

                /* Now we can get the file offsets, lengths, and filenames */
                for (uint i = 0; i < files; i++)
                {
                    /* Get filename and extension */
                    string filename = data.ReadString(0x20 + (i * 0x30), 32, Encoding.GetEncoding("Shift_JIS")); // Name
                    string fileext  = data.ReadString(0x10 + (i * 0x30), 4);  // Extension

                    fileList.Entries[i] = new ArchiveFileList.Entry(
                        data.ReadUInt(0x14 + (i * 0x30)), // Offset
                        data.ReadUInt(0x18 + (i * 0x30)), // Length
                        (filename == String.Empty ? String.Empty : filename) + (fileext == string.Empty ? string.Empty : '.' + fileext) // Filename
                    );
                }

                return fileList;
            }
            catch
            {
                /* Something went wrong, so return nothing */
                return null;
            }
        }
        protected override bool FeedDataVideo(Stream pData, uint dataLength, uint processedLength, uint totalLength, uint absoluteTimestamp,
            bool isAudio)
        {
            var pos = pData.Position;
            _videoBytesCount += dataLength;
            _videoPacketsCount++;
            //1. Test and see if this is an inbound RTMP stream. If so,
            //we have to strip out the RTMP 9 bytes header
            var inStreamType = InStream.Type;
            if ((inStreamType == ST_IN_NET_RTMP)
                    || (inStreamType == ST_IN_NET_LIVEFLV))
            {
                //2. Test and see if we have a brand new packet
                if (processedLength == 0)
                {
                    //3.This must be a payload packet, not codec setup
                    pData.ReadByte();

                    if (pData.ReadByte() != 1)
                        return true;
                    //4. since this is a brand new packet, empty previous buffer
                    _videoBuffer.IgnoreAll();
                    pData.Position -= 2;
                }

                //5. Store the data into the buffer
                pData.CopyPartTo(_videoBuffer,(int) dataLength);

                //6. Test and see if this is the last chunk of the RTMP packet
                if (dataLength + processedLength == totalLength)
                {
                    //7. This is the last chunk. Get the pointer and length
                    pData = _videoBuffer;
                    pData.Position = 0;
                    dataLength = (uint) _videoBuffer.GetAvaliableByteCounts();

                    //8. We must have at least 9 bytes (RTMP header size)
                    if (dataLength < 9)
                    {
                        WARN("Bogus packet");
                        return true;
                    }

                    //9. Read the composition timestamp and add it to the
                    //absolute timestamp
                    pData.Position = 1;
                    var compositionTimeStamp = (pData.ReadUInt()) & 0x00ffffff;
                    absoluteTimestamp += compositionTimeStamp;
                    
                    //10. Ignore RTMP header and composition offset

                    dataLength -= 5;

                    uint nalSize = 0;
                    //uint32_t tsIncrement = 0;

                    //11. Start looping over the RTMP payload. Each NAL has a 4 bytes
                    //header indicating the length of the following NAL
                    while (dataLength >= 4)
                    {
                        //12. Read the nal size and compare it to the actual amount
                        //of data remaining on the buffer
                        nalSize = pData.ReadUInt();
                        pos = pData.Position;
                        if (nalSize > (dataLength - 4))
                        {
                            WARN("Bogus packet");
                            return true;
                        }

                        //13. skip theNAL size field
                       
                        dataLength -= 4;

                        //14. Is this a 0 sized NAL? if so, skip it
                        if (nalSize == 0)
                            continue;

                        //15. Feed the NAL unit using RTP FUA
                        if (!FeedDataVideoFUA(pData, nalSize, 0, nalSize,
                                absoluteTimestamp))
                        { //+ (double) tsIncrement / 90000.00)) {
                            FATAL("Unable to feed data");
                            return false;
                        }
                        //16. move to the next NAL
               
                        dataLength -= nalSize;
                    }
                }
                return true;
            }
            else
            {
                //17. This is NAL stream. Feed it as it is
                return FeedDataVideoFUA(pData, dataLength, processedLength, totalLength,absoluteTimestamp);
                        
            }
        }