Rpc.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. using SharpCifs.Dcerpc.Ndr;
  18. namespace SharpCifs.Dcerpc
  19. {
  20. public class Rpc
  21. {
  22. public class UuidT : NdrObject
  23. {
  24. public int TimeLow;
  25. public short TimeMid;
  26. public short TimeHiAndVersion;
  27. public byte ClockSeqHiAndReserved;
  28. public byte ClockSeqLow;
  29. public byte[] Node;
  30. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  31. public override void Encode(NdrBuffer dst)
  32. {
  33. dst.Align(4);
  34. dst.Enc_ndr_long(TimeLow);
  35. dst.Enc_ndr_short(TimeMid);
  36. dst.Enc_ndr_short(TimeHiAndVersion);
  37. dst.Enc_ndr_small(ClockSeqHiAndReserved);
  38. dst.Enc_ndr_small(ClockSeqLow);
  39. int nodes = 6;
  40. int nodei = dst.Index;
  41. dst.Advance(1 * nodes);
  42. dst = dst.Derive(nodei);
  43. for (int i = 0; i < nodes; i++)
  44. {
  45. dst.Enc_ndr_small(Node[i]);
  46. }
  47. }
  48. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  49. public override void Decode(NdrBuffer src)
  50. {
  51. src.Align(4);
  52. TimeLow = src.Dec_ndr_long();
  53. TimeMid = (short)src.Dec_ndr_short();
  54. TimeHiAndVersion = (short)src.Dec_ndr_short();
  55. ClockSeqHiAndReserved = unchecked((byte)src.Dec_ndr_small());
  56. ClockSeqLow = unchecked((byte)src.Dec_ndr_small());
  57. int nodes = 6;
  58. int nodei = src.Index;
  59. src.Advance(1 * nodes);
  60. if (Node == null)
  61. {
  62. if (nodes < 0 || nodes > unchecked(0xFFFF))
  63. {
  64. throw new NdrException(NdrException.InvalidConformance);
  65. }
  66. Node = new byte[nodes];
  67. }
  68. src = src.Derive(nodei);
  69. for (int i = 0; i < nodes; i++)
  70. {
  71. Node[i] = unchecked((byte)src.Dec_ndr_small());
  72. }
  73. }
  74. }
  75. public class PolicyHandle : NdrObject
  76. {
  77. public int Type;
  78. public UuidT Uuid;
  79. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  80. public override void Encode(NdrBuffer dst)
  81. {
  82. dst.Align(4);
  83. dst.Enc_ndr_long(Type);
  84. dst.Enc_ndr_long(Uuid.TimeLow);
  85. dst.Enc_ndr_short(Uuid.TimeMid);
  86. dst.Enc_ndr_short(Uuid.TimeHiAndVersion);
  87. dst.Enc_ndr_small(Uuid.ClockSeqHiAndReserved);
  88. dst.Enc_ndr_small(Uuid.ClockSeqLow);
  89. int uuidNodes = 6;
  90. int uuidNodei = dst.Index;
  91. dst.Advance(1 * uuidNodes);
  92. dst = dst.Derive(uuidNodei);
  93. for (int i = 0; i < uuidNodes; i++)
  94. {
  95. dst.Enc_ndr_small(Uuid.Node[i]);
  96. }
  97. }
  98. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  99. public override void Decode(NdrBuffer src)
  100. {
  101. src.Align(4);
  102. Type = src.Dec_ndr_long();
  103. src.Align(4);
  104. if (Uuid == null)
  105. {
  106. Uuid = new UuidT();
  107. }
  108. Uuid.TimeLow = src.Dec_ndr_long();
  109. Uuid.TimeMid = (short)src.Dec_ndr_short();
  110. Uuid.TimeHiAndVersion = (short)src.Dec_ndr_short();
  111. Uuid.ClockSeqHiAndReserved = unchecked((byte)src.Dec_ndr_small());
  112. Uuid.ClockSeqLow = unchecked((byte)src.Dec_ndr_small());
  113. int uuidNodes = 6;
  114. int uuidNodei = src.Index;
  115. src.Advance(1 * uuidNodes);
  116. if (Uuid.Node == null)
  117. {
  118. if (uuidNodes < 0 || uuidNodes > unchecked(0xFFFF))
  119. {
  120. throw new NdrException(NdrException.InvalidConformance);
  121. }
  122. Uuid.Node = new byte[uuidNodes];
  123. }
  124. src = src.Derive(uuidNodei);
  125. for (int i = 0; i < uuidNodes; i++)
  126. {
  127. Uuid.Node[i] = unchecked((byte)src.Dec_ndr_small());
  128. }
  129. }
  130. }
  131. public class Unicode_string : NdrObject
  132. {
  133. public short Length;
  134. public short MaximumLength;
  135. public short[] Buffer;
  136. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  137. public override void Encode(NdrBuffer dst)
  138. {
  139. dst.Align(4);
  140. dst.Enc_ndr_short(Length);
  141. dst.Enc_ndr_short(MaximumLength);
  142. dst.Enc_ndr_referent(Buffer, 1);
  143. if (Buffer != null)
  144. {
  145. dst = dst.Deferred;
  146. int bufferl = Length / 2;
  147. int buffers = MaximumLength / 2;
  148. dst.Enc_ndr_long(buffers);
  149. dst.Enc_ndr_long(0);
  150. dst.Enc_ndr_long(bufferl);
  151. int bufferi = dst.Index;
  152. dst.Advance(2 * bufferl);
  153. dst = dst.Derive(bufferi);
  154. for (int i = 0; i < bufferl; i++)
  155. {
  156. dst.Enc_ndr_short(Buffer[i]);
  157. }
  158. }
  159. }
  160. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  161. public override void Decode(NdrBuffer src)
  162. {
  163. src.Align(4);
  164. Length = (short)src.Dec_ndr_short();
  165. MaximumLength = (short)src.Dec_ndr_short();
  166. int bufferp = src.Dec_ndr_long();
  167. if (bufferp != 0)
  168. {
  169. src = src.Deferred;
  170. int buffers = src.Dec_ndr_long();
  171. src.Dec_ndr_long();
  172. int bufferl = src.Dec_ndr_long();
  173. int bufferi = src.Index;
  174. src.Advance(2 * bufferl);
  175. if (Buffer == null)
  176. {
  177. if (buffers < 0 || buffers > unchecked(0xFFFF))
  178. {
  179. throw new NdrException(NdrException.InvalidConformance);
  180. }
  181. Buffer = new short[buffers];
  182. }
  183. src = src.Derive(bufferi);
  184. for (int i = 0; i < bufferl; i++)
  185. {
  186. Buffer[i] = (short)src.Dec_ndr_short();
  187. }
  188. }
  189. }
  190. }
  191. public class SidT : NdrObject
  192. {
  193. public byte Revision;
  194. public byte SubAuthorityCount;
  195. public byte[] IdentifierAuthority;
  196. public int[] SubAuthority;
  197. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  198. public override void Encode(NdrBuffer dst)
  199. {
  200. dst.Align(4);
  201. int subAuthoritys = SubAuthorityCount;
  202. dst.Enc_ndr_long(subAuthoritys);
  203. dst.Enc_ndr_small(Revision);
  204. dst.Enc_ndr_small(SubAuthorityCount);
  205. int identifierAuthoritys = 6;
  206. int identifierAuthorityi = dst.Index;
  207. dst.Advance(1 * identifierAuthoritys);
  208. int subAuthorityi = dst.Index;
  209. dst.Advance(4 * subAuthoritys);
  210. dst = dst.Derive(identifierAuthorityi);
  211. for (int i = 0; i < identifierAuthoritys; i++)
  212. {
  213. dst.Enc_ndr_small(IdentifierAuthority[i]);
  214. }
  215. dst = dst.Derive(subAuthorityi);
  216. for (int i1 = 0; i1 < subAuthoritys; i1++)
  217. {
  218. dst.Enc_ndr_long(SubAuthority[i1]);
  219. }
  220. }
  221. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  222. public override void Decode(NdrBuffer src)
  223. {
  224. src.Align(4);
  225. int subAuthoritys = src.Dec_ndr_long();
  226. Revision = unchecked((byte)src.Dec_ndr_small());
  227. SubAuthorityCount = unchecked((byte)src.Dec_ndr_small());
  228. int identifierAuthoritys = 6;
  229. int identifierAuthorityi = src.Index;
  230. src.Advance(1 * identifierAuthoritys);
  231. int subAuthorityi = src.Index;
  232. src.Advance(4 * subAuthoritys);
  233. if (IdentifierAuthority == null)
  234. {
  235. if (identifierAuthoritys < 0 || identifierAuthoritys > unchecked(0xFFFF))
  236. {
  237. throw new NdrException(NdrException.InvalidConformance);
  238. }
  239. IdentifierAuthority = new byte[identifierAuthoritys];
  240. }
  241. src = src.Derive(identifierAuthorityi);
  242. for (int i = 0; i < identifierAuthoritys; i++)
  243. {
  244. IdentifierAuthority[i] = unchecked((byte)src.Dec_ndr_small());
  245. }
  246. if (SubAuthority == null)
  247. {
  248. if (subAuthoritys < 0 || subAuthoritys > unchecked(0xFFFF))
  249. {
  250. throw new NdrException(NdrException.InvalidConformance);
  251. }
  252. SubAuthority = new int[subAuthoritys];
  253. }
  254. src = src.Derive(subAuthorityi);
  255. for (int i1 = 0; i1 < subAuthoritys; i1++)
  256. {
  257. SubAuthority[i1] = src.Dec_ndr_long();
  258. }
  259. }
  260. }
  261. }
  262. }