NamedPipeStatus.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2017 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. namespace SMBLibrary.SMB1
  9. {
  10. public enum ReadMode : byte
  11. {
  12. ByteMode = 0x00,
  13. MessageMode = 0x01,
  14. }
  15. public enum NamedPipeType : byte
  16. {
  17. ByteModePipe = 0x00,
  18. MessageModePipe = 0x01,
  19. }
  20. public enum Endpoint : byte
  21. {
  22. ClientSideEnd = 0x00,
  23. ServerSideEnd = 0x01,
  24. }
  25. public enum NonBlocking : byte
  26. {
  27. Block = 0x00,
  28. DoNotBlock = 0x01,
  29. }
  30. /// <summary>
  31. /// SMB_NMPIPE_STATUS
  32. /// </summary>
  33. public struct NamedPipeStatus // ushort
  34. {
  35. public const int Length = 2;
  36. public byte ICount;
  37. public ReadMode ReadMode;
  38. public NamedPipeType NamedPipeType;
  39. public Endpoint Endpoint;
  40. public NonBlocking NonBlocking;
  41. public NamedPipeStatus(byte[] buffer, int offset)
  42. {
  43. ICount = buffer[offset + 0];
  44. ReadMode = (ReadMode)(buffer[offset + 1] & 0x03);
  45. NamedPipeType = (NamedPipeType)((buffer[offset + 1] & 0x0C) >> 2);
  46. Endpoint = (Endpoint)((buffer[offset + 1] & 0x40) >> 6);
  47. NonBlocking = (NonBlocking)((buffer[offset + 1] & 0x80) >> 7);
  48. }
  49. public NamedPipeStatus(ushort value)
  50. {
  51. ICount = (byte)(value & 0xFF);
  52. ReadMode = (ReadMode)((value & 0x0300) >> 8);
  53. NamedPipeType = (NamedPipeType)((value & 0x0C00) >> 10);
  54. Endpoint = (Endpoint)((value & 0x4000) >> 14);
  55. NonBlocking = (NonBlocking)((value & 0x80) >> 15);
  56. }
  57. public void WriteBytes(byte[] buffer, int offset)
  58. {
  59. buffer[offset + 0] = ICount;
  60. buffer[offset + 1] = (byte)((byte)ReadMode & 0x03);
  61. buffer[offset + 1] |= (byte)(((byte)NamedPipeType << 2) & 0x0C);
  62. buffer[offset + 1] |= (byte)(((byte)Endpoint << 6) & 0x40);
  63. buffer[offset + 1] |= (byte)(((byte)NonBlocking << 7) & 0x80);
  64. }
  65. public void WriteBytes(byte[] buffer, ref int offset)
  66. {
  67. WriteBytes(buffer, offset);
  68. offset += Length;
  69. }
  70. public ushort ToUInt16()
  71. {
  72. ushort result = ICount;
  73. result |= (ushort)(((byte)ReadMode << 8) & 0x0300);
  74. result |= (ushort)(((byte)NamedPipeType << 10) & 0x0C00);
  75. result |= (ushort)(((byte)Endpoint << 14) & 0x4000);
  76. result |= (ushort)(((byte)NonBlocking << 15) & 0x8000);
  77. return result;
  78. }
  79. public static NamedPipeStatus Read(byte[] buffer, ref int offset)
  80. {
  81. offset += Length;
  82. return new NamedPipeStatus(buffer, offset - 2);
  83. }
  84. }
  85. }