Beispiel #1
0
        public void Finish(string title, string author, string comments)
        {
            this.m_charFormat.Finish(this.m_wordText.CurrentCp);
            this.m_parFormat.Finish(this.m_wordText.CurrentCp);
            byte[]       array         = new byte[FileInformationBlock.FieldsSize];
            BinaryWriter binaryWriter  = new BinaryWriter(this.m_tableStream);
            BinaryWriter binaryWriter2 = new BinaryWriter(this.m_mainStream);

            binaryWriter2.Write(FileInformationBlock.StartBuffer, 0, FileInformationBlock.StartBuffer.Length);
            binaryWriter2.Write(new byte[2], 0, 2);
            binaryWriter2.Write(array);
            binaryWriter2.Write(FileInformationBlock.EndBuffer, 0, FileInformationBlock.EndBuffer.Length);
            binaryWriter.Flush();
            int num = (int)binaryWriter.BaseStream.Position;

            LittleEndian.PutInt(array, this.GetFcField(33), num);
            this.m_wordText.WriteClxTo(binaryWriter, this.m_fcStart);
            LittleEndian.PutInt(array, this.GetLcbField(33), (int)binaryWriter.BaseStream.Position - num);
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(1), num);
            binaryWriter.Write(StyleSheet.Buffer, 0, StyleSheet.Buffer.Length);
            binaryWriter.Flush();
            LittleEndian.PutInt(array, this.GetLcbField(1), (int)binaryWriter.BaseStream.Position - num);
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(31), num);
            binaryWriter.Write(DocumentProperties.Buffer, 0, DocumentProperties.Buffer.Length);
            binaryWriter.Flush();
            LittleEndian.PutInt(array, this.GetLcbField(31), (int)binaryWriter.BaseStream.Position - num);
            binaryWriter2.Flush();
            this.TransferData(this.m_wordText.Stream, binaryWriter2.BaseStream, 4096);
            int num2 = (int)binaryWriter2.BaseStream.Position;
            int num3 = 512 - num2 % 512;

            binaryWriter2.Write(new byte[num3], 0, num3);
            binaryWriter2.Flush();
            int num4 = (int)binaryWriter2.BaseStream.Position / 512;

            this.TransferData(this.m_charFormat.Stream, binaryWriter2.BaseStream, 4096);
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(12), num);
            this.m_charFormat.WriteBinTableTo(binaryWriter, ref num4);
            binaryWriter.Flush();
            LittleEndian.PutInt(array, this.GetLcbField(12), (int)binaryWriter.BaseStream.Position - num);
            this.TransferData(this.m_parFormat.Stream, binaryWriter2.BaseStream, 4096);
            binaryWriter.Flush();
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(13), num);
            this.m_parFormat.WriteBinTableTo(binaryWriter, ref num4);
            binaryWriter.Flush();
            LittleEndian.PutInt(array, this.GetLcbField(13), (int)binaryWriter.BaseStream.Position - num);
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(6), num);
            this.m_secFormat.WriteTo(binaryWriter, binaryWriter2, this.m_wordText.CurrentCp);
            LittleEndian.PutInt(array, this.GetLcbField(6), (int)binaryWriter.BaseStream.Position - num);
            if (this.m_fldsMain.Size > 0)
            {
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(16), num);
                LittleEndian.PutInt(array, this.GetLcbField(16), this.m_fldsMain.Size);
                this.m_fldsMain.WriteTo(binaryWriter, 0, this.m_wordText.CurrentCp);
                binaryWriter.Flush();
            }
            if (this.m_fldsHdr.Size > 0)
            {
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(17), num);
                LittleEndian.PutInt(array, this.GetLcbField(17), this.m_fldsHdr.Size);
                this.m_fldsHdr.WriteTo(binaryWriter, this.m_ccpText, this.m_wordText.CurrentCp);
                binaryWriter.Flush();
            }
            if (this.m_headerFooterOffsets != null)
            {
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(11), num);
                LittleEndian.PutInt(array, this.GetLcbField(11), this.m_headerFooterOffsets.Length * 4);
                for (int i = 0; i < this.m_headerFooterOffsets.Length; i++)
                {
                    binaryWriter.Write(this.m_headerFooterOffsets[i]);
                }
                binaryWriter.Flush();
            }
            if (this.m_bookmarks.Count > 0)
            {
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(22), num);
                this.m_bookmarks.SerializeStarts(binaryWriter, this.m_wordText.CurrentCp);
                LittleEndian.PutInt(array, this.GetLcbField(22), (int)binaryWriter.BaseStream.Position - num);
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(23), num);
                this.m_bookmarks.SerializeEnds(binaryWriter, this.m_wordText.CurrentCp);
                LittleEndian.PutInt(array, this.GetLcbField(23), (int)binaryWriter.BaseStream.Position - num);
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(21), num);
                this.m_bookmarks.SerializeNames(binaryWriter);
                LittleEndian.PutInt(array, this.GetLcbField(21), (int)binaryWriter.BaseStream.Position - num);
            }
            if (this.m_listIndex > 0)
            {
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(73), num);
                binaryWriter.Write((short)this.m_listIndex);
                binaryWriter.Flush();
                this.TransferData(this.m_listStream, binaryWriter.BaseStream, (int)this.m_listStream.Length);
                this.TransferData(this.m_listLevelStream, binaryWriter.BaseStream, (int)this.m_listLevelStream.Length);
                LittleEndian.PutInt(array, this.GetLcbField(73), (int)binaryWriter.BaseStream.Position - num);
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(74), num);
                binaryWriter.Write(this.m_listIndex);
                binaryWriter.Flush();
                this.WriteListFormatOverrides(binaryWriter);
                LittleEndian.PutInt(array, this.GetLcbField(74), (int)binaryWriter.BaseStream.Position - num);
                num = (int)binaryWriter.BaseStream.Position;
                LittleEndian.PutInt(array, this.GetFcField(91), num);
                this.WriteListNameTable(binaryWriter);
                LittleEndian.PutInt(array, this.GetLcbField(91), (int)binaryWriter.BaseStream.Position - num);
            }
            num = (int)binaryWriter.BaseStream.Position;
            LittleEndian.PutInt(array, this.GetFcField(15), num);
            binaryWriter.Write((short)this.m_currentFontIndex);
            binaryWriter.Write((short)0);
            binaryWriter.Flush();
            this.TransferData(this.m_fontTable, binaryWriter.BaseStream, (int)this.m_fontTable.Length);
            LittleEndian.PutInt(array, this.GetLcbField(15), (int)binaryWriter.BaseStream.Position - num);
            binaryWriter2.Flush();
            binaryWriter2.BaseStream.Seek(24L, SeekOrigin.Begin);
            binaryWriter2.Write(this.m_fcStart);
            binaryWriter2.Write(num2);
            binaryWriter2.Flush();
            binaryWriter2.BaseStream.Seek(64L, SeekOrigin.Begin);
            binaryWriter2.Write((int)binaryWriter2.BaseStream.Length);
            binaryWriter2.Flush();
            binaryWriter2.BaseStream.Seek(76L, SeekOrigin.Begin);
            binaryWriter2.Write((this.m_ccpText > 0) ? this.m_ccpText : this.m_wordText.CurrentCp);
            binaryWriter2.Flush();
            if (this.m_ccpText > 0)
            {
                binaryWriter2.BaseStream.Seek(84L, SeekOrigin.Begin);
                binaryWriter2.Write(this.m_ccpHdd);
                binaryWriter2.Flush();
            }
            int val = 0;

            for (int j = 0; j < array.Length; j += 8)
            {
                int @int = LittleEndian.getInt(array, j);
                int int2 = LittleEndian.getInt(array, j + 4);
                if (int2 == 0)
                {
                    LittleEndian.PutInt(array, j, val);
                }
                else
                {
                    val = @int + int2;
                }
            }
            binaryWriter2.BaseStream.Seek(FileInformationBlock.StartBuffer.Length, SeekOrigin.Begin);
            binaryWriter2.Write((short)(array.Length / 8));
            binaryWriter2.Write(array, 0, array.Length);
            binaryWriter2.Flush();
            StructuredStorage.CreateMultiStreamFile(new Stream[3]
            {
                this.m_mainStream,
                this.m_tableStream,
                this.m_dataStream
            }, new string[3]
            {
                "WordDocument",
                "1Table",
                "Data"
            }, "00020906-0000-0000-c000-000000000046", author, title, comments, this.m_outStream, false);
            this.m_listStream.Close();
            this.m_mainStream.Close();
            this.m_tableStream.Close();
            this.m_dataStream.Close();
            this.m_fontTable.Close();
            this.m_listLevelStream.Close();
            this.m_charFormat.Stream.Close();
            this.m_parFormat.Stream.Close();
            this.m_wordText.Stream.Close();
        }
