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