Esempio n. 1
0
        public static void ArchiveToFileLocation(VirtualStream dataStream, string FullFilePath)
        {
            try
            {
                //string saveTo = "C:\\Projects\\Unicer.Archive\\ArchiveFolder\\teste.xml";
                // create a write stream
                FileStream   FileArchiveStream = new FileStream(FullFilePath, FileMode.Create, FileAccess.Write);
                BinaryWriter FileBinaryWriter  = new BinaryWriter(FileArchiveStream);

                // Read the stream and write to the archive file
                byte[] Buffer = new byte[256];
                int    SizeRead;
                while ((SizeRead = dataStream.Read(Buffer, 0, 256)) != 0)
                {
                    FileBinaryWriter.Write(Buffer, 0, SizeRead);
                }

                // Flush and close the output stream
                FileBinaryWriter.Flush();
                FileBinaryWriter.Close();
            }
            catch (IOException Ex)
            {
                //System.Diagnostics.EventLog.WriteEntry(EventData.EVENT_LOG_SOURCE, "An error occured creating the archive file - the file has not been archived.\n\nThe following error was raised: " + Ex.Message, EventLogEntryType.Error);
            }

            // dataStream.Seek(0, SeekOrigin.Begin); //(ERROR IS THROW HERE)
            // return (dataStream);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses the 0Table (or 1Table) for FKP _entries containing PAPX
        /// </summary>
        /// <param name="fib">The FileInformationBlock</param>
        /// <param name="wordStream">The WordDocument stream</param>
        /// <param name="tableStream">The 0Table stream</param>
        /// <returns></returns>
        public static List <FormattedDiskPagePAPX> GetAllPAPXFKPs(FileInformationBlock fib, VirtualStream wordStream, VirtualStream tableStream, VirtualStream dataStream)
        {
            List <FormattedDiskPagePAPX> list = new List <FormattedDiskPagePAPX>();

            //get bintable for PAPX
            byte[] binTablePapx = new byte[fib.lcbPlcfBtePapx];
            tableStream.Read(binTablePapx, 0, binTablePapx.Length, (int)fib.fcPlcfBtePapx);

            //there are n offsets and n-1 fkp's in the bin table
            int n = (((int)fib.lcbPlcfBtePapx - 4) / 8) + 1;

            //Get the indexed PAPX FKPs
            for (int i = (n * 4); i < binTablePapx.Length; i += 4)
            {
                //indexed FKP is the xth 512byte page
                int fkpnr = System.BitConverter.ToInt32(binTablePapx, i);

                //so starts at:
                int offset = fkpnr * 512;

                //parse the FKP and add it to the list
                list.Add(new FormattedDiskPagePAPX(wordStream, offset, dataStream));
            }

            return(list);
        }
Esempio n. 3
0
        public List <char> GetAllChars(VirtualStream wordStream)
        {
            List <char> chars = new List <char>();

            foreach (PieceDescriptor pcd in this.Pieces)
            {
                //get the FC end of this piece
                Int32 pcdFcEnd = pcd.cpEnd - pcd.cpStart;
                if (pcd.encoding == Encoding.Unicode)
                {
                    pcdFcEnd *= 2;
                }
                pcdFcEnd += (Int32)pcd.fc;

                int    cb    = pcdFcEnd - (Int32)pcd.fc;
                byte[] bytes = new byte[cb];

                //read all bytes
                wordStream.Read(bytes, 0, cb, (Int32)pcd.fc);

                //get the chars
                char[] plainChars = pcd.encoding.GetString(bytes).ToCharArray();

                //add to list
                foreach (char c in plainChars)
                {
                    chars.Add(c);
                }
            }
            return(chars);
        }
Esempio n. 4
0
        /// <summary>
        /// Parses the bytes to retrieve a AuthorTable
        /// </summary>
        /// <param name="bytes">The bytes</param>
        public AuthorTable(FileInformationBlock fib, VirtualStream tableStream)
        {
            int pos = 8;

            byte[]        uniChar = new byte[2];
            StringBuilder name    = new StringBuilder();

            while (pos < fib.lcbSttbfRMark)
            {
                tableStream.Read(uniChar, 0, 2, (int)(fib.fcSttbfRMark + pos));
                char cPos = Encoding.Unicode.GetString(uniChar).ToCharArray()[0];
                if ((int)cPos > 0x1F)
                {
                    name.Append(cPos);
                }
                else
                {
                    //there is a seperator that terminates this name
                    this.Add(name.ToString());
                    name = new StringBuilder();
                }
                pos += 2;
            }
            //add last name
            this.Add(name.ToString());
        }
        public FormattedDiskPageCHPX(VirtualStream wordStream, int offset)
        {
            this.Type       = FKPType.Character;
            this.WordStream = wordStream;

            //read the 512 bytes (FKP)
            var bytes = new byte[512];

            wordStream.Read(bytes, 0, 512, offset);

            //get the count first
            this.crun = bytes[511];

            //create and fill the array with the adresses
            this.rgfc = new int[this.crun + 1];
            int j = 0;

            for (int i = 0; i < this.rgfc.Length; i++)
            {
                this.rgfc[i] = System.BitConverter.ToInt32(bytes, j);
                j           += 4;
            }

            //create arrays
            this.rgb     = new byte[this.crun];
            this.grpchpx = new CharacterPropertyExceptions[this.crun];

            j = 4 * (this.crun + 1);
            for (int i = 0; i < this.rgb.Length; i++)
            {
                //fill the rgb array
                byte wordOffset = bytes[j];
                this.rgb[i] = wordOffset;
                j++;

                if (wordOffset != 0)
                {
                    //read first byte of CHPX
                    //it's the count of bytes
                    byte cb = bytes[wordOffset * 2];

                    //read the bytes of chpx
                    var chpx = new byte[cb];
                    Array.Copy(bytes, (wordOffset * 2) + 1, chpx, 0, chpx.Length);

                    //parse CHPX and fill grpchpx
                    this.grpchpx[i] = new CharacterPropertyExceptions(chpx);
                }
                else
                {
                    //create a CHPX which doesn't modify anything
                    this.grpchpx[i] = new CharacterPropertyExceptions();
                }
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            Init();

            if (m_stm.Length == 0)
            {
                Read(this.settings.RootName);
            }

            return(m_stm.Read(buffer, offset, count));
        }
Esempio n. 7
0
        /// <summary>
        /// Parses the streams to retrieve a StyleSheet.
        /// </summary>
        /// <param name="fib">The FileInformationBlock</param>
        /// <param name="tableStream">The 0Table or 1Table stream</param>
        public StyleSheet(FileInformationBlock fib, VirtualStream tableStream, VirtualStream dataStream)
        {
            IStreamReader tableReader = new VirtualStreamReader(tableStream);

            //read size of the STSHI
            var stshiLengthBytes = new byte[2];

            tableStream.Read(stshiLengthBytes, 0, stshiLengthBytes.Length, fib.fcStshf);
            short cbStshi = System.BitConverter.ToInt16(stshiLengthBytes, 0);

            //read the bytes of the STSHI
            var stshi = tableReader.ReadBytes(fib.fcStshf + 2, cbStshi);

            //parses STSHI
            this.stshi = new StyleSheetInformation(stshi);

            //create list of STDs
            this.Styles = new List <StyleSheetDescription>();
            for (int i = 0; i < this.stshi.cstd; i++)
            {
                //get the cbStd
                ushort cbStd = tableReader.ReadUInt16();

                if (cbStd != 0)
                {
                    //read the STD bytes
                    var std = tableReader.ReadBytes(cbStd);

                    //parse the STD bytes
                    this.Styles.Add(new StyleSheetDescription(std, (int)this.stshi.cbSTDBaseInFile, dataStream));
                }
                else
                {
                    this.Styles.Add(null);
                }
            }
        }
        /// <summary>
        /// Parses the bytes to retrieve a PAPX
        /// </summary>
        /// <param name="bytes">The bytes starting with the istd</param>
        public ParagraphPropertyExceptions(byte[] bytes, VirtualStream dataStream)
            : base(new List <byte>(bytes).GetRange(2, bytes.Length - 2).ToArray())
        {
            if (bytes.Length != 0)
            {
                this.istd = System.BitConverter.ToUInt16(bytes, 0);
            }

            //There is a SPRM that points to an offset in the data stream,
            //where a list of SPRM is saved.
            foreach (SinglePropertyModifier sprm in this.grpprl)
            {
                if (sprm.OpCode == SinglePropertyModifier.OperationCode.sprmPHugePapx || (int)sprm.OpCode == 0x6646)
                {
                    IStreamReader reader = new VirtualStreamReader(dataStream);
                    UInt32        fc     = System.BitConverter.ToUInt32(sprm.Arguments, 0);

                    //parse the size of the external grpprl
                    byte[] sizebytes = new byte[2];
                    dataStream.Read(sizebytes, 0, 2, (int)fc);
                    UInt16 size = System.BitConverter.ToUInt16(sizebytes, 0);

                    //parse the external grpprl
                    //byte[] grpprlBytes = new byte[size];
                    //dataStream.Read(grpprlBytes);
                    byte[]             grpprlBytes = reader.ReadBytes(size);
                    PropertyExceptions externalPx  = new PropertyExceptions(grpprlBytes);

                    //assign the external grpprl
                    this.grpprl = externalPx.grpprl;

                    //remove the sprmPHugePapx
                    this.grpprl.Remove(sprm);
                }
            }
        }
Esempio n. 9
0
        public FormattedDiskPagePAPX(VirtualStream wordStream, Int32 offset, VirtualStream dataStream)
        {
            this.Type       = FKPType.Paragraph;
            this.WordStream = wordStream;

            //read the 512 bytes (FKP)
            byte[] bytes = new byte[512];
            wordStream.Read(bytes, 0, 512, offset);

            //get the count
            this.crun = bytes[511];

            //create and fill the array with the adresses
            this.rgfc = new Int32[this.crun + 1];
            int j = 0;

            for (int i = 0; i < rgfc.Length; i++)
            {
                rgfc[i] = System.BitConverter.ToInt32(bytes, j);
                j      += 4;
            }

            //create arrays
            this.rgbx    = new BX[this.crun];
            this.grppapx = new ParagraphPropertyExceptions[this.crun];

            j = 4 * (this.crun + 1);
            for (int i = 0; i < rgbx.Length; i++)
            {
                //read the 12 for PHE
                byte[] phe = new byte[12];
                Array.Copy(bytes, j + 1, phe, 0, phe.Length);

                //fill the rgbx array
                BX bx = new BX();
                bx.wordOffset = bytes[j];
                bx.phe        = new ParagraphHeight(phe, false);
                rgbx[i]       = bx;
                j            += 13;

                if (bx.wordOffset != 0)
                {
                    //read first byte of PAPX
                    //PAPX is stored in a FKP; so the first byte is a count of words
                    byte padbyte = 0;
                    byte cw      = bytes[bx.wordOffset * 2];

                    //if that byte is zero, it's a pad byte, and the word count is the following byte
                    if (cw == 0)
                    {
                        padbyte = 1;
                        cw      = bytes[bx.wordOffset * 2 + 1];
                    }

                    if (cw != 0)
                    {
                        //read the bytes for papx
                        byte[] papx = new byte[cw * 2];
                        Array.Copy(bytes, (bx.wordOffset * 2) + padbyte + 1, papx, 0, papx.Length);

                        //parse PAPX and fill grppapx
                        this.grppapx[i] = new ParagraphPropertyExceptions(papx, dataStream);
                    }
                }
                else
                {
                    //create a PAPX which doesn't modify anything
                    this.grppapx[i] = new ParagraphPropertyExceptions();
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Parses the pice table and creates a list of PieceDescriptors.
        /// </summary>
        /// <param name="fib">The FIB</param>
        /// <param name="tableStream">The 0Table or 1Table stream</param>
        public PieceTable(FileInformationBlock fib, VirtualStream tableStream)
        {
            //Read the bytes of complex file information
            byte[] bytes = new byte[fib.lcbClx];
            tableStream.Read(bytes, 0, (int)fib.lcbClx, (int)fib.fcClx);

            this.Pieces = new List <PieceDescriptor>();
            this.FileCharacterPositions = new Dictionary <int, int>();
            this.CharacterPositions     = new Dictionary <int, int>();

            int  pos  = 0;
            bool goon = true;

            while (goon)
            {
                try
                {
                    byte type = bytes[pos];

                    //check if the type of the entry is a piece table
                    if (type == 2)
                    {
                        Int32 lcb = System.BitConverter.ToInt32(bytes, pos + 1);

                        //read the piece table
                        byte[] piecetable = new byte[lcb];
                        Array.Copy(bytes, pos + 5, piecetable, 0, piecetable.Length);

                        //count of PCD _entries
                        int n = (lcb - 4) / 12;

                        //and n piece descriptors
                        for (int i = 0; i < n; i++)
                        {
                            //read the CP
                            int   indexCp = i * 4;
                            Int32 cp      = System.BitConverter.ToInt32(piecetable, indexCp);

                            //read the next CP
                            int   indexCpNext = (i + 1) * 4;
                            Int32 cpNext      = System.BitConverter.ToInt32(piecetable, indexCpNext);

                            //read the PCD
                            int    indexPcd = ((n + 1) * 4) + (i * 8);
                            byte[] pcdBytes = new byte[8];
                            Array.Copy(piecetable, indexPcd, pcdBytes, 0, 8);
                            PieceDescriptor pcd = new PieceDescriptor(pcdBytes);
                            pcd.cpStart = cp;
                            pcd.cpEnd   = cpNext;

                            //add pcd
                            this.Pieces.Add(pcd);

                            //add positions
                            Int32 f     = (Int32)pcd.fc;
                            Int32 multi = 1;
                            if (pcd.encoding == Encoding.Unicode)
                            {
                                multi = 2;
                            }
                            for (int c = pcd.cpStart; c < pcd.cpEnd; c++)
                            {
                                if (!this.FileCharacterPositions.ContainsKey(c))
                                {
                                    this.FileCharacterPositions.Add(c, f);
                                }
                                if (!this.CharacterPositions.ContainsKey(f))
                                {
                                    this.CharacterPositions.Add(f, c);
                                }

                                f += multi;
                            }
                        }
                        int maxCp = this.FileCharacterPositions.Count;
                        this.FileCharacterPositions.Add(maxCp, fib.fcMac);
                        this.CharacterPositions.Add(fib.fcMac, maxCp);

                        //piecetable was found
                        goon = false;
                    }
                    //entry is no piecetable so goon
                    else if (type == 1)
                    {
                        Int16 cb = System.BitConverter.ToInt16(bytes, pos + 1);
                        pos = pos + 1 + 2 + cb;
                    }
                    else
                    {
                        goon = false;
                    }
                }
                catch (Exception)
                {
                    goon = false;
                }
            }
        }
Esempio n. 11
0
        public List <char> GetChars(Int32 fcStart, Int32 fcEnd, VirtualStream wordStream)
        {
            List <char> chars = new List <char>();

            for (int i = 0; i < this.Pieces.Count; i++)
            {
                PieceDescriptor pcd = this.Pieces[i];

                //get the FC end of this piece
                Int32 pcdFcEnd = pcd.cpEnd - pcd.cpStart;
                if (pcd.encoding == Encoding.Unicode)
                {
                    pcdFcEnd *= 2;
                }
                pcdFcEnd += (Int32)pcd.fc;

                if (pcdFcEnd < fcStart)
                {
                    //this piece is before the requested range
                    continue;
                }
                else if (fcStart >= pcd.fc && fcEnd > pcdFcEnd)
                {
                    //requested char range starts at this piece
                    //read from fcStart to pcdFcEnd

                    //get count of bytes
                    int    cb    = pcdFcEnd - fcStart;
                    byte[] bytes = new byte[cb];

                    //read all bytes
                    wordStream.Read(bytes, 0, cb, (Int32)fcStart);

                    //get the chars
                    char[] plainChars = pcd.encoding.GetString(bytes).ToCharArray();

                    //add to list
                    foreach (char c in plainChars)
                    {
                        chars.Add(c);
                    }
                }
                else if (fcStart <= pcd.fc && fcEnd >= pcdFcEnd)
                {
                    //the full piece is part of the requested range
                    //read from pc.fc to pcdFcEnd

                    //get count of bytes
                    int    cb    = pcdFcEnd - (Int32)pcd.fc;
                    byte[] bytes = new byte[cb];

                    //read all bytes
                    wordStream.Read(bytes, 0, cb, (Int32)pcd.fc);

                    //get the chars
                    char[] plainChars = pcd.encoding.GetString(bytes).ToCharArray();

                    //add to list
                    foreach (char c in plainChars)
                    {
                        chars.Add(c);
                    }
                }
                else if (fcStart < pcd.fc && fcEnd >= pcd.fc && fcEnd <= pcdFcEnd)
                {
                    //requested char range ends at this piece
                    //read from pcd.fc to fcEnd

                    //get count of bytes
                    int    cb    = fcEnd - (Int32)pcd.fc;
                    byte[] bytes = new byte[cb];

                    //read all bytes
                    wordStream.Read(bytes, 0, cb, (Int32)pcd.fc);

                    //get the chars
                    char[] plainChars = pcd.encoding.GetString(bytes).ToCharArray();

                    //add to list
                    foreach (char c in plainChars)
                    {
                        chars.Add(c);
                    }

                    break;
                }
                else if (fcStart >= pcd.fc && fcEnd <= pcdFcEnd)
                {
                    //requested chars are completly in this piece
                    //read from fcStart to fcEnd

                    //get count of bytes
                    int    cb    = fcEnd - fcStart;
                    byte[] bytes = new byte[cb];

                    //read all bytes
                    wordStream.Read(bytes, 0, cb, (Int32)fcStart);

                    //get the chars
                    char[] plainChars = pcd.encoding.GetString(bytes).ToCharArray();

                    //set the list
                    chars = new List <char>(plainChars);

                    break;
                }
                else if (fcEnd < pcd.fc)
                {
                    //this piece is beyond the requested range
                    break;
                }
            }
            return(chars);
        }
        private WebResponse SendHttpRequest(IBaseMessage msg, HttpAdapterProperties config)
        {
            #region SSO Usage Example
            // If we needed to use SSO to lookup the remote system credentials

            /*
             * string ssoAffiliateApplication = props.AffiliateApplication;
             * if (ssoAffiliateApplication.Length > 0)
             * {
             *  SSOResult ssoResult = new SSOResult(msg, affiliateApplication);
             *  string userName  = ssoResult.UserName;
             *  string password  = ssoResult.Result[0];
             *  string etcEtcEtc = ssoResult.Result[1]; // (you can have additional metadata associated with the login in SSO)
             *
             *  // use these credentials to login to the remote system
             *  // ideally zero off the password memory once we are done
             * }
             */
            #endregion // SSO Usage Example

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(config.Uri);
            request.Method  = config.HttpVerb;
            request.Timeout = config.Timeout;

            // Configure the authentication type...
            if (HttpAdapterProperties.AuthenticationType.KerberosAuthentication == config.AuthType)
            {
                request.AllowWriteStreamBuffering = true;
                request.PreAuthenticate           = false;
                request.Credentials = CredentialCache.DefaultCredentials;
            }
            else
            {
                request.AllowWriteStreamBuffering = false;
                request.PreAuthenticate           = true;
            }

            // Note: both the body part and the stream maybe null, so we need to
            // check for them
            string           charset  = string.Empty;
            IBaseMessagePart bodyPart = msg.BodyPart;
            Stream           btsStream;

            if (null != bodyPart && (null != (btsStream = bodyPart.GetOriginalDataStream())))
            {
                charset = bodyPart.Charset;
                string contentTypeCharset = config.ContentType;

                if (contentTypeCharset.ToLowerInvariant().StartsWith("text/"))
                {
                    // Append charset information (if it's not there already)!
                    if (charset != null && charset.Length > 0 && contentTypeCharset.ToLowerInvariant().IndexOf("charset=") == -1)
                    {
                        contentTypeCharset += "; charset=" + charset;
                    }
                }
                request.ContentType = contentTypeCharset;

                // Wrap the BTS stream in the seekable stream to obtain the content-legnth!
                btsStream             = new VirtualStream(btsStream);
                request.ContentLength = btsStream.Length;

                // Get the request stream and write the data into it...
                Stream webStream = request.GetRequestStream();
                int    bytesRead = 0;
                byte[] buff      = new byte[IO_BUFFER_SIZE];
                while ((bytesRead = btsStream.Read(buff, 0, IO_BUFFER_SIZE)) > 0)
                {
                    webStream.Write(buff, 0, bytesRead);
                }

                webStream.Flush();
                webStream.Close();
            }

            return(request.GetResponse());
        }