88 *******************************************************************************/
99package org .cryptomator .cryptofs ;
1010
11- import static org .cryptomator .cryptolib .Constants .CHUNK_SIZE ;
12- import static org .cryptomator .cryptolib .Constants .PAYLOAD_SIZE ;
13-
1411import java .io .IOException ;
1512import java .nio .ByteBuffer ;
1613import java .nio .MappedByteBuffer ;
2623import java .util .Set ;
2724import java .util .concurrent .ExecutionException ;
2825
29- import org .cryptomator .cryptolib .AuthenticationFailedException ;
30- import org .cryptomator .cryptolib .Cryptor ;
31- import org .cryptomator .cryptolib .FileHeader ;
26+ import org .cryptomator .cryptolib .api . AuthenticationFailedException ;
27+ import org .cryptomator .cryptolib .api . Cryptor ;
28+ import org .cryptomator .cryptolib .api . FileHeader ;
3229import org .slf4j .Logger ;
3330import org .slf4j .LoggerFactory ;
3431
@@ -63,7 +60,7 @@ public CryptoFileChannel(Cryptor cryptor, Path ciphertextPath, Set<? extends Ope
6360 if (adjustedOptions .contains (StandardOpenOption .CREATE_NEW ) || adjustedOptions .contains (StandardOpenOption .CREATE ) && ch .size () == 0 ) {
6461 header = cryptor .fileHeaderCryptor ().create ();
6562 } else {
66- ByteBuffer existingHeaderBuf = ByteBuffer .allocate (FileHeader . SIZE );
63+ ByteBuffer existingHeaderBuf = ByteBuffer .allocate (cryptor . fileHeaderCryptor (). headerSize () );
6764 ch .position (0 );
6865 ch .read (existingHeaderBuf );
6966 existingHeaderBuf .flip ();
@@ -77,11 +74,12 @@ public int read(ByteBuffer dst, long position) throws IOException {
7774 int origLimit = dst .limit ();
7875 dst .limit ((int ) Math .min (origLimit , size () - position ));
7976 int read = 0 ;
77+ int payloadSize = cryptor .fileContentCryptor ().cleartextChunkSize ();
8078 while (dst .hasRemaining ()) {
8179 long pos = position + read ;
82- long chunkIndex = pos / PAYLOAD_SIZE ;
83- int offset = (int ) pos % PAYLOAD_SIZE ;
84- int len = Math .min (dst .remaining (), PAYLOAD_SIZE - offset );
80+ long chunkIndex = pos / payloadSize ;
81+ int offset = (int ) pos % payloadSize ;
82+ int len = Math .min (dst .remaining (), payloadSize - offset );
8583 final ByteBuffer chunkBuf = loadCleartextChunk (chunkIndex );
8684 chunkBuf .position (offset ).limit (Math .min (chunkBuf .limit (), len ));
8785 dst .put (chunkBuf );
@@ -94,18 +92,19 @@ public int read(ByteBuffer dst, long position) throws IOException {
9492 @ Override
9593 public int write (ByteBuffer src , long position ) throws IOException {
9694 int written = 0 ;
95+ int payloadSize = cryptor .fileContentCryptor ().cleartextChunkSize ();
9796 while (src .hasRemaining ()) {
9897 long pos = position + written ;
99- long chunkIndex = pos / PAYLOAD_SIZE ;
100- int offset = (int ) pos % PAYLOAD_SIZE ;
101- int len = Math .min (src .remaining (), PAYLOAD_SIZE - offset );
98+ long chunkIndex = pos / payloadSize ;
99+ int offset = (int ) pos % payloadSize ;
100+ int len = Math .min (src .remaining (), payloadSize - offset );
102101 if (pos + len > size ()) {
103102 // append
104103 setSize (pos + len );
105104 }
106- if (len == PAYLOAD_SIZE ) {
105+ if (len == payloadSize ) {
107106 // complete chunk, no need to load and decrypt from file:
108- cleartextChunks .put (chunkIndex , ByteBuffer .allocate (PAYLOAD_SIZE ));
107+ cleartextChunks .put (chunkIndex , ByteBuffer .allocate (payloadSize ));
109108 }
110109 final ByteBuffer chunkBuf = loadCleartextChunk (chunkIndex );
111110 chunkBuf .position (offset ).limit (Math .max (chunkBuf .limit (), len ));
@@ -120,16 +119,16 @@ public int write(ByteBuffer src, long position) throws IOException {
120119
121120 @ Override
122121 public long size () {
123- return header .getPayload (). getFilesize ();
122+ return header .getFilesize ();
124123 }
125124
126125 private void setSize (long size ) {
127- header .getPayload (). setFilesize (size );
126+ header .setFilesize (size );
128127 }
129128
130129 @ Override
131130 public FileChannel truncate (long size ) throws IOException {
132- ch .truncate (FileHeader . SIZE );
131+ ch .truncate (cryptor . fileHeaderCryptor (). headerSize () );
133132 setSize (Math .min (size , size ()));
134133 // TODO Auto-generated method stub
135134 return null ;
@@ -195,12 +194,14 @@ private class CleartextChunkLoader extends CacheLoader<Long, ByteBuffer> {
195194 @ Override
196195 public ByteBuffer load (Long chunkIndex ) throws Exception {
197196 LOG .debug ("load chunk" + chunkIndex );
198- long ciphertextPos = chunkIndex * CHUNK_SIZE + FileHeader .SIZE ;
199- ByteBuffer ciphertextBuf = ByteBuffer .allocate (CHUNK_SIZE );
197+ int payloadSize = cryptor .fileContentCryptor ().cleartextChunkSize ();
198+ int chunkSize = cryptor .fileContentCryptor ().ciphertextChunkSize ();
199+ long ciphertextPos = chunkIndex * chunkSize + cryptor .fileHeaderCryptor ().headerSize ();
200+ ByteBuffer ciphertextBuf = ByteBuffer .allocate (chunkSize );
200201 int read = ch .read (ciphertextBuf , ciphertextPos );
201202 if (read == -1 ) {
202203 // append
203- return ByteBuffer .allocate (PAYLOAD_SIZE );
204+ return ByteBuffer .allocate (payloadSize );
204205 } else {
205206 ciphertextBuf .flip ();
206207 return cryptor .fileContentCryptor ().decryptChunk (ciphertextBuf , chunkIndex , header , true );
@@ -214,9 +215,9 @@ private class CleartextChunkSaver implements RemovalListener<Long, ByteBuffer> {
214215 @ Override
215216 public void onRemoval (RemovalNotification <Long , ByteBuffer > notification ) {
216217 long chunkIndex = notification .getKey ();
217- if (openOptions .contains (StandardOpenOption .WRITE ) && chunkIndex * PAYLOAD_SIZE < size ()) {
218+ if (openOptions .contains (StandardOpenOption .WRITE ) && chunkIndex * cryptor . fileContentCryptor (). cleartextChunkSize () < size ()) {
218219 LOG .debug ("save chunk" + chunkIndex );
219- long ciphertextPos = chunkIndex * CHUNK_SIZE + FileHeader . SIZE ;
220+ long ciphertextPos = chunkIndex * cryptor . fileContentCryptor (). ciphertextChunkSize () + cryptor . fileHeaderCryptor (). headerSize () ;
220221 ByteBuffer cleartextBuf = notification .getValue ().asReadOnlyBuffer ();
221222 cleartextBuf .flip ();
222223 ByteBuffer ciphertextBuf = cryptor .fileContentCryptor ().encryptChunk (cleartextBuf , chunkIndex , header );
0 commit comments