001 /* 002 * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org) 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 005 * software and associated documentation files (the "Software"), to deal in the Software 006 * without restriction, including without limitation the rights to use, copy, modify, 007 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 008 * permit persons to whom the Software is furnished to do so, subject to the following 009 * conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in all copies 012 * or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 015 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 016 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 017 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 018 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 019 * DEALINGS IN THE SOFTWARE. 020 * 021 */ 022 023 package org.apache.directory.shared.asn1.der; 024 025 026 import java.io.IOException; 027 import java.util.Arrays; 028 029 030 /** 031 * DER object. 032 */ 033 public abstract class DERObject implements DEREncodable 034 { 035 static final int TERMINATOR = 0x00; 036 037 static final int BOOLEAN = 0x01; 038 039 static final int INTEGER = 0x02; 040 041 static final int BIT_STRING = 0x03; 042 043 static final int OCTET_STRING = 0x04; 044 045 static final int NULL = 0x05; 046 047 static final int OBJECT_IDENTIFIER = 0x06; 048 049 static final int EXTERNAL = 0x08; 050 051 static final int ENUMERATED = 0x0a; 052 053 static final int SEQUENCE = 0x10; 054 055 static final int SET = 0x11; 056 057 static final int NUMERIC_STRING = 0x12; 058 059 static final int PRINTABLE_STRING = 0x13; 060 061 static final int T61_STRING = 0x14; 062 063 static final int VIDEOTEX_STRING = 0x15; 064 065 static final int IA5_STRING = 0x16; 066 067 static final int UTC_TIME = 0x17; 068 069 static final int GENERALIZED_TIME = 0x18; 070 071 static final int GRAPHIC_STRING = 0x19; 072 073 static final int VISIBLE_STRING = 0x1a; 074 075 static final int GENERAL_STRING = 0x1b; 076 077 static final int UNIVERSAL_STRING = 0x1c; 078 079 static final int BMP_STRING = 0x1e; 080 081 static final int UTF8_STRING = 0x0c; 082 083 static final int CONSTRUCTED = 0x20; 084 085 static final int APPLICATION = 0x40; 086 087 static final int TAGGED = 0x80; 088 089 protected int tag; 090 091 protected byte[] value; 092 093 094 /** 095 * Basic DERObject constructor. 096 */ 097 protected DERObject(int tag, byte[] value) 098 { 099 this.tag = tag; 100 this.value = value; 101 } 102 103 104 public void encode( ASN1OutputStream out ) throws IOException 105 { 106 out.writeEncoded( tag, value ); 107 } 108 109 110 byte[] getOctets() 111 { 112 return value; 113 } 114 115 116 /** 117 * Fast rotate left and XOR hashcode generator. 118 * 119 * @return a hash code for the byte array backing this object. 120 */ 121 public int hashCode() 122 { 123 int hash = 0; 124 int len = value.length; 125 126 for ( int i = 0; i < len; i++ ) 127 { 128 // rotate left and xor 129 hash <<= 1; 130 if ( hash < 0 ) 131 { 132 hash |= 1; 133 } 134 hash ^= value[i]; 135 } 136 137 return hash; 138 } 139 140 141 /** 142 * Two DERObjects are equal if their underlying byte arrays are equal. 143 * 144 * @return true if the two DERObject underlying byte arrays are equal. 145 */ 146 public boolean equals( Object o ) 147 { 148 if ( this == o ) 149 { 150 return true; 151 } 152 153 if ( !( o instanceof DERObject ) ) 154 { 155 return false; 156 } 157 158 DERObject that = ( DERObject ) o; 159 160 return Arrays.equals( this.value, that.value ); 161 } 162 }