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 public final class BitString { 31 32 private static final byte[] SET_MASK = { (byte) 128, 64, 32, 16, 8, 4, 2, 1 }; 33 34 private static final byte[] RESET_MASK = { 0x7f, (byte) 0xbf, (byte) 0xdf, 35 (byte) 0xef, (byte) 0xf7, (byte) 0xfb, (byte) 0xfd, (byte) 0xfe, }; 36 37 /** Sequence of bits padded with unused bits. */ 38 public final byte[] bytes; 39 40 /** Number of unused bits in the last byte. */ 41 public final int unusedBits; 42 43 /** 44 * @param bytes array of bytes that represents bit string, 45 * including unused bits 46 * @param unusedBits number of unused bits 47 * @throws IllegalArgumentException - if parameters are invalid 48 */ 49 public BitString(byte[] bytes, int unusedBits) { 50 // constraints are set according X.690 51 if (unusedBits < 0 || unusedBits > 7) { 52 throw new IllegalArgumentException("Number of unused bits MUST be in range 0-7"); 53 } 54 55 if (bytes.length == 0 && unusedBits != 0) { 56 throw new IllegalArgumentException("For empty bit string unused bits MUST be 0"); 57 } 58 59 this.bytes = bytes; 60 this.unusedBits = unusedBits; 61 } 62 63 public BitString(boolean[] values) { 64 unusedBits = values.length % 8; 65 int size = values.length / 8; 66 if (unusedBits != 0) { 67 size++; 68 } 69 bytes = new byte[size]; 70 for (int i = 0; i < values.length; i++) { 71 setBit(i, values[i]); 72 } 73 } 74 75 public boolean getBit(int bit) { 76 int offset = bit % 8; 77 int index = bit / 8; 78 return (bytes[index] & SET_MASK[offset]) != 0; 79 } 80 81 public void setBit(int bit, boolean value) { 82 int offset = bit % 8; 83 int index = bit / 8; 84 if (value) { 85 bytes[index] |= SET_MASK[offset]; 86 } else { 87 bytes[index] &= RESET_MASK[offset]; 88 } 89 } 90 91 public boolean[] toBooleanArray() { 92 boolean[] result = new boolean[bytes.length * 8 - unusedBits]; 93 for (int i = 0; i < result.length; i++) { 94 result[i] = getBit(i); 95 } 96 return result; 97 } 98 } 99