RPCHelper.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.RPC
  12. {
  13. public class RPCHelper
  14. {
  15. /// <summary>
  16. /// Read port_any_t string structure
  17. /// </summary>
  18. public static string ReadPortAddress(byte[] buffer, int offset)
  19. {
  20. ushort length = LittleEndianConverter.ToUInt16(buffer, offset + 0);
  21. // The length includes the C NULL string termination
  22. return ByteReader.ReadAnsiString(buffer, offset + 2, length - 1);
  23. }
  24. public static string ReadPortAddress(byte[] buffer, ref int offset)
  25. {
  26. string result = ReadPortAddress(buffer, offset);
  27. offset += result.Length + 3;
  28. return result;
  29. }
  30. public static void WritePortAddress(byte[] buffer, int offset, string value)
  31. {
  32. ushort length = (ushort)(value.Length + 1);
  33. LittleEndianWriter.WriteUInt16(buffer, offset + 0, length);
  34. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset + 2, value);
  35. }
  36. public static void WritePortAddress(byte[] buffer, ref int offset, string value)
  37. {
  38. WritePortAddress(buffer, offset, value);
  39. offset += value.Length + 3;
  40. }
  41. }
  42. }