001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.kerberos.shared.messages.value.flags; 021 022 import org.apache.directory.shared.asn1.primitives.BitString; 023 024 /** 025 * An implementation of a BitString for any KerberosFlags. The different values 026 * are stored in an int, as there can't be more than 32 flags (TicketFlag). 027 * 028 * Some basic operations are implemented in this abstract class, like those 029 * manipulating flags. 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $ 033 */ 034 public abstract class AbstractKerberosFlags extends BitString implements KerberosFlags 035 { 036 /** 037 * The maximum size of the BitString as specified for Kerberos flags. 038 */ 039 public static final int MAX_SIZE = 32; 040 041 /** The associated value */ 042 protected int value; 043 044 045 /** 046 * Standard constructor, which create a BitString containing 32 bits 047 */ 048 public AbstractKerberosFlags() 049 { 050 super( MAX_SIZE ); 051 } 052 053 054 /** 055 * Standard constructor, taking a byte array 056 */ 057 public AbstractKerberosFlags( byte[] flags ) 058 { 059 super( flags ); 060 value = ( ( getBytes()[0] & 0x00F ) << 24 ) | ( ( getBytes()[1] & 0x00FF ) << 16 ) | ( ( getBytes()[2] & 0x00FF ) << 8 ) | ( 0x00FF & getBytes()[3] ); 061 } 062 063 064 /** 065 * A static method to get the bayte array representation of an int 066 * @return The byte array for a list of flags. 067 */ 068 public static byte[] getBytes( int flags ) 069 { 070 return new byte[]{ 071 (byte)( flags >>> 24), 072 (byte)( ( flags >> 16 ) & 0x00ff ), 073 (byte)( ( flags >> 8 ) & 0x00ff ), 074 (byte)( flags & 0x00ff ) }; 075 } 076 077 078 /** 079 * @return The byte array for a KerberosFlags 080 */ 081 public byte[] getBytes() 082 { 083 return getData(); 084 } 085 086 087 /** 088 * Returns the int value associated with the flags 089 */ 090 public int getIntValue() 091 { 092 return value; 093 } 094 095 096 /** 097 * Check if a flag is set 098 * @param flags The flags to test 099 * @param flag The flag to check 100 * @return True if the flag is set in the list of flags 101 */ 102 public static boolean isFlagSet( int flags, int flag ) 103 { 104 return ( flags & ( 1 << flag) ) != 0; 105 } 106 107 108 /** 109 * Check if a flag is set for the actual value 110 * 111 * @param flag The flag to check 112 * @return True if the flag is set in the list of flags 113 */ 114 public boolean isFlagSet( KerberosFlag flag ) 115 { 116 return ( value & ( 1 << flag.getOrdinal() ) ) != 0; 117 } 118 119 120 /** 121 * Check if a flag is set 122 * @param flag The flags to test 123 * @return True if the flag is set in the list of flags 124 */ 125 public boolean isFlagSet( int flag ) 126 { 127 return ( flag & ( 1 << value ) ) != 0; 128 } 129 130 131 /** 132 * Set a flag in a list of flags 133 * 134 * @param flag The flag to set 135 */ 136 public void setFlag( KerberosFlag flag ) 137 { 138 value |= 1 << flag.getOrdinal(); 139 setBit( flag.getOrdinal() ); 140 } 141 142 143 /** 144 * Set a flag in a list of flags 145 * 146 * @param flag The flag to set 147 */ 148 public void setFlag( int flag ) 149 { 150 value |= 1 << flag; 151 setBit( flag ); 152 } 153 154 155 /** 156 * Modify a byte array to an integer value 157 * @param bytes The 4 bytes byte array to transform. 158 */ 159 public void setFlags( byte[] bytes ) 160 { 161 if ( (bytes== null ) || ( bytes.length != 4 ) ) 162 { 163 value = -1; 164 } 165 166 value = ( ( getBytes()[0] & 0x00F ) << 24 ) | ( ( getBytes()[1] & 0x00FF ) << 16 ) | ( ( getBytes()[2] & 0x00FF ) << 8 ) | ( 0x00FF & getBytes()[3] ); 167 setData( bytes ); 168 } 169 170 171 /** 172 * clear a flag in a list of flags 173 * 174 * @param flag The flag to set 175 */ 176 public void clearFlag( KerberosFlag flag ) 177 { 178 value &= ~( 1 << flag.getOrdinal() ); 179 clearBit( flag.getOrdinal() ); 180 } 181 182 183 /** 184 * clear a flag in a list of flags 185 * 186 * @param flag The flag to set 187 */ 188 public void clearFlag( int flag ) 189 { 190 value &= ~( 1 << flag ); 191 clearBit( flag ); 192 } 193 194 195 /** 196 * @return The hex value for this flag, in its position. 197 * For instance, getting the flag 5 will return 0x0000 0010 198 */ 199 public int getHexValue() 200 { 201 return 1 << value; 202 } 203 }