11
11
12
12
import static com .tonikelope .megabasterd .CryptTools .*;
13
13
import static com .tonikelope .megabasterd .MiscTools .*;
14
- import java .io .BufferedInputStream ;
15
- import java .io .FileInputStream ;
16
- import java .io .IOException ;
14
+ import java .io .ByteArrayInputStream ;
15
+ import java .io .ByteArrayOutputStream ;
17
16
import java .util .HashMap ;
17
+ import java .util .concurrent .ConcurrentHashMap ;
18
18
import java .util .logging .Level ;
19
19
import java .util .logging .Logger ;
20
20
import javax .crypto .BadPaddingException ;
@@ -33,6 +33,7 @@ public class UploadMACGenerator implements Runnable, SecureSingleThreadNotifiabl
33
33
private final Object _secure_notify_lock ;
34
34
private boolean _notified ;
35
35
private volatile boolean _exit ;
36
+ public final ConcurrentHashMap <Long , ByteArrayOutputStream > CHUNK_QUEUE = new ConcurrentHashMap <>();
36
37
37
38
public UploadMACGenerator (Upload upload ) {
38
39
_secure_notify_lock = new Object ();
@@ -122,36 +123,40 @@ public void run() {
122
123
123
124
Cipher cryptor = genCrypter ("AES" , "AES/CBC/NoPadding" , _upload .getByte_file_key (), i32a2bin (mac_iv ));
124
125
125
- try ( BufferedInputStream is = new BufferedInputStream (new FileInputStream (_upload .getFile_name ()))) {
126
+ int [] chunk_mac = new int [4 ];
127
+ byte [] byte_block = new byte [16 ];
128
+ byte [] chunk_bytes ;
126
129
127
- if (tot > 0 ) {
128
- is .skip (tot );
129
- }
130
+ try {
131
+ while (!_exit && !_upload .isStopped () && !_upload .getMain_panel ().isExit ()) {
130
132
131
- int [] chunk_mac = new int [4 ];
132
- byte [] byte_block = new byte [16 ];
133
+ int reads ;
133
134
134
- try {
135
- while (!_exit && !_upload .isStopped () && !_upload .getMain_panel ().isExit ()) {
135
+ long chunk_offset = ChunkWriterManager .calculateChunkOffset (chunk_id , 1 );
136
136
137
- int reads ;
137
+ long chunk_size = ChunkWriterManager . calculateChunkSize ( chunk_id , _upload . getFile_size (), chunk_offset , 1 ) ;
138
138
139
- long chunk_offset = ChunkWriterManager .calculateChunkOffset (chunk_id , 1 );
139
+ ChunkWriterManager .checkChunkID (chunk_id , _upload . getFile_size (), chunk_offset );
140
140
141
- long chunk_size = ChunkWriterManager .calculateChunkSize (chunk_id , _upload .getFile_size (), chunk_offset , 1 );
141
+ while (!CHUNK_QUEUE .containsKey (chunk_offset )) {
142
+ MiscTools .pausar (1000 );
143
+ }
142
144
143
- ChunkWriterManager . checkChunkID ( chunk_id , _upload . getFile_size (), chunk_offset );
145
+ try {
144
146
145
- try {
147
+ chunk_mac [0 ] = file_iv [0 ];
148
+ chunk_mac [1 ] = file_iv [1 ];
149
+ chunk_mac [2 ] = file_iv [0 ];
150
+ chunk_mac [3 ] = file_iv [1 ];
146
151
147
- chunk_mac [0 ] = file_iv [0 ];
148
- chunk_mac [1 ] = file_iv [1 ];
149
- chunk_mac [2 ] = file_iv [0 ];
150
- chunk_mac [3 ] = file_iv [1 ];
152
+ long conta_chunk = 0L ;
151
153
152
- long conta_chunk = 0L ;
154
+ try ( ByteArrayOutputStream baos = CHUNK_QUEUE .remove (chunk_offset )) {
155
+ chunk_bytes = baos .toByteArray ();
156
+ }
153
157
154
- while (conta_chunk < chunk_size && (reads = is .read (byte_block )) != -1 ) {
158
+ try ( ByteArrayInputStream bais = new ByteArrayInputStream (chunk_bytes )) {
159
+ while (conta_chunk < chunk_size && (reads = bais .read (byte_block )) != -1 ) {
155
160
156
161
if (reads < byte_block .length ) {
157
162
for (int i = reads ; i < byte_block .length ; i ++) {
@@ -172,45 +177,46 @@ public void run() {
172
177
tot += reads ;
173
178
174
179
}
180
+ }
175
181
176
- for (int i = 0 ; i < file_mac .length ; i ++) {
177
- file_mac [i ] ^= chunk_mac [i ];
178
- }
182
+ for (int i = 0 ; i < file_mac .length ; i ++) {
183
+ file_mac [i ] ^= chunk_mac [i ];
184
+ }
179
185
180
- file_mac = bin2i32a (cryptor .doFinal (i32a2bin (file_mac )));
186
+ file_mac = bin2i32a (cryptor .doFinal (i32a2bin (file_mac )));
181
187
182
- } catch (IOException | IllegalBlockSizeException | BadPaddingException ex ) {
183
- LOG .log (Level .SEVERE , ex .getMessage ());
184
- }
188
+ } catch (IllegalBlockSizeException | BadPaddingException ex ) {
189
+ LOG .log (Level .SEVERE , ex .getMessage ());
190
+ }
185
191
186
- chunk_id ++;
192
+ chunk_id ++;
187
193
188
- int new_cbc_per = (int ) ((((double ) tot ) / _upload .getFile_size ()) * 100 );
194
+ int new_cbc_per = (int ) ((((double ) tot ) / _upload .getFile_size ()) * 100 );
189
195
190
- if (new_cbc_per != cbc_per ) {
191
- _upload .getView ().updateCBC ("CBC-MAC " + String .valueOf (new_cbc_per ) + "%" );
192
- cbc_per = new_cbc_per ;
193
- }
196
+ if (new_cbc_per != cbc_per ) {
197
+ _upload .getView ().updateCBC ("CBC-MAC " + String .valueOf (new_cbc_per ) + "%" );
198
+ cbc_per = new_cbc_per ;
194
199
}
195
200
196
- mac = ( tot == _upload . getFile_size ());
201
+ }
197
202
198
- } catch ( ChunkInvalidException e ) {
203
+ mac = ( tot == _upload . getFile_size ());
199
204
200
- mac = true ;
201
- }
205
+ } catch (ChunkInvalidException e ) {
202
206
203
- _upload .setTemp_mac_data (String .valueOf (tot ) + "#" + String .valueOf (chunk_id ) + "#" + Bin2BASE64 (i32a2bin (file_mac )));
207
+ mac = true ;
208
+ }
204
209
205
- if ( mac ) {
210
+ _upload . setTemp_mac_data ( String . valueOf ( tot ) + "#" + String . valueOf ( chunk_id ) + "#" + Bin2BASE64 ( i32a2bin ( file_mac )));
206
211
207
- int [] meta_mac = { file_mac [ 0 ] ^ file_mac [ 1 ], file_mac [ 2 ] ^ file_mac [ 3 ]};
212
+ if ( mac ) {
208
213
209
- _upload . setFile_meta_mac ( meta_mac ) ;
214
+ int [] meta_mac = { file_mac [ 0 ] ^ file_mac [ 1 ], file_mac [ 2 ] ^ file_mac [ 3 ]} ;
210
215
211
- LOG .log (Level .INFO , "{0} MAC GENERATOR {1} finished MAC CALCULATION. Waiting workers to finish uploading (if any)..." , new Object []{Thread .currentThread ().getName (), getUpload ().getFile_name ()});
216
+ _upload .setFile_meta_mac (meta_mac );
217
+
218
+ LOG .log (Level .INFO , "{0} MAC GENERATOR {1} finished MAC CALCULATION. Waiting workers to finish uploading (if any)..." , new Object []{Thread .currentThread ().getName (), getUpload ().getFile_name ()});
212
219
213
- }
214
220
}
215
221
216
222
while (!_exit && !_upload .isStopped () && !_upload .getChunkworkers ().isEmpty ()) {
0 commit comments