1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vladimir N. Molotkov, Stepan M. Mishura
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.asn1;
24
25import java.io.IOException;
26import java.io.UnsupportedEncodingException;
27
28
29/**
30 * This class is the super class for all string ASN.1 types
31 *
32 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
33 */
34
35public abstract class ASN1StringType extends ASN1Type {
36
37    // TODO: what about defining them as separate classes?
38    // TODO: check decoded/encoded characters
39    public static final ASN1StringType BMPSTRING = new ASN1StringType(
40            TAG_BMPSTRING) {
41    };
42
43    public static final ASN1StringType IA5STRING = new ASN1StringType(
44            TAG_IA5STRING) {
45    };
46
47    public static final ASN1StringType GENERALSTRING = new ASN1StringType(
48            TAG_GENERALSTRING) {
49    };
50
51    public static final ASN1StringType PRINTABLESTRING = new ASN1StringType(
52            TAG_PRINTABLESTRING) {
53    };
54
55    public static final ASN1StringType TELETEXSTRING = new ASN1StringType(
56            TAG_TELETEXSTRING) {
57    };
58
59    public static final ASN1StringType UNIVERSALSTRING = new ASN1StringType(
60            TAG_UNIVERSALSTRING) {
61    };
62
63    public static final ASN1StringType UTF8STRING = new ASN1StringType(
64            TAG_UTF8STRING) {
65
66        public Object getDecodedObject(BerInputStream in) throws IOException {
67            return new String(in.buffer, in.contentOffset, in.length, "UTF-8"); //$NON-NLS-1$
68        }
69
70        public void setEncodingContent(BerOutputStream out) {
71
72            try {
73                byte[] bytes = ((String) out.content).getBytes("UTF-8"); //$NON-NLS-1$
74                out.content = bytes;
75                out.length = bytes.length;
76            } catch (UnsupportedEncodingException e) {
77                throw new RuntimeException(e.getMessage());
78            }
79        }
80    };
81
82    public ASN1StringType(int tagNumber) {
83        super(tagNumber);
84    }
85
86    //
87    //
88    // Decode
89    //
90    //
91
92    /**
93     * Tests provided identifier.
94     *
95     * @param identifier -
96     *            identifier to be verified
97     * @return - true if identifier correspond to primitive or constructed
98     *         identifier of this ASN.1 string type, otherwise false
99     */
100    public final boolean checkTag(int identifier) {
101        return this.id == identifier || this.constrId == identifier;
102    }
103
104    public Object decode(BerInputStream in) throws IOException {
105
106        in.readString(this);
107
108        if (in.isVerify) {
109            return null;
110        }
111        return getDecodedObject(in);
112    }
113
114    /**
115     * Extracts String object from BER input stream.
116     *
117     * @param in - BER input stream
118     * @return java.land.String object
119     */
120    public Object getDecodedObject(BerInputStream in) throws IOException {
121        /* To ensure we get the correct encoding on non-ASCII platforms, specify
122           that we wish to convert from ASCII to the default platform encoding */
123        return new String(in.buffer, in.contentOffset, in.length, "ISO-8859-1");
124    }
125
126    //
127    //
128    // Encode
129    //
130    //
131
132    public void encodeASN(BerOutputStream out) {
133        out.encodeTag(id);
134        encodeContent(out);
135    }
136
137    public void encodeContent(BerOutputStream out) {
138        out.encodeString();
139    }
140
141    public void setEncodingContent(BerOutputStream out) {
142        try {
143            byte[] bytes = ((String) out.content).getBytes("UTF-8"); //$NON-NLS-1$
144            out.content = bytes;
145            out.length = bytes.length;
146        } catch (UnsupportedEncodingException e) {
147            throw new RuntimeException(e.getMessage());
148        }
149    }
150}
151
152
153
154