NamedPipeStatus.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 byte ICount;
  36. public ReadMode ReadMode;
  37. public NamedPipeType NamedPipeType;
  38. public Endpoint Endpoint;
  39. public NonBlocking NonBlocking;
  40. public NamedPipeStatus(byte[] buffer, int offset)
  41. {
  42. ICount = buffer[offset];
  43. ReadMode = (ReadMode)(buffer[offset + 1] & 0x03);
  44. NamedPipeType = (NamedPipeType)((buffer[offset + 1] & 0x0C) >> 2);
  45. Endpoint = (Endpoint)((buffer[offset + 1] & 0x40) >> 6);
  46. NonBlocking = (NonBlocking)((buffer[offset + 1] & 0x80) >> 7);
  47. }
  48. public NamedPipeStatus(ushort value)
  49. {
  50. ICount = (byte)(value & 0xFF);
  51. ReadMode = (ReadMode)((value & 0x0300) >> 8);
  52. NamedPipeType = (NamedPipeType)((value & 0x0C00) >> 10);
  53. Endpoint = (Endpoint)((value & 0x4000) >> 14);
  54. NonBlocking = (NonBlocking)((value & 0x80) >> 15);
  55. }
  56. public void WriteBytes(byte[] buffer, int offset)
  57. {
  58. buffer[offset + 0] = ICount;
  59. buffer[offset + 1] = (byte)((byte)ReadMode & 0x03);
  60. buffer[offset + 1] |= (byte)(((byte)NamedPipeType << 2) & 0x0C);
  61. buffer[offset + 1] |= (byte)(((byte)Endpoint << 6) & 0x40);
  62. buffer[offset + 1] |= (byte)(((byte)NonBlocking << 7) & 0x80);
  63. }
  64. public void WriteBytes(byte[] buffer, ref int offset)
  65. {
  66. WriteBytes(buffer, offset);
  67. offset += 2;
  68. }
  69. public ushort ToUInt16()
  70. {
  71. ushort result = ICount;
  72. result |= (ushort)(((byte)ReadMode << 8) & 0x0300);
  73. result |= (ushort)(((byte)NamedPipeType << 10) & 0x0C00);
  74. result |= (ushort)(((byte)Endpoint << 14) & 0x4000);
  75. result |= (ushort)(((byte)NonBlocking << 15) & 0x8000);
  76. return result;
  77. }
  78. public static NamedPipeStatus Read(byte[] buffer, ref int offset)
  79. {
  80. offset += 2;
  81. return new NamedPipeStatus(buffer, offset - 2);
  82. }
  83. }
  84. }