Compress.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 CoreUtil.Internal;
  92. namespace CoreUtil
  93. {
  94. public static class ZLib
  95. {
  96. public static byte[] Compress(byte[] src)
  97. {
  98. return Compress(src, zlibConst.Z_DEFAULT_COMPRESSION);
  99. }
  100. public static byte[] Compress(byte[] src, int level)
  101. {
  102. return Compress(src, level, false);
  103. }
  104. public static byte[] Compress(byte[] src, int level, bool noHeader)
  105. {
  106. int dstSize = src.Length * 2 + 100;
  107. byte[] dst = new byte[dstSize];
  108. compress2(ref dst, src, level, noHeader);
  109. return dst;
  110. }
  111. public static byte[] Uncompress(byte[] src, int originalSize)
  112. {
  113. byte[] dst = new byte[originalSize];
  114. uncompress(ref dst, src);
  115. return dst;
  116. }
  117. static void compress2(ref byte[] dest, byte[] src, int level, bool noHeader)
  118. {
  119. ZStream stream = new ZStream();
  120. stream.next_in = src;
  121. stream.avail_in = src.Length;
  122. stream.next_out = dest;
  123. stream.avail_out = dest.Length;
  124. if (noHeader == false)
  125. {
  126. stream.deflateInit(level);
  127. }
  128. else
  129. {
  130. stream.deflateInit(level, -15);
  131. }
  132. stream.deflate(zlibConst.Z_FINISH);
  133. Array.Resize<byte>(ref dest, (int)stream.total_out);
  134. }
  135. static void uncompress(ref byte[] dest, byte[] src)
  136. {
  137. ZStream stream = new ZStream();
  138. stream.next_in = src;
  139. stream.avail_in = src.Length;
  140. stream.next_out = dest;
  141. stream.avail_out = dest.Length;
  142. stream.inflateInit();
  143. int err = stream.inflate(zlibConst.Z_FINISH);
  144. if (err != zlibConst.Z_STREAM_END)
  145. {
  146. stream.inflateEnd();
  147. throw new ApplicationException();
  148. }
  149. Array.Resize<byte>(ref dest, (int)stream.total_out);
  150. err = stream.inflateEnd();
  151. if (err != zlibConst.Z_OK)
  152. {
  153. throw new ApplicationException();
  154. }
  155. }
  156. }
  157. }
  158. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  159. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  160. // Join us: http://www.tsukuba.ac.jp/english/admission/