comparison src/ch/ethz/ssh2/crypto/cipher/CipherInputStream.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
11 * CipherInputStream. 11 * CipherInputStream.
12 * 12 *
13 * @author Christian Plattner 13 * @author Christian Plattner
14 * @version $Id: CipherInputStream.java 11 2011-05-27 14:14:06Z dkocher@sudo.ch $ 14 * @version $Id: CipherInputStream.java 11 2011-05-27 14:14:06Z dkocher@sudo.ch $
15 */ 15 */
16 public class CipherInputStream 16 public class CipherInputStream {
17 { 17 BlockCipher currentCipher;
18 BlockCipher currentCipher; 18 InputStream bi;
19 InputStream bi; 19 byte[] buffer;
20 byte[] buffer; 20 byte[] enc;
21 byte[] enc; 21 int blockSize;
22 int blockSize; 22 int pos;
23 int pos;
24 23
25 /* 24 /*
26 * We cannot use java.io.BufferedInputStream, since that is not available in 25 * We cannot use java.io.BufferedInputStream, since that is not available in
27 * J2ME. Everything could be improved alot here. 26 * J2ME. Everything could be improved alot here.
28 */ 27 */
29 28
30 private static final int BUFF_SIZE = 8192; 29 private static final int BUFF_SIZE = 8192;
31 byte[] input_buffer = new byte[BUFF_SIZE]; 30 byte[] input_buffer = new byte[BUFF_SIZE];
32 int input_buffer_pos = 0; 31 int input_buffer_pos = 0;
33 int input_buffer_size = 0; 32 int input_buffer_size = 0;
34 33
35 public CipherInputStream(BlockCipher tc, InputStream bi) 34 public CipherInputStream(BlockCipher tc, InputStream bi) {
36 { 35 this.bi = bi;
37 this.bi = bi; 36 changeCipher(tc);
38 changeCipher(tc); 37 }
39 }
40 38
41 private int fill_buffer() throws IOException 39 private int fill_buffer() throws IOException {
42 { 40 input_buffer_pos = 0;
43 input_buffer_pos = 0; 41 input_buffer_size = 0;
44 input_buffer_size = 0; 42 input_buffer_size = bi.read(input_buffer, 0, BUFF_SIZE);
45 input_buffer_size = bi.read(input_buffer, 0, BUFF_SIZE); 43 return input_buffer_size;
46 return input_buffer_size; 44 }
47 }
48 45
49 private int internal_read(byte[] b, int off, int len) throws IOException 46 private int internal_read(byte[] b, int off, int len) throws IOException {
50 { 47 if (input_buffer_size < 0) {
51 if (input_buffer_size < 0) 48 return -1;
52 { 49 }
53 return -1;
54 }
55 50
56 if (input_buffer_pos >= input_buffer_size) 51 if (input_buffer_pos >= input_buffer_size) {
57 { 52 if (fill_buffer() <= 0) {
58 if (fill_buffer() <= 0) 53 return -1;
59 { 54 }
60 return -1; 55 }
61 }
62 }
63 56
64 int avail = input_buffer_size - input_buffer_pos; 57 int avail = input_buffer_size - input_buffer_pos;
65 int thiscopy = (len > avail) ? avail : len; 58 int thiscopy = (len > avail) ? avail : len;
59 System.arraycopy(input_buffer, input_buffer_pos, b, off, thiscopy);
60 input_buffer_pos += thiscopy;
61 return thiscopy;
62 }
66 63
67 System.arraycopy(input_buffer, input_buffer_pos, b, off, thiscopy); 64 public void changeCipher(BlockCipher bc) {
68 input_buffer_pos += thiscopy; 65 this.currentCipher = bc;
66 blockSize = bc.getBlockSize();
67 buffer = new byte[blockSize];
68 enc = new byte[blockSize];
69 pos = blockSize;
70 }
69 71
70 return thiscopy; 72 private void getBlock() throws IOException {
71 } 73 int n = 0;
72 74
73 public void changeCipher(BlockCipher bc) 75 while (n < blockSize) {
74 { 76 int len = internal_read(enc, n, blockSize - n);
75 this.currentCipher = bc;
76 blockSize = bc.getBlockSize();
77 buffer = new byte[blockSize];
78 enc = new byte[blockSize];
79 pos = blockSize;
80 }
81 77
82 private void getBlock() throws IOException 78 if (len < 0) {
83 { 79 throw new IOException("Cannot read full block, EOF reached.");
84 int n = 0; 80 }
85 while (n < blockSize)
86 {
87 int len = internal_read(enc, n, blockSize - n);
88 if (len < 0)
89 {
90 throw new IOException("Cannot read full block, EOF reached.");
91 }
92 n += len;
93 }
94 81
95 try 82 n += len;
96 { 83 }
97 currentCipher.transformBlock(enc, 0, buffer, 0);
98 }
99 catch (Exception e)
100 {
101 throw new IOException("Error while decrypting block.");
102 }
103 pos = 0;
104 }
105 84
106 public int read(byte[] dst) throws IOException 85 try {
107 { 86 currentCipher.transformBlock(enc, 0, buffer, 0);
108 return read(dst, 0, dst.length); 87 }
109 } 88 catch (Exception e) {
89 throw new IOException("Error while decrypting block.");
90 }
110 91
111 public int read(byte[] dst, int off, int len) throws IOException 92 pos = 0;
112 { 93 }
113 int count = 0;
114 94
115 while (len > 0) 95 public int read(byte[] dst) throws IOException {
116 { 96 return read(dst, 0, dst.length);
117 if (pos >= blockSize) 97 }
118 {
119 getBlock();
120 }
121 98
122 int avail = blockSize - pos; 99 public int read(byte[] dst, int off, int len) throws IOException {
123 int copy = Math.min(avail, len); 100 int count = 0;
124 System.arraycopy(buffer, pos, dst, off, copy);
125 pos += copy;
126 off += copy;
127 len -= copy;
128 count += copy;
129 }
130 return count;
131 }
132 101
133 public int read() throws IOException 102 while (len > 0) {
134 { 103 if (pos >= blockSize) {
135 if (pos >= blockSize) 104 getBlock();
136 { 105 }
137 getBlock();
138 }
139 return buffer[pos++] & 0xff;
140 }
141 106
142 public int readPlain(byte[] b, int off, int len) throws IOException 107 int avail = blockSize - pos;
143 { 108 int copy = Math.min(avail, len);
144 if (pos != blockSize) 109 System.arraycopy(buffer, pos, dst, off, copy);
145 { 110 pos += copy;
146 throw new IOException("Cannot read plain since crypto buffer is not aligned."); 111 off += copy;
147 } 112 len -= copy;
148 int n = 0; 113 count += copy;
149 while (n < len) 114 }
150 { 115
151 int cnt = internal_read(b, off + n, len - n); 116 return count;
152 if (cnt < 0) 117 }
153 { 118
154 throw new IOException("Cannot fill buffer, EOF reached."); 119 public int read() throws IOException {
155 } 120 if (pos >= blockSize) {
156 n += cnt; 121 getBlock();
157 } 122 }
158 return n; 123
159 } 124 return buffer[pos++] & 0xff;
125 }
126
127 public int readPlain(byte[] b, int off, int len) throws IOException {
128 if (pos != blockSize) {
129 throw new IOException("Cannot read plain since crypto buffer is not aligned.");
130 }
131
132 int n = 0;
133
134 while (n < len) {
135 int cnt = internal_read(b, off + n, len - n);
136
137 if (cnt < 0) {
138 throw new IOException("Cannot fill buffer, EOF reached.");
139 }
140
141 n += cnt;
142 }
143
144 return n;
145 }
160 } 146 }