예제 #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;
        }
예제 #2
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, table.Length);
            tableStream.Write(numHolder);
            tableStream.Write(table);
        }
예제 #3
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;
                }
            }
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
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);
                }
            }
        }
예제 #7
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;
        }
예제 #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);
 }
예제 #9
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());
            }
        }
        /**
         * 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);
            }
        }
예제 #11
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);
        }
예제 #12
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, 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());

        }
예제 #13
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, (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());
        }
예제 #14
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;
        }