1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using SharpCifs.Util;
using SharpCifs.Util.Sharpen;
namespace SharpCifs.Dcerpc.Ndr
{
public class NdrBuffer
{
internal int Referent;
internal Hashtable Referents;
internal class Entry
{
internal int Referent;
internal object Obj;
}
public byte[] Buf;
public int Start;
public int Index;
public int Length;
public NdrBuffer Deferred;
public NdrBuffer(byte[] buf, int start)
{
this.Buf = buf;
this.Start = Index = start;
Length = 0;
Deferred = this;
}
public virtual NdrBuffer Derive(int idx)
{
NdrBuffer nb = new NdrBuffer(Buf, Start);
nb.Index = idx;
nb.Deferred = Deferred;
return nb;
}
public virtual void Reset()
{
Index = Start;
Length = 0;
Deferred = this;
}
public virtual int GetIndex()
{
return Index;
}
public virtual void SetIndex(int index)
{
this.Index = index;
}
public virtual int GetCapacity()
{
return Buf.Length - Start;
}
public virtual int GetTailSpace()
{
return Buf.Length - Index;
}
public virtual byte[] GetBuffer()
{
return Buf;
}
public virtual int Align(int boundary, byte value)
{
int n = Align(boundary);
int i = n;
while (i > 0)
{
Buf[Index - i] = value;
i--;
}
return n;
}
public virtual void WriteOctetArray(byte[] b, int i, int l)
{
Array.Copy(b, i, Buf, Index, l);
Advance(l);
}
public virtual void ReadOctetArray(byte[] b, int i, int l)
{
Array.Copy(Buf, Index, b, i, l);
Advance(l);
}
public virtual int GetLength()
{
return Deferred.Length;
}
public virtual void SetLength(int length)
{
Deferred.Length = length;
}
public virtual void Advance(int n)
{
Index += n;
if ((Index - Start) > Deferred.Length)
{
Deferred.Length = Index - Start;
}
}
public virtual int Align(int boundary)
{
int m = boundary - 1;
int i = Index - Start;
int n = ((i + m) & ~m) - i;
Advance(n);
return n;
}
public virtual void Enc_ndr_small(int s)
{
Buf[Index] = unchecked((byte)(s & unchecked(0xFF)));
Advance(1);
}
public virtual int Dec_ndr_small()
{
int val = Buf[Index] & unchecked(0xFF);
Advance(1);
return val;
}
public virtual void Enc_ndr_short(int s)
{
Align(2);
Encdec.Enc_uint16le((short)s, Buf, Index);
Advance(2);
}
public virtual int Dec_ndr_short()
{
Align(2);
int val = Encdec.Dec_uint16le(Buf, Index);
Advance(2);
return val;
}
public virtual void Enc_ndr_long(int l)
{
Align(4);
Encdec.Enc_uint32le(l, Buf, Index);
Advance(4);
}
public virtual int Dec_ndr_long()
{
Align(4);
int val = Encdec.Dec_uint32le(Buf, Index);
Advance(4);
return val;
}
public virtual void Enc_ndr_hyper(long h)
{
Align(8);
Encdec.Enc_uint64le(h, Buf, Index);
Advance(8);
}
public virtual long Dec_ndr_hyper()
{
Align(8);
long val = Encdec.Dec_uint64le(Buf, Index);
Advance(8);
return val;
}
public virtual void Enc_ndr_string(string s)
{
Align(4);
int i = Index;
int len = s.Length;
Encdec.Enc_uint32le(len + 1, Buf, i);
i += 4;
Encdec.Enc_uint32le(0, Buf, i);
i += 4;
Encdec.Enc_uint32le(len + 1, Buf, i);
i += 4;
try
{
Array.Copy(Runtime.GetBytesForString(s, "UTF-16LE"), 0, Buf, i, len
* 2);
}
catch (UnsupportedEncodingException)
{
}
i += len * 2;
Buf[i++] = unchecked((byte)('\0'));
Buf[i++] = unchecked((byte)('\0'));
Advance(i - Index);
}
/// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
public virtual string Dec_ndr_string()
{
Align(4);
int i = Index;
string val = null;
int len = Encdec.Dec_uint32le(Buf, i);
i += 12;
if (len != 0)
{
len--;
int size = len * 2;
try
{
if (size < 0 || size > unchecked(0xFFFF))
{
throw new NdrException(NdrException.InvalidConformance);
}
val = Runtime.GetStringForBytes(Buf, i, size, "UTF-16LE");
i += size + 2;
}
catch (UnsupportedEncodingException)
{
}
}
Advance(i - Index);
return val;
}
private int GetDceReferent(object obj)
{
Entry e;
if (Referents == null)
{
Referents = new Hashtable();
Referent = 1;
}
if ((e = (Entry)Referents.Get(obj)) == null)
{
e = new Entry();
e.Referent = Referent++;
e.Obj = obj;
Referents.Put(obj, e);
}
return e.Referent;
}
public virtual void Enc_ndr_referent(object obj, int type)
{
if (obj == null)
{
Enc_ndr_long(0);
return;
}
switch (type)
{
case 1:
case 3:
{
Enc_ndr_long(Runtime.IdentityHashCode(obj));
return;
}
case 2:
{
Enc_ndr_long(GetDceReferent(obj));
return;
}
}
}
public override string ToString()
{
return "start=" + Start + ",index=" + Index + ",length=" + GetLength();
}
}
}
|