/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A package private utility class to convert indefinite length DER
* encoded byte arrays to definite length DER encoded byte arrays.
*
* This assumes that the basic data structure is "tag, length, value"
* triplet. In the case where the length is "indefinite", terminating
* end-of-contents bytes are expected.
*
* @author Hemma Prafullchandra
*/
class DerIndefLenConverter {
}
// if bit 8 is set then it implies either indefinite length or long form
}
/*
* Default package private constructor
*/
DerIndefLenConverter() { }
/**
* Checks whether the given length byte is of the form
* <em>Indefinite</em>.
*
* @param lengthByte the length byte from a DER encoded
* object.
* @return true if the byte is of Indefinite form otherwise
* returns false.
*/
}
/**
* Parse the tag and if it is an end-of-contents tag then
* add the current position to the <code>eocList</code> vector.
*/
return;
int numOfEncapsulatedLenBytes = 0;
int index;
// Determine the first element in the vector that does not
// have a matching EOC
break;
} else {
}
}
if (index < 0) {
throw new IOException("EOC does not have matching " +
"indefinite-length tag");
}
unresolved--;
// Add the number of bytes required to represent this section
// to the total number of length bytes,
// and subtract the indefinite-length tag (1 byte) and
// EOC bytes (2 bytes) for this section
}
dataPos++;
}
/**
* Write the tag and if it is an end-of-contents tag
* then skip the tag and its 1 byte length of zero.
*/
private void writeTag() {
return;
dataPos++; // skip length
writeTag();
} else
}
/**
* Parse the length and if it is an indefinite length then add
* the current position to the <code>ndefsList</code> vector.
*/
int curLen = 0;
return curLen;
if (isIndefinite(lenByte)) {
unresolved++;
return curLen;
}
if (isLongForm(lenByte)) {
if (lenByte > 4)
throw new IOException("Too much data");
throw new IOException("Too little data");
for (int i = 0; i < lenByte; i++)
} else {
}
return curLen;
}
/**
* Write the length and if it is an indefinite length
* then calculate the definite length from the positions
* of the indefinite length and its matching EOC terminator.
* Then, write the value.
*/
return;
int curLen = 0;
if (isIndefinite(lenByte)) {
return;
}
if (isLongForm(lenByte)) {
for (int i = 0; i < lenByte; i++)
} else
}
if (curLen < 128) {
} else {
}
}
byte[] lenBytes;
int index = 0;
if (curLen < 128) {
lenBytes = new byte[1];
lenBytes = new byte[2];
lenBytes = new byte[3];
lenBytes = new byte[4];
} else {
lenBytes = new byte[5];
}
return lenBytes;
}
// Returns the number of bytes needed to represent the given length
// in ASN.1 notation
int numOfLenBytes = 0;
if (len < 128) {
numOfLenBytes = 1;
numOfLenBytes = 2;
numOfLenBytes = 3;
numOfLenBytes = 4;
} else {
numOfLenBytes = 5;
}
return numOfLenBytes;
}
/**
* Parse the value;
*/
}
/**
* Write the value;
*/
for (int i=0; i < curLen; i++)
}
/**
* Converts a indefinite length DER encoded byte array to
* a definte length DER encoding.
*
* @param indefData the byte array holding the indefinite
* length encoding.
* @return the byte array containing the definite length
* DER encoding.
* @exception IOException on parsing or re-writing errors.
*/
int len=0;
int unused = 0;
// parse and set up the vectors of all the indefinite-lengths
parseTag();
len = parseLength();
if (unresolved == 0) {
break;
}
}
if (unresolved != 0) {
throw new IOException("not all indef len BER resolved");
}
// write out the new byte array replacing all the indefinite-lengths
// and EOCs
writeTag();
}
return newData;
}
}