DataConverter.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. //
  2. // Authors:
  3. // Miguel de Icaza (miguel@novell.com)
  4. //
  5. // See the following url for documentation:
  6. // http://www.mono-project.com/Mono_DataConvert
  7. //
  8. // Compilation Options:
  9. // MONO_DATACONVERTER_PUBLIC:
  10. // Makes the class public instead of the default internal.
  11. //
  12. // MONO_DATACONVERTER_STATIC_METHODS:
  13. // Exposes the public static methods.
  14. //
  15. // TODO:
  16. // Support for "DoubleWordsAreSwapped" for ARM devices
  17. //
  18. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  19. //
  20. // Permission is hereby granted, free of charge, to any person obtaining
  21. // a copy of this software and associated documentation files (the
  22. // "Software"), to deal in the Software without restriction, including
  23. // without limitation the rights to use, copy, modify, merge, publish,
  24. // distribute, sublicense, and/or sell copies of the Software, and to
  25. // permit persons to whom the Software is furnished to do so, subject to
  26. // the following conditions:
  27. //
  28. // The above copyright notice and this permission notice shall be
  29. // included in all copies or substantial portions of the Software.
  30. //
  31. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  34. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  35. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  36. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  37. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. //
  39. using System;
  40. #pragma warning disable 3021
  41. namespace SharpCompress.Converters
  42. {
  43. #if MONO_DATACONVERTER_PUBLIC
  44. public
  45. #endif
  46. internal unsafe abstract class DataConverter
  47. {
  48. // Disables the warning: CLS compliance checking will not be performed on
  49. // `XXXX' because it is not visible from outside this assembly
  50. #pragma warning disable 3019
  51. private static readonly DataConverter SwapConv = new SwapConverter();
  52. public static readonly bool IsLittleEndian = BitConverter.IsLittleEndian;
  53. public abstract double GetDouble(byte[] data, int index);
  54. public abstract float GetFloat(byte[] data, int index);
  55. public abstract long GetInt64(byte[] data, int index);
  56. public abstract int GetInt32(byte[] data, int index);
  57. public abstract short GetInt16(byte[] data, int index);
  58. [CLSCompliant(false)]
  59. public abstract uint GetUInt32(byte[] data, int index);
  60. [CLSCompliant(false)]
  61. public abstract ushort GetUInt16(byte[] data, int index);
  62. [CLSCompliant(false)]
  63. public abstract ulong GetUInt64(byte[] data, int index);
  64. public abstract void PutBytes(byte[] dest, int destIdx, double value);
  65. public abstract void PutBytes(byte[] dest, int destIdx, float value);
  66. public abstract void PutBytes(byte[] dest, int destIdx, int value);
  67. public abstract void PutBytes(byte[] dest, int destIdx, long value);
  68. public abstract void PutBytes(byte[] dest, int destIdx, short value);
  69. [CLSCompliant(false)]
  70. public abstract void PutBytes(byte[] dest, int destIdx, ushort value);
  71. [CLSCompliant(false)]
  72. public abstract void PutBytes(byte[] dest, int destIdx, uint value);
  73. [CLSCompliant(false)]
  74. public abstract void PutBytes(byte[] dest, int destIdx, ulong value);
  75. public byte[] GetBytes(double value)
  76. {
  77. byte[] ret = new byte[8];
  78. PutBytes(ret, 0, value);
  79. return ret;
  80. }
  81. public byte[] GetBytes(float value)
  82. {
  83. byte[] ret = new byte[4];
  84. PutBytes(ret, 0, value);
  85. return ret;
  86. }
  87. public byte[] GetBytes(int value)
  88. {
  89. byte[] ret = new byte[4];
  90. PutBytes(ret, 0, value);
  91. return ret;
  92. }
  93. public byte[] GetBytes(long value)
  94. {
  95. byte[] ret = new byte[8];
  96. PutBytes(ret, 0, value);
  97. return ret;
  98. }
  99. public byte[] GetBytes(short value)
  100. {
  101. byte[] ret = new byte[2];
  102. PutBytes(ret, 0, value);
  103. return ret;
  104. }
  105. [CLSCompliant(false)]
  106. public byte[] GetBytes(ushort value)
  107. {
  108. byte[] ret = new byte[2];
  109. PutBytes(ret, 0, value);
  110. return ret;
  111. }
  112. [CLSCompliant(false)]
  113. public byte[] GetBytes(uint value)
  114. {
  115. byte[] ret = new byte[4];
  116. PutBytes(ret, 0, value);
  117. return ret;
  118. }
  119. [CLSCompliant(false)]
  120. public byte[] GetBytes(ulong value)
  121. {
  122. byte[] ret = new byte[8];
  123. PutBytes(ret, 0, value);
  124. return ret;
  125. }
  126. static public DataConverter LittleEndian => BitConverter.IsLittleEndian ? Native : SwapConv;
  127. static public DataConverter BigEndian => BitConverter.IsLittleEndian ? SwapConv : Native;
  128. static public DataConverter Native { get; } = new CopyConverter();
  129. internal void Check(byte[] dest, int destIdx, int size)
  130. {
  131. if (dest == null)
  132. {
  133. throw new ArgumentNullException(nameof(dest));
  134. }
  135. if (destIdx < 0 || destIdx > dest.Length - size)
  136. {
  137. throw new ArgumentException("destIdx");
  138. }
  139. }
  140. private class CopyConverter : DataConverter
  141. {
  142. public override double GetDouble(byte[] data, int index)
  143. {
  144. if (data == null)
  145. {
  146. throw new ArgumentNullException(nameof(data));
  147. }
  148. if (data.Length - index < 8)
  149. {
  150. throw new ArgumentException("index");
  151. }
  152. if (index < 0)
  153. {
  154. throw new ArgumentException("index");
  155. }
  156. double ret;
  157. byte* b = (byte*)&ret;
  158. for (int i = 0; i < 8; i++)
  159. {
  160. b[i] = data[index + i];
  161. }
  162. return ret;
  163. }
  164. public override ulong GetUInt64(byte[] data, int index)
  165. {
  166. if (data == null)
  167. {
  168. throw new ArgumentNullException(nameof(data));
  169. }
  170. if (data.Length - index < 8)
  171. {
  172. throw new ArgumentException("index");
  173. }
  174. if (index < 0)
  175. {
  176. throw new ArgumentException("index");
  177. }
  178. ulong ret;
  179. byte* b = (byte*)&ret;
  180. for (int i = 0; i < 8; i++)
  181. {
  182. b[i] = data[index + i];
  183. }
  184. return ret;
  185. }
  186. public override long GetInt64(byte[] data, int index)
  187. {
  188. if (data == null)
  189. {
  190. throw new ArgumentNullException(nameof(data));
  191. }
  192. if (data.Length - index < 8)
  193. {
  194. throw new ArgumentException("index");
  195. }
  196. if (index < 0)
  197. {
  198. throw new ArgumentException("index");
  199. }
  200. long ret;
  201. byte* b = (byte*)&ret;
  202. for (int i = 0; i < 8; i++)
  203. {
  204. b[i] = data[index + i];
  205. }
  206. return ret;
  207. }
  208. public override float GetFloat(byte[] data, int index)
  209. {
  210. if (data == null)
  211. {
  212. throw new ArgumentNullException(nameof(data));
  213. }
  214. if (data.Length - index < 4)
  215. {
  216. throw new ArgumentException("index");
  217. }
  218. if (index < 0)
  219. {
  220. throw new ArgumentException("index");
  221. }
  222. float ret;
  223. byte* b = (byte*)&ret;
  224. for (int i = 0; i < 4; i++)
  225. {
  226. b[i] = data[index + i];
  227. }
  228. return ret;
  229. }
  230. public override int GetInt32(byte[] data, int index)
  231. {
  232. if (data == null)
  233. {
  234. throw new ArgumentNullException(nameof(data));
  235. }
  236. if (data.Length - index < 4)
  237. {
  238. throw new ArgumentException("index");
  239. }
  240. if (index < 0)
  241. {
  242. throw new ArgumentException("index");
  243. }
  244. int ret;
  245. byte* b = (byte*)&ret;
  246. for (int i = 0; i < 4; i++)
  247. {
  248. b[i] = data[index + i];
  249. }
  250. return ret;
  251. }
  252. public override uint GetUInt32(byte[] data, int index)
  253. {
  254. if (data == null)
  255. {
  256. throw new ArgumentNullException(nameof(data));
  257. }
  258. if (data.Length - index < 4)
  259. {
  260. throw new ArgumentException("index");
  261. }
  262. if (index < 0)
  263. {
  264. throw new ArgumentException("index");
  265. }
  266. uint ret;
  267. byte* b = (byte*)&ret;
  268. for (int i = 0; i < 4; i++)
  269. {
  270. b[i] = data[index + i];
  271. }
  272. return ret;
  273. }
  274. public override short GetInt16(byte[] data, int index)
  275. {
  276. if (data == null)
  277. {
  278. throw new ArgumentNullException(nameof(data));
  279. }
  280. if (data.Length - index < 2)
  281. {
  282. throw new ArgumentException("index");
  283. }
  284. if (index < 0)
  285. {
  286. throw new ArgumentException("index");
  287. }
  288. short ret;
  289. byte* b = (byte*)&ret;
  290. for (int i = 0; i < 2; i++)
  291. {
  292. b[i] = data[index + i];
  293. }
  294. return ret;
  295. }
  296. public override ushort GetUInt16(byte[] data, int index)
  297. {
  298. if (data == null)
  299. {
  300. throw new ArgumentNullException(nameof(data));
  301. }
  302. if (data.Length - index < 2)
  303. {
  304. throw new ArgumentException("index");
  305. }
  306. if (index < 0)
  307. {
  308. throw new ArgumentException("index");
  309. }
  310. ushort ret;
  311. byte* b = (byte*)&ret;
  312. for (int i = 0; i < 2; i++)
  313. {
  314. b[i] = data[index + i];
  315. }
  316. return ret;
  317. }
  318. public override void PutBytes(byte[] dest, int destIdx, double value)
  319. {
  320. Check(dest, destIdx, 8);
  321. fixed (byte* target = &dest[destIdx])
  322. {
  323. long* source = (long*)&value;
  324. *((long*)target) = *source;
  325. }
  326. }
  327. public override void PutBytes(byte[] dest, int destIdx, float value)
  328. {
  329. Check(dest, destIdx, 4);
  330. fixed (byte* target = &dest[destIdx])
  331. {
  332. uint* source = (uint*)&value;
  333. *((uint*)target) = *source;
  334. }
  335. }
  336. public override void PutBytes(byte[] dest, int destIdx, int value)
  337. {
  338. Check(dest, destIdx, 4);
  339. fixed (byte* target = &dest[destIdx])
  340. {
  341. uint* source = (uint*)&value;
  342. *((uint*)target) = *source;
  343. }
  344. }
  345. public override void PutBytes(byte[] dest, int destIdx, uint value)
  346. {
  347. Check(dest, destIdx, 4);
  348. fixed (byte* target = &dest[destIdx])
  349. {
  350. uint* source = &value;
  351. *((uint*)target) = *source;
  352. }
  353. }
  354. public override void PutBytes(byte[] dest, int destIdx, long value)
  355. {
  356. Check(dest, destIdx, 8);
  357. fixed (byte* target = &dest[destIdx])
  358. {
  359. long* source = &value;
  360. *((long*)target) = *source;
  361. }
  362. }
  363. public override void PutBytes(byte[] dest, int destIdx, ulong value)
  364. {
  365. Check(dest, destIdx, 8);
  366. fixed (byte* target = &dest[destIdx])
  367. {
  368. ulong* source = &value;
  369. *((ulong*)target) = *source;
  370. }
  371. }
  372. public override void PutBytes(byte[] dest, int destIdx, short value)
  373. {
  374. Check(dest, destIdx, 2);
  375. fixed (byte* target = &dest[destIdx])
  376. {
  377. ushort* source = (ushort*)&value;
  378. *((ushort*)target) = *source;
  379. }
  380. }
  381. public override void PutBytes(byte[] dest, int destIdx, ushort value)
  382. {
  383. Check(dest, destIdx, 2);
  384. fixed (byte* target = &dest[destIdx])
  385. {
  386. ushort* source = &value;
  387. *((ushort*)target) = *source;
  388. }
  389. }
  390. }
  391. private class SwapConverter : DataConverter
  392. {
  393. public override double GetDouble(byte[] data, int index)
  394. {
  395. if (data == null)
  396. {
  397. throw new ArgumentNullException(nameof(data));
  398. }
  399. if (data.Length - index < 8)
  400. {
  401. throw new ArgumentException("index");
  402. }
  403. if (index < 0)
  404. {
  405. throw new ArgumentException("index");
  406. }
  407. double ret;
  408. byte* b = (byte*)&ret;
  409. for (int i = 0; i < 8; i++)
  410. {
  411. b[7 - i] = data[index + i];
  412. }
  413. return ret;
  414. }
  415. public override ulong GetUInt64(byte[] data, int index)
  416. {
  417. if (data == null)
  418. {
  419. throw new ArgumentNullException(nameof(data));
  420. }
  421. if (data.Length - index < 8)
  422. {
  423. throw new ArgumentException("index");
  424. }
  425. if (index < 0)
  426. {
  427. throw new ArgumentException("index");
  428. }
  429. ulong ret;
  430. byte* b = (byte*)&ret;
  431. for (int i = 0; i < 8; i++)
  432. {
  433. b[7 - i] = data[index + i];
  434. }
  435. return ret;
  436. }
  437. public override long GetInt64(byte[] data, int index)
  438. {
  439. if (data == null)
  440. {
  441. throw new ArgumentNullException(nameof(data));
  442. }
  443. if (data.Length - index < 8)
  444. {
  445. throw new ArgumentException("index");
  446. }
  447. if (index < 0)
  448. {
  449. throw new ArgumentException("index");
  450. }
  451. long ret;
  452. byte* b = (byte*)&ret;
  453. for (int i = 0; i < 8; i++)
  454. {
  455. b[7 - i] = data[index + i];
  456. }
  457. return ret;
  458. }
  459. public override float GetFloat(byte[] data, int index)
  460. {
  461. if (data == null)
  462. {
  463. throw new ArgumentNullException(nameof(data));
  464. }
  465. if (data.Length - index < 4)
  466. {
  467. throw new ArgumentException("index");
  468. }
  469. if (index < 0)
  470. {
  471. throw new ArgumentException("index");
  472. }
  473. float ret;
  474. byte* b = (byte*)&ret;
  475. for (int i = 0; i < 4; i++)
  476. {
  477. b[3 - i] = data[index + i];
  478. }
  479. return ret;
  480. }
  481. public override int GetInt32(byte[] data, int index)
  482. {
  483. if (data == null)
  484. {
  485. throw new ArgumentNullException(nameof(data));
  486. }
  487. if (data.Length - index < 4)
  488. {
  489. throw new ArgumentException("index");
  490. }
  491. if (index < 0)
  492. {
  493. throw new ArgumentException("index");
  494. }
  495. int ret;
  496. byte* b = (byte*)&ret;
  497. for (int i = 0; i < 4; i++)
  498. {
  499. b[3 - i] = data[index + i];
  500. }
  501. return ret;
  502. }
  503. public override uint GetUInt32(byte[] data, int index)
  504. {
  505. if (data == null)
  506. {
  507. throw new ArgumentNullException(nameof(data));
  508. }
  509. if (data.Length - index < 4)
  510. {
  511. throw new ArgumentException("index");
  512. }
  513. if (index < 0)
  514. {
  515. throw new ArgumentException("index");
  516. }
  517. uint ret;
  518. byte* b = (byte*)&ret;
  519. for (int i = 0; i < 4; i++)
  520. {
  521. b[3 - i] = data[index + i];
  522. }
  523. return ret;
  524. }
  525. public override short GetInt16(byte[] data, int index)
  526. {
  527. if (data == null)
  528. {
  529. throw new ArgumentNullException(nameof(data));
  530. }
  531. if (data.Length - index < 2)
  532. {
  533. throw new ArgumentException("index");
  534. }
  535. if (index < 0)
  536. {
  537. throw new ArgumentException("index");
  538. }
  539. short ret;
  540. byte* b = (byte*)&ret;
  541. for (int i = 0; i < 2; i++)
  542. {
  543. b[1 - i] = data[index + i];
  544. }
  545. return ret;
  546. }
  547. public override ushort GetUInt16(byte[] data, int index)
  548. {
  549. if (data == null)
  550. {
  551. throw new ArgumentNullException(nameof(data));
  552. }
  553. if (data.Length - index < 2)
  554. {
  555. throw new ArgumentException("index");
  556. }
  557. if (index < 0)
  558. {
  559. throw new ArgumentException("index");
  560. }
  561. ushort ret;
  562. byte* b = (byte*)&ret;
  563. for (int i = 0; i < 2; i++)
  564. {
  565. b[1 - i] = data[index + i];
  566. }
  567. return ret;
  568. }
  569. public override void PutBytes(byte[] dest, int destIdx, double value)
  570. {
  571. Check(dest, destIdx, 8);
  572. fixed (byte* target = &dest[destIdx])
  573. {
  574. byte* source = (byte*)&value;
  575. for (int i = 0; i < 8; i++)
  576. {
  577. target[i] = source[7 - i];
  578. }
  579. }
  580. }
  581. public override void PutBytes(byte[] dest, int destIdx, float value)
  582. {
  583. Check(dest, destIdx, 4);
  584. fixed (byte* target = &dest[destIdx])
  585. {
  586. byte* source = (byte*)&value;
  587. for (int i = 0; i < 4; i++)
  588. {
  589. target[i] = source[3 - i];
  590. }
  591. }
  592. }
  593. public override void PutBytes(byte[] dest, int destIdx, int value)
  594. {
  595. Check(dest, destIdx, 4);
  596. fixed (byte* target = &dest[destIdx])
  597. {
  598. byte* source = (byte*)&value;
  599. for (int i = 0; i < 4; i++)
  600. {
  601. target[i] = source[3 - i];
  602. }
  603. }
  604. }
  605. public override void PutBytes(byte[] dest, int destIdx, uint value)
  606. {
  607. Check(dest, destIdx, 4);
  608. fixed (byte* target = &dest[destIdx])
  609. {
  610. byte* source = (byte*)&value;
  611. for (int i = 0; i < 4; i++)
  612. {
  613. target[i] = source[3 - i];
  614. }
  615. }
  616. }
  617. public override void PutBytes(byte[] dest, int destIdx, long value)
  618. {
  619. Check(dest, destIdx, 8);
  620. fixed (byte* target = &dest[destIdx])
  621. {
  622. byte* source = (byte*)&value;
  623. for (int i = 0; i < 8; i++)
  624. {
  625. target[i] = source[7 - i];
  626. }
  627. }
  628. }
  629. public override void PutBytes(byte[] dest, int destIdx, ulong value)
  630. {
  631. Check(dest, destIdx, 8);
  632. fixed (byte* target = &dest[destIdx])
  633. {
  634. byte* source = (byte*)&value;
  635. for (int i = 0; i < 8; i++)
  636. {
  637. target[i] = source[7 - i];
  638. }
  639. }
  640. }
  641. public override void PutBytes(byte[] dest, int destIdx, short value)
  642. {
  643. Check(dest, destIdx, 2);
  644. fixed (byte* target = &dest[destIdx])
  645. {
  646. byte* source = (byte*)&value;
  647. for (int i = 0; i < 2; i++)
  648. {
  649. target[i] = source[1 - i];
  650. }
  651. }
  652. }
  653. public override void PutBytes(byte[] dest, int destIdx, ushort value)
  654. {
  655. Check(dest, destIdx, 2);
  656. fixed (byte* target = &dest[destIdx])
  657. {
  658. byte* source = (byte*)&value;
  659. for (int i = 0; i < 2; i++)
  660. {
  661. target[i] = source[1 - i];
  662. }
  663. }
  664. }
  665. }
  666. #if MONO_DATACONVERTER_STATIC_METHODS
  667. static unsafe void PutBytesLE (byte *dest, byte *src, int count)
  668. {
  669. int i = 0;
  670. if (BitConverter.IsLittleEndian){
  671. for (; i < count; i++)
  672. *dest++ = *src++;
  673. } else {
  674. dest += count;
  675. for (; i < count; i++)
  676. *(--dest) = *src++;
  677. }
  678. }
  679. static unsafe void PutBytesBE (byte *dest, byte *src, int count)
  680. {
  681. int i = 0;
  682. if (BitConverter.IsLittleEndian){
  683. dest += count;
  684. for (; i < count; i++)
  685. *(--dest) = *src++;
  686. } else {
  687. for (; i < count; i++)
  688. *dest++ = *src++;
  689. }
  690. }
  691. static unsafe void PutBytesNative (byte *dest, byte *src, int count)
  692. {
  693. int i = 0;
  694. for (; i < count; i++)
  695. dest [i-count] = *src++;
  696. }
  697. static public unsafe double DoubleFromLE (byte[] data, int index)
  698. {
  699. if (data == null)
  700. throw new ArgumentNullException ("data");
  701. if (data.Length - index < 8)
  702. throw new ArgumentException ("index");
  703. if (index < 0)
  704. throw new ArgumentException ("index");
  705. double ret;
  706. fixed (byte *src = &data[index]){
  707. PutBytesLE ((byte *) &ret, src, 8);
  708. }
  709. return ret;
  710. }
  711. static public unsafe float FloatFromLE (byte [] data, int index)
  712. {
  713. if (data == null)
  714. throw new ArgumentNullException ("data");
  715. if (data.Length - index < 4)
  716. throw new ArgumentException ("index");
  717. if (index < 0)
  718. throw new ArgumentException ("index");
  719. float ret;
  720. fixed (byte *src = &data[index]){
  721. PutBytesLE ((byte *) &ret, src, 4);
  722. }
  723. return ret;
  724. }
  725. static public unsafe long Int64FromLE (byte [] data, int index)
  726. {
  727. if (data == null)
  728. throw new ArgumentNullException ("data");
  729. if (data.Length - index < 8)
  730. throw new ArgumentException ("index");
  731. if (index < 0)
  732. throw new ArgumentException ("index");
  733. long ret;
  734. fixed (byte *src = &data[index]){
  735. PutBytesLE ((byte *) &ret, src, 8);
  736. }
  737. return ret;
  738. }
  739. static public unsafe ulong UInt64FromLE (byte [] data, int index)
  740. {
  741. if (data == null)
  742. throw new ArgumentNullException ("data");
  743. if (data.Length - index < 8)
  744. throw new ArgumentException ("index");
  745. if (index < 0)
  746. throw new ArgumentException ("index");
  747. ulong ret;
  748. fixed (byte *src = &data[index]){
  749. PutBytesLE ((byte *) &ret, src, 8);
  750. }
  751. return ret;
  752. }
  753. static public unsafe int Int32FromLE (byte [] data, int index)
  754. {
  755. if (data == null)
  756. throw new ArgumentNullException ("data");
  757. if (data.Length - index < 4)
  758. throw new ArgumentException ("index");
  759. if (index < 0)
  760. throw new ArgumentException ("index");
  761. int ret;
  762. fixed (byte *src = &data[index]){
  763. PutBytesLE ((byte *) &ret, src, 4);
  764. }
  765. return ret;
  766. }
  767. static public unsafe uint UInt32FromLE (byte [] data, int index)
  768. {
  769. if (data == null)
  770. throw new ArgumentNullException ("data");
  771. if (data.Length - index < 4)
  772. throw new ArgumentException ("index");
  773. if (index < 0)
  774. throw new ArgumentException ("index");
  775. uint ret;
  776. fixed (byte *src = &data[index]){
  777. PutBytesLE ((byte *) &ret, src, 4);
  778. }
  779. return ret;
  780. }
  781. static public unsafe short Int16FromLE (byte [] data, int index)
  782. {
  783. if (data == null)
  784. throw new ArgumentNullException ("data");
  785. if (data.Length - index < 2)
  786. throw new ArgumentException ("index");
  787. if (index < 0)
  788. throw new ArgumentException ("index");
  789. short ret;
  790. fixed (byte *src = &data[index]){
  791. PutBytesLE ((byte *) &ret, src, 2);
  792. }
  793. return ret;
  794. }
  795. static public unsafe ushort UInt16FromLE (byte [] data, int index)
  796. {
  797. if (data == null)
  798. throw new ArgumentNullException ("data");
  799. if (data.Length - index < 2)
  800. throw new ArgumentException ("index");
  801. if (index < 0)
  802. throw new ArgumentException ("index");
  803. ushort ret;
  804. fixed (byte *src = &data[index]){
  805. PutBytesLE ((byte *) &ret, src, 2);
  806. }
  807. return ret;
  808. }
  809. static public unsafe double DoubleFromBE (byte[] data, int index)
  810. {
  811. if (data == null)
  812. throw new ArgumentNullException ("data");
  813. if (data.Length - index < 8)
  814. throw new ArgumentException ("index");
  815. if (index < 0)
  816. throw new ArgumentException ("index");
  817. double ret;
  818. fixed (byte *src = &data[index]){
  819. PutBytesBE ((byte *) &ret, src, 8);
  820. }
  821. return ret;
  822. }
  823. static public unsafe float FloatFromBE (byte [] data, int index)
  824. {
  825. if (data == null)
  826. throw new ArgumentNullException ("data");
  827. if (data.Length - index < 4)
  828. throw new ArgumentException ("index");
  829. if (index < 0)
  830. throw new ArgumentException ("index");
  831. float ret;
  832. fixed (byte *src = &data[index]){
  833. PutBytesBE ((byte *) &ret, src, 4);
  834. }
  835. return ret;
  836. }
  837. static public unsafe long Int64FromBE (byte [] data, int index)
  838. {
  839. if (data == null)
  840. throw new ArgumentNullException ("data");
  841. if (data.Length - index < 8)
  842. throw new ArgumentException ("index");
  843. if (index < 0)
  844. throw new ArgumentException ("index");
  845. long ret;
  846. fixed (byte *src = &data[index]){
  847. PutBytesBE ((byte *) &ret, src, 8);
  848. }
  849. return ret;
  850. }
  851. static public unsafe ulong UInt64FromBE (byte [] data, int index)
  852. {
  853. if (data == null)
  854. throw new ArgumentNullException ("data");
  855. if (data.Length - index < 8)
  856. throw new ArgumentException ("index");
  857. if (index < 0)
  858. throw new ArgumentException ("index");
  859. ulong ret;
  860. fixed (byte *src = &data[index]){
  861. PutBytesBE ((byte *) &ret, src, 8);
  862. }
  863. return ret;
  864. }
  865. static public unsafe int Int32FromBE (byte [] data, int index)
  866. {
  867. if (data == null)
  868. throw new ArgumentNullException ("data");
  869. if (data.Length - index < 4)
  870. throw new ArgumentException ("index");
  871. if (index < 0)
  872. throw new ArgumentException ("index");
  873. int ret;
  874. fixed (byte *src = &data[index]){
  875. PutBytesBE ((byte *) &ret, src, 4);
  876. }
  877. return ret;
  878. }
  879. static public unsafe uint UInt32FromBE (byte [] data, int index)
  880. {
  881. if (data == null)
  882. throw new ArgumentNullException ("data");
  883. if (data.Length - index < 4)
  884. throw new ArgumentException ("index");
  885. if (index < 0)
  886. throw new ArgumentException ("index");
  887. uint ret;
  888. fixed (byte *src = &data[index]){
  889. PutBytesBE ((byte *) &ret, src, 4);
  890. }
  891. return ret;
  892. }
  893. static public unsafe short Int16FromBE (byte [] data, int index)
  894. {
  895. if (data == null)
  896. throw new ArgumentNullException ("data");
  897. if (data.Length - index < 2)
  898. throw new ArgumentException ("index");
  899. if (index < 0)
  900. throw new ArgumentException ("index");
  901. short ret;
  902. fixed (byte *src = &data[index]){
  903. PutBytesBE ((byte *) &ret, src, 2);
  904. }
  905. return ret;
  906. }
  907. static public unsafe ushort UInt16FromBE (byte [] data, int index)
  908. {
  909. if (data == null)
  910. throw new ArgumentNullException ("data");
  911. if (data.Length - index < 2)
  912. throw new ArgumentException ("index");
  913. if (index < 0)
  914. throw new ArgumentException ("index");
  915. ushort ret;
  916. fixed (byte *src = &data[index]){
  917. PutBytesBE ((byte *) &ret, src, 2);
  918. }
  919. return ret;
  920. }
  921. static public unsafe double DoubleFromNative (byte[] data, int index)
  922. {
  923. if (data == null)
  924. throw new ArgumentNullException ("data");
  925. if (data.Length - index < 8)
  926. throw new ArgumentException ("index");
  927. if (index < 0)
  928. throw new ArgumentException ("index");
  929. double ret;
  930. fixed (byte *src = &data[index]){
  931. PutBytesNative ((byte *) &ret, src, 8);
  932. }
  933. return ret;
  934. }
  935. static public unsafe float FloatFromNative (byte [] data, int index)
  936. {
  937. if (data == null)
  938. throw new ArgumentNullException ("data");
  939. if (data.Length - index < 4)
  940. throw new ArgumentException ("index");
  941. if (index < 0)
  942. throw new ArgumentException ("index");
  943. float ret;
  944. fixed (byte *src = &data[index]){
  945. PutBytesNative ((byte *) &ret, src, 4);
  946. }
  947. return ret;
  948. }
  949. static public unsafe long Int64FromNative (byte [] data, int index)
  950. {
  951. if (data == null)
  952. throw new ArgumentNullException ("data");
  953. if (data.Length - index < 8)
  954. throw new ArgumentException ("index");
  955. if (index < 0)
  956. throw new ArgumentException ("index");
  957. long ret;
  958. fixed (byte *src = &data[index]){
  959. PutBytesNative ((byte *) &ret, src, 8);
  960. }
  961. return ret;
  962. }
  963. static public unsafe ulong UInt64FromNative (byte [] data, int index)
  964. {
  965. if (data == null)
  966. throw new ArgumentNullException ("data");
  967. if (data.Length - index < 8)
  968. throw new ArgumentException ("index");
  969. if (index < 0)
  970. throw new ArgumentException ("index");
  971. ulong ret;
  972. fixed (byte *src = &data[index]){
  973. PutBytesNative ((byte *) &ret, src, 8);
  974. }
  975. return ret;
  976. }
  977. static public unsafe int Int32FromNative (byte [] data, int index)
  978. {
  979. if (data == null)
  980. throw new ArgumentNullException ("data");
  981. if (data.Length - index < 4)
  982. throw new ArgumentException ("index");
  983. if (index < 0)
  984. throw new ArgumentException ("index");
  985. int ret;
  986. fixed (byte *src = &data[index]){
  987. PutBytesNative ((byte *) &ret, src, 4);
  988. }
  989. return ret;
  990. }
  991. static public unsafe uint UInt32FromNative (byte [] data, int index)
  992. {
  993. if (data == null)
  994. throw new ArgumentNullException ("data");
  995. if (data.Length - index < 4)
  996. throw new ArgumentException ("index");
  997. if (index < 0)
  998. throw new ArgumentException ("index");
  999. uint ret;
  1000. fixed (byte *src = &data[index]){
  1001. PutBytesNative ((byte *) &ret, src, 4);
  1002. }
  1003. return ret;
  1004. }
  1005. static public unsafe short Int16FromNative (byte [] data, int index)
  1006. {
  1007. if (data == null)
  1008. throw new ArgumentNullException ("data");
  1009. if (data.Length - index < 2)
  1010. throw new ArgumentException ("index");
  1011. if (index < 0)
  1012. throw new ArgumentException ("index");
  1013. short ret;
  1014. fixed (byte *src = &data[index]){
  1015. PutBytesNative ((byte *) &ret, src, 2);
  1016. }
  1017. return ret;
  1018. }
  1019. static public unsafe ushort UInt16FromNative (byte [] data, int index)
  1020. {
  1021. if (data == null)
  1022. throw new ArgumentNullException ("data");
  1023. if (data.Length - index < 2)
  1024. throw new ArgumentException ("index");
  1025. if (index < 0)
  1026. throw new ArgumentException ("index");
  1027. ushort ret;
  1028. fixed (byte *src = &data[index]){
  1029. PutBytesNative ((byte *) &ret, src, 2);
  1030. }
  1031. return ret;
  1032. }
  1033. unsafe static byte[] GetBytesPtr (byte *ptr, int count)
  1034. {
  1035. byte [] ret = new byte [count];
  1036. for (int i = 0; i < count; i++) {
  1037. ret [i] = ptr [i];
  1038. }
  1039. return ret;
  1040. }
  1041. unsafe static byte[] GetBytesSwap (bool swap, byte *ptr, int count)
  1042. {
  1043. byte [] ret = new byte [count];
  1044. if (swap){
  1045. int t = count-1;
  1046. for (int i = 0; i < count; i++) {
  1047. ret [t-i] = ptr [i];
  1048. }
  1049. } else {
  1050. for (int i = 0; i < count; i++) {
  1051. ret [i] = ptr [i];
  1052. }
  1053. }
  1054. return ret;
  1055. }
  1056. unsafe public static byte[] GetBytesNative (bool value)
  1057. {
  1058. return GetBytesPtr ((byte *) &value, 1);
  1059. }
  1060. unsafe public static byte[] GetBytesNative (char value)
  1061. {
  1062. return GetBytesPtr ((byte *) &value, 2);
  1063. }
  1064. unsafe public static byte[] GetBytesNative (short value)
  1065. {
  1066. return GetBytesPtr ((byte *) &value, 2);
  1067. }
  1068. unsafe public static byte[] GetBytesNative (int value)
  1069. {
  1070. return GetBytesPtr ((byte *) &value, 4);
  1071. }
  1072. unsafe public static byte[] GetBytesNative (long value)
  1073. {
  1074. return GetBytesPtr ((byte *) &value, 8);
  1075. }
  1076. [CLSCompliant (false)]
  1077. unsafe public static byte[] GetBytesNative (ushort value)
  1078. {
  1079. return GetBytesPtr ((byte *) &value, 2);
  1080. }
  1081. [CLSCompliant (false)]
  1082. unsafe public static byte[] GetBytesNative (uint value)
  1083. {
  1084. return GetBytesPtr ((byte *) &value, 4);
  1085. }
  1086. [CLSCompliant (false)]
  1087. unsafe public static byte[] GetBytesNative (ulong value)
  1088. {
  1089. return GetBytesPtr ((byte *) &value, 8);
  1090. }
  1091. unsafe public static byte[] GetBytesNative (float value)
  1092. {
  1093. return GetBytesPtr ((byte *) &value, 4);
  1094. }
  1095. unsafe public static byte[] GetBytesNative (double value)
  1096. {
  1097. return GetBytesPtr ((byte *) &value, 8);
  1098. }
  1099. unsafe public static byte[] GetBytesLE (bool value)
  1100. {
  1101. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 1);
  1102. }
  1103. unsafe public static byte[] GetBytesLE (char value)
  1104. {
  1105. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2);
  1106. }
  1107. unsafe public static byte[] GetBytesLE (short value)
  1108. {
  1109. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2);
  1110. }
  1111. unsafe public static byte[] GetBytesLE (int value)
  1112. {
  1113. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4);
  1114. }
  1115. unsafe public static byte[] GetBytesLE (long value)
  1116. {
  1117. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8);
  1118. }
  1119. [CLSCompliant (false)]
  1120. unsafe public static byte[] GetBytesLE (ushort value)
  1121. {
  1122. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2);
  1123. }
  1124. [CLSCompliant (false)]
  1125. unsafe public static byte[] GetBytesLE (uint value)
  1126. {
  1127. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4);
  1128. }
  1129. [CLSCompliant (false)]
  1130. unsafe public static byte[] GetBytesLE (ulong value)
  1131. {
  1132. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8);
  1133. }
  1134. unsafe public static byte[] GetBytesLE (float value)
  1135. {
  1136. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4);
  1137. }
  1138. unsafe public static byte[] GetBytesLE (double value)
  1139. {
  1140. return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8);
  1141. }
  1142. unsafe public static byte[] GetBytesBE (bool value)
  1143. {
  1144. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 1);
  1145. }
  1146. unsafe public static byte[] GetBytesBE (char value)
  1147. {
  1148. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2);
  1149. }
  1150. unsafe public static byte[] GetBytesBE (short value)
  1151. {
  1152. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2);
  1153. }
  1154. unsafe public static byte[] GetBytesBE (int value)
  1155. {
  1156. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4);
  1157. }
  1158. unsafe public static byte[] GetBytesBE (long value)
  1159. {
  1160. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8);
  1161. }
  1162. [CLSCompliant (false)]
  1163. unsafe public static byte[] GetBytesBE (ushort value)
  1164. {
  1165. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2);
  1166. }
  1167. [CLSCompliant (false)]
  1168. unsafe public static byte[] GetBytesBE (uint value)
  1169. {
  1170. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4);
  1171. }
  1172. [CLSCompliant (false)]
  1173. unsafe public static byte[] GetBytesBE (ulong value)
  1174. {
  1175. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8);
  1176. }
  1177. unsafe public static byte[] GetBytesBE (float value)
  1178. {
  1179. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4);
  1180. }
  1181. unsafe public static byte[] GetBytesBE (double value)
  1182. {
  1183. return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8);
  1184. }
  1185. #endif
  1186. }
  1187. }