| 1 |
// Copyright 2001 freehep |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.ByteArrayInputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.InputStream; |
| 7 |
|
| 8 |
/** |
| 9 |
* Decrypts using the EEXEC form (Used by Type 1 fonts). |
| 10 |
* |
| 11 |
* @author Simon Fischer |
| 12 |
* @version $Id: EEXECDecryption.java 8584 2006-08-10 23:06:37Z duns $ |
| 13 |
*/ |
| 14 |
public class EEXECDecryption extends InputStream implements EEXECConstants { |
| 15 |
|
| 16 |
private int n, c1, c2, r; |
| 17 |
|
| 18 |
private InputStream in; |
| 19 |
|
| 20 |
private boolean first = true; |
| 21 |
|
| 22 |
/** |
| 23 |
* Creates an EEXECDecryption from the given stream |
| 24 |
* |
| 25 |
* @param in stream to read from |
| 26 |
*/ |
| 27 |
public EEXECDecryption(InputStream in) { |
| 28 |
this(in, EEXEC_R, N); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates an EEXECDecryption from the given stream |
| 33 |
* |
| 34 |
* @param in stream to read from |
| 35 |
* @param r |
| 36 |
* @param n |
| 37 |
*/ |
| 38 |
public EEXECDecryption(InputStream in, int r, int n) { |
| 39 |
this.in = in; |
| 40 |
this.r = r; |
| 41 |
this.n = n; |
| 42 |
this.c1 = C1; |
| 43 |
this.c2 = C2; |
| 44 |
} |
| 45 |
|
| 46 |
private int decrypt(int cipher) { |
| 47 |
int plain = (cipher ^ (r >>> 8)) % 256; |
| 48 |
r = ((cipher + r) * c1 + c2) % 65536; |
| 49 |
return plain; |
| 50 |
} |
| 51 |
|
| 52 |
public int read() throws IOException { |
| 53 |
if (first) { |
| 54 |
byte[] bytes = new byte[n]; |
| 55 |
boolean notHex = false; |
| 56 |
for (int i = 0; i < bytes.length; i++) { |
| 57 |
int c = in.read(); |
| 58 |
bytes[i] = (byte) c; |
| 59 |
if (!Character.isDigit((char) c) && !((c >= 'a') && (c <= 'f')) |
| 60 |
&& !((c >= 'A') && (c <= 'F'))) |
| 61 |
notHex = true; |
| 62 |
} |
| 63 |
if (notHex) { |
| 64 |
for (int i = 0; i < bytes.length; i++) { |
| 65 |
decrypt(bytes[i] & 0x00ff); |
| 66 |
} |
| 67 |
} else { |
| 68 |
InputStream tempIn = new ASCIIHexInputStream( |
| 69 |
new ByteArrayInputStream(bytes), true); |
| 70 |
int asciiDecoded; |
| 71 |
int byteCount = 0; |
| 72 |
while ((asciiDecoded = tempIn.read()) >= 0) { |
| 73 |
decrypt(asciiDecoded); |
| 74 |
byteCount++; |
| 75 |
} |
| 76 |
in = new ASCIIHexInputStream(in, true); |
| 77 |
while (byteCount < n) { |
| 78 |
decrypt(in.read()); |
| 79 |
byteCount++; |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
first = false; |
| 84 |
} |
| 85 |
|
| 86 |
int b = in.read(); |
| 87 |
if (b == -1) |
| 88 |
return -1; |
| 89 |
else |
| 90 |
return decrypt(b); |
| 91 |
} |
| 92 |
|
| 93 |
public void close() throws IOException { |
| 94 |
super.close(); |
| 95 |
in.close(); |
| 96 |
} |
| 97 |
} |