Util.cs 23 KB

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