Encdec.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 System;
  18. using System.IO;
  19. using SharpCifs.Util.Sharpen;
  20. namespace SharpCifs.Util
  21. {
  22. public class Encdec
  23. {
  24. public const long MillisecondsBetween1970And1601 = 11644473600000L;
  25. public const long SecBetweeen1904And1970 = 2082844800L;
  26. public const int Time1970Sec32Be = 1;
  27. public const int Time1970Sec32Le = 2;
  28. public const int Time1904Sec32Be = 3;
  29. public const int Time1904Sec32Le = 4;
  30. public const int Time1601Nanos64Le = 5;
  31. public const int Time1601Nanos64Be = 6;
  32. public const int Time1970Millis64Be = 7;
  33. public const int Time1970Millis64Le = 8;
  34. public static int Enc_uint16be(short s, byte[] dst, int di)
  35. {
  36. dst[di++] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
  37. dst[di] = unchecked((byte)(s & unchecked(0xFF)));
  38. return 2;
  39. }
  40. public static int Enc_uint32be(int i, byte[] dst, int di)
  41. {
  42. dst[di++] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
  43. dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
  44. dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
  45. dst[di] = unchecked((byte)(i & unchecked(0xFF)));
  46. return 4;
  47. }
  48. public static int Enc_uint16le(short s, byte[] dst, int di)
  49. {
  50. dst[di++] = unchecked((byte)(s & unchecked(0xFF)));
  51. dst[di] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
  52. return 2;
  53. }
  54. public static int Enc_uint32le(int i, byte[] dst, int di)
  55. {
  56. dst[di++] = unchecked((byte)(i & unchecked(0xFF)));
  57. dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
  58. dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
  59. dst[di] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
  60. return 4;
  61. }
  62. public static short Dec_uint16be(byte[] src, int si)
  63. {
  64. return (short)(((src[si] & unchecked(0xFF)) << 8)
  65. | (src[si + 1] & unchecked(0xFF)));
  66. }
  67. public static int Dec_uint32be(byte[] src, int si)
  68. {
  69. return ((src[si] & unchecked(0xFF)) << 24)
  70. | ((src[si + 1] & unchecked(0xFF)) << 16)
  71. | ((src[si + 2] & unchecked(0xFF)) << 8)
  72. | (src[si + 3] & unchecked(0xFF));
  73. }
  74. public static short Dec_uint16le(byte[] src, int si)
  75. {
  76. return (short)((src[si] & unchecked(0xFF))
  77. | ((src[si + 1] & unchecked(0xFF)) << 8));
  78. }
  79. public static int Dec_uint32le(byte[] src, int si)
  80. {
  81. return (src[si] & unchecked(0xFF))
  82. | ((src[si + 1] & unchecked(0xFF)) << 8)
  83. | ((src[si + 2] & unchecked(0xFF)) << 16)
  84. | ((src[si + 3] & unchecked(0xFF)) << 24);
  85. }
  86. public static int Enc_uint64be(long l, byte[] dst, int di)
  87. {
  88. Enc_uint32be((int)(l & unchecked(0xFFFFFFFFL)), dst, di + 4);
  89. Enc_uint32be((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di);
  90. return 8;
  91. }
  92. public static int Enc_uint64le(long l, byte[] dst, int di)
  93. {
  94. Enc_uint32le((int)(l & unchecked(0xFFFFFFFFL)), dst, di);
  95. Enc_uint32le((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di + 4);
  96. return 8;
  97. }
  98. public static long Dec_uint64be(byte[] src, int si)
  99. {
  100. long l;
  101. l = Dec_uint32be(src, si) & unchecked(0xFFFFFFFFL);
  102. l <<= 32;
  103. l |= Dec_uint32be(src, si + 4) & unchecked(0xFFFFFFFFL);
  104. return l;
  105. }
  106. public static long Dec_uint64le(byte[] src, int si)
  107. {
  108. long l;
  109. l = Dec_uint32le(src, si + 4) & unchecked(0xFFFFFFFFL);
  110. l <<= 32;
  111. l |= Dec_uint32le(src, si) & unchecked(0xFFFFFFFFL);
  112. return l;
  113. }
  114. public static int Enc_floatle(float f, byte[] dst, int di)
  115. {
  116. return Enc_uint32le((int)BitConverter.DoubleToInt64Bits(f), dst, di);
  117. }
  118. public static int Enc_floatbe(float f, byte[] dst, int di)
  119. {
  120. return Enc_uint32be((int)BitConverter.DoubleToInt64Bits(f), dst, di);
  121. }
  122. public static float Dec_floatle(byte[] src, int si)
  123. {
  124. return (float)BitConverter.Int64BitsToDouble(Dec_uint32le(src, si));
  125. }
  126. public static float Dec_floatbe(byte[] src, int si)
  127. {
  128. return (float)BitConverter.Int64BitsToDouble(Dec_uint32be(src, si));
  129. }
  130. public static int Enc_doublele(double d, byte[] dst, int di)
  131. {
  132. return Enc_uint64le(BitConverter.DoubleToInt64Bits(d), dst, di);
  133. }
  134. public static int Enc_doublebe(double d, byte[] dst, int di)
  135. {
  136. return Enc_uint64be(BitConverter.DoubleToInt64Bits(d), dst, di);
  137. }
  138. public static double Dec_doublele(byte[] src, int si)
  139. {
  140. return BitConverter.Int64BitsToDouble(Dec_uint64le(src, si));
  141. }
  142. public static double Dec_doublebe(byte[] src, int si)
  143. {
  144. return BitConverter.Int64BitsToDouble(Dec_uint64be(src, si));
  145. }
  146. public static int Enc_time(DateTime date, byte[] dst, int di, int enc)
  147. {
  148. long t;
  149. switch (enc)
  150. {
  151. case Time1970Sec32Be:
  152. {
  153. return Enc_uint32be((int)(date.GetTime() / 1000L), dst, di);
  154. }
  155. case Time1970Sec32Le:
  156. {
  157. return Enc_uint32le((int)(date.GetTime() / 1000L), dst, di);
  158. }
  159. case Time1904Sec32Be:
  160. {
  161. return Enc_uint32be((int)((date.GetTime() / 1000L
  162. + SecBetweeen1904And1970)
  163. & unchecked((int)(0xFFFFFFFF))),
  164. dst,
  165. di);
  166. }
  167. case Time1904Sec32Le:
  168. {
  169. return Enc_uint32le((int)((date.GetTime() / 1000L
  170. + SecBetweeen1904And1970)
  171. & unchecked((int)(0xFFFFFFFF))),
  172. dst,
  173. di);
  174. }
  175. case Time1601Nanos64Be:
  176. {
  177. t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
  178. return Enc_uint64be(t, dst, di);
  179. }
  180. case Time1601Nanos64Le:
  181. {
  182. t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
  183. return Enc_uint64le(t, dst, di);
  184. }
  185. case Time1970Millis64Be:
  186. {
  187. return Enc_uint64be(date.GetTime(), dst, di);
  188. }
  189. case Time1970Millis64Le:
  190. {
  191. return Enc_uint64le(date.GetTime(), dst, di);
  192. }
  193. default:
  194. {
  195. throw new ArgumentException("Unsupported time encoding");
  196. }
  197. }
  198. }
  199. public static DateTime Dec_time(byte[] src, int si, int enc)
  200. {
  201. long t;
  202. switch (enc)
  203. {
  204. case Time1970Sec32Be:
  205. {
  206. return Sharpen.Extensions.CreateDate(Dec_uint32be(src, si) * 1000L);
  207. }
  208. case Time1970Sec32Le:
  209. {
  210. return Sharpen.Extensions.CreateDate(Dec_uint32le(src, si) * 1000L);
  211. }
  212. case Time1904Sec32Be:
  213. {
  214. return Sharpen.Extensions.CreateDate( ( (Dec_uint32be(src, si)
  215. & unchecked(0xFFFFFFFFL))
  216. - SecBetweeen1904And1970)
  217. * 1000L );
  218. }
  219. case Time1904Sec32Le:
  220. {
  221. return Sharpen.Extensions.CreateDate( ( (Dec_uint32le(src, si)
  222. & unchecked(0xFFFFFFFFL))
  223. - SecBetweeen1904And1970)
  224. * 1000L);
  225. }
  226. case Time1601Nanos64Be:
  227. {
  228. t = Dec_uint64be(src, si);
  229. return Sharpen.Extensions.CreateDate(t / 10000L
  230. - MillisecondsBetween1970And1601);
  231. }
  232. case Time1601Nanos64Le:
  233. {
  234. t = Dec_uint64le(src, si);
  235. return Sharpen.Extensions.CreateDate(t / 10000L
  236. - MillisecondsBetween1970And1601);
  237. }
  238. case Time1970Millis64Be:
  239. {
  240. return Sharpen.Extensions.CreateDate(Dec_uint64be(src, si));
  241. }
  242. case Time1970Millis64Le:
  243. {
  244. return Sharpen.Extensions.CreateDate(Dec_uint64le(src, si));
  245. }
  246. default:
  247. {
  248. throw new ArgumentException("Unsupported time encoding");
  249. }
  250. }
  251. }
  252. /// <exception cref="System.IO.IOException"></exception>
  253. public static int Enc_utf8(string str, byte[] dst, int di, int dlim)
  254. {
  255. int start = di;
  256. int ch;
  257. int strlen = str.Length;
  258. for (int i = 0; di < dlim && i < strlen; i++)
  259. {
  260. ch = str[i];
  261. if ((ch >= unchecked(0x0001)) && (ch <= unchecked(0x007F)))
  262. {
  263. dst[di++] = unchecked((byte)ch);
  264. }
  265. else
  266. {
  267. if (ch > unchecked(0x07FF))
  268. {
  269. if ((dlim - di) < 3)
  270. {
  271. break;
  272. }
  273. dst[di++] = unchecked((byte)(unchecked(0xE0) | ((ch >> 12) & unchecked(0x0F))));
  274. dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 6) & unchecked(0x3F))));
  275. dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
  276. }
  277. else
  278. {
  279. if ((dlim - di) < 2)
  280. {
  281. break;
  282. }
  283. dst[di++] = unchecked((byte)(unchecked(0xC0) | ((ch >> 6) & unchecked(0x1F))));
  284. dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
  285. }
  286. }
  287. }
  288. return di - start;
  289. }
  290. /// <exception cref="System.IO.IOException"></exception>
  291. public static string Dec_utf8(byte[] src, int si, int slim)
  292. {
  293. char[] uni = new char[slim - si];
  294. int ui;
  295. int ch;
  296. for (ui = 0; si < slim && (ch = src[si++] & unchecked(0xFF)) != 0; ui++)
  297. {
  298. if (ch < unchecked(0x80))
  299. {
  300. uni[ui] = (char)ch;
  301. }
  302. else
  303. {
  304. if ((ch & unchecked(0xE0)) == unchecked(0xC0))
  305. {
  306. if ((slim - si) < 2)
  307. {
  308. break;
  309. }
  310. uni[ui] = (char)((ch & unchecked(0x1F)) << 6);
  311. ch = src[si++] & unchecked(0xFF);
  312. uni[ui] |= (char)((char)ch & unchecked(0x3F));
  313. if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
  314. 0x80))
  315. {
  316. throw new IOException("Invalid UTF-8 sequence");
  317. }
  318. }
  319. else
  320. {
  321. if ((ch & unchecked(0xF0)) == unchecked(0xE0))
  322. {
  323. if ((slim - si) < 3)
  324. {
  325. break;
  326. }
  327. uni[ui] = (char)((ch & unchecked(0x0F)) << 12);
  328. ch = src[si++] & unchecked(0xFF);
  329. if ((ch & unchecked(0xC0)) != unchecked(0x80))
  330. {
  331. throw new IOException("Invalid UTF-8 sequence");
  332. }
  333. uni[ui] |= (char)((char)(ch & unchecked(0x3F)) << 6);
  334. ch = src[si++] & unchecked(0xFF);
  335. uni[ui] |= (char)((char)ch & unchecked(0x3F));
  336. if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
  337. 0x800))
  338. {
  339. throw new IOException("Invalid UTF-8 sequence");
  340. }
  341. }
  342. else
  343. {
  344. throw new IOException("Unsupported UTF-8 sequence");
  345. }
  346. }
  347. }
  348. }
  349. return new string(uni, 0, ui);
  350. }
  351. /// <exception cref="System.IO.IOException"></exception>
  352. public static string Dec_ucs2le(byte[] src, int si, int slim, char[] buf)
  353. {
  354. int bi;
  355. for (bi = 0; (si + 1) < slim; bi++, si += 2)
  356. {
  357. buf[bi] = (char)Dec_uint16le(src, si);
  358. if (buf[bi] == '\0')
  359. {
  360. break;
  361. }
  362. }
  363. return new string(buf, 0, bi);
  364. }
  365. }
  366. }