src/share/classes/com/sun/crypto/provider/CipherCore.java

Print this page
8027575: b113 causing a lot of memory allocation and regression for wls_webapp_atomics


 703 
 704         // figure out how much can be sent to crypto function
 705         int len = buffered + inputLen - minBytes;
 706         if (padding != null && decrypting) {
 707             // do not include the padding bytes when decrypting
 708             len -= blockSize;
 709         }
 710         // do not count the trailing bytes which do not make up a unit
 711         len = (len > 0 ? (len - (len%unitBytes)) : 0);
 712 
 713         // check output buffer capacity
 714         if ((output == null) ||
 715             ((output.length - outputOffset) < len)) {
 716             throw new ShortBufferException("Output buffer must be "
 717                                            + "(at least) " + len
 718                                            + " bytes long");
 719         }
 720 
 721         int outLen = 0;
 722         if (len != 0) { // there is some work to do









 723             if (len <= buffered) {
 724                 // all to-be-processed data are from 'buffer'
 725                 if (decrypting) {
 726                     outLen = cipher.decrypt(buffer, 0, len, output, outputOffset);
 727                 } else {
 728                     outLen = cipher.encrypt(buffer, 0, len, output, outputOffset);
 729                 }
 730                 buffered -= len;
 731                 if (buffered != 0) {
 732                     System.arraycopy(buffer, len, buffer, 0, buffered);
 733                 }
 734             } else { // len > buffered
 735                 if ((input != output) && (buffered == 0)) {
 736                     // all to-be-processed data are from 'input'
 737                     // however, note that if 'input' and 'output' are the same,
 738                     // then they can't be passed directly to the underlying cipher
 739                     // engine operations as data may be overwritten before they
 740                     // are read.







 741                     if (decrypting) {
 742                         outLen = cipher.decrypt(input, inputOffset, len, output, outputOffset);
 743                     } else {
 744                         outLen = cipher.encrypt(input, inputOffset, len, output, outputOffset);
 745                     }
 746                     inputOffset += len;
 747                     inputLen -= len;
 748                 } else {
 749                     // assemble the data using both 'buffer' and 'input'
 750                     byte[] in = new byte[len];
 751                     int inConsumed = len - buffered;
 752                     if (buffered != 0) {
 753                         System.arraycopy(buffer, 0, in, 0, buffered);
 754                         buffered = 0;
 755                     }
 756                     if (inConsumed != 0) {
 757                         System.arraycopy(input, inputOffset, in, len - inConsumed, inConsumed);
 758                         inputOffset += inConsumed;
 759                         inputLen -= inConsumed;
 760                     }
 761                     if (decrypting) {
 762                         outLen = cipher.decrypt(in, 0, len, output, outputOffset);

 763                     } else {
 764                         outLen = cipher.encrypt(in, 0, len, output, outputOffset);

 765                     }


 766                 }
 767             }
 768             // Let's keep track of how many bytes are needed to make
 769             // the total input length a multiple of blocksize when
 770             // padding is applied
 771             if (unitBytes != blockSize) {
 772                 if (len < diffBlocksize) {
 773                     diffBlocksize -= len;
 774                 } else {
 775                     diffBlocksize = blockSize -
 776                         ((len - diffBlocksize) % blockSize);
 777                 }
 778             }
 779         }
 780         // Store remaining input into 'buffer' again
 781         if (inputLen > 0) {
 782             System.arraycopy(input, inputOffset, buffer, buffered,
 783                              inputLen);
 784             buffered += inputLen;
 785         }




 703 
 704         // figure out how much can be sent to crypto function
 705         int len = buffered + inputLen - minBytes;
 706         if (padding != null && decrypting) {
 707             // do not include the padding bytes when decrypting
 708             len -= blockSize;
 709         }
 710         // do not count the trailing bytes which do not make up a unit
 711         len = (len > 0 ? (len - (len%unitBytes)) : 0);
 712 
 713         // check output buffer capacity
 714         if ((output == null) ||
 715             ((output.length - outputOffset) < len)) {
 716             throw new ShortBufferException("Output buffer must be "
 717                                            + "(at least) " + len
 718                                            + " bytes long");
 719         }
 720 
 721         int outLen = 0;
 722         if (len != 0) { // there is some work to do
 723             if ((input == output)
 724                  && (outputOffset <= (inputOffset + buffer.length))
 725                  && (inputOffset <= (outputOffset + buffer.length))) {
 726                 // copy 'input' out to avoid its content being 
 727                 // overwritten prematurely.
 728                 input = Arrays.copyOfRange(input, inputOffset,
 729                     inputOffset + inputLen);
 730                 inputOffset = 0;
 731             }
 732             if (len <= buffered) {
 733                 // all to-be-processed data are from 'buffer'
 734                 if (decrypting) {
 735                     outLen = cipher.decrypt(buffer, 0, len, output, outputOffset);
 736                 } else {
 737                     outLen = cipher.encrypt(buffer, 0, len, output, outputOffset);
 738                 }
 739                 buffered -= len;
 740                 if (buffered != 0) {
 741                     System.arraycopy(buffer, len, buffer, 0, buffered);
 742                 }
 743             } else { // len > buffered
 744                 int inputConsumed = len - buffered;
 745                 int temp;
 746                 if (buffered > 0) {
 747                     int bufferCapacity = buffer.length - buffered;
 748                     if (bufferCapacity != 0) {
 749                         temp = Math.min(bufferCapacity, inputConsumed);
 750                         System.arraycopy(input, inputOffset, buffer, buffered, temp);
 751                         inputOffset += temp;
 752                         inputConsumed -= temp;
 753                         inputLen -= temp;
 754                         buffered += temp;
 755                     }
 756                     // process 'buffer'
 757                     if (decrypting) {
 758                          outLen = cipher.decrypt(buffer, 0, buffered, output, outputOffset);
 759                     } else {
 760                          outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
 761                     }
 762                     outputOffset += outLen;







 763                     buffered = 0;
 764                 }
 765                 if (inputConsumed > 0) { // still has input to process




 766                     if (decrypting) {
 767                         outLen += cipher.decrypt(input, inputOffset, inputConsumed,
 768                             output, outputOffset);
 769                     } else {
 770                         outLen += cipher.encrypt(input, inputOffset, inputConsumed,
 771                             output, outputOffset);
 772                     }
 773                     inputOffset += inputConsumed;
 774                     inputLen -= inputConsumed;
 775                 }
 776             }
 777             // Let's keep track of how many bytes are needed to make
 778             // the total input length a multiple of blocksize when
 779             // padding is applied
 780             if (unitBytes != blockSize) {
 781                 if (len < diffBlocksize) {
 782                     diffBlocksize -= len;
 783                 } else {
 784                     diffBlocksize = blockSize -
 785                         ((len - diffBlocksize) % blockSize);
 786                 }
 787             }
 788         }
 789         // Store remaining input into 'buffer' again
 790         if (inputLen > 0) {
 791             System.arraycopy(input, inputOffset, buffer, buffered,
 792                              inputLen);
 793             buffered += inputLen;
 794         }