You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1030 lines
38 KiB

  1. using Apewer.Internals;
  2. using Externals.Compression.Checksums;
  3. using Externals.Compression.Zip;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. namespace Apewer
  12. {
  13. /// <summary>二进制。</summary>
  14. public class BinaryUtility
  15. {
  16. /// <summary>空字节数组,每次获取都将创建新的引用。</summary>
  17. public static byte[] EmptyBytes { get => new byte[0]; }
  18. #region ASCII
  19. /// <summary>CRLF。</summary>
  20. public static byte[] CRLF { get => new byte[] { 13, 10 }; }
  21. /// <summary>0x00 空字符。</summary>
  22. public static byte[] NULL { get => new byte[] { 0x00 }; }
  23. #endregion
  24. #region Bytes Array
  25. /// <summary>克隆字节数组。当源为 NULL 时获取零元素字节数组。</summary>
  26. public static byte[] Clone(byte[] bytes)
  27. {
  28. if (bytes == null || bytes.LongLength < 0L) return EmptyBytes;
  29. var result = new byte[bytes.LongLength];
  30. bytes.CopyTo(result, 0L);
  31. return result;
  32. }
  33. /// <summary>创建数组,元素值为零。</summary>
  34. public static byte[] ZeroArray(int length = 0)
  35. {
  36. if (length < 1) return new byte[0];
  37. var array = new byte[length];
  38. for (int i = 0; i < length; i++) array[i] = 0;
  39. return array;
  40. }
  41. /// <summary>所有字节取反。</summary>
  42. public static byte[] Adverse(byte[] bytes)
  43. {
  44. if (bytes == null || bytes.LongLength < 1L) return EmptyBytes;
  45. var adverse = new byte[bytes.LongLength];
  46. for (var i = 0L; i < bytes.LongLength; i++) adverse[i] = Convert.ToByte(255 - bytes[i]);
  47. return adverse;
  48. }
  49. /// <summary>确定此字节数组实例的开头是否与指定的字节数组匹配。</summary>
  50. public static bool StartsWith(byte[] bytes, params byte[] head)
  51. {
  52. var data = bytes;
  53. if (data == null) return false;
  54. if (head == null) return false;
  55. var datalength = data.LongLength;
  56. var headlength = head.LongLength;
  57. if (datalength < headlength) return false;
  58. if (headlength < 1L) return true;
  59. for (long i = 0; i < head.LongLength; i++)
  60. {
  61. if (data[i] != head[i]) return false;
  62. }
  63. return true;
  64. }
  65. /// <summary>确定此字节数组实例的结尾是否与指定的字节数组匹配。</summary>
  66. public static bool EndsWith(byte[] bytes, params byte[] end)
  67. {
  68. if (bytes == null) return false;
  69. if (end == null) return false;
  70. var dataLength = bytes.LongLength;
  71. var endLength = end.LongLength;
  72. if (dataLength < endLength) return false;
  73. if (endLength < 1L) return true;
  74. for (long i = 0; i < endLength; i++)
  75. {
  76. var dataindex = dataLength - i - 1;
  77. var headindex = endLength - i - 1;
  78. if (bytes[dataindex] != end[headindex]) return false;
  79. }
  80. return true;
  81. }
  82. /// <summary>合并字节数组。</summary>
  83. public static byte[] Merge(IEnumerable<byte[]> array)
  84. {
  85. if (array == null) return EmptyBytes;
  86. var total = 0L;
  87. foreach (var bytes in array)
  88. {
  89. if (bytes == null) continue;
  90. total += bytes.LongLength;
  91. }
  92. var result = new byte[total];
  93. var offset = 0L;
  94. if (total > 0)
  95. {
  96. foreach (var bytes in array)
  97. {
  98. if (bytes == null) continue;
  99. var length = bytes.LongLength;
  100. if (length < 1L) continue;
  101. Array.Copy(bytes, 0L, result, offset, length);
  102. offset += length;
  103. }
  104. }
  105. return result;
  106. }
  107. /// <summary>合并字节数组。</summary>
  108. public static byte[] Merge(params byte[][] array) => Merge(array as IEnumerable<byte[]>);
  109. /// <summary>为字节数组增加字节。</summary>
  110. public static byte[] Append(byte[] head, params byte[] bytes) => Merge(head, bytes);
  111. /// <summary>为文本数据添加 BOM 字节,若已存在则忽略。</summary>
  112. public static byte[] AddTextBom(params byte[] bytes)
  113. {
  114. var bom = new byte[] { 0xEF, 0xBB, 0xBF };
  115. if (bytes == null || bytes.LongLength < 1L) return bom;
  116. var hasBom = (bytes.Length >= 3) && (bytes[0] == 0xEF) && (bytes[1] == 0xBB) && (bytes[2] == 0xBF);
  117. return hasBom ? Merge(bytes) : Merge(bom, bytes);
  118. }
  119. /// <summary>去除文本数据的 BOM 字节,若不存在则忽略。</summary>
  120. public static byte[] WipeTextBom(byte[] bytes)
  121. {
  122. if (bytes == null) return EmptyBytes;
  123. var hasBom = (bytes.Length >= 3) && (bytes[0] == 0xEF) && (bytes[1] == 0xBB) && (bytes[2] == 0xBF);
  124. var offset = hasBom ? 3 : 0;
  125. var length = bytes.Length - offset;
  126. var wiped = new byte[length];
  127. if (length > 0) Array.Copy(bytes, offset, wiped, 0, length);
  128. return wiped;
  129. }
  130. #endregion
  131. #region Text
  132. /// <summary>将字节数组转换为十六进制文本。</summary>
  133. public static string ToHex(params byte[] bytes)
  134. {
  135. int length = bytes.Length;
  136. if (length > 0)
  137. {
  138. var sb = new StringBuilder();
  139. for (int i = 0; i < length; i++)
  140. {
  141. sb.Append(Constant.HexCollection[bytes[i] / 16]);
  142. sb.Append(Constant.HexCollection[bytes[i] % 16]);
  143. }
  144. return sb.ToString();
  145. }
  146. return "";
  147. }
  148. /// <summary>将十六进制文本转换为字节数组。</summary>
  149. public static byte[] FromHex(string hex)
  150. {
  151. if (string.IsNullOrEmpty(hex) || hex.Length < 2) return EmptyBytes;
  152. if (hex.Length % 2 != 0) return EmptyBytes;
  153. var lower = hex.ToLower().ToCharArray();
  154. var half = lower.Length / 2;
  155. var bytes = new byte[half];
  156. for (var i = 0; i < half; i++)
  157. {
  158. var offset = i * 2;
  159. var h = Constant.HexCollection.IndexOf(lower[offset]);
  160. var l = Constant.HexCollection.IndexOf(lower[offset + 1]);
  161. if (h < 0 || l < 0) return EmptyBytes;
  162. bytes[i] = Convert.ToByte((h * 16) + l);
  163. }
  164. return bytes;
  165. }
  166. /// <summary>将字节数组格式化为大写十六进制字符串。</summary>
  167. public static string ToX2(params byte[] bytes)
  168. {
  169. if (bytes == null) return "";
  170. var sb = new StringBuilder();
  171. for (int i = 0; i < bytes.Length; i++)
  172. {
  173. sb.Append(bytes[i].ToString("X2"));
  174. }
  175. return sb.ToString();
  176. }
  177. /// <summary>Byte[] -> Base64</summary>
  178. public static string ToBase64(params byte[] bytes)
  179. {
  180. if (bytes.Length < 1) return Constant.EmptyString;
  181. try { return Convert.ToBase64String(bytes); }
  182. catch { return Constant.EmptyString; }
  183. }
  184. /// <summary>Base64 -> Byte[]</summary>
  185. public static byte[] FromBase64(string base64)
  186. {
  187. if (string.IsNullOrEmpty(base64)) return EmptyBytes;
  188. try { return Convert.FromBase64String(base64); }
  189. catch { return EmptyBytes; }
  190. }
  191. /// <summary>转换字节数组为文本,默认使用 UTF-8 代码页。</summary>
  192. public static string ToText(byte[] bytes, Encoding encoding = null)
  193. {
  194. if (bytes.Length < 1) return Constant.EmptyString;
  195. try { return (encoding ?? Encoding.UTF8).GetString(bytes); }
  196. catch { return Constant.EmptyString; }
  197. }
  198. /// <summary>转换文本为字节数组,默认使用 UTF-8 代码页。</summary>
  199. public static byte[] FromText(string text, Encoding encoding = null)
  200. {
  201. if (string.IsNullOrEmpty(text)) return EmptyBytes;
  202. try { return (encoding ?? Encoding.UTF8).GetBytes(text); }
  203. catch { return EmptyBytes; }
  204. }
  205. /// <summary>检查字节数组包含 UTF-8 BOM 头。</summary>
  206. public static bool ContainsBOM(byte[] bytes)
  207. {
  208. if (bytes == null) return false;
  209. if (bytes.LongLength < 3L) return false;
  210. return bytes[0L] == 0xEF && bytes[1L] == 0xBB && bytes[2L] == 0xBF;
  211. }
  212. /// <summary>检查字节数组是 UTF-8 文本。可指定检查的最大字节长度。</summary>
  213. /// <param name="bytes">要检查的字节数组。</param>
  214. /// <param name="offset">已检查的偏移量。</param>
  215. /// <param name="checkLength">检查的最大字节长度。</param>
  216. public static bool IsUTF8(byte[] bytes, Class<int> offset, int checkLength = 1048576)
  217. {
  218. return IsUTF8(bytes, offset, checkLength);
  219. }
  220. /// <summary>检查字节数组是 UTF-8 文本,默认最多检测 1MB 数据。</summary>
  221. /// <param name="bytes">要检查的字节数组。</param>
  222. /// <param name="checkLength">检查的最大字节长度,指定为 0 将不限制检查长度。</param>
  223. /// <param name="offset">已检查的偏移量,用于调试。</param>
  224. public static bool IsUTF8(byte[] bytes, int checkLength = 1048576, Class<int> offset = null)
  225. {
  226. // UTF8在Unicode的基础上制定了这样一套规则:
  227. // 1.对于单字节字符,比特位的最高位为0;
  228. // 2.对于多字节字符,第一个字节的比特位中,最高位有n个1,剩下的n - 1个字节的比特位中,最高位都是10。
  229. // 好了,我知道你一定看不懂,那就先来看看下面例子后,再去看上面定义吧。
  230. // 比如一个字符(“A”),它在UTF8中的编码为(用二进制表示):01000001。由于比特位的最高位是0,表示它是单字节,它只需要1个字节就可以表示。
  231. // 再比如一个字符(“判”),它在UTF8中的编码为(用二进制表示):11100101 10001000 10100100。由于在第一个字节中,比特位最高位有3个1,说明这个字符总共需要3个字节来表示,且后3 - 1 = 2位字节中,比特位的最高位为10。
  232. if (bytes == null) return false;
  233. var length = bytes.LongLength;
  234. // 检查 BOM 头。
  235. if (ContainsBOM(bytes)) return true;
  236. var hasOffset = offset != null;
  237. var append = 0;
  238. if (hasOffset) offset.Value = 0;
  239. for (int i = 0; i < length; i++)
  240. {
  241. if (checkLength > 0 && i >= checkLength) break;
  242. var b = bytes[i];
  243. if (hasOffset) offset.Value = i;
  244. // 追加字节最高位为 0。
  245. if (append > 0)
  246. {
  247. if (b >> 6 != 2) return false;
  248. append -= 1;
  249. continue;
  250. }
  251. // ASCII 字符。
  252. if (b < 128) continue;
  253. // 2 字节 UTF-8。
  254. if (b >= 0xC0 && b <= 0xDF)
  255. {
  256. append = 1;
  257. continue;
  258. }
  259. // 3 字节 UTF-8 字符。
  260. if (b >= 0xE0 && b <= 0xEF)
  261. {
  262. append = 2;
  263. continue;
  264. }
  265. // 4 字节 UTF-8 字符。
  266. if (b >= 0xF0 && b <= 0xF7)
  267. {
  268. append = 3;
  269. continue;
  270. }
  271. // 5 字节 UTF-8 字符。
  272. if (b >= 0xF8 && b <= 0xFB)
  273. {
  274. append = 4;
  275. continue;
  276. }
  277. // 6 字节 UTF-8 字符。
  278. if (b >= 0xFC && b <= 0xFD)
  279. {
  280. append = 5;
  281. continue;
  282. }
  283. // 未知字节,非 UTF-8 定义。
  284. return false;
  285. }
  286. return true;
  287. }
  288. #endregion
  289. #region 压缩、解压。
  290. /// <summary>对数据进行 GZip 压缩。</summary>
  291. public static byte[] ToGzip(byte[] plain)
  292. {
  293. if (plain == null || plain.Length == 0) return EmptyBytes;
  294. byte[] result;
  295. using (var output = new MemoryStream())
  296. {
  297. using (var zip = new GZipStream(output, CompressionMode.Compress, true))
  298. {
  299. zip.Write(plain, 0, plain.Length);
  300. }
  301. result = output.ToArray();
  302. }
  303. return result;
  304. }
  305. /// <summary>对数据进行 GZip 解压。</summary>
  306. public static byte[] FromGzip(byte[] gzip)
  307. {
  308. if (gzip == null || gzip.Length == 0) return EmptyBytes;
  309. byte[] result;
  310. using (var input = new MemoryStream(gzip))
  311. {
  312. input.Position = 0;
  313. using (var output = new MemoryStream())
  314. {
  315. using (var zip = new GZipStream(input, CompressionMode.Decompress, true))
  316. {
  317. Read(zip, output, null, 8192);
  318. result = output.ToArray();
  319. }
  320. }
  321. }
  322. return result;
  323. }
  324. /// <summary>压缩字典为 ZIP 文件。</summary>
  325. /// <param name="files">由文件名和文件内容组成的字典。</param>
  326. /// <param name="target">要输出的 ZIP 流。</param>
  327. public static Exception ToZip(Dictionary<string, byte[]> files, Stream target)
  328. {
  329. var zip = null as ZipOutputStream;
  330. try
  331. {
  332. if (files == null) return new ArgumentNullException();
  333. if (target == null) return new ArgumentNullException();
  334. if (!target.CanWrite) return new NotSupportedException();
  335. zip = new ZipOutputStream(target);
  336. zip.SetLevel(1);
  337. foreach (var file in files)
  338. {
  339. var crc = new Crc32();
  340. crc.Reset();
  341. crc.Update(file.Value);
  342. var zipentry = new ZipEntry(file.Key);
  343. zipentry.CompressionMethod = CompressionMethod.Deflated;
  344. //vzipentry.Size = vfile.Value.LongLength;
  345. zipentry.Crc = crc.Value;
  346. zipentry.IsUnicodeText = true;
  347. zip.PutNextEntry(zipentry);
  348. zip.Write(file.Value, 0, file.Value.Length);
  349. zip.CloseEntry();
  350. }
  351. zip.IsStreamOwner = false;
  352. zip.Finish();
  353. zip.Flush();
  354. zip.Close();
  355. return null;
  356. }
  357. catch (Exception ex)
  358. {
  359. RuntimeUtility.Dispose(zip);
  360. return ex;
  361. }
  362. }
  363. /// <summary>压缩到 ZIP 流。</summary>
  364. /// <param name="names">ZIP 内的文件名。</param>
  365. /// <param name="target">要输出的 ZIP 流。</param>
  366. /// <param name="modifildGetter">获取文件的修改时间。</param>
  367. /// <param name="disposeFiles">释放已写入 ZIP 的输入流。</param>
  368. public static Exception ToZip(IEnumerable<string> names, Stream target, Func<string, Stream> inputGetter, Func<string, DateTime> modifildGetter = null, bool disposeFiles = false)
  369. {
  370. var zip = null as ZipOutputStream;
  371. try
  372. {
  373. if (names == null) return new ArgumentNullException("names");
  374. if (target == null) return new ArgumentNullException("target");
  375. if (!target.CanWrite) return new ArgumentNullException("target");
  376. zip = new ZipOutputStream(target);
  377. zip.SetLevel(1);
  378. foreach (var name in names)
  379. {
  380. if (string.IsNullOrEmpty(name)) continue;
  381. // stream
  382. var capacity = 1024;
  383. var input = inputGetter == null ? null : inputGetter(name);
  384. if (input != null)
  385. {
  386. if (!input.CanSeek || !input.CanRead)
  387. {
  388. if (disposeFiles) RuntimeUtility.Dispose(input);
  389. return new NotSupportedException("获取到的输入流不支持 Seek 或 Read。");
  390. }
  391. }
  392. var crc = new Crc32();
  393. crc.Reset();
  394. if (input != null)
  395. {
  396. input.ResetPosition();
  397. while (true)
  398. {
  399. var count = 0;
  400. var buffer = new byte[capacity];
  401. count = input.Read(buffer, 0, buffer.Length);
  402. if (count == 0) break;
  403. crc.Update(buffer, 0, count);
  404. }
  405. }
  406. var entry = new ZipEntry(name);
  407. entry.CompressionMethod = CompressionMethod.Deflated;
  408. //vzipentry.Size = vfile.Value.LongLength;
  409. entry.Crc = crc.Value;
  410. entry.IsUnicodeText = true;
  411. if (modifildGetter != null) entry.DateTime = modifildGetter(name);
  412. zip.PutNextEntry(entry);
  413. if (input != null)
  414. {
  415. input.ResetPosition();
  416. while (true)
  417. {
  418. var count = 0;
  419. var buffer = new byte[capacity];
  420. count = input.Read(buffer, 0, buffer.Length);
  421. if (count == 0) break;
  422. zip.Write(buffer, 0, count);
  423. }
  424. }
  425. zip.CloseEntry();
  426. if (disposeFiles) RuntimeUtility.Dispose(input);
  427. }
  428. zip.IsStreamOwner = false;
  429. zip.Finish();
  430. zip.Flush();
  431. zip.Close();
  432. return null;
  433. }
  434. catch (Exception ex)
  435. {
  436. RuntimeUtility.Dispose(zip);
  437. return ex;
  438. }
  439. }
  440. /// <summary>压缩字典为 ZIP 包。</summary>
  441. /// <param name="files">由文件名和文件内容组成的字典。</param>
  442. public static byte[] ToZip(Dictionary<string, byte[]> files)
  443. {
  444. if (files == null) return null;
  445. var output = new MemoryStream();
  446. var input = null as Stream;
  447. var ex = ToZip(files.Keys, output, (name) =>
  448. {
  449. RuntimeUtility.Dispose(input);
  450. if (name.IsEmpty()) return null;
  451. var bytes = files[name];
  452. if (bytes == null || bytes.LongLength < 1L) return null;
  453. input = new MemoryStream();
  454. Write(input, bytes);
  455. return input;
  456. }, null, true);
  457. RuntimeUtility.Dispose(input);
  458. var result = output.ToArray();
  459. RuntimeUtility.Dispose(result);
  460. return result;
  461. }
  462. /// <summary>解压 ZIP 文件。</summary>
  463. public static Exception FromZip(Stream input, ZipOnFile onFile, ZipOnDirectory onDirectory = null, bool disposeOutput = false)
  464. {
  465. const int BufferCapacity = 1024;
  466. var zip = null as ZipInputStream;
  467. try
  468. {
  469. if (input == null) return new ArgumentNullException("input");
  470. if (!input.CanRead) return new NotSupportedException();
  471. if (onFile == null) return new ArgumentNullException("extraction");
  472. zip = new ZipInputStream(input);
  473. while (true)
  474. {
  475. var entry = zip.GetNextEntry();
  476. if (entry == null) break;
  477. var name = entry.Name;
  478. var size = entry.Size;
  479. var modified = entry.DateTime;
  480. if (entry.IsFile)
  481. {
  482. var output = null as Stream;
  483. try
  484. {
  485. output = onFile(name, size, modified);
  486. if (output == null) continue;
  487. if (!output.CanWrite)
  488. {
  489. RuntimeUtility.Dispose(output);
  490. continue;
  491. }
  492. var writed = 0L;
  493. while (true)
  494. {
  495. var buffer = new byte[BufferCapacity];
  496. var count = zip.Read(buffer, 0, BufferCapacity);
  497. writed += count;
  498. if (count < 1) break;
  499. output.Write(buffer, 0, count);
  500. }
  501. if (disposeOutput) RuntimeUtility.Dispose(output, true);
  502. }
  503. catch (Exception ex)
  504. {
  505. if (disposeOutput) RuntimeUtility.Dispose(output, true);
  506. RuntimeUtility.Dispose(zip);
  507. return ex;
  508. }
  509. }
  510. if (onDirectory != null && entry.IsDirectory)
  511. {
  512. try
  513. {
  514. onDirectory(name, modified);
  515. }
  516. catch (Exception ex)
  517. {
  518. RuntimeUtility.Dispose(zip);
  519. return ex;
  520. }
  521. }
  522. }
  523. zip.Dispose();
  524. return null;
  525. }
  526. catch (Exception ex)
  527. {
  528. RuntimeUtility.Dispose(zip);
  529. return ex;
  530. }
  531. }
  532. /// <summary>解压 .ZIP 文件为字典。</summary>
  533. public static Dictionary<string, byte[]> FromZip(byte[] zip)
  534. {
  535. var result = new Dictionary<string, byte[]>();
  536. if (zip == null) return result;
  537. if (zip.LongLength < 1) return result;
  538. var packagememory = new System.IO.MemoryStream(zip);
  539. try
  540. {
  541. var zipstream = new ZipInputStream(packagememory);
  542. while (true)
  543. {
  544. var entry = zipstream.GetNextEntry();
  545. if (entry == null) break;
  546. if (entry.IsFile)
  547. {
  548. var cellname = entry.Name;
  549. var celldata = new byte[0];
  550. {
  551. var cellstream = new System.IO.MemoryStream();
  552. while (true)
  553. {
  554. var blockdata = new byte[1024];
  555. var blockread = zipstream.Read(blockdata, 0, 1024);
  556. if (blockread < 1) break;
  557. cellstream.Write(blockdata, 0, blockread);
  558. }
  559. celldata = cellstream.ToArray();
  560. cellstream.Dispose();
  561. }
  562. if (result.ContainsKey(cellname)) result[cellname] = celldata;
  563. else result.Add(cellname, celldata);
  564. }
  565. }
  566. zipstream.Dispose();
  567. }
  568. catch (Exception ex)
  569. {
  570. Debug.WriteLine(ex.ToString());
  571. }
  572. packagememory.Dispose();
  573. return result;
  574. }
  575. #endregion
  576. #region Stream
  577. /// <summary>关闭,并释放流。</summary>
  578. public static void Dispose(Stream stream, bool flush = false, bool close = true)
  579. {
  580. if (stream != null)
  581. {
  582. try { if (flush) stream.Flush(); } catch { }
  583. try { if (close) stream.Close(); } catch { }
  584. try { stream.Dispose(); } catch { }
  585. }
  586. }
  587. /// <summary>关闭,并释放流。</summary>
  588. public static void Dispose(IEnumerable<Stream> streams, bool flush = false, bool close = true)
  589. {
  590. if (streams != null)
  591. {
  592. foreach (var stream in streams) Dispose(stream, flush, close);
  593. }
  594. }
  595. /// <summary>重置流的位置到开始位置。</summary>
  596. public static bool ResetPosition(Stream stream)
  597. {
  598. if (stream == null) return false;
  599. try
  600. {
  601. stream.Position = 0;
  602. if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin);
  603. return true;
  604. }
  605. catch
  606. {
  607. return false;
  608. }
  609. }
  610. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的总字节数。</summary>
  611. /// <param name="source">要读取的源流。</param>
  612. /// <param name="destination">要写入的目标流。</param>
  613. /// <param name="progress">已写入的字节数,返回 TRUE 继续读取,返回 FALSE 中断读取。</param>
  614. /// <param name="buffer">缓冲区大小,最小值为 1。</param>
  615. /// <returns>已写入的字节数。</returns>
  616. public static long Read(Stream source, Stream destination, Func<long, bool> progress = null, int buffer = 4096)
  617. {
  618. if (source == null || !source.CanRead) return 0;
  619. if (destination == null || !destination.CanWrite) return 0;
  620. var limit = buffer < 1 ? 1 : buffer;
  621. var total = 0L;
  622. var count = 0;
  623. var callback = progress == null ? false : true;
  624. while (true)
  625. {
  626. count = 0;
  627. var temp = new byte[limit];
  628. try
  629. {
  630. count = source.Read(temp, 0, limit);
  631. if (count < 1) break;
  632. }
  633. catch { break; }
  634. try
  635. {
  636. destination.Write(temp, 0, count);
  637. }
  638. catch { break; }
  639. total += count;
  640. if (callback)
  641. {
  642. var @continue = progress(total);
  643. if (!@continue) break;
  644. }
  645. }
  646. return total;
  647. }
  648. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的总字节数。</summary>
  649. /// <param name="source">要读取的源流。</param>
  650. /// <param name="destination">要写入的目标流。</param>
  651. /// <param name="progress">已写入的字节数,返回 TRUE 继续读取,返回 FALSE 中断读取。</param>
  652. /// <param name="buffer">缓冲区大小,最小值为 1。</param>
  653. /// <returns>已写入的字节数。</returns>
  654. public static long Read(Stream source, Stream destination, Action<long> progress, int buffer = 1024)
  655. {
  656. return Read(source, destination, (x) => { progress?.Invoke(x); return true; }, buffer);
  657. }
  658. /// <summary>读取源流中的数据。</summary>
  659. /// <param name="source">源流。</param>
  660. /// <param name="buffer">缓冲区大小,最小值为 1。</param>
  661. /// <param name="dispose">读取结束后释放源流。</param>
  662. public static byte[] Read(Stream source, int buffer = 1024, bool dispose = false)
  663. {
  664. var result = null as byte[];
  665. using (var memory = new MemoryStream())
  666. {
  667. Read(source, memory, null, buffer);
  668. result = memory.ToArray();
  669. }
  670. if (dispose) Dispose(source);
  671. return result;
  672. }
  673. /// <summary>读取源流中的数据。</summary>
  674. /// <param name="source">源流。</param>
  675. /// <param name="dispose">读取结束后释放源流。</param>
  676. public static byte[] Read(Stream source, bool dispose) => Read(source, 1024, dispose);
  677. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的总字节数。</summary>
  678. /// <param name="source">要读取的源流。</param>
  679. /// <param name="destination">要写入的目标流。</param>
  680. /// <param name="writed">已写入的字节数,返回 TRUE 继续读取,返回 FALSE 中断读取。</param>
  681. /// <param name="buffer">缓冲区大小,最小值为 1。</param>
  682. /// <returns>已写入的字节数。</returns>
  683. public static long Read(IEnumerable<Stream> sources, Stream destination, Func<long, bool> writed = null, int buffer = 1024)
  684. {
  685. var total = 0L;
  686. if (sources != null)
  687. {
  688. if (writed == null)
  689. {
  690. foreach (var source in sources) total += Read(source, destination, null, buffer);
  691. }
  692. else
  693. {
  694. foreach (var source in sources)
  695. {
  696. Read(source, destination, (x) =>
  697. {
  698. total += x;
  699. return writed(total);
  700. }, buffer);
  701. }
  702. }
  703. }
  704. return total;
  705. }
  706. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的总字节数。</summary>
  707. /// <param name="source">要读取的源流。</param>
  708. /// <param name="destination">要写入的目标流。</param>
  709. /// <param name="writed">已写入的字节数,返回 TRUE 继续读取,返回 FALSE 中断读取。</param>
  710. /// <param name="buffer">缓冲区大小,最小值为 1。</param>
  711. /// <returns>已写入的字节数。</returns>
  712. public static long Read(IEnumerable<Stream> sources, Stream destination, Action<long> writed, int buffer = 1024)
  713. {
  714. return Read(sources, destination, (x) => { writed?.Invoke(x); return true; }, buffer);
  715. }
  716. /// <summary>向目标流写入数据,最多可写入 2147483648 字节。</summary>
  717. public static int Write(Stream destination, byte[] bytes, Action<int> writed, int buffer = 1048576)
  718. {
  719. if (destination == null || !destination.CanWrite) return 0;
  720. if (bytes == null || bytes.Length < 1 || bytes.LongLength > int.MaxValue) return 0;
  721. var limit = buffer < 1 ? 1 : buffer;
  722. var total = 0;
  723. try
  724. {
  725. var length = bytes.Length;
  726. while (total < length)
  727. {
  728. var block = length - total;
  729. if (block > limit) block = limit;
  730. destination.Write(bytes, total, block);
  731. total += block;
  732. writed?.Invoke(total);
  733. }
  734. }
  735. catch (Exception ex)
  736. {
  737. Logger.Internals.Exception($"{nameof(BinaryUtility)}.{nameof(Write)}", ex);
  738. }
  739. return total;
  740. }
  741. /// <summary>向目标流写入数据,最多可写入 2147483647 字节(> 20 GB)。</summary>
  742. public static int Write(Stream destination, params byte[] bytes) => Write(destination, bytes, null);
  743. #endregion
  744. #region AES
  745. private static RijndaelManaged Aes256Provider(byte[] key)
  746. {
  747. var k = key ?? EmptyBytes; // AesFill(key);
  748. var p = new RijndaelManaged();
  749. p.Key = k;
  750. p.Mode = CipherMode.ECB;
  751. p.Padding = PaddingMode.PKCS7;
  752. return p;
  753. }
  754. /// <summary>对数据进行 AES 加密。</summary>
  755. public static byte[] Aes256Encrypt(byte[] bytes, byte[] key = null)
  756. {
  757. if (bytes == null) return EmptyBytes;
  758. if (bytes.Length == 0) return EmptyBytes;
  759. var rm = Aes256Provider(key);
  760. var result = new byte[0];
  761. var ct = rm.CreateEncryptor();
  762. try
  763. {
  764. result = ct.TransformFinalBlock(bytes, 0, bytes.Length);
  765. }
  766. catch { }
  767. return result;
  768. }
  769. /// <summary>对数据进行 AES 解密。</summary>
  770. public static byte[] Aes256Decrypt(byte[] cipher, byte[] key = null)
  771. {
  772. if (cipher == null) return EmptyBytes;
  773. if (cipher.Length == 0) return EmptyBytes;
  774. var rm = Aes256Provider(key);
  775. var result = new byte[0];
  776. var ct = rm.CreateDecryptor();
  777. try
  778. {
  779. result = ct.TransformFinalBlock(cipher, 0, cipher.Length);
  780. }
  781. catch { }
  782. ct.Dispose();
  783. return result;
  784. }
  785. #endregion
  786. #region Hash
  787. private static byte[] ComputeHash<T>(byte[] bytes) where T : HashAlgorithm, new()
  788. {
  789. if (bytes != null)
  790. {
  791. try
  792. {
  793. var algorithm = new T();
  794. var result = algorithm.ComputeHash(bytes);
  795. algorithm.Clear();
  796. #if !NET20
  797. algorithm.Dispose();
  798. #endif
  799. return result;
  800. }
  801. catch { }
  802. }
  803. return EmptyBytes;
  804. }
  805. private static byte[] ComputeHash<T>(Stream stream, bool dispose, Action<long> progress) where T : HashAlgorithm, new()
  806. {
  807. if (progress == null)
  808. {
  809. if (stream != null)
  810. {
  811. var result = EmptyBytes;
  812. try
  813. {
  814. var algorithm = new T();
  815. result = algorithm.ComputeHash(stream);
  816. algorithm.Clear();
  817. #if !NET20
  818. algorithm.Dispose();
  819. #endif
  820. }
  821. catch { }
  822. if (dispose) stream.Dispose();
  823. return result;
  824. }
  825. return EmptyBytes;
  826. }
  827. else
  828. {
  829. if (stream == null) return EmptyBytes;
  830. // 初始化。
  831. var validcallback = progress != null;
  832. var capacity = Constant.DefaultBufferCapacity;
  833. var buffer = new byte[capacity];
  834. var algorithm = new T();
  835. algorithm.Initialize();
  836. // 读取。
  837. var failed = false;
  838. while (true)
  839. {
  840. var read = 0;
  841. try { read = stream.Read(buffer, 0, capacity); }
  842. catch { failed = true; }
  843. if (read < capacity)
  844. {
  845. if (read < 1)
  846. {
  847. algorithm.TransformFinalBlock(new byte[0], 0, 0);
  848. }
  849. else
  850. {
  851. algorithm.TransformFinalBlock(buffer, 0, Convert.ToInt32(read));
  852. }
  853. break;
  854. }
  855. else
  856. {
  857. algorithm.TransformBlock(buffer, 0, Convert.ToInt32(read), buffer, 0);
  858. }
  859. }
  860. if (failed)
  861. {
  862. algorithm.Clear();
  863. #if !NET20
  864. algorithm.Dispose();
  865. #endif
  866. if (dispose) stream.Dispose();
  867. return EmptyBytes;
  868. }
  869. else
  870. {
  871. var result = algorithm.Hash;
  872. algorithm.Clear();
  873. #if !NET20
  874. algorithm.Dispose();
  875. #endif
  876. if (dispose) stream.Dispose();
  877. return result;
  878. }
  879. }
  880. }
  881. /// <summary>获取 MD5 值。</summary>
  882. public static byte[] MD5(params byte[] bytes) => ComputeHash<MD5CryptoServiceProvider>(bytes);
  883. /// <summary>获取 MD5 值。</summary>
  884. public static byte[] MD5(Stream stream, Action<long> progress = null) => ComputeHash<MD5CryptoServiceProvider>(stream, false, progress);
  885. /// <summary>获取 SHA1 值。</summary>
  886. public static byte[] SHA1(params byte[] bytes) => ComputeHash<SHA1CryptoServiceProvider>(bytes);
  887. /// <summary>获取 SHA1 值。</summary>
  888. public static byte[] SHA1(Stream stream, Action<long> progress = null) => ComputeHash<SHA1CryptoServiceProvider>(stream, false, progress);
  889. /// <summary>获取 SHA256 值。</summary>
  890. public static byte[] SHA256(params byte[] bytes) => ComputeHash<SHA256CryptoServiceProvider>(bytes);
  891. /// <summary>获取 SHA256 值。</summary>
  892. public static byte[] SHA256(Stream stream, Action<long> progress = null) => ComputeHash<SHA256CryptoServiceProvider>(stream, false, progress);
  893. /// <summary>获取 SHA512 值。</summary>
  894. public static byte[] SHA512(params byte[] bytes) => ComputeHash<SHA512CryptoServiceProvider>(bytes);
  895. /// <summary>获取 SHA512 值。</summary>
  896. public static byte[] SHA512(Stream stream, Action<long> progress = null) => ComputeHash<SHA512CryptoServiceProvider>(stream, false, progress);
  897. #endregion
  898. }
  899. }