aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/IO/SharpCifs/Util/Encdec.cs
blob: 7e4d57705b5de1adb73b56e4b70a2c6e07aae257 (plain)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 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 System.IO;
using SharpCifs.Util.Sharpen;

namespace SharpCifs.Util
{
	public class Encdec
	{
		public const long MillisecondsBetween1970And1601 = 11644473600000L;

		public const long SecBetweeen1904And1970 = 2082844800L;

		public const int Time1970Sec32Be = 1;

		public const int Time1970Sec32Le = 2;

		public const int Time1904Sec32Be = 3;

		public const int Time1904Sec32Le = 4;

		public const int Time1601Nanos64Le = 5;

		public const int Time1601Nanos64Be = 6;

		public const int Time1970Millis64Be = 7;

		public const int Time1970Millis64Le = 8;

		public static int Enc_uint16be(short s, byte[] dst, int di)
		{
			dst[di++] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
			dst[di] = unchecked((byte)(s & unchecked(0xFF)));
			return 2;
		}

		public static int Enc_uint32be(int i, byte[] dst, int di)
		{
			dst[di++] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
			dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
			dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
			dst[di] = unchecked((byte)(i & unchecked(0xFF)));
			return 4;
		}

		public static int Enc_uint16le(short s, byte[] dst, int di)
		{
			dst[di++] = unchecked((byte)(s & unchecked(0xFF)));
			dst[di] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
			return 2;
		}

		public static int Enc_uint32le(int i, byte[] dst, int di)
		{
			dst[di++] = unchecked((byte)(i & unchecked(0xFF)));
			dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
			dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
			dst[di] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
			return 4;
		}

		public static short Dec_uint16be(byte[] src, int si)
		{
			return (short)(((src[si] & unchecked(0xFF)) << 8) | (src[si + 1] & unchecked(
				0xFF)));
		}

		public static int Dec_uint32be(byte[] src, int si)
		{
			return ((src[si] & unchecked(0xFF)) << 24) | ((src[si + 1] & unchecked(0xFF)) << 16) | ((src[si + 2] & unchecked(0xFF)) << 8) | (src[si + 3] 
				& unchecked(0xFF));
		}

		public static short Dec_uint16le(byte[] src, int si)
		{
			return (short)((src[si] & unchecked(0xFF)) | ((src[si + 1] & unchecked(0xFF)) << 8));
		}

		public static int Dec_uint32le(byte[] src, int si)
		{
			return (src[si] & unchecked(0xFF)) | ((src[si + 1] & unchecked(0xFF
				)) << 8) | ((src[si + 2] & unchecked(0xFF)) << 16) | ((src[si + 3] & unchecked(
				0xFF)) << 24);
		}

