Exemplo n.º 1
0
        public static int Write(HWPFStream tableStream, String[] entries)
        {
            byte[] header = new byte[6];
            LittleEndian.PutShort(header, 0, unchecked ((short)0xffff));

            if (entries == null || entries.Length == 0)
            {
                LittleEndian.PutInt(header, 2, 0);
                tableStream.Write(header);
                return(6);
            }

            LittleEndian.PutInt(header, 2, entries.Length);
            tableStream.Write(header);
            int size = 6;

            foreach (String entry in entries)
            {
                byte[] buf = new byte[entry.Length * 2 + 2];
                LittleEndian.PutShort(buf, 0, (short)entry.Length);
                StringUtil.PutUnicodeLE(entry, buf, 2);
                tableStream.Write(buf);
                size += buf.Length;
            }

            return(size);
        }
Exemplo n.º 2
0
        public void WriteTo(HWPFStream out1)
        {
            int offset = 0;

            // add two bytes so we can prepend the stylesheet w/ its size
            byte[] buf = new byte[_stshiLength + 2];
            LittleEndian.PutShort(buf, offset, (short)_stshiLength);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_styleDescriptions.Length);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_baseLength);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_flags);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_maxIndex);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_maxFixedIndex);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_stylenameVersion);
            offset += LittleEndianConsts.SHORT_SIZE;

            LittleEndian.PutShort(buf, offset, (short)_rgftc[0]);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_rgftc[1]);
            offset += LittleEndianConsts.SHORT_SIZE;
            LittleEndian.PutShort(buf, offset, (short)_rgftc[2]);

            out1.Write(buf);

            byte[] sizeHolder = new byte[2];
            for (int x = 0; x < _styleDescriptions.Length; x++)
            {
                if (_styleDescriptions[x] != null)
                {
                    byte[] std = _styleDescriptions[x].ToArray();

                    // adjust the size so it is always on a word boundary
                    LittleEndian.PutShort(sizeHolder, (short)((std.Length) + (std.Length % 2)));
                    out1.Write(sizeHolder);
                    out1.Write(std);

                    // Must always start on a word boundary.
                    if (std.Length % 2 == 1)
                    {
                        out1.Write('\0');
                    }
                }
                else
                {
                    sizeHolder[0] = 0;
                    sizeHolder[1] = 0;
                    out1.Write(sizeHolder);
                }
            }
        }
Exemplo n.º 3
0
        public void WriteTo(HWPFStream wordDocumentStream, HWPFStream tableStream)
        {
            tableStream.Write(TEXT_PIECE_TABLE_TYPE);

            byte[] table = _tpt.WriteTo(wordDocumentStream);

            byte[] numHolder = new byte[LittleEndianConsts.INT_SIZE];
            LittleEndian.PutInt(numHolder, 0, table.Length);
            tableStream.Write(numHolder);
            tableStream.Write(table);
        }
Exemplo n.º 4
0
        public void WriteTo(HWPFFileSystem sys, int fcMin)
        {
            HWPFStream docStream   = sys.GetStream("WordDocument");
            Stream     tableStream = sys.GetStream("1Table");

            PlexOfCps binTable = new PlexOfCps(4);

            // each FKP must start on a 512 byte page.
            int docOffset = docStream.Offset;
            int mod       = docOffset % POIFSConstants.SMALLER_BIG_BLOCK_SIZE;

            if (mod != 0)
            {
                byte[] pAdding = new byte[POIFSConstants.SMALLER_BIG_BLOCK_SIZE - mod];
                docStream.Write(pAdding);
            }

            // get the page number for the first fkp
            docOffset = docStream.Offset;
            int pageNum = docOffset / POIFSConstants.SMALLER_BIG_BLOCK_SIZE;

            // get the ending fc
            int endingFc = ((PropertyNode)_textRuns[_textRuns.Count - 1]).End;

            endingFc += fcMin;


            List <CHPX> overflow = _textRuns;

            do
            {
                PropertyNode startingProp = (PropertyNode)overflow[0];
                int          start        = startingProp.Start + fcMin;

                CHPFormattedDiskPage cfkp = new CHPFormattedDiskPage();
                cfkp.Fill(overflow);

                byte[] bufFkp = cfkp.ToArray(fcMin);
                docStream.Write(bufFkp);
                overflow = cfkp.GetOverflow();

                int end = endingFc;
                if (overflow != null)
                {
                    end = ((PropertyNode)overflow[0]).Start + fcMin;
                }

                byte[] intHolder = new byte[4];
                LittleEndian.PutInt(intHolder, pageNum++);
                binTable.AddProperty(new GenericPropertyNode(start, end, intHolder));
            }while (overflow != null);
            byte[] bytes = binTable.ToByteArray();
            tableStream.Write(bytes, 0, bytes.Length);
        }
