Home | History | Annotate | Download | only in asn1
      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 
     23 package org.apache.harmony.security.asn1;
     24 
     25 import java.io.IOException;
     26 import java.math.BigInteger;
     27 
     28 
     29 /**
     30  * This class represents ASN.1 Integer type.
     31  *
     32  * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
     33  */
     34 
     35 public class ASN1Integer extends ASN1Primitive {
     36 
     37     // default implementation
     38     private static final ASN1Integer ASN1 = new ASN1Integer();
     39 
     40     /**
     41      * Constructs ASN.1 Integer type
     42      *
     43      * The constructor is provided for inheritance purposes
     44      * when there is a need to create a custom ASN.1 Integer type.
     45      * To get a default implementation it is recommended to use
     46      * getInstance() method.
     47      */
     48     public ASN1Integer() {
     49         super(TAG_INTEGER);
     50     }
     51 
     52     /**
     53      * Returns ASN.1 Integer type default implementation
     54      *
     55      * The default implementation works with encoding
     56      * that is represented as byte array in two's-complement notation.
     57      *
     58      * @return ASN.1 Integer type default implementation
     59      */
     60     public static ASN1Integer getInstance() {
     61         return ASN1;
     62     }
     63 
     64     //
     65     //
     66     // Decode
     67     //
     68     //
     69 
     70     public Object decode(BerInputStream in) throws IOException {
     71         in.readInteger();
     72 
     73         if (in.isVerify) {
     74             return null;
     75         }
     76         return getDecodedObject(in);
     77     }
     78 
     79     /**
     80      * Extracts array of bytes from BER input stream.
     81      *
     82      * @param in - BER input stream
     83      * @return array of bytes
     84      */
     85     public Object getDecodedObject(BerInputStream in) throws IOException {
     86         byte[] bytesEncoded = new byte[in.length];
     87         System.arraycopy(in.buffer, in.contentOffset, bytesEncoded, 0,
     88                 in.length);
     89         return bytesEncoded;
     90     }
     91 
     92     //
     93     //
     94     // Encode
     95     //
     96     //
     97 
     98     public void encodeContent(BerOutputStream out) {
     99         out.encodeInteger();
    100     }
    101 
    102     public void setEncodingContent(BerOutputStream out) {
    103         out.length = ((byte[]) out.content).length;
    104     }
    105 
    106 // BEGIN android-changed
    107     /**
    108      * Converts decoded ASN.1 Integer to int value.
    109      * If the object represents an integer value
    110      * larger than 32 bits, the high bits will be lost.
    111      *
    112      * @param decoded a decoded object corresponding to this type
    113      * @return decoded int value.
    114      */
    115     public static int toIntValue(Object decoded) {
    116         return new BigInteger((byte[]) decoded).intValue();//FIXME optimize
    117     }
    118 
    119     /**
    120      * Converts decoded ASN.1 Integer to a BigInteger.
    121      *
    122      * @param decoded a decoded object corresponding to this type
    123      * @return decoded BigInteger value.
    124      */
    125     public static BigInteger toBigIntegerValue(Object decoded) {
    126         return new BigInteger((byte[]) decoded);//FIXME optimize
    127     }
    128 // END android-changed
    129 
    130     /**
    131      * Converts primitive int value to a form most suitable for encoding.
    132      *
    133      * @param value primitive value to be encoded
    134      * @return object suitable for encoding
    135      */
    136     public static Object fromIntValue(int value) {
    137         //FIXME optimize
    138         return BigInteger.valueOf(value).toByteArray();
    139     }
    140 }
    141