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 Alexey V. Varlamov, Stepan M. Mishura
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.asn1;
     24 
     25 /**
     26  * Represents ASN.1 bit string value
     27  *
     28  * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
     29  */
     30 
     31 public final class BitString {
     32 
     33     private static final byte[] SET_MASK = { (byte) 128, 64, 32, 16, 8, 4, 2, 1 };
     34 
     35     private static final byte[] RESET_MASK = { 0x7f, (byte) 0xbf, (byte) 0xdf,
     36             (byte) 0xef, (byte) 0xf7, (byte) 0xfb, (byte) 0xfd, (byte) 0xfe, };
     37 
     38     /**
     39      * Sequence of bits padded with unused bits.
     40      * @see #unusedBits
     41      */
     42     public final byte[] bytes;
     43 
     44     /**
     45      * Number of unused bits in the last byte.
     46      */
     47     public final int unusedBits;
     48 
     49     /**
     50      * Constructs bit string
     51      *
     52      * @param bytes - array of bytes that represents bit string,
     53      *                including unused bits
     54      * @param unusedBits - number of unused bits
     55      * @throws IllegalArgumentException - if parameters are invalid
     56      */
     57     public BitString(byte[] bytes, int unusedBits) {
     58 
     59         // constraints are set according X.690
     60         if (unusedBits < 0 || unusedBits > 7) {
     61             throw new IllegalArgumentException("Number of unused bits MUST be in range 0-7");
     62         }
     63 
     64         if (bytes.length == 0 && unusedBits != 0) {
     65             throw new IllegalArgumentException("For empty bit string unused bits MUST be 0");
     66         }
     67 
     68         this.bytes = bytes;
     69         this.unusedBits = unusedBits;
     70     }
     71 
     72     /**
     73      * Constructs bit string from array of booleans
     74      *
     75      * @param values - array of booleans
     76      */
     77     public BitString(boolean[] values) {
     78         unusedBits = values.length % 8;
     79         int size = values.length / 8;
     80         if (unusedBits != 0) {
     81             size++;
     82         }
     83         bytes = new byte[size];
     84         for (int i = 0; i < values.length; i++) {
     85             setBit(i, values[i]);
     86         }
     87     }
     88 
     89     public boolean getBit(int bit) {
     90         int offset = bit % 8;
     91         int index = bit / 8;
     92         return (bytes[index] & SET_MASK[offset]) != 0;
     93     }
     94 
     95     public void setBit(int bit, boolean value) {
     96         int offset = bit % 8;
     97         int index = bit / 8;
     98         if (value) {
     99             bytes[index] |= SET_MASK[offset];
    100         } else {
    101             bytes[index] &= RESET_MASK[offset];
    102         }
    103     }
    104 
    105     public boolean[] toBooleanArray() {
    106         boolean[] result = new boolean[bytes.length * 8 - unusedBits];
    107         for (int i = 0; i < result.length; i++) {
    108             result[i] = getBit(i);
    109         }
    110         return result;
    111     }
    112 }
    113