示例#1
0
        private string GetTagStringData(MsvBox.SegmentType type)
        {
            string result     = "";
            int    foundCount = 0;

            foreach (MpegBox ma in msvBoxes)
            {
                MsvBox pa = new MsvBox(ma);
                foundCount++;
                try
                {
                    result = MsvBox.GetSegmentUnicodeStringData(type, pa.DataSegments);
                }
                catch (MsvTagNotFoundException)
                {
                    foundCount--;
                }
            }
            if (foundCount == 1)
            {
                return(result);
            }
            else if (foundCount > 1)
            {
                throw new MsvDecoderException("Multiple MSV " +
                                              Enum.GetName(typeof(MsvBox.SegmentType), type) + " tags found.");
            }
            else
            {
                throw new MsvTagNotFoundException("No MSV " +
                                                  Enum.GetName(typeof(MsvBox.SegmentType), type) + " tag found.");
            }
        }
示例#2
0
        internal void SetTagStringData(MsvBox.SegmentType type, string s)
        {
            // first get the old tag in order to verify its presence and length
            string oldTag = this.GetTagStringData(type);
            // lengthDelta is size difference in double-bytes between s and null-stripped oldTag
            int lengthDelta = (s.Length - (oldTag.Length - 1)) * 2;

            mpegFile.SetTagUnicodeStringData(type, s, lengthDelta);
            return;
        }
示例#3
0
        internal void SetTagData(MsvBox.SegmentType type, byte[] bytes, int lengthDelta)
        {
            // verify data is not too big to fit in an MSV data segment
            if (bytes.Length > 0xFFFF)
            {
                throw new MsvArgumentException("Size of data exceeds MSV tag capacity.");
            }

            // verify there is only one tag to write to throughout the entire file
            ArrayList al = this.GetMsvSegmentList();

            if (al.IndexOf((uint)type) == al.LastIndexOf((uint)type) &&
                al.Contains((uint)type))
            {
                SetTagData(this.BoxTree, type, bytes, lengthDelta, false);
                return;
            }
            throw new MsvDecoderException("Number of segment matches found was not equal to one.");
        }
