src/java.base/share/classes/sun/security/util/DerIndefLenConverter.java
changeset 55714 e17ec6bc670a
parent 55707 b8a12f53226e
child 58679 9c3209ff7550
equal deleted inserted replaced
55713:6cd74f29752c 55714:e17ec6bc670a
    90     /**
    90     /**
    91      * Parse the tag and if it is an end-of-contents tag then
    91      * Parse the tag and if it is an end-of-contents tag then
    92      * add the current position to the <code>eocList</code> vector.
    92      * add the current position to the <code>eocList</code> vector.
    93      */
    93      */
    94     private void parseTag() throws IOException {
    94     private void parseTag() throws IOException {
    95         if (dataPos == dataSize)
    95         if (isEOC(data[dataPos]) && (data[dataPos + 1] == 0)) {
    96             return;
    96             int numOfEncapsulatedLenBytes = 0;
    97         try {
    97             Object elem = null;
    98             if (isEOC(data[dataPos]) && (data[dataPos + 1] == 0)) {
    98             int index;
    99                 int numOfEncapsulatedLenBytes = 0;
    99             for (index = ndefsList.size()-1; index >= 0; index--) {
   100                 Object elem = null;
   100                 // Determine the first element in the vector that does not
   101                 int index;
   101                 // have a matching EOC
   102                 for (index = ndefsList.size()-1; index >= 0; index--) {
   102                 elem = ndefsList.get(index);
   103                     // Determine the first element in the vector that does not
   103                 if (elem instanceof Integer) {
   104                     // have a matching EOC
   104                     break;
   105                     elem = ndefsList.get(index);
   105                 } else {
   106                     if (elem instanceof Integer) {
   106                     numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
   107                         break;
       
   108                     } else {
       
   109                         numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
       
   110                     }
       
   111                 }
   107                 }
   112                 if (index < 0) {
   108             }
   113                     throw new IOException("EOC does not have matching " +
   109             if (index < 0) {
   114                                           "indefinite-length tag");
   110                 throw new IOException("EOC does not have matching " +
   115                 }
   111                                       "indefinite-length tag");
   116                 int sectionLen = dataPos - ((Integer)elem).intValue() +
   112             }
   117                                  numOfEncapsulatedLenBytes;
   113             int sectionLen = dataPos - ((Integer)elem).intValue() +
   118                 byte[] sectionLenBytes = getLengthBytes(sectionLen);
   114                              numOfEncapsulatedLenBytes;
   119                 ndefsList.set(index, sectionLenBytes);
   115             byte[] sectionLenBytes = getLengthBytes(sectionLen);
   120                 unresolved--;
   116             ndefsList.set(index, sectionLenBytes);
   121 
   117             unresolved--;
   122                 // Add the number of bytes required to represent this section
   118 
   123                 // to the total number of length bytes,
   119             // Add the number of bytes required to represent this section
   124                 // and subtract the indefinite-length tag (1 byte) and
   120             // to the total number of length bytes,
   125                 // EOC bytes (2 bytes) for this section
   121             // and subtract the indefinite-length tag (1 byte) and
   126                 numOfTotalLenBytes += (sectionLenBytes.length - 3);
   122             // EOC bytes (2 bytes) for this section
   127             }
   123             numOfTotalLenBytes += (sectionLenBytes.length - 3);
   128             dataPos++;
   124         }
   129         } catch (IndexOutOfBoundsException iobe) {
   125         dataPos++;
   130             throw new IOException(iobe);
       
   131         }
       
   132     }
   126     }
   133 
   127 
   134     /**
   128     /**
   135      * Write the tag and if it is an end-of-contents tag
   129      * Write the tag and if it is an end-of-contents tag
   136      * then skip the tag and its 1 byte length of zero.
   130      * then skip the tag and its 1 byte length of zero.
   334         int len=0;
   328         int len=0;
   335         int unused = 0;
   329         int unused = 0;
   336 
   330 
   337         // parse and set up the vectors of all the indefinite-lengths
   331         // parse and set up the vectors of all the indefinite-lengths
   338         while (dataPos < dataSize) {
   332         while (dataPos < dataSize) {
       
   333             if (dataPos + 2 > dataSize) {
       
   334                 // There should be at least one tag and one length
       
   335                 return null;
       
   336             }
   339             parseTag();
   337             parseTag();
   340             len = parseLength();
   338             len = parseLength();
   341             if (len < 0) {
   339             if (len < 0) {
   342                 return null;
   340                 return null;
   343             }
   341             }