UnicodeString.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. namespace SharpCifs.Dcerpc
  18. {
  19. public class UnicodeString : Rpc.Unicode_string
  20. {
  21. internal bool Zterm;
  22. public UnicodeString(bool zterm)
  23. {
  24. this.Zterm = zterm;
  25. }
  26. public UnicodeString(Rpc.Unicode_string rus, bool zterm)
  27. {
  28. Length = rus.Length;
  29. MaximumLength = rus.MaximumLength;
  30. Buffer = rus.Buffer;
  31. this.Zterm = zterm;
  32. }
  33. public UnicodeString(string str, bool zterm)
  34. {
  35. this.Zterm = zterm;
  36. int len = str.Length;
  37. int zt = zterm ? 1 : 0;
  38. Length = MaximumLength = (short)((len + zt) * 2);
  39. Buffer = new short[len + zt];
  40. int i;
  41. for (i = 0; i < len; i++)
  42. {
  43. Buffer[i] = (short)str[i];
  44. }
  45. if (zterm)
  46. {
  47. Buffer[i] = 0;
  48. }
  49. }
  50. public override string ToString()
  51. {
  52. int len = Length / 2 - (Zterm ? 1 : 0);
  53. char[] ca = new char[len];
  54. for (int i = 0; i < len; i++)
  55. {
  56. ca[i] = (char)Buffer[i];
  57. }
  58. return new string(ca, 0, len);
  59. }
  60. }
  61. }