Buf.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  48. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  49. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  50. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  51. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  52. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  53. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  54. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  55. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  56. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  57. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  58. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  59. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  60. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  61. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  62. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  63. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  64. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  65. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  66. //
  67. //
  68. // SOURCE CODE CONTRIBUTION
  69. // ------------------------
  70. //
  71. // Your contribution to SoftEther VPN Project is much appreciated.
  72. // Please send patches to us through GitHub.
  73. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  74. // http://www.softether.org/5-download/src/9.patch
  75. //
  76. //
  77. // DEAR SECURITY EXPERTS
  78. // ---------------------
  79. //
  80. // If you find a bug or a security vulnerability please kindly inform us
  81. // about the problem immediately so that we can fix the security problem
  82. // to protect a lot of users around the world as soon as possible.
  83. //
  84. // Our e-mail address for security reports is:
  85. // softether-vpn-security [at] softether.org
  86. //
  87. // Please note that the above e-mail address is not a technical support
  88. // inquiry address. If you need technical assistance, please visit
  89. // http://www.softether.org/ and ask your question on the users forum.
  90. //
  91. // Thank you for your cooperation.
  92. //
  93. //
  94. // NO MEMORY OR RESOURCE LEAKS
  95. // ---------------------------
  96. //
  97. // The memory-leaks and resource-leaks verification under the stress
  98. // test has been passed before release this source code.
  99. using System;
  100. using System.Threading;
  101. using System.Data;
  102. using System.Data.Sql;
  103. using System.Data.SqlClient;
  104. using System.Data.SqlTypes;
  105. using System.Text;
  106. using System.Configuration;
  107. using System.Collections;
  108. using System.Collections.Generic;
  109. using System.Security.Cryptography;
  110. using System.Web;
  111. using System.Web.Security;
  112. using System.Web.UI;
  113. using System.Web.UI.WebControls;
  114. using System.Web.UI.WebControls.WebParts;
  115. using System.Web.UI.HtmlControls;
  116. using System.IO;
  117. using System.Drawing;
  118. using System.Drawing.Imaging;
  119. using System.Drawing.Drawing2D;
  120. using System.Web.Mail;
  121. namespace CoreUtil
  122. {
  123. // FIFO
  124. public class Fifo
  125. {
  126. byte[] p;
  127. int pos, size;
  128. public int Size
  129. {
  130. get { return size; }
  131. }
  132. public byte[] Data
  133. {
  134. get
  135. {
  136. return this.p;
  137. }
  138. }
  139. public int DataOffset
  140. {
  141. get
  142. {
  143. return this.pos;
  144. }
  145. }
  146. public int PhysicalSize
  147. {
  148. get
  149. {
  150. return p.Length;
  151. }
  152. }
  153. int reallocMemSize;
  154. public const int FifoInitMemSize = 4096;
  155. public const int FifoReallocMemSize = 65536;
  156. public const int FifoReallocMemSizeSmall = 65536;
  157. long totalWriteSize = 0, totalReadSize = 0;
  158. public long TotalReadSize
  159. {
  160. get { return totalReadSize; }
  161. }
  162. public long TotalWriteSize
  163. {
  164. get { return totalWriteSize; }
  165. }
  166. public Fifo()
  167. {
  168. init(0);
  169. }
  170. public Fifo(int reallocMemSize)
  171. {
  172. init(reallocMemSize);
  173. }
  174. void init(int reallocMemSize)
  175. {
  176. if (reallocMemSize == 0)
  177. {
  178. reallocMemSize = FifoReallocMemSize;
  179. }
  180. this.size = this.pos = 0;
  181. this.reallocMemSize = reallocMemSize;
  182. this.p = new byte[FifoInitMemSize];
  183. }
  184. public void Write(Buf buf)
  185. {
  186. Write(buf.ByteData);
  187. }
  188. public void Write(byte[] src)
  189. {
  190. Write(src, src.Length);
  191. }
  192. public void SkipWrite(int size)
  193. {
  194. Write(null, size);
  195. }
  196. public void Write(byte[] src, int size)
  197. {
  198. Write(src, 0, size);
  199. }
  200. public void Write(byte[] src, int offset, int size)
  201. {
  202. int i, need_size;
  203. bool realloc_flag;
  204. i = this.size;
  205. this.size += size;
  206. need_size = this.pos + this.size;
  207. realloc_flag = false;
  208. int memsize = p.Length;
  209. while (need_size > memsize)
  210. {
  211. memsize = Math.Max(memsize, FifoInitMemSize) * 3;
  212. realloc_flag = true;
  213. }
  214. if (realloc_flag)
  215. {
  216. byte[] new_p = new byte[memsize];
  217. Util.CopyByte(new_p, 0, this.p, 0, this.p.Length);
  218. this.p = new_p;
  219. }
  220. if (src != null)
  221. {
  222. Util.CopyByte(this.p, this.pos + i, src, offset, size);
  223. }
  224. totalWriteSize += size;
  225. }
  226. public byte[] Read()
  227. {
  228. return Read(this.Size);
  229. }
  230. public void ReadToBuf(Buf buf, int size)
  231. {
  232. byte[] data = Read(size);
  233. buf.Write(data);
  234. }
  235. public Buf ReadToBuf(int size)
  236. {
  237. byte[] data = Read(size);
  238. return new Buf(data);
  239. }
  240. public byte[] Read(int size)
  241. {
  242. byte[] ret = new byte[size];
  243. int read_size = Read(ret);
  244. Array.Resize<byte>(ref ret, read_size);
  245. return ret;
  246. }
  247. public int Read(byte[] dst)
  248. {
  249. return Read(dst, dst.Length);
  250. }
  251. public int SkipRead(int size)
  252. {
  253. return Read(null, size);
  254. }
  255. public int Read(byte[] dst, int size)
  256. {
  257. return Read(dst, 0, size);
  258. }
  259. public int Read(byte[] dst, int offset, int size)
  260. {
  261. int read_size;
  262. read_size = Math.Min(size, this.size);
  263. if (read_size == 0)
  264. {
  265. return 0;
  266. }
  267. if (dst != null)
  268. {
  269. Util.CopyByte(dst, offset, this.p, this.pos, size);
  270. }
  271. this.pos += read_size;
  272. this.size -= read_size;
  273. if (this.size == 0)
  274. {
  275. this.pos = 0;
  276. }
  277. if (this.pos >= FifoInitMemSize &&
  278. this.p.Length >= this.reallocMemSize &&
  279. (this.p.Length / 2) > this.size)
  280. {
  281. byte[] new_p;
  282. int new_size;
  283. new_size = Math.Max(this.p.Length / 2, FifoInitMemSize);
  284. new_p = new byte[new_size];
  285. Util.CopyByte(new_p, 0, this.p, this.pos, this.size);
  286. this.p = new_p;
  287. this.pos = 0;
  288. }
  289. totalReadSize += read_size;
  290. return read_size;
  291. }
  292. }
  293. public class Buf
  294. {
  295. MemoryStream buf;
  296. public Buf()
  297. {
  298. init(new byte[0]);
  299. }
  300. public Buf(byte[] data)
  301. {
  302. init(data);
  303. }
  304. void init(byte[] data)
  305. {
  306. buf = new MemoryStream();
  307. Write(data);
  308. SeekToBegin();
  309. }
  310. public void Clear()
  311. {
  312. buf.SetLength(0);
  313. }
  314. public void WriteByte(byte data)
  315. {
  316. byte[] a = new byte[1] { data };
  317. Write(a);
  318. }
  319. public void Write(byte[] data)
  320. {
  321. buf.Write(data, 0, data.Length);
  322. }
  323. public void Write(byte[] data, int pos, int len)
  324. {
  325. buf.Write(data, pos, len);
  326. }
  327. public uint Size
  328. {
  329. get
  330. {
  331. return (uint)buf.Length;
  332. }
  333. }
  334. public uint Pos
  335. {
  336. get
  337. {
  338. return (uint)buf.Position;
  339. }
  340. }
  341. public byte[] ByteData
  342. {
  343. get
  344. {
  345. return buf.ToArray();
  346. }
  347. }
  348. public byte this[uint i]
  349. {
  350. get
  351. {
  352. return buf.GetBuffer()[i];
  353. }
  354. set
  355. {
  356. buf.GetBuffer()[i] = value;
  357. }
  358. }
  359. public byte[] Read()
  360. {
  361. return Read(this.Size);
  362. }
  363. public byte[] Read(uint size)
  364. {
  365. byte[] tmp = new byte[size];
  366. int i = buf.Read(tmp, 0, (int)size);
  367. byte[] ret = new byte[i];
  368. Array.Copy(tmp, 0, ret, 0, i);
  369. return ret;
  370. }
  371. public byte ReadByte()
  372. {
  373. byte[] a = Read(1);
  374. return a[0];
  375. }
  376. public void SeekToBegin()
  377. {
  378. Seek(0);
  379. }
  380. public void SeekToEnd()
  381. {
  382. Seek(0, SeekOrigin.End);
  383. }
  384. public void Seek(uint offset)
  385. {
  386. Seek(offset, SeekOrigin.Begin);
  387. }
  388. public void Seek(uint offset, SeekOrigin mode)
  389. {
  390. buf.Seek(offset, mode);
  391. }
  392. public ushort ReadShort()
  393. {
  394. byte[] data = Read(2);
  395. if (data.Length != 2)
  396. {
  397. return 0;
  398. }
  399. if (Env.IsLittleEndian)
  400. {
  401. Array.Reverse(data);
  402. }
  403. return BitConverter.ToUInt16(data, 0);
  404. }
  405. public ushort RawReadShort()
  406. {
  407. byte[] data = Read(2);
  408. if (data.Length != 2)
  409. {
  410. return 0;
  411. }
  412. return BitConverter.ToUInt16(data, 0);
  413. }
  414. public uint ReadInt()
  415. {
  416. byte[] data = Read(4);
  417. if (data.Length != 4)
  418. {
  419. return 0;
  420. }
  421. if (Env.IsLittleEndian)
  422. {
  423. Array.Reverse(data);
  424. }
  425. return BitConverter.ToUInt32(data, 0);
  426. }
  427. public uint RawReadInt()
  428. {
  429. byte[] data = Read(4);
  430. if (data.Length != 4)
  431. {
  432. return 0;
  433. }
  434. return BitConverter.ToUInt32(data, 0);
  435. }
  436. public ulong ReadInt64()
  437. {
  438. byte[] data = Read(8);
  439. if (data.Length != 8)
  440. {
  441. return 0;
  442. }
  443. if (Env.IsLittleEndian)
  444. {
  445. Array.Reverse(data);
  446. }
  447. return BitConverter.ToUInt64(data, 0);
  448. }
  449. public ulong RawReadInt64()
  450. {
  451. byte[] data = Read(8);
  452. if (data.Length != 8)
  453. {
  454. return 0;
  455. }
  456. return BitConverter.ToUInt64(data, 0);
  457. }
  458. public string ReadStr()
  459. {
  460. return ReadStr(false);
  461. }
  462. public string ReadStr(bool include_null)
  463. {
  464. uint len = ReadInt();
  465. byte[] data = Read(len - (uint)(include_null ? 1 : 0));
  466. return Str.ShiftJisEncoding.GetString(data);
  467. }
  468. public string ReadUniStr()
  469. {
  470. return ReadUniStr(false);
  471. }
  472. public string ReadUniStr(bool include_null)
  473. {
  474. uint len = ReadInt();
  475. if (len == 0)
  476. {
  477. return null;
  478. }
  479. byte[] data = Read(len - (uint)(include_null ? 2 : 0));
  480. return Str.Utf8Encoding.GetString(data);
  481. }
  482. public void WriteShort(ushort shortValue)
  483. {
  484. byte[] data = BitConverter.GetBytes(shortValue);
  485. if (Env.IsLittleEndian)
  486. {
  487. Array.Reverse(data);
  488. }
  489. Write(data);
  490. }
  491. public void RawWriteShort(ushort shortValue)
  492. {
  493. byte[] data = BitConverter.GetBytes(shortValue);
  494. Write(data);
  495. }
  496. public void WriteInt(uint intValue)
  497. {
  498. byte[] data = BitConverter.GetBytes(intValue);
  499. if (Env.IsLittleEndian)
  500. {
  501. Array.Reverse(data);
  502. }
  503. Write(data);
  504. }
  505. public void RawWriteInt(uint intValue)
  506. {
  507. byte[] data = BitConverter.GetBytes(intValue);
  508. Write(data);
  509. }
  510. public void WriteInt64(ulong int64Value)
  511. {
  512. byte[] data = BitConverter.GetBytes(int64Value);
  513. if (Env.IsLittleEndian)
  514. {
  515. Array.Reverse(data);
  516. }
  517. Write(data);
  518. }
  519. public void RawWriteInt64(ulong int64Value)
  520. {
  521. byte[] data = BitConverter.GetBytes(int64Value);
  522. Write(data);
  523. }
  524. public string ReadNextLineAsString()
  525. {
  526. return ReadNextLineAsString(Str.Utf8Encoding);
  527. }
  528. public string ReadNextLineAsString(Encoding encoding)
  529. {
  530. byte[] ret = ReadNextLineAsData();
  531. if (ret == null)
  532. {
  533. return null;
  534. }
  535. return encoding.GetString(ret);
  536. }
  537. public byte[] ReadNextLineAsData()
  538. {
  539. if (this.Size <= this.Pos)
  540. {
  541. return null;
  542. }
  543. long pos = buf.Position;
  544. long i;
  545. byte[] tmp = new byte[1];
  546. for (i = pos; i < buf.Length; i++)
  547. {
  548. buf.Read(tmp, 0, 1);
  549. if (tmp[0] == 13 || tmp[0] == 10)
  550. {
  551. if (tmp[0] == 13)
  552. {
  553. if ((i + 2) < buf.Length)
  554. {
  555. i++;
  556. }
  557. }
  558. break;
  559. }
  560. }
  561. long len = i - pos;
  562. buf.Position = pos;
  563. byte[] ret = Read((uint)((int)len));
  564. try
  565. {
  566. Seek(1, SeekOrigin.Current);
  567. }
  568. catch
  569. {
  570. }
  571. if (ret.Length >= 1 && ret[ret.Length - 1] == 13)
  572. {
  573. Array.Resize<byte>(ref ret, ret.Length - 1);
  574. }
  575. return ret;
  576. }
  577. public void WriteStr(string strValue)
  578. {
  579. WriteStr(strValue, false);
  580. }
  581. public void WriteStr(string strValue, bool include_null)
  582. {
  583. byte[] data = Str.ShiftJisEncoding.GetBytes(strValue);
  584. WriteInt((uint)data.Length + (uint)(include_null ? 1 : 0));
  585. Write(data);
  586. }
  587. public void WriteUniStr(string strValue)
  588. {
  589. WriteUniStr(strValue, false);
  590. }
  591. public void WriteUniStr(string strValue, bool include_null)
  592. {
  593. byte[] data = Str.Utf8Encoding.GetBytes(strValue);
  594. WriteInt((uint)data.Length + (uint)(include_null ? 2 : 0));
  595. Write(data);
  596. }
  597. public static Buf ReadFromFile(string filename)
  598. {
  599. return new Buf(IO.ReadFile(filename));
  600. }
  601. public void WriteToFile(string filename)
  602. {
  603. IO.SaveFile(filename, this.ByteData);
  604. }
  605. public static Buf ReadFromStream(Stream st)
  606. {
  607. Buf ret = new Buf();
  608. int size = 32767;
  609. while (true)
  610. {
  611. byte[] tmp = new byte[size];
  612. int i = st.Read(tmp, 0, tmp.Length);
  613. if (i <= 0)
  614. {
  615. break;
  616. }
  617. Array.Resize<byte>(ref tmp, i);
  618. ret.Write(tmp);
  619. }
  620. ret.SeekToBegin();
  621. return ret;
  622. }
  623. }
  624. }
  625. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  626. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  627. // Join us: http://www.tsukuba.ac.jp/english/admission/