/*
* 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 BER encoder.
*
* @author Jagane Sundar
* @author Scott Seligman
* @author Vincent Ryan
*/
private int curSeqIndex;
private int seqOffset[];
// When buf is full, expand its size by the following factor.
/**
* Creates a BER buffer for encoding.
*/
public BerEncoder() {
this(DEFAULT_BUFSIZE);
}
/**
* Creates a BER buffer of a specified size for encoding.
* Specify the initial bufsize. Buffer will be expanded as needed.
* @param bufsize The number of bytes for the buffer.
*/
offset = 0;
seqOffset = new int[INITIAL_SEQUENCES];
curSeqIndex = 0;
}
/**
* Resets encoder to state when newly constructed. Zeros out
* internal data structures.
*/
public void reset() {
while (offset > 0) {
}
while (curSeqIndex > 0) {
}
}
// ------------------ Accessor methods ------------
/**
* Gets the number of encoded bytes in this BER buffer.
*/
public int getDataLen() {
return offset;
}
/**
* Gets the buffer that contains the BER encoding. Throws an
* exception if unmatched beginSeq() and endSeq() pairs were
* encountered. Not entire buffer contains encoded bytes.
* Use getDataLen() to determine number of encoded bytes.
* Use getBuffer(true) to get rid of excess bytes in array.
* @throws IllegalStateException If buffer contains unbalanced sequence.
*/
public byte[] getBuf() {
if (curSeqIndex != 0) {
throw new IllegalStateException("BER encode error: Unbalanced SEQUENCEs.");
}
return buf;
}
/**
* Gets the buffer that contains the BER encoding, trimming unused bytes.
*
* @throws IllegalStateException If buffer contains unbalanced sequence.
*/
public byte[] getTrimmedBuf() {
int len = getDataLen();
return trimBuf;
}
// -------------- encoding methods -------------
/**
* Begin encoding a sequence with a tag.
*/
// Double the size of the SEQUENCE array if it overflows
seqOffsetTmp[i] = seqOffset[i];
}
}
// Save space for sequence length.
// %%% Currently we save enough space for sequences up to 64k.
// For larger sequences we'll need to shift the data to the right
// in endSeq(). If we could instead pad the length field with
// zeros, it would be a big win.
ensureFreeBytes(3);
offset += 3;
curSeqIndex++;
}
/**
* Terminate a BER sequence.
*/
curSeqIndex--;
if (curSeqIndex < 0) {
throw new IllegalStateException("BER encode error: Unbalanced SEQUENCEs.");
}
if (len <= 0x7f) {
} else if (len <= 0xff) {
} else if (len <= 0xffff) {
} else if (len <= 0xffffff) {
} else {
throw new EncodeException("SEQUENCE too long");
}
}
/**
* Shifts contents of buf in the range [start,start+len) a specified amount.
* Positive shift value means shift to the right.
*/
if (shift > 0) {
}
}
/**
* Encode a single byte.
*/
public void encodeByte(int b) {
ensureFreeBytes(1);
}
/*
private void deleteByte() {
offset--;
}
*/
/*
* Encodes an int.
*<blockquote><pre>
* BER integer ::= 0x02 berlength byte {byte}*
*</pre></blockquote>
*/
public void encodeInt(int i) {
encodeInt(i, 0x02);
}
/**
* Encodes an int and a tag.
*<blockquote><pre>
* BER integer w tag ::= tag berlength byte {byte}*
*</pre></blockquote>
*/
int mask = 0xff800000;
int intsize = 4;
intsize--;
i <<= 8;
}
}
//
// encodes an int using numbytes for the actual encoding.
//
//
// integer ::= 0x02 asnlength byte {byte}*
//
if (intsize > 4) {
throw new IllegalArgumentException("BER encode error: INTEGER too long.");
}
int mask = 0xff000000;
while (intsize-- > 0) {
i <<= 8;
}
}
/**
* Encodes a boolean.
*<blockquote><pre>
* BER boolean ::= 0x01 0x01 {0xff|0x00}
*</pre></blockquote>
*/
public void encodeBoolean(boolean b) {
encodeBoolean(b, ASN_BOOLEAN);
}
/**
* Encodes a boolean and a tag
*<blockquote><pre>
* BER boolean w TAG ::= tag 0x01 {0xff|0x00}
*</pre></blockquote>
*/
ensureFreeBytes(3);
}
/**
* Encodes a string.
*<blockquote><pre>
* BER string ::= 0x04 strlen byte1 byte2...
*</pre></blockquote>
* The string is converted into bytes using UTF-8 or ISO-Latin-1.
*/
throws EncodeException {
}
/**
* Encodes a string and a tag.
*<blockquote><pre>
* BER string w TAG ::= tag strlen byte1 byte2...
*</pre></blockquote>
*/
throws EncodeException {
int i = 0;
int count;
count = 0;
} else if (encodeUTF8) {
try {
} catch (UnsupportedEncodingException e) {
throw new EncodeException("UTF8 not available on platform");
}
} else {
try {
} catch (UnsupportedEncodingException e) {
throw new EncodeException("8859_1 not available on platform");
}
}
while (i < count) {
}
}
/**
* Encodes a portion of an octet string and a tag.
*/
throws EncodeException {
if (length > 0) {
}
}
/**
* Encodes an octet string and a tag.
*/
}
if (len < 128) {
} else if (len <= 0xff) {
} else if (len <= 0xffff) {
} else if (len <= 0xffffff) {
} else {
throw new EncodeException("string too long");
}
}
/**
* Encodes an array of strings.
*/
throws EncodeException {
return;
}
}
/*
private void encodeNull() {
//
// NULL ::= 0x05 0x00
//
encodeByte(0x05);
encodeByte(0x00);
}
*/
/**
* Ensures that there are at least "len" unused bytes in "buf".
* When more space is needed "buf" is expanded by a factor of
* BUF_GROWTH_FACTOR, then "len" bytes are added if "buf" still
* isn't large enough.
*/
}
// Only copy bytes in the range [0, offset)
}
}
}