Beispiel #2
0
        public static bool CreateMultiStreamFile(Stream[] sources, string[] streamNames, string clsId, string author, string title, string comments, Stream output, bool forceInMemory)
        {
            OLEStructuredStorage.UCOMIStorage   uCOMIStorage   = null;
            OLEStructuredStorage.UCOMILockBytes uCOMILockBytes = null;
            IStream stream = null;
            string  text   = null;
            Guid    guid   = new Guid(clsId);
            long    num    = 0L;
            long    num2   = 0L;

            for (int i = 0; i < sources.Length; i++)
            {
                num2 = Math.Max(sources[i].Length, num2);
                num += sources[i].Length;
            }
            int num3 = Math.Min(1048576, (int)num2 + 512);

            try
            {
                int grfMode  = 134221842;
                int grfMode2 = 18;
                text = Path.GetTempPath() + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) + ".doc.tmp";
                if (OLEStructuredStorage.StgCreateDocfile(text, grfMode, 0, out uCOMIStorage) != 0)
                {
                    return(false);
                }
                byte[] array = new byte[num3];
                for (int j = 0; j < streamNames.Length; j++)
                {
                    sources[j].Seek(0L, SeekOrigin.Begin);
                    uCOMIStorage.CreateStream(streamNames[j], grfMode2, 0, 0, out stream);
                    if (stream != null)
                    {
                        IntPtr pcbWritten = default(IntPtr);
                        int    num4       = 0;
                        while ((num4 = sources[j].Read(array, 0, num3)) > 0)
                        {
                            stream.Write(array, num4, pcbWritten);
                        }
                        sources[j] = null;
                    }
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }
                OLEStructuredStorage.IPropertySetStorage propertySetStorage = null;
                OLEStructuredStorage.IPropertyStorage    propertyStorage    = null;
                int  num5  = OLEStructuredStorage.StgCreatePropSetStg(uCOMIStorage, 0, out propertySetStorage);
                Guid guid2 = new Guid("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}");
                propertySetStorage.Create(ref guid2, IntPtr.Zero, 0, grfMode2, out propertyStorage);
                StructuredStorage.WriteProperty(propertyStorage, 2, title);
                StructuredStorage.WriteProperty(propertyStorage, 4, author);
                StructuredStorage.WriteProperty(propertyStorage, 6, comments);
                Marshal.ReleaseComObject(propertyStorage);
                Marshal.ReleaseComObject(propertySetStorage);
                uCOMIStorage.SetClass(ref guid);
                uCOMIStorage.Commit(0);
                Marshal.ReleaseComObject(uCOMIStorage);
                uCOMIStorage = null;
                using (Stream stream2 = File.OpenRead(text))
                {
                    int num6 = 0;
                    while ((num6 = stream2.Read(array, 0, array.Length)) > 0)
                    {
                        output.Write(array, 0, num6);
                    }
                }
                array = null;
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (!string.IsNullOrEmpty(text) && File.Exists(text))
                {
                    File.Delete(text);
                }
                if (stream != null)
                {
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }
                if (uCOMIStorage != null)
                {
                    Marshal.ReleaseComObject(uCOMIStorage);
                    uCOMIStorage = null;
                }
                if (uCOMILockBytes != null)
                {
                    Marshal.ReleaseComObject(uCOMILockBytes);
                    uCOMILockBytes = null;
                }
            }
        }