Encdec.cs 11 KB

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