示例#4
0
        private void SetTagData(ArrayList mpegBoxes, MsvBox.SegmentType type, byte[] bytes,
                                int lengthDelta, bool haveProcessedMdat)
        {
            foreach (MpegBox mb in mpegBoxes)
            {
                if (MpegBox.BoxType.mdat == mb.Type)
                {
                    haveProcessedMdat = true;
                }
                else if (MpegBox.BoxType.moov == mb.Type || MpegBox.BoxType.trak == mb.Type ||
                         MpegBox.BoxType.mdia == mb.Type || MpegBox.BoxType.minf == mb.Type ||
                         MpegBox.BoxType.stbl == mb.Type)
                {
                    SetTagData(mb.Children, type, bytes, lengthDelta, haveProcessedMdat);
                }
                else if (MpegBox.BoxType.stco == mb.Type)
                {
                    // if we've not yet encountered the mdat box, then it must come after the moov box
                    // and so we'll need to adjust all the chunk offsets to match their new locations
                    // if we are resizing the tag's length
                    if (!haveProcessedMdat && lengthDelta != 0)
                    {
                        // go to the start of the actual stco data
                        long dataStartPosition = mb.AbsoluteOffset() + 8;
                        this.br.BaseStream.Seek(dataStartPosition, System.IO.SeekOrigin.Begin);
                        if (0x00 != br.ReadByte())
                        {
                            throw new MsvEncoderException("Detected chunk offset box version is not supported.");
                        }
                        if (!BU.AreByteArraysEqual(new byte[] { 0x00, 0x00, 0x00 }, br.ReadBytes(3)))
                        {
                            throw new MsvEncoderException("Detected chunk offset box flags are not supported.");
                        }
                        UInt32 chunkCount      = BU.ReverseToUInt32(this.br.ReadBytes(4));
                        UInt32 tempChunkOffset = 0;
                        for (int i = 0; i < chunkCount; i++)
                        {
                            // get next chunk offset
                            tempChunkOffset = BU.ReverseToUInt32(this.br.ReadBytes(4));
                            // back pointer up, then rewrite chunk offset to reflect lengthDelta
                            this.br.BaseStream.Seek(-4, System.IO.SeekOrigin.Current);
                            this.bw.Write(BU.ReverseToBytes((UInt32)(tempChunkOffset + lengthDelta)));
                        }
                    }
                }
                else if (MpegBox.BoxUuidType.MsvTag == mb.UuidType)
                {
                    MsvBox pb = new MsvBox(mb);
                    foreach (MsvTagDataSegment ds in pb.DataSegments)
                    {
                        if ((uint)type == ds.type)
                        {
                            // go to the start of the actual data within ds
                            // this assumes that the structure preceding the data is always
                            // consistent between all segments having write support
                            long dataStartPosition = ds.absoluteOffset + 10;
                            this.br.BaseStream.Seek(dataStartPosition, System.IO.SeekOrigin.Begin);
                            // verify that we know what data we're about to overwrite
                            byte[] compare = this.br.ReadBytes(ds.SegmentData.Length);
                            if (!BU.AreByteArraysEqual(compare, ds.SegmentData))
                            {
                                throw new MsvEncoderException(
                                          "The filestream position was invalid while writing tag data.");
                            }

                            if (bytes.Length != ds.SegmentData.Length)
                            {
                                // we have to grow or shrink the file and adjust its size markers
                                long moveByteCount    = this.bw.BaseStream.Length - this.bw.BaseStream.Position;
                                int  moveBlockSize    = 131072;                               // move in 128kb blocks
                                int  partialBlockSize = (int)(moveByteCount % moveBlockSize); // remaining partial block
                                // verify that the supplied lengthDelta value is correct
                                // the length delta is a negative number when shrinking a tag
                                if (lengthDelta != bytes.Length - ds.SegmentData.Length)
                                {
                                    throw new MsvEncoderException(
                                              "lengthDelta supplied did not match old tag length found.");
                                }
                                byte[] buffer;       // buffer for moving blocks

                                if (lengthDelta > 0) // new tag is too big, so grow the file
                                {
                                    // grow the file the needed number of bytes before moving data out
                                    this.br.BaseStream.SetLength(this.bw.BaseStream.Length + lengthDelta);

                                    // need to shift subsequent data further down the file
                                    if (moveByteCount >= moveBlockSize)
                                    {
                                        // we have at least one full block to move
                                        for (int i = 0; i < moveByteCount / moveBlockSize; i++)
                                        {
                                            // move pointer to beginning of next block to be moved
                                            // blocks are moved in reverse, from EOF backward
                                            this.br.BaseStream.Seek((moveBlockSize * (i + 1) + lengthDelta) * -1,
                                                                    System.IO.SeekOrigin.End);
                                            // read in a block
                                            buffer = br.ReadBytes(moveBlockSize);
                                            // move pointer backwards to start of the block, minus the grow size
                                            this.br.BaseStream.Seek((moveBlockSize - lengthDelta) * -1,
                                                                    System.IO.SeekOrigin.Current);
                                            this.bw.Write(buffer);
                                            buffer = null;
                                        }
                                    }
                                    // move any remaining partial block
                                    if (0 != partialBlockSize)
                                    {
                                        // move pointer to start of partial block
                                        this.br.BaseStream.Seek(dataStartPosition + ds.segmentData.Length,
                                                                System.IO.SeekOrigin.Begin);
                                        // read in the partial block
                                        buffer = br.ReadBytes(partialBlockSize);
                                        // move pointer backwards to start of partial block, minus the grow size
                                        this.br.BaseStream.Seek((partialBlockSize - lengthDelta) * -1,
                                                                System.IO.SeekOrigin.Current);
                                        this.bw.Write(buffer);
                                        buffer = null;
                                    }
                                }
                                else if (lengthDelta < 0)  // new tag is too small, so shrink the file
                                {
                                    // need to shift subsequent data further up the file
                                    // move pointer to beginning of first block
                                    this.br.BaseStream.Seek(dataStartPosition + ds.segmentData.Length,
                                                            System.IO.SeekOrigin.Begin);

                                    if (moveByteCount >= moveBlockSize)
                                    {
                                        // we have at least one full block to move
                                        for (int i = 0; i < moveByteCount / moveBlockSize; i++)
                                        {
                                            // read in a block
                                            buffer = br.ReadBytes(moveBlockSize);
                                            // move pointer backwards to start of the block, plus the shrink size
                                            // remember that lengthDelta is a negative number for shrink operations
                                            this.br.BaseStream.Seek((moveBlockSize - lengthDelta) * -1,
                                                                    System.IO.SeekOrigin.Current);
                                            this.bw.Write(buffer);
                                            buffer = null;
                                            // block is moved, so advance pointer to the beginning of the
                                            // next block
                                            this.br.BaseStream.Seek(lengthDelta * -1,
                                                                    System.IO.SeekOrigin.Current);
                                        }
                                    }
                                    // move any remaining partial block
                                    if (0 != partialBlockSize)
                                    {
                                        // pointer should already be at start of partial block
                                        // read in the partial block
                                        buffer = br.ReadBytes(partialBlockSize);
                                        // move pointer backwards to start of the block, plus the shrink size
                                        // remember that lengthDelta is a negative number for shrink operations
                                        this.br.BaseStream.Seek((partialBlockSize - lengthDelta) * -1,
                                                                System.IO.SeekOrigin.Current);
                                        this.bw.Write(buffer);
                                        buffer = null;
                                    }
                                    // shrink the file the needed number of bytes after moving data in
                                    this.br.BaseStream.SetLength(this.bw.BaseStream.Length + lengthDelta);
                                }

                                // now fix up all the size markers
                                UInt32 temp32 = 0;
                                UInt16 temp16 = 0;
                                // this tag's size marker
                                this.br.BaseStream.Seek(ds.absoluteOffset, System.IO.SeekOrigin.Begin);
                                temp16 = BU.ReverseToUInt16(this.br.ReadBytes(2)); // tag size
                                this.br.BaseStream.Seek(-2, System.IO.SeekOrigin.Current);
                                if (temp16 != ds.size)
                                {
                                    throw new MsvEncoderException(
                                              "The filestream position was invalid while writing tag size.");
                                }
                                this.bw.Write(BU.ReverseToBytes((UInt16)(temp16 + lengthDelta)));
                                // the rest of the size markers we must first read and then change
                                // incase for some reason we're changing them multiple times

                                // both of this MSV box's external and internal size markers
                                this.br.BaseStream.Seek(mb.AbsoluteOffset(), System.IO.SeekOrigin.Begin);
                                temp32 = BU.ReverseToUInt32(this.br.ReadBytes(4)); // UUID box size
                                this.br.BaseStream.Seek(-4, System.IO.SeekOrigin.Current);
                                this.bw.Write(BU.ReverseToBytes((UInt32)(temp32 + lengthDelta)));
                                this.br.BaseStream.Seek(20, System.IO.SeekOrigin.Current);
                                temp32 = BU.ReverseToUInt32(this.br.ReadBytes(4)); // MSV internalSize
                                this.br.BaseStream.Seek(-4, System.IO.SeekOrigin.Current);
                                this.bw.Write(BU.ReverseToBytes((UInt32)(temp32 + lengthDelta)));

                                // size markers of this box's parent and all the parent's parents
                                MpegBox tempBox = mb.Parent;
                                while (tempBox != null)
                                {
                                    this.br.BaseStream.Seek(tempBox.AbsoluteOffset(),
                                                            System.IO.SeekOrigin.Begin);
                                    temp32 = BU.ReverseToUInt32(this.br.ReadBytes(4)); // tempBox size
                                    this.br.BaseStream.Seek(-4, System.IO.SeekOrigin.Current);
                                    this.bw.Write(BU.ReverseToBytes((UInt32)(temp32 + lengthDelta)));

                                    tempBox = tempBox.Parent;
                                }
                            }

                            // set reader back to start of tag data we wish to overwrite
                            this.br.BaseStream.Seek(dataStartPosition, System.IO.SeekOrigin.Begin);
                            // now write the new data over the old data
                            this.bw.Write(bytes);
                            this.bw.BaseStream.Seek(dataStartPosition, System.IO.SeekOrigin.Begin);
                            // verify it worked
                            byte[] verify = this.br.ReadBytes(bytes.Length);
                            if (!BU.AreByteArraysEqual(verify, bytes))
                            {
                                throw new MsvEncoderException("The data write failed verification.");
                            }
                            // make sure all the data changes get flushed to disk
                            this.bw.Flush();
                        }
                    }
                }
            }
            return;
        }
示例#5
0
 internal void SetTagUnicodeStringData(MsvBox.SegmentType type, string stringData, int lengthDelta)
 {
     System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding(true, false);
     byte[] dataBytes = encoding.GetBytes(stringData + '\x00');
     this.SetTagData(type, dataBytes, lengthDelta);
 }