FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / java / security / provider / DERReader.java
blob0f6e492a5f5302d65bccf738dd33aded53a5973b
1 /* DERReader.java
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.provider;
41 import java.math.BigInteger;
42 import gnu.java.security.der.DEREncodingException;
44 public class DERReader
46 byte source[];
47 int pos;
49 static final int UNIVERSAL = 1;
50 static final int APPLICATION = 2;
51 static final int CONTEXT_SPECIFIC = 3;
52 static final int PRIVATE = 4;
55 public DERReader()
57 source = null;
58 pos = 0;
61 public DERReader( byte source[] )
63 init( source );
66 public void init( String source )
68 init( source.getBytes() );
71 public void init( byte source[] )
73 this.source = source;
74 pos = 0;
77 public BigInteger getBigInteger() throws DEREncodingException
79 return new BigInteger( getPrimitive() );
82 //Reads Primitive, definite-length method
83 private byte[] getPrimitive() throws DEREncodingException
85 int tmp = pos;
87 //Read Identifier
88 byte identifier = source[tmp++];
89 if( (0x20 & identifier) != 0)
90 throw new DEREncodingException();
91 int type = translateLeadIdentifierByte(identifier);
92 //System.out.println("Type: " + type);
94 //get tag
95 int tag = (0x1f & identifier);
96 //if( tag == 0x1f)
97 // tag = getIdentifier(tmp);
98 //System.out.println("Tag: " + tag);
100 //get length
101 byte len = source[tmp]; //may be length of length parameter
102 long length = 0x7f & len;
103 int i;
104 if( (0x80 & len) != 0 ) {
105 //System.out.println("Extra Long Length");
106 len &= 0x7f;
107 //System.out.println("Length of Length: " + len);
108 //get length here
109 length = 0;
110 for( i = 0; i < len; i++ ) {
111 tmp++;
112 length <<= 8;
113 length += (source[tmp] < 0 ) ?
114 (256 + source[tmp]) :
115 source[tmp];
116 //System.out.println("Length of Length: " + length);
118 tmp++;
119 } else
120 tmp++;
122 /*System.out.println("Position: " + tmp);
123 System.out.println("Length: " + length);
124 for( i = 0; i < 10; i++)
125 System.out.print(source[tmp + i] + " ");
126 System.out.println();*/
128 byte tmpb[] = new byte[ (int)length ];
129 System.arraycopy( source, tmp, tmpb, 0, (int)length);
130 pos = (int)(tmp + length);
131 return tmpb;
134 private int translateLeadIdentifierByte(byte b)
136 if( (0x3f & b ) == b)
137 return UNIVERSAL;
138 else if( (0x7f & b ) == b)
139 return APPLICATION;
140 else if( (0xbf & b ) == b)
141 return CONTEXT_SPECIFIC;
142 else
143 return PRIVATE;
146 private int getIdentifier(int tpos)
148 while( (0x80 & source[tpos]) != 0)
149 tpos++;
150 return tpos;