KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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