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.

EvilUnclosedBRFixingInputStream.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.xssf.util;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. /**
  20. * This is a seriously sick fix for the fact that some .xlsx
  21. * files contain raw bits of HTML, without being escaped
  22. * or properly turned into XML.
  23. * The result is that they contain things like >br<,
  24. * which breaks the XML parsing.
  25. * This very sick InputStream wrapper attempts to spot
  26. * these go past, and fix them.
  27. * Only works for UTF-8 and US-ASCII based streams!
  28. * It should only be used where experience shows the problem
  29. * can occur...
  30. */
  31. public class EvilUnclosedBRFixingInputStream extends InputStream {
  32. private InputStream source;
  33. private byte[] spare;
  34. private static byte[] detect = new byte[] {
  35. (byte)'<', (byte)'b', (byte)'r', (byte)'>'
  36. };
  37. public EvilUnclosedBRFixingInputStream(InputStream source) {
  38. this.source = source;
  39. }
  40. /**
  41. * Warning - doesn't fix!
  42. */
  43. @Override
  44. public int read() throws IOException {
  45. return source.read();
  46. }
  47. @Override
  48. public int read(byte[] b, int off, int len) throws IOException {
  49. if(spare != null) {
  50. // This is risky, but spare is normally only a byte or two...
  51. System.arraycopy(spare, 0, b, off, spare.length);
  52. int ret = spare.length;
  53. spare = null;
  54. return ret;
  55. }
  56. int read = source.read(b, off, len);
  57. read = fixUp(b, off, read);
  58. return read;
  59. }
  60. @Override
  61. public int read(byte[] b) throws IOException {
  62. return this.read(b, 0, b.length);
  63. }
  64. private int fixUp(byte[] b, int offset, int read) {
  65. // Find places to fix
  66. ArrayList<Integer> fixAt = new ArrayList<Integer>();
  67. for(int i=offset; i<offset+read-4; i++) {
  68. boolean going = true;
  69. for(int j=0; j<detect.length && going; j++) {
  70. if(b[i+j] != detect[j]) {
  71. going = false;
  72. }
  73. }
  74. if(going) {
  75. fixAt.add(i);
  76. }
  77. }
  78. if(fixAt.size()==0) {
  79. return read;
  80. }
  81. // Save a bit, if needed to fit
  82. int overshoot = offset+read+fixAt.size() - b.length;
  83. if(overshoot > 0) {
  84. spare = new byte[overshoot];
  85. System.arraycopy(b, b.length-overshoot, spare, 0, overshoot);
  86. read -= overshoot;
  87. }
  88. // Fix them, in reverse order so the
  89. // positions are valid
  90. for(int j=fixAt.size()-1; j>=0; j--) {
  91. int i = fixAt.get(j);
  92. byte[] tmp = new byte[read-i-3];
  93. System.arraycopy(b, i+3, tmp, 0, tmp.length);
  94. b[i+3] = (byte)'/';
  95. System.arraycopy(tmp, 0, b, i+4, tmp.length);
  96. // It got one longer
  97. read++;
  98. }
  99. return read;
  100. }
  101. }