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.

Biff8DecryptingStream.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.record.crypto;
  16. import java.io.InputStream;
  17. import org.apache.poi.EncryptedDocumentException;
  18. import org.apache.poi.hssf.record.BiffHeaderInput;
  19. import org.apache.poi.util.LittleEndianInput;
  20. import org.apache.poi.util.LittleEndianInputStream;
  21. /**
  22. *
  23. * @author Josh Micich
  24. */
  25. public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndianInput {
  26. private final LittleEndianInput _le;
  27. private final Biff8Cipher _cipher;
  28. public Biff8DecryptingStream(InputStream in, int initialOffset, Biff8EncryptionKey key) {
  29. if (key instanceof Biff8RC4Key) {
  30. _cipher = new Biff8RC4(initialOffset, (Biff8RC4Key)key);
  31. } else if (key instanceof Biff8XORKey) {
  32. _cipher = new Biff8XOR(initialOffset, (Biff8XORKey)key);
  33. } else {
  34. throw new EncryptedDocumentException("Crypto API not supported yet.");
  35. }
  36. if (in instanceof LittleEndianInput) {
  37. // accessing directly is an optimisation
  38. _le = (LittleEndianInput) in;
  39. } else {
  40. // less optimal, but should work OK just the same. Often occurs in junit tests.
  41. _le = new LittleEndianInputStream(in);
  42. }
  43. }
  44. public int available() {
  45. return _le.available();
  46. }
  47. /**
  48. * Reads an unsigned short value without decrypting
  49. */
  50. public int readRecordSID() {
  51. int sid = _le.readUShort();
  52. _cipher.skipTwoBytes();
  53. _cipher.startRecord(sid);
  54. return sid;
  55. }
  56. /**
  57. * Reads an unsigned short value without decrypting
  58. */
  59. public int readDataSize() {
  60. int dataSize = _le.readUShort();
  61. _cipher.skipTwoBytes();
  62. _cipher.setNextRecordSize(dataSize);
  63. return dataSize;
  64. }
  65. public double readDouble() {
  66. long valueLongBits = readLong();
  67. double result = Double.longBitsToDouble(valueLongBits);
  68. if (Double.isNaN(result)) {
  69. throw new RuntimeException("Did not expect to read NaN"); // (Because Excel typically doesn't write NaN
  70. }
  71. return result;
  72. }
  73. public void readFully(byte[] buf) {
  74. readFully(buf, 0, buf.length);
  75. }
  76. public void readFully(byte[] buf, int off, int len) {
  77. _le.readFully(buf, off, len);
  78. _cipher.xor(buf, off, len);
  79. }
  80. public int readUByte() {
  81. return readByte() & 0xFF;
  82. }
  83. public byte readByte() {
  84. return (byte) _cipher.xorByte(_le.readUByte());
  85. }
  86. public int readUShort() {
  87. return readShort() & 0xFFFF;
  88. }
  89. public short readShort() {
  90. return (short) _cipher.xorShort(_le.readUShort());
  91. }
  92. public int readInt() {
  93. return _cipher.xorInt(_le.readInt());
  94. }
  95. public long readLong() {
  96. return _cipher.xorLong(_le.readLong());
  97. }
  98. }