1 19 20 package jcifs.netbios; 21 22 import java.io.UnsupportedEncodingException ; 23 import jcifs.Config; 24 import jcifs.util.Hexdump; 25 26 public class Name { 27 28 private static final int TYPE_OFFSET = 31; 29 private static final int SCOPE_OFFSET = 33; 30 private static final String DEFAULT_SCOPE = Config.getProperty( "jcifs.netbios.scope" ); 31 32 static final String OEM_ENCODING = 33 Config.getProperty( "jcifs.encoding", 34 System.getProperty( "file.encoding" )); 35 36 public String name, scope; 37 public int hexCode; 38 int srcHashCode; 41 42 Name() { 43 } 44 public Name( String name, int hexCode, String scope ) { 45 if( name.length() > 15 ) { 46 name = name.substring( 0, 15 ); 47 } 48 this.name = name.toUpperCase(); 49 this.hexCode = hexCode; 50 this.scope = scope != null && scope.length() > 0 ? scope : DEFAULT_SCOPE; 51 this.srcHashCode = 0; 52 } 53 54 int writeWireFormat( byte[] dst, int dstIndex ) { 55 dst[dstIndex] = 0x20; 57 58 try { 60 byte tmp[] = name.getBytes( Name.OEM_ENCODING ); 61 int i; 62 for( i = 0; i < tmp.length; i++ ) { 63 dst[dstIndex + ( 2 * i + 1 )] = (byte)((( tmp[i] & 0xF0 ) >> 4 ) + 0x41 ); 64 dst[dstIndex + ( 2 * i + 2 )] = (byte)(( tmp[i] & 0x0F ) + 0x41 ); 65 } 66 for( ; i < 15; i++ ) { 67 dst[dstIndex + ( 2 * i + 1 )] = (byte)0x43; 68 dst[dstIndex + ( 2 * i + 2 )] = (byte)0x41; 69 } 70 dst[dstIndex + TYPE_OFFSET] = (byte)((( hexCode & 0xF0 ) >> 4 ) + 0x41 ); 71 dst[dstIndex + TYPE_OFFSET + 1] = (byte)(( hexCode & 0x0F ) + 0x41 ); 72 } catch( UnsupportedEncodingException uee ) { 73 } 74 return SCOPE_OFFSET + writeScopeWireFormat( dst, dstIndex + SCOPE_OFFSET ); 75 } 76 77 int readWireFormat( byte[] src, int srcIndex ) { 78 79 byte tmp[] = new byte[SCOPE_OFFSET]; 80 int length = 15; 81 for( int i = 0; i < 15; i++ ) { 82 tmp[i] = (byte)((( src[srcIndex + ( 2 * i + 1 )] & 0xFF ) - 0x41 ) << 4 ); 83 tmp[i] |= (byte)((( src[srcIndex + ( 2 * i + 2 )] & 0xFF ) - 0x41 ) & 0x0F ); 84 if( tmp[i] != (byte)' ' ) { 85 length = i + 1; 86 } 87 } 88 try { 89 name = new String ( tmp, 0, length, Name.OEM_ENCODING ); 90 } catch( UnsupportedEncodingException uee ) { 91 } 92 hexCode = (( src[srcIndex + TYPE_OFFSET] & 0xFF ) - 0x41 ) << 4; 93 hexCode |= (( src[srcIndex + TYPE_OFFSET + 1] & 0xFF ) - 0x41 ) & 0x0F; 94 return SCOPE_OFFSET + readScopeWireFormat( src, srcIndex + SCOPE_OFFSET ); 95 } 96 int writeScopeWireFormat( byte[] dst, int dstIndex ) { 97 if( scope == null ) { 98 dst[dstIndex] = (byte)0x00; 99 return 1; 100 } 101 102 dst[dstIndex++] = (byte)'.'; 104 try { 105 System.arraycopy( scope.getBytes( Name.OEM_ENCODING ), 0, dst, dstIndex, scope.length() ); 106 } catch( UnsupportedEncodingException uee ) { 107 } 108 dstIndex += scope.length(); 109 110 dst[dstIndex++] = (byte)0x00; 111 112 114 int i = dstIndex - 2; 115 int e = i - scope.length(); 116 int c = 0; 117 118 do { 119 if( dst[i] == '.' ) { 120 dst[i] = (byte)c; 121 c = 0; 122 } else { 123 c++; 124 } 125 } while( i-- > e ); 126 return scope.length() + 2; 127 } 128 int readScopeWireFormat( byte[] src, int srcIndex ) { 129 int start = srcIndex; 130 int n; 131 StringBuffer sb; 132 133 if(( n = src[srcIndex++] & 0xFF ) == 0 ) { 134 scope = null; 135 return 1; 136 } 137 138 try { 139 sb = new StringBuffer ( new String ( src, srcIndex, n, Name.OEM_ENCODING )); 140 srcIndex += n; 141 while(( n = src[srcIndex++] & 0xFF ) != 0 ) { 142 sb.append( '.' ).append( new String ( src, srcIndex, n, Name.OEM_ENCODING )); 143 srcIndex += n; 144 } 145 scope = sb.toString(); 146 } catch( UnsupportedEncodingException uee ) { 147 } 148 149 return srcIndex - start; 150 } 151 152 public int hashCode() { 153 int result; 154 155 result = name.hashCode(); 156 result += 65599 * hexCode; 157 result += 65599 * srcHashCode; 160 if( scope != null && scope.length() != 0 ) { 161 result += scope.hashCode(); 162 } 163 return result; 164 } 165 public boolean equals( Object obj ) { 166 Name n; 167 168 if( !( obj instanceof Name )) { 169 return false; 170 } 171 n = (Name)obj; 172 if( scope == null && n.scope == null ) { 173 return name.equals( n.name ) && hexCode == n.hexCode; 174 } 175 return name.equals( n.name ) && hexCode == n.hexCode && scope.equals( n.scope ); 176 } 177 public String toString() { 178 StringBuffer sb = new StringBuffer (); 179 String n = name; 180 181 if( n == null ) { 183 n = "null"; 184 } else if( n.charAt( 0 ) == 0x01 ) { 185 char c[] = n.toCharArray(); 186 c[0] = '.'; 187 c[1] = '.'; 188 c[14] = '.'; 189 n = new String ( c ); 190 } 191 192 sb.append( n ).append( "<" ).append( Hexdump.toHexString( hexCode, 2 )).append( ">" ); 193 if( scope != null ) { 194 sb.append( "." ).append( scope ); 195 } 196 return sb.toString(); 197 } 198 } 199 200 | Popular Tags |