NDRUnicodeString.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 NDRUnicodeString : INDRStructure
  14. {
  15. public string Value;
  16. public NDRUnicodeString()
  17. {
  18. Value = String.Empty;
  19. }
  20. public NDRUnicodeString(string value)
  21. {
  22. Value = value;
  23. }
  24. public NDRUnicodeString(NDRParser parser)
  25. {
  26. Read(parser);
  27. }
  28. // 14.3.4.2 - Conformant and Varying Strings
  29. public void Read(NDRParser parser)
  30. {
  31. uint maxCount = parser.ReadUInt32();
  32. // the offset from the first index of the string to the first index of the actual subset being passed
  33. uint index = parser.ReadUInt32();
  34. // actualCount includes the null terminator
  35. uint actualCount = parser.ReadUInt32();
  36. StringBuilder builder = new StringBuilder();
  37. for (int position = 0; position < actualCount - 1; position++)
  38. {
  39. builder.Append((char)parser.ReadUInt16());
  40. }
  41. this.Value = builder.ToString();
  42. parser.ReadUInt16(); // null terminator
  43. }
  44. public void Write(NDRWriter writer)
  45. {
  46. int length = 0;
  47. if (Value != null)
  48. {
  49. length = Value.Length;
  50. }
  51. // maxCount includes the null terminator
  52. uint maxCount = (uint)(length + 1);
  53. writer.WriteUInt32(maxCount);
  54. // the offset from the first index of the string to the first index of the actual subset being passed
  55. uint index = 0;
  56. writer.WriteUInt32(index);
  57. // actualCount includes the null terminator
  58. uint actualCount = (uint)(length + 1);
  59. writer.WriteUInt32(actualCount);
  60. for (int position = 0; position < length; position++)
  61. {
  62. writer.WriteUInt16((ushort)Value[position]);
  63. }
  64. writer.WriteUInt16(0); // null terminator
  65. }
  66. }
  67. }