1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.kerberos.shared.messages.value.flags;
21
22 import org.apache.directory.shared.asn1.primitives.BitString;
23
24 /**
25 * An implementation of a BitString for any KerberosFlags. The different values
26 * are stored in an int, as there can't be more than 32 flags (TicketFlag).
27 *
28 * Some basic operations are implemented in this abstract class, like those
29 * manipulating flags.
30 *
31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32 * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $
33 */
34 public abstract class AbstractKerberosFlags extends BitString implements KerberosFlags
35 {
36 /**
37 * The maximum size of the BitString as specified for Kerberos flags.
38 */
39 public static final int MAX_SIZE = 32;
40
41 /** The associated value */
42 protected int value;
43
44
45 /**
46 * Standard constructor, which create a BitString containing 32 bits
47 */
48 public AbstractKerberosFlags()
49 {
50 super( MAX_SIZE );
51 }
52
53
54 /**
55 * Standard constructor, taking a byte array
56 */
57 public AbstractKerberosFlags( byte[] flags )
58 {
59 super( flags );
60 value = ( ( getBytes()[0] & 0x00F ) << 24 ) | ( ( getBytes()[1] & 0x00FF ) << 16 ) | ( ( getBytes()[2] & 0x00FF ) << 8 ) | ( 0x00FF & getBytes()[3] );
61 }
62
63
64 /**
65 * A static method to get the bayte array representation of an int
66 * @return The byte array for a list of flags.
67 */
68 public static byte[] getBytes( int flags )
69 {
70 return new byte[]{
71 (byte)( flags >>> 24),
72 (byte)( ( flags >> 16 ) & 0x00ff ),
73 (byte)( ( flags >> 8 ) & 0x00ff ),
74 (byte)( flags & 0x00ff ) };
75 }
76
77
78 /**
79 * @return The byte array for a KerberosFlags
80 */
81 public byte[] getBytes()
82 {
83 return getData();
84 }
85
86
87 /**
88 * Returns the int value associated with the flags
89 */
90 public int getIntValue()
91 {
92 return value;
93 }
94
95
96 /**
97 * Check if a flag is set
98 * @param flags The flags to test
99 * @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 }