You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlfLfo.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.hwpf.model;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import java.util.NoSuchElementException;
  24. import org.apache.logging.log4j.LogManager;
  25. import org.apache.logging.log4j.Logger;
  26. import org.apache.poi.hwpf.model.types.LFOAbstractType;
  27. import org.apache.poi.util.IOUtils;
  28. import org.apache.poi.util.LittleEndian;
  29. import org.apache.poi.util.LittleEndianConsts;
  30. import static org.apache.logging.log4j.util.Unbox.box;
  31. /**
  32. * The PlfLfo structure contains the list format override data for the document.
  33. * <p>
  34. * Documentation quoted from Page 424 of 621. [MS-DOC] -- v20110315 Word (.doc)
  35. * Binary File Format
  36. */
  37. public class PlfLfo {
  38. private static final Logger LOGGER = LogManager.getLogger(PlfLfo.class);
  39. private static final int MAX_NUMBER_OF_LFO = 100_000;
  40. /**
  41. * An unsigned integer that specifies the count of elements in both the
  42. * rgLfo and rgLfoData arrays.
  43. */
  44. private int _lfoMac;
  45. private LFO[] _rgLfo;
  46. private LFOData[] _rgLfoData;
  47. PlfLfo( byte[] tableStream, int fcPlfLfo, int lcbPlfLfo )
  48. {
  49. /*
  50. * The PlfLfo structure contains the list format override data for the
  51. * document. -- Page 424 of 621. [MS-DOC] -- v20110315 Word (.doc)
  52. * Binary File Format
  53. */
  54. int offset = fcPlfLfo;
  55. /*
  56. * lfoMac (4 bytes): An unsigned integer that specifies the count of
  57. * elements in both the rgLfo and rgLfoData arrays. -- Page 424 of 621.
  58. * [MS-DOC] -- v20110315 Word (.doc) Binary File Format
  59. */
  60. long lfoMacLong = LittleEndian.getUInt( tableStream, offset );
  61. offset += LittleEndianConsts.INT_SIZE;
  62. if ( lfoMacLong > Integer.MAX_VALUE )
  63. {
  64. throw new UnsupportedOperationException(
  65. "Apache POI doesn't support rgLfo/rgLfoData size large than "
  66. + Integer.MAX_VALUE + " elements" );
  67. }
  68. IOUtils.safelyAllocateCheck(lfoMacLong, MAX_NUMBER_OF_LFO);
  69. this._lfoMac = (int) lfoMacLong;
  70. _rgLfo = new LFO[_lfoMac];
  71. _rgLfoData = new LFOData[_lfoMac];
  72. /*
  73. * An array of LFO structures. The number of elements in this array is
  74. * specified by lfoMac. -- Page 424 of 621. [MS-DOC] -- v20110315 Word
  75. * (.doc) Binary File Format
  76. */
  77. for ( int x = 0; x < _lfoMac; x++ )
  78. {
  79. LFO lfo = new LFO( tableStream, offset );
  80. offset += LFOAbstractType.getSize();
  81. _rgLfo[x] = lfo;
  82. }
  83. /*
  84. * An array of LFOData that is parallel to rgLfo. The number of elements
  85. * that are contained in this array is specified by lfoMac. -- Page 424
  86. * of 621. [MS-DOC] -- v20110315 Word (.doc) Binary File Format
  87. */
  88. for ( int x = 0; x < _lfoMac; x++ )
  89. {
  90. LFOData lfoData = new LFOData( tableStream, offset,
  91. _rgLfo[x].getClfolvl() );
  92. offset += lfoData.getSizeInBytes();
  93. _rgLfoData[x] = lfoData;
  94. }
  95. if ( ( offset - fcPlfLfo ) != lcbPlfLfo )
  96. {
  97. LOGGER.atWarn().log("Actual size of PlfLfo is {} bytes, but expected {}", box(offset - fcPlfLfo),box(lcbPlfLfo));
  98. }
  99. }
  100. void add( LFO lfo, LFOData lfoData )
  101. {
  102. // _lfoMac is the size of the array
  103. _rgLfo = Arrays.copyOf(_rgLfo, _lfoMac + 1);
  104. _rgLfo[_lfoMac] = lfo;
  105. _rgLfoData = Arrays.copyOf(_rgLfoData, _lfoMac + 1);
  106. _rgLfoData[_lfoMac] = lfoData;
  107. _lfoMac = _lfoMac + 1;
  108. }
  109. @Override
  110. public boolean equals( Object obj ) {
  111. if (this == obj)
  112. return true;
  113. if (obj == null)
  114. return false;
  115. if (getClass() != obj.getClass())
  116. return false;
  117. PlfLfo other = (PlfLfo) obj;
  118. return _lfoMac == other._lfoMac &&
  119. Arrays.equals(_rgLfo, other._rgLfo) &&
  120. Arrays.equals(_rgLfoData, other._rgLfoData);
  121. }
  122. /**
  123. * An unsigned integer that specifies the count of elements in both the
  124. * rgLfo and rgLfoData arrays.
  125. */
  126. public int getLfoMac()
  127. {
  128. return _lfoMac;
  129. }
  130. public int getIlfoByLsid( int lsid )
  131. {
  132. for ( int i = 0; i < _lfoMac; i++ )
  133. {
  134. if ( _rgLfo[i].getLsid() == lsid )
  135. {
  136. return i + 1;
  137. }
  138. }
  139. throw new NoSuchElementException( "LFO with lsid " + lsid
  140. + " not found" );
  141. }
  142. /**
  143. * @param ilfo 1-based index
  144. * @return The {@link LFO} stored at the given index
  145. */
  146. public LFO getLfo( int ilfo ) throws NoSuchElementException
  147. {
  148. if ( ilfo <= 0 || ilfo > _lfoMac )
  149. {
  150. throw new NoSuchElementException( "LFO with ilfo " + ilfo
  151. + " not found. lfoMac is " + _lfoMac );
  152. }
  153. return _rgLfo[ilfo - 1];
  154. }
  155. /**
  156. * @param ilfo 1-based index
  157. * @return The {@link LFOData} stored at the given index
  158. */
  159. public LFOData getLfoData( int ilfo ) throws NoSuchElementException
  160. {
  161. if ( ilfo <= 0 || ilfo > _lfoMac )
  162. {
  163. throw new NoSuchElementException( "LFOData with ilfo " + ilfo
  164. + " not found. lfoMac is " + _lfoMac );
  165. }
  166. return _rgLfoData[ilfo - 1];
  167. }
  168. @Override
  169. public int hashCode() {
  170. return Arrays.deepHashCode(new Object[]{_lfoMac, _rgLfo, _rgLfoData});
  171. }
  172. void writeTo( FileInformationBlock fib, ByteArrayOutputStream outputStream )
  173. throws IOException
  174. {
  175. final int offset = outputStream.size();
  176. fib.setFcPlfLfo( offset );
  177. LittleEndian.putUInt( _lfoMac, outputStream );
  178. byte[] bs = new byte[LFOAbstractType.getSize() * _lfoMac];
  179. for ( int i = 0; i < _lfoMac; i++ )
  180. {
  181. _rgLfo[i].serialize( bs, i * LFOAbstractType.getSize() );
  182. }
  183. outputStream.write( bs, 0, LFOAbstractType.getSize() * _lfoMac );
  184. for ( int i = 0; i < _lfoMac; i++ )
  185. {
  186. _rgLfoData[i].writeTo( outputStream );
  187. }
  188. fib.setLcbPlfLfo( outputStream.size() - offset );
  189. }
  190. }