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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.LittleEndian;
  28. import org.apache.poi.util.LittleEndianConsts;
  29. import static org.apache.logging.log4j.util.Unbox.box;
  30. /**
  31. * The PlfLfo structure contains the list format override data for the document.
  32. * <p>
  33. * Documentation quoted from Page 424 of 621. [MS-DOC] -- v20110315 Word (.doc)
  34. * Binary File Format
  35. */
  36. public class PlfLfo
  37. {
  38. private static final Logger LOGGER = LogManager.getLogger(PlfLfo.class);
  39. /**
  40. * An unsigned integer that specifies the count of elements in both the
  41. * rgLfo and rgLfoData arrays.
  42. */
  43. private int _lfoMac;
  44. private LFO[] _rgLfo;
  45. private LFOData[] _rgLfoData;
  46. PlfLfo( byte[] tableStream, int fcPlfLfo, int lcbPlfLfo )
  47. {
  48. /*
  49. * The PlfLfo structure contains the list format override data for the
  50. * document. -- Page 424 of 621. [MS-DOC] -- v20110315 Word (.doc)
  51. * Binary File Format
  52. */
  53. int offset = fcPlfLfo;
  54. /*
  55. * lfoMac (4 bytes): An unsigned integer that specifies the count of
  56. * elements in both the rgLfo and rgLfoData arrays. -- Page 424 of 621.
  57. * [MS-DOC] -- v20110315 Word (.doc) Binary File Format
  58. */
  59. long lfoMacLong = LittleEndian.getUInt( tableStream, offset );
  60. offset += LittleEndianConsts.INT_SIZE;
  61. if ( lfoMacLong > Integer.MAX_VALUE )
  62. {
  63. throw new UnsupportedOperationException(
  64. "Apache POI doesn't support rgLfo/rgLfoData size large than "
  65. + Integer.MAX_VALUE + " elements" );
  66. }
  67. this._lfoMac = (int) lfoMacLong;
  68. _rgLfo = new LFO[_lfoMac];
  69. _rgLfoData = new LFOData[_lfoMac];
  70. /*
  71. * An array of LFO structures. The number of elements in this array is
  72. * specified by lfoMac. -- Page 424 of 621. [MS-DOC] -- v20110315 Word
  73. * (.doc) Binary File Format
  74. */
  75. for ( int x = 0; x < _lfoMac; x++ )
  76. {
  77. LFO lfo = new LFO( tableStream, offset );
  78. offset += LFOAbstractType.getSize();
  79. _rgLfo[x] = lfo;
  80. }
  81. /*
  82. * An array of LFOData that is parallel to rgLfo. The number of elements
  83. * that are contained in this array is specified by lfoMac. -- Page 424
  84. * of 621. [MS-DOC] -- v20110315 Word (.doc) Binary File Format
  85. */
  86. for ( int x = 0; x < _lfoMac; x++ )
  87. {
  88. LFOData lfoData = new LFOData( tableStream, offset,
  89. _rgLfo[x].getClfolvl() );
  90. offset += lfoData.getSizeInBytes();
  91. _rgLfoData[x] = lfoData;
  92. }
  93. if ( ( offset - fcPlfLfo ) != lcbPlfLfo )
  94. {
  95. LOGGER.atWarn().log("Actual size of PlfLfo is {} bytes, but expected {}", box(offset - fcPlfLfo),box(lcbPlfLfo));
  96. }
  97. }
  98. void add( LFO lfo, LFOData lfoData )
  99. {
  100. // _lfoMac is the size of the array
  101. _rgLfo = Arrays.copyOf(_rgLfo, _lfoMac + 1);
  102. _rgLfo[_lfoMac] = lfo;
  103. _rgLfoData = Arrays.copyOf(_rgLfoData, _lfoMac + 1);
  104. _rgLfoData[_lfoMac] = lfoData;
  105. _lfoMac = _lfoMac + 1;
  106. }
  107. @Override
  108. public boolean equals( Object obj ) {
  109. if (this == obj)
  110. return true;
  111. if (obj == null)
  112. return false;
  113. if (getClass() != obj.getClass())
  114. return false;
  115. PlfLfo other = (PlfLfo) obj;
  116. return _lfoMac == other._lfoMac &&
  117. Arrays.equals(_rgLfo, other._rgLfo) &&
  118. Arrays.equals(_rgLfoData, other._rgLfoData);
  119. }
  120. /**
  121. * An unsigned integer that specifies the count of elements in both the
  122. * rgLfo and rgLfoData arrays.
  123. */
  124. public int getLfoMac()
  125. {
  126. return _lfoMac;
  127. }
  128. public int getIlfoByLsid( int lsid )
  129. {
  130. for ( int i = 0; i < _lfoMac; i++ )
  131. {
  132. if ( _rgLfo[i].getLsid() == lsid )
  133. {
  134. return i + 1;
  135. }
  136. }
  137. throw new NoSuchElementException( "LFO with lsid " + lsid
  138. + " not found" );
  139. }
  140. /**
  141. * @param ilfo 1-based index
  142. * @return The {@link LFO} stored at the given index
  143. */
  144. public LFO getLfo( int ilfo ) throws NoSuchElementException
  145. {
  146. if ( ilfo <= 0 || ilfo > _lfoMac )
  147. {
  148. throw new NoSuchElementException( "LFO with ilfo " + ilfo
  149. + " not found. lfoMac is " + _lfoMac );
  150. }
  151. return _rgLfo[ilfo - 1];
  152. }
  153. /**
  154. * @param ilfo 1-based index
  155. * @return The {@link LFOData} stored at the given index
  156. */
  157. public LFOData getLfoData( int ilfo ) throws NoSuchElementException
  158. {
  159. if ( ilfo <= 0 || ilfo > _lfoMac )
  160. {
  161. throw new NoSuchElementException( "LFOData with ilfo " + ilfo
  162. + " not found. lfoMac is " + _lfoMac );
  163. }
  164. return _rgLfoData[ilfo - 1];
  165. }
  166. @Override
  167. public int hashCode() {
  168. return Arrays.deepHashCode(new Object[]{_lfoMac, _rgLfo, _rgLfoData});
  169. }
  170. void writeTo( FileInformationBlock fib, ByteArrayOutputStream outputStream )
  171. throws IOException
  172. {
  173. final int offset = outputStream.size();
  174. fib.setFcPlfLfo( offset );
  175. LittleEndian.putUInt( _lfoMac, outputStream );
  176. byte[] bs = new byte[LFOAbstractType.getSize() * _lfoMac];
  177. for ( int i = 0; i < _lfoMac; i++ )
  178. {
  179. _rgLfo[i].serialize( bs, i * LFOAbstractType.getSize() );
  180. }
  181. outputStream.write( bs, 0, LFOAbstractType.getSize() * _lfoMac );
  182. for ( int i = 0; i < _lfoMac; i++ )
  183. {
  184. _rgLfoData[i].writeTo( outputStream );
  185. }
  186. fib.setLcbPlfLfo( outputStream.size() - offset );
  187. }
  188. }