Util.cs 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // Comments: Tetsuo Sugiyama, Ph.D.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // version 2 as published by the Free Software Foundation.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License version 2
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  29. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  30. //
  31. //
  32. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  33. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  34. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  35. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  36. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  37. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  38. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  39. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  40. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  41. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  42. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  43. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  44. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  45. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  46. //
  47. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  48. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  49. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  50. // COUNTRIES MIGHT BE RESTRICTED.
  51. //
  52. //
  53. // SOURCE CODE CONTRIBUTION
  54. // ------------------------
  55. //
  56. // Your contribution to SoftEther VPN Project is much appreciated.
  57. // Please send patches to us through GitHub.
  58. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  59. // http://www.softether.org/5-download/src/9.patch
  60. //
  61. //
  62. // DEAR SECURITY EXPERTS
  63. // ---------------------
  64. //
  65. // If you find a bug or a security vulnerability please kindly inform us
  66. // about the problem immediately so that we can fix the security problem
  67. // to protect a lot of users around the world as soon as possible.
  68. //
  69. // Our e-mail address for security reports is:
  70. // softether-vpn-security [at] softether.org
  71. //
  72. // Please note that the above e-mail address is not a technical support
  73. // inquiry address. If you need technical assistance, please visit
  74. // http://www.softether.org/ and ask your question on the users forum.
  75. //
  76. // Thank you for your cooperation.
  77. //
  78. //
  79. // NO MEMORY OR RESOURCE LEAKS
  80. // ---------------------------
  81. //
  82. // The memory-leaks and resource-leaks verification under the stress
  83. // test has been passed before release this source code.
  84. using System;
  85. using System.Threading;
  86. using System.Data;
  87. using System.Data.Sql;
  88. using System.Data.SqlClient;
  89. using System.Data.SqlTypes;
  90. using System.Text;
  91. using System.Configuration;
  92. using System.Collections;
  93. using System.Collections.Generic;
  94. using System.Security.Cryptography;
  95. using System.Web;
  96. using System.Web.Security;
  97. using System.Web.UI;
  98. using System.Web.UI.WebControls;
  99. using System.Web.UI.WebControls.WebParts;
  100. using System.Web.UI.HtmlControls;
  101. using System.IO;
  102. using System.Drawing;
  103. using System.Drawing.Imaging;
  104. using System.Drawing.Drawing2D;
  105. using System.Web.Mail;
  106. using System.Xml;
  107. using System.Xml.Schema;
  108. using System.Xml.Serialization;
  109. using System.ComponentModel;
  110. using System.Runtime.InteropServices;
  111. namespace CoreUtil
  112. {
  113. public enum CoreLanguage
  114. {
  115. Japanese = 0,
  116. English = 1,
  117. }
  118. public class CoreLanguageClass
  119. {
  120. public readonly CoreLanguage Language;
  121. public readonly int Id;
  122. readonly string name;
  123. public string Name
  124. {
  125. get
  126. {
  127. if (name == "ja")
  128. {
  129. if (CoreLanguageList.RegardsJapanAsJP)
  130. {
  131. return "jp";
  132. }
  133. }
  134. return name;
  135. }
  136. }
  137. public readonly string TitleInEnglish;
  138. public readonly string TitleInNative;
  139. public CoreLanguageClass(CoreLanguage lang, int id, string name,
  140. string titleInEnglish, string titleInNative)
  141. {
  142. this.Language = lang;
  143. this.Id = id;
  144. this.name = name;
  145. this.TitleInEnglish = titleInEnglish;
  146. this.TitleInNative = titleInNative;
  147. }
  148. public static void SetCurrentThreadLanguageClass(CoreLanguageClass lang)
  149. {
  150. ThreadData.CurrentThreadData.DataList["current_thread_language"] = lang;
  151. }
  152. public static CoreLanguageClass CurrentThreadLanguageClass
  153. {
  154. get
  155. {
  156. return GetCurrentThreadLanguageClass();
  157. }
  158. set
  159. {
  160. SetCurrentThreadLanguageClass(value);
  161. }
  162. }
  163. public static CoreLanguage CurrentThreadLanguage
  164. {
  165. get
  166. {
  167. return CurrentThreadLanguageClass.Language;
  168. }
  169. }
  170. public static CoreLanguageClass GetCurrentThreadLanguageClass()
  171. {
  172. CoreLanguageClass lang = null;
  173. try
  174. {
  175. lang = (CoreLanguageClass)ThreadData.CurrentThreadData.DataList["current_thread_language"];
  176. }
  177. catch
  178. {
  179. }
  180. if (lang == null)
  181. {
  182. lang = CoreLanguageList.DefaultLanguage;
  183. SetCurrentThreadLanguageClass(lang);
  184. }
  185. return lang;
  186. }
  187. }
  188. public static class CoreLanguageList
  189. {
  190. public static readonly CoreLanguageClass DefaultLanguage;
  191. public static readonly CoreLanguageClass Japanese;
  192. public static readonly CoreLanguageClass English;
  193. public static bool RegardsJapanAsJP = false;
  194. public static readonly List<CoreLanguageClass> LanguageList = new List<CoreLanguageClass>();
  195. static CoreLanguageList()
  196. {
  197. CoreLanguageList.LanguageList = new List<CoreLanguageClass>();
  198. CoreLanguageList.Japanese = new CoreLanguageClass(CoreLanguage.Japanese,
  199. 0, "ja", "Japanese", "日本語");
  200. CoreLanguageList.English = new CoreLanguageClass(CoreLanguage.English,
  201. 1, "en", "English", "English");
  202. CoreLanguageList.DefaultLanguage = CoreLanguageList.Japanese;
  203. CoreLanguageList.LanguageList.Add(CoreLanguageList.Japanese);
  204. CoreLanguageList.LanguageList.Add(CoreLanguageList.English);
  205. }
  206. public static CoreLanguageClass GetLanguageClassByName(string name)
  207. {
  208. Str.NormalizeStringStandard(ref name);
  209. foreach (CoreLanguageClass c in LanguageList)
  210. {
  211. if (Str.StrCmpi(c.Name, name))
  212. {
  213. return c;
  214. }
  215. }
  216. return DefaultLanguage;
  217. }
  218. public static CoreLanguageClass GetLangugageClassByEnum(CoreLanguage lang)
  219. {
  220. foreach (CoreLanguageClass c in LanguageList)
  221. {
  222. if (c.Language == lang)
  223. {
  224. return c;
  225. }
  226. }
  227. return DefaultLanguage;
  228. }
  229. }
  230. public static class Util
  231. {
  232. public const int SizeOfInt32 = 4;
  233. public const int SizeOfInt16 = 2;
  234. public const int SizeOfInt64 = 8;
  235. public const int SizeOfInt8 = 1;
  236. public static byte[] ToByte(ushort i)
  237. {
  238. byte[] ret = BitConverter.GetBytes(i);
  239. Endian(ret);
  240. return ret;
  241. }
  242. public static byte[] ToByte(short i)
  243. {
  244. byte[] ret = BitConverter.GetBytes(i);
  245. Endian(ret);
  246. return ret;
  247. }
  248. public static byte[] ToByte(uint i)
  249. {
  250. byte[] ret = BitConverter.GetBytes(i);
  251. Endian(ret);
  252. return ret;
  253. }
  254. public static byte[] ToByte(int i)
  255. {
  256. byte[] ret = BitConverter.GetBytes(i);
  257. Endian(ret);
  258. return ret;
  259. }
  260. public static byte[] ToByte(ulong i)
  261. {
  262. byte[] ret = BitConverter.GetBytes(i);
  263. Endian(ret);
  264. return ret;
  265. }
  266. public static byte[] ToByte(long i)
  267. {
  268. byte[] ret = BitConverter.GetBytes(i);
  269. Endian(ret);
  270. return ret;
  271. }
  272. public static ushort ByteToUShort(byte[] b)
  273. {
  274. byte[] c = CloneByteArray(b);
  275. Endian(c);
  276. return BitConverter.ToUInt16(c, 0);
  277. }
  278. public static short ByteToShort(byte[] b)
  279. {
  280. byte[] c = CloneByteArray(b);
  281. Endian(c);
  282. return BitConverter.ToInt16(c, 0);
  283. }
  284. public static uint ByteToUInt(byte[] b)
  285. {
  286. byte[] c = CloneByteArray(b);
  287. Endian(c);
  288. return BitConverter.ToUInt32(c, 0);
  289. }
  290. public static int ByteToInt(byte[] b)
  291. {
  292. byte[] c = CloneByteArray(b);
  293. Endian(c);
  294. return BitConverter.ToInt32(c, 0);
  295. }
  296. public static ulong ByteToULong(byte[] b)
  297. {
  298. byte[] c = CloneByteArray(b);
  299. Endian(c);
  300. return BitConverter.ToUInt64(c, 0);
  301. }
  302. public static long ByteToLong(byte[] b)
  303. {
  304. byte[] c = CloneByteArray(b);
  305. Endian(c);
  306. return BitConverter.ToInt64(c, 0);
  307. }
  308. public static byte[] ReadAllFromStream(Stream st)
  309. {
  310. byte[] tmp = new byte[32 * 1024];
  311. Buf b = new Buf();
  312. while (true)
  313. {
  314. int r = st.Read(tmp, 0, tmp.Length);
  315. if (r == 0)
  316. {
  317. break;
  318. }
  319. b.Write(tmp, 0, r);
  320. }
  321. return b.ByteData;
  322. }
  323. public static List<T> CloneList<T>(List<T> src)
  324. {
  325. List<T> ret = new List<T>();
  326. foreach (T t in src)
  327. {
  328. ret.Add(t);
  329. }
  330. return ret;
  331. }
  332. public static byte[] ExtractByteArray(byte[] data, int pos, int len)
  333. {
  334. byte[] ret = new byte[len];
  335. Util.CopyByte(ret, 0, data, pos, len);
  336. return ret;
  337. }
  338. public static T[] CombineArray<T>(params T[][] arrays)
  339. {
  340. List<T> o = new List<T>();
  341. foreach (T[] array in arrays)
  342. {
  343. foreach (T element in array)
  344. {
  345. o.Add(element);
  346. }
  347. }
  348. return o.ToArray();
  349. }
  350. public static byte[] CombineByteArray(byte[] b1, byte[] b2)
  351. {
  352. Buf b = new Buf();
  353. if (b1 != null)
  354. {
  355. b.Write(b1);
  356. }
  357. if (b2 != null)
  358. {
  359. b.Write(b2);
  360. }
  361. return b.ByteData;
  362. }
  363. public static byte[] RemoveStartByteArray(byte[] src, int numBytes)
  364. {
  365. if (numBytes == 0)
  366. {
  367. return src;
  368. }
  369. int num = src.Length - numBytes;
  370. byte[] ret = new byte[num];
  371. Util.CopyByte(ret, 0, src, numBytes, num);
  372. return ret;
  373. }
  374. public static DateTime[] GetYearNendoList(DateTime startYear, DateTime endYear)
  375. {
  376. startYear = GetStartOfNendo(startYear);
  377. endYear = GetEndOfNendo(endYear);
  378. if (startYear > endYear)
  379. {
  380. throw new ArgumentException();
  381. }
  382. List<DateTime> ret = new List<DateTime>();
  383. DateTime dt;
  384. for (dt = startYear; dt <= endYear; dt = GetStartOfNendo(dt.AddYears(1)))
  385. {
  386. ret.Add(dt);
  387. }
  388. return ret.ToArray();
  389. }
  390. public static DateTime[] GetYearList(DateTime startYear, DateTime endYear)
  391. {
  392. startYear = GetStartOfYear(startYear);
  393. endYear = GetEndOfYear(endYear);
  394. if (startYear > endYear)
  395. {
  396. throw new ArgumentException();
  397. }
  398. List<DateTime> ret = new List<DateTime>();
  399. DateTime dt;
  400. for (dt = startYear; dt <= endYear; dt = GetStartOfYear(dt.AddYears(1)))
  401. {
  402. ret.Add(dt);
  403. }
  404. return ret.ToArray();
  405. }
  406. public static DateTime[] GetMonthList(DateTime startMonth, DateTime endMonth)
  407. {
  408. startMonth = GetStartOfMonth(startMonth);
  409. endMonth = GetEndOfMonth(endMonth);
  410. if (startMonth > endMonth)
  411. {
  412. throw new ArgumentException();
  413. }
  414. List<DateTime> ret = new List<DateTime>();
  415. DateTime dt;
  416. for (dt = startMonth; dt <= endMonth; dt = GetStartOfMonth(dt.AddMonths(1)))
  417. {
  418. ret.Add(dt);
  419. }
  420. return ret.ToArray();
  421. }
  422. public static int GetAge(DateTime birthDay, DateTime now)
  423. {
  424. birthDay = birthDay.Date;
  425. now = now.Date;
  426. DateTime dayBirthDay = new DateTime(2000, birthDay.Month, birthDay.Day);
  427. DateTime dayNow = new DateTime(2000, now.Month, now.Day);
  428. int ret = now.Year - birthDay.Year;
  429. if (dayBirthDay > dayNow)
  430. {
  431. ret -= 1;
  432. }
  433. return Math.Max(ret, 0);
  434. }
  435. public static int GetNumOfDaysInMonth(DateTime dt)
  436. {
  437. DateTime dt1 = new DateTime(dt.Year, dt.Month, dt.Day);
  438. DateTime dt2 = dt1.AddMonths(1);
  439. TimeSpan span = dt2 - dt1;
  440. return span.Days;
  441. }
  442. public static int GetNumMonthSpan(DateTime dt1, DateTime dt2, bool kiriage)
  443. {
  444. if (dt1 > dt2)
  445. {
  446. DateTime dtt = dt2;
  447. dt2 = dt1;
  448. dt1 = dtt;
  449. }
  450. int i;
  451. DateTime dt = dt1;
  452. for (i = 0; ; i++)
  453. {
  454. if (kiriage)
  455. {
  456. if (dt >= dt2)
  457. {
  458. return i;
  459. }
  460. }
  461. else
  462. {
  463. if (dt >= dt2.AddMonths(1).AddTicks(-1))
  464. {
  465. return i;
  466. }
  467. }
  468. dt = dt.AddMonths(1);
  469. }
  470. }
  471. public static DateTime GetStartOfMonth(DateTime dt)
  472. {
  473. return new DateTime(dt.Year, dt.Month, 1);
  474. }
  475. public static DateTime GetEndOfMonth(DateTime dt)
  476. {
  477. return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddSeconds(-1).Date;
  478. }
  479. public static DateTime GetStartOfYear(DateTime dt)
  480. {
  481. return new DateTime(dt.Year, 1, 1, 0, 0, 0);
  482. }
  483. public static DateTime GetEndOfYear(DateTime dt)
  484. {
  485. return GetStartOfYear(dt).AddYears(1).AddSeconds(-1).Date;
  486. }
  487. public static DateTime GetEndOfMonthForSettle(DateTime dt)
  488. {
  489. dt = new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddSeconds(-1).Date;
  490. if (dt.Month == 4 && (new DateTime(dt.Year, 4, 29).DayOfWeek == DayOfWeek.Sunday))
  491. {
  492. dt = dt.AddDays(1);
  493. }
  494. while ((dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday) ||
  495. (dt.Month == 12 && dt.Day >= 29) ||
  496. (dt.Month == 1 && dt.Day <= 3))
  497. {
  498. dt = dt.AddDays(1);
  499. }
  500. return dt;
  501. }
  502. public static DateTime GetStartOfDay(DateTime dt)
  503. {
  504. return dt.Date;
  505. }
  506. public static DateTime GetEndOfDate(DateTime dt)
  507. {
  508. return GetStartOfDay(dt).AddDays(1).AddTicks(-1);
  509. }
  510. public static int GetNendo(DateTime dt)
  511. {
  512. if (dt.Month >= 4)
  513. {
  514. return dt.Year;
  515. }
  516. else
  517. {
  518. return dt.Year - 1;
  519. }
  520. }
  521. public static DateTime GetStartOfNendo(DateTime dt)
  522. {
  523. return GetStartOfNendo(GetNendo(dt));
  524. }
  525. public static DateTime GetStartOfNendo(int nendo)
  526. {
  527. return new DateTime(nendo, 4, 1, 0, 0, 0).Date;
  528. }
  529. public static DateTime GetEndOfNendo(DateTime dt)
  530. {
  531. return GetEndOfNendo(GetNendo(dt));
  532. }
  533. public static DateTime GetEndOfNendo(int nendo)
  534. {
  535. return new DateTime(nendo + 1, 3, 31, 0, 0, 0).Date;
  536. }
  537. public static void Endian(byte[] b)
  538. {
  539. if (Env.IsLittleEndian)
  540. {
  541. Array.Reverse(b);
  542. }
  543. }
  544. public static byte[] EndianRetByte(byte[] b)
  545. {
  546. b = Util.CloneByteArray(b);
  547. Endian(b);
  548. return b;
  549. }
  550. public static UInt16 Endian(UInt16 v)
  551. {
  552. return Util.ByteToUShort(Util.EndianRetByte(Util.ToByte(v)));
  553. }
  554. public static UInt32 Endian(UInt32 v)
  555. {
  556. return Util.ByteToUInt(Util.EndianRetByte(Util.ToByte(v)));
  557. }
  558. public static UInt64 Endian(UInt64 v)
  559. {
  560. return Util.ByteToULong(Util.EndianRetByte(Util.ToByte(v)));
  561. }
  562. public static string SafeDomainStr(string str)
  563. {
  564. string ret = str.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").Replace("#", "")
  565. .Replace("%", "").Replace("%", "").Replace("&", "").Replace(".", "");
  566. if (ret == "")
  567. {
  568. ret = "host";
  569. }
  570. return ret;
  571. }
  572. public static byte[] CopyByte(byte[] src)
  573. {
  574. return (byte[])src.Clone();
  575. }
  576. public static byte[] CopyByte(byte[] src, int srcOffset)
  577. {
  578. return CopyByte(src, srcOffset, src.Length - srcOffset);
  579. }
  580. public static byte[] CopyByte(byte[] src, int srcOffset, int size)
  581. {
  582. byte[] ret = new byte[size];
  583. CopyByte(ret, 0, src, srcOffset, size);
  584. return ret;
  585. }
  586. public static void CopyByte(byte[] dst, byte[] src, int srcOffset, int size)
  587. {
  588. CopyByte(dst, 0, src, srcOffset, size);
  589. }
  590. public static void CopyByte(byte[] dst, int dstOffset, byte[] src)
  591. {
  592. CopyByte(dst, dstOffset, src, 0, src.Length);
  593. }
  594. public static void CopyByte(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size)
  595. {
  596. Array.Copy(src, srcOffset, dst, dstOffset, size);
  597. }
  598. public static bool IsZero(byte[] data)
  599. {
  600. return IsZero(data, 0, data.Length);
  601. }
  602. public static bool IsZero(byte[] data, int offset, int size)
  603. {
  604. int i;
  605. for (i = offset; i < offset + size; i++)
  606. {
  607. if (data[i] != 0)
  608. {
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. public static bool CompareByte(byte[] b1, byte[] b2)
  615. {
  616. if (b1.Length != b2.Length)
  617. {
  618. return false;
  619. }
  620. int i, len;
  621. len = b1.Length;
  622. for (i = 0; i < len; i++)
  623. {
  624. if (b1[i] != b2[i])
  625. {
  626. return false;
  627. }
  628. }
  629. return true;
  630. }
  631. public static int CompareByteRetInt(byte[] b1, byte[] b2)
  632. {
  633. int i;
  634. for (i = 0; ; i++)
  635. {
  636. int a1 = -1, a2 = -1;
  637. if (b1.Length < i)
  638. {
  639. a1 = (int)b1[i];
  640. }
  641. if (b2.Length < i)
  642. {
  643. a2 = (int)b2[i];
  644. }
  645. if (a1 > a2)
  646. {
  647. return 1;
  648. }
  649. else if (a1 < a2)
  650. {
  651. return -1;
  652. }
  653. if (a1 == -1 && a2 == -1)
  654. {
  655. return 0;
  656. }
  657. }
  658. }
  659. public static byte[] CloneByteArray(byte[] src)
  660. {
  661. byte[] ret = new byte[src.Length];
  662. Util.CopyByte(ret, src, 0, src.Length);
  663. return ret;
  664. }
  665. public static DateTime UnixTimeToDateTime(uint t)
  666. {
  667. return new DateTime(1970, 1, 1).AddSeconds(t);
  668. }
  669. public static uint DateTimeToUnixTime(DateTime dt)
  670. {
  671. TimeSpan ts = dt - new DateTime(1970, 1, 1);
  672. if (ts.Ticks < 0)
  673. {
  674. throw new InvalidDataException("dt");
  675. }
  676. return (uint)ts.TotalSeconds;
  677. }
  678. public static DateTime ConvertDateTime(ulong time64)
  679. {
  680. if (time64 == 0)
  681. {
  682. return new DateTime(0);
  683. }
  684. return new DateTime(((long)time64 + 62135629200000) * 10000);
  685. }
  686. public static ulong ConvertDateTime(DateTime dt)
  687. {
  688. if (dt.Ticks == 0)
  689. {
  690. return 0;
  691. }
  692. return (ulong)dt.Ticks / 10000 - 62135629200000;
  693. }
  694. public static TimeSpan ConvertTimeSpan(ulong tick)
  695. {
  696. return new TimeSpan((long)tick * 10000);
  697. }
  698. public static ulong ConvertTimeSpan(TimeSpan span)
  699. {
  700. return (ulong)span.Ticks / 10000;
  701. }
  702. public static ushort DateTimeToDosDate(DateTime dt)
  703. {
  704. return (ushort)(
  705. ((uint)(dt.Year - 1980) << 9) |
  706. ((uint)dt.Month << 5) |
  707. (uint)dt.Day);
  708. }
  709. public static ushort DateTimeToDosTime(DateTime dt)
  710. {
  711. return (ushort)(
  712. ((uint)dt.Hour << 11) |
  713. ((uint)dt.Minute << 5) |
  714. ((uint)dt.Second >> 1));
  715. }
  716. public static bool IsNullOrEmpty(object o)
  717. {
  718. if (o == null)
  719. {
  720. return true;
  721. }
  722. if (o is string)
  723. {
  724. string s = (string)o;
  725. return Str.IsEmptyStr(s);
  726. }
  727. if (o is Array)
  728. {
  729. Array a = (Array)o;
  730. if (a.Length == 0)
  731. {
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. public static byte[] GetXmlSchemaFromType(Type type)
  738. {
  739. XmlSchemas sms = new XmlSchemas();
  740. XmlSchemaExporter ex = new XmlSchemaExporter(sms);
  741. XmlReflectionImporter im = new XmlReflectionImporter();
  742. XmlTypeMapping map = im.ImportTypeMapping(type);
  743. ex.ExportTypeMapping(map);
  744. sms.Compile(null, false);
  745. MemoryStream ms = new MemoryStream();
  746. StreamWriter sw = new StreamWriter(ms);
  747. foreach (System.Xml.Schema.XmlSchema sm in sms)
  748. {
  749. sm.Write(sw);
  750. }
  751. sw.Close();
  752. ms.Flush();
  753. byte[] data = ms.ToArray();
  754. return data;
  755. }
  756. public static string GetXmlSchemaFromTypeString(Type type)
  757. {
  758. byte[] data = GetXmlSchemaFromType(type);
  759. return Str.Utf8Encoding.GetString(data);
  760. }
  761. public static string ObjectToXmlString(object o)
  762. {
  763. byte[] data = ObjectToXml(o);
  764. return Str.Utf8Encoding.GetString(data);
  765. }
  766. public static byte[] ObjectToXml(object o)
  767. {
  768. if (o == null)
  769. {
  770. return null;
  771. }
  772. Type t = o.GetType();
  773. return ObjectToXml(o, t);
  774. }
  775. public static string ObjectToXmlString(object o, Type t)
  776. {
  777. byte[] data = ObjectToXml(o, t);
  778. return Str.Utf8Encoding.GetString(data);
  779. }
  780. public static byte[] ObjectToXml(object o, Type t)
  781. {
  782. if (o == null)
  783. {
  784. return null;
  785. }
  786. MemoryStream ms = new MemoryStream();
  787. XmlSerializer x = new XmlSerializer(t);
  788. x.Serialize(ms, o);
  789. return ms.ToArray();
  790. }
  791. public static object XmlToObject(string str, Type t)
  792. {
  793. if (Str.IsEmptyStr(str))
  794. {
  795. return null;
  796. }
  797. byte[] data = Str.Utf8Encoding.GetBytes(str);
  798. return XmlToObject(data, t);
  799. }
  800. public static object XmlToObject(byte[] data, Type t)
  801. {
  802. if (data == null || data.Length == 0)
  803. {
  804. return null;
  805. }
  806. MemoryStream ms = new MemoryStream();
  807. ms.Write(data, 0, data.Length);
  808. ms.Position = 0;
  809. XmlSerializer x = new XmlSerializer(t);
  810. return x.Deserialize(ms);
  811. }
  812. public static void NoOP(object o)
  813. {
  814. }
  815. public static void NoOP()
  816. {
  817. }
  818. public static bool False
  819. {
  820. get
  821. {
  822. return false;
  823. }
  824. }
  825. public static bool True
  826. {
  827. get
  828. {
  829. return true;
  830. }
  831. }
  832. public static int Zero
  833. {
  834. get
  835. {
  836. return 0;
  837. }
  838. }
  839. public static object ByteToStruct(byte[] src, Type type)
  840. {
  841. int size = src.Length;
  842. if (size != SizeOfStruct(type))
  843. {
  844. throw new SystemException("size error");
  845. }
  846. IntPtr p = Marshal.AllocHGlobal(size);
  847. try
  848. {
  849. Marshal.Copy(src, 0, p, size);
  850. return Marshal.PtrToStructure(p, type);
  851. }
  852. finally
  853. {
  854. Marshal.FreeHGlobal(p);
  855. }
  856. }
  857. public static byte[] StructToByte(object obj)
  858. {
  859. int size = SizeOfStruct(obj);
  860. IntPtr p = Marshal.AllocHGlobal(size);
  861. try
  862. {
  863. Marshal.StructureToPtr(obj, p, false);
  864. byte[] ret = new byte[size];
  865. Marshal.Copy(p, ret, 0, size);
  866. return ret;
  867. }
  868. finally
  869. {
  870. Marshal.FreeHGlobal(p);
  871. }
  872. }
  873. public static int SizeOfStruct(object obj)
  874. {
  875. return Marshal.SizeOf(obj);
  876. }
  877. public static int SizeOfStruct(Type type)
  878. {
  879. return Marshal.SizeOf(type);
  880. }
  881. public static XmlAndXsd GenerateXmlAndXsd(object obj)
  882. {
  883. XmlAndXsd ret = new XmlAndXsd();
  884. Type type = obj.GetType();
  885. ret.XsdFileName = Str.MakeSafeFileName(type.Name + ".xsd");
  886. ret.XsdData = GetXmlSchemaFromType(type);
  887. ret.XmlFileName = Str.MakeSafeFileName(type.Name + ".xml");
  888. string str = Util.ObjectToXmlString(obj);
  889. str = str.Replace(
  890. "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"",
  891. "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:noNamespaceSchemaLocation=\""
  892. + ret.XsdFileName
  893. + "\"");
  894. ret.XmlData = Str.Utf8Encoding.GetBytes(str);
  895. return ret;
  896. }
  897. }
  898. public class XmlAndXsd
  899. {
  900. public byte[] XmlData;
  901. public byte[] XsdData;
  902. public string XmlFileName;
  903. public string XsdFileName;
  904. }
  905. }
  906. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  907. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  908. // Join us: http://www.tsukuba.ac.jp/english/admission/