		public static int Enc_uint64be(long l, byte[] dst, int di)
		{
			Enc_uint32be((int)(l & unchecked(0xFFFFFFFFL)), dst, di + 4);
			Enc_uint32be((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di);
			return 8;
		}

		public static int Enc_uint64le(long l, byte[] dst, int di)
		{
			Enc_uint32le((int)(l & unchecked(0xFFFFFFFFL)), dst, di);
			Enc_uint32le((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di + 4);
			return 8;
		}

		public static long Dec_uint64be(byte[] src, int si)
		{
			long l;
			l = Dec_uint32be(src, si) & unchecked(0xFFFFFFFFL);
			l <<= 32;
			l |= Dec_uint32be(src, si + 4) & unchecked(0xFFFFFFFFL);
			return l;
		}

		public static long Dec_uint64le(byte[] src, int si)
		{
			long l;
			l = Dec_uint32le(src, si + 4) & unchecked(0xFFFFFFFFL);
			l <<= 32;
			l |= Dec_uint32le(src, si) & unchecked(0xFFFFFFFFL);
			return l;
		}

		public static int Enc_floatle(float f, byte[] dst, int di)
		{
			return Enc_uint32le((int)BitConverter.DoubleToInt64Bits(f), dst, di);
		}

		public static int Enc_floatbe(float f, byte[] dst, int di)
		{
            return Enc_uint32be((int)BitConverter.DoubleToInt64Bits(f), dst, di);
		}

		public static float Dec_floatle(byte[] src, int si)
		{
			return (float)BitConverter.Int64BitsToDouble(Dec_uint32le(src, si));
		}

		public static float Dec_floatbe(byte[] src, int si)
		{
            return (float)BitConverter.Int64BitsToDouble(Dec_uint32be(src, si));
		}

		public static int Enc_doublele(double d, byte[] dst, int di)
		{
            return Enc_uint64le(BitConverter.DoubleToInt64Bits(d), dst, di);
		}

		public static int Enc_doublebe(double d, byte[] dst, int di)
		{
            return Enc_uint64be(BitConverter.DoubleToInt64Bits(d), dst, di);
		}

		public static double Dec_doublele(byte[] src, int si)
		{
            return BitConverter.Int64BitsToDouble(Dec_uint64le(src, si));
		}

		public static double Dec_doublebe(byte[] src, int si)
		{
            return BitConverter.Int64BitsToDouble(Dec_uint64be(src, si));
		}

		public static int Enc_time(DateTime date, byte[] dst, int di, int enc)
		{
			long t;
			switch (enc)
			{
				case Time1970Sec32Be:
				{
					return Enc_uint32be((int)(date.GetTime() / 1000L), dst, di);
				}

				case Time1970Sec32Le:
				{
					return Enc_uint32le((int)(date.GetTime() / 1000L), dst, di);
				}

				case Time1904Sec32Be:
				{
					return Enc_uint32be((int)((date.GetTime() / 1000L + SecBetweeen1904And1970) &
						 unchecked((int)(0xFFFFFFFF))), dst, di);
				}

				case Time1904Sec32Le:
				{
					return Enc_uint32le((int)((date.GetTime() / 1000L + SecBetweeen1904And1970) &
						 unchecked((int)(0xFFFFFFFF))), dst, di);
				}

				case Time1601Nanos64Be:
				{
					t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
					return Enc_uint64be(t, dst, di);
				}

				case Time1601Nanos64Le:
				{
					t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
					return Enc_uint64le(t, dst, di);
				}

				case Time1970Millis64Be:
				{
					return Enc_uint64be(date.GetTime(), dst, di);
				}

				case Time1970Millis64Le:
				{
					return Enc_uint64le(date.GetTime(), dst, di);
				}

				default:
				{
					throw new ArgumentException("Unsupported time encoding");
				}
			}
		}

		public static DateTime Dec_time(byte[] src, int si, int enc)
		{
			long t;
			switch (enc)
			{
				case Time1970Sec32Be:
				{
					return Sharpen.Extensions.CreateDate(Dec_uint32be(src, si) * 1000L);
				}

				case Time1970Sec32Le:
				{
					return Sharpen.Extensions.CreateDate(Dec_uint32le(src, si) * 1000L);
				}

				case Time1904Sec32Be:
				{
					return Sharpen.Extensions.CreateDate(((Dec_uint32be(src, si) & unchecked(0xFFFFFFFFL)) - SecBetweeen1904And1970) * 1000L);
				}

				case Time1904Sec32Le:
				{
					return Sharpen.Extensions.CreateDate(((Dec_uint32le(src, si) & unchecked(0xFFFFFFFFL)) - SecBetweeen1904And1970) * 1000L);
				}

				case Time1601Nanos64Be:
				{
					t = Dec_uint64be(src, si);
					return Sharpen.Extensions.CreateDate(t / 10000L - MillisecondsBetween1970And1601
						);
				}

				case Time1601Nanos64Le:
				{
					t = Dec_uint64le(src, si);
					return Sharpen.Extensions.CreateDate(t / 10000L - MillisecondsBetween1970And1601
						);
				}

				case Time1970Millis64Be:
				{
					return Sharpen.Extensions.CreateDate(Dec_uint64be(src, si));
				}

				case Time1970Millis64Le:
				{
					return Sharpen.Extensions.CreateDate(Dec_uint64le(src, si));
				}

				default:
				{
					throw new ArgumentException("Unsupported time encoding");
				}
			}
		}

		/// <exception cref="System.IO.IOException"></exception>
		public static int Enc_utf8(string str, byte[] dst, int di, int dlim)
		{
			int start = di;
			int ch;
			int strlen = str.Length;
			for (int i = 0; di < dlim && i < strlen; i++)
			{
				ch = str[i];
				if ((ch >= unchecked(0x0001)) && (ch <= unchecked(0x007F)))
				{
					dst[di++] = unchecked((byte)ch);
				}
				else
				{
					if (ch > unchecked(0x07FF))
					{
						if ((dlim - di) < 3)
						{
							break;
						}
						dst[di++] = unchecked((byte)(unchecked(0xE0) | ((ch >> 12) & unchecked(0x0F))));
						dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 6) & unchecked(0x3F))));
						dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
					}
					else
					{
						if ((dlim - di) < 2)
						{
							break;
						}
						dst[di++] = unchecked((byte)(unchecked(0xC0) | ((ch >> 6) & unchecked(0x1F))));
						dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
					}
				}
			}
			return di - start;
		}

		/// <exception cref="System.IO.IOException"></exception>
		public static string Dec_utf8(byte[] src, int si, int slim)
		{
			char[] uni = new char[slim - si];
			int ui;
			int ch;
			for (ui = 0; si < slim && (ch = src[si++] & unchecked(0xFF)) != 0; ui++)
			{
				if (ch < unchecked(0x80))
				{
					uni[ui] = (char)ch;
				}
				else
				{
					if ((ch & unchecked(0xE0)) == unchecked(0xC0))
					{
						if ((slim - si) < 2)
						{
							break;
						}
						uni[ui] = (char)((ch & unchecked(0x1F)) << 6);
						ch = src[si++] & unchecked(0xFF);
						uni[ui] |= (char)((char)ch & unchecked(0x3F));
						if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
							0x80))
						{
							throw new IOException("Invalid UTF-8 sequence");
						}
					}
					else
					{
						if ((ch & unchecked(0xF0)) == unchecked(0xE0))
						{
							if ((slim - si) < 3)
							{
								break;
							}
							uni[ui] = (char)((ch & unchecked(0x0F)) << 12);
							ch = src[si++] & unchecked(0xFF);
							if ((ch & unchecked(0xC0)) != unchecked(0x80))
							{
								throw new IOException("Invalid UTF-8 sequence");
							}
						    uni[ui] |= (char)((char)(ch & unchecked(0x3F)) << 6);
						    ch = src[si++] & unchecked(0xFF);
						    uni[ui] |= (char)((char)ch & unchecked(0x3F));
						    if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
						        0x800))
						    {
						        throw new IOException("Invalid UTF-8 sequence");
						    }
						}
						else
						{
							throw new IOException("Unsupported UTF-8 sequence");
						}
					}
				}
			}
			return new string(uni, 0, ui);
		}

		/// <exception cref="System.IO.IOException"></exception>
		public static string Dec_ucs2le(byte[] src, int si, int slim, char[] buf)
		{
			int bi;
			for (bi = 0; (si + 1) < slim; bi++, si += 2)
			{
				buf[bi] = (char)Dec_uint16le(src, si);
				if (buf[bi] == '\0')
				{
					break;
				}
			}
			return new string(buf, 0, bi);
		}
	}
}