Name.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.Text;
  19. using SharpCifs.Util;
  20. using SharpCifs.Util.Sharpen;
  21. namespace SharpCifs.Netbios
  22. {
  23. public class Name
  24. {
  25. private const int TypeOffset = 31;
  26. private const int ScopeOffset = 33;
  27. private static readonly string DefaultScope = Config.GetProperty("jcifs.netbios.scope"
  28. );
  29. internal static readonly string OemEncoding = Config.GetProperty("jcifs.encoding"
  30. , Runtime.GetProperty("file.encoding"));
  31. public string name;
  32. public string Scope;
  33. public int HexCode;
  34. internal int SrcHashCode;
  35. public Name()
  36. {
  37. }
  38. public Name(string name, int hexCode, string scope)
  39. {
  40. if (name.Length > 15)
  41. {
  42. name = Runtime.Substring(name, 0, 15);
  43. }
  44. this.name = name.ToUpper();
  45. this.HexCode = hexCode;
  46. this.Scope = !string.IsNullOrEmpty(scope) ? scope : DefaultScope;
  47. SrcHashCode = 0;
  48. }
  49. internal virtual int WriteWireFormat(byte[] dst, int dstIndex)
  50. {
  51. // write 0x20 in first byte
  52. dst[dstIndex] = unchecked(0x20);
  53. // write name
  54. try
  55. {
  56. byte[] tmp = Runtime.GetBytesForString(name, OemEncoding
  57. );
  58. int i;
  59. for (i = 0; i < tmp.Length; i++)
  60. {
  61. dst[dstIndex + (2 * i + 1)] = unchecked((byte)(((tmp[i] & unchecked(0xF0))
  62. >> 4) + unchecked(0x41)));
  63. dst[dstIndex + (2 * i + 2)] = unchecked((byte)((tmp[i] & unchecked(0x0F))
  64. + unchecked(0x41)));
  65. }
  66. for (; i < 15; i++)
  67. {
  68. dst[dstIndex + (2 * i + 1)] = unchecked(unchecked(0x43));
  69. dst[dstIndex + (2 * i + 2)] = unchecked(unchecked(0x41));
  70. }
  71. dst[dstIndex + TypeOffset] = unchecked((byte)(((HexCode & unchecked(0xF0)
  72. ) >> 4) + unchecked(0x41)));
  73. dst[dstIndex + TypeOffset + 1] = unchecked((byte)((HexCode & unchecked(0x0F)) + unchecked(0x41)));
  74. }
  75. catch (UnsupportedEncodingException)
  76. {
  77. }
  78. return ScopeOffset + WriteScopeWireFormat(dst, dstIndex + ScopeOffset);
  79. }
  80. internal virtual int ReadWireFormat(byte[] src, int srcIndex)
  81. {
  82. byte[] tmp = new byte[ScopeOffset];
  83. int length = 15;
  84. for (int i = 0; i < 15; i++)
  85. {
  86. tmp[i] = unchecked((byte)(((src[srcIndex + (2 * i + 1)] & unchecked(0xFF))
  87. - unchecked(0x41)) << 4));
  88. tmp[i] |= unchecked((byte)(((src[srcIndex + (2 * i + 2)] & unchecked(0xFF)
  89. ) - unchecked(0x41)) & unchecked(0x0F)));
  90. if (tmp[i] != unchecked((byte)' '))
  91. {
  92. length = i + 1;
  93. }
  94. }
  95. try
  96. {
  97. name = Runtime.GetStringForBytes(tmp, 0, length, OemEncoding
  98. );
  99. }
  100. catch (UnsupportedEncodingException)
  101. {
  102. }
  103. HexCode = ((src[srcIndex + TypeOffset] & unchecked(0xFF)) - unchecked(0x41)) << 4;
  104. HexCode |= ((src[srcIndex + TypeOffset + 1] & unchecked(0xFF)) - unchecked(
  105. 0x41)) & unchecked(0x0F);
  106. return ScopeOffset + ReadScopeWireFormat(src, srcIndex + ScopeOffset);
  107. }
  108. internal int ReadWireFormatDos(byte[] src, int srcIndex)
  109. {
  110. int length = 15;
  111. byte[] tmp = new byte[length];
  112. Array.Copy(src, srcIndex, tmp, 0, length);
  113. try
  114. {
  115. name = Runtime.GetStringForBytes(tmp, 0, length).Trim();
  116. }
  117. catch (Exception ex)
  118. {
  119. }
  120. HexCode = src[srcIndex + length];
  121. return length + 1;
  122. }
  123. internal virtual int WriteScopeWireFormat(byte[] dst, int dstIndex)
  124. {
  125. if (Scope == null)
  126. {
  127. dst[dstIndex] = unchecked(unchecked(0x00));
  128. return 1;
  129. }
  130. // copy new scope in
  131. dst[dstIndex++] = unchecked((byte)('.'));
  132. try
  133. {
  134. Array.Copy(Runtime.GetBytesForString(Scope, OemEncoding
  135. ), 0, dst, dstIndex, Scope.Length);
  136. }
  137. catch (UnsupportedEncodingException)
  138. {
  139. }
  140. dstIndex += Scope.Length;
  141. dst[dstIndex++] = unchecked(unchecked(0x00));
  142. // now go over scope backwards converting '.' to label length
  143. int i = dstIndex - 2;
  144. int e = i - Scope.Length;
  145. int c = 0;
  146. do
  147. {
  148. if (dst[i] == '.')
  149. {
  150. dst[i] = unchecked((byte)c);
  151. c = 0;
  152. }
  153. else
  154. {
  155. c++;
  156. }
  157. }
  158. while (i-- > e);
  159. return Scope.Length + 2;
  160. }
  161. internal virtual int ReadScopeWireFormat(byte[] src, int srcIndex)
  162. {
  163. int start = srcIndex;
  164. int n;
  165. StringBuilder sb;
  166. if ((n = src[srcIndex++] & unchecked(0xFF)) == 0)
  167. {
  168. Scope = null;
  169. return 1;
  170. }
  171. try
  172. {
  173. sb = new StringBuilder(Runtime.GetStringForBytes(src, srcIndex, n, OemEncoding));
  174. srcIndex += n;
  175. while ((n = src[srcIndex++] & unchecked(0xFF)) != 0)
  176. {
  177. sb.Append('.').Append(Runtime.GetStringForBytes(src, srcIndex, n, OemEncoding));
  178. srcIndex += n;
  179. }
  180. Scope = sb.ToString();
  181. }
  182. catch (UnsupportedEncodingException)
  183. {
  184. }
  185. return srcIndex - start;
  186. }
  187. public override int GetHashCode()
  188. {
  189. int result;
  190. result = name.GetHashCode();
  191. result += 65599 * HexCode;
  192. result += 65599 * SrcHashCode;
  193. if (Scope != null && Scope.Length != 0)
  194. {
  195. result += Scope.GetHashCode();
  196. }
  197. return result;
  198. }
  199. public override bool Equals(object obj)
  200. {
  201. Name n;
  202. if (!(obj is Name))
  203. {
  204. return false;
  205. }
  206. n = (Name)obj;
  207. if (Scope == null && n.Scope == null)
  208. {
  209. return name.Equals(n.name) && HexCode == n.HexCode;
  210. }
  211. return name.Equals(n.name) && HexCode == n.HexCode && Scope.Equals(n.Scope);
  212. }
  213. public override string ToString()
  214. {
  215. StringBuilder sb = new StringBuilder();
  216. //return "";
  217. string n = name;
  218. // fix MSBROWSE name
  219. if (n == null)
  220. {
  221. n = "null";
  222. }
  223. else
  224. {
  225. if (n[0] == unchecked(0x01))
  226. {
  227. char[] c = n.ToCharArray();
  228. c[0] = '.';
  229. c[1] = '.';
  230. c[14] = '.';
  231. n = new string(c);
  232. }
  233. }
  234. sb.Append(n).Append("<").Append(Hexdump.ToHexString(HexCode, 2)).Append(">");
  235. if (Scope != null)
  236. {
  237. sb.Append(".").Append(Scope);
  238. }
  239. return sb.ToString();
  240. }
  241. }
  242. }