001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020:
021: /**
022: * DER UTF8String object.
023: */
024: public class DERUTF8String extends DERObject implements DERString {
025: String string;
026:
027: /**
028: * return an UTF8 string from the passed in object.
029: *
030: * @exception IllegalArgumentException if the object cannot be converted.
031: */
032: public static DERUTF8String getInstance(Object obj) {
033: if (obj == null || obj instanceof DERUTF8String) {
034: return (DERUTF8String) obj;
035: }
036:
037: if (obj instanceof ASN1OctetString) {
038: return new DERUTF8String(((ASN1OctetString) obj)
039: .getOctets());
040: }
041:
042: if (obj instanceof ASN1TaggedObject) {
043: return getInstance(((ASN1TaggedObject) obj).getObject());
044: }
045:
046: throw new IllegalArgumentException(
047: "illegal object in getInstance: "
048: + obj.getClass().getName());
049: }
050:
051: /**
052: * return an UTF8 String from a tagged object.
053: *
054: * @param obj the tagged object holding the object we want
055: * @param explicit true if the object is meant to be explicitly
056: * tagged false otherwise.
057: * @exception IllegalArgumentException if the tagged object cannot
058: * be converted.
059: */
060: public static DERUTF8String getInstance(ASN1TaggedObject obj,
061: boolean explicit) {
062: return getInstance(obj.getObject());
063: }
064:
065: /**
066: * basic constructor - byte encoded string.
067: */
068: DERUTF8String(byte[] string) {
069: int i = 0;
070: int length = 0;
071:
072: while (i < string.length) {
073: length++;
074: if ((string[i] & 0xe0) == 0xe0) {
075: i += 3;
076: } else if ((string[i] & 0xc0) == 0xc0) {
077: i += 2;
078: } else {
079: i += 1;
080: }
081: }
082:
083: char[] cs = new char[length];
084:
085: i = 0;
086: length = 0;
087:
088: while (i < string.length) {
089: char ch;
090:
091: if ((string[i] & 0xe0) == 0xe0) {
092: ch = (char) (((string[i] & 0x1f) << 12)
093: | ((string[i + 1] & 0x3f) << 6) | (string[i + 2] & 0x3f));
094: i += 3;
095: } else if ((string[i] & 0xc0) == 0xc0) {
096: ch = (char) (((string[i] & 0x3f) << 6) | (string[i + 1] & 0x3f));
097: i += 2;
098: } else {
099: ch = (char) (string[i] & 0xff);
100: i += 1;
101: }
102:
103: cs[length++] = ch;
104: }
105:
106: this .string = new String(cs);
107: }
108:
109: /**
110: * basic constructor
111: */
112: public DERUTF8String(String string) {
113: this .string = string;
114: }
115:
116: public String getString() {
117: return string;
118: }
119:
120: public int hashCode() {
121: return this .getString().hashCode();
122: }
123:
124: public boolean equals(Object o) {
125: if (!(o instanceof DERUTF8String)) {
126: return false;
127: }
128:
129: DERUTF8String s = (DERUTF8String) o;
130:
131: return this .getString().equals(s.getString());
132: }
133:
134: void encode(DEROutputStream out) throws IOException {
135: char[] c = string.toCharArray();
136: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
137:
138: for (int i = 0; i != c.length; i++) {
139: char ch = c[i];
140:
141: if (ch < 0x0080) {
142: bOut.write(ch);
143: } else if (ch < 0x0800) {
144: bOut.write(0xc0 | (ch >> 6));
145: bOut.write(0x80 | (ch & 0x3f));
146: } else {
147: bOut.write(0xe0 | (ch >> 12));
148: bOut.write(0x80 | ((ch >> 6) & 0x3F));
149: bOut.write(0x80 | (ch & 0x3F));
150: }
151: }
152:
153: out.writeEncoded(UTF8_STRING, bOut.toByteArray());
154: }
155: }
|