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.

FileInformationBlock.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.model;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Modifier;
  20. import java.util.HashSet;
  21. import java.util.Locale;
  22. import org.apache.logging.log4j.LogManager;
  23. import org.apache.logging.log4j.Logger;
  24. import org.apache.poi.hwpf.model.types.FibBaseAbstractType;
  25. import org.apache.poi.hwpf.model.types.FibRgW97AbstractType;
  26. import org.apache.poi.util.IOUtils;
  27. import org.apache.poi.util.Internal;
  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. * <p>The File Information Block (FIB). Holds pointers
  33. * to various bits of the file, and lots of flags which
  34. * specify properties of the document.
  35. * <ul>
  36. * <li>The {@link FibBase} class, holds the first 32 bytes.</li>
  37. * <li>The next part, the fibRgW / FibRgW97, is handled by the {@link FibRgW97}.</li>
  38. * <li>The next part, the fibRgLw / FibRgLw97, is handled by the {@link FibRgLw97}.</li>
  39. * <li>Finally, the rest of the fields are handled by the {@link FIBFieldHandler}.</li>
  40. * </ul>
  41. */
  42. @Internal
  43. public final class FileInformationBlock {
  44. //arbitrarily selected; may need to increase
  45. private static final int MAX_RECORD_LENGTH = 100_000;
  46. private static final Logger LOG = LogManager.getLogger(FileInformationBlock.class);
  47. private final FibBase _fibBase;
  48. private final int _csw;
  49. private final FibRgW97 _fibRgW;
  50. private final int _cslw;
  51. private final FibRgLw _fibRgLw;
  52. private int _cbRgFcLcb;
  53. private FIBFieldHandler _fieldHandler;
  54. private int _cswNew;
  55. private final int _nFibNew;
  56. private final byte[] _fibRgCswNew;
  57. /** Creates a new instance of FileInformationBlock */
  58. public FileInformationBlock( byte[] mainDocument )
  59. {
  60. int offset = 0;
  61. _fibBase = new FibBase( mainDocument, offset );
  62. offset = FibBaseAbstractType.getSize();
  63. assert offset == 32;
  64. _csw = LittleEndian.getUShort( mainDocument, offset );
  65. offset += LittleEndianConsts.SHORT_SIZE;
  66. _fibRgW = new FibRgW97( mainDocument, offset );
  67. offset += FibRgW97AbstractType.getSize();
  68. assert offset == 62;
  69. _cslw = LittleEndian.getUShort( mainDocument, offset );
  70. offset += LittleEndianConsts.SHORT_SIZE;
  71. if ( _fibBase.getNFib() < 105 )
  72. {
  73. _fibRgLw = new FibRgLw95( mainDocument, offset );
  74. offset += FibRgLw97.getSize();
  75. // magic number, run tests after changes
  76. _cbRgFcLcb = 74;
  77. // skip fibRgFcLcbBlob (read later at fillVariableFields)
  78. offset += _cbRgFcLcb * LittleEndianConsts.INT_SIZE * 2;
  79. _cswNew = LittleEndian.getUShort( mainDocument, offset );
  80. offset += LittleEndianConsts.SHORT_SIZE;
  81. _cswNew = 0;
  82. _nFibNew = -1;
  83. _fibRgCswNew = new byte[0];
  84. return;
  85. }
  86. _fibRgLw = new FibRgLw97( mainDocument, offset );
  87. offset += FibRgLw97.getSize();
  88. assert offset == 152;
  89. _cbRgFcLcb = LittleEndian.getUShort( mainDocument, offset );
  90. offset += LittleEndianConsts.SHORT_SIZE;
  91. // skip fibRgFcLcbBlob (read later at fillVariableFields)
  92. offset += _cbRgFcLcb * LittleEndianConsts.INT_SIZE * 2;
  93. _cswNew = LittleEndian.getUShort( mainDocument, offset );
  94. offset += LittleEndianConsts.SHORT_SIZE;
  95. if ( _cswNew != 0 )
  96. {
  97. _nFibNew = LittleEndian.getUShort( mainDocument, offset );
  98. offset += LittleEndianConsts.SHORT_SIZE;
  99. // first short is already read as _nFibNew
  100. final int fibRgCswNewLength = ( _cswNew - 1 ) * LittleEndianConsts.SHORT_SIZE;
  101. _fibRgCswNew = IOUtils.safelyClone(mainDocument, offset, fibRgCswNewLength, MAX_RECORD_LENGTH);
  102. }
  103. else
  104. {
  105. _nFibNew = -1;
  106. _fibRgCswNew = new byte[0];
  107. }
  108. assertCbRgFcLcb();
  109. assertCswNew();
  110. }
  111. private void assertCbRgFcLcb()
  112. {
  113. int nfib = getNFib();
  114. String nfibHex = String.format(Locale.ROOT, "%04X", nfib);
  115. // Note - CommonCrawl shows there's more variation in these than
  116. // the documentation suggests, so accept common clusters around
  117. // the "correct" value as well
  118. switch ( nfib )
  119. {
  120. case 0x0071:
  121. // Found in CommonCrawl corpus but not in the docs...
  122. break;
  123. case 0x00BE:
  124. case 0x00BF:
  125. case 0x00C0:
  126. case 0x00C1: // Docs "official"
  127. case 0x00C2:
  128. case 0x00C3:
  129. assertCbRgFcLcb(nfibHex, 0x005D, "0x005D", _cbRgFcLcb );
  130. break;
  131. case 0x00D8:
  132. case 0x00D9: // Docs "official"
  133. assertCbRgFcLcb(nfibHex, 0x006C, "0x006C", _cbRgFcLcb );
  134. break;
  135. case 0x0101:
  136. assertCbRgFcLcb( "0x0101", 0x0088, "0x0088", _cbRgFcLcb );
  137. break;
  138. // TODO Is CommonCrawl 265 = 0x109 the one above or below?
  139. case 0x010B:
  140. case 0x010C: // Docs "official"
  141. assertCbRgFcLcb(nfibHex, 0x00A4, "0x00A4", _cbRgFcLcb );
  142. break;
  143. case 0x0112:
  144. assertCbRgFcLcb( "0x0112", 0x00B7, "0x00B7", _cbRgFcLcb );
  145. break;
  146. default:
  147. /* The Word spec has a much smaller list of "valid" values
  148. * to what the large CommonCrawl corpus contains!
  149. */
  150. LOG.atWarn().log("Invalid file format version number: {}({})", box(nfib),nfibHex);
  151. }
  152. }
  153. private static void assertCbRgFcLcb( final String strNFib,
  154. final int expectedCbRgFcLcb, final String strCbRgFcLcb,
  155. final int cbRgFcLcb )
  156. {
  157. if ( cbRgFcLcb == expectedCbRgFcLcb )
  158. return;
  159. LOG.atWarn().log("Since FIB.nFib == {} value of FIB.cbRgFcLcb MUST be {}, not 0x{}", strNFib, strCbRgFcLcb, Integer.toHexString(cbRgFcLcb));
  160. }
  161. private void assertCswNew()
  162. {
  163. switch ( getNFib() )
  164. {
  165. case 0x00C1:
  166. assertCswNew( "0x00C1", 0x0000, "0x0000", _cswNew );
  167. break;
  168. case 0x00D9:
  169. assertCswNew( "0x00D9", 0x0002, "0x0002", _cswNew );
  170. break;
  171. case 0x0101:
  172. assertCswNew( "0x0101", 0x0002, "0x0002", _cswNew );
  173. break;
  174. case 0x010C:
  175. assertCswNew( "0x010C", 0x0002, "0x0002", _cswNew );
  176. break;
  177. case 0x0112:
  178. assertCswNew( "0x0112", 0x0005, "0x0005", _cswNew );
  179. break;
  180. default:
  181. LOG.atWarn().log("Invalid file format version number: {}", box(getNFib()));
  182. }
  183. }
  184. private static void assertCswNew( final String strNFib,
  185. final int expectedCswNew, final String strExpectedCswNew,
  186. final int cswNew )
  187. {
  188. if ( cswNew == expectedCswNew )
  189. return;
  190. LOG.atWarn().log("Since FIB.nFib == {} value of FIB.cswNew MUST be {}, not 0x{}", strNFib, strExpectedCswNew, Integer.toHexString(cswNew));
  191. }
  192. public void fillVariableFields( byte[] mainDocument, byte[] tableStream )
  193. {
  194. /*
  195. * Listed fields won't be treat as UnhandledDataStructure. For all other
  196. * fields FIBFieldHandler will load it content into
  197. * UnhandledDataStructure and save them on save.
  198. */
  199. HashSet<Integer> knownFieldSet = new HashSet<>();
  200. knownFieldSet.add(FIBFieldHandler.STSHF);
  201. knownFieldSet.add(FIBFieldHandler.CLX);
  202. knownFieldSet.add(FIBFieldHandler.DOP);
  203. knownFieldSet.add(FIBFieldHandler.PLCFBTECHPX);
  204. knownFieldSet.add(FIBFieldHandler.PLCFBTEPAPX);
  205. knownFieldSet.add(FIBFieldHandler.PLCFSED);
  206. knownFieldSet.add(FIBFieldHandler.PLFLST);
  207. knownFieldSet.add(FIBFieldHandler.PLFLFO);
  208. // field info
  209. for ( FieldsDocumentPart part : FieldsDocumentPart.values() )
  210. knownFieldSet.add(part.getFibFieldsField());
  211. // bookmarks
  212. knownFieldSet.add(FIBFieldHandler.PLCFBKF);
  213. knownFieldSet.add(FIBFieldHandler.PLCFBKL);
  214. knownFieldSet.add(FIBFieldHandler.STTBFBKMK);
  215. // notes
  216. for ( NoteType noteType : NoteType.values() ) {
  217. knownFieldSet.add(noteType.getFibDescriptorsFieldIndex());
  218. knownFieldSet.add(noteType.getFibTextPositionsFieldIndex());
  219. }
  220. knownFieldSet.add( FIBFieldHandler.STTBFFFN );
  221. knownFieldSet.add( FIBFieldHandler.STTBFRMARK );
  222. knownFieldSet.add( FIBFieldHandler.STTBSAVEDBY );
  223. knownFieldSet.add( FIBFieldHandler.MODIFIED );
  224. _fieldHandler = new FIBFieldHandler( mainDocument, 154, _cbRgFcLcb,
  225. tableStream, knownFieldSet, true );
  226. }
  227. @Override
  228. public String toString()
  229. {
  230. StringBuilder stringBuilder = new StringBuilder( );
  231. stringBuilder.append( _fibBase );
  232. stringBuilder.append( "[FIB2]\n" );
  233. stringBuilder.append( "\tSubdocuments info:\n" );
  234. for ( SubdocumentType type : SubdocumentType.values() )
  235. {
  236. stringBuilder.append( "\t\t" );
  237. stringBuilder.append( type );
  238. stringBuilder.append( " has length of " );
  239. stringBuilder.append( getSubdocumentTextStreamLength( type ) );
  240. stringBuilder.append( " char(s)\n" );
  241. }
  242. stringBuilder.append( "\tFields PLCF info:\n" );
  243. for ( FieldsDocumentPart part : FieldsDocumentPart.values() )
  244. {
  245. stringBuilder.append( "\t\t" );
  246. stringBuilder.append( part );
  247. stringBuilder.append( ": PLCF starts at " );
  248. stringBuilder.append( getFieldsPlcfOffset( part ) );
  249. stringBuilder.append( " and have length of " );
  250. stringBuilder.append( getFieldsPlcfLength( part ) );
  251. stringBuilder.append( "\n" );
  252. }
  253. stringBuilder.append( "\tNotes PLCF info:\n" );
  254. for ( NoteType noteType : NoteType.values() )
  255. {
  256. stringBuilder.append( "\t\t" );
  257. stringBuilder.append( noteType );
  258. stringBuilder.append( ": descriptions starts " );
  259. stringBuilder.append( getNotesDescriptorsOffset( noteType ) );
  260. stringBuilder.append( " and have length of " );
  261. stringBuilder.append( getNotesDescriptorsSize( noteType ) );
  262. stringBuilder.append( " bytes\n" );
  263. stringBuilder.append( "\t\t" );
  264. stringBuilder.append( noteType );
  265. stringBuilder.append( ": text positions starts " );
  266. stringBuilder.append( getNotesTextPositionsOffset( noteType ) );
  267. stringBuilder.append( " and have length of " );
  268. stringBuilder.append( getNotesTextPositionsSize( noteType ) );
  269. stringBuilder.append( " bytes\n" );
  270. }
  271. stringBuilder.append( _fieldHandler );
  272. try
  273. {
  274. stringBuilder.append( "\tJava reflection info:\n" );
  275. for ( Method method : FileInformationBlock.class.getMethods() )
  276. {
  277. if ( !method.getName().startsWith( "get" )
  278. || !Modifier.isPublic( method.getModifiers() )
  279. || Modifier.isStatic( method.getModifiers() )
  280. || method.getParameterTypes().length > 0 )
  281. continue;
  282. stringBuilder.append( "\t\t" );
  283. stringBuilder.append( method.getName() );
  284. stringBuilder.append( " => " );
  285. stringBuilder.append( method.invoke( this ) );
  286. stringBuilder.append( "\n" );
  287. }
  288. }
  289. catch ( Exception exc )
  290. {
  291. stringBuilder.append("(exc: ").append(exc.getMessage()).append(")");
  292. }
  293. stringBuilder.append( "[/FIB2]\n" );
  294. return stringBuilder.toString();
  295. }
  296. public int getNFib()
  297. {
  298. if ( _cswNew == 0 )
  299. return _fibBase.getNFib();
  300. return _nFibNew;
  301. }
  302. public int getFcDop()
  303. {
  304. return _fieldHandler.getFieldOffset(FIBFieldHandler.DOP);
  305. }
  306. public void setFcDop(int fcDop)
  307. {
  308. _fieldHandler.setFieldOffset(FIBFieldHandler.DOP, fcDop);
  309. }
  310. public int getLcbDop()
  311. {
  312. return _fieldHandler.getFieldSize(FIBFieldHandler.DOP);
  313. }
  314. public void setLcbDop(int lcbDop)
  315. {
  316. _fieldHandler.setFieldSize(FIBFieldHandler.DOP, lcbDop);
  317. }
  318. public int getFcStshf()
  319. {
  320. return _fieldHandler.getFieldOffset(FIBFieldHandler.STSHF);
  321. }
  322. public int getLcbStshf()
  323. {
  324. return _fieldHandler.getFieldSize(FIBFieldHandler.STSHF);
  325. }
  326. public void setFcStshf(int fcStshf)
  327. {
  328. _fieldHandler.setFieldOffset(FIBFieldHandler.STSHF, fcStshf);
  329. }
  330. public void setLcbStshf(int lcbStshf)
  331. {
  332. _fieldHandler.setFieldSize(FIBFieldHandler.STSHF, lcbStshf);
  333. }
  334. public int getFcClx()
  335. {
  336. return _fieldHandler.getFieldOffset(FIBFieldHandler.CLX);
  337. }
  338. public int getLcbClx()
  339. {
  340. return _fieldHandler.getFieldSize(FIBFieldHandler.CLX);
  341. }
  342. public void setFcClx(int fcClx)
  343. {
  344. _fieldHandler.setFieldOffset(FIBFieldHandler.CLX, fcClx);
  345. }
  346. public void setLcbClx(int lcbClx)
  347. {
  348. _fieldHandler.setFieldSize(FIBFieldHandler.CLX, lcbClx);
  349. }
  350. public int getFcPlcfbteChpx()
  351. {
  352. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFBTECHPX);
  353. }
  354. public int getLcbPlcfbteChpx()
  355. {
  356. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFBTECHPX);
  357. }
  358. public void setFcPlcfbteChpx(int fcPlcfBteChpx)
  359. {
  360. _fieldHandler.setFieldOffset(FIBFieldHandler.PLCFBTECHPX, fcPlcfBteChpx);
  361. }
  362. public void setLcbPlcfbteChpx(int lcbPlcfBteChpx)
  363. {
  364. _fieldHandler.setFieldSize(FIBFieldHandler.PLCFBTECHPX, lcbPlcfBteChpx);
  365. }
  366. public int getFcPlcfbtePapx()
  367. {
  368. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFBTEPAPX);
  369. }
  370. public int getLcbPlcfbtePapx()
  371. {
  372. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFBTEPAPX);
  373. }
  374. public void setFcPlcfbtePapx(int fcPlcfBtePapx)
  375. {
  376. _fieldHandler.setFieldOffset(FIBFieldHandler.PLCFBTEPAPX, fcPlcfBtePapx);
  377. }
  378. public void setLcbPlcfbtePapx(int lcbPlcfBtePapx)
  379. {
  380. _fieldHandler.setFieldSize(FIBFieldHandler.PLCFBTEPAPX, lcbPlcfBtePapx);
  381. }
  382. public int getFcPlcfsed()
  383. {
  384. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFSED);
  385. }
  386. public int getLcbPlcfsed()
  387. {
  388. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFSED);
  389. }
  390. public void setFcPlcfsed(int fcPlcfSed)
  391. {
  392. _fieldHandler.setFieldOffset(FIBFieldHandler.PLCFSED, fcPlcfSed);
  393. }
  394. public void setLcbPlcfsed(int lcbPlcfSed)
  395. {
  396. _fieldHandler.setFieldSize(FIBFieldHandler.PLCFSED, lcbPlcfSed);
  397. }
  398. @Deprecated
  399. public int getFcPlcfLst()
  400. {
  401. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFLST);
  402. }
  403. /**
  404. * An unsigned integer that specifies an offset in the Table Stream. A
  405. * PlfLst that contains list formatting information begins at this offset.
  406. * An array of LVLs is appended to the PlfLst. lcbPlfLst does not account
  407. * for the array of LVLs. The size of the array of LVLs is specified by the
  408. * LSTFs in PlfLst. For each LSTF whose fSimpleList is set to 0x1, there is
  409. * one LVL in the array of LVLs that specifies the level formatting of the
  410. * single level in the list which corresponds to the LSTF. And, for each
  411. * LSTF whose fSimpleList is set to 0x0, there are 9 LVLs in the array of
  412. * LVLs that specify the level formatting of the respective levels in the
  413. * list which corresponds to the LSTF. This array of LVLs is in the same
  414. * respective order as the LSTFs in PlfLst. If lcbPlfLst is 0, fcPlfLst is
  415. * undefined and MUST be ignored.
  416. * <p>
  417. * Quote from
  418. * "[MS-DOC] -- v20110315, Word (.doc) Binary File Format; page 76 / 621"
  419. */
  420. public int getFcPlfLst()
  421. {
  422. return _fieldHandler.getFieldOffset( FIBFieldHandler.PLFLST );
  423. }
  424. @Deprecated
  425. public int getLcbPlcfLst()
  426. {
  427. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFLST);
  428. }
  429. public int getLcbPlfLst()
  430. {
  431. return _fieldHandler.getFieldSize( FIBFieldHandler.PLFLST );
  432. }
  433. @Deprecated
  434. public void setFcPlcfLst( int fcPlcfLst )
  435. {
  436. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFLST, fcPlcfLst );
  437. }
  438. public void setFcPlfLst( int fcPlfLst )
  439. {
  440. _fieldHandler.setFieldOffset( FIBFieldHandler.PLFLST, fcPlfLst );
  441. }
  442. @Deprecated
  443. public void setLcbPlcfLst( int lcbPlcfLst )
  444. {
  445. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFLST, lcbPlcfLst );
  446. }
  447. public void setLcbPlfLst( int lcbPlfLst )
  448. {
  449. _fieldHandler.setFieldSize( FIBFieldHandler.PLFLST, lcbPlfLst );
  450. }
  451. /**
  452. * An unsigned integer that specifies an offset in the Table Stream. A
  453. * PlfLfo that contains list formatting override information begins at this
  454. * offset. If lcbPlfLfo is zero, fcPlfLfo is undefined and MUST be ignored.
  455. * <p>
  456. * Quote from
  457. * "[MS-DOC] -- v20110315, Word (.doc) Binary File Format; page 76 / 621"
  458. */
  459. public int getFcPlfLfo()
  460. {
  461. return _fieldHandler.getFieldOffset( FIBFieldHandler.PLFLFO );
  462. }
  463. public int getLcbPlfLfo()
  464. {
  465. return _fieldHandler.getFieldSize(FIBFieldHandler.PLFLFO);
  466. }
  467. /**
  468. * @return Offset in table stream of the STTBF that records bookmark names
  469. * in the main document
  470. */
  471. public int getFcSttbfbkmk()
  472. {
  473. return _fieldHandler.getFieldOffset( FIBFieldHandler.STTBFBKMK );
  474. }
  475. public void setFcSttbfbkmk( int offset )
  476. {
  477. _fieldHandler.setFieldOffset( FIBFieldHandler.STTBFBKMK, offset );
  478. }
  479. /**
  480. * @return Count of bytes in Sttbfbkmk
  481. */
  482. public int getLcbSttbfbkmk()
  483. {
  484. return _fieldHandler.getFieldSize( FIBFieldHandler.STTBFBKMK );
  485. }
  486. public void setLcbSttbfbkmk( int length )
  487. {
  488. _fieldHandler.setFieldSize( FIBFieldHandler.STTBFBKMK, length );
  489. }
  490. /**
  491. * @return Offset in table stream of the PLCF that records the beginning CP
  492. * offsets of bookmarks in the main document. See BKF structure
  493. * definition.
  494. */
  495. public int getFcPlcfbkf()
  496. {
  497. return _fieldHandler.getFieldOffset( FIBFieldHandler.PLCFBKF );
  498. }
  499. public void setFcPlcfbkf( int offset )
  500. {
  501. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFBKF, offset );
  502. }
  503. /**
  504. * @return Count of bytes in Plcfbkf
  505. */
  506. public int getLcbPlcfbkf()
  507. {
  508. return _fieldHandler.getFieldSize( FIBFieldHandler.PLCFBKF );
  509. }
  510. public void setLcbPlcfbkf( int length )
  511. {
  512. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFBKF, length );
  513. }
  514. /**
  515. * @return Offset in table stream of the PLCF that records the ending CP
  516. * offsets of bookmarks recorded in the main document. No structure
  517. * is stored in this PLCF.
  518. */
  519. public int getFcPlcfbkl()
  520. {
  521. return _fieldHandler.getFieldOffset( FIBFieldHandler.PLCFBKL );
  522. }
  523. public void setFcPlcfbkl( int offset )
  524. {
  525. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFBKL, offset );
  526. }
  527. /**
  528. * @return Count of bytes in Plcfbkl
  529. */
  530. public int getLcbPlcfbkl()
  531. {
  532. return _fieldHandler.getFieldSize( FIBFieldHandler.PLCFBKL );
  533. }
  534. public void setLcbPlcfbkl( int length )
  535. {
  536. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFBKL, length );
  537. }
  538. public void setFcPlfLfo(int fcPlfLfo)
  539. {
  540. _fieldHandler.setFieldOffset(FIBFieldHandler.PLFLFO, fcPlfLfo);
  541. }
  542. public void setLcbPlfLfo(int lcbPlfLfo)
  543. {
  544. _fieldHandler.setFieldSize(FIBFieldHandler.PLFLFO, lcbPlfLfo);
  545. }
  546. public int getFcSttbfffn()
  547. {
  548. return _fieldHandler.getFieldOffset(FIBFieldHandler.STTBFFFN);
  549. }
  550. public int getLcbSttbfffn()
  551. {
  552. return _fieldHandler.getFieldSize(FIBFieldHandler.STTBFFFN);
  553. }
  554. public void setFcSttbfffn(int fcSttbFffn)
  555. {
  556. _fieldHandler.setFieldOffset(FIBFieldHandler.STTBFFFN, fcSttbFffn);
  557. }
  558. public void setLcbSttbfffn(int lcbSttbFffn)
  559. {
  560. _fieldHandler.setFieldSize(FIBFieldHandler.STTBFFFN, lcbSttbFffn);
  561. }
  562. public int getFcSttbfRMark()
  563. {
  564. return _fieldHandler.getFieldOffset(FIBFieldHandler.STTBFRMARK);
  565. }
  566. public int getLcbSttbfRMark()
  567. {
  568. return _fieldHandler.getFieldSize(FIBFieldHandler.STTBFRMARK);
  569. }
  570. public void setFcSttbfRMark(int fcSttbfRMark)
  571. {
  572. _fieldHandler.setFieldOffset(FIBFieldHandler.STTBFRMARK, fcSttbfRMark);
  573. }
  574. public void setLcbSttbfRMark(int lcbSttbfRMark)
  575. {
  576. _fieldHandler.setFieldSize(FIBFieldHandler.STTBFRMARK, lcbSttbfRMark);
  577. }
  578. /**
  579. * Return the offset to the PlcfHdd, in the table stream,
  580. * i.e. fcPlcfHdd
  581. */
  582. public int getPlcfHddOffset() {
  583. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFHDD);
  584. }
  585. /**
  586. * Return the size of the PlcfHdd, in the table stream,
  587. * i.e. lcbPlcfHdd
  588. */
  589. public int getPlcfHddSize() {
  590. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFHDD);
  591. }
  592. public void setPlcfHddOffset(int fcPlcfHdd) {
  593. _fieldHandler.setFieldOffset(FIBFieldHandler.PLCFHDD, fcPlcfHdd);
  594. }
  595. public void setPlcfHddSize(int lcbPlcfHdd) {
  596. _fieldHandler.setFieldSize(FIBFieldHandler.PLCFHDD, lcbPlcfHdd);
  597. }
  598. public int getFcSttbSavedBy()
  599. {
  600. return _fieldHandler.getFieldOffset(FIBFieldHandler.STTBSAVEDBY);
  601. }
  602. public int getLcbSttbSavedBy()
  603. {
  604. return _fieldHandler.getFieldSize(FIBFieldHandler.STTBSAVEDBY);
  605. }
  606. public void setFcSttbSavedBy(int fcSttbSavedBy)
  607. {
  608. _fieldHandler.setFieldOffset(FIBFieldHandler.STTBSAVEDBY, fcSttbSavedBy);
  609. }
  610. public void setLcbSttbSavedBy(int fcSttbSavedBy)
  611. {
  612. _fieldHandler.setFieldSize(FIBFieldHandler.STTBSAVEDBY, fcSttbSavedBy);
  613. }
  614. public int getModifiedLow()
  615. {
  616. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLFLFO);
  617. }
  618. public int getModifiedHigh()
  619. {
  620. return _fieldHandler.getFieldSize(FIBFieldHandler.PLFLFO);
  621. }
  622. public void setModifiedLow(int modifiedLow)
  623. {
  624. _fieldHandler.setFieldOffset(FIBFieldHandler.PLFLFO, modifiedLow);
  625. }
  626. public void setModifiedHigh(int modifiedHigh)
  627. {
  628. _fieldHandler.setFieldSize(FIBFieldHandler.PLFLFO, modifiedHigh);
  629. }
  630. /**
  631. * How many bytes of the main stream contain real data.
  632. */
  633. public int getCbMac()
  634. {
  635. return _fibRgLw.getCbMac();
  636. }
  637. /**
  638. * Updates the count of the number of bytes in the
  639. * main stream which contain real data
  640. */
  641. public void setCbMac( int cbMac )
  642. {
  643. _fibRgLw.setCbMac( cbMac );
  644. }
  645. /**
  646. * @return length of specified subdocument text stream in characters
  647. */
  648. public int getSubdocumentTextStreamLength( SubdocumentType type )
  649. {
  650. if ( type == null )
  651. throw new IllegalArgumentException( "argument 'type' is null" );
  652. return _fibRgLw.getSubdocumentTextStreamLength( type );
  653. }
  654. public void setSubdocumentTextStreamLength( SubdocumentType type, int length )
  655. {
  656. if ( type == null )
  657. throw new IllegalArgumentException( "argument 'type' is null" );
  658. if ( length < 0 )
  659. throw new IllegalArgumentException(
  660. "Subdocument length can't be less than 0 (passed value is "
  661. + length + "). " + "If there is no subdocument "
  662. + "length must be set to zero." );
  663. _fibRgLw.setSubdocumentTextStreamLength( type, length );
  664. }
  665. public void clearOffsetsSizes()
  666. {
  667. _fieldHandler.clearFields();
  668. }
  669. public int getFieldsPlcfOffset( FieldsDocumentPart part )
  670. {
  671. return _fieldHandler.getFieldOffset( part.getFibFieldsField() );
  672. }
  673. public int getFieldsPlcfLength( FieldsDocumentPart part )
  674. {
  675. return _fieldHandler.getFieldSize( part.getFibFieldsField() );
  676. }
  677. public void setFieldsPlcfOffset( FieldsDocumentPart part, int offset )
  678. {
  679. _fieldHandler.setFieldOffset( part.getFibFieldsField(), offset );
  680. }
  681. public void setFieldsPlcfLength( FieldsDocumentPart part, int length )
  682. {
  683. _fieldHandler.setFieldSize( part.getFibFieldsField(), length );
  684. }
  685. @Deprecated
  686. public int getFcPlcffldAtn()
  687. {
  688. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDATN);
  689. }
  690. @Deprecated
  691. public int getLcbPlcffldAtn()
  692. {
  693. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDATN);
  694. }
  695. @Deprecated
  696. public void setFcPlcffldAtn( int offset )
  697. {
  698. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDATN, offset );
  699. }
  700. @Deprecated
  701. public void setLcbPlcffldAtn( int size )
  702. {
  703. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDATN, size );
  704. }
  705. @Deprecated
  706. public int getFcPlcffldEdn()
  707. {
  708. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDEDN);
  709. }
  710. @Deprecated
  711. public int getLcbPlcffldEdn()
  712. {
  713. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDEDN);
  714. }
  715. @Deprecated
  716. public void setFcPlcffldEdn( int offset )
  717. {
  718. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDEDN, offset );
  719. }
  720. @Deprecated
  721. public void setLcbPlcffldEdn( int size )
  722. {
  723. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDEDN, size );
  724. }
  725. @Deprecated
  726. public int getFcPlcffldFtn()
  727. {
  728. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDFTN);
  729. }
  730. @Deprecated
  731. public int getLcbPlcffldFtn()
  732. {
  733. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDFTN);
  734. }
  735. @Deprecated
  736. public void setFcPlcffldFtn( int offset )
  737. {
  738. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDFTN, offset );
  739. }
  740. @Deprecated
  741. public void setLcbPlcffldFtn( int size )
  742. {
  743. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDFTN, size );
  744. }
  745. @Deprecated
  746. public int getFcPlcffldHdr()
  747. {
  748. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDHDR);
  749. }
  750. @Deprecated
  751. public int getLcbPlcffldHdr()
  752. {
  753. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDHDR);
  754. }
  755. @Deprecated
  756. public void setFcPlcffldHdr( int offset )
  757. {
  758. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDHDR, offset );
  759. }
  760. @Deprecated
  761. public void setLcbPlcffldHdr( int size )
  762. {
  763. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDHDR, size );
  764. }
  765. @Deprecated
  766. public int getFcPlcffldHdrtxbx()
  767. {
  768. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDHDRTXBX);
  769. }
  770. @Deprecated
  771. public int getLcbPlcffldHdrtxbx()
  772. {
  773. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDHDRTXBX);
  774. }
  775. @Deprecated
  776. public void setFcPlcffldHdrtxbx( int offset )
  777. {
  778. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDHDRTXBX, offset );
  779. }
  780. @Deprecated
  781. public void setLcbPlcffldHdrtxbx( int size )
  782. {
  783. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDHDRTXBX, size );
  784. }
  785. @Deprecated
  786. public int getFcPlcffldMom()
  787. {
  788. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDMOM);
  789. }
  790. @Deprecated
  791. public int getLcbPlcffldMom()
  792. {
  793. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDMOM);
  794. }
  795. @Deprecated
  796. public void setFcPlcffldMom( int offset )
  797. {
  798. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDMOM, offset );
  799. }
  800. @Deprecated
  801. public void setLcbPlcffldMom( int size )
  802. {
  803. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDMOM, size );
  804. }
  805. @Deprecated
  806. public int getFcPlcffldTxbx()
  807. {
  808. return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDTXBX);
  809. }
  810. @Deprecated
  811. public int getLcbPlcffldTxbx()
  812. {
  813. return _fieldHandler.getFieldSize(FIBFieldHandler.PLCFFLDTXBX);
  814. }
  815. @Deprecated
  816. public void setFcPlcffldTxbx( int offset )
  817. {
  818. _fieldHandler.setFieldOffset( FIBFieldHandler.PLCFFLDTXBX, offset );
  819. }
  820. @Deprecated
  821. public void setLcbPlcffldTxbx( int size )
  822. {
  823. _fieldHandler.setFieldSize( FIBFieldHandler.PLCFFLDTXBX, size );
  824. }
  825. public int getFSPAPlcfOffset( FSPADocumentPart part )
  826. {
  827. return _fieldHandler.getFieldOffset( part.getFibFieldsField() );
  828. }
  829. public int getFSPAPlcfLength( FSPADocumentPart part )
  830. {
  831. return _fieldHandler.getFieldSize( part.getFibFieldsField() );
  832. }
  833. public void setFSPAPlcfOffset( FSPADocumentPart part, int offset )
  834. {
  835. _fieldHandler.setFieldOffset( part.getFibFieldsField(), offset );
  836. }
  837. public void setFSPAPlcfLength( FSPADocumentPart part, int length )
  838. {
  839. _fieldHandler.setFieldSize( part.getFibFieldsField(), length );
  840. }
  841. /**
  842. * @return Offset in the Table Stream at which the {@link OfficeArtContent} exists.
  843. */
  844. public int getFcDggInfo()
  845. {
  846. return _fieldHandler.getFieldOffset(FIBFieldHandler.DGGINFO);
  847. }
  848. /**
  849. * Returns the size, in bytes, of the {@link OfficeArtContent} at the offset {@link #getFcDggInfo()}.
  850. * <p>
  851. * If {@code 0}, there MUST NOT be any drawings in the document.
  852. *
  853. * @return Size, in bytes, of the {@link OfficeArtContent} at the offset {@link #getFcDggInfo()}.
  854. */
  855. public int getLcbDggInfo()
  856. {
  857. return _fieldHandler.getFieldSize(FIBFieldHandler.DGGINFO);
  858. }
  859. public int getNotesDescriptorsOffset( NoteType noteType )
  860. {
  861. return _fieldHandler.getFieldOffset( noteType
  862. .getFibDescriptorsFieldIndex() );
  863. }
  864. public void setNotesDescriptorsOffset( NoteType noteType, int offset )
  865. {
  866. _fieldHandler.setFieldOffset( noteType.getFibDescriptorsFieldIndex(),
  867. offset );
  868. }
  869. public int getNotesDescriptorsSize( NoteType noteType )
  870. {
  871. return _fieldHandler.getFieldSize( noteType
  872. .getFibDescriptorsFieldIndex() );
  873. }
  874. public void setNotesDescriptorsSize( NoteType noteType, int offset )
  875. {
  876. _fieldHandler.setFieldSize( noteType.getFibDescriptorsFieldIndex(),
  877. offset );
  878. }
  879. public int getNotesTextPositionsOffset( NoteType noteType )
  880. {
  881. return _fieldHandler.getFieldOffset( noteType
  882. .getFibTextPositionsFieldIndex() );
  883. }
  884. public void setNotesTextPositionsOffset( NoteType noteType, int offset )
  885. {
  886. _fieldHandler.setFieldOffset( noteType.getFibTextPositionsFieldIndex(),
  887. offset );
  888. }
  889. public int getNotesTextPositionsSize( NoteType noteType )
  890. {
  891. return _fieldHandler.getFieldSize( noteType
  892. .getFibTextPositionsFieldIndex() );
  893. }
  894. public void setNotesTextPositionsSize( NoteType noteType, int offset )
  895. {
  896. _fieldHandler.setFieldSize( noteType.getFibTextPositionsFieldIndex(),
  897. offset );
  898. }
  899. public void writeTo( byte[] mainStream, ByteArrayOutputStream tableStream )
  900. throws IOException
  901. {
  902. _cbRgFcLcb = _fieldHandler.getFieldsCount();
  903. _fibBase.serialize( mainStream, 0 );
  904. int offset = FibBaseAbstractType.getSize();
  905. LittleEndian.putUShort( mainStream, offset, _csw );
  906. offset += LittleEndianConsts.SHORT_SIZE;
  907. _fibRgW.serialize( mainStream, offset );
  908. offset += FibRgW97AbstractType.getSize();
  909. LittleEndian.putUShort( mainStream, offset, _cslw );
  910. offset += LittleEndianConsts.SHORT_SIZE;
  911. ( (FibRgLw97) _fibRgLw ).serialize( mainStream, offset );
  912. offset += FibRgLw97.getSize();
  913. LittleEndian.putUShort( mainStream, offset, _cbRgFcLcb );
  914. offset += LittleEndianConsts.SHORT_SIZE;
  915. _fieldHandler.writeTo( mainStream, offset, tableStream );
  916. offset += _cbRgFcLcb * LittleEndianConsts.INT_SIZE * 2;
  917. LittleEndian.putUShort( mainStream, offset, _cswNew );
  918. offset += LittleEndianConsts.SHORT_SIZE;
  919. if ( _cswNew != 0 )
  920. {
  921. LittleEndian.putUShort( mainStream, offset, _nFibNew );
  922. offset += LittleEndianConsts.SHORT_SIZE;
  923. System.arraycopy( _fibRgCswNew, 0, mainStream, offset,
  924. _fibRgCswNew.length );
  925. offset += _fibRgCswNew.length;
  926. }
  927. }
  928. public int getSize()
  929. {
  930. return FibBaseAbstractType.getSize() + LittleEndianConsts.SHORT_SIZE + FibRgW97AbstractType.getSize()
  931. + LittleEndianConsts.SHORT_SIZE + FibRgLw97.getSize()
  932. + LittleEndianConsts.SHORT_SIZE + _fieldHandler.sizeInBytes();
  933. }
  934. public FibBase getFibBase()
  935. {
  936. return _fibBase;
  937. }
  938. }