Exemplo n.º 5
0
        public void WriteTo(HWPFStream docStream,
                            HWPFStream tableStream, CharIndexTranslator translator)

        {
            PlexOfCps binTable = new PlexOfCps(4);

            // each FKP must start on a 512 byte page.
            int docOffset = docStream.Offset;
            int mod       = docOffset % POIFSConstants.SMALLER_BIG_BLOCK_SIZE;

            if (mod != 0)
            {
                byte[] pAdding = new byte[POIFSConstants.SMALLER_BIG_BLOCK_SIZE - mod];
                docStream.Write(pAdding);
            }

            // get the page number for the first fkp
            docOffset = docStream.Offset;
            int pageNum = docOffset / POIFSConstants.SMALLER_BIG_BLOCK_SIZE;

            // get the ending fc
            int endingFc = ((PropertyNode)_paragraphs[_paragraphs.Count - 1]).End;


            List <PAPX> overflow = _paragraphs;

            do
            {
                PropertyNode startingProp = (PropertyNode)overflow[0];
                int          start        = translator.GetByteIndex(startingProp.Start);

                PAPFormattedDiskPage pfkp = new PAPFormattedDiskPage(_dataStream);
                pfkp.Fill(overflow);

                byte[] bufFkp = pfkp.ToByteArray(tableStream, translator);
                docStream.Write(bufFkp);
                overflow = pfkp.GetOverflow();

                int end = endingFc;
                if (overflow != null)
                {
                    end = translator.GetByteIndex(overflow[0].Start);
                }

                byte[] intHolder = new byte[4];
                LittleEndian.PutInt(intHolder, pageNum++);
                binTable.AddProperty(new GenericPropertyNode(start, end, intHolder));
            }while (overflow != null);
            byte[] bytes = binTable.ToByteArray();
            tableStream.Write(bytes, 0, bytes.Length);
        }
Exemplo n.º 6
0
        public void WriteTo(HWPFFileSystem sys)
        {
            HWPFStream tableStream = sys.GetStream("1Table");

            byte[] buf = new byte[LittleEndianConsts.SHORT_SIZE];
            LittleEndian.PutShort(buf, 0, _stringCount);
            tableStream.Write(buf);
            LittleEndian.PutShort(buf, 0, _extraDataSz);
            tableStream.Write(buf);

            for (int i = 0; i < _fontNames.Length; i++)
            {
                tableStream.Write(_fontNames[i].ToArray());
            }
        }
Exemplo n.º 7
0
        /**
         * Writes this table to the table stream.
         *
         * @param tableStream  the table stream to write to.
         * @throws IOException  if an error occurs while writing.
         */
        public void WriteTo(HWPFStream tableStream)
        {
            byte[] header = new byte[6];
            LittleEndian.PutShort(header, 0, fExtend);
            LittleEndian.PutShort(header, 2, cData);
            LittleEndian.PutShort(header, 4, cbExtra);
            tableStream.Write(header);

            foreach (String name in entries)
            {
                byte[] buf = new byte[name.Length * 2 + 2];
                LittleEndian.PutShort(buf, 0, (short)name.Length);
                StringUtil.PutUnicodeLE(name, buf, 2);
                tableStream.Write(buf);
            }
        }
