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.

TestRecordLister.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.hssf.dev;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.PrintWriter;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import org.apache.commons.io.output.NullWriter;
  23. import org.apache.poi.hssf.record.ContinueRecord;
  24. import org.apache.poi.hssf.record.Record;
  25. import org.apache.poi.hssf.record.RecordFactory;
  26. import org.apache.poi.hssf.record.RecordInputStream;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.util.RecordFormatException;
  29. /**
  30. * This is a low-level debugging class, which simply prints out what records come in what order.
  31. * Most people will want to use {@link BiffViewer} or {@link TestEFBiffViewer}, but this can be handy when
  32. * trying to make sense of {@link ContinueRecord} special cases.
  33. * <p>
  34. * Output is of the form:
  35. * SID - Length - Type (if known)
  36. * byte0 byte1 byte2 byte3 .... byte(n-4) byte(n-3) byte(n-2) byte(n-1)
  37. */
  38. class TestRecordLister extends BaseTestIteratingXLS {
  39. @Override
  40. protected Map<String, Class<? extends Throwable>> getExcludes() {
  41. Map<String, Class<? extends Throwable>> excludes = super.getExcludes();
  42. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5786329142919168.xls", RecordFormatException.class);
  43. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5889658057523200.xls", IndexOutOfBoundsException.class);
  44. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5175219985448960.xls", RecordFormatException.class);
  45. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6137883240824832.xls", RecordFormatException.class);
  46. return excludes;
  47. }
  48. @Override
  49. void runOneFile(File fileIn) throws IOException {
  50. // replace it with System.out if you like it more verbatim
  51. PrintWriter out = new PrintWriter(NullWriter.INSTANCE);
  52. try (POIFSFileSystem fs = new POIFSFileSystem(fileIn, true);
  53. InputStream din = BiffViewer.getPOIFSInputStream(fs)) {
  54. RecordInputStream rinp = new RecordInputStream(din);
  55. while (rinp.hasNextRecord()) {
  56. int sid = rinp.getNextSid();
  57. rinp.nextRecord();
  58. int size = rinp.available();
  59. Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
  60. out.printf(Locale.ROOT, "%1$#06x (%1$04d) - %2$#05x (%2$03d) bytes", sid, size);
  61. if (clz != null) {
  62. out.print(" \t");
  63. out.print(clz.getSimpleName());
  64. }
  65. out.println();
  66. byte[] data = rinp.readRemainder();
  67. if (data.length > 0) {
  68. out.print(" ");
  69. out.println(formatData(data));
  70. }
  71. }
  72. }
  73. }
  74. /*
  75. private static String formatSID(int sid) {
  76. String hex = Integer.toHexString(sid);
  77. String dec = Integer.toString(sid);
  78. StringBuilder s = new StringBuilder();
  79. s.append("0x");
  80. for (int i = hex.length(); i < 4; i++) {
  81. s.append('0');
  82. }
  83. s.append(hex);
  84. s.append(" (");
  85. for (int i = dec.length(); i < 4; i++) {
  86. s.append('0');
  87. }
  88. s.append(dec);
  89. s.append(")");
  90. return s.toString();
  91. }
  92. private static String formatSize(int size) {
  93. String hex = Integer.toHexString(size);
  94. String dec = Integer.toString(size);
  95. final String MAX_DIGITS = "000";
  96. StringBuilder s = new StringBuilder();
  97. s.append(MAX_DIGITS, 0, Math.max(MAX_DIGITS.length()-hex.length(),0));
  98. s.append(hex);
  99. s.append(" (");
  100. s.append(MAX_DIGITS, 0, Math.max(MAX_DIGITS.length()-dec.length(),0));
  101. s.append(dec);
  102. s.append(')');
  103. return s.toString();
  104. }*/
  105. private static String formatData(byte[] data) {
  106. if (data == null || data.length == 0) {
  107. return "";
  108. }
  109. StringBuilder s = new StringBuilder();
  110. // If possible, do first 4 and last 4 bytes
  111. final int MAX_BYTES = 9;
  112. int bLen = Math.min(data.length, MAX_BYTES);
  113. for (int i=0; i<bLen; i++) {
  114. if (i>0) {
  115. s.append(' ');
  116. }
  117. int b;
  118. if (i<MAX_BYTES/2) {
  119. b = data[i];
  120. } else if (i == MAX_BYTES/2 && data.length > MAX_BYTES) {
  121. s.append("...");
  122. continue;
  123. } else {
  124. b = data[data.length-(bLen-i)];
  125. }
  126. // byte to hex
  127. if (b < 0) {
  128. b += 256;
  129. }
  130. if (b < 16) {
  131. s.append('0');
  132. }
  133. s.append(Integer.toHexString(b));
  134. }
  135. return s.toString();
  136. }
  137. }