KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > netbios > Name


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  * "Christopher R. Hertel" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.netbios;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
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 JavaDoc DEFAULT_SCOPE = Config.getProperty( "jcifs.netbios.scope" );
31
32     static final String JavaDoc OEM_ENCODING =
33                 Config.getProperty( "jcifs.encoding",
34                         System.getProperty( "file.encoding" ));
35
36     public String JavaDoc name, scope;
37     public int hexCode;
38     int srcHashCode; /* srcHashCode must be set by name resolution
39                       * routines before entry into addressCache
40                       */

41
42     Name() {
43     }
44     public Name( String JavaDoc name, int hexCode, String JavaDoc 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         // write 0x20 in first byte
56
dst[dstIndex] = 0x20;
57
58         // write name
59
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 JavaDoc 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 JavaDoc( tmp, 0, length, Name.OEM_ENCODING );
90         } catch( UnsupportedEncodingException JavaDoc 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         // copy new scope in
103
dst[dstIndex++] = (byte)'.';
104         try {
105             System.arraycopy( scope.getBytes( Name.OEM_ENCODING ), 0, dst, dstIndex, scope.length() );
106         } catch( UnsupportedEncodingException JavaDoc uee ) {
107         }
108         dstIndex += scope.length();
109
110         dst[dstIndex++] = (byte)0x00;
111
112         // now go over scope backwards converting '.' to label length
113

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 JavaDoc sb;
132
133         if(( n = src[srcIndex++] & 0xFF ) == 0 ) {
134             scope = null;
135             return 1;
136         }
137
138         try {
139             sb = new StringBuffer JavaDoc( new String JavaDoc( src, srcIndex, n, Name.OEM_ENCODING ));
140             srcIndex += n;
141             while(( n = src[srcIndex++] & 0xFF ) != 0 ) {
142                 sb.append( '.' ).append( new String JavaDoc( src, srcIndex, n, Name.OEM_ENCODING ));
143                 srcIndex += n;
144             }
145             scope = sb.toString();
146         } catch( UnsupportedEncodingException JavaDoc 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; /* hashCode is different depending
158                                         * on where it came from
159                                         */

160         if( scope != null && scope.length() != 0 ) {
161             result += scope.hashCode();
162         }
163         return result;
164     }
165     public boolean equals( Object JavaDoc 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 JavaDoc toString() {
178         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
179         String JavaDoc n = name;
180
181         // fix MSBROWSE name
182
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 JavaDoc( 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