Exemplo n.º 8
0
 private void WriteStringValue(HWPFStream tableStream, String value)
 {
     byte[] buf = new byte[value.Length * 2 + 2];
     LittleEndian.PutShort(buf, 0, (short)value.Length);
     StringUtil.PutUnicodeLE(value, buf, 2);
     tableStream.Write(buf);
 }
Exemplo n.º 9
0
        internal void WriteTo(byte[] mainStream, int offset, HWPFStream tableStream)
        {
            int length = _fields.Length / 2;

            LittleEndian.PutShort(mainStream, offset, (short)length);
            offset += LittleEndianConsts.SHORT_SIZE;

            for (int x = 0; x < length; x++)
            {
                UnhandledDataStructure ds = (UnhandledDataStructure)_unknownMap[x];
                if (ds != null)
                {
                    LittleEndian.PutInt(mainStream, offset, tableStream.Offset);
                    offset += LittleEndianConsts.INT_SIZE;
                    byte[] buf = ds.GetBuf();
                    tableStream.Write(buf);
                    LittleEndian.PutInt(mainStream, offset, buf.Length);
                    offset += LittleEndianConsts.INT_SIZE;
                }
                else
                {
                    LittleEndian.PutInt(mainStream, offset, _fields[x * 2]);
                    offset += LittleEndianConsts.INT_SIZE;
                    LittleEndian.PutInt(mainStream, offset, _fields[(x * 2) + 1]);
                    offset += LittleEndianConsts.INT_SIZE;
                }
            }
        }
Exemplo n.º 10
0
        public void WriteTo(HWPFFileSystem sys, int fcMin)
        {
            HWPFStream docStream   = sys.GetStream("WordDocument");
            HWPFStream tableStream = sys.GetStream("1Table");

            int       offset = docStream.Offset;
            int       len    = _sections.Count;
            PlexOfCps plex   = new PlexOfCps(SED_SIZE);

            for (int x = 0; x < len; x++)
            {
                SEPX   sepx   = _sections[x];
                byte[] grpprl = sepx.GetGrpprl();

                // write the sepx to the document stream. starts with a 2 byte size
                // followed by the grpprl
                byte[] shortBuf = new byte[2];
                LittleEndian.PutShort(shortBuf, (short)grpprl.Length);

                docStream.Write(shortBuf);
                docStream.Write(grpprl);

                // set the fc in the section descriptor
                SectionDescriptor sed = sepx.GetSectionDescriptor();
                sed.SetFc(offset);

                // add the section descriptor bytes to the PlexOfCps.


                // original line -
                //GenericPropertyNode property = new GenericPropertyNode(sepx.Start, sepx.End, sed.ToArray());

                // Line using Ryan's FCtoCP() conversion method -
                // unable to observe any effect on our testcases when using this code - piers
                GenericPropertyNode property = new GenericPropertyNode(tpt.GetCharIndex(sepx.StartBytes), tpt.GetCharIndex(sepx.EndBytes), sed.ToArray());


                plex.AddProperty(property);

                offset = docStream.Offset;
            }
            tableStream.Write(plex.ToByteArray());
        }
Exemplo n.º 11
0
        /**
         * Writes this table to the table stream.
         *
         * @param tableStream the table stream to write to.
         * @throws IOException if an error occurs while writing.
         */
        public void WriteTo(HWPFStream tableStream)
        {
            byte[] header = new byte[6];
            LittleEndian.PutShort(header, 0, unknownValue);
            LittleEndian.PutInt(header, 2, entries.Length * 2);
            tableStream.Write(header);

            for (int i = 0; i < entries.Length; i++)
            {
                WriteStringValue(tableStream, entries[i].GetUserName());
                WriteStringValue(tableStream, entries[i].GetSaveLocation());
            }
        }
