123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Utilities;
- namespace SMBLibrary.NetBios
- {
- public class NetBiosUtils
- {
-
-
-
-
-
- public static string GetMSNetBiosName(string name, NetBiosSuffix suffix)
- {
- if (name.Length > 15)
- {
- name = name.Substring(0, 15);
- }
- else if (name.Length < 15)
- {
- name = name.PadRight(15);
- }
- return name + (char)suffix;
- }
- public static string GetNameFromMSNetBiosName(string netBiosName)
- {
- if (netBiosName.Length != 16)
- {
- throw new ArgumentException("Invalid MS NetBIOS name");
- }
- netBiosName = netBiosName.Substring(0, 15);
- return netBiosName.TrimEnd(' ');
- }
- public static NetBiosSuffix GetSuffixFromMSNetBiosName(string netBiosName)
- {
- if (netBiosName.Length != 16)
- {
- throw new ArgumentException("Invalid MS NetBIOS name");
- }
- return (NetBiosSuffix)netBiosName[15];
- }
- public static byte[] EncodeName(string name, NetBiosSuffix suffix, string scopeID)
- {
- string netBiosName = GetMSNetBiosName(name, suffix);
- return EncodeName(netBiosName, scopeID);
- }
-
-
- public static byte[] EncodeName(string netBiosName, string scopeID)
- {
- string domainName = FirstLevelEncoding(netBiosName, scopeID);
- return SecondLevelEncoding(domainName);
- }
-
-
-
-
-
-
-
-
-
- public static string FirstLevelEncoding(string netBiosName, string scopeID)
- {
-
- if (netBiosName.Length != 16)
- {
- throw new ArgumentException("Invalid MS NetBIOS name");
- }
- StringBuilder builder = new StringBuilder();
- for (int index = 0; index < netBiosName.Length; index++)
- {
- byte c = (byte)netBiosName[index];
- byte high = (byte)(0x41 + (c >> 4));
- byte low = (byte)(0x41 + (c & 0x0F));
- builder.Append((char)high);
- builder.Append((char)low);
- }
- if (scopeID.Length > 0)
- {
- builder.Append(".");
- builder.Append(scopeID);
- }
- return builder.ToString();
- }
-
-
-
-
-
-
-
-
-
- public static byte[] SecondLevelEncoding(string domainName)
- {
- string[] labels = domainName.Split('.');
- int length = 1;
- for (int index = 0; index < labels.Length; index++)
- {
- length += 1 + labels[index].Length;
- if (labels[index].Length > 63)
- {
- throw new ArgumentException("Invalid NetBIOS label length");
- }
- }
-
- byte[] result = new byte[length];
- int offset = 0;
- foreach(string label in labels)
- {
- result[offset] = (byte)label.Length;
- offset++;
- ByteWriter.WriteAnsiString(result, offset, label, label.Length);
- offset += label.Length;
- }
- result[offset] = 0;
- return result;
- }
- public static string DecodeName(byte[] buffer, ref int offset)
- {
- string domainName = SecondLevelDecoding(buffer, ref offset);
- string name = domainName.Split('.')[0];
- return FirstLevelDecoding(name);
- }
- public static string SecondLevelDecoding(byte[] buffer, ref int offset)
- {
- StringBuilder builder = new StringBuilder();
- byte labelLength = ByteReader.ReadByte(buffer, ref offset);
- while (labelLength > 0)
- {
- if (builder.Length > 0)
- {
- builder.Append(".");
- }
-
- if (labelLength > 63)
- {
- throw new ArgumentException("Invalid NetBIOS label length");
- }
- string label = ByteReader.ReadAnsiString(buffer, offset, labelLength);
- builder.Append(label);
- offset += labelLength;
- labelLength = ByteReader.ReadByte(buffer, ref offset);
- }
- return builder.ToString();
- }
- public static string FirstLevelDecoding(string name)
- {
- StringBuilder builder = new StringBuilder();
- for(int index = 0; index < name.Length; index += 2)
- {
- byte c0 = (byte)name[index];
- byte c1 = (byte)name[index + 1];
- byte high = (byte)(((c0 - 0x41) & 0xF) << 4);
- byte low = (byte)((c1 - 0x41) & 0xF);
- byte c = (byte)(high | low);
- builder.Append((char)c);
- }
- return builder.ToString();
- }
- public static void WriteNamePointer(byte[] buffer, ref int offset, int nameOffset)
- {
- WriteNamePointer(buffer, offset, nameOffset);
- offset += 2;
- }
-
-
-
-
- public static void WriteNamePointer(byte[] buffer, int offset, int nameOffset)
- {
- ushort pointer = (ushort)(0xC000 | (nameOffset & 0x3FFF));
- BigEndianWriter.WriteUInt16(buffer, offset, pointer);
- }
- public static void WriteNamePointer(Stream stream, int nameOffset)
- {
- ushort pointer = (ushort)(0xC000 | (nameOffset & 0x3FFF));
- BigEndianWriter.WriteUInt16(stream, pointer);
- }
- }
- }
|