PageRenderTime 53ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/hwpf/model/FIBFieldHandler.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 307 lines | 249 code | 26 blank | 32 comment | 12 complexity | 2f26962a9383030e63d5ec81359d7573 MD5 | raw file
Possible License(s): Apache-2.0
  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.IOException;
  17. import java.util.Arrays;
  18. import java.util.HashMap;
  19. import java.util.HashSet;
  20. import java.util.Map;
  21. import org.apache.poi.hwpf.model.io.HWPFOutputStream;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.POILogFactory;
  25. import org.apache.poi.util.POILogger;
  26. @Internal
  27. public final class FIBFieldHandler
  28. {
  29. // 154 == 0x009A; 158 == 0x009E
  30. public static final int STSHFORIG = 0;
  31. public static final int STSHF = 1;
  32. public static final int PLCFFNDREF = 2;
  33. public static final int PLCFFNDTXT = 3;
  34. public static final int PLCFANDREF = 4;
  35. public static final int PLCFANDTXT = 5;
  36. public static final int PLCFSED = 6;
  37. public static final int PLCFPAD = 7;
  38. public static final int PLCFPHE = 8;
  39. public static final int STTBGLSY = 9;
  40. // 234 == 0x00EA; 238 == 0x00EE
  41. public static final int PLCFGLSY = 10;
  42. // 242 == 0200F2; 246 == 0x00F6
  43. public static final int PLCFHDD = 11;
  44. public static final int PLCFBTECHPX = 12;
  45. public static final int PLCFBTEPAPX = 13;
  46. public static final int PLCFSEA = 14;
  47. public static final int STTBFFFN = 15;
  48. public static final int PLCFFLDMOM = 16;
  49. public static final int PLCFFLDHDR = 17;
  50. // 298 == 0x12A; 302 == 0x12E
  51. public static final int PLCFFLDFTN = 18;
  52. // 306 == 0x132; 310 == 0x0136
  53. public static final int PLCFFLDATN = 19;
  54. // 314 == 0x013A; 318 == 0x013E
  55. public static final int PLCFFLDMCR = 20;
  56. // 322 == 0x0142; 326 == 0x0146
  57. public static final int STTBFBKMK = 21;
  58. // 330 == 0x014A; 334 == 0x014E
  59. public static final int PLCFBKF = 22;
  60. public static final int PLCFBKL = 23;
  61. public static final int CMDS = 24;
  62. public static final int PLCMCR = 25;
  63. public static final int STTBFMCR = 26;
  64. public static final int PRDRVR = 27;
  65. public static final int PRENVPORT = 28;
  66. public static final int PRENVLAND = 29;
  67. public static final int WSS = 30;
  68. // 402 == 0x0192; 406 == 0x0196
  69. public static final int DOP = 31;
  70. public static final int STTBFASSOC = 32;
  71. public static final int CLX = 33;
  72. public static final int PLCFPGDFTN = 34;
  73. public static final int AUTOSAVESOURCE = 35;
  74. public static final int GRPXSTATNOWNERS = 36;//validated
  75. public static final int STTBFATNBKMK = 37;
  76. public static final int PLCFDOAMOM = 38;
  77. public static final int PLCDOAHDR = 39;
  78. // 474 == 0x01DA; 478 == 0x01DE
  79. public static final int PLCSPAMOM = 40;
  80. // 482 == 0x01E2; 490 == 0x01E6
  81. public static final int PLCSPAHDR = 41;
  82. public static final int PLCFATNBKF = 42;
  83. // 498 == 0x01F2; 502 == 0x01F6
  84. public static final int PLCFATNBKL = 43;
  85. // 506 == 0x01FA; 510 == 0x01FE
  86. public static final int PMS = 44;
  87. // 514 == 0x0202; 518 == 0x0206
  88. public static final int FORMFLDSTTBS = 45;
  89. public static final int PLCFENDREF = 46;
  90. public static final int PLCFENDTXT = 47;
  91. public static final int PLCFFLDEDN = 48;
  92. public static final int PLCFPGDEDN = 49;
  93. // 554 == 0x022A; 558 == 0x022E -- long
  94. public static final int DGGINFO = 50;
  95. public static final int STTBFRMARK = 51;
  96. public static final int STTBCAPTION = 52;
  97. public static final int STTBAUTOCAPTION = 53;
  98. public static final int PLCFWKB = 54;
  99. public static final int PLCFSPL = 55;
  100. public static final int PLCFTXBXTXT = 56;
  101. // 610 -- 0x0262; 614 == 0x0266
  102. public static final int PLCFFLDTXBX = 57;// validated
  103. public static final int PLCFHDRTXBXTXT = 58;
  104. public static final int PLCFFLDHDRTXBX = 59;
  105. public static final int STWUSER = 60;
  106. public static final int STTBTTMBD = 61;
  107. public static final int UNUSED = 62;
  108. public static final int PGDMOTHER = 63;
  109. public static final int BKDMOTHER = 64;
  110. public static final int PGDFTN = 65;
  111. public static final int BKDFTN = 66;
  112. public static final int PGDEDN = 67;
  113. public static final int BKDEDN = 68;
  114. public static final int STTBFINTFLD = 69;
  115. public static final int ROUTESLIP = 70;
  116. public static final int STTBSAVEDBY = 71;
  117. public static final int STTBFNM = 72;
  118. public static final int PLFLST = 73;
  119. @Deprecated
  120. public static final int PLCFLST = PLFLST;
  121. public static final int PLFLFO = 74;
  122. public static final int PLCFTXBXBKD = 75;//validated
  123. public static final int PLCFTXBXHDRBKD = 76;
  124. public static final int DOCUNDO = 77;
  125. public static final int RGBUSE = 78;
  126. public static final int USP = 79;
  127. public static final int USKF = 80;
  128. public static final int PLCUPCRGBUSE = 81;
  129. public static final int PLCUPCUSP = 82;
  130. public static final int STTBGLSYSTYLE = 83;
  131. public static final int PLGOSL = 84;
  132. public static final int PLCOCX = 85;
  133. public static final int PLCFBTELVC = 86;
  134. public static final int MODIFIED = 87;
  135. public static final int PLCFLVC = 88;
  136. public static final int PLCASUMY = 89;
  137. public static final int PLCFGRAM = 90;
  138. public static final int STTBLISTNAMES = 91;
  139. public static final int STTBFUSSR = 92;
  140. private static POILogger log = POILogFactory.getLogger(FIBFieldHandler.class);
  141. private static final int FIELD_SIZE = LittleEndian.INT_SIZE * 2;
  142. private Map<Integer, UnhandledDataStructure> _unknownMap = new HashMap<Integer, UnhandledDataStructure>();
  143. private int[] _fields;
  144. FIBFieldHandler(byte[] mainStream, int offset, int cbRgFcLcb, byte[] tableStream,
  145. HashSet<Integer> offsetList, boolean areKnown)
  146. {
  147. _fields = new int[cbRgFcLcb * 2];
  148. for (int x = 0; x < cbRgFcLcb; x++)
  149. {
  150. int fieldOffset = (x * FIELD_SIZE) + offset;
  151. int dsOffset = LittleEndian.getInt(mainStream, fieldOffset);
  152. fieldOffset += LittleEndian.INT_SIZE;
  153. int dsSize = LittleEndian.getInt(mainStream, fieldOffset);
  154. if (offsetList.contains(Integer.valueOf(x)) ^ areKnown)
  155. {
  156. if (dsSize > 0)
  157. {
  158. if (dsOffset + dsSize > tableStream.length)
  159. {
  160. log.log(POILogger.WARN, "Unhandled data structure points to outside the buffer. " +
  161. "offset = " + dsOffset + ", length = " + dsSize +
  162. ", buffer length = " + tableStream.length);
  163. }
  164. else
  165. {
  166. UnhandledDataStructure unhandled = new UnhandledDataStructure(
  167. tableStream, dsOffset, dsSize);
  168. _unknownMap.put(Integer.valueOf(x), unhandled);
  169. }
  170. }
  171. }
  172. _fields[x*2] = dsOffset;
  173. _fields[(x*2) + 1] = dsSize;
  174. }
  175. }
  176. public void clearFields()
  177. {
  178. Arrays.fill(_fields, 0);
  179. }
  180. public int getFieldOffset(int field)
  181. {
  182. return _fields[field*2];
  183. }
  184. public int getFieldSize(int field)
  185. {
  186. return _fields[(field*2) + 1];
  187. }
  188. public void setFieldOffset(int field, int offset)
  189. {
  190. _fields[field*2] = offset;
  191. }
  192. public void setFieldSize(int field, int size)
  193. {
  194. _fields[(field*2) + 1] = size;
  195. }
  196. public int sizeInBytes()
  197. {
  198. return (_fields.length * LittleEndian.INT_SIZE);
  199. }
  200. public int getFieldsCount() {
  201. return _fields.length / 2;
  202. }
  203. void writeTo(byte[] mainStream, int offset, HWPFOutputStream tableStream)
  204. throws IOException
  205. {
  206. for (int x = 0; x < _fields.length/2; x++)
  207. {
  208. UnhandledDataStructure ds = _unknownMap.get(Integer.valueOf(x));
  209. if (ds != null)
  210. {
  211. _fields[x * 2] = tableStream.getOffset();
  212. LittleEndian.putInt(mainStream, offset, tableStream.getOffset());
  213. offset += LittleEndian.INT_SIZE;
  214. byte[] buf = ds.getBuf();
  215. tableStream.write(buf);
  216. _fields[(x * 2) + 1] = buf.length;
  217. LittleEndian.putInt(mainStream, offset, buf.length);
  218. offset += LittleEndian.INT_SIZE;
  219. }
  220. else
  221. {
  222. LittleEndian.putInt(mainStream, offset, _fields[x * 2]);
  223. offset += LittleEndian.INT_SIZE;
  224. LittleEndian.putInt(mainStream, offset, _fields[(x * 2) + 1]);
  225. offset += LittleEndian.INT_SIZE;
  226. }
  227. }
  228. }
  229. private static String leftPad( String text, int value, char padChar )
  230. {
  231. if ( text.length() >= value )
  232. return text;
  233. StringBuilder result = new StringBuilder();
  234. for ( int i = 0; i < ( value - text.length() ); i++ )
  235. {
  236. result.append( padChar );
  237. }
  238. result.append( text );
  239. return result.toString();
  240. }
  241. @Override
  242. public String toString()
  243. {
  244. StringBuilder result = new StringBuilder();
  245. result.append( "[FIBFieldHandler]:\n" );
  246. result.append( "\tFields:\n" );
  247. result.append( "\t" );
  248. result.append( leftPad( "Index", 8, ' ' ) );
  249. result.append( leftPad( "FIB offset", 15, ' ' ) );
  250. result.append( leftPad( "Offset", 8, ' ' ) );
  251. result.append( leftPad( "Size", 8, ' ' ) );
  252. result.append( '\n' );
  253. for ( int x = 0; x < _fields.length / 2; x++ )
  254. {
  255. result.append( '\t' );
  256. result.append( leftPad( Integer.toString( x ), 8, ' ' ) );
  257. result.append( leftPad(
  258. Integer.toString( 154 + x * LittleEndian.INT_SIZE * 2 ), 6,
  259. ' ' ) );
  260. result.append( " 0x" );
  261. result.append( leftPad(
  262. Integer.toHexString( 154 + x * LittleEndian.INT_SIZE * 2 ),
  263. 4, '0' ) );
  264. result.append( leftPad( Integer.toString( getFieldOffset( x ) ), 8,
  265. ' ' ) );
  266. result.append( leftPad( Integer.toString( getFieldSize( x ) ), 8,
  267. ' ' ) );
  268. UnhandledDataStructure structure = _unknownMap.get( Integer
  269. .valueOf( x ) );
  270. if ( structure != null )
  271. {
  272. result.append( " => Unknown structure of size " );
  273. result.append( structure._buf.length );
  274. }
  275. result.append( '\n' );
  276. }
  277. return result.toString();
  278. }
  279. }