Exemplo n.º 12
0
        public byte[] WriteTo(HWPFStream docStream)
        {

            PlexOfCps textPlex = new PlexOfCps(PieceDescriptor.SizeInBytes);
            // int fcMin = docStream.Getoffset();

            int size = _textPieces.Count;
            for (int x = 0; x < size; x++)
            {
                TextPiece next = _textPieces[x];
                PieceDescriptor pd = next.PieceDescriptor;

                int offset = docStream.Offset;
                int mod = (offset % POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
                if (mod != 0)
                {
                    mod = POIFSConstants.SMALLER_BIG_BLOCK_SIZE - mod;
                    byte[] buf = new byte[mod];
                    docStream.Write(buf);
                }

                // set the text piece position to the current docStream offset.
                pd.FilePosition = (docStream.Offset);

                // write the text to the docstream and save the piece descriptor to
                // the
                // plex which will be written later to the tableStream.
                docStream.Write(next.RawBytes);

                // The TextPiece is already in characters, which
                // Makes our life much easier
                int nodeStart = next.Start;
                int nodeEnd = next.End;
                textPlex.AddProperty(new GenericPropertyNode(nodeStart, nodeEnd, pd.ToByteArray()));
            }

            return textPlex.ToByteArray();

        }
Exemplo n.º 13
0
        public void TestReadWriteFromNonZeroOffset()
        {
            HWPFFileSystem fileSys  = new HWPFFileSystem();
            HWPFStream     tableOut = fileSys.GetStream("1Table");

            tableOut.Write(new byte[20]); // 20 bytes of whatever at the front.
            _styleSheet.WriteTo(tableOut);

            byte[] newTableStream = tableOut.ToArray();

            StyleSheet newStyleSheet = new StyleSheet(newTableStream, 20);

            Assert.AreEqual(newStyleSheet, _styleSheet);
        }
Exemplo n.º 14
0
        public void WriteListDataTo(HWPFStream tableStream)
        {
            int listSize = _listMap.Count;

            // use this stream as a buffer for the levels since their size varies.
            MemoryStream levelBuf = new MemoryStream();

            byte[] shortHolder = new byte[2];
            LittleEndian.PutShort(shortHolder, 0, (short)listSize);
            tableStream.Write(shortHolder);
            //TODO:: sort the keys
            foreach (int x in _listMap.Keys)
            {
                ListData lst = _listMap[x];
                tableStream.Write(lst.ToArray());
                ListLevel[] lvls = lst.GetLevels();
                for (int y = 0; y < lvls.Length; y++)
                {
                    byte[] bytes = lvls[y].ToArray();
                    levelBuf.Write(bytes, 0, bytes.Length);
                }
            }
            tableStream.Write(levelBuf.ToArray());
        }
Exemplo n.º 15
0
        public void WriteListOverridesTo(HWPFStream tableStream)
        {
            // use this stream as a buffer for the levels since their size varies.
            MemoryStream levelBuf = new MemoryStream();

            int size = _overrideList.Count;

            byte[] intHolder = new byte[4];
            LittleEndian.PutInt(intHolder, 0, size);
            tableStream.Write(intHolder);

            for (int x = 0; x < size; x++)
            {
                ListFormatOverride lfo = _overrideList[x];
                tableStream.Write(lfo.ToArray());
                ListFormatOverrideLevel[] lfolvls = lfo.GetLevelOverrides();
                for (int y = 0; y < lfolvls.Length; y++)
                {
                    byte[] bytes = lfolvls[y].ToArray();
                    levelBuf.Write(bytes, 0, bytes.Length);
                }
            }
            tableStream.Write(levelBuf.ToArray());
        }
Exemplo n.º 16
0
        public void WriteRef(FileInformationBlock fib, HWPFStream tableStream)
        {
            if (descriptors == null || descriptors.Length == 0)
            {
                fib.SetNotesDescriptorsOffset(noteType, tableStream.Offset);
                fib.SetNotesDescriptorsSize(noteType, 0);
                return;
            }

            int start = tableStream.Offset;

            byte[] data = descriptors.ToByteArray();
            tableStream.Write(data);
            int end = tableStream.Offset;

            fib.SetNotesDescriptorsOffset(noteType, start);
            fib.SetNotesDescriptorsSize(noteType, end - start);
        }
Exemplo n.º 17
0
        public void WriteTxt(FileInformationBlock fib, HWPFStream tableStream)
        {
            if (textPositions == null || textPositions.Length == 0)
            {
                fib.SetNotesTextPositionsOffset(noteType, tableStream.Offset);
                fib.SetNotesTextPositionsSize(noteType, 0);
                return;
            }

            int start = tableStream.Offset;

            byte[] data = textPositions.ToByteArray();
            tableStream.Write(data);
            int end = tableStream.Offset;

            fib.SetNotesTextPositionsOffset(noteType, start);
            fib.SetNotesTextPositionsSize(noteType, end - start);
        }
Exemplo n.º 18
0
        public void WritePlcfBkmkl(FileInformationBlock fib,
                                   HWPFStream tableStream)
        {
            if (descriptorsLim == null || descriptorsLim.Length == 0)
            {
                fib.SetFcPlcfbkl(0);
                fib.SetLcbPlcfbkl(0);
                return;
            }

            int start = tableStream.Offset;

            tableStream.Write(descriptorsLim.ToByteArray());
            int end = tableStream.Offset;

            fib.SetFcPlcfbkl(start);
            fib.SetLcbPlcfbkl(end - start);
        }
Exemplo n.º 19
0
        private int SavePlex(FileInformationBlock fib, FieldsDocumentPart part,
                             PlexOfCps plexOfCps, HWPFStream outputStream)
        {
            if (plexOfCps == null || plexOfCps.Length == 0)
            {
                fib.SetFieldsPlcfOffset(part, outputStream.Offset);
                fib.SetFieldsPlcfLength(part, 0);
                return(0);
            }

            byte[] data = plexOfCps.ToByteArray();

            int start  = outputStream.Offset;
            int length = data.Length;

            outputStream.Write(data);

            fib.SetFieldsPlcfOffset(part, start);
            fib.SetFieldsPlcfLength(part, length);

            return(length);
        }
Exemplo n.º 20
0
        /**
         * Creates a byte array representation of this data structure. Suitable for
         * writing to a Word document.
         *
         * @param fcMin The file offset in the main stream where text begins.
         * @return A byte array representing this data structure.
         */
        internal byte[] ToByteArray(HWPFStream dataStream,
                                    CharIndexTranslator translator)
        {
            byte[] buf          = new byte[512];
            int    size         = _papxList.Count;
            int    grpprlOffset = 0;
            int    bxOffset     = 0;
            int    fcOffset     = 0;

            byte[] lastGrpprl = new byte[0];

            // total size is currently the size of one FC
            int totalSize = FC_SIZE;

            int index = 0;

            for (; index < size; index++)
            {
                byte[] grpprl       = ((PAPX)_papxList[index]).GetGrpprl();
                int    grpprlLength = grpprl.Length;

                // is grpprl huge?
                if (grpprlLength > 488)
                {
                    grpprlLength = 8; // set equal to size of sprmPHugePapx grpprl
                }

                // check to see if we have enough room for an FC, a BX, and the grpprl
                // and the 1 byte size of the grpprl.
                int addition = 0;
                if (!Arrays.Equals(grpprl, lastGrpprl))
                {
                    addition = (FC_SIZE + BX_SIZE + grpprlLength + 1);
                }
                else
                {
                    addition = (FC_SIZE + BX_SIZE);
                }

                totalSize += addition;

                // if size is uneven we will have to add one so the first grpprl falls
                // on a word boundary
                if (totalSize > 511 + (index % 2))
                {
                    totalSize -= addition;
                    break;
                }

                // grpprls must fall on word boundaries
                if (grpprlLength % 2 > 0)
                {
                    totalSize += 1;
                }
                else
                {
                    totalSize += 2;
                }
                lastGrpprl = grpprl;
            }

            // see if we couldn't fit some
            if (index != size)
            {
                _overFlow = new List <PAPX>();
                _overFlow.AddRange(_papxList.GetRange(index, size - index));
            }

            // index should equal number of papxs that will be in this fkp now.
            buf[511] = (byte)index;

            bxOffset     = (FC_SIZE * index) + FC_SIZE;
            grpprlOffset = 511;

            PAPX papx = null;

            lastGrpprl = new byte[0];
            for (int x = 0; x < index; x++)
            {
                papx = _papxList[x];
                byte[] phe    = papx.GetParagraphHeight().ToArray();
                byte[] grpprl = papx.GetGrpprl();

                // is grpprl huge?
                if (grpprl.Length > 488)
                {
                    /*
                     * // if so do we have storage at GetHugeGrpprloffset()
                     * int hugeGrpprlOffset = papx.GetHugeGrpprlOffset();
                     * if (hugeGrpprlOffset == -1) // then we have no storage...
                     * {
                     *  throw new InvalidOperationException(
                     *        "This Paragraph has no dataStream storage.");
                     * }
                     * // we have some storage...
                     *
                     * // get the size of the existing storage
                     * int maxHugeGrpprlSize = LittleEndian.GetUShort(_dataStream, hugeGrpprlOffset);
                     *
                     * if (maxHugeGrpprlSize < grpprl.Length - 2)
                     * { // grpprl.Length-2 because we don't store the istd
                     *  throw new InvalidOperationException(
                     *      "This Paragraph's dataStream storage is too small.");
                     * }
                     *
                     *
                     * // store grpprl at hugeGrpprlOffset
                     * Array.Copy(grpprl, 2, _dataStream, hugeGrpprlOffset + 2,
                     *               grpprl.Length - 2); // grpprl.Length-2 because we don't store the istd
                     * LittleEndian.PutUShort(_dataStream, hugeGrpprlOffset, grpprl.Length - 2);
                     */

                    byte[] hugePapx = new byte[grpprl.Length - 2];
                    System.Array.Copy(grpprl, 2, hugePapx, 0, grpprl.Length - 2);
                    int dataStreamOffset = dataStream.Offset;
                    dataStream.Write(hugePapx);

                    // grpprl = grpprl Containing only a sprmPHugePapx2
                    int istd = LittleEndian.GetUShort(grpprl, 0);
                    grpprl = new byte[8];
                    LittleEndian.PutUShort(grpprl, 0, istd);
                    LittleEndian.PutUShort(grpprl, 2, 0x6646); // sprmPHugePapx2
                    LittleEndian.PutInt(grpprl, 4, dataStreamOffset);
                }

                bool same = Arrays.Equals(lastGrpprl, grpprl);
                if (!same)
                {
                    grpprlOffset -= (grpprl.Length + (2 - grpprl.Length % 2));
                    grpprlOffset -= (grpprlOffset % 2);
                }
                LittleEndian.PutInt(buf, fcOffset, translator.GetByteIndex(papx.Start));
                buf[bxOffset] = (byte)(grpprlOffset / 2);
                Array.Copy(phe, 0, buf, bxOffset + 1, phe.Length);

                // refer to the section on PAPX in the spec. Places a size on the front
                // of the PAPX. Has to do with how the grpprl stays on word
                // boundaries.
                if (!same)
                {
                    int copyOffset = grpprlOffset;
                    if ((grpprl.Length % 2) > 0)
                    {
                        buf[copyOffset++] = (byte)((grpprl.Length + 1) / 2);
                    }
                    else
                    {
                        buf[++copyOffset] = (byte)((grpprl.Length) / 2);
                        copyOffset++;
                    }
                    Array.Copy(grpprl, 0, buf, copyOffset, grpprl.Length);
                    lastGrpprl = grpprl;
                }

                bxOffset += BX_SIZE;
                fcOffset += FC_SIZE;
            }

            LittleEndian.PutInt(buf, fcOffset, translator.GetByteIndex(papx.End));
            return(buf);
        }