mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 17:10:20 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc6c8473d6 | ||
|
|
815b0998c7 | ||
|
|
32fe1cc2a2 | ||
|
|
0f62bbab30 | ||
|
|
f5af57621e | ||
|
|
a58b26be52 | ||
|
|
6bdff04c53 | ||
|
|
6e0bf6461e | ||
|
|
22a852a834 | ||
|
|
4837d2aeda | ||
|
|
5388febb0d | ||
|
|
d8a85c658c | ||
|
|
7249a78449 | ||
|
|
4de19ff60d | ||
|
|
6cf1cd4875 | ||
|
|
7c9c58a074 | ||
|
|
3745b4498a | ||
|
|
70aa518b5b | ||
|
|
562bf06fac | ||
|
|
a823dba480 | ||
|
|
ad6f78a87a | ||
|
|
e8646f87f5 | ||
|
|
ea21a88cf5 | ||
|
|
f5253e8cd2 | ||
|
|
f0467745ce | ||
|
|
d7ab125adb | ||
|
|
1d402fd23b | ||
|
|
70308609be | ||
|
|
b4137a6590 | ||
|
|
b99f1dfa0b | ||
|
|
70b2f8cb22 | ||
|
|
05bc7718e1 | ||
|
|
58dc608f72 | ||
|
|
1b50e22003 | ||
|
|
75e221ebc0 | ||
|
|
18c931d4a4 | ||
|
|
8d979f1d63 | ||
|
|
811c42fb59 | ||
|
|
d193ac5650 | ||
|
|
ff27b5062d | ||
|
|
fea6edcaa6 | ||
|
|
10ca74767f | ||
|
|
7d059777c3 | ||
|
|
df1971778d | ||
|
|
edd1a2ed03 | ||
|
|
c9b36989e5 | ||
|
|
0ebd723f3d | ||
|
|
ba2c0d13f4 | ||
|
|
065c9875d8 | ||
|
|
23dc65d036 | ||
|
|
dc8fc64ad0 | ||
|
|
c91733841b | ||
|
|
26b0823111 | ||
|
|
b2102cdaae | ||
|
|
7d910decd1 | ||
|
|
1ed76d1bd6 | ||
|
|
d840117172 | ||
|
|
6701757fca | ||
|
|
394fcbfa43 | ||
|
|
1de5080573 | ||
|
|
8a84316e5f | ||
|
|
ef0cdbc087 | ||
|
|
e0dc4d545e | ||
|
|
46908626ab | ||
|
|
1e802f2552 | ||
|
|
f3609d9a4f | ||
|
|
5ea869194c | ||
|
|
8f389f0504 | ||
|
|
cb9e18ec9d | ||
|
|
b585e440d2 | ||
|
|
4e4824afab | ||
|
|
7f0542b133 | ||
|
|
9f187b5dd0 |
No files matched your search
@@ -1,3 +1,84 @@
|
||||
Hello everyone!
|
||||
|
||||
We are releasing a new version of Mono, Mono 0.22. A new release
|
||||
is made today because of the few recent bug-fixes that were committed
|
||||
to CVS.
|
||||
|
||||
Source code and binaries for this release can be found on the
|
||||
web page,
|
||||
|
||||
http://www.go-mono.com/download.html
|
||||
|
||||
The URLs for the sources are:
|
||||
|
||||
* MCS package (the Class Libraries, C# and VB.NET compiler
|
||||
and other assorted tools written in Managed code):
|
||||
|
||||
http://www.go-mono.com/archive/mcs-0.22.tar.gz
|
||||
|
||||
* Mono package (the Runtime engine and JIT compiler):
|
||||
|
||||
http://www.go-mono.com/archive/mono-0.22.tar.gz
|
||||
|
||||
RPM packages for this release can be downloaded from the web-page
|
||||
as well as from the 'Mono' channel on Red Carpet. Debian packages will
|
||||
appear on the download page later, as well as an installer for our
|
||||
Windows users.
|
||||
|
||||
Since last Thursday, 320 commits have been made to our CVS
|
||||
repository. These following hackers contributed to Mono since version
|
||||
0.21:
|
||||
|
||||
Aleksey Demakov, Alexandre Pigolkine, Atsushi Enomoto, Elan
|
||||
Feingeld, Dick Porter, Dietmar Maurer, Duncan Mak, Gonzalo
|
||||
Paniagua, Ian MacLean, Jackson Harper, Jean-Marc Andre, Jerome
|
||||
Laban, Lluis Sanchez, Martin Baulig, Miguel de Icaza, Nick
|
||||
Drochak, Paolo Molaro, Pedro Martinez, Per Ameng, Peter Williams,
|
||||
Rafael Teixeira, Reggie Burnett, Sebastien Pouliot, Tim Coleman
|
||||
and Zoltan Varga.
|
||||
|
||||
Highlights:
|
||||
|
||||
* The "MemoryStream" bug.
|
||||
This bug affected a lot of classes, and made them crashy,
|
||||
database code, XML parsing and a few others were
|
||||
crashing. Thanks to Gonzalo for fixing this bug.
|
||||
|
||||
* System.Data:
|
||||
More bug fixes from Aleksey and Tim.
|
||||
|
||||
* Reflection:
|
||||
Zoltan continues to provide fixes to our Reflection.Emit code
|
||||
to host IKVM.
|
||||
|
||||
* Remoting:
|
||||
Lluis added support for activation using activation
|
||||
attributes.
|
||||
|
||||
* PEToolkit:
|
||||
Jackson imported the PEAPI package from the Queensland
|
||||
University of Technology in Australia. This will replace the
|
||||
existing Mono.PEToolkit for our ILasm back-end.
|
||||
|
||||
* Windows Forms:
|
||||
More fixes from Reggie and Alexandre.
|
||||
|
||||
* System.Web.Mail:
|
||||
Per has been working on this namespace. He announces recently
|
||||
that all major parts of System.Web.Mail has now been implemented.
|
||||
|
||||
* System.Web.Mobile:
|
||||
Gaurav continues to make progress here.
|
||||
|
||||
* Misc:
|
||||
|
||||
Ian MacLean contributed a /compile flag to monoresgen and
|
||||
assorted bug-fixes and improvements from the rest of the team.
|
||||
|
||||
|
||||
My name is Duncan Mak, and I just made my first Mono release.
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
Hello everyone!
|
||||
|
||||
The Mono Team introduces the best Mono release so far we have
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
AC_INIT(README)
|
||||
AC_CANONICAL_SYSTEM
|
||||
AM_CONFIG_HEADER(config.h)
|
||||
AM_INIT_AUTOMAKE(mono, 0.21)
|
||||
AM_INIT_AUTOMAKE(mono, 0.23)
|
||||
AM_MAINTAINER_MODE
|
||||
|
||||
#
|
||||
|
||||
+179
-109
@@ -16,10 +16,137 @@
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.22</b><br>
|
||||
<a href="archive/mono-0.22">Release notes</a><br>
|
||||
March 5, 2003
|
||||
</td>
|
||||
<td>
|
||||
<table cellspacing="1" cellpadding="3">
|
||||
<tr bgcolor="#BBBBBB">
|
||||
<td>
|
||||
<b>Source Code</b>
|
||||
</td>
|
||||
<td>
|
||||
<ul>
|
||||
<li><a href="archive/mcs-0.22.tar.gz">Mono Class Libraries and C# Compiler.</a>
|
||||
<li><a href="archive/mono-0.22.tar.gz">Mono Runtime</a>
|
||||
<li><a href="archive/gc6.1.tar.gz">Boehm GC 6.1</a>
|
||||
<!-- <li><a href="archive/xsp-0.3.tar.gz">XSP web server (0.3)</a> -->
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr bgcolor="#BBBBBB">
|
||||
<td>
|
||||
<b>Source RPMs</b><br>
|
||||
(No RPMS for XSP).
|
||||
</td>
|
||||
<td>
|
||||
<ul>
|
||||
<li><a href="archive/libgc-6.1-1.src.rpm">libgc-6.1-1.src.rpm</a>
|
||||
<li><a href="archive/mono-0.22-1.src.rpm">mono-0.22-1.src.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.3/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-73-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/mono-0.22-1.i386.rpm">mono-0.22-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/mono-devel-0.22-1.i386.rpm">mono-devel-0.22-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.2/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-72-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/mono-0.22-1.i386.rpm">mono-0.22-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/mono-devel-0.22-1.i386.rpm">mono-devel-0.22-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.1/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-71-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/mono-0.22-1.i386.rpm">mono-0.22-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/mono-devel-0.22-1.i386.rpm">mono-devel-0.22-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Mandrake 8.2/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/mandrake-82-i386/libgc-6.1-1.i586.rpm">libgc-6.1-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/libgc-devel-6.1-1.i586.rpm">libgc-devel-6.1-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/mono-0.22-1.i586.rpm">mono-0.22-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/mono-devel-0.22-1.i586.rpm">mono-devel-0.22-1.i586.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>SuSE 8.0/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/suse-80-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/mono-0.22-1.i386.rpm">mono-0.22-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/mono-devel-0.22-1.i386.rpm">mono-devel-0.22-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<!-- <tr> -->
|
||||
<!-- <td bgcolor="#999999"> -->
|
||||
<!-- <b>Debian packages</b> -->
|
||||
<!-- <ul> -->
|
||||
<!-- <li><a href="http://www.atoker.com/mono/">Alp's web site</a> -->
|
||||
<!-- </ul> -->
|
||||
<!-- </td> -->
|
||||
<!-- </tr> -->
|
||||
<tr> -->
|
||||
<td bgcolor="#999999">
|
||||
<b>Windows Setup Wizard (NT/2000/XP)</b>
|
||||
<ul>
|
||||
<li><a href="archive/mono-0.22-win32-1.exe">Mono-Setup</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<tr bgcolor="#CCCCC">
|
||||
<td>
|
||||
<b>Regression Tests</b><br>
|
||||
</td>
|
||||
<td>
|
||||
You can get binaries for the Mono Regression Test
|
||||
Suite <a href="archive/mono-tests.tar.gz">here</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
der Releases:
|
||||
|
||||
We provide binaries for older releases until we have packages for the new release.
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.21</b><br>
|
||||
<a href="archive/mono-0.21">Release notes</a><br>
|
||||
Jan 21th, 2003
|
||||
Feb 27th, 2003
|
||||
</td>
|
||||
<td>
|
||||
<table cellspacing="1" cellpadding="3">
|
||||
@@ -53,71 +180,20 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.3/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-73-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/mono-0.21-1.i386.rpm">mono-0.21-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-73-i386/mono-devel-0.21-1.i386.rpm">mono-devel-0.21-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.2/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-72-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/mono-0.21-1.i386.rpm">mono-0.21-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-72-i386/mono-devel-0.21-1.i386.rpm">mono-devel-0.21-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 7.1/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-71-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/mono-0.21-1.i386.rpm">mono-0.21-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-71-i386/mono-devel-0.21-1.i386.rpm">mono-devel-0.21-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Mandrake 8.2/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/mandrake-82-i386/libgc-6.1-1.i586.rpm">libgc-6.1-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/libgc-devel-6.1-1.i586.rpm">libgc-devel-6.1-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/mono-0.21-1.i586.rpm">mono-0.21-1.i586.rpm</a>
|
||||
<li><a href="archive/mandrake-82-i386/mono-devel-0.21-1.i586.rpm">mono-devel-0.21-1.i586.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>SuSE 8.0/x86</b>
|
||||
<ul>
|
||||
<li><a href="archive/suse-80-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/mono-0.21-1.i386.rpm">mono-0.21-1.i386.rpm</a>
|
||||
<li><a href="archive/suse-80-i386/mono-devel-0.21-1.i386.rpm">mono-devel-0.21-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Debian packages</b>
|
||||
<b>Debian packages</b>
|
||||
<ul>
|
||||
<li><a href="http://www.atoker.com/mono/">Alp's web site</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Windows Setup Wizard (NT/2000/XP)</b>
|
||||
<ul>
|
||||
<li><a href="archive/mono-0.21-win32-1.exe">Mono-Setup</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -135,19 +211,13 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
<br>
|
||||
* Older Releases:
|
||||
|
||||
We provide binaries for older releases until we have packages for the new release.
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.20</b><br>
|
||||
<a href="archive/mono-0.20">Release notes</a><br>
|
||||
Jan 20th, 2003
|
||||
Feb 20th, 2003
|
||||
</td>
|
||||
<td>
|
||||
<table cellspacing="1" cellpadding="3">
|
||||
@@ -182,28 +252,28 @@
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat 8.0/x86</b>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-80-i386/libgc-6.1-1.i386.rpm">libgc-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-80-i386/libgc-devel-6.1-1.i386.rpm">libgc-devel-6.1-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-80-i386/mono-0.20-1.i386.rpm">mono-0.20-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-80-i386/mono-devel-0.20-1.i386.rpm">mono-devel-0.20-1.i386.rpm</a>
|
||||
</ul>
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Debian packages</b>
|
||||
<td bgcolor="#999999">
|
||||
<b>Debian packages</b>
|
||||
<ul>
|
||||
<li><a href="http://www.atoker.com/mono/">Alp's web site</a>
|
||||
</ul>
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Windows Setup Wizard (NT/2000/XP)</b>
|
||||
<ul>
|
||||
<li><a href="archive/mono-0.20-stable-win32-2.exe">Mono-Setup</a>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -223,7 +293,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.19</b><br>
|
||||
<a href="archive/mono-0.19">Release notes</a><br>
|
||||
@@ -265,7 +335,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.18</b><br>
|
||||
<a href="archive/mono-0.18">Release notes</a><br>
|
||||
@@ -309,7 +379,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.17</b><br>
|
||||
<a href="archive/mono-0.17">Release notes</a><br>
|
||||
@@ -364,7 +434,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.16</b><br>
|
||||
<a href="archive/mono-0.16">Release notes</a><br>
|
||||
@@ -401,7 +471,7 @@
|
||||
<tr>
|
||||
<td bgcolor="#999999">
|
||||
<b>Linux s390</b>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><a href="archive/s390/glib2-2.0.6-13.s390.rpm">glib2-2.0.6-13.s390.rpm</a>
|
||||
<li><a href="archive/s390/glib2-devel-2.0.6-13.s390.rpm">glib2-devel-2.0.6-13.s390.rpm</a>
|
||||
<li><a href="archive/s390/libgc-6.1alpha5-1.s390.rpm">libgc-6.1alpha5-1.s390.rpm</a>
|
||||
@@ -422,7 +492,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<tr bgcolor="#DDDDDD">
|
||||
<td>
|
||||
<b>Release 0.15</b><br>
|
||||
<a href="archive/mono-0.15">Release notes</a><br>
|
||||
@@ -466,7 +536,7 @@
|
||||
</td>
|
||||
<td bgcolor="#999999">
|
||||
<b>Red Hat null-8.0/x86</b>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><a href="archive/redhat-null-i386/libgc-6.1alpha5-1.i386.rpm">libgc-6.1alpha5-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-null-i386/libgc-devel-6.1alpha5-1.i386.rpm">libgc-devel-6.1alpha5-1.i386.rpm</a>
|
||||
<li><a href="archive/redhat-null-i386/mono-0.15-1.i386.rpm">mono-0.15-1.i386.rpm</a>
|
||||
@@ -480,7 +550,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
** Binaries for other platforms.
|
||||
inaries for other platforms.
|
||||
|
||||
Already daily volunteer's made binaries:
|
||||
|
||||
@@ -489,9 +559,9 @@
|
||||
* <a href="http://www.superin.formativ.net/mono/mono.htm">Windows</a> (works without cygwin!)
|
||||
</ul>
|
||||
|
||||
* Snapshots
|
||||
apshots
|
||||
|
||||
<a name="snapshots">
|
||||
ame="snapshots">
|
||||
|
||||
<a href="http://www.go-mono.com/snapshots">Nightly snapshots</a> of
|
||||
the CVS repository are made every day at 10pm EST (Boston
|
||||
@@ -501,7 +571,7 @@
|
||||
The <a href="anoncvs.html">anoncvs</a> mirrors provided by
|
||||
Hispalinux are updated every six hours.
|
||||
|
||||
* Compiling the code
|
||||
mpiling the code
|
||||
|
||||
There are a number of ways of compiling Mono
|
||||
|
||||
@@ -515,8 +585,8 @@
|
||||
|
||||
(which is an easy way to get the latest CVS information)
|
||||
|
||||
<a name="scripts">
|
||||
* Compiling with scripts
|
||||
ame="scripts">
|
||||
mpiling with scripts
|
||||
|
||||
There are scripts to help build mono for both Unix and Windows. Get
|
||||
<a href="mono-build.sh">mono-build.sh</a> for Unix, or <a
|
||||
@@ -547,14 +617,14 @@
|
||||
set your CVSROOT environment variable before running the script to
|
||||
select a particular cvs server.
|
||||
|
||||
<a name="install">
|
||||
** Building the software manually
|
||||
ame="install">
|
||||
uilding the software manually
|
||||
|
||||
You will need to obtain the Mono dependencies first: <a
|
||||
href="http://www.gtk.org">glib 2.x</a> and
|
||||
<a href="http://www.freedesktop.org/software/pkgconfig">pkg-config</a>.
|
||||
|
||||
*** Building on Linux
|
||||
Building on Linux
|
||||
|
||||
<ul>
|
||||
Unpack the Mono runtime distribution:
|
||||
@@ -596,9 +666,9 @@
|
||||
</ul>
|
||||
|
||||
|
||||
*** On Windows, to install and work on the compiler and the class libraries:
|
||||
On Windows, to install and work on the compiler and the class libraries:
|
||||
|
||||
**** Install Cygwin and the GNU build tools, Microsoft's .NET Framework SDK and the precompiled GLIB 2.0 and pkg-config libraries.
|
||||
Install Cygwin and the GNU build tools, Microsoft's .NET Framework SDK and the precompiled GLIB 2.0 and pkg-config libraries.
|
||||
|
||||
<ul>
|
||||
|
||||
@@ -625,9 +695,9 @@
|
||||
<pre>
|
||||
make
|
||||
</pre>
|
||||
</ul>
|
||||
</ul>
|
||||
|
||||
*** On Windows, to compile the mono runtime:
|
||||
On Windows, to compile the mono runtime:
|
||||
|
||||
<ul>
|
||||
|
||||
@@ -646,7 +716,7 @@
|
||||
|
||||
</ul>
|
||||
|
||||
* Software resources and notes
|
||||
ftware resources and notes
|
||||
|
||||
The required and additional software can be downloaded here:
|
||||
|
||||
@@ -681,7 +751,7 @@
|
||||
</ul>
|
||||
|
||||
|
||||
*** Notes on compiling GLIB 2.0 and pkg-config from source:
|
||||
Notes on compiling GLIB 2.0 and pkg-config from source:
|
||||
|
||||
|
||||
<ul>
|
||||
@@ -691,18 +761,18 @@
|
||||
`make maintainer-clean' after the update).
|
||||
|
||||
* Download, compile and install <a href="http://www.freedesktop.org/software/pkgconfig">pkg-config</a> from source.
|
||||
(I had to change line 674 of
|
||||
<nobr><tt>pkg-config-0.8.0/glib-1.2.8/gstrfuncs.c</tt></nobr> from
|
||||
(I had to change line 674 of
|
||||
<nobr><tt>pkg-config-0.8.0/glib-1.2.8/gstrfuncs.c</tt></nobr> from
|
||||
<nobr><tt>extern char *strsignal (int sig);</tt></nobr> to
|
||||
<nobr><tt>extern const char *strsignal (int sig);</tt></nobr>.
|
||||
|
||||
<pre>
|
||||
tar xzvf pkg-config-0.8.0.tar.gz
|
||||
cd pkg-config-0.8.0
|
||||
./configure --prefix=/usr
|
||||
make
|
||||
make install
|
||||
</pre>
|
||||
>
|
||||
xzvf pkg-config-0.8.0.tar.gz
|
||||
kg-config-0.8.0
|
||||
nfigure --prefix=/usr
|
||||
|
||||
install
|
||||
e>
|
||||
|
||||
* Download, compile and install <a href="ftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.7.tar.gz">libiconv</a> from source.<br>
|
||||
If you dont have MS Visual C/C++ 4.0 or 5.0 or 6.0 you can also try
|
||||
@@ -710,15 +780,15 @@ make install
|
||||
href="http://www.gimp.org/win32/libiconv-dev-20001007.zip">
|
||||
http://www.gimp.org/win32/libiconv-dev-20001007.zip</a>.
|
||||
|
||||
|
||||
|
||||
* Download, compile and install the
|
||||
<a href="ftp://ftp.gtk.org/pub/gtk/v2.0/glib-2.0.6.tar.gz">
|
||||
glib 2.0</a> from source.
|
||||
|
||||
</ul>
|
||||
|
||||
<a name="upgrading">
|
||||
** Upgrading
|
||||
ame="upgrading">
|
||||
pgrading
|
||||
|
||||
To upgrade your Mono installation from CVS, it is very
|
||||
important that you update your Mono and MCS modules from CVS
|
||||
|
||||
@@ -102,6 +102,19 @@
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
@item Mar 5th, 2003: Mono 0.22; MonoDoc 0.2; Debugger 0.2.1: Release-o-Rama.
|
||||
|
||||
Mono 0.22 has been released. See the <a
|
||||
href="archive/mono-0.22">release notes</a>. This is a bug fix
|
||||
release.
|
||||
|
||||
A new preview of MonoDoc 0.2, the Mono Documentation browser
|
||||
has been <a href="http://lists.ximian.com/archives/public/gtk-sharp-list/2003-March/001266.html">released</a>.
|
||||
|
||||
Martin also announced a <a
|
||||
href="http://lists.ximian.com/archives/public/mono-list/2003-March/012756.html">new
|
||||
release</a> of the Mono Debugger (both GUI and command line).
|
||||
|
||||
@item Mar 3rd, 2003: The Mono Hackers Hall Of Fame welcomes Zoltan Varga
|
||||
|
||||
The <a href="hackers.html">Mono Hackers Hall Of Fame</a>
|
||||
|
||||
+14
-2
@@ -119,7 +119,7 @@
|
||||
<table border=1>
|
||||
<tr>
|
||||
<td>
|
||||
<img src="http://go-mono.com/images/sqlsharp.png"></a>
|
||||
<img src="http://go-mono.com/images/sqlsharp.png">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -134,8 +134,20 @@
|
||||
<table border=1>
|
||||
<tr>
|
||||
<td>
|
||||
<img src="http://go-mono.com/images/skynet.png"></a>
|
||||
<img src="http://go-mono.com/images/skynet.png">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
** Windows.Forms
|
||||
|
||||
Windows.Forms running with Wine on Linux:
|
||||
|
||||
<table border=1>
|
||||
<tr>
|
||||
<td>
|
||||
<a href="http://go-mono.com/images/WINESWF.JPG"><img src="http://go-mono.com/images/WINESWF-thumb.JPG"></a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
* System.Windows.Forms
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
System.Windows.Forms is currently being implemented using the
|
||||
Win32 API, we will be using <a
|
||||
href="http://www.winehq.com">WineLib</a> on Unix systems to
|
||||
@@ -19,6 +22,12 @@
|
||||
Gtk/FrameBuffer is an option. If you have suggestions or
|
||||
recommendations, please let us <a
|
||||
href="mailto:[email protected]">let us know</a>
|
||||
</td>
|
||||
<td>
|
||||
<a href="images/WINESWF.JPG"><img src="images/WINESWF-mini.JPG"></a>
|
||||
</td>
|
||||
|
||||
</table>
|
||||
|
||||
* Contributing
|
||||
|
||||
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
|
||||
Threading in Mono
|
||||
=================
|
||||
|
||||
0. Terminology
|
||||
--------------
|
||||
|
||||
"Main thread" - The initial OS-native thread that the
|
||||
application started with.
|
||||
|
||||
"Helper thread" - A native thread created internally
|
||||
by the runtime, such as the finalizer thread, or an
|
||||
asynchronous delegate invocation thread. These
|
||||
threads can run managed code.
|
||||
|
||||
"Primary CLR thread" - The native thread that called
|
||||
the Main() method when executing an assembly.
|
||||
|
||||
"Secondary CLR thread" - A native thread created by a
|
||||
program that instantiates a System.Threading.Thread object
|
||||
and calls its Start() method.
|
||||
|
||||
1. Thread exit behaviour in the standalone mono runtime
|
||||
-------------------------------------------------------
|
||||
|
||||
The correct behaviour of the runtime should be:
|
||||
|
||||
a) If Main() returns, the runtime should wait for all
|
||||
foreground secondary CLR threads to finish. The
|
||||
wording in the class documentation states: "Once all
|
||||
foreground threads belonging to a process have
|
||||
terminated, the common language runtime ends the
|
||||
process by invoking Abort on any background threads
|
||||
that are still alive." Testing seems to indicate that
|
||||
the background thread can't cancel the Abort by
|
||||
catching the ThreadAbortException and calling
|
||||
ResetAbort here. Indeed, not even the finally block
|
||||
seems to be executed.
|
||||
|
||||
b) if any of the primary CLR thread, a secondary CLR
|
||||
thread or a helper thread calls
|
||||
System.Environment.Exit(), the application should
|
||||
terminate immediately without waiting for foreground
|
||||
primary or secondary CLR threads to finish.
|
||||
|
||||
c) if the primary CLR thread throws an uncaught
|
||||
exception, the application should terminate
|
||||
immediately without waiting for secondary CLR threads
|
||||
to finish. This might be implemented internally by
|
||||
pretending that all the running secondary CLR threads
|
||||
are background threads.
|
||||
|
||||
d) if a secondary CLR thread throws an uncaught
|
||||
exception that thread should terminate and all other
|
||||
threads should continue to execute.
|
||||
|
||||
e) if a helper thread throws an uncaught exception and
|
||||
that thread happens to be the GC finalizer thread,
|
||||
testing seems to indicate that the exception stack
|
||||
trace is displayed as normal, and the exception is
|
||||
then ignored (as though there is a try {} catch{}
|
||||
around all finalizers that just prints the stack
|
||||
trace.) Calling Abort() on the GC finalizer thread
|
||||
also does not cause it to exit: it behaves as though
|
||||
the ThreadAbortException is caught and ResetAbort is
|
||||
called. Asynchronous delegate helper threads should
|
||||
behave as secondary CLR threads, but uncaught
|
||||
exceptions should be rethrown on the thread that calls
|
||||
EndInvoke().
|
||||
|
||||
|
||||
The difficulties happen with cases b) and c):
|
||||
|
||||
The current implementation of
|
||||
System.Environment.Exit() calls exit(2) directly,
|
||||
which is rather unfriendly: it prevents any runtime
|
||||
cleanup, statistics gathering, etc. and is pretty
|
||||
obnoxious to embedded code.
|
||||
|
||||
The current exception handling code calls ExitThread()
|
||||
(emulated with pthread_exit() in the io-layer) if an
|
||||
exception is not caught.
|
||||
|
||||
When called from the main thread, both POSIX
|
||||
pthread_exit() and w32 ExitThread() block if there are
|
||||
other threads still running (in the w32 case, if there
|
||||
are other foreground threads still running; threads
|
||||
can set as background.) If the main thread is also
|
||||
the primary CLR thread, then the application will
|
||||
block until all other threads (including helper
|
||||
threads) terminate. Some helper threads will not
|
||||
terminate until specifically told to by the runtime:
|
||||
for example, the GC finalizer thread needs to run
|
||||
until all of the primary and secondary CLR threads
|
||||
have finished.
|
||||
|
||||
Also, if the main thread is also the primary CLR
|
||||
thread, the runtime loses the opportunity to do any
|
||||
cleaning up. Adding a special case to call exit(2)
|
||||
instead of ExitThread() in the primary CLR thread
|
||||
suffers from the same problems as
|
||||
System.Environment.Exit() calling exit(2).
|
||||
|
||||
|
||||
The simple solution is to run the primary CLR thread
|
||||
in a new native thread, leaving the main thread free
|
||||
for housekeeping duties. There still needs to be some
|
||||
special handling for the case where the primary CLR
|
||||
thread fails to catch an exception: the secondary CLR
|
||||
threads then need to be terminated.
|
||||
|
||||
When the primary and secondary CLR threads have all
|
||||
terminated, the helper threads can be killed off and
|
||||
the runtime can clean itself up and exit.
|
||||
|
||||
|
||||
|
||||
2. Thread initialisation
|
||||
------------------------
|
||||
|
||||
Threads have to undergo some initialisation before
|
||||
managed code can be executed. A
|
||||
System.Threading.Thread object must be created, and
|
||||
the thread details need to be stored so that the
|
||||
threads can be managed later. The JIT needs to record
|
||||
the last managed frame stack pointer in a TLS slot,
|
||||
and the current Thread object is also recorded.
|
||||
|
||||
New threads created by managed calls to
|
||||
System.Threading.Thread methods will have all needed
|
||||
initialisation performed. Threads created by the
|
||||
runtime with calls to mono_thread_create() will too.
|
||||
Existing threads can be passed to the runtime; these
|
||||
must call mono_thread_attach() before any CLR code can
|
||||
be executed on that thread.
|
||||
|
||||
|
||||
3. Constraints on embedding the Mono runtime
|
||||
--------------------------------------------
|
||||
|
||||
The discussion above concerning application behaviour
|
||||
in the event of threads terminating, whether by
|
||||
returning from the start function, throwing uncaught
|
||||
exceptions or by calling System.Environment.Exit(),
|
||||
only really applies to the standalone Mono runtime.
|
||||
|
||||
An embedding application should specify what behaviour
|
||||
is required when, for example,
|
||||
System.Environment.Exit() is called. The application
|
||||
is also responsible for its own thread management, and
|
||||
it should be prepared for any of the primary CLR
|
||||
thread or secondary CLR threads to terminate at any
|
||||
time. The application should also take into account
|
||||
that the runtime will create helper threads as needed,
|
||||
as this may cause pthread_exit() or ExitThread() to
|
||||
block indefinitely, as noted above.
|
||||
@@ -1,3 +1,11 @@
|
||||
2003-03-06 Jackson Harper <[email protected]>
|
||||
|
||||
* makefile.gnu: Add PEAPI to the build
|
||||
|
||||
2003-03-06 Sebastien Pouliot <[email protected]>
|
||||
|
||||
* library.build: added Mono.Security to build, test and clean.
|
||||
|
||||
2003-03-04 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* class/library.build:
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// AssemblyInfo.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: AssemblyTitle("SharpZipLibrary")]
|
||||
[assembly: AssemblyDescription("free C# zlib implementation")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("#ZipLibrary")]
|
||||
[assembly: AssemblyCopyright("(c) Mike Krueger 2001-2002")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
[assembly: AssemblyVersion("0.31.0.0")]
|
||||
|
||||
[assembly: AssemblyDelaySign(false)]
|
||||
[assembly: AssemblyKeyFile("SharpZipLib.key")]
|
||||
@@ -0,0 +1,98 @@
|
||||
// BZip2.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.BZip2 {
|
||||
|
||||
/// <summary>
|
||||
/// Does all the compress and decompress pre-operation stuff.
|
||||
/// Sets up the streams and file header characters.
|
||||
/// Uses multiply overloaded methods to call for the compress/decompress.
|
||||
/// </summary>
|
||||
public sealed class BZip2
|
||||
{
|
||||
public static void Decompress(Stream instream, Stream outstream)
|
||||
{
|
||||
BufferedStream bos = new BufferedStream(outstream);
|
||||
BufferedStream bis = new BufferedStream(instream);
|
||||
int b = bis.ReadByte();
|
||||
if (b != 'B') {
|
||||
return;
|
||||
}
|
||||
b = bis.ReadByte();
|
||||
if (b != 'Z') {
|
||||
return;
|
||||
}
|
||||
BZip2InputStream bzis = new BZip2InputStream(bis);
|
||||
int ch = bzis.ReadByte();
|
||||
while (ch != -1) {
|
||||
bos.WriteByte((byte)ch);
|
||||
ch = bzis.ReadByte();
|
||||
}
|
||||
bos.Flush();
|
||||
}
|
||||
|
||||
public static void Compress(Stream instream, Stream outstream, int blockSize)
|
||||
{
|
||||
BufferedStream bos = new BufferedStream(outstream);
|
||||
bos.WriteByte((byte)'B');
|
||||
bos.WriteByte((byte)'Z');
|
||||
BufferedStream bis = new BufferedStream(instream);
|
||||
int ch = bis.ReadByte();
|
||||
BZip2OutputStream bzos = new BZip2OutputStream(bos);
|
||||
while(ch != -1) {
|
||||
bzos.WriteByte((byte)ch);
|
||||
ch = bis.ReadByte();
|
||||
}
|
||||
bis.Close();
|
||||
bzos.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* derived from a file which contained this license :
|
||||
* Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
*/
|
||||
@@ -0,0 +1,155 @@
|
||||
// BZip2Constants.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.BZip2 {
|
||||
|
||||
/// <summary>
|
||||
/// Base class for both the compress and decompress classes.
|
||||
/// Holds common arrays, and static data.
|
||||
/// </summary>
|
||||
public sealed class BZip2Constants
|
||||
{
|
||||
public readonly static int[] rNums = {
|
||||
619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
|
||||
985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
|
||||
733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
|
||||
419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
|
||||
878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
|
||||
862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
|
||||
150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
|
||||
170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
|
||||
73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
|
||||
909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
|
||||
641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
|
||||
161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
|
||||
382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
|
||||
98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
|
||||
227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
|
||||
469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
|
||||
184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
|
||||
715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
|
||||
951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
|
||||
652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
|
||||
645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
|
||||
609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
|
||||
653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
|
||||
411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
|
||||
170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
|
||||
857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
|
||||
669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
|
||||
944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
|
||||
344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
|
||||
897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
|
||||
433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
|
||||
686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
|
||||
946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
|
||||
978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
|
||||
680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
|
||||
707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
|
||||
297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
|
||||
134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
|
||||
343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
|
||||
140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
|
||||
170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
|
||||
369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
|
||||
804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
|
||||
896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
|
||||
661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
|
||||
768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
|
||||
61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
|
||||
372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
|
||||
780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
|
||||
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
|
||||
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
|
||||
936, 638
|
||||
};
|
||||
|
||||
public readonly static int baseBlockSize = 100000;
|
||||
|
||||
public readonly static int MAX_ALPHA_SIZE = 258;
|
||||
public readonly static int MAX_CODE_LEN = 23;
|
||||
|
||||
public readonly static int RUNA = 0;
|
||||
public readonly static int RUNB = 1;
|
||||
|
||||
public readonly static int N_GROUPS = 6;
|
||||
public readonly static int G_SIZE = 50;
|
||||
public readonly static int N_ITERS = 4;
|
||||
|
||||
public readonly static int MAX_SELECTORS = (2 + (900000 / G_SIZE));
|
||||
|
||||
public readonly static int NUM_OVERSHOOT_BYTES = 20;
|
||||
}
|
||||
}
|
||||
|
||||
/* This file was derived from a file containing under this license:
|
||||
*
|
||||
* This file is a part of bzip2 and/or libbzip2, a program and
|
||||
* library for lossless, block-sorting data compression.
|
||||
*
|
||||
* Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. The origin of this software must not be misrepresented; you must
|
||||
* not claim that you wrote the original software. If you use this
|
||||
* software in a product, an acknowledgment in the product
|
||||
* documentation would be appreciated but is not required.
|
||||
*
|
||||
* 3. Altered source versions must be plainly marked as such, and must
|
||||
* not be misrepresented as being the original software.
|
||||
*
|
||||
* 4. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Java version ported by Keiron Liddle, Aftex Software <[email protected]> 1999-2001
|
||||
*/
|
||||
@@ -0,0 +1,945 @@
|
||||
// BZip2InputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.BZip2 {
|
||||
|
||||
/// <summary>
|
||||
/// An input stream that decompresses from the BZip2 format (without the file
|
||||
/// header chars) to be read as any other stream.
|
||||
/// </summary>
|
||||
public class BZip2InputStream : Stream
|
||||
{
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanRead {
|
||||
get {
|
||||
return baseStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanSeek {
|
||||
get {
|
||||
return baseStream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanWrite {
|
||||
get {
|
||||
return baseStream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Length {
|
||||
get {
|
||||
return baseStream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Position {
|
||||
get {
|
||||
return baseStream.Position;
|
||||
}
|
||||
set {
|
||||
baseStream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the baseInputStream
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
if (baseStream != null) {
|
||||
baseStream.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return baseStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void SetLength(long val)
|
||||
{
|
||||
baseStream.SetLength(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void Write(byte[] array, int offset, int count)
|
||||
{
|
||||
baseStream.Write(array, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void WriteByte(byte val)
|
||||
{
|
||||
baseStream.WriteByte(val);
|
||||
}
|
||||
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int rb = ReadByte();
|
||||
if (rb == -1) {
|
||||
return i;
|
||||
}
|
||||
b[off + i] = (byte)rb;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the input stream
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
if (baseStream != null) {
|
||||
baseStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
static void Cadvise()
|
||||
{
|
||||
Console.WriteLine("CRC Error");
|
||||
//throw new CCoruptionError();
|
||||
}
|
||||
|
||||
static void BadBGLengths()
|
||||
{
|
||||
Console.WriteLine("bad BG lengths");
|
||||
}
|
||||
|
||||
static void bitStreamEOF()
|
||||
{
|
||||
Console.WriteLine("bit stream eof");
|
||||
}
|
||||
|
||||
static void CompressedStreamEOF()
|
||||
{
|
||||
Console.WriteLine("compressed stream eof");
|
||||
}
|
||||
|
||||
void MakeMaps()
|
||||
{
|
||||
nInUse = 0;
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
if (inUse[i]) {
|
||||
seqToUnseq[nInUse] = (byte)i;
|
||||
unseqToSeq[i] = (byte)nInUse;
|
||||
nInUse++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*--
|
||||
index of the last char in the block, so
|
||||
the block size == last + 1.
|
||||
--*/
|
||||
int last;
|
||||
|
||||
/*--
|
||||
index in zptr[] of original string after sorting.
|
||||
--*/
|
||||
int origPtr;
|
||||
|
||||
/*--
|
||||
always: in the range 0 .. 9.
|
||||
The current block size is 100000 * this number.
|
||||
--*/
|
||||
int blockSize100k;
|
||||
|
||||
bool blockRandomised;
|
||||
|
||||
// private int bytesIn;
|
||||
// private int bytesOut;
|
||||
int bsBuff;
|
||||
int bsLive;
|
||||
IChecksum mCrc = new StrangeCRC();
|
||||
|
||||
bool[] inUse = new bool[256];
|
||||
int nInUse;
|
||||
|
||||
byte[] seqToUnseq = new byte[256];
|
||||
byte[] unseqToSeq = new byte[256];
|
||||
|
||||
byte[] selector = new byte[BZip2Constants.MAX_SELECTORS];
|
||||
byte[] selectorMtf = new byte[BZip2Constants.MAX_SELECTORS];
|
||||
|
||||
int[] tt;
|
||||
byte[] ll8;
|
||||
|
||||
/*--
|
||||
freq table collected to save a pass over the data
|
||||
during decompression.
|
||||
--*/
|
||||
int[] unzftab = new int[256];
|
||||
|
||||
int[][] limit = new int[BZip2Constants.N_GROUPS][];
|
||||
int[][] baseArray = new int[BZip2Constants.N_GROUPS][];
|
||||
int[][] perm = new int[BZip2Constants.N_GROUPS][];
|
||||
int[] minLens = new int[BZip2Constants.N_GROUPS];
|
||||
|
||||
Stream baseStream;
|
||||
bool streamEnd = false;
|
||||
|
||||
int currentChar = -1;
|
||||
|
||||
const int START_BLOCK_STATE = 1;
|
||||
const int RAND_PART_A_STATE = 2;
|
||||
const int RAND_PART_B_STATE = 3;
|
||||
const int RAND_PART_C_STATE = 4;
|
||||
const int NO_RAND_PART_A_STATE = 5;
|
||||
const int NO_RAND_PART_B_STATE = 6;
|
||||
const int NO_RAND_PART_C_STATE = 7;
|
||||
|
||||
int currentState = START_BLOCK_STATE;
|
||||
|
||||
int storedBlockCRC, storedCombinedCRC;
|
||||
int computedBlockCRC;
|
||||
uint computedCombinedCRC;
|
||||
|
||||
int count, chPrev, ch2;
|
||||
int tPos;
|
||||
int rNToGo = 0;
|
||||
int rTPos = 0;
|
||||
int i2, j2;
|
||||
byte z;
|
||||
|
||||
public BZip2InputStream(Stream zStream)
|
||||
{
|
||||
// init arrays
|
||||
for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
|
||||
limit[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
|
||||
baseArray[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
|
||||
perm[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
|
||||
}
|
||||
|
||||
ll8 = null;
|
||||
tt = null;
|
||||
BsSetStream(zStream);
|
||||
Initialize();
|
||||
InitBlock();
|
||||
SetupBlock();
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
if (streamEnd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int retChar = currentChar;
|
||||
switch (currentState) {
|
||||
case RAND_PART_B_STATE:
|
||||
SetupRandPartB();
|
||||
break;
|
||||
case RAND_PART_C_STATE:
|
||||
SetupRandPartC();
|
||||
break;
|
||||
case NO_RAND_PART_B_STATE:
|
||||
SetupNoRandPartB();
|
||||
break;
|
||||
case NO_RAND_PART_C_STATE:
|
||||
SetupNoRandPartC();
|
||||
break;
|
||||
case START_BLOCK_STATE:
|
||||
case NO_RAND_PART_A_STATE:
|
||||
case RAND_PART_A_STATE:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return retChar;
|
||||
}
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
char magic3 = BsGetUChar();
|
||||
char magic4 = BsGetUChar();
|
||||
|
||||
if (magic3 != 'h' || magic4 < '1' || magic4 > '9') {
|
||||
streamEnd = true;
|
||||
return;
|
||||
}
|
||||
|
||||
SetDecompressStructureSizes(magic4 - '0');
|
||||
computedCombinedCRC = 0;
|
||||
}
|
||||
|
||||
void InitBlock()
|
||||
{
|
||||
char magic1 = BsGetUChar();
|
||||
char magic2 = BsGetUChar();
|
||||
char magic3 = BsGetUChar();
|
||||
char magic4 = BsGetUChar();
|
||||
char magic5 = BsGetUChar();
|
||||
char magic6 = BsGetUChar();
|
||||
|
||||
if (magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90) {
|
||||
Complete();
|
||||
return;
|
||||
}
|
||||
|
||||
if (magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59) {
|
||||
BadBlockHeader();
|
||||
streamEnd = true;
|
||||
return;
|
||||
}
|
||||
|
||||
storedBlockCRC = BsGetInt32();
|
||||
|
||||
blockRandomised = (BsR(1) == 1);
|
||||
|
||||
// currBlockNo++;
|
||||
GetAndMoveToFrontDecode();
|
||||
|
||||
mCrc.Reset();
|
||||
currentState = START_BLOCK_STATE;
|
||||
}
|
||||
|
||||
void EndBlock()
|
||||
{
|
||||
computedBlockCRC = (int)mCrc.Value;
|
||||
|
||||
/*-- A bad CRC is considered a fatal error. --*/
|
||||
if (storedBlockCRC != computedBlockCRC) {
|
||||
CrcError();
|
||||
}
|
||||
|
||||
// 1528150659
|
||||
computedCombinedCRC = ((computedCombinedCRC << 1) & 0xFFFFFFFF) | (computedCombinedCRC >> 31);
|
||||
computedCombinedCRC = computedCombinedCRC ^ (uint)computedBlockCRC;
|
||||
}
|
||||
|
||||
void Complete()
|
||||
{
|
||||
storedCombinedCRC = BsGetInt32();
|
||||
if (storedCombinedCRC != (int)computedCombinedCRC) {
|
||||
CrcError();
|
||||
}
|
||||
|
||||
streamEnd = true;
|
||||
}
|
||||
|
||||
static void BlockOverrun()
|
||||
{
|
||||
Console.WriteLine("Block overrun");
|
||||
}
|
||||
|
||||
static void BadBlockHeader()
|
||||
{
|
||||
Console.WriteLine("Bad block header");
|
||||
}
|
||||
|
||||
static void CrcError()
|
||||
{
|
||||
Console.WriteLine("crc error");
|
||||
}
|
||||
|
||||
|
||||
void BsSetStream(Stream f)
|
||||
{
|
||||
baseStream = f;
|
||||
bsLive = 0;
|
||||
bsBuff = 0;
|
||||
}
|
||||
|
||||
void FillBuffer()
|
||||
{
|
||||
int thech = 0;
|
||||
|
||||
try {
|
||||
thech = baseStream.ReadByte();
|
||||
} catch (Exception) {
|
||||
CompressedStreamEOF();
|
||||
}
|
||||
|
||||
if (thech == -1) {
|
||||
CompressedStreamEOF();
|
||||
}
|
||||
|
||||
bsBuff = (bsBuff << 8) | (thech & 0xFF);
|
||||
bsLive += 8;
|
||||
}
|
||||
|
||||
int BsR(int n)
|
||||
{
|
||||
while (bsLive < n) {
|
||||
FillBuffer();
|
||||
}
|
||||
|
||||
int v = (bsBuff >> (bsLive - n)) & ((1 << n) - 1);
|
||||
bsLive -= n;
|
||||
return v;
|
||||
}
|
||||
|
||||
char BsGetUChar()
|
||||
{
|
||||
return (char)BsR(8);
|
||||
}
|
||||
|
||||
int BsGetint()
|
||||
{
|
||||
int u = 0;
|
||||
u = (u << 8) | BsR(8);
|
||||
u = (u << 8) | BsR(8);
|
||||
u = (u << 8) | BsR(8);
|
||||
u = (u << 8) | BsR(8);
|
||||
return u;
|
||||
}
|
||||
|
||||
int BsGetIntVS(int numBits)
|
||||
{
|
||||
return (int)BsR(numBits);
|
||||
}
|
||||
|
||||
int BsGetInt32()
|
||||
{
|
||||
return (int)BsGetint();
|
||||
}
|
||||
|
||||
void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize)
|
||||
{
|
||||
int pp = 0;
|
||||
|
||||
for (int i = minLen; i <= maxLen; ++i) {
|
||||
for (int j = 0; j < alphaSize; ++j) {
|
||||
if (length[j] == i) {
|
||||
perm[pp] = j;
|
||||
++pp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < BZip2Constants.MAX_CODE_LEN; i++) {
|
||||
baseArray[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < alphaSize; i++) {
|
||||
++baseArray[length[i] + 1];
|
||||
}
|
||||
|
||||
for (int i = 1; i < BZip2Constants.MAX_CODE_LEN; i++) {
|
||||
baseArray[i] += baseArray[i - 1];
|
||||
}
|
||||
|
||||
for (int i = 0; i < BZip2Constants.MAX_CODE_LEN; i++) {
|
||||
limit[i] = 0;
|
||||
}
|
||||
|
||||
int vec = 0;
|
||||
|
||||
for (int i = minLen; i <= maxLen; i++) {
|
||||
vec += (baseArray[i + 1] - baseArray[i]);
|
||||
limit[i] = vec - 1;
|
||||
vec <<= 1;
|
||||
}
|
||||
|
||||
for (int i = minLen + 1; i <= maxLen; i++) {
|
||||
baseArray[i] = ((limit[i - 1] + 1) << 1) - baseArray[i];
|
||||
}
|
||||
}
|
||||
|
||||
void RecvDecodingTables()
|
||||
{
|
||||
char[][] len = new char[BZip2Constants.N_GROUPS][];
|
||||
for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
|
||||
len[i] = new char[BZip2Constants.MAX_ALPHA_SIZE];
|
||||
}
|
||||
|
||||
bool[] inUse16 = new bool[16];
|
||||
|
||||
/*--- Receive the mapping table ---*/
|
||||
for (int i = 0; i < 16; i++) {
|
||||
inUse16[i] = (BsR(1) == 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (inUse16[i]) {
|
||||
for (int j = 0; j < 16; j++) {
|
||||
inUse[i * 16 + j] = (BsR(1) == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MakeMaps();
|
||||
int alphaSize = nInUse + 2;
|
||||
|
||||
/*--- Now the selectors ---*/
|
||||
int nGroups = BsR(3);
|
||||
int nSelectors = BsR(15);
|
||||
|
||||
for (int i = 0; i < nSelectors; i++) {
|
||||
int j = 0;
|
||||
while (BsR(1) == 1) {
|
||||
j++;
|
||||
}
|
||||
selectorMtf[i] = (byte)j;
|
||||
}
|
||||
|
||||
/*--- Undo the MTF values for the selectors. ---*/
|
||||
byte[] pos = new byte[BZip2Constants.N_GROUPS];
|
||||
for (int v = 0; v < nGroups; v++) {
|
||||
pos[v] = (byte)v;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nSelectors; i++) {
|
||||
int v = selectorMtf[i];
|
||||
byte tmp = pos[v];
|
||||
while (v > 0) {
|
||||
pos[v] = pos[v - 1];
|
||||
v--;
|
||||
}
|
||||
pos[0] = tmp;
|
||||
selector[i] = tmp;
|
||||
}
|
||||
|
||||
/*--- Now the coding tables ---*/
|
||||
for (int t = 0; t < nGroups; t++) {
|
||||
int curr = BsR(5);
|
||||
for (int i = 0; i < alphaSize; i++) {
|
||||
while (BsR(1) == 1) {
|
||||
if (BsR(1) == 0) {
|
||||
curr++;
|
||||
} else {
|
||||
curr--;
|
||||
}
|
||||
}
|
||||
len[t][i] = (char)curr;
|
||||
}
|
||||
}
|
||||
|
||||
/*--- Create the Huffman decoding tables ---*/
|
||||
for (int t = 0; t < nGroups; t++) {
|
||||
int minLen = 32;
|
||||
int maxLen = 0;
|
||||
for (int i = 0; i < alphaSize; i++) {
|
||||
maxLen = Math.Max(maxLen, len[t][i]);
|
||||
minLen = Math.Min(minLen, len[t][i]);
|
||||
}
|
||||
HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize);
|
||||
minLens[t] = minLen;
|
||||
}
|
||||
}
|
||||
|
||||
void GetAndMoveToFrontDecode()
|
||||
{
|
||||
byte[] yy = new byte[256];
|
||||
int nextSym;
|
||||
|
||||
int limitLast = BZip2Constants.baseBlockSize * blockSize100k;
|
||||
origPtr = BsGetIntVS(24);
|
||||
|
||||
RecvDecodingTables();
|
||||
int EOB = nInUse+1;
|
||||
int groupNo = -1;
|
||||
int groupPos = 0;
|
||||
|
||||
/*--
|
||||
Setting up the unzftab entries here is not strictly
|
||||
necessary, but it does save having to do it later
|
||||
in a separate pass, and so saves a block's worth of
|
||||
cache misses.
|
||||
--*/
|
||||
for (int i = 0; i <= 255; i++) {
|
||||
unzftab[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 255; i++) {
|
||||
yy[i] = (byte)i;
|
||||
}
|
||||
|
||||
last = -1;
|
||||
|
||||
if (groupPos == 0) {
|
||||
groupNo++;
|
||||
groupPos = BZip2Constants.G_SIZE;
|
||||
}
|
||||
|
||||
groupPos--;
|
||||
int zt = selector[groupNo];
|
||||
int zn = minLens[zt];
|
||||
int zvec = BsR(zn);
|
||||
int zj;
|
||||
|
||||
while (zvec > limit[zt][zn]) {
|
||||
zn++;
|
||||
while (bsLive < 1) {
|
||||
FillBuffer();
|
||||
}
|
||||
zj = (bsBuff >> (bsLive-1)) & 1;
|
||||
bsLive--;
|
||||
zvec = (zvec << 1) | zj;
|
||||
}
|
||||
nextSym = perm[zt][zvec - baseArray[zt][zn]];
|
||||
|
||||
while (true) {
|
||||
|
||||
if (nextSym == EOB) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (nextSym == BZip2Constants.RUNA || nextSym == BZip2Constants.RUNB) {
|
||||
int s = -1;
|
||||
int n = 1;
|
||||
do {
|
||||
if (nextSym == BZip2Constants.RUNA) {
|
||||
s += (0+1) * n;
|
||||
} else if (nextSym == BZip2Constants.RUNB) {
|
||||
s += (1+1) * n;
|
||||
}
|
||||
|
||||
n <<= 1;
|
||||
|
||||
if (groupPos == 0) {
|
||||
groupNo++;
|
||||
groupPos = BZip2Constants.G_SIZE;
|
||||
}
|
||||
|
||||
groupPos--;
|
||||
|
||||
zt = selector[groupNo];
|
||||
zn = minLens[zt];
|
||||
zvec = BsR(zn);
|
||||
|
||||
while (zvec > limit[zt][zn]) {
|
||||
zn++;
|
||||
while (bsLive < 1) {
|
||||
FillBuffer();
|
||||
}
|
||||
zj = (bsBuff >> (bsLive - 1)) & 1;
|
||||
bsLive--;
|
||||
zvec = (zvec << 1) | zj;
|
||||
}
|
||||
nextSym = perm[zt][zvec - baseArray[zt][zn]];
|
||||
} while (nextSym == BZip2Constants.RUNA || nextSym == BZip2Constants.RUNB);
|
||||
|
||||
s++;
|
||||
byte ch = seqToUnseq[yy[0]];
|
||||
unzftab[ch] += s;
|
||||
|
||||
while (s > 0) {
|
||||
last++;
|
||||
ll8[last] = ch;
|
||||
s--;
|
||||
}
|
||||
|
||||
if (last >= limitLast) {
|
||||
BlockOverrun();
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
last++;
|
||||
if (last >= limitLast) {
|
||||
BlockOverrun();
|
||||
}
|
||||
|
||||
byte tmp = yy[nextSym - 1];
|
||||
unzftab[seqToUnseq[tmp]]++;
|
||||
ll8[last] = seqToUnseq[tmp];
|
||||
|
||||
for (int j = nextSym-1; j > 0; --j) {
|
||||
yy[j] = yy[j - 1];
|
||||
}
|
||||
yy[0] = tmp;
|
||||
|
||||
if (groupPos == 0) {
|
||||
groupNo++;
|
||||
groupPos = BZip2Constants.G_SIZE;
|
||||
}
|
||||
|
||||
groupPos--;
|
||||
zt = selector[groupNo];
|
||||
zn = minLens[zt];
|
||||
zvec = BsR(zn);
|
||||
while (zvec > limit[zt][zn]) {
|
||||
zn++;
|
||||
while (bsLive < 1) {
|
||||
FillBuffer();
|
||||
}
|
||||
zj = (bsBuff >> (bsLive-1)) & 1;
|
||||
bsLive--;
|
||||
zvec = (zvec << 1) | zj;
|
||||
}
|
||||
nextSym = perm[zt][zvec - baseArray[zt][zn]];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetupBlock()
|
||||
{
|
||||
int[] cftab = new int[257];
|
||||
|
||||
cftab[0] = 0;
|
||||
Array.Copy(unzftab, 0, cftab, 1, 256);
|
||||
|
||||
for (int i = 1; i <= 256; i++) {
|
||||
cftab[i] += cftab[i - 1];
|
||||
}
|
||||
|
||||
for (int i = 0; i <= last; i++) {
|
||||
byte ch = ll8[i];
|
||||
tt[cftab[ch]] = i;
|
||||
cftab[ch]++;
|
||||
}
|
||||
|
||||
cftab = null;
|
||||
|
||||
tPos = tt[origPtr];
|
||||
|
||||
count = 0;
|
||||
i2 = 0;
|
||||
ch2 = 256; /*-- not a char and not EOF --*/
|
||||
|
||||
if (blockRandomised) {
|
||||
rNToGo = 0;
|
||||
rTPos = 0;
|
||||
SetupRandPartA();
|
||||
} else {
|
||||
SetupNoRandPartA();
|
||||
}
|
||||
}
|
||||
|
||||
void SetupRandPartA()
|
||||
{
|
||||
if(i2 <= last) {
|
||||
chPrev = ch2;
|
||||
ch2 = ll8[tPos];
|
||||
tPos = tt[tPos];
|
||||
if (rNToGo == 0) {
|
||||
rNToGo = BZip2Constants.rNums[rTPos];
|
||||
rTPos++;
|
||||
if(rTPos == 512) {
|
||||
rTPos = 0;
|
||||
}
|
||||
}
|
||||
rNToGo--;
|
||||
ch2 ^= (int)((rNToGo == 1) ? 1 : 0);
|
||||
i2++;
|
||||
|
||||
currentChar = ch2;
|
||||
currentState = RAND_PART_B_STATE;
|
||||
mCrc.Update(ch2);
|
||||
} else {
|
||||
EndBlock();
|
||||
InitBlock();
|
||||
SetupBlock();
|
||||
}
|
||||
}
|
||||
|
||||
void SetupNoRandPartA()
|
||||
{
|
||||
if (i2 <= last) {
|
||||
chPrev = ch2;
|
||||
ch2 = ll8[tPos];
|
||||
tPos = tt[tPos];
|
||||
i2++;
|
||||
|
||||
currentChar = ch2;
|
||||
currentState = NO_RAND_PART_B_STATE;
|
||||
mCrc.Update(ch2);
|
||||
} else {
|
||||
EndBlock();
|
||||
InitBlock();
|
||||
SetupBlock();
|
||||
}
|
||||
}
|
||||
|
||||
void SetupRandPartB()
|
||||
{
|
||||
if (ch2 != chPrev) {
|
||||
currentState = RAND_PART_A_STATE;
|
||||
count = 1;
|
||||
SetupRandPartA();
|
||||
} else {
|
||||
count++;
|
||||
if (count >= 4) {
|
||||
z = ll8[tPos];
|
||||
tPos = tt[tPos];
|
||||
if (rNToGo == 0) {
|
||||
rNToGo = BZip2Constants.rNums[rTPos];
|
||||
rTPos++;
|
||||
if (rTPos == 512) {
|
||||
rTPos = 0;
|
||||
}
|
||||
}
|
||||
rNToGo--;
|
||||
z ^= (byte)((rNToGo == 1) ? 1 : 0);
|
||||
j2 = 0;
|
||||
currentState = RAND_PART_C_STATE;
|
||||
SetupRandPartC();
|
||||
} else {
|
||||
currentState = RAND_PART_A_STATE;
|
||||
SetupRandPartA();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetupRandPartC()
|
||||
{
|
||||
if (j2 < (int)z) {
|
||||
currentChar = ch2;
|
||||
mCrc.Update(ch2);
|
||||
j2++;
|
||||
} else {
|
||||
currentState = RAND_PART_A_STATE;
|
||||
i2++;
|
||||
count = 0;
|
||||
SetupRandPartA();
|
||||
}
|
||||
}
|
||||
|
||||
void SetupNoRandPartB()
|
||||
{
|
||||
if (ch2 != chPrev) {
|
||||
currentState = NO_RAND_PART_A_STATE;
|
||||
count = 1;
|
||||
SetupNoRandPartA();
|
||||
} else {
|
||||
count++;
|
||||
if (count >= 4) {
|
||||
z = ll8[tPos];
|
||||
tPos = tt[tPos];
|
||||
currentState = NO_RAND_PART_C_STATE;
|
||||
j2 = 0;
|
||||
SetupNoRandPartC();
|
||||
} else {
|
||||
currentState = NO_RAND_PART_A_STATE;
|
||||
SetupNoRandPartA();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetupNoRandPartC()
|
||||
{
|
||||
if (j2 < (int)z) {
|
||||
currentChar = ch2;
|
||||
mCrc.Update(ch2);
|
||||
j2++;
|
||||
} else {
|
||||
currentState = NO_RAND_PART_A_STATE;
|
||||
i2++;
|
||||
count = 0;
|
||||
SetupNoRandPartA();
|
||||
}
|
||||
}
|
||||
|
||||
void SetDecompressStructureSizes(int newSize100k)
|
||||
{
|
||||
if (!(0 <= newSize100k && newSize100k <= 9 &&
|
||||
0 <= blockSize100k && blockSize100k <= 9)) {
|
||||
throw new ApplicationException("Invalid block size");
|
||||
}
|
||||
|
||||
blockSize100k = newSize100k;
|
||||
|
||||
if (newSize100k == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int n = BZip2Constants.baseBlockSize * newSize100k;
|
||||
ll8 = new byte[n];
|
||||
tt = new int[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
/* This file was derived from a file containing under this license:
|
||||
*
|
||||
* This file is a part of bzip2 and/or libbzip2, a program and
|
||||
* library for lossless, block-sorting data compression.
|
||||
*
|
||||
* Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. The origin of this software must not be misrepresented; you must
|
||||
* not claim that you wrote the original software. If you use this
|
||||
* software in a product, an acknowledgment in the product
|
||||
* documentation would be appreciated but is not required.
|
||||
*
|
||||
* 3. Altered source versions must be plainly marked as such, and must
|
||||
* not be misrepresented as being the original software.
|
||||
*
|
||||
* 4. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Java version ported by Keiron Liddle, Aftex Software <[email protected]> 1999-2001
|
||||
*/
|
||||
+1746
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,199 @@
|
||||
// Adler32.cs - Computes Adler32 data checksum of a data stream
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Checksums {
|
||||
|
||||
/// <summary>
|
||||
/// Computes Adler32 checksum for a stream of data. An Adler32
|
||||
/// checksum is not as reliable as a CRC32 checksum, but a lot faster to
|
||||
/// compute.
|
||||
///
|
||||
/// The specification for Adler32 may be found in RFC 1950.
|
||||
/// ZLIB Compressed Data Format Specification version 3.3)
|
||||
///
|
||||
///
|
||||
/// From that document:
|
||||
///
|
||||
/// "ADLER32 (Adler-32 checksum)
|
||||
/// This contains a checksum value of the uncompressed data
|
||||
/// (excluding any dictionary data) computed according to Adler-32
|
||||
/// algorithm. This algorithm is a 32-bit extension and improvement
|
||||
/// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
|
||||
/// standard.
|
||||
///
|
||||
/// Adler-32 is composed of two sums accumulated per byte: s1 is
|
||||
/// the sum of all bytes, s2 is the sum of all s1 values. Both sums
|
||||
/// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
|
||||
/// Adler-32 checksum is stored as s2*65536 + s1 in most-
|
||||
/// significant-byte first (network) order."
|
||||
///
|
||||
/// "8.2. The Adler-32 algorithm
|
||||
///
|
||||
/// The Adler-32 algorithm is much faster than the CRC32 algorithm yet
|
||||
/// still provides an extremely low probability of undetected errors.
|
||||
///
|
||||
/// The modulo on unsigned long accumulators can be delayed for 5552
|
||||
/// bytes, so the modulo operation time is negligible. If the bytes
|
||||
/// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
|
||||
/// and order sensitive, unlike the first sum, which is just a
|
||||
/// checksum. That 65521 is prime is important to avoid a possible
|
||||
/// large class of two-byte errors that leave the check unchanged.
|
||||
/// (The Fletcher checksum uses 255, which is not prime and which also
|
||||
/// makes the Fletcher check insensitive to single byte changes 0 -
|
||||
/// 255.)
|
||||
///
|
||||
/// The sum s1 is initialized to 1 instead of zero to make the length
|
||||
/// of the sequence part of s2, so that the length does not have to be
|
||||
/// checked separately. (Any sequence of zeroes has a Fletcher
|
||||
/// checksum of zero.)"
|
||||
/// </summary>
|
||||
/// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream"/>
|
||||
/// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream"/>
|
||||
public sealed class Adler32 : IChecksum
|
||||
{
|
||||
/// <summary>
|
||||
/// largest prime smaller than 65536
|
||||
/// </summary>
|
||||
readonly static uint BASE = 65521;
|
||||
|
||||
uint checksum;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Adler32 data checksum computed so far.
|
||||
/// </summary>
|
||||
public long Value {
|
||||
get {
|
||||
return checksum;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <code>Adler32</code> class.
|
||||
/// The checksum starts off with a value of 1.
|
||||
/// </summary>
|
||||
public Adler32()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the Adler32 checksum to the initial value.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
checksum = 1; //Initialize to 1
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the checksum with the byte b.
|
||||
/// </summary>
|
||||
/// <param name="bval">
|
||||
/// the data value to add. The high byte of the int is ignored.
|
||||
/// </param>
|
||||
public void Update(int bval)
|
||||
{
|
||||
//We could make a length 1 byte array and call update again, but I
|
||||
//would rather not have that overhead
|
||||
uint s1 = checksum & 0xFFFF;
|
||||
uint s2 = checksum >> 16;
|
||||
|
||||
s1 = (s1 + ((uint)bval & 0xFF)) % BASE;
|
||||
s2 = (s1 + s2) % BASE;
|
||||
|
||||
checksum = (s2 << 16) + s1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the checksum with the bytes taken from the array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// buffer an array of bytes
|
||||
/// </param>
|
||||
public void Update(byte[] buffer)
|
||||
{
|
||||
Update(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the checksum with the bytes taken from the array.
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// an array of bytes
|
||||
/// </param>
|
||||
/// <param name="off">
|
||||
/// the start of the data used for this update
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the number of bytes to use for this update
|
||||
/// </param>
|
||||
public void Update(byte[] buf, int off, int len)
|
||||
{
|
||||
if (buf == null) {
|
||||
throw new ArgumentNullException("buf");
|
||||
}
|
||||
|
||||
if (off < 0 || len < 0 || off + len > buf.Length) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
//(By Per Bothner)
|
||||
uint s1 = checksum & 0xFFFF;
|
||||
uint s2 = checksum >> 16;
|
||||
|
||||
while (len > 0) {
|
||||
// We can defer the modulo operation:
|
||||
// s1 maximally grows from 65521 to 65521 + 255 * 3800
|
||||
// s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
|
||||
int n = 3800;
|
||||
if (n > len) {
|
||||
n = len;
|
||||
}
|
||||
len -= n;
|
||||
while (--n >= 0) {
|
||||
s1 = s1 + (uint)(buf[off++] & 0xFF);
|
||||
s2 = s2 + s1;
|
||||
}
|
||||
s1 %= BASE;
|
||||
s2 %= BASE;
|
||||
}
|
||||
|
||||
checksum = (s2 << 16) | s1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
// CRC32.cs - Computes CRC32 data checksum of a data stream
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Checksums {
|
||||
|
||||
/// <summary>
|
||||
/// Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
|
||||
/// x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
|
||||
///
|
||||
/// Polynomials over GF(2) are represented in binary, one bit per coefficient,
|
||||
/// with the lowest powers in the most significant bit. Then adding polynomials
|
||||
/// is just exclusive-or, and multiplying a polynomial by x is a right shift by
|
||||
/// one. If we call the above polynomial p, and represent a byte as the
|
||||
/// polynomial q, also with the lowest power in the most significant bit (so the
|
||||
/// byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
|
||||
/// where a mod b means the remainder after dividing a by b.
|
||||
///
|
||||
/// This calculation is done using the shift-register method of multiplying and
|
||||
/// taking the remainder. The register is initialized to zero, and for each
|
||||
/// incoming bit, x^32 is added mod p to the register if the bit is a one (where
|
||||
/// x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
|
||||
/// x (which is shifting right by one and adding x^32 mod p if the bit shifted
|
||||
/// out is a one). We start with the highest power (least significant bit) of
|
||||
/// q and repeat for all eight bits of q.
|
||||
///
|
||||
/// The table is simply the CRC of all possible eight bit values. This is all
|
||||
/// the information needed to generate CRC's on data a byte at a time for all
|
||||
/// combinations of CRC register values and incoming bytes.
|
||||
/// </summary>
|
||||
public sealed class Crc32 : IChecksum
|
||||
{
|
||||
readonly static uint CrcSeed = 0xFFFFFFFF;
|
||||
|
||||
readonly static uint[] CrcTable = new uint[] {
|
||||
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419,
|
||||
0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4,
|
||||
0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
|
||||
0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
|
||||
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856,
|
||||
0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
|
||||
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
|
||||
0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
|
||||
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
|
||||
0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A,
|
||||
0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599,
|
||||
0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
|
||||
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190,
|
||||
0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
|
||||
0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E,
|
||||
0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
|
||||
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED,
|
||||
0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
|
||||
0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3,
|
||||
0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
|
||||
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
|
||||
0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
|
||||
0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010,
|
||||
0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
|
||||
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17,
|
||||
0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6,
|
||||
0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615,
|
||||
0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
|
||||
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344,
|
||||
0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
|
||||
0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A,
|
||||
0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
|
||||
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1,
|
||||
0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C,
|
||||
0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
|
||||
0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
|
||||
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE,
|
||||
0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
|
||||
0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C,
|
||||
0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
|
||||
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B,
|
||||
0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
|
||||
0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1,
|
||||
0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
|
||||
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278,
|
||||
0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
|
||||
0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66,
|
||||
0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
|
||||
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
|
||||
0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8,
|
||||
0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B,
|
||||
0x2D02EF8D
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The crc data checksum so far.
|
||||
/// </summary>
|
||||
uint crc = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the CRC32 data checksum computed so far.
|
||||
/// </summary>
|
||||
public long Value {
|
||||
get {
|
||||
return (long)crc;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the CRC32 data checksum as if no update was ever called.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
crc = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the checksum with the int bval.
|
||||
/// </summary>
|
||||
/// <param name = "bval">
|
||||
/// the byte is taken as the lower 8 bits of bval
|
||||
/// </param>
|
||||
public void Update(int bval)
|
||||
{
|
||||
crc ^= CrcSeed;
|
||||
crc = CrcTable[(crc ^ bval) & 0xFF] ^ (crc >> 8);
|
||||
crc ^= CrcSeed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the checksum with the bytes taken from the array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// buffer an array of bytes
|
||||
/// </param>
|
||||
public void Update(byte[] buffer)
|
||||
{
|
||||
Update(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the byte array to the data checksum.
|
||||
/// </summary>
|
||||
/// <param name = "buf">
|
||||
/// the buffer which contains the data
|
||||
/// </param>
|
||||
/// <param name = "off">
|
||||
/// the offset in the buffer where the data starts
|
||||
/// </param>
|
||||
/// <param name = "len">
|
||||
/// the length of the data
|
||||
/// </param>
|
||||
public void Update(byte[] buf, int off, int len)
|
||||
{
|
||||
if (buf == null) {
|
||||
throw new ArgumentNullException("buf");
|
||||
}
|
||||
|
||||
if (off < 0 || len < 0 || off + len > buf.Length) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
crc ^= CrcSeed;
|
||||
|
||||
while (--len >= 0) {
|
||||
crc = CrcTable[(crc ^ buf[off++]) & 0xFF] ^ (crc >> 8);
|
||||
}
|
||||
|
||||
crc ^= CrcSeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// IChecksum.cs - Interface to compute a data checksum
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Checksums {
|
||||
|
||||
/// <summary>
|
||||
/// Interface to compute a data checksum used by checked input/output streams.
|
||||
/// A data checksum can be updated by one byte or with a byte array. After each
|
||||
/// update the value of the current checksum can be returned by calling
|
||||
/// <code>getValue</code>. The complete checksum object can also be reset
|
||||
/// so it can be used again with new data.
|
||||
/// </summary>
|
||||
public interface IChecksum
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the data checksum computed so far.
|
||||
/// </summary>
|
||||
long Value {
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the data checksum as if no update was ever called.
|
||||
/// </summary>
|
||||
void Reset();
|
||||
|
||||
/// <summary>
|
||||
/// Adds one byte to the data checksum.
|
||||
/// </summary>
|
||||
/// <param name = "bval">
|
||||
/// the data value to add. The high byte of the int is ignored.
|
||||
/// </param>
|
||||
void Update(int bval);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the data checksum with the bytes taken from the array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// buffer an array of bytes
|
||||
/// </param>
|
||||
void Update(byte[] buffer);
|
||||
|
||||
/// <summary>
|
||||
/// Adds the byte array to the data checksum.
|
||||
/// </summary>
|
||||
/// <param name = "buf">
|
||||
/// the buffer which contains the data
|
||||
/// </param>
|
||||
/// <param name = "off">
|
||||
/// the offset in the buffer where the data starts
|
||||
/// </param>
|
||||
/// <param name = "len">
|
||||
/// the length of the data
|
||||
/// </param>
|
||||
void Update(byte[] buf, int off, int len);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
// StrangeCRC.cs - computes a crc used in the bziplib ... I don't think that
|
||||
// this is the 'standard' crc, please correct me, if I'm wrong
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Checksums {
|
||||
|
||||
public class StrangeCRC : IChecksum
|
||||
{
|
||||
readonly static uint[] crc32Table = {
|
||||
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
|
||||
0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
|
||||
0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
|
||||
0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
|
||||
0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
|
||||
0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
|
||||
0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
|
||||
0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
|
||||
0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
|
||||
0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
|
||||
0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
|
||||
0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
|
||||
0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
|
||||
0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
|
||||
0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
|
||||
0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
|
||||
0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
|
||||
0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
|
||||
0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
|
||||
0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
|
||||
0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
|
||||
0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
|
||||
0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
|
||||
0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
|
||||
0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
|
||||
0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
|
||||
0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
|
||||
0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
|
||||
0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
|
||||
0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
|
||||
0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
|
||||
0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
|
||||
0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
|
||||
0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
|
||||
0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
|
||||
0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
|
||||
0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
|
||||
0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
|
||||
0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
|
||||
0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
|
||||
0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
|
||||
0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
|
||||
0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
|
||||
0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
|
||||
0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
|
||||
0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
|
||||
0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
|
||||
0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
|
||||
0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
|
||||
0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
|
||||
0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
|
||||
0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
|
||||
0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
|
||||
0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
|
||||
0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
|
||||
0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
|
||||
0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
|
||||
0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
|
||||
0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
|
||||
0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
|
||||
0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
|
||||
0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
|
||||
0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
|
||||
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
|
||||
};
|
||||
int globalCrc;
|
||||
|
||||
public StrangeCRC()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
globalCrc = -1;
|
||||
}
|
||||
|
||||
public long Value {
|
||||
get {
|
||||
return ~globalCrc;
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(int inCh)
|
||||
{
|
||||
int temp = (globalCrc >> 24) ^ inCh;
|
||||
if (temp < 0) {
|
||||
temp = 256 + temp;
|
||||
}
|
||||
globalCrc = (int)((globalCrc << 8) ^ crc32Table[temp]);
|
||||
}
|
||||
|
||||
public void Update(byte[] buf)
|
||||
{
|
||||
Update(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public void Update(byte[] buf, int off, int len)
|
||||
{
|
||||
if (buf == null) {
|
||||
throw new ArgumentNullException("buf");
|
||||
}
|
||||
|
||||
if (off < 0 || len < 0 || off + len > buf.Length) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Update(buf[off++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// GZIPConstants.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.GZip {
|
||||
|
||||
/// <summary>
|
||||
/// This class contains constants used for gzip.
|
||||
/// </summary>
|
||||
public class GZipConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// Magic number found at start of GZIP header
|
||||
/// </summary>
|
||||
public static readonly int GZIP_MAGIC = 0x1F8B;
|
||||
|
||||
/* The flag byte is divided into individual bits as follows:
|
||||
|
||||
bit 0 FTEXT
|
||||
bit 1 FHCRC
|
||||
bit 2 FEXTRA
|
||||
bit 3 FNAME
|
||||
bit 4 FCOMMENT
|
||||
bit 5 reserved
|
||||
bit 6 reserved
|
||||
bit 7 reserved
|
||||
*/
|
||||
public const int FTEXT = 0x1;
|
||||
public const int FHCRC = 0x2;
|
||||
public const int FEXTRA = 0x4;
|
||||
public const int FNAME = 0x8;
|
||||
public const int FCOMMENT = 0x10;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
// GzipInputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.GZip {
|
||||
|
||||
/// <summary>
|
||||
/// This filter stream is used to decompress a "GZIP" format stream.
|
||||
/// The "GZIP" format is described baseInputStream RFC 1952.
|
||||
///
|
||||
/// author of the original java version : John Leuner
|
||||
/// </summary>
|
||||
/// <example> This sample shows how to unzip a gzipped file
|
||||
/// <code>
|
||||
/// using System;
|
||||
/// using System.IO;
|
||||
///
|
||||
/// using NZlib.GZip;
|
||||
///
|
||||
/// class MainClass
|
||||
/// {
|
||||
/// public static void Main(string[] args)
|
||||
/// {
|
||||
/// Stream s = new GZipInputStream(File.OpenRead(args[0]));
|
||||
/// FileStream fs = File.Create(Path.GetFileNameWithoutExtension(args[0]));
|
||||
/// int size = 2048;
|
||||
/// byte[] writeData = new byte[2048];
|
||||
/// while (true) {
|
||||
/// size = s.Read(writeData, 0, size);
|
||||
/// if (size > 0) {
|
||||
/// fs.Write(writeData, 0, size);
|
||||
/// } else {
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// s.Close();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class GZipInputStream : InflaterInputStream
|
||||
{
|
||||
|
||||
//Variables
|
||||
|
||||
/// <summary>
|
||||
/// CRC-32 value for uncompressed data
|
||||
/// </summary>
|
||||
protected Crc32 crc = new Crc32();
|
||||
|
||||
/// <summary>
|
||||
/// Indicates end of stream
|
||||
/// </summary>
|
||||
protected bool eos;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GzipInputStream with the default buffer size
|
||||
/// </summary>
|
||||
/// <param name="baseInputStream">
|
||||
/// The stream to read compressed data from (baseInputStream GZIP format)
|
||||
/// </param>
|
||||
public GZipInputStream(Stream baseInputStream) : this(baseInputStream, 4096)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GZIPInputStream with the specified buffer size
|
||||
/// </summary>
|
||||
/// <param name="baseInputStream">
|
||||
/// The stream to read compressed data from (baseInputStream GZIP format)
|
||||
/// </param>
|
||||
/// <param name="size">
|
||||
/// Size of the buffer to use
|
||||
/// </param>
|
||||
public GZipInputStream(Stream baseInputStream, int size) : base(baseInputStream, new Inflater(true), size)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads uncompressed data into an array of bytes
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// the buffer to read uncompressed data into
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// the offset indicating where the data should be placed
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the number of uncompressed bytes to be read
|
||||
/// </param>
|
||||
public override int Read(byte[] buf, int offset, int len)
|
||||
{
|
||||
// We first have to slurp baseInputStream the GZIP header, then we feed all the
|
||||
// rest of the data to the superclass.
|
||||
//
|
||||
// As we do that we continually update the CRC32. Once the data is
|
||||
// finished, we check the CRC32
|
||||
//
|
||||
// This means we don't need our own buffer, as everything is done
|
||||
// baseInputStream the superclass.
|
||||
if (!readGZIPHeader) {
|
||||
ReadHeader();
|
||||
}
|
||||
|
||||
if (eos) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// System.err.println("GZIPIS.read(byte[], off, len ... " + offset + " and len " + len);
|
||||
//We don't have to read the header, so we just grab data from the superclass
|
||||
int numRead = base.Read(buf, offset, len);
|
||||
if (numRead > 0) {
|
||||
crc.Update(buf, offset, numRead);
|
||||
}
|
||||
|
||||
if (inf.IsFinished) {
|
||||
ReadFooter();
|
||||
}
|
||||
return numRead;
|
||||
}
|
||||
|
||||
private void ReadHeader()
|
||||
{
|
||||
/* 1. Check the two magic bytes */
|
||||
Crc32 headCRC = new Crc32();
|
||||
int magic = baseInputStream.ReadByte();
|
||||
if (magic < 0) {
|
||||
eos = true;
|
||||
return;
|
||||
}
|
||||
headCRC.Update(magic);
|
||||
if (magic != (GZipConstants.GZIP_MAGIC >> 8)) {
|
||||
throw new IOException("Error baseInputStream GZIP header, first byte doesn't match");
|
||||
}
|
||||
|
||||
magic = baseInputStream.ReadByte();
|
||||
if (magic != (GZipConstants.GZIP_MAGIC & 0xFF)) {
|
||||
throw new IOException("Error baseInputStream GZIP header, second byte doesn't match");
|
||||
}
|
||||
headCRC.Update(magic);
|
||||
|
||||
/* 2. Check the compression type (must be 8) */
|
||||
int CM = baseInputStream.ReadByte();
|
||||
if (CM != 8) {
|
||||
throw new IOException("Error baseInputStream GZIP header, data not baseInputStream deflate format");
|
||||
}
|
||||
headCRC.Update(CM);
|
||||
|
||||
/* 3. Check the flags */
|
||||
int flags = baseInputStream.ReadByte();
|
||||
if (flags < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
headCRC.Update(flags);
|
||||
|
||||
/* This flag byte is divided into individual bits as follows:
|
||||
|
||||
bit 0 FTEXT
|
||||
bit 1 FHCRC
|
||||
bit 2 FEXTRA
|
||||
bit 3 FNAME
|
||||
bit 4 FCOMMENT
|
||||
bit 5 reserved
|
||||
bit 6 reserved
|
||||
bit 7 reserved
|
||||
*/
|
||||
|
||||
/* 3.1 Check the reserved bits are zero */
|
||||
|
||||
if ((flags & 0xd0) != 0) {
|
||||
throw new IOException("Reserved flag bits baseInputStream GZIP header != 0");
|
||||
}
|
||||
|
||||
/* 4.-6. Skip the modification time, extra flags, and OS type */
|
||||
for (int i=0; i< 6; i++) {
|
||||
int readByte = baseInputStream.ReadByte();
|
||||
if (readByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
|
||||
/* 7. Read extra field */
|
||||
if ((flags & GZipConstants.FEXTRA) != 0) {
|
||||
/* Skip subfield id */
|
||||
for (int i=0; i< 2; i++) {
|
||||
int readByte = baseInputStream.ReadByte();
|
||||
if (readByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
|
||||
int len1, len2, extraLen;
|
||||
len1 = baseInputStream.ReadByte();
|
||||
len2 = baseInputStream.ReadByte();
|
||||
if ((len1 < 0) || (len2 < 0)) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
headCRC.Update(len1);
|
||||
headCRC.Update(len2);
|
||||
|
||||
extraLen = (len1 << 8) | len2;
|
||||
for (int i = 0; i < extraLen;i++) {
|
||||
int readByte = baseInputStream.ReadByte();
|
||||
if (readByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
}
|
||||
|
||||
/* 8. Read file name */
|
||||
if ((flags & GZipConstants.FNAME) != 0) {
|
||||
int readByte;
|
||||
while ( (readByte = baseInputStream.ReadByte()) > 0) {
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
if (readByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP file name");
|
||||
}
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
|
||||
/* 9. Read comment */
|
||||
if ((flags & GZipConstants.FCOMMENT) != 0) {
|
||||
int readByte;
|
||||
while ( (readByte = baseInputStream.ReadByte()) > 0) {
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
|
||||
if (readByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP comment");
|
||||
}
|
||||
headCRC.Update(readByte);
|
||||
}
|
||||
|
||||
/* 10. Read header CRC */
|
||||
if ((flags & GZipConstants.FHCRC) != 0) {
|
||||
int tempByte;
|
||||
int crcval = baseInputStream.ReadByte();
|
||||
if (crcval < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
|
||||
tempByte = baseInputStream.ReadByte();
|
||||
if (tempByte < 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP header");
|
||||
}
|
||||
|
||||
crcval = (crcval << 8) | tempByte;
|
||||
if (crcval != ((int) headCRC.Value & 0xffff)) {
|
||||
throw new IOException("Header CRC value mismatch");
|
||||
}
|
||||
}
|
||||
|
||||
readGZIPHeader = true;
|
||||
//System.err.println("Read GZIP header");
|
||||
}
|
||||
|
||||
private void ReadFooter()
|
||||
{
|
||||
byte[] footer = new byte[8];
|
||||
int avail = inf.RemainingInput;
|
||||
if (avail > 8) {
|
||||
avail = 8;
|
||||
}
|
||||
System.Array.Copy(buf, len - inf.RemainingInput, footer, 0, avail);
|
||||
int needed = 8 - avail;
|
||||
while (needed > 0) {
|
||||
int count = baseInputStream.Read(footer, 8-needed, needed);
|
||||
if (count <= 0) {
|
||||
throw new Exception("Early EOF baseInputStream GZIP footer");
|
||||
}
|
||||
needed -= count; //Jewel Jan 16
|
||||
}
|
||||
int crcval = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8) | ((footer[2] & 0xff) << 16) | (footer[3] << 24);
|
||||
if (crcval != (int) crc.Value) {
|
||||
throw new IOException("GZIP crc sum mismatch, theirs \"" + crcval + "\" and ours \"" + (int) crc.Value);
|
||||
}
|
||||
int total = (footer[4] & 0xff) | ((footer[5] & 0xff) << 8) | ((footer[6] & 0xff) << 16) | (footer[7] << 24);
|
||||
if (total != inf.TotalOut) {
|
||||
throw new IOException("Number of bytes mismatch");
|
||||
}
|
||||
/* XXX Should we support multiple members.
|
||||
* Difficult, since there may be some bytes still baseInputStream buf
|
||||
*/
|
||||
eos = true;
|
||||
}
|
||||
|
||||
/* Have we read the GZIP header yet? */
|
||||
private bool readGZIPHeader;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
// GzipOutputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.GZip {
|
||||
|
||||
/// <summary>
|
||||
/// This filter stream is used to compress a stream into a "GZIP" stream.
|
||||
/// The "GZIP" format is described in RFC 1952.
|
||||
///
|
||||
/// author of the original java version : John Leuner
|
||||
/// </summary>
|
||||
/// <example> This sample shows how to gzip a file
|
||||
/// <code>
|
||||
/// using System;
|
||||
/// using System.IO;
|
||||
///
|
||||
/// using NZlib.GZip;
|
||||
///
|
||||
/// class MainClass
|
||||
/// {
|
||||
/// public static void Main(string[] args)
|
||||
/// {
|
||||
/// Stream s = new GZipOutputStream(File.Create(args[0] + ".gz"));
|
||||
/// FileStream fs = File.OpenRead(args[0]);
|
||||
/// byte[] writeData = new byte[fs.Length];
|
||||
/// fs.Read(writeData, 0, (int)fs.Length);
|
||||
/// s.Write(writeData, 0, writeData.Length);
|
||||
/// s.Close();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class GZipOutputStream : DeflaterOutputStream
|
||||
{
|
||||
//Variables
|
||||
|
||||
/// <summary>
|
||||
/// CRC-32 value for uncompressed data
|
||||
/// </summary>
|
||||
protected Crc32 crc = new Crc32();
|
||||
|
||||
// Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GzipOutputStream with the default buffer size
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// The stream to read data (to be compressed) from
|
||||
/// </param>
|
||||
public GZipOutputStream(Stream baseOutputStream) : this(baseOutputStream, 4096)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GZIPOutputStream with the specified buffer size
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// The stream to read data (to be compressed) from
|
||||
/// </param>
|
||||
/// <param name="size">
|
||||
/// Size of the buffer to use
|
||||
/// </param>
|
||||
public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size)
|
||||
{
|
||||
// TODO : find out correctness, orgininally this was : (int) (System.currentTimeMillis() / 1000L);
|
||||
int mod_time = (int)(DateTime.Now.Ticks / 10000L); // Ticks give back 100ns intervals
|
||||
byte[] gzipHeader = {
|
||||
/* The two magic bytes */
|
||||
(byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) GZipConstants.GZIP_MAGIC,
|
||||
|
||||
/* The compression type */
|
||||
(byte) Deflater.DEFLATED,
|
||||
|
||||
/* The flags (not set) */
|
||||
0,
|
||||
|
||||
/* The modification time */
|
||||
(byte) mod_time, (byte) (mod_time >> 8),
|
||||
(byte) (mod_time >> 16), (byte) (mod_time >> 24),
|
||||
|
||||
/* The extra flags */
|
||||
0,
|
||||
|
||||
/* The OS type (unknown) */
|
||||
(byte) 255
|
||||
};
|
||||
|
||||
baseOutputStream.Write(gzipHeader, 0, gzipHeader.Length);
|
||||
// System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )");
|
||||
}
|
||||
|
||||
public override void Write(byte[] buf, int off, int len)
|
||||
{
|
||||
crc.Update(buf, off, len);
|
||||
base.Write(buf, off, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes remaining compressed output data to the output stream
|
||||
/// and closes it.
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
Finish();
|
||||
baseOutputStream.Close();
|
||||
}
|
||||
|
||||
public override void Finish()
|
||||
{
|
||||
base.Finish();
|
||||
|
||||
int totalin = def.TotalIn;
|
||||
int crcval = (int) (crc.Value & 0xffffffff);
|
||||
|
||||
// System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin));
|
||||
|
||||
byte[] gzipFooter = {
|
||||
(byte) crcval, (byte) (crcval >> 8),
|
||||
(byte) (crcval >> 16), (byte) (crcval >> 24),
|
||||
|
||||
(byte) totalin, (byte) (totalin >> 8),
|
||||
(byte) (totalin >> 16), (byte) (totalin >> 24)
|
||||
};
|
||||
|
||||
baseOutputStream.Write(gzipFooter, 0, gzipFooter.Length);
|
||||
// System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )");
|
||||
}
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
// BZip2OutputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
/// <summary>
|
||||
/// This exception is used to indicate that there is a problem
|
||||
/// with a TAR archive header.
|
||||
/// </summary>
|
||||
public class InvalidHeaderException : System.IO.IOException
|
||||
{
|
||||
public InvalidHeaderException()
|
||||
{
|
||||
}
|
||||
|
||||
public InvalidHeaderException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The original Java file had this header:
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,693 @@
|
||||
// TarInputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
public delegate void ProgressMessageHandler(TarArchive archive, string message);
|
||||
|
||||
/// <summary>
|
||||
/// The TarArchive class implements the concept of a
|
||||
/// tar archive. A tar archive is a series of entries, each of
|
||||
/// which represents a file system object. Each entry in
|
||||
/// the archive consists of a header record. Directory entries
|
||||
/// consist only of the header record, and are followed by entries
|
||||
/// for the directory's contents. File entries consist of a
|
||||
/// header record followed by the number of records needed to
|
||||
/// contain the file's contents. All entries are written on
|
||||
/// record boundaries. Records are 512 bytes long.
|
||||
///
|
||||
/// TarArchives are instantiated in either read or write mode,
|
||||
/// based upon whether they are instantiated with an InputStream
|
||||
/// or an OutputStream. Once instantiated TarArchives read/write
|
||||
/// mode can not be changed.
|
||||
///
|
||||
/// There is currently no support for random access to tar archives.
|
||||
/// However, it seems that subclassing TarArchive, and using the
|
||||
/// TarBuffer.getCurrentRecordNum() and TarBuffer.getCurrentBlockNum()
|
||||
/// methods, this would be rather trvial.
|
||||
/// </summary>
|
||||
public class TarArchive
|
||||
{
|
||||
protected bool verbose;
|
||||
protected bool debug;
|
||||
protected bool keepOldFiles;
|
||||
protected bool asciiTranslate;
|
||||
|
||||
protected int userId;
|
||||
protected string userName;
|
||||
protected int groupId;
|
||||
protected string groupName;
|
||||
|
||||
protected string rootPath;
|
||||
protected string pathPrefix;
|
||||
|
||||
protected int recordSize;
|
||||
protected byte[] recordBuf;
|
||||
|
||||
protected TarInputStream tarIn;
|
||||
protected TarOutputStream tarOut;
|
||||
|
||||
public event ProgressMessageHandler ProgressMessageEvent;
|
||||
|
||||
protected virtual void OnProgressMessageEvent(string message)
|
||||
{
|
||||
if (ProgressMessageEvent != null) {
|
||||
ProgressMessageEvent(this, message);
|
||||
}
|
||||
}
|
||||
|
||||
protected TarArchive()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The InputStream based constructors create a TarArchive for the
|
||||
/// purposes of e'x'tracting or lis't'ing a tar archive. Thus, use
|
||||
/// these constructors when you wish to extract files from or list
|
||||
/// the contents of an existing tar archive.
|
||||
/// </summary>
|
||||
public static TarArchive CreateInputTarArchive(Stream inputStream)
|
||||
{
|
||||
return CreateInputTarArchive(inputStream, TarBuffer.DEFAULT_BLKSIZE);
|
||||
}
|
||||
|
||||
public static TarArchive CreateInputTarArchive(Stream inputStream, int blockSize)
|
||||
{
|
||||
return CreateInputTarArchive(inputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
|
||||
}
|
||||
|
||||
public static TarArchive CreateInputTarArchive(Stream inputStream, int blockSize, int recordSize)
|
||||
{
|
||||
TarArchive archive = new TarArchive();
|
||||
archive.tarIn = new TarInputStream(inputStream, blockSize, recordSize);
|
||||
archive.Initialize(recordSize);
|
||||
return archive;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The OutputStream based constructors create a TarArchive for the
|
||||
/// purposes of 'c'reating a tar archive. Thus, use these constructors
|
||||
/// when you wish to create a new tar archive and write files into it.
|
||||
/// </summary>
|
||||
public static TarArchive CreateOutputTarArchive(Stream outputStream)
|
||||
{
|
||||
return CreateOutputTarArchive(outputStream, TarBuffer.DEFAULT_BLKSIZE);
|
||||
}
|
||||
|
||||
public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockSize)
|
||||
{
|
||||
return CreateOutputTarArchive(outputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
|
||||
}
|
||||
|
||||
public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockSize, int recordSize)
|
||||
{
|
||||
TarArchive archive = new TarArchive();
|
||||
archive.tarOut = new TarOutputStream(outputStream, blockSize, recordSize);
|
||||
archive.Initialize(recordSize);
|
||||
return archive;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Common constructor initialization code.
|
||||
/// </summary>
|
||||
void Initialize(int recordSize)
|
||||
{
|
||||
this.rootPath = null;
|
||||
this.pathPrefix = null;
|
||||
|
||||
// this.tempPath = System.getProperty( "user.dir" );
|
||||
|
||||
this.userId = 0;
|
||||
this.userName = String.Empty;
|
||||
this.groupId = 0;
|
||||
this.groupName = String.Empty;
|
||||
|
||||
this.debug = false;
|
||||
this.verbose = false;
|
||||
this.keepOldFiles = false;
|
||||
|
||||
this.recordBuf = new byte[RecordSize];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the debugging flag.
|
||||
*
|
||||
* @param debugF The new debug setting.
|
||||
*/
|
||||
public void SetDebug(bool debugF)
|
||||
{
|
||||
this.debug = debugF;
|
||||
if (this.tarIn != null) {
|
||||
this.tarIn.SetDebug(debugF);
|
||||
}
|
||||
if (this.tarOut != null) {
|
||||
this.tarOut.SetDebug(debugF);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/Set the verbosity setting.
|
||||
/// </summary>
|
||||
public bool IsVerbose {
|
||||
get {
|
||||
return verbose;
|
||||
}
|
||||
set {
|
||||
verbose = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the flag that determines whether existing files are
|
||||
/// kept, or overwritten during extraction.
|
||||
/// </summary>
|
||||
/// <param name="keepOldFiles">
|
||||
/// If true, do not overwrite existing files.
|
||||
/// </param>
|
||||
public void SetKeepOldFiles(bool keepOldFiles)
|
||||
{
|
||||
this.keepOldFiles = keepOldFiles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the ascii file translation flag. If ascii file translatio
|
||||
/// is true, then the MIME file type will be consulted to determine
|
||||
/// if the file is of type 'text/*'. If the MIME type is not found,
|
||||
/// then the TransFileTyper is consulted if it is not null. If
|
||||
/// either of these two checks indicates the file is an ascii text
|
||||
/// file, it will be translated. The translation converts the local
|
||||
/// operating system's concept of line ends into the UNIX line end,
|
||||
/// '\n', which is the defacto standard for a TAR archive. This makes
|
||||
/// text files compatible with UNIX, and since most tar implementations
|
||||
/// text files compatible with UNIX, and since most tar implementations
|
||||
/// </summary>
|
||||
/// <param name= "asciiTranslate">
|
||||
/// If true, translate ascii text files.
|
||||
/// </param>
|
||||
public void SetAsciiTranslation(bool asciiTranslate)
|
||||
{
|
||||
this.asciiTranslate = asciiTranslate;
|
||||
}
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// Set the object that will determine if a file is of type
|
||||
/// ascii text for translation purposes.
|
||||
/// </summary>
|
||||
/// <param name="transTyper">
|
||||
/// The new TransFileTyper object.
|
||||
/// </param>
|
||||
public void SetTransFileTyper(TarTransFileTyper transTyper)
|
||||
{
|
||||
this.transTyper = transTyper;
|
||||
}*/
|
||||
|
||||
/// <summary>
|
||||
/// Set user and group information that will be used to fill in the
|
||||
/// tar archive's entry headers. Since Java currently provides no means
|
||||
/// of determining a user name, user id, group name, or group id for
|
||||
/// a given File, TarArchive allows the programmer to specify values
|
||||
/// to be used in their place.
|
||||
/// </summary>
|
||||
/// <param name="userId">
|
||||
/// The user Id to use in the headers.
|
||||
/// </param>
|
||||
/// <param name="userName">
|
||||
/// The user name to use in the headers.
|
||||
/// </param>
|
||||
/// <param name="groupId">
|
||||
/// The group id to use in the headers.
|
||||
/// </param>
|
||||
/// <param name="groupName">
|
||||
/// The group name to use in the headers.
|
||||
/// </param>
|
||||
public void SetUserInfo(int userId, string userName, int groupId, string groupName)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.userName = userName;
|
||||
this.groupId = groupId;
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the user id being used for archive entry headers.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current user id.
|
||||
/// </returns>
|
||||
public int UserId {
|
||||
get {
|
||||
return this.userId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the user name being used for archive entry headers.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current user name.
|
||||
/// </returns>
|
||||
public string UserName {
|
||||
get {
|
||||
return this.userName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the group id being used for archive entry headers.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current group id.
|
||||
/// </returns>
|
||||
public int GroupId {
|
||||
get {
|
||||
return this.groupId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the group name being used for archive entry headers.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current group name.
|
||||
/// </returns>
|
||||
public string GroupName {
|
||||
get {
|
||||
return this.groupName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the archive's record size. Because of its history, tar
|
||||
/// supports the concept of buffered IO consisting of BLOCKS of
|
||||
/// RECORDS. This allowed tar to match the IO characteristics of
|
||||
/// the physical device being used. Of course, in the Java world,
|
||||
/// this makes no sense, WITH ONE EXCEPTION - archives are expected
|
||||
/// to be propertly "blocked". Thus, all of the horrible TarBuffer
|
||||
/// support boils down to simply getting the "boundaries" correct.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The record size this archive is using.
|
||||
/// </returns>
|
||||
public int RecordSize {
|
||||
get {
|
||||
if (this.tarIn != null) {
|
||||
return this.tarIn.GetRecordSize();
|
||||
} else if (this.tarOut != null) {
|
||||
return this.tarOut.GetRecordSize();
|
||||
}
|
||||
return TarBuffer.DEFAULT_RCDSIZE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close the archive. This simply calls the underlying
|
||||
/// tar stream's close() method.
|
||||
/// </summary>
|
||||
public void CloseArchive()
|
||||
{
|
||||
if (this.tarIn != null) {
|
||||
this.tarIn.Close();
|
||||
} else if (this.tarOut != null) {
|
||||
this.tarOut.Flush();
|
||||
this.tarOut.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the "list" command and list the contents of the archive.
|
||||
///
|
||||
/// NOTE That this method uses the progress display to actually list
|
||||
/// the conents. If the progress display is not set, nothing will be
|
||||
/// listed!
|
||||
/// </summary>
|
||||
public void ListContents()
|
||||
{
|
||||
while (true) {
|
||||
TarEntry entry = this.tarIn.GetNextEntry();
|
||||
|
||||
if (entry == null) {
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("READ EOF RECORD");
|
||||
}
|
||||
break;
|
||||
}
|
||||
OnProgressMessageEvent(entry.Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the "extract" command and extract the contents of the archive.
|
||||
/// </summary>
|
||||
/// <param name="destDir">
|
||||
/// The destination directory into which to extract.
|
||||
/// </param>
|
||||
public void ExtractContents(string destDir)
|
||||
{
|
||||
while (true) {
|
||||
TarEntry entry = this.tarIn.GetNextEntry();
|
||||
|
||||
if (entry == null) {
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("READ EOF RECORD");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
this.ExtractEntry(destDir, entry);
|
||||
}
|
||||
}
|
||||
|
||||
void EnsureDirectoryExists(string directoryName)
|
||||
{
|
||||
if (!Directory.Exists(directoryName)) {
|
||||
try {
|
||||
Directory.CreateDirectory(directoryName);
|
||||
} catch (Exception e) {
|
||||
throw new IOException("error making directory path '" + directoryName + "', " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IsBinary(string filename)
|
||||
{
|
||||
FileStream fs = File.OpenRead(filename);
|
||||
|
||||
byte[] content = new byte[fs.Length];
|
||||
|
||||
fs.Read(content, 0, (int)fs.Length);
|
||||
fs.Close();
|
||||
|
||||
// assume that ascii 0 or
|
||||
// ascii 255 are only found in non text files.
|
||||
// and that all non text files contain 0 and 255
|
||||
foreach (byte b in content) {
|
||||
if (b == 0 || b == 255) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract an entry from the archive. This method assumes that the
|
||||
/// tarIn stream has been properly set with a call to getNextEntry().
|
||||
/// </summary>
|
||||
/// <param name="destDir">
|
||||
/// The destination directory into which to extract.
|
||||
/// </param>
|
||||
/// <param name="entry">
|
||||
/// The TarEntry returned by tarIn.getNextEntry().
|
||||
/// </param>
|
||||
void ExtractEntry(string destDir, TarEntry entry)
|
||||
{
|
||||
if (this.verbose) {
|
||||
OnProgressMessageEvent(entry.Name);
|
||||
}
|
||||
|
||||
string name = entry.Name;
|
||||
name = name.Replace('/', Path.DirectorySeparatorChar);
|
||||
|
||||
if (!destDir.EndsWith(Path.DirectorySeparatorChar.ToString())) {
|
||||
destDir += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
string destFile = destDir + name;
|
||||
|
||||
if (entry.IsDirectory) {
|
||||
EnsureDirectoryExists(destFile);
|
||||
} else {
|
||||
string parentDirectory = Path.GetDirectoryName(destFile);
|
||||
EnsureDirectoryExists(parentDirectory);
|
||||
|
||||
if (this.keepOldFiles && File.Exists(destFile)) {
|
||||
if (this.verbose) {
|
||||
OnProgressMessageEvent("not overwriting " + entry.Name);
|
||||
}
|
||||
} else {
|
||||
bool asciiTrans = false;
|
||||
Stream outputStream = File.Create(destFile);
|
||||
if (this.asciiTranslate) {
|
||||
asciiTrans = !IsBinary(destFile);
|
||||
// original java sourcecode :
|
||||
// MimeType mime = null;
|
||||
// string contentType = null;
|
||||
// try {
|
||||
// contentType = FileTypeMap.getDefaultFileTypeMap().getContentType( destFile );
|
||||
//
|
||||
// mime = new MimeType(contentType);
|
||||
//
|
||||
// if (mime.getPrimaryType().equalsIgnoreCase( "text" )) {
|
||||
// asciiTrans = true;
|
||||
// } else if ( this.transTyper != null ) {
|
||||
// if ( this.transTyper.isAsciiFile( entry.getName() ) ) {
|
||||
// asciiTrans = true;
|
||||
// }
|
||||
// }
|
||||
// } catch (MimeTypeParseException ex) {
|
||||
// }
|
||||
//
|
||||
// if (this.debug) {
|
||||
// Console.Error.WriteLine(("EXTRACT TRANS? '" + asciiTrans + "' ContentType='" + contentType + "' PrimaryType='" + mime.getPrimaryType() + "'" );
|
||||
// }
|
||||
}
|
||||
|
||||
StreamWriter outw = null;
|
||||
if (asciiTrans) {
|
||||
outw = new StreamWriter(outputStream);
|
||||
}
|
||||
|
||||
byte[] rdbuf = new byte[32 * 1024];
|
||||
|
||||
while (true) {
|
||||
int numRead = this.tarIn.Read(rdbuf, 0, rdbuf.Length);
|
||||
|
||||
if (numRead <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (asciiTrans) {
|
||||
for (int off = 0, b = 0; b < numRead; ++b) {
|
||||
if (rdbuf[b] == 10) {
|
||||
string s = Encoding.ASCII.GetString(rdbuf, off, (b - off));
|
||||
outw.WriteLine(s);
|
||||
off = b + 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
outputStream.Write(rdbuf, 0, numRead);
|
||||
}
|
||||
}
|
||||
|
||||
if (asciiTrans) {
|
||||
outw.Close();
|
||||
} else {
|
||||
outputStream.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an entry to the archive. This method will call the putNextEntry
|
||||
/// and then write the contents of the entry, and finally call closeEntry()()
|
||||
/// for entries that are files. For directories, it will call putNextEntry(),
|
||||
/// and then, if the recurse flag is true, process each entry that is a
|
||||
/// child of the directory.
|
||||
/// </summary>
|
||||
/// <param name="entry">
|
||||
/// The TarEntry representing the entry to write to the archive.
|
||||
/// </param>
|
||||
/// <param name="recurse">
|
||||
/// If true, process the children of directory entries.
|
||||
/// </param>
|
||||
public void WriteEntry(TarEntry entry, bool recurse)
|
||||
{
|
||||
bool asciiTrans = false;
|
||||
|
||||
string tempFileName = null;
|
||||
string eFile = entry.File;
|
||||
|
||||
// Work on a copy of the entry so we can manipulate it.
|
||||
// Note that we must distinguish how the entry was constructed.
|
||||
//
|
||||
if (eFile == null || eFile.Length == 0) {
|
||||
entry = TarEntry.CreateTarEntry(entry.Name);
|
||||
} else {
|
||||
//
|
||||
// The user may have explicitly set the entry's name to
|
||||
// something other than the file's path, so we must save
|
||||
// and restore it. This should work even when the name
|
||||
// was set from the File's name.
|
||||
//
|
||||
string saveName = entry.Name;
|
||||
entry = TarEntry.CreateEntryFromFile(eFile);
|
||||
entry.Name = saveName;
|
||||
}
|
||||
|
||||
if (this.verbose) {
|
||||
OnProgressMessageEvent(entry.Name);
|
||||
}
|
||||
|
||||
if (this.asciiTranslate && !entry.IsDirectory) {
|
||||
asciiTrans = !IsBinary(eFile);
|
||||
// original java source :
|
||||
// MimeType mime = null;
|
||||
// string contentType = null;
|
||||
//
|
||||
// try {
|
||||
// contentType = FileTypeMap.getDefaultFileTypeMap(). getContentType( eFile );
|
||||
//
|
||||
// mime = new MimeType( contentType );
|
||||
//
|
||||
// if ( mime.getPrimaryType().
|
||||
// equalsIgnoreCase( "text" ) )
|
||||
// {
|
||||
// asciiTrans = true;
|
||||
// }
|
||||
// else if ( this.transTyper != null )
|
||||
// {
|
||||
// if ( this.transTyper.isAsciiFile( eFile ) )
|
||||
// {
|
||||
// asciiTrans = true;
|
||||
// }
|
||||
// }
|
||||
// } catch ( MimeTypeParseException ex )
|
||||
// {
|
||||
// // IGNORE THIS ERROR...
|
||||
// }
|
||||
//
|
||||
// if (this.debug) {
|
||||
// Console.Error.WriteLine("CREATE TRANS? '" + asciiTrans + "' ContentType='" + contentType + "' PrimaryType='" + mime.getPrimaryType()+ "'" );
|
||||
// }
|
||||
|
||||
if (asciiTrans) {
|
||||
tempFileName = Path.GetTempFileName();
|
||||
|
||||
StreamReader inStream = File.OpenText(eFile);
|
||||
Stream outStream = new BufferedStream(File.Create(tempFileName));
|
||||
|
||||
while (true) {
|
||||
string line = inStream.ReadLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
byte[] data = Encoding.ASCII.GetBytes(line);
|
||||
outStream.Write(data, 0, data.Length);
|
||||
outStream.WriteByte((byte)'\n');
|
||||
}
|
||||
|
||||
inStream.Close();
|
||||
outStream.Flush();
|
||||
outStream.Close();
|
||||
|
||||
entry.Size = new FileInfo(tempFileName).Length;
|
||||
|
||||
eFile = tempFileName;
|
||||
}
|
||||
}
|
||||
|
||||
string newName = null;
|
||||
|
||||
if (this.rootPath != null) {
|
||||
if (entry.Name.StartsWith(this.rootPath)) {
|
||||
newName = entry.Name.Substring(this.rootPath.Length + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
if (this.pathPrefix != null) {
|
||||
newName = (newName == null) ? this.pathPrefix + "/" + entry.Name : this.pathPrefix + "/" + newName;
|
||||
}
|
||||
|
||||
if (newName != null) {
|
||||
entry.Name = newName;
|
||||
}
|
||||
|
||||
this.tarOut.PutNextEntry(entry);
|
||||
|
||||
if (entry.IsDirectory) {
|
||||
if (recurse) {
|
||||
TarEntry[] list = entry.GetDirectoryEntries();
|
||||
for (int i = 0; i < list.Length; ++i) {
|
||||
this.WriteEntry(list[i], recurse);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Stream inputStream = File.OpenRead(eFile);
|
||||
int numWritten = 0;
|
||||
byte[] eBuf = new byte[32 * 1024];
|
||||
while (true) {
|
||||
int numRead = inputStream.Read(eBuf, 0, eBuf.Length);
|
||||
|
||||
if (numRead <=0) {
|
||||
break;
|
||||
}
|
||||
|
||||
this.tarOut.Write(eBuf, 0, numRead);
|
||||
numWritten += numRead;
|
||||
}
|
||||
Console.WriteLine("written " + numWritten + " bytes");
|
||||
|
||||
inputStream.Close();
|
||||
|
||||
if (tempFileName != null && tempFileName.Length > 0) {
|
||||
File.Delete(tempFileName);
|
||||
}
|
||||
|
||||
this.tarOut.CloseEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The original Java file had this header:
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
@@ -0,0 +1,437 @@
|
||||
// TarBuffer.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
/// <summary>
|
||||
/// The TarBuffer class implements the tar archive concept
|
||||
/// of a buffered input stream. This concept goes back to the
|
||||
/// days of blocked tape drives and special io devices. In the
|
||||
/// C# universe, the only real function that this class
|
||||
/// performs is to ensure that files have the correct "block"
|
||||
/// size, or other tars will complain.
|
||||
/// <p>
|
||||
/// You should never have a need to access this class directly.
|
||||
/// TarBuffers are created by Tar IO Streams.
|
||||
/// </p>
|
||||
/// </summary>
|
||||
public class TarBuffer
|
||||
{
|
||||
public static readonly int DEFAULT_RCDSIZE = 512;
|
||||
public static readonly int DEFAULT_BLKSIZE = DEFAULT_RCDSIZE * 20;
|
||||
|
||||
Stream inputStream;
|
||||
Stream outputStream;
|
||||
|
||||
byte[] blockBuffer;
|
||||
int currBlkIdx;
|
||||
int currRecIdx;
|
||||
int blockSize;
|
||||
int recordSize;
|
||||
int recsPerBlock;
|
||||
|
||||
bool debug;
|
||||
|
||||
protected TarBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
public static TarBuffer CreateInputTarBuffer(Stream inputStream)
|
||||
{
|
||||
return CreateInputTarBuffer(inputStream, TarBuffer.DEFAULT_BLKSIZE);
|
||||
}
|
||||
public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockSize )
|
||||
{
|
||||
return CreateInputTarBuffer(inputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
|
||||
}
|
||||
public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockSize, int recordSize)
|
||||
{
|
||||
TarBuffer tarBuffer = new TarBuffer();
|
||||
tarBuffer.inputStream = inputStream;
|
||||
tarBuffer.outputStream = null;
|
||||
tarBuffer.Initialize(blockSize, recordSize);
|
||||
|
||||
return tarBuffer;
|
||||
}
|
||||
|
||||
public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
|
||||
{
|
||||
return CreateOutputTarBuffer(outputStream, TarBuffer.DEFAULT_BLKSIZE);
|
||||
}
|
||||
public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockSize )
|
||||
{
|
||||
return CreateOutputTarBuffer(outputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
|
||||
}
|
||||
public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockSize, int recordSize)
|
||||
{
|
||||
TarBuffer tarBuffer = new TarBuffer();
|
||||
tarBuffer.inputStream = null;
|
||||
tarBuffer.outputStream = outputStream;
|
||||
tarBuffer.Initialize(blockSize, recordSize);
|
||||
|
||||
return tarBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialization common to all constructors.
|
||||
/// </summary>
|
||||
void Initialize(int blockSize, int recordSize)
|
||||
{
|
||||
this.debug = false;
|
||||
this.blockSize = blockSize;
|
||||
this.recordSize = recordSize;
|
||||
this.recsPerBlock = this.blockSize / this.recordSize;
|
||||
this.blockBuffer = new byte[this.blockSize];
|
||||
|
||||
if (inputStream != null) {
|
||||
this.currBlkIdx = -1;
|
||||
this.currRecIdx = this.recsPerBlock;
|
||||
} else {
|
||||
this.currBlkIdx = 0;
|
||||
this.currRecIdx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the TAR Buffer's block size. Blocks consist of multiple records.
|
||||
/// </summary>
|
||||
public int GetBlockSize()
|
||||
{
|
||||
return this.blockSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the TAR Buffer's record size.
|
||||
/// </summary>
|
||||
public int GetRecordSize()
|
||||
{
|
||||
return this.recordSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the debugging flag for the buffer.
|
||||
/// </summary>
|
||||
public void SetDebug(bool debug)
|
||||
{
|
||||
this.debug = debug;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if an archive record indicate End of Archive. End of
|
||||
/// archive is indicated by a record that consists entirely of null bytes.
|
||||
/// </summary>
|
||||
/// <param name = "record">
|
||||
/// The record data to check.
|
||||
/// </param>
|
||||
public bool IsEOFRecord(byte[] record)
|
||||
{
|
||||
for (int i = 0, sz = this.GetRecordSize(); i < sz; ++i) {
|
||||
if (record[i] != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip over a record on the input stream.
|
||||
/// </summary>
|
||||
public void SkipRecord()
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("SkipRecord: recIdx = " + this.currRecIdx + " blkIdx = " + this.currBlkIdx);
|
||||
}
|
||||
|
||||
if (this.inputStream == null) {
|
||||
throw new System.IO.IOException("no input stream defined");
|
||||
}
|
||||
|
||||
if (this.currRecIdx >= this.recsPerBlock) {
|
||||
if (!this.ReadBlock()) {
|
||||
return; // UNDONE
|
||||
}
|
||||
}
|
||||
|
||||
this.currRecIdx++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a record from the input stream and return the data.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The record data.
|
||||
/// </returns>
|
||||
public byte[] ReadRecord()
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine( "ReadRecord: recIdx = " + this.currRecIdx + " blkIdx = " + this.currBlkIdx );
|
||||
}
|
||||
|
||||
if (this.inputStream == null) {
|
||||
throw new System.IO.IOException("no input stream defined");
|
||||
}
|
||||
|
||||
if (this.currRecIdx >= this.recsPerBlock) {
|
||||
if (!this.ReadBlock()) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
byte[] result = new byte[this.recordSize];
|
||||
|
||||
Array.Copy(this.blockBuffer, (this.currRecIdx * this.recordSize), result, 0, this.recordSize );
|
||||
this.currRecIdx++;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <returns>
|
||||
/// false if End-Of-File, else true
|
||||
/// </returns>
|
||||
bool ReadBlock()
|
||||
{
|
||||
Console.WriteLine(this.debug);
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("ReadBlock: blkIdx = " + this.currBlkIdx);
|
||||
}
|
||||
|
||||
if (this.inputStream == null) {
|
||||
throw new System.IO.IOException("no input stream stream defined");
|
||||
}
|
||||
|
||||
this.currRecIdx = 0;
|
||||
|
||||
int offset = 0;
|
||||
int bytesNeeded = this.blockSize;
|
||||
for (; bytesNeeded > 0 ;) {
|
||||
long numBytes = this.inputStream.Read(this.blockBuffer, offset, bytesNeeded);
|
||||
|
||||
//
|
||||
// NOTE
|
||||
// We have fit EOF, and the block is not full!
|
||||
//
|
||||
// This is a broken archive. It does not follow the standard
|
||||
// blocking algorithm. However, because we are generous, and
|
||||
// it requires little effort, we will simply ignore the error
|
||||
// and continue as if the entire block were read. This does
|
||||
// not appear to break anything upstream. We used to return
|
||||
// false in this case.
|
||||
//
|
||||
// Thanks to '[email protected]' for this fix.
|
||||
//
|
||||
if (numBytes <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
offset += (int)numBytes;
|
||||
bytesNeeded -= (int)numBytes;
|
||||
if (numBytes != this.blockSize) {
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("ReadBlock: INCOMPLETE READ " + numBytes + " of " + this.blockSize + " bytes read.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.currBlkIdx++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current block number, zero based.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current zero based block number.
|
||||
/// </returns>
|
||||
public int GetCurrentBlockNum()
|
||||
{
|
||||
return this.currBlkIdx;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current record number, within the current block, zero based.
|
||||
/// Thus, current offset = (currentBlockNum * recsPerBlk) + currentRecNum.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The current zero based record number.
|
||||
/// </returns>
|
||||
public int GetCurrentRecordNum()
|
||||
{
|
||||
return this.currRecIdx - 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an archive record to the archive.
|
||||
/// </summary>
|
||||
/// <param name="record">
|
||||
/// The record data to write to the archive.
|
||||
/// </param>
|
||||
///
|
||||
public void WriteRecord(byte[] record)
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("WriteRecord: recIdx = " + this.currRecIdx + " blkIdx = " + this.currBlkIdx );
|
||||
}
|
||||
|
||||
if (this.outputStream == null) {
|
||||
throw new System.IO.IOException("no output stream defined");
|
||||
}
|
||||
|
||||
if (record.Length != this.recordSize) {
|
||||
throw new IOException("record to write has length '" + record.Length + "' which is not the record size of '" + this.recordSize + "'" );
|
||||
}
|
||||
|
||||
if (this.currRecIdx >= this.recsPerBlock) {
|
||||
this.WriteBlock();
|
||||
}
|
||||
Array.Copy(record, 0, this.blockBuffer, (this.currRecIdx * this.recordSize), this.recordSize );
|
||||
this.currRecIdx++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an archive record to the archive, where the record may be
|
||||
/// inside of a larger array buffer. The buffer must be "offset plus
|
||||
/// record size" long.
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// The buffer containing the record data to write.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// The offset of the record data within buf.
|
||||
/// </param>
|
||||
public void WriteRecord(byte[] buf, int offset)
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("WriteRecord: recIdx = " + this.currRecIdx + " blkIdx = " + this.currBlkIdx );
|
||||
}
|
||||
|
||||
if (this.outputStream == null) {
|
||||
throw new System.IO.IOException("no output stream stream defined");
|
||||
}
|
||||
|
||||
if ((offset + this.recordSize) > buf.Length) {
|
||||
throw new IOException("record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
|
||||
}
|
||||
|
||||
if (this.currRecIdx >= this.recsPerBlock) {
|
||||
this.WriteBlock();
|
||||
}
|
||||
|
||||
Array.Copy(buf, offset, this.blockBuffer, (this.currRecIdx * this.recordSize), this.recordSize );
|
||||
|
||||
this.currRecIdx++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a TarBuffer block to the archive.
|
||||
/// </summary>
|
||||
void WriteBlock()
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("WriteBlock: blkIdx = " + this.currBlkIdx);
|
||||
}
|
||||
|
||||
if (this.outputStream == null) {
|
||||
throw new System.IO.IOException("no output stream defined");
|
||||
}
|
||||
|
||||
this.outputStream.Write(this.blockBuffer, 0, this.blockSize);
|
||||
this.outputStream.Flush();
|
||||
|
||||
this.currRecIdx = 0;
|
||||
this.currBlkIdx++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the current data block if it has any data in it.
|
||||
/// </summary>
|
||||
void Flush()
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("TarBuffer.FlushBlock() called.");
|
||||
}
|
||||
|
||||
if (this.outputStream == null) {
|
||||
throw new System.IO.IOException("no output base stream defined");
|
||||
}
|
||||
|
||||
if (this.currRecIdx > 0) {
|
||||
this.WriteBlock();
|
||||
}
|
||||
outputStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close the TarBuffer. If this is an output buffer, also flush the
|
||||
/// current block before closing.
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("TarBuffer.Close().");
|
||||
}
|
||||
|
||||
if (outputStream != null ) {
|
||||
Flush();
|
||||
|
||||
// if ( this.outStream != System.out
|
||||
// && this.outStream != System.err ) {
|
||||
outputStream.Close();
|
||||
outputStream = null;
|
||||
} else if (inputStream != null) {
|
||||
// if (this.inStream != System.in ) {
|
||||
inputStream.Close();
|
||||
inputStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The original Java file had this header:
|
||||
*
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
@@ -0,0 +1,672 @@
|
||||
// TarEntry.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents an entry in a Tar archive. It consists
|
||||
/// of the entry's header, as well as the entry's File. Entries
|
||||
/// can be instantiated in one of three ways, depending on how
|
||||
/// they are to be used.
|
||||
/// <p>
|
||||
/// TarEntries that are created from the header bytes read from
|
||||
/// an archive are instantiated with the TarEntry( byte[] )
|
||||
/// constructor. These entries will be used when extracting from
|
||||
/// or listing the contents of an archive. These entries have their
|
||||
/// header filled in using the header bytes. They also set the File
|
||||
/// to null, since they reference an archive entry not a file.</p>
|
||||
/// <p>
|
||||
/// TarEntries that are created from Files that are to be written
|
||||
/// into an archive are instantiated with the TarEntry( File )
|
||||
/// constructor. These entries have their header filled in using
|
||||
/// the File's information. They also keep a reference to the File
|
||||
/// for convenience when writing entries.</p>
|
||||
/// <p>
|
||||
/// Finally, TarEntries can be constructed from nothing but a name.
|
||||
/// This allows the programmer to construct the entry by hand, for
|
||||
/// instance when only an InputStream is available for writing to
|
||||
/// the archive, and the header information is constructed from
|
||||
/// other information. In this case the header fields are set to
|
||||
/// defaults and the File is set to null.</p>
|
||||
///
|
||||
/// <p>
|
||||
/// The C structure for a Tar Entry's header is:
|
||||
/// <pre>
|
||||
/// struct header {
|
||||
/// char name[NAMSIZ];
|
||||
/// char mode[8];
|
||||
/// char uid[8];
|
||||
/// char gid[8];
|
||||
/// char size[12];
|
||||
/// char mtime[12];
|
||||
/// char chksum[8];
|
||||
/// char linkflag;
|
||||
/// char linkname[NAMSIZ];
|
||||
/// char magic[8];
|
||||
/// char uname[TUNMLEN];
|
||||
/// char gname[TGNMLEN];
|
||||
/// char devmajor[8];
|
||||
/// char devminor[8];
|
||||
/// } header;
|
||||
/// </pre>
|
||||
/// </p>
|
||||
/// <see cref="TarHeader"/>
|
||||
/// </summary>
|
||||
public class TarEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// If this entry represents a File, this references it.
|
||||
/// </summary>
|
||||
protected string file;
|
||||
|
||||
/// <summary>
|
||||
/// This is the entry's header information.
|
||||
/// </summary>
|
||||
protected TarHeader header;
|
||||
|
||||
/// <summary>
|
||||
/// Only Create Entries with the static CreateXYZ methods or a headerBuffer.
|
||||
/// </summary>
|
||||
private TarEntry()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct an entry from an archive's header bytes. File is set
|
||||
/// to null.
|
||||
/// </summary>
|
||||
/// <param name = "headerBuf">
|
||||
/// The header bytes from a tar archive entry.
|
||||
/// </param>
|
||||
public TarEntry(byte[] headerBuf)
|
||||
{
|
||||
this.Initialize();
|
||||
this.ParseTarHeader(this.header, headerBuf);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Construct an entry with only a name. This allows the programmer
|
||||
/// to construct the entry's header "by hand". File is set to null.
|
||||
/// </summary>
|
||||
public static TarEntry CreateTarEntry(string name)
|
||||
{
|
||||
TarEntry entry = new TarEntry();
|
||||
entry.Initialize();
|
||||
entry.NameTarHeader(entry.header, name);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct an entry for a file. File is set to file, and the
|
||||
/// header is constructed from information from the file.
|
||||
/// </summary>
|
||||
/// <param name = "fileName">
|
||||
/// The file that the entry represents.
|
||||
/// </param>
|
||||
public static TarEntry CreateEntryFromFile(string fileName)
|
||||
{
|
||||
TarEntry entry = new TarEntry();
|
||||
entry.Initialize();
|
||||
entry.GetFileTarHeader(entry.header, fileName);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialization code common to all constructors.
|
||||
/// </summary>
|
||||
void Initialize()
|
||||
{
|
||||
this.file = null;
|
||||
this.header = new TarHeader();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the two entries are equal. Equality is determined
|
||||
/// by the header names being equal.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if the entries are equal.
|
||||
/// </returns>
|
||||
public override bool Equals(object it)
|
||||
{
|
||||
if (!(it is TarEntry)) {
|
||||
return false;
|
||||
}
|
||||
return this.header.name.ToString().Equals(((TarEntry)it).header.name.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Must be overridden when you override Equals.
|
||||
/// </summary>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.header.name.ToString().GetHashCode();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the given entry is a descendant of this entry.
|
||||
/// Descendancy is determined by the name of the descendant
|
||||
/// starting with this entry's name.
|
||||
/// </summary>
|
||||
/// <param name = "desc">
|
||||
/// Entry to be checked as a descendent of this.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if entry is a descendant of this.
|
||||
/// </returns>
|
||||
public bool IsDescendent(TarEntry desc)
|
||||
{
|
||||
return desc.header.name.ToString().StartsWith(this.header.name.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get this entry's header.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// This entry's TarHeader.
|
||||
/// </returns>
|
||||
public TarHeader TarHeader {
|
||||
get {
|
||||
return this.header;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/Set this entry's name.
|
||||
/// </summary>
|
||||
public string Name {
|
||||
get {
|
||||
return this.header.name.ToString();
|
||||
}
|
||||
set {
|
||||
this.header.name = new StringBuilder(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/set this entry's user id.
|
||||
/// </summary>
|
||||
public int UserId {
|
||||
get {
|
||||
return this.header.userId;
|
||||
}
|
||||
set {
|
||||
this.header.userId = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/set this entry's group id.
|
||||
/// </summary>
|
||||
public int GroupId {
|
||||
get {
|
||||
return this.header.groupId;
|
||||
}
|
||||
set {
|
||||
this.header.groupId = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/set this entry's user name.
|
||||
/// </summary>
|
||||
public string UserName {
|
||||
get {
|
||||
return this.header.userName.ToString();
|
||||
}
|
||||
set {
|
||||
this.header.userName = new StringBuilder(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/set this entry's group name.
|
||||
/// </summary>
|
||||
public string GroupName {
|
||||
get {
|
||||
return this.header.groupName.ToString();
|
||||
}
|
||||
set {
|
||||
this.header.groupName = new StringBuilder(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience method to set this entry's group and user ids.
|
||||
/// </summary>
|
||||
/// <param name="userId">
|
||||
/// This entry's new user id.
|
||||
/// </param>
|
||||
/// <param name="groupId">
|
||||
/// This entry's new group id.
|
||||
/// </param>
|
||||
public void SetIds(int userId, int groupId)
|
||||
{
|
||||
UserId = userId;
|
||||
GroupId = groupId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience method to set this entry's group and user names.
|
||||
/// </summary>
|
||||
/// <param name="userName">
|
||||
/// This entry's new user name.
|
||||
/// </param>
|
||||
/// <param name="groupName">
|
||||
/// This entry's new group name.
|
||||
/// </param>
|
||||
public void SetNames(string userName, string groupName)
|
||||
{
|
||||
UserName = userName;
|
||||
GroupName = groupName;
|
||||
}
|
||||
|
||||
// TODO :
|
||||
// /**
|
||||
// * Set this entry's modification time. The parameter passed
|
||||
// * to this method is in "Java time".
|
||||
// *
|
||||
// * @param time This entry's new modification time.
|
||||
// */
|
||||
// public void setModTime( long time )
|
||||
// {
|
||||
// this.header.modTime = time / 1000;
|
||||
// }
|
||||
|
||||
/// Convert time to DateTimes
|
||||
/**
|
||||
* Get/Set this entry's modification time.
|
||||
*
|
||||
* @param time This entry's new modification time.
|
||||
*/
|
||||
public DateTime ModTime {
|
||||
get {
|
||||
return this.header.modTime;
|
||||
}
|
||||
set {
|
||||
this.header.modTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get this entry's file.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// This entry's file.
|
||||
/// </returns>
|
||||
public string File {
|
||||
get {
|
||||
return this.file;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/set this entry's file size.
|
||||
/// </summary>
|
||||
public long Size {
|
||||
get {
|
||||
return this.header.size;
|
||||
}
|
||||
set {
|
||||
this.header.size = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience method that will modify an entry's name directly
|
||||
/// in place in an entry header buffer byte array.
|
||||
/// </summary>
|
||||
/// <param name="outbuf">
|
||||
/// The buffer containing the entry header to modify.
|
||||
/// </param>
|
||||
/// <param name="newName">
|
||||
/// The new name to place into the header buffer.
|
||||
/// </param>
|
||||
public void AdjustEntryName(byte[] outbuf, string newName)
|
||||
{
|
||||
int offset = 0;
|
||||
offset = TarHeader.GetNameBytes(new StringBuilder(newName), outbuf, offset, TarHeader.NAMELEN);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return whether or not this entry represents a directory.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if this entry is a directory.
|
||||
/// </returns>
|
||||
public bool IsDirectory
|
||||
{
|
||||
get {
|
||||
if (this.file != null) {
|
||||
return Directory.Exists(file);
|
||||
}
|
||||
|
||||
if (this.header != null) {
|
||||
if (this.header.linkFlag == TarHeader.LF_DIR || this.header.name.ToString().EndsWith( "/" )) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill in a TarHeader with information from a File.
|
||||
/// </summary>
|
||||
/// <param name="hdr">
|
||||
/// The TarHeader to fill in.
|
||||
/// </param>
|
||||
/// <param name="file">
|
||||
/// The file from which to get the header information.
|
||||
/// </param>
|
||||
public void GetFileTarHeader(TarHeader hdr, string file)
|
||||
{
|
||||
this.file = file;
|
||||
|
||||
string name = Path.GetDirectoryName(file);
|
||||
|
||||
if (Path.DirectorySeparatorChar == '\\') { // check if the OS is a windows
|
||||
// Strip off drive letters!
|
||||
if (name.Length > 2) {
|
||||
char ch1 = name[0];
|
||||
char ch2 = name[1];
|
||||
|
||||
if (ch2 == ':' && Char.IsLetter(ch1)) {
|
||||
name = name.Substring(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
name = name.Replace(Path.DirectorySeparatorChar, '/');
|
||||
|
||||
// No absolute pathnames
|
||||
// Windows (and Posix?) paths can start with "\\NetworkDrive\",
|
||||
// so we loop on starting /'s.
|
||||
while (name.StartsWith("/")) {
|
||||
name = name.Substring(1);
|
||||
}
|
||||
|
||||
hdr.linkName = new StringBuilder(String.Empty);
|
||||
hdr.name = new StringBuilder(name);
|
||||
|
||||
if (Directory.Exists(file)) {
|
||||
hdr.mode = 040755; // TODO : what does this magic number ?? Mike
|
||||
hdr.linkFlag = TarHeader.LF_DIR;
|
||||
if (hdr.name[hdr.name.Length - 1] != '/') {
|
||||
hdr.name.Append("/");
|
||||
}
|
||||
hdr.size = 0;
|
||||
} else {
|
||||
hdr.mode = 0100644; // TODO : again a magic number
|
||||
hdr.linkFlag = TarHeader.LF_NORMAL;
|
||||
Console.WriteLine(file.Replace('/', Path.DirectorySeparatorChar));
|
||||
hdr.size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
|
||||
}
|
||||
|
||||
// UNDONE When File lets us get the userName, use it!
|
||||
hdr.modTime = System.IO.File.GetLastAccessTime(file.Replace('/', Path.DirectorySeparatorChar));
|
||||
hdr.checkSum = 0;
|
||||
hdr.devMajor = 0;
|
||||
hdr.devMinor = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If this entry represents a file, and the file is a directory, return
|
||||
/// an array of TarEntries for this entry's children.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// An array of TarEntry's for this entry's children.
|
||||
/// </returns>
|
||||
public TarEntry[] GetDirectoryEntries()
|
||||
{
|
||||
if (this.file == null || !Directory.Exists(this.file)) {
|
||||
return new TarEntry[0];
|
||||
}
|
||||
|
||||
string[] list = Directory.GetFileSystemEntries(this.file);
|
||||
TarEntry[] result = new TarEntry[list.Length];
|
||||
|
||||
string dirName = file;
|
||||
if (!dirName.EndsWith(Path.DirectorySeparatorChar.ToString())) {
|
||||
dirName += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
for (int i = 0; i < list.Length; ++i) {
|
||||
result[i] = TarEntry.CreateEntryFromFile(list[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute the checksum of a tar entry header.
|
||||
/// </summary>
|
||||
/// <param name = "buf">
|
||||
/// The tar entry's header buffer.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The computed checksum.
|
||||
/// </returns>
|
||||
public long ComputeCheckSum(byte[] buf)
|
||||
{
|
||||
long sum = 0;
|
||||
for (int i = 0; i < buf.Length; ++i) {
|
||||
sum += 255 & buf[i]; // TODO : I think the 255 & x isn't neccessary += buf[i] should be enough. CHECK IT!
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an entry's header information to a header buffer.
|
||||
/// </summary>
|
||||
/// <param name = "outbuf">
|
||||
/// The tar entry header buffer to fill in.
|
||||
/// </param>
|
||||
public void WriteEntryHeader(byte[] outbuf)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
offset = TarHeader.GetNameBytes(this.header.name, outbuf, offset, TarHeader.NAMELEN);
|
||||
offset = TarHeader.GetOctalBytes(this.header.mode, outbuf, offset, TarHeader.MODELEN);
|
||||
offset = TarHeader.GetOctalBytes(this.header.userId, outbuf, offset, TarHeader.UIDLEN);
|
||||
offset = TarHeader.GetOctalBytes(this.header.groupId, outbuf, offset, TarHeader.GIDLEN);
|
||||
|
||||
long size = this.header.size;
|
||||
|
||||
offset = TarHeader.GetLongOctalBytes(size, outbuf, offset, TarHeader.SIZELEN);
|
||||
offset = TarHeader.GetLongOctalBytes(GetCTime(this.header.modTime), outbuf, offset, TarHeader.MODTIMELEN);
|
||||
|
||||
int csOffset = offset;
|
||||
for (int c = 0; c < TarHeader.CHKSUMLEN; ++c) {
|
||||
outbuf[offset++] = (byte)' ';
|
||||
}
|
||||
|
||||
outbuf[offset++] = this.header.linkFlag;
|
||||
|
||||
offset = TarHeader.GetNameBytes(this.header.linkName, outbuf, offset, TarHeader.NAMELEN);
|
||||
offset = TarHeader.GetNameBytes(this.header.magic, outbuf, offset, TarHeader.MAGICLEN);
|
||||
offset = TarHeader.GetNameBytes(this.header.userName, outbuf, offset, TarHeader.UNAMELEN);
|
||||
offset = TarHeader.GetNameBytes(this.header.groupName, outbuf, offset, TarHeader.GNAMELEN);
|
||||
|
||||
offset = TarHeader.GetOctalBytes(this.header.devMajor, outbuf, offset, TarHeader.DEVLEN);
|
||||
offset = TarHeader.GetOctalBytes(this.header.devMinor, outbuf, offset, TarHeader.DEVLEN);
|
||||
|
||||
for (; offset < outbuf.Length;) {
|
||||
outbuf[offset++] = 0;
|
||||
}
|
||||
|
||||
long checkSum = this.ComputeCheckSum(outbuf);
|
||||
|
||||
TarHeader.GetCheckSumOctalBytes(checkSum, outbuf, csOffset, TarHeader.CHKSUMLEN);
|
||||
}
|
||||
|
||||
// time conversion functions
|
||||
readonly static long timeConversionFactor = 10000000L;
|
||||
readonly static DateTime datetTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||||
|
||||
static int GetCTime(System.DateTime dateTime)
|
||||
{
|
||||
return (int)((dateTime.Ticks - datetTime1970.Ticks) / timeConversionFactor);
|
||||
}
|
||||
|
||||
static DateTime GetDateTimeFromCTime(long ticks)
|
||||
{
|
||||
return new DateTime(datetTime1970.Ticks + ticks * timeConversionFactor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an entry's TarHeader information from a header buffer.
|
||||
/// </summary>
|
||||
/// <param name ="hdr">
|
||||
/// Parse an entry's TarHeader information from a header buffer.
|
||||
/// </param>
|
||||
/// <param name = "header">
|
||||
/// The tar entry header buffer to get information from.
|
||||
/// </param>
|
||||
public void ParseTarHeader(TarHeader hdr, byte[] header)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
hdr.name = TarHeader.ParseName(header, offset, TarHeader.NAMELEN);
|
||||
|
||||
offset += TarHeader.NAMELEN;
|
||||
|
||||
hdr.mode = (int)TarHeader.ParseOctal(header, offset, TarHeader.MODELEN);
|
||||
|
||||
offset += TarHeader.MODELEN;
|
||||
|
||||
hdr.userId = (int)TarHeader.ParseOctal(header, offset, TarHeader.UIDLEN);
|
||||
|
||||
offset += TarHeader.UIDLEN;
|
||||
|
||||
hdr.groupId = (int)TarHeader.ParseOctal(header, offset, TarHeader.GIDLEN);
|
||||
|
||||
offset += TarHeader.GIDLEN;
|
||||
|
||||
hdr.size = TarHeader.ParseOctal(header, offset, TarHeader.SIZELEN);
|
||||
|
||||
offset += TarHeader.SIZELEN;
|
||||
|
||||
hdr.modTime = GetDateTimeFromCTime(TarHeader.ParseOctal(header, offset, TarHeader.MODTIMELEN));
|
||||
|
||||
offset += TarHeader.MODTIMELEN;
|
||||
|
||||
hdr.checkSum = (int)TarHeader.ParseOctal(header, offset, TarHeader.CHKSUMLEN);
|
||||
|
||||
offset += TarHeader.CHKSUMLEN;
|
||||
|
||||
hdr.linkFlag = header[ offset++ ];
|
||||
|
||||
hdr.linkName = TarHeader.ParseName(header, offset, TarHeader.NAMELEN);
|
||||
|
||||
offset += TarHeader.NAMELEN;
|
||||
|
||||
hdr.magic = TarHeader.ParseName(header, offset, TarHeader.MAGICLEN);
|
||||
|
||||
offset += TarHeader.MAGICLEN;
|
||||
|
||||
hdr.userName = TarHeader.ParseName(header, offset, TarHeader.UNAMELEN);
|
||||
|
||||
offset += TarHeader.UNAMELEN;
|
||||
|
||||
hdr.groupName = TarHeader.ParseName(header, offset, TarHeader.GNAMELEN);
|
||||
|
||||
offset += TarHeader.GNAMELEN;
|
||||
|
||||
hdr.devMajor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
|
||||
|
||||
offset += TarHeader.DEVLEN;
|
||||
|
||||
hdr.devMinor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill in a TarHeader given only the entry's name.
|
||||
/// </summary>
|
||||
/// <param name="hdr">
|
||||
/// The TarHeader to fill in.
|
||||
/// </param>
|
||||
/// <param name="name">
|
||||
/// The tar entry name.
|
||||
/// </param>
|
||||
public void NameTarHeader(TarHeader hdr, string name)
|
||||
{
|
||||
bool isDir = name.EndsWith("/");
|
||||
|
||||
hdr.checkSum = 0;
|
||||
hdr.devMajor = 0;
|
||||
hdr.devMinor = 0;
|
||||
|
||||
hdr.name = new StringBuilder(name);
|
||||
hdr.mode = isDir ? 040755 : 0100644; // TODO : I think I've seen these magics before ...
|
||||
hdr.userId = 0;
|
||||
hdr.groupId = 0;
|
||||
hdr.size = 0;
|
||||
hdr.checkSum = 0;
|
||||
|
||||
hdr.modTime = DateTime.Now;//(new java.util.Date()).getTime() / 1000;
|
||||
|
||||
hdr.linkFlag = isDir ? TarHeader.LF_DIR : TarHeader.LF_NORMAL;
|
||||
|
||||
hdr.linkName = new StringBuilder(String.Empty);
|
||||
hdr.userName = new StringBuilder(String.Empty);
|
||||
hdr.groupName = new StringBuilder(String.Empty);
|
||||
|
||||
hdr.devMajor = 0;
|
||||
hdr.devMinor = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The original Java file had this header:
|
||||
*
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
@@ -0,0 +1,500 @@
|
||||
// TarHeader.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This class encapsulates the Tar Entry Header used in Tar Archives.
|
||||
/// The class also holds a number of tar constants, used mostly in headers.
|
||||
/// </summary>
|
||||
public class TarHeader : ICloneable
|
||||
{
|
||||
/// <summary>
|
||||
/// The length of the name field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int NAMELEN = 100;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the mode field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int MODELEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the user id field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int UIDLEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the group id field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int GIDLEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the checksum field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int CHKSUMLEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the size field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int SIZELEN = 12;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the magic field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int MAGICLEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the modification time field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int MODTIMELEN = 12;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the user name field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int UNAMELEN = 32;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the group name field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int GNAMELEN = 32;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the devices field in a header buffer.
|
||||
/// </summary>
|
||||
public readonly static int DEVLEN = 8;
|
||||
|
||||
/// <summary>
|
||||
/// LF_ constants represent the "link flag" of an entry, or more commonly,
|
||||
/// the "entry type". This is the "old way" of indicating a normal file.
|
||||
/// </summary>
|
||||
public readonly static byte LF_OLDNORM = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Normal file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_NORMAL = (byte) '0';
|
||||
|
||||
/// <summary>
|
||||
/// Link file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_LINK = (byte) '1';
|
||||
|
||||
/// <summary>
|
||||
/// Symbolic link file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_SYMLINK = (byte) '2';
|
||||
|
||||
/// <summary>
|
||||
/// Character device file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_CHR = (byte) '3';
|
||||
|
||||
/// <summary>
|
||||
/// Block device file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_BLK = (byte) '4';
|
||||
|
||||
/// <summary>
|
||||
/// Directory file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_DIR = (byte) '5';
|
||||
|
||||
/// <summary>
|
||||
/// FIFO (pipe) file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_FIFO = (byte) '6';
|
||||
|
||||
/// <summary>
|
||||
/// Contiguous file type.
|
||||
/// </summary>
|
||||
public readonly static byte LF_CONTIG = (byte) '7';
|
||||
|
||||
/// <summary>
|
||||
/// The magic tag representing a POSIX tar archive.
|
||||
/// </summary>
|
||||
public readonly static string TMAGIC = "ustar";
|
||||
|
||||
/// <summary>
|
||||
/// The magic tag representing a GNU tar archive.
|
||||
/// </summary>
|
||||
public readonly static string GNU_TMAGIC = "ustar ";
|
||||
|
||||
/// <summary>
|
||||
/// The entry's name.
|
||||
/// </summary>
|
||||
public StringBuilder name;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's permission mode.
|
||||
/// </summary>
|
||||
public int mode;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's user id.
|
||||
/// </summary>
|
||||
public int userId;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's group id.
|
||||
/// </summary>
|
||||
public int groupId;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's size.
|
||||
/// </summary>
|
||||
public long size;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's modification time.
|
||||
/// </summary>
|
||||
public DateTime modTime;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's checksum.
|
||||
/// </summary>
|
||||
public int checkSum;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's link flag.
|
||||
/// </summary>
|
||||
public byte linkFlag;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's link name.
|
||||
/// </summary>
|
||||
public StringBuilder linkName;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's magic tag.
|
||||
/// </summary>
|
||||
public StringBuilder magic;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's user name.
|
||||
/// </summary>
|
||||
public StringBuilder userName;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's group name.
|
||||
/// </summary>
|
||||
public StringBuilder groupName;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's major device number.
|
||||
/// </summary>
|
||||
public int devMajor;
|
||||
|
||||
/// <summary>
|
||||
/// The entry's minor device number.
|
||||
/// </summary>
|
||||
public int devMinor;
|
||||
|
||||
public TarHeader()
|
||||
{
|
||||
this.magic = new StringBuilder(TarHeader.TMAGIC);
|
||||
|
||||
this.name = new StringBuilder();
|
||||
this.linkName = new StringBuilder();
|
||||
|
||||
string user = Environment.UserName;
|
||||
|
||||
if (user.Length > 31) {
|
||||
user = user.Substring(0, 31);
|
||||
}
|
||||
|
||||
this.userId = 0;
|
||||
this.groupId = 0;
|
||||
this.userName = new StringBuilder(user);
|
||||
this.groupName = new StringBuilder(String.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TarHeaders can be cloned.
|
||||
/// </summary>
|
||||
public object Clone()
|
||||
{
|
||||
TarHeader hdr = new TarHeader();
|
||||
|
||||
hdr.name = (this.name == null) ? null : new StringBuilder(this.name.ToString());
|
||||
hdr.mode = this.mode;
|
||||
hdr.userId = this.userId;
|
||||
hdr.groupId = this.groupId;
|
||||
hdr.size = this.size;
|
||||
hdr.modTime = this.modTime;
|
||||
hdr.checkSum = this.checkSum;
|
||||
hdr.linkFlag = this.linkFlag;
|
||||
hdr.linkName = (this.linkName == null) ? null : new StringBuilder(this.linkName.ToString());
|
||||
hdr.magic = (this.magic == null) ? null : new StringBuilder(this.magic.ToString());
|
||||
hdr.userName = (this.userName == null) ? null : new StringBuilder(this.userName.ToString());
|
||||
hdr.groupName = (this.groupName == null) ? null : new StringBuilder(this.groupName.ToString());
|
||||
hdr.devMajor = this.devMajor;
|
||||
hdr.devMinor = this.devMinor;
|
||||
|
||||
return hdr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the name of this entry.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The entry's name.
|
||||
/// </returns>
|
||||
public string GetName()
|
||||
{
|
||||
return this.name.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an octal string from a header buffer. This is used for the
|
||||
/// file permission mode value.
|
||||
/// </summary>
|
||||
/// <param name = "header">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The long value of the octal string.
|
||||
/// </returns>
|
||||
public static long ParseOctal(byte[] header, int offset, int length)
|
||||
{
|
||||
long result = 0;
|
||||
bool stillPadding = true;
|
||||
|
||||
int end = offset + length;
|
||||
for (int i = offset; i < end ; ++i) {
|
||||
if (header[i] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (header[i] == (byte)' ' || header[i] == '0') {
|
||||
if (stillPadding) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (header[i] == (byte)' ') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stillPadding = false;
|
||||
|
||||
result = (result << 3) + (header[i] - '0');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an entry name from a header buffer.
|
||||
/// </summary>
|
||||
/// <param name="header">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The header's entry name.
|
||||
/// </returns>
|
||||
public static StringBuilder ParseName(byte[] header, int offset, int length)
|
||||
{
|
||||
StringBuilder result = new StringBuilder(length);
|
||||
|
||||
for (int i = offset; i < offset + length; ++i) {
|
||||
if (header[i] == 0) {
|
||||
break;
|
||||
}
|
||||
result.Append((char)header[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine the number of bytes in an entry name.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// </param>
|
||||
/// <param name="buf">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The number of bytes in a header's entry name.
|
||||
/// </returns>
|
||||
public static int GetNameBytes(StringBuilder name, byte[] buf, int offset, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < length && i < name.Length; ++i) {
|
||||
buf[offset + i] = (byte)name[i];
|
||||
}
|
||||
|
||||
for (; i < length ; ++i) {
|
||||
buf[offset + i] = 0;
|
||||
}
|
||||
|
||||
return offset + length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an octal integer from a header buffer.
|
||||
/// </summary>
|
||||
/// <param name = "val">
|
||||
/// </param>
|
||||
/// <param name = "buf">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The integer value of the octal bytes.
|
||||
/// </returns>
|
||||
public static int GetOctalBytes(long val, byte[] buf, int offset, int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
|
||||
int idx = length - 1;
|
||||
|
||||
buf[offset + idx] = 0;
|
||||
--idx;
|
||||
buf[offset + idx] = (byte)' ';
|
||||
--idx;
|
||||
|
||||
if (val == 0) {
|
||||
buf[offset + idx] = (byte)'0';
|
||||
--idx;
|
||||
} else {
|
||||
for (long v = val; idx >= 0 && v > 0; --idx) {
|
||||
buf[offset + idx] = (byte)((byte)'0' + (byte)(v & 7));
|
||||
v >>= 3;
|
||||
}
|
||||
}
|
||||
|
||||
for (; idx >= 0; --idx) {
|
||||
buf[offset + idx] = (byte)' ';
|
||||
}
|
||||
|
||||
return offset + length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an octal long integer from a header buffer.
|
||||
/// </summary>
|
||||
/// <param name = "val">
|
||||
/// </param>
|
||||
/// <param name = "buf">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The long value of the octal bytes.
|
||||
/// </returns>
|
||||
public static int GetLongOctalBytes(long val, byte[] buf, int offset, int length)
|
||||
{
|
||||
byte[] temp = new byte[length + 1];
|
||||
TarHeader.GetOctalBytes(val, temp, 0, length + 1);
|
||||
Array.Copy(temp, 0, buf, offset, length);
|
||||
return offset + length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse the checksum octal integer from a header buffer.
|
||||
/// </summary>
|
||||
/// <param name = "val">
|
||||
/// </param>
|
||||
/// <param name = "buf">
|
||||
/// The header buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "offset">
|
||||
/// The offset into the buffer from which to parse.
|
||||
/// </param>
|
||||
/// <param name = "length">
|
||||
/// The number of header bytes to parse.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The integer value of the entry's checksum.
|
||||
/// </returns>
|
||||
public static int GetCheckSumOctalBytes(long val, byte[] buf, int offset, int length)
|
||||
{
|
||||
TarHeader.GetOctalBytes(val, buf, offset, length);
|
||||
buf[offset + length - 1] = (byte)' ';
|
||||
buf[offset + length - 2] = 0;
|
||||
return offset + length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The original Java file had this header:
|
||||
*
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
@@ -0,0 +1,522 @@
|
||||
// TarInputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
/// <summary>
|
||||
/// The TarInputStream reads a UNIX tar archive as an InputStream.
|
||||
/// methods are provided to position at each successive entry in
|
||||
/// the archive, and the read each entry as a normal input stream
|
||||
/// using read().
|
||||
/// </summary>
|
||||
public class TarInputStream : Stream
|
||||
{
|
||||
protected bool debug;
|
||||
protected bool hasHitEOF;
|
||||
|
||||
protected int entrySize;
|
||||
protected int entryOffset;
|
||||
|
||||
protected byte[] readBuf;
|
||||
|
||||
protected TarBuffer buffer;
|
||||
protected TarEntry currEntry;
|
||||
protected IEntryFactory eFactory;
|
||||
|
||||
Stream inputStream;
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanRead {
|
||||
get {
|
||||
return inputStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanSeek {
|
||||
get {
|
||||
return inputStream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanWrite {
|
||||
get {
|
||||
return inputStream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Length {
|
||||
get {
|
||||
return inputStream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Position {
|
||||
get {
|
||||
return inputStream.Position;
|
||||
}
|
||||
set {
|
||||
inputStream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the baseInputStream
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
inputStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return inputStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void SetLength(long val)
|
||||
{
|
||||
inputStream.SetLength(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void Write(byte[] array, int offset, int count)
|
||||
{
|
||||
inputStream.Write(array, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void WriteByte(byte val)
|
||||
{
|
||||
inputStream.WriteByte(val);
|
||||
}
|
||||
|
||||
|
||||
public TarInputStream(Stream inputStream) : this(inputStream, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE)
|
||||
{
|
||||
}
|
||||
|
||||
public TarInputStream(Stream inputStream, int blockSize) : this(inputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE)
|
||||
{
|
||||
}
|
||||
|
||||
public TarInputStream(Stream inputStream, int blockSize, int recordSize)
|
||||
{
|
||||
this.inputStream = inputStream;
|
||||
this.buffer = TarBuffer.CreateInputTarBuffer(inputStream, blockSize, recordSize);
|
||||
|
||||
this.readBuf = null;
|
||||
this.debug = false;
|
||||
this.hasHitEOF = false;
|
||||
this.eFactory = null;
|
||||
}
|
||||
|
||||
public void SetDebug(bool debugF)
|
||||
{
|
||||
this.debug = debugF;
|
||||
SetBufferDebug(debugF);
|
||||
}
|
||||
public void SetBufferDebug(bool debug)
|
||||
{
|
||||
this.buffer.SetDebug(debug);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void SetEntryFactory(IEntryFactory factory)
|
||||
{
|
||||
this.eFactory = factory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes this stream. Calls the TarBuffer's close() method.
|
||||
/// The underlying stream is closed by the TarBuffer.
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
this.buffer.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the record size being used by this stream's TarBuffer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// TarBuffer record size.
|
||||
/// </returns>
|
||||
public int GetRecordSize()
|
||||
{
|
||||
return this.buffer.GetRecordSize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the available data that can be read from the current
|
||||
/// entry in the archive. This does not indicate how much data
|
||||
/// is left in the entire archive, only in the current entry.
|
||||
/// This value is determined from the entry's size header field
|
||||
/// and the amount of data already read from the current entry.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The number of available bytes for the current entry.
|
||||
/// </returns>
|
||||
public int Available {
|
||||
get {
|
||||
return this.entrySize - this.entryOffset;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip bytes in the input buffer. This skips bytes in the
|
||||
/// current entry's data, not the entire archive, and will
|
||||
/// stop at the end of the current entry's data if the number
|
||||
/// to skip extends beyond that point.
|
||||
/// </summary>
|
||||
/// <param name="numToSkip">
|
||||
/// The number of bytes to skip.
|
||||
/// </param>
|
||||
public void Skip(int numToSkip)
|
||||
{
|
||||
// REVIEW
|
||||
// This is horribly inefficient, but it ensures that we
|
||||
// properly skip over bytes via the TarBuffer...
|
||||
//
|
||||
byte[] skipBuf = new byte[8 * 1024];
|
||||
|
||||
for (int num = numToSkip; num > 0;){
|
||||
int numRead = this.Read(skipBuf, 0, (num > skipBuf.Length ? skipBuf.Length : num));
|
||||
|
||||
if (numRead == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
num -= numRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Since we do not support marking just yet, we return false.
|
||||
/// </summary>
|
||||
public bool IsMarkSupported {
|
||||
get {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Since we do not support marking just yet, we do nothing.
|
||||
/// </summary>
|
||||
/// <param name ="markLimit">
|
||||
/// The limit to mark.
|
||||
/// </param>
|
||||
public void Mark(int markLimit)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Since we do not support marking just yet, we do nothing.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next entry in this tar archive. This will skip
|
||||
/// over any remaining data in the current entry, if there
|
||||
/// is one, and place the input stream at the header of the
|
||||
/// next entry, and read the header and instantiate a new
|
||||
/// TarEntry from the header bytes and return that entry.
|
||||
/// If there are no more entries in the archive, null will
|
||||
/// be returned to indicate that the end of the archive has
|
||||
/// been reached.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The next TarEntry in the archive, or null.
|
||||
/// </returns>
|
||||
public TarEntry GetNextEntry()
|
||||
{
|
||||
if (this.hasHitEOF) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.currEntry != null) {
|
||||
int numToSkip = this.entrySize - this.entryOffset;
|
||||
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("TarInputStream: SKIP currENTRY '" + this.currEntry.Name + "' SZ " + this.entrySize + " OFF " + this.entryOffset + " skipping " + numToSkip + " bytes");
|
||||
}
|
||||
|
||||
if (numToSkip > 0) {
|
||||
this.Skip(numToSkip);
|
||||
}
|
||||
|
||||
this.readBuf = null;
|
||||
}
|
||||
|
||||
byte[] headerBuf = this.buffer.ReadRecord();
|
||||
|
||||
if (headerBuf == null) {
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("READ NULL RECORD");
|
||||
}
|
||||
|
||||
this.hasHitEOF = true;
|
||||
} else if (this.buffer.IsEOFRecord(headerBuf)) {
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine( "READ EOF RECORD" );
|
||||
}
|
||||
|
||||
this.hasHitEOF = true;
|
||||
}
|
||||
|
||||
if (this.hasHitEOF) {
|
||||
this.currEntry = null;
|
||||
} else {
|
||||
try {
|
||||
if (this.eFactory == null) {
|
||||
this.currEntry = new TarEntry(headerBuf);
|
||||
} else {
|
||||
this.currEntry = this.eFactory.CreateEntry(headerBuf);
|
||||
}
|
||||
|
||||
if (!(headerBuf[257] == 'u' && headerBuf[258] == 's' && headerBuf[259] == 't' && headerBuf[260] == 'a' && headerBuf[261] == 'r')) {
|
||||
throw new InvalidHeaderException("header magic is not 'ustar', but '" + headerBuf[257] + headerBuf[258] + headerBuf[259] + headerBuf[260] + headerBuf[261] +
|
||||
"', or (dec) " + ((int)headerBuf[257]) + ", " + ((int)headerBuf[258]) + ", " + ((int)headerBuf[259]) + ", " + ((int)headerBuf[260]) + ", " + ((int)headerBuf[261]));
|
||||
}
|
||||
|
||||
if (this.debug) {
|
||||
Console.Error.WriteLine("TarInputStream: SET CURRENTRY '" + this.currEntry.Name + "' size = " + this.currEntry.Size);
|
||||
}
|
||||
|
||||
this.entryOffset = 0;
|
||||
|
||||
// REVIEW How do we resolve this discrepancy?!
|
||||
this.entrySize = (int) this.currEntry.Size;
|
||||
} catch (InvalidHeaderException ex) {
|
||||
this.entrySize = 0;
|
||||
this.entryOffset = 0;
|
||||
this.currEntry = null;
|
||||
throw new InvalidHeaderException("bad header in block " + this.buffer.GetCurrentBlockNum() + " record " + this.buffer.GetCurrentRecordNum() + ", " + ex.Message);
|
||||
}
|
||||
}
|
||||
return this.currEntry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a byte from the current tar archive entry.
|
||||
/// This method simply calls read(byte[], int, int).
|
||||
/// </summary>
|
||||
public override int ReadByte()
|
||||
{
|
||||
byte[] oneByteBuffer = new byte[1];
|
||||
int num = this.Read(oneByteBuffer, 0, 1);
|
||||
if (num <= 0) { // return -1 to indicate that no byte was read.
|
||||
return -1;
|
||||
}
|
||||
return (int)oneByteBuffer[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads bytes from the current tar archive entry.
|
||||
///
|
||||
/// This method is aware of the boundaries of the current
|
||||
/// entry in the archive and will deal with them as if they
|
||||
/// entry in the archive and will deal with them as if they
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// The buffer into which to place bytes read.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// The offset at which to place bytes read.
|
||||
/// </param>
|
||||
/// <param name="numToRead">
|
||||
/// The number of bytes to read.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The number of bytes read, or -1 at EOF.
|
||||
/// </returns>
|
||||
public override int Read(byte[] buf, int offset, int numToRead)
|
||||
{
|
||||
int totalRead = 0;
|
||||
|
||||
if (this.entryOffset >= this.entrySize) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((numToRead + this.entryOffset) > this.entrySize) {
|
||||
numToRead = this.entrySize - this.entryOffset;
|
||||
}
|
||||
|
||||
if (this.readBuf != null) {
|
||||
int sz = (numToRead > this.readBuf.Length) ? this.readBuf.Length : numToRead;
|
||||
|
||||
Array.Copy(this.readBuf, 0, buf, offset, sz);
|
||||
|
||||
if (sz >= this.readBuf.Length) {
|
||||
this.readBuf = null;
|
||||
} else {
|
||||
int newLen = this.readBuf.Length - sz;
|
||||
byte[] newBuf = new byte[ newLen ];
|
||||
Array.Copy(this.readBuf, sz, newBuf, 0, newLen);
|
||||
this.readBuf = newBuf;
|
||||
}
|
||||
|
||||
totalRead += sz;
|
||||
numToRead -= sz;
|
||||
offset += sz;
|
||||
}
|
||||
|
||||
while (numToRead > 0) {
|
||||
byte[] rec = this.buffer.ReadRecord();
|
||||
if (rec == null) {
|
||||
// Unexpected EOF!
|
||||
throw new IOException("unexpected EOF with " + numToRead + " bytes unread");
|
||||
}
|
||||
|
||||
int sz = numToRead;
|
||||
int recLen = rec.Length;
|
||||
|
||||
if (recLen > sz) {
|
||||
Array.Copy(rec, 0, buf, offset, sz);
|
||||
this.readBuf = new byte[recLen - sz];
|
||||
Array.Copy(rec, sz, this.readBuf, 0, recLen - sz);
|
||||
} else {
|
||||
sz = recLen;
|
||||
Array.Copy(rec, 0, buf, offset, recLen);
|
||||
}
|
||||
|
||||
totalRead += sz;
|
||||
numToRead -= sz;
|
||||
offset += sz;
|
||||
}
|
||||
|
||||
this.entryOffset += totalRead;
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of the current tar archive entry directly into
|
||||
/// an output stream.
|
||||
/// </summary>
|
||||
/// <param name="outputStream">
|
||||
/// The OutputStream into which to write the entry's data.
|
||||
/// </param>
|
||||
public void CopyEntryContents(Stream outputStream)
|
||||
{
|
||||
byte[] buf = new byte[32 * 1024];
|
||||
|
||||
while (true) {
|
||||
int numRead = this.Read(buf, 0, buf.Length);
|
||||
if (numRead <= 0) {
|
||||
break;
|
||||
}
|
||||
outputStream.Write(buf, 0, numRead);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This interface is provided, with the method setEntryFactory(), to allow
|
||||
/// the programmer to have their own TarEntry subclass instantiated for the
|
||||
/// entries return from getNextEntry().
|
||||
/// </summary>
|
||||
public interface IEntryFactory
|
||||
{
|
||||
TarEntry CreateEntry(string name);
|
||||
|
||||
TarEntry CreateEntryFromFile(string fileName);
|
||||
|
||||
TarEntry CreateEntry(byte[] headerBuf);
|
||||
}
|
||||
|
||||
public class EntryFactoryAdapter : IEntryFactory
|
||||
{
|
||||
public TarEntry CreateEntry(string name)
|
||||
{
|
||||
return TarEntry.CreateTarEntry(name);
|
||||
}
|
||||
|
||||
public TarEntry CreateEntryFromFile(string fileName)
|
||||
{
|
||||
return TarEntry.CreateEntryFromFile(fileName);
|
||||
}
|
||||
|
||||
public TarEntry CreateEntry(byte[] headerBuf)
|
||||
{
|
||||
return new TarEntry(headerBuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* The original Java file had this header:
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,379 @@
|
||||
// TarOutputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Tar {
|
||||
|
||||
/// <summary>
|
||||
/// The TarOutputStream writes a UNIX tar archive as an OutputStream.
|
||||
/// Methods are provided to put entries, and then write their contents
|
||||
/// by writing to this stream using write().
|
||||
/// </summary>
|
||||
/// public
|
||||
public class TarOutputStream : Stream
|
||||
{
|
||||
protected bool debug;
|
||||
protected int currSize;
|
||||
protected int currBytes;
|
||||
protected byte[] recordBuf;
|
||||
protected int assemLen;
|
||||
protected byte[] assemBuf;
|
||||
|
||||
protected TarBuffer buffer;
|
||||
protected Stream outputStream;
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanRead {
|
||||
get {
|
||||
return outputStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanSeek {
|
||||
get {
|
||||
return outputStream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanWrite {
|
||||
get {
|
||||
return outputStream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Length {
|
||||
get {
|
||||
return outputStream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Position {
|
||||
get {
|
||||
return outputStream.Position;
|
||||
}
|
||||
set {
|
||||
outputStream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return outputStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void SetLength(long val)
|
||||
{
|
||||
outputStream.SetLength(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override int ReadByte()
|
||||
{
|
||||
return outputStream.ReadByte();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
return outputStream.Read(b, off, len);
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
outputStream.Flush();
|
||||
}
|
||||
|
||||
public TarOutputStream(Stream outputStream) : this(outputStream, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE)
|
||||
{
|
||||
}
|
||||
|
||||
public TarOutputStream(Stream outputStream, int blockSize) : this(outputStream, blockSize, TarBuffer.DEFAULT_RCDSIZE)
|
||||
{
|
||||
}
|
||||
|
||||
public TarOutputStream(Stream outputStream, int blockSize, int recordSize)
|
||||
{
|
||||
this.outputStream = outputStream;
|
||||
this.buffer = TarBuffer.CreateOutputTarBuffer(outputStream, blockSize, recordSize);
|
||||
|
||||
this.debug = false;
|
||||
this.assemLen = 0;
|
||||
this.assemBuf = new byte[recordSize];
|
||||
this.recordBuf = new byte[recordSize];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the debugging flag.
|
||||
/// </summary>
|
||||
/// <param name = "debugF">
|
||||
/// True to turn on debugging.
|
||||
/// </param>
|
||||
public void SetDebug(bool debugF)
|
||||
{
|
||||
this.debug = debugF;
|
||||
SetBufferDebug(debugF);
|
||||
}
|
||||
|
||||
public void SetBufferDebug(bool debug)
|
||||
{
|
||||
this.buffer.SetDebug(debug);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the TAR archive without closing the underlying OutputStream.
|
||||
/// The result is that the EOF record of nulls is written.
|
||||
/// </summary>
|
||||
public void Finish()
|
||||
{
|
||||
this.WriteEOFRecord();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the TAR archive and closes the underlying OutputStream.
|
||||
/// This means that finish() is called followed by calling the
|
||||
/// TarBuffer's close().
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
this.Finish();
|
||||
this.buffer.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the record size being used by this stream's TarBuffer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The TarBuffer record size.
|
||||
/// </returns>
|
||||
public int GetRecordSize()
|
||||
{
|
||||
return this.buffer.GetRecordSize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Put an entry on the output stream. This writes the entry's
|
||||
/// header record and positions the output stream for writing
|
||||
/// the contents of the entry. Once this method is called, the
|
||||
/// stream is ready for calls to write() to write the entry's
|
||||
/// contents. Once the contents are written, closeEntry()
|
||||
/// <B>MUST</B> be called to ensure that all buffered data
|
||||
/// is completely written to the output stream.
|
||||
/// </summary>
|
||||
/// <param name="entry">
|
||||
/// The TarEntry to be written to the archive.
|
||||
/// </param>
|
||||
public void PutNextEntry(TarEntry entry)
|
||||
{
|
||||
if (entry.TarHeader.name.Length > TarHeader.NAMELEN) {
|
||||
throw new InvalidHeaderException("file name '" + entry.TarHeader.name + "' is too long ( > " + TarHeader.NAMELEN + " bytes )");
|
||||
}
|
||||
|
||||
entry.WriteEntryHeader(this.recordBuf);
|
||||
this.buffer.WriteRecord(this.recordBuf);
|
||||
|
||||
this.currBytes = 0;
|
||||
|
||||
this.currSize = entry.IsDirectory ? 0 : (int)entry.Size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close an entry. This method MUST be called for all file
|
||||
/// entries that contain data. The reason is that we must
|
||||
/// buffer data written to the stream in order to satisfy
|
||||
/// the buffer's record based writes. Thus, there may be
|
||||
/// data fragments still being assembled that must be written
|
||||
/// to the output stream before this entry is closed and the
|
||||
/// next entry written.
|
||||
/// </summary>
|
||||
public void CloseEntry()
|
||||
{
|
||||
if (this.assemLen > 0) {
|
||||
for (int i = this.assemLen; i < this.assemBuf.Length; ++i) {
|
||||
this.assemBuf[i] = 0;
|
||||
}
|
||||
|
||||
this.buffer.WriteRecord(this.assemBuf);
|
||||
|
||||
this.currBytes += this.assemLen;
|
||||
this.assemLen = 0;
|
||||
}
|
||||
|
||||
if (this.currBytes < this.currSize) {
|
||||
throw new IOException("entry closed at '" + this.currBytes + "' before the '" + this.currSize + "' bytes specified in the header were written");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a byte to the current tar archive entry.
|
||||
/// This method simply calls Write(byte[], int, int).
|
||||
/// </summary>
|
||||
/// <param name="b">
|
||||
/// The byte written.
|
||||
/// </param>
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
this.Write(new byte[] { b }, 0, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes bytes to the current tar archive entry. This method
|
||||
/// is aware of the current entry and will throw an exception if
|
||||
/// you attempt to write bytes past the length specified for the
|
||||
/// current entry. The method is also (painfully) aware of the
|
||||
/// record buffering required by TarBuffer, and manages buffers
|
||||
/// that are not a multiple of recordsize in length, including
|
||||
/// assembling records from small buffers.
|
||||
/// </summary>
|
||||
/// <param name = "wBuf">
|
||||
/// The buffer to write to the archive.
|
||||
/// </param>
|
||||
/// <param name = "wOffset">
|
||||
/// The offset in the buffer from which to get bytes.
|
||||
/// </param>
|
||||
/// <param name = "numToWrite">
|
||||
/// The number of bytes to write.
|
||||
/// </param>
|
||||
public override void Write(byte[] wBuf, int wOffset, int numToWrite)
|
||||
{
|
||||
if ((this.currBytes + numToWrite) > this.currSize) {
|
||||
throw new IOException("request to write '" + numToWrite + "' bytes exceeds size in header of '" + this.currSize + "' bytes");
|
||||
}
|
||||
|
||||
//
|
||||
// We have to deal with assembly!!!
|
||||
// The programmer can be writing little 32 byte chunks for all
|
||||
// we know, and we must assemble complete records for writing.
|
||||
// REVIEW Maybe this should be in TarBuffer? Could that help to
|
||||
// eliminate some of the buffer copying.
|
||||
//
|
||||
if (this.assemLen > 0) {
|
||||
if ((this.assemLen + numToWrite ) >= this.recordBuf.Length) {
|
||||
int aLen = this.recordBuf.Length - this.assemLen;
|
||||
|
||||
Array.Copy(this.assemBuf, 0, this.recordBuf, 0, this.assemLen);
|
||||
|
||||
Array.Copy(wBuf, wOffset, this.recordBuf, this.assemLen, aLen);
|
||||
|
||||
this.buffer.WriteRecord(this.recordBuf);
|
||||
|
||||
this.currBytes += this.recordBuf.Length;
|
||||
|
||||
wOffset += aLen;
|
||||
numToWrite -= aLen;
|
||||
|
||||
this.assemLen = 0;
|
||||
} else {// ( (this.assemLen + numToWrite ) < this.recordBuf.length )
|
||||
Array.Copy(wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite);
|
||||
wOffset += numToWrite;
|
||||
this.assemLen += numToWrite;
|
||||
numToWrite -= numToWrite;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// When we get here we have EITHER:
|
||||
// o An empty "assemble" buffer.
|
||||
// o No bytes to write (numToWrite == 0)
|
||||
//
|
||||
while (numToWrite > 0) {
|
||||
if (numToWrite < this.recordBuf.Length) {
|
||||
Array.Copy(wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite);
|
||||
this.assemLen += numToWrite;
|
||||
break;
|
||||
}
|
||||
|
||||
this.buffer.WriteRecord(wBuf, wOffset);
|
||||
|
||||
int num = this.recordBuf.Length;
|
||||
this.currBytes += num;
|
||||
numToWrite -= num;
|
||||
wOffset += num;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an EOF (end of archive) record to the tar archive.
|
||||
/// An EOF record consists of a record of all zeros.
|
||||
/// </summary>
|
||||
void WriteEOFRecord()
|
||||
{
|
||||
for (int i = 0; i < this.recordBuf.Length; ++i) {
|
||||
this.recordBuf[i] = 0;
|
||||
}
|
||||
this.buffer.WriteRecord(this.recordBuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The original Java file had this header:
|
||||
** Authored by Timothy Gerard Endres
|
||||
** <mailto:[email protected]> <http://www.trustice.com>
|
||||
**
|
||||
** This work has been placed into the public domain.
|
||||
** You may use this work in any way and for any purpose you wish.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
|
||||
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
|
||||
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
|
||||
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
|
||||
** REDISTRIBUTION OF THIS SOFTWARE.
|
||||
**
|
||||
*/
|
||||
+542
@@ -0,0 +1,542 @@
|
||||
// Deflater.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// This is the Deflater class. The deflater class compresses input
|
||||
/// with the deflate algorithm described in RFC 1951. It has several
|
||||
/// compression levels and three different strategies described below.
|
||||
///
|
||||
/// This class is <i>not</i> thread safe. This is inherent in the API, due
|
||||
/// to the split of deflate and setInput.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class Deflater
|
||||
{
|
||||
/// <summary>
|
||||
/// The best and slowest compression level. This tries to find very
|
||||
/// long and distant string repetitions.
|
||||
/// </summary>
|
||||
public static int BEST_COMPRESSION = 9;
|
||||
|
||||
/// <summary>
|
||||
/// The worst but fastest compression level.
|
||||
/// </summary>
|
||||
public static int BEST_SPEED = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The default compression level.
|
||||
/// </summary>
|
||||
public static int DEFAULT_COMPRESSION = -1;
|
||||
|
||||
/// <summary>
|
||||
/// This level won't compress at all but output uncompressed blocks.
|
||||
/// </summary>
|
||||
public static int NO_COMPRESSION = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The compression method. This is the only method supported so far.
|
||||
/// There is no need to use this constant at all.
|
||||
/// </summary>
|
||||
public static int DEFLATED = 8;
|
||||
|
||||
/*
|
||||
* The Deflater can do the following state transitions:
|
||||
*
|
||||
* (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---.
|
||||
* / | (2) (5) |
|
||||
* / v (5) |
|
||||
* (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
|
||||
* \ | (3) | ,-------'
|
||||
* | | | (3) /
|
||||
* v v (5) v v
|
||||
* (1) -> BUSY_STATE ----> FINISHING_STATE
|
||||
* | (6)
|
||||
* v
|
||||
* FINISHED_STATE
|
||||
* \_____________________________________/
|
||||
* | (7)
|
||||
* v
|
||||
* CLOSED_STATE
|
||||
*
|
||||
* (1) If we should produce a header we start in INIT_STATE, otherwise
|
||||
* we start in BUSY_STATE.
|
||||
* (2) A dictionary may be set only when we are in INIT_STATE, then
|
||||
* we change the state as indicated.
|
||||
* (3) Whether a dictionary is set or not, on the first call of deflate
|
||||
* we change to BUSY_STATE.
|
||||
* (4) -- intentionally left blank -- :)
|
||||
* (5) FINISHING_STATE is entered, when flush() is called to indicate that
|
||||
* there is no more INPUT. There are also states indicating, that
|
||||
* the header wasn't written yet.
|
||||
* (6) FINISHED_STATE is entered, when everything has been flushed to the
|
||||
* internal pending output buffer.
|
||||
* (7) At any time (7)
|
||||
*
|
||||
*/
|
||||
|
||||
private static int IS_SETDICT = 0x01;
|
||||
private static int IS_FLUSHING = 0x04;
|
||||
private static int IS_FINISHING = 0x08;
|
||||
|
||||
private static int INIT_STATE = 0x00;
|
||||
private static int SETDICT_STATE = 0x01;
|
||||
// private static int INIT_FINISHING_STATE = 0x08;
|
||||
// private static int SETDICT_FINISHING_STATE = 0x09;
|
||||
private static int BUSY_STATE = 0x10;
|
||||
private static int FLUSHING_STATE = 0x14;
|
||||
private static int FINISHING_STATE = 0x1c;
|
||||
private static int FINISHED_STATE = 0x1e;
|
||||
private static int CLOSED_STATE = 0x7f;
|
||||
|
||||
/// <summary>
|
||||
/// Compression level.
|
||||
/// </summary>
|
||||
private int level;
|
||||
|
||||
/// <summary>
|
||||
/// should we include a header.
|
||||
/// </summary>
|
||||
private bool noHeader;
|
||||
|
||||
// /// <summary>
|
||||
// /// Compression strategy.
|
||||
// /// </summary>
|
||||
// private int strategy;
|
||||
|
||||
/// <summary>
|
||||
/// The current state.
|
||||
/// </summary>
|
||||
private int state;
|
||||
|
||||
/// <summary>
|
||||
/// The total bytes of output written.
|
||||
/// </summary>
|
||||
private int totalOut;
|
||||
|
||||
/// <summary>
|
||||
/// The pending output.
|
||||
/// </summary>
|
||||
private DeflaterPending pending;
|
||||
|
||||
/// <summary>
|
||||
/// The deflater engine.
|
||||
/// </summary>
|
||||
private DeflaterEngine engine;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new deflater with default compression level.
|
||||
/// </summary>
|
||||
public Deflater() : this(DEFAULT_COMPRESSION, false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new deflater with given compression level.
|
||||
/// </summary>
|
||||
/// <param name="lvl">
|
||||
/// the compression level, a value between NO_COMPRESSION
|
||||
/// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
|
||||
/// </param>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
|
||||
public Deflater(int lvl) : this(lvl, false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new deflater with given compression level.
|
||||
/// </summary>
|
||||
/// <param name="lvl">
|
||||
/// the compression level, a value between NO_COMPRESSION
|
||||
/// and BEST_COMPRESSION.
|
||||
/// </param>
|
||||
/// <param name="nowrap">
|
||||
/// true, if we should suppress the deflate header at the
|
||||
/// beginning and the adler checksum at the end of the output. This is
|
||||
/// useful for the GZIP format.
|
||||
/// </param>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
|
||||
public Deflater(int lvl, bool nowrap)
|
||||
{
|
||||
if (lvl == DEFAULT_COMPRESSION) {
|
||||
lvl = 6;
|
||||
} else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
|
||||
throw new ArgumentOutOfRangeException("lvl");
|
||||
}
|
||||
|
||||
pending = new DeflaterPending();
|
||||
engine = new DeflaterEngine(pending);
|
||||
this.noHeader = nowrap;
|
||||
SetStrategy(DeflateStrategy.Default);
|
||||
SetLevel(lvl);
|
||||
Reset();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Resets the deflater. The deflater acts afterwards as if it was
|
||||
/// just created with the same compression level and strategy as it
|
||||
/// had before.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
state = (noHeader ? BUSY_STATE : INIT_STATE);
|
||||
totalOut = 0;
|
||||
pending.Reset();
|
||||
engine.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current adler checksum of the data that was processed so far.
|
||||
/// </summary>
|
||||
public int Adler {
|
||||
get {
|
||||
return engine.Adler;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of input bytes processed so far.
|
||||
/// </summary>
|
||||
public int TotalIn {
|
||||
get {
|
||||
return engine.TotalIn;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of output bytes so far.
|
||||
/// </summary>
|
||||
public int TotalOut {
|
||||
get {
|
||||
return totalOut;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the current input block. Further calls to deflate() will
|
||||
/// produce enough output to inflate everything in the current input
|
||||
/// block. This is not part of Sun's JDK so I have made it package
|
||||
/// private. It is used by DeflaterOutputStream to implement
|
||||
/// flush().
|
||||
/// </summary>
|
||||
public void Flush()
|
||||
{
|
||||
state |= IS_FLUSHING;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finishes the deflater with the current input block. It is an error
|
||||
/// to give more input after this method was called. This method must
|
||||
/// be called to force all bytes to be flushed.
|
||||
/// </summary>
|
||||
public void Finish()
|
||||
{
|
||||
state |= IS_FLUSHING | IS_FINISHING;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the stream was finished and no more output bytes
|
||||
/// are available.
|
||||
/// </summary>
|
||||
public bool IsFinished {
|
||||
get {
|
||||
return state == FINISHED_STATE && pending.IsFlushed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if the input buffer is empty.
|
||||
/// You should then call setInput().
|
||||
/// NOTE: This method can also return true when the stream
|
||||
/// was finished.
|
||||
/// </summary>
|
||||
public bool IsNeedingInput {
|
||||
get {
|
||||
return engine.NeedsInput();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the data which should be compressed next. This should be only
|
||||
/// called when needsInput indicates that more input is needed.
|
||||
/// If you call setInput when needsInput() returns false, the
|
||||
/// previous input that is still pending will be thrown away.
|
||||
/// The given byte array should not be changed, before needsInput() returns
|
||||
/// true again.
|
||||
/// This call is equivalent to <code>setInput(input, 0, input.length)</code>.
|
||||
/// </summary>
|
||||
/// <param name="input">
|
||||
/// the buffer containing the input data.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if the buffer was finished() or ended().
|
||||
/// </exception>
|
||||
public void SetInput(byte[] input)
|
||||
{
|
||||
SetInput(input, 0, input.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the data which should be compressed next. This should be
|
||||
/// only called when needsInput indicates that more input is needed.
|
||||
/// The given byte array should not be changed, before needsInput() returns
|
||||
/// true again.
|
||||
/// </summary>
|
||||
/// <param name="input">
|
||||
/// the buffer containing the input data.
|
||||
/// </param>
|
||||
/// <param name="off">
|
||||
/// the start of the data.
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the length of the data.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if the buffer was finished() or ended() or if previous input is still pending.
|
||||
/// </exception>
|
||||
public void SetInput(byte[] input, int off, int len)
|
||||
{
|
||||
if ((state & IS_FINISHING) != 0) {
|
||||
throw new InvalidOperationException("finish()/end() already called");
|
||||
}
|
||||
engine.SetInput(input, off, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the compression level. There is no guarantee of the exact
|
||||
/// position of the change, but if you call this when needsInput is
|
||||
/// true the change of compression level will occur somewhere near
|
||||
/// before the end of the so far given input.
|
||||
/// </summary>
|
||||
/// <param name="lvl">
|
||||
/// the new compression level.
|
||||
/// </param>
|
||||
public void SetLevel(int lvl)
|
||||
{
|
||||
if (lvl == DEFAULT_COMPRESSION) {
|
||||
lvl = 6;
|
||||
} else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
|
||||
throw new ArgumentOutOfRangeException("lvl");
|
||||
}
|
||||
|
||||
|
||||
if (level != lvl) {
|
||||
level = lvl;
|
||||
engine.SetLevel(lvl);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the compression strategy. Strategy is one of
|
||||
/// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
|
||||
/// position where the strategy is changed, the same as for
|
||||
/// setLevel() applies.
|
||||
/// </summary>
|
||||
/// <param name="stgy">
|
||||
/// the new compression strategy.
|
||||
/// </param>
|
||||
public void SetStrategy(DeflateStrategy stgy)
|
||||
{
|
||||
engine.Strategy = stgy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deflates the current input block to the given array. It returns
|
||||
/// the number of bytes compressed, or 0 if either
|
||||
/// needsInput() or finished() returns true or length is zero.
|
||||
/// </summary>
|
||||
/// <param name="output">
|
||||
/// the buffer where to write the compressed data.
|
||||
/// </param>
|
||||
public int Deflate(byte[] output)
|
||||
{
|
||||
return Deflate(output, 0, output.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deflates the current input block to the given array. It returns
|
||||
/// the number of bytes compressed, or 0 if either
|
||||
/// needsInput() or finished() returns true or length is zero.
|
||||
/// </summary>
|
||||
/// <param name="output">
|
||||
/// the buffer where to write the compressed data.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// the offset into the output array.
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// the maximum number of bytes that may be written.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if end() was called.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if offset and/or length don't match the array length.
|
||||
/// </exception>
|
||||
public int Deflate(byte[] output, int offset, int length)
|
||||
{
|
||||
int origLength = length;
|
||||
|
||||
if (state == CLOSED_STATE) {
|
||||
throw new InvalidOperationException("Deflater closed");
|
||||
}
|
||||
|
||||
if (state < BUSY_STATE) {
|
||||
/* output header */
|
||||
int header = (DEFLATED +
|
||||
((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
|
||||
int level_flags = (level - 1) >> 1;
|
||||
if (level_flags < 0 || level_flags > 3) {
|
||||
level_flags = 3;
|
||||
}
|
||||
header |= level_flags << 6;
|
||||
if ((state & IS_SETDICT) != 0) {
|
||||
/* Dictionary was set */
|
||||
header |= DeflaterConstants.PRESET_DICT;
|
||||
}
|
||||
header += 31 - (header % 31);
|
||||
|
||||
|
||||
pending.WriteShortMSB(header);
|
||||
if ((state & IS_SETDICT) != 0) {
|
||||
int chksum = engine.Adler;
|
||||
engine.ResetAdler();
|
||||
pending.WriteShortMSB(chksum >> 16);
|
||||
pending.WriteShortMSB(chksum & 0xffff);
|
||||
}
|
||||
|
||||
state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
int count = pending.Flush(output, offset, length);
|
||||
offset += count;
|
||||
totalOut += count;
|
||||
length -= count;
|
||||
|
||||
if (length == 0 || state == FINISHED_STATE) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) {
|
||||
if (state == BUSY_STATE) {
|
||||
/* We need more input now */
|
||||
return origLength - length;
|
||||
} else if (state == FLUSHING_STATE) {
|
||||
if (level != NO_COMPRESSION) {
|
||||
/* We have to supply some lookahead. 8 bit lookahead
|
||||
* are needed by the zlib inflater, and we must fill
|
||||
* the next byte, so that all bits are flushed.
|
||||
*/
|
||||
int neededbits = 8 + ((-pending.BitCount) & 7);
|
||||
while (neededbits > 0) {
|
||||
/* write a static tree block consisting solely of
|
||||
* an EOF:
|
||||
*/
|
||||
pending.WriteBits(2, 10);
|
||||
neededbits -= 10;
|
||||
}
|
||||
}
|
||||
state = BUSY_STATE;
|
||||
} else if (state == FINISHING_STATE) {
|
||||
pending.AlignToByte();
|
||||
/* We have completed the stream */
|
||||
if (!noHeader) {
|
||||
int adler = engine.Adler;
|
||||
pending.WriteShortMSB(adler >> 16);
|
||||
pending.WriteShortMSB(adler & 0xffff);
|
||||
}
|
||||
state = FINISHED_STATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return origLength - length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the dictionary which should be used in the deflate process.
|
||||
/// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.
|
||||
/// </summary>
|
||||
/// <param name="dict">
|
||||
/// the dictionary.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if setInput () or deflate () were already called or another dictionary was already set.
|
||||
/// </exception>
|
||||
public void SetDictionary(byte[] dict)
|
||||
{
|
||||
SetDictionary(dict, 0, dict.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the dictionary which should be used in the deflate process.
|
||||
/// The dictionary should be a byte array containing strings that are
|
||||
/// likely to occur in the data which should be compressed. The
|
||||
/// dictionary is not stored in the compressed output, only a
|
||||
/// checksum. To decompress the output you need to supply the same
|
||||
/// dictionary again.
|
||||
/// </summary>
|
||||
/// <param name="dict">
|
||||
/// the dictionary.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// an offset into the dictionary.
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// the length of the dictionary.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if setInput () or deflate () were already called or another dictionary was already set.
|
||||
/// </exception>
|
||||
public void SetDictionary(byte[] dict, int offset, int length)
|
||||
{
|
||||
if (state != INIT_STATE) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
state = SETDICT_STATE;
|
||||
engine.SetDictionary(dict, offset, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
// DeflaterConstants.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// This class contains constants used for the deflater.
|
||||
/// </summary>
|
||||
public class DeflaterConstants
|
||||
{
|
||||
public const bool DEBUGGING = false;
|
||||
|
||||
public const int STORED_BLOCK = 0;
|
||||
public const int STATIC_TREES = 1;
|
||||
public const int DYN_TREES = 2;
|
||||
public const int PRESET_DICT = 0x20;
|
||||
|
||||
public const int DEFAULT_MEM_LEVEL = 8;
|
||||
|
||||
public const int MAX_MATCH = 258;
|
||||
public const int MIN_MATCH = 3;
|
||||
|
||||
public const int MAX_WBITS = 15;
|
||||
public const int WSIZE = 1 << MAX_WBITS;
|
||||
public const int WMASK = WSIZE - 1;
|
||||
|
||||
public const int HASH_BITS = DEFAULT_MEM_LEVEL + 7;
|
||||
public const int HASH_SIZE = 1 << HASH_BITS;
|
||||
public const int HASH_MASK = HASH_SIZE - 1;
|
||||
public const int HASH_SHIFT = (HASH_BITS + MIN_MATCH - 1) / MIN_MATCH;
|
||||
|
||||
public const int MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
|
||||
public const int MAX_DIST = WSIZE - MIN_LOOKAHEAD;
|
||||
|
||||
public const int PENDING_BUF_SIZE = 1 << (DEFAULT_MEM_LEVEL + 8);
|
||||
public static int MAX_BLOCK_SIZE = Math.Min(65535, PENDING_BUF_SIZE-5);
|
||||
|
||||
public const int DEFLATE_STORED = 0;
|
||||
public const int DEFLATE_FAST = 1;
|
||||
public const int DEFLATE_SLOW = 2;
|
||||
|
||||
public static int[] GOOD_LENGTH = { 0, 4, 4, 4, 4, 8, 8, 8, 32, 32 };
|
||||
public static int[] MAX_LAZY = { 0, 4, 5, 6, 4,16, 16, 32, 128, 258 };
|
||||
public static int[] NICE_LENGTH = { 0, 8,16,32,16,32,128,128, 258, 258 };
|
||||
public static int[] MAX_CHAIN = { 0, 4, 8,32,16,32,128,256,1024,4096 };
|
||||
public static int[] COMPR_FUNC = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2 };
|
||||
}
|
||||
}
|
||||
+650
@@ -0,0 +1,650 @@
|
||||
// DeflaterEngine.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
public enum DeflateStrategy {
|
||||
// The default strategy.
|
||||
Default = 0,
|
||||
|
||||
// This strategy will only allow longer string repetitions. It is
|
||||
// useful for random data with a small character set.
|
||||
Filtered = 1,
|
||||
|
||||
// This strategy will not look for string repetitions at all. It
|
||||
// only encodes with Huffman trees (which means, that more common
|
||||
// characters get a smaller encoding.
|
||||
HuffmanOnly = 2
|
||||
}
|
||||
|
||||
public class DeflaterEngine : DeflaterConstants
|
||||
{
|
||||
static int TOO_FAR = 4096;
|
||||
|
||||
int ins_h;
|
||||
// private byte[] buffer;
|
||||
short[] head;
|
||||
short[] prev;
|
||||
|
||||
int matchStart, matchLen;
|
||||
bool prevAvailable;
|
||||
int blockStart;
|
||||
int strstart, lookahead;
|
||||
byte[] window;
|
||||
|
||||
DeflateStrategy strategy;
|
||||
int max_chain, max_lazy, niceLength, goodLength;
|
||||
|
||||
/// <summary>
|
||||
/// The current compression function.
|
||||
/// </summary>
|
||||
int comprFunc;
|
||||
|
||||
/// <summary>
|
||||
/// The input data for compression.
|
||||
/// </summary>
|
||||
byte[] inputBuf;
|
||||
|
||||
/// <summary>
|
||||
/// The total bytes of input read.
|
||||
/// </summary>
|
||||
int totalIn;
|
||||
|
||||
/// <summary>
|
||||
/// The offset into inputBuf, where input data starts.
|
||||
/// </summary>
|
||||
int inputOff;
|
||||
|
||||
/// <summary>
|
||||
/// The end offset of the input data.
|
||||
/// </summary>
|
||||
int inputEnd;
|
||||
|
||||
DeflaterPending pending;
|
||||
DeflaterHuffman huffman;
|
||||
|
||||
/// <summary>
|
||||
/// The adler checksum
|
||||
/// </summary>
|
||||
Adler32 adler;
|
||||
|
||||
public DeflaterEngine(DeflaterPending pending)
|
||||
{
|
||||
this.pending = pending;
|
||||
huffman = new DeflaterHuffman(pending);
|
||||
adler = new Adler32();
|
||||
|
||||
window = new byte[2 * WSIZE];
|
||||
head = new short[HASH_SIZE];
|
||||
prev = new short[WSIZE];
|
||||
|
||||
/* We start at index 1, to avoid a implementation deficiency, that
|
||||
* we cannot build a repeat pattern at index 0.
|
||||
*/
|
||||
blockStart = strstart = 1;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
huffman.Reset();
|
||||
adler.Reset();
|
||||
blockStart = strstart = 1;
|
||||
lookahead = 0;
|
||||
totalIn = 0;
|
||||
prevAvailable = false;
|
||||
matchLen = MIN_MATCH - 1;
|
||||
|
||||
for (int i = 0; i < HASH_SIZE; i++) {
|
||||
head[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < WSIZE; i++) {
|
||||
prev[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetAdler()
|
||||
{
|
||||
adler.Reset();
|
||||
}
|
||||
|
||||
public int Adler {
|
||||
get {
|
||||
return (int)adler.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public int TotalIn {
|
||||
get {
|
||||
return totalIn;
|
||||
}
|
||||
}
|
||||
|
||||
public DeflateStrategy Strategy {
|
||||
get {
|
||||
return strategy;
|
||||
}
|
||||
set {
|
||||
strategy = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLevel(int lvl)
|
||||
{
|
||||
goodLength = DeflaterConstants.GOOD_LENGTH[lvl];
|
||||
max_lazy = DeflaterConstants.MAX_LAZY[lvl];
|
||||
niceLength = DeflaterConstants.NICE_LENGTH[lvl];
|
||||
max_chain = DeflaterConstants.MAX_CHAIN[lvl];
|
||||
|
||||
if (DeflaterConstants.COMPR_FUNC[lvl] != comprFunc) {
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("Change from "+comprFunc +" to "
|
||||
// + DeflaterConstants.COMPR_FUNC[lvl]);
|
||||
// }
|
||||
switch (comprFunc) {
|
||||
case DEFLATE_STORED:
|
||||
if (strstart > blockStart) {
|
||||
huffman.FlushStoredBlock(window, blockStart,
|
||||
strstart - blockStart, false);
|
||||
blockStart = strstart;
|
||||
}
|
||||
UpdateHash();
|
||||
break;
|
||||
case DEFLATE_FAST:
|
||||
if (strstart > blockStart) {
|
||||
huffman.FlushBlock(window, blockStart, strstart - blockStart,
|
||||
false);
|
||||
blockStart = strstart;
|
||||
}
|
||||
break;
|
||||
case DEFLATE_SLOW:
|
||||
if (prevAvailable) {
|
||||
huffman.TallyLit(window[strstart-1] & 0xff);
|
||||
}
|
||||
if (strstart > blockStart) {
|
||||
huffman.FlushBlock(window, blockStart, strstart - blockStart,
|
||||
false);
|
||||
blockStart = strstart;
|
||||
}
|
||||
prevAvailable = false;
|
||||
matchLen = MIN_MATCH - 1;
|
||||
break;
|
||||
}
|
||||
comprFunc = COMPR_FUNC[lvl];
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateHash()
|
||||
{
|
||||
// if (DEBUGGING) {
|
||||
// Console.WriteLine("updateHash: "+strstart);
|
||||
// }
|
||||
ins_h = (window[strstart] << HASH_SHIFT) ^ window[strstart + 1];
|
||||
}
|
||||
|
||||
private int InsertString()
|
||||
{
|
||||
short match;
|
||||
int hash = ((ins_h << HASH_SHIFT) ^ window[strstart + (MIN_MATCH -1)]) & HASH_MASK;
|
||||
|
||||
// if (DEBUGGING) {
|
||||
// if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^
|
||||
// (window[strstart + 1] << HASH_SHIFT) ^
|
||||
// (window[strstart + 2])) & HASH_MASK)) {
|
||||
// throw new Exception("hash inconsistent: "+hash+"/"
|
||||
// +window[strstart]+","
|
||||
// +window[strstart+1]+","
|
||||
// +window[strstart+2]+","+HASH_SHIFT);
|
||||
// }
|
||||
// }
|
||||
|
||||
prev[strstart & WMASK] = match = head[hash];
|
||||
head[hash] = (short)strstart;
|
||||
ins_h = hash;
|
||||
return match & 0xffff;
|
||||
}
|
||||
|
||||
void SlideWindow()
|
||||
{
|
||||
Array.Copy(window, WSIZE, window, 0, WSIZE);
|
||||
matchStart -= WSIZE;
|
||||
strstart -= WSIZE;
|
||||
blockStart -= WSIZE;
|
||||
|
||||
/* Slide the hash table (could be avoided with 32 bit values
|
||||
* at the expense of memory usage).
|
||||
*/
|
||||
for (int i = 0; i < HASH_SIZE; ++i) {
|
||||
int m = head[i] & 0xffff;
|
||||
head[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
|
||||
}
|
||||
|
||||
/* Slide the prev table. */
|
||||
for (int i = 0; i < WSIZE; i++) {
|
||||
int m = prev[i] & 0xffff;
|
||||
prev[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void FillWindow()
|
||||
{
|
||||
/* If the window is almost full and there is insufficient lookahead,
|
||||
* move the upper half to the lower one to make room in the upper half.
|
||||
*/
|
||||
if (strstart >= WSIZE + MAX_DIST) {
|
||||
SlideWindow();
|
||||
}
|
||||
|
||||
/* If there is not enough lookahead, but still some input left,
|
||||
* read in the input
|
||||
*/
|
||||
while (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd) {
|
||||
int more = 2 * WSIZE - lookahead - strstart;
|
||||
|
||||
if (more > inputEnd - inputOff) {
|
||||
more = inputEnd - inputOff;
|
||||
}
|
||||
|
||||
System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
|
||||
adler.Update(inputBuf, inputOff, more);
|
||||
|
||||
inputOff += more;
|
||||
totalIn += more;
|
||||
lookahead += more;
|
||||
}
|
||||
|
||||
if (lookahead >= MIN_MATCH) {
|
||||
UpdateHash();
|
||||
}
|
||||
}
|
||||
|
||||
private bool FindLongestMatch(int curMatch)
|
||||
{
|
||||
int chainLength = this.max_chain;
|
||||
int niceLength = this.niceLength;
|
||||
short[] prev = this.prev;
|
||||
int scan = this.strstart;
|
||||
int match;
|
||||
int best_end = this.strstart + matchLen;
|
||||
int best_len = Math.Max(matchLen, MIN_MATCH - 1);
|
||||
|
||||
int limit = Math.Max(strstart - MAX_DIST, 0);
|
||||
|
||||
int strend = strstart + MAX_MATCH - 1;
|
||||
byte scan_end1 = window[best_end - 1];
|
||||
byte scan_end = window[best_end];
|
||||
|
||||
/* Do not waste too much time if we already have a good match: */
|
||||
if (best_len >= this.goodLength) {
|
||||
chainLength >>= 2;
|
||||
}
|
||||
|
||||
/* Do not look for matches beyond the end of the input. This is necessary
|
||||
* to make deflate deterministic.
|
||||
*/
|
||||
if (niceLength > lookahead) {
|
||||
niceLength = lookahead;
|
||||
}
|
||||
|
||||
if (DeflaterConstants.DEBUGGING && strstart > 2 * WSIZE - MIN_LOOKAHEAD) {
|
||||
throw new InvalidOperationException("need lookahead");
|
||||
}
|
||||
|
||||
do {
|
||||
if (DeflaterConstants.DEBUGGING && curMatch >= strstart) {
|
||||
throw new InvalidOperationException("future match");
|
||||
}
|
||||
if (window[curMatch + best_len] != scan_end ||
|
||||
window[curMatch + best_len - 1] != scan_end1 ||
|
||||
window[curMatch] != window[scan] ||
|
||||
window[curMatch + 1] != window[scan + 1]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match = curMatch + 2;
|
||||
scan += 2;
|
||||
|
||||
/* We check for insufficient lookahead only every 8th comparison;
|
||||
* the 256th check will be made at strstart+258.
|
||||
*/
|
||||
while (window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] &&
|
||||
window[++scan] == window[++match] && scan < strend) ;
|
||||
|
||||
if (scan > best_end) {
|
||||
// if (DeflaterConstants.DEBUGGING && ins_h == 0)
|
||||
// System.err.println("Found match: "+curMatch+"-"+(scan-strstart));
|
||||
matchStart = curMatch;
|
||||
best_end = scan;
|
||||
best_len = scan - strstart;
|
||||
if (best_len >= niceLength) {
|
||||
break;
|
||||
}
|
||||
|
||||
scan_end1 = window[best_end - 1];
|
||||
scan_end = window[best_end];
|
||||
}
|
||||
scan = strstart;
|
||||
} while ((curMatch = (prev[curMatch & WMASK] & 0xffff)) > limit && --chainLength != 0);
|
||||
|
||||
matchLen = Math.Min(best_len, lookahead);
|
||||
return matchLen >= MIN_MATCH;
|
||||
}
|
||||
|
||||
public void SetDictionary(byte[] buffer, int offset, int length)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && strstart != 1) {
|
||||
throw new InvalidOperationException("strstart not 1");
|
||||
}
|
||||
adler.Update(buffer, offset, length);
|
||||
if (length < MIN_MATCH) {
|
||||
return;
|
||||
}
|
||||
if (length > MAX_DIST) {
|
||||
offset += length - MAX_DIST;
|
||||
length = MAX_DIST;
|
||||
}
|
||||
|
||||
System.Array.Copy(buffer, offset, window, strstart, length);
|
||||
|
||||
UpdateHash();
|
||||
--length;
|
||||
while (--length > 0) {
|
||||
InsertString();
|
||||
strstart++;
|
||||
}
|
||||
strstart += 2;
|
||||
blockStart = strstart;
|
||||
}
|
||||
|
||||
private bool DeflateStored(bool flush, bool finish)
|
||||
{
|
||||
if (!flush && lookahead == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
strstart += lookahead;
|
||||
lookahead = 0;
|
||||
|
||||
int storedLen = strstart - blockStart;
|
||||
|
||||
if ((storedLen >= DeflaterConstants.MAX_BLOCK_SIZE) || /* Block is full */
|
||||
(blockStart < WSIZE && storedLen >= MAX_DIST) || /* Block may move out of window */
|
||||
flush) {
|
||||
bool lastBlock = finish;
|
||||
if (storedLen > DeflaterConstants.MAX_BLOCK_SIZE) {
|
||||
storedLen = DeflaterConstants.MAX_BLOCK_SIZE;
|
||||
lastBlock = false;
|
||||
}
|
||||
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("storedBlock["+storedLen+","+lastBlock+"]");
|
||||
// }
|
||||
|
||||
huffman.FlushStoredBlock(window, blockStart, storedLen, lastBlock);
|
||||
blockStart += storedLen;
|
||||
return !lastBlock;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool DeflateFast(bool flush, bool finish)
|
||||
{
|
||||
if (lookahead < MIN_LOOKAHEAD && !flush) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (lookahead >= MIN_LOOKAHEAD || flush) {
|
||||
if (lookahead == 0) {
|
||||
/* We are flushing everything */
|
||||
huffman.FlushBlock(window, blockStart, strstart - blockStart, finish);
|
||||
blockStart = strstart;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strstart > 2 * WSIZE - MIN_LOOKAHEAD)
|
||||
{
|
||||
/* slide window, as findLongestMatch need this.
|
||||
* This should only happen when flushing and the window
|
||||
* is almost full.
|
||||
*/
|
||||
SlideWindow();
|
||||
}
|
||||
|
||||
int hashHead;
|
||||
if (lookahead >= MIN_MATCH &&
|
||||
(hashHead = InsertString()) != 0 &&
|
||||
strategy != DeflateStrategy.HuffmanOnly &&
|
||||
strstart - hashHead <= MAX_DIST &&
|
||||
FindLongestMatch(hashHead)) {
|
||||
/* longestMatch sets matchStart and matchLen */
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// for (int i = 0 ; i < matchLen; i++) {
|
||||
// if (window[strstart+i] != window[matchStart + i]) {
|
||||
// throw new Exception();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
huffman.TallyDist(strstart - matchStart, matchLen);
|
||||
|
||||
lookahead -= matchLen;
|
||||
if (matchLen <= max_lazy && lookahead >= MIN_MATCH) {
|
||||
while (--matchLen > 0) {
|
||||
++strstart;
|
||||
InsertString();
|
||||
}
|
||||
++strstart;
|
||||
} else {
|
||||
strstart += matchLen;
|
||||
if (lookahead >= MIN_MATCH - 1) {
|
||||
UpdateHash();
|
||||
}
|
||||
}
|
||||
matchLen = MIN_MATCH - 1;
|
||||
continue;
|
||||
} else {
|
||||
/* No match found */
|
||||
huffman.TallyLit(window[strstart] & 0xff);
|
||||
++strstart;
|
||||
--lookahead;
|
||||
}
|
||||
|
||||
if (huffman.IsFull()) {
|
||||
bool lastBlock = finish && lookahead == 0;
|
||||
huffman.FlushBlock(window, blockStart, strstart - blockStart,
|
||||
lastBlock);
|
||||
blockStart = strstart;
|
||||
return !lastBlock;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool DeflateSlow(bool flush, bool finish)
|
||||
{
|
||||
if (lookahead < MIN_LOOKAHEAD && !flush) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (lookahead >= MIN_LOOKAHEAD || flush) {
|
||||
if (lookahead == 0) {
|
||||
if (prevAvailable) {
|
||||
huffman.TallyLit(window[strstart-1] & 0xff);
|
||||
}
|
||||
prevAvailable = false;
|
||||
|
||||
/* We are flushing everything */
|
||||
if (DeflaterConstants.DEBUGGING && !flush) {
|
||||
throw new Exception("Not flushing, but no lookahead");
|
||||
}
|
||||
huffman.FlushBlock(window, blockStart, strstart - blockStart,
|
||||
finish);
|
||||
blockStart = strstart;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strstart >= 2 * WSIZE - MIN_LOOKAHEAD)
|
||||
{
|
||||
/* slide window, as findLongestMatch need this.
|
||||
* This should only happen when flushing and the window
|
||||
* is almost full.
|
||||
*/
|
||||
SlideWindow();
|
||||
}
|
||||
|
||||
int prevMatch = matchStart;
|
||||
int prevLen = matchLen;
|
||||
if (lookahead >= MIN_MATCH) {
|
||||
int hashHead = InsertString();
|
||||
if (strategy != DeflateStrategy.HuffmanOnly && hashHead != 0 && strstart - hashHead <= MAX_DIST && FindLongestMatch(hashHead))
|
||||
{
|
||||
/* longestMatch sets matchStart and matchLen */
|
||||
|
||||
/* Discard match if too small and too far away */
|
||||
if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == MIN_MATCH && strstart - matchStart > TOO_FAR))) {
|
||||
matchLen = MIN_MATCH - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* previous match was better */
|
||||
if (prevLen >= MIN_MATCH && matchLen <= prevLen) {
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// for (int i = 0 ; i < matchLen; i++) {
|
||||
// if (window[strstart-1+i] != window[prevMatch + i])
|
||||
// throw new Exception();
|
||||
// }
|
||||
// }
|
||||
huffman.TallyDist(strstart - 1 - prevMatch, prevLen);
|
||||
prevLen -= 2;
|
||||
do {
|
||||
strstart++;
|
||||
lookahead--;
|
||||
if (lookahead >= MIN_MATCH) {
|
||||
InsertString();
|
||||
}
|
||||
} while (--prevLen > 0);
|
||||
strstart ++;
|
||||
lookahead--;
|
||||
prevAvailable = false;
|
||||
matchLen = MIN_MATCH - 1;
|
||||
} else {
|
||||
if (prevAvailable) {
|
||||
huffman.TallyLit(window[strstart-1] & 0xff);
|
||||
}
|
||||
prevAvailable = true;
|
||||
strstart++;
|
||||
lookahead--;
|
||||
}
|
||||
|
||||
if (huffman.IsFull()) {
|
||||
int len = strstart - blockStart;
|
||||
if (prevAvailable) {
|
||||
len--;
|
||||
}
|
||||
bool lastBlock = (finish && lookahead == 0 && !prevAvailable);
|
||||
huffman.FlushBlock(window, blockStart, len, lastBlock);
|
||||
blockStart += len;
|
||||
return !lastBlock;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Deflate(bool flush, bool finish)
|
||||
{
|
||||
bool progress;
|
||||
do {
|
||||
FillWindow();
|
||||
bool canFlush = flush && inputOff == inputEnd;
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("window: ["+blockStart+","+strstart+","
|
||||
// +lookahead+"], "+comprFunc+","+canFlush);
|
||||
// }
|
||||
switch (comprFunc) {
|
||||
case DEFLATE_STORED:
|
||||
progress = DeflateStored(canFlush, finish);
|
||||
break;
|
||||
case DEFLATE_FAST:
|
||||
progress = DeflateFast(canFlush, finish);
|
||||
break;
|
||||
case DEFLATE_SLOW:
|
||||
progress = DeflateSlow(canFlush, finish);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("unknown comprFunc");
|
||||
}
|
||||
} while (pending.IsFlushed && progress); /* repeat while we have no pending output and progress was made */
|
||||
return progress;
|
||||
}
|
||||
|
||||
public void SetInput(byte[] buf, int off, int len)
|
||||
{
|
||||
if (inputOff < inputEnd) {
|
||||
throw new InvalidOperationException("Old input was not completely processed");
|
||||
}
|
||||
|
||||
int end = off + len;
|
||||
|
||||
/* We want to throw an ArrayIndexOutOfBoundsException early. The
|
||||
* check is very tricky: it also handles integer wrap around.
|
||||
*/
|
||||
if (0 > off || off > end || end > buf.Length) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
inputBuf = buf;
|
||||
inputOff = off;
|
||||
inputEnd = end;
|
||||
}
|
||||
|
||||
public bool NeedsInput()
|
||||
{
|
||||
return inputEnd == inputOff;
|
||||
}
|
||||
}
|
||||
}
|
||||
+778
@@ -0,0 +1,778 @@
|
||||
// DeflaterHuffman.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// This is the DeflaterHuffman class.
|
||||
///
|
||||
/// This class is <i>not</i> thread safe. This is inherent in the API, due
|
||||
/// to the split of deflate and setInput.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class DeflaterHuffman
|
||||
{
|
||||
private static int BUFSIZE = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6);
|
||||
private static int LITERAL_NUM = 286;
|
||||
private static int DIST_NUM = 30;
|
||||
private static int BITLEN_NUM = 19;
|
||||
private static int REP_3_6 = 16;
|
||||
private static int REP_3_10 = 17;
|
||||
private static int REP_11_138 = 18;
|
||||
private static int EOF_SYMBOL = 256;
|
||||
private static int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
|
||||
|
||||
private static byte[] bit4Reverse = {
|
||||
0,
|
||||
8,
|
||||
4,
|
||||
12,
|
||||
2,
|
||||
10,
|
||||
6,
|
||||
14,
|
||||
1,
|
||||
9,
|
||||
5,
|
||||
13,
|
||||
3,
|
||||
11,
|
||||
7,
|
||||
15
|
||||
};
|
||||
|
||||
|
||||
public class Tree
|
||||
{
|
||||
public short[] freqs;
|
||||
public short[] codes;
|
||||
public byte[] length;
|
||||
public int[] bl_counts;
|
||||
public int minNumCodes, numCodes;
|
||||
public int maxLength;
|
||||
DeflaterHuffman dh;
|
||||
|
||||
public Tree(DeflaterHuffman dh, int elems, int minCodes, int maxLength)
|
||||
{
|
||||
this.dh = dh;
|
||||
this.minNumCodes = minCodes;
|
||||
this.maxLength = maxLength;
|
||||
freqs = new short[elems];
|
||||
bl_counts = new int[maxLength];
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
for (int i = 0; i < freqs.Length; i++) {
|
||||
freqs[i] = 0;
|
||||
}
|
||||
codes = null;
|
||||
length = null;
|
||||
}
|
||||
|
||||
public void WriteSymbol(int code)
|
||||
{
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// freqs[code]--;
|
||||
// // Console.Write("writeSymbol("+freqs.length+","+code+"): ");
|
||||
// }
|
||||
dh.pending.WriteBits(codes[code] & 0xffff, length[code]);
|
||||
}
|
||||
|
||||
public void CheckEmpty()
|
||||
{
|
||||
bool empty = true;
|
||||
for (int i = 0; i < freqs.Length; i++) {
|
||||
if (freqs[i] != 0) {
|
||||
Console.WriteLine("freqs["+i+"] == "+freqs[i]);
|
||||
empty = false;
|
||||
}
|
||||
}
|
||||
if (!empty) {
|
||||
throw new Exception();
|
||||
}
|
||||
Console.WriteLine("checkEmpty suceeded!");
|
||||
}
|
||||
|
||||
public void SetStaticCodes(short[] stCodes, byte[] stLength)
|
||||
{
|
||||
codes = stCodes;
|
||||
length = stLength;
|
||||
}
|
||||
|
||||
public void BuildCodes()
|
||||
{
|
||||
int numSymbols = freqs.Length;
|
||||
int[] nextCode = new int[maxLength];
|
||||
int code = 0;
|
||||
codes = new short[freqs.Length];
|
||||
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("buildCodes: "+freqs.Length);
|
||||
// }
|
||||
|
||||
for (int bits = 0; bits < maxLength; bits++) {
|
||||
nextCode[bits] = code;
|
||||
code += bl_counts[bits] << (15 - bits);
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("bits: "+(bits+1)+" count: "+bl_counts[bits]
|
||||
// +" nextCode: "+code); // HACK : Integer.toHexString(
|
||||
// }
|
||||
}
|
||||
if (DeflaterConstants.DEBUGGING && code != 65536) {
|
||||
throw new Exception("Inconsistent bl_counts!");
|
||||
}
|
||||
|
||||
for (int i=0; i < numCodes; i++) {
|
||||
int bits = length[i];
|
||||
if (bits > 0) {
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("codes["+i+"] = rev(" + nextCode[bits-1]+")," // HACK : Integer.toHexString(
|
||||
// +bits);
|
||||
// }
|
||||
codes[i] = BitReverse(nextCode[bits-1]);
|
||||
nextCode[bits-1] += 1 << (16 - bits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void BuildLength(int[] childs)
|
||||
{
|
||||
this.length = new byte [freqs.Length];
|
||||
int numNodes = childs.Length / 2;
|
||||
int numLeafs = (numNodes + 1) / 2;
|
||||
int overflow = 0;
|
||||
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
bl_counts[i] = 0;
|
||||
}
|
||||
|
||||
/* First calculate optimal bit lengths */
|
||||
int[] lengths = new int[numNodes];
|
||||
lengths[numNodes-1] = 0;
|
||||
|
||||
for (int i = numNodes - 1; i >= 0; i--) {
|
||||
if (childs[2*i+1] != -1) {
|
||||
int bitLength = lengths[i] + 1;
|
||||
if (bitLength > maxLength) {
|
||||
bitLength = maxLength;
|
||||
overflow++;
|
||||
}
|
||||
lengths[childs[2*i]] = lengths[childs[2*i+1]] = bitLength;
|
||||
} else {
|
||||
/* A leaf node */
|
||||
int bitLength = lengths[i];
|
||||
bl_counts[bitLength - 1]++;
|
||||
this.length[childs[2*i]] = (byte) lengths[i];
|
||||
}
|
||||
}
|
||||
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("Tree "+freqs.Length+" lengths:");
|
||||
// for (int i=0; i < numLeafs; i++) {
|
||||
// Console.WriteLine("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]]
|
||||
// + " len: "+length[childs[2*i]]);
|
||||
// }
|
||||
// }
|
||||
|
||||
if (overflow == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int incrBitLen = maxLength - 1;
|
||||
do {
|
||||
/* Find the first bit length which could increase: */
|
||||
while (bl_counts[--incrBitLen] == 0)
|
||||
;
|
||||
|
||||
/* Move this node one down and remove a corresponding
|
||||
* amount of overflow nodes.
|
||||
*/
|
||||
do {
|
||||
bl_counts[incrBitLen]--;
|
||||
bl_counts[++incrBitLen]++;
|
||||
overflow -= 1 << (maxLength - 1 - incrBitLen);
|
||||
} while (overflow > 0 && incrBitLen < maxLength - 1);
|
||||
} while (overflow > 0);
|
||||
|
||||
/* We may have overshot above. Move some nodes from maxLength to
|
||||
* maxLength-1 in that case.
|
||||
*/
|
||||
bl_counts[maxLength-1] += overflow;
|
||||
bl_counts[maxLength-2] -= overflow;
|
||||
|
||||
/* Now recompute all bit lengths, scanning in increasing
|
||||
* frequency. It is simpler to reconstruct all lengths instead of
|
||||
* fixing only the wrong ones. This idea is taken from 'ar'
|
||||
* written by Haruhiko Okumura.
|
||||
*
|
||||
* The nodes were inserted with decreasing frequency into the childs
|
||||
* array.
|
||||
*/
|
||||
int nodePtr = 2 * numLeafs;
|
||||
for (int bits = maxLength; bits != 0; bits--) {
|
||||
int n = bl_counts[bits-1];
|
||||
while (n > 0) {
|
||||
int childPtr = 2*childs[nodePtr++];
|
||||
if (childs[childPtr + 1] == -1) {
|
||||
/* We found another leaf */
|
||||
length[childs[childPtr]] = (byte) bits;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("*** After overflow elimination. ***");
|
||||
// for (int i=0; i < numLeafs; i++) {
|
||||
// Console.WriteLine("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]]
|
||||
// + " len: "+length[childs[2*i]]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public void BuildTree()
|
||||
{
|
||||
int numSymbols = freqs.Length;
|
||||
|
||||
/* heap is a priority queue, sorted by frequency, least frequent
|
||||
* nodes first. The heap is a binary tree, with the property, that
|
||||
* the parent node is smaller than both child nodes. This assures
|
||||
* that the smallest node is the first parent.
|
||||
*
|
||||
* The binary tree is encoded in an array: 0 is root node and
|
||||
* the nodes 2*n+1, 2*n+2 are the child nodes of node n.
|
||||
*/
|
||||
int[] heap = new int[numSymbols];
|
||||
int heapLen = 0;
|
||||
int maxCode = 0;
|
||||
for (int n = 0; n < numSymbols; n++) {
|
||||
int freq = freqs[n];
|
||||
if (freq != 0) {
|
||||
/* Insert n into heap */
|
||||
int pos = heapLen++;
|
||||
int ppos;
|
||||
while (pos > 0 && freqs[heap[ppos = (pos - 1) / 2]] > freq) {
|
||||
heap[pos] = heap[ppos];
|
||||
pos = ppos;
|
||||
}
|
||||
heap[pos] = n;
|
||||
|
||||
maxCode = n;
|
||||
}
|
||||
}
|
||||
|
||||
/* We could encode a single literal with 0 bits but then we
|
||||
* don't see the literals. Therefore we force at least two
|
||||
* literals to avoid this case. We don't care about order in
|
||||
* this case, both literals get a 1 bit code.
|
||||
*/
|
||||
while (heapLen < 2) {
|
||||
int node = maxCode < 2 ? ++maxCode : 0;
|
||||
heap[heapLen++] = node;
|
||||
}
|
||||
|
||||
numCodes = Math.Max(maxCode + 1, minNumCodes);
|
||||
|
||||
int numLeafs = heapLen;
|
||||
int[] childs = new int[4*heapLen - 2];
|
||||
int[] values = new int[2*heapLen - 1];
|
||||
int numNodes = numLeafs;
|
||||
for (int i = 0; i < heapLen; i++) {
|
||||
int node = heap[i];
|
||||
childs[2*i] = node;
|
||||
childs[2*i+1] = -1;
|
||||
values[i] = freqs[node] << 8;
|
||||
heap[i] = i;
|
||||
}
|
||||
|
||||
/* Construct the Huffman tree by repeatedly combining the least two
|
||||
* frequent nodes.
|
||||
*/
|
||||
do {
|
||||
int first = heap[0];
|
||||
int last = heap[--heapLen];
|
||||
|
||||
/* Propagate the hole to the leafs of the heap */
|
||||
int ppos = 0;
|
||||
int path = 1;
|
||||
while (path < heapLen) {
|
||||
if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) {
|
||||
path++;
|
||||
}
|
||||
|
||||
heap[ppos] = heap[path];
|
||||
ppos = path;
|
||||
path = path * 2 + 1;
|
||||
}
|
||||
|
||||
/* Now propagate the last element down along path. Normally
|
||||
* it shouldn't go too deep.
|
||||
*/
|
||||
int lastVal = values[last];
|
||||
while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) {
|
||||
heap[path] = heap[ppos];
|
||||
}
|
||||
heap[path] = last;
|
||||
|
||||
|
||||
int second = heap[0];
|
||||
|
||||
/* Create a new node father of first and second */
|
||||
last = numNodes++;
|
||||
childs[2*last] = first;
|
||||
childs[2*last+1] = second;
|
||||
int mindepth = Math.Min(values[first] & 0xff, values[second] & 0xff);
|
||||
values[last] = lastVal = values[first] + values[second] - mindepth + 1;
|
||||
|
||||
/* Again, propagate the hole to the leafs */
|
||||
ppos = 0;
|
||||
path = 1;
|
||||
while (path < heapLen) {
|
||||
if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) {
|
||||
path++;
|
||||
}
|
||||
|
||||
heap[ppos] = heap[path];
|
||||
ppos = path;
|
||||
path = ppos * 2 + 1;
|
||||
}
|
||||
|
||||
/* Now propagate the new element down along path */
|
||||
while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) {
|
||||
heap[path] = heap[ppos];
|
||||
}
|
||||
heap[path] = last;
|
||||
} while (heapLen > 1);
|
||||
|
||||
if (heap[0] != childs.Length / 2 - 1) {
|
||||
throw new Exception("Weird!");
|
||||
}
|
||||
BuildLength(childs);
|
||||
}
|
||||
|
||||
public int GetEncodedLength()
|
||||
{
|
||||
int len = 0;
|
||||
for (int i = 0; i < freqs.Length; i++) {
|
||||
len += freqs[i] * length[i];
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
public void CalcBLFreq(Tree blTree)
|
||||
{
|
||||
int max_count; /* max repeat count */
|
||||
int min_count; /* min repeat count */
|
||||
int count; /* repeat count of the current code */
|
||||
int curlen = -1; /* length of current code */
|
||||
|
||||
int i = 0;
|
||||
while (i < numCodes) {
|
||||
count = 1;
|
||||
int nextlen = length[i];
|
||||
if (nextlen == 0) {
|
||||
max_count = 138;
|
||||
min_count = 3;
|
||||
} else {
|
||||
max_count = 6;
|
||||
min_count = 3;
|
||||
if (curlen != nextlen) {
|
||||
blTree.freqs[nextlen]++;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
curlen = nextlen;
|
||||
i++;
|
||||
|
||||
while (i < numCodes && curlen == length[i]) {
|
||||
i++;
|
||||
if (++count >= max_count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count < min_count) {
|
||||
blTree.freqs[curlen] += (short)count;
|
||||
} else if (curlen != 0) {
|
||||
blTree.freqs[REP_3_6]++;
|
||||
} else if (count <= 10) {
|
||||
blTree.freqs[REP_3_10]++;
|
||||
} else {
|
||||
blTree.freqs[REP_11_138]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteTree(Tree blTree)
|
||||
{
|
||||
int max_count; /* max repeat count */
|
||||
int min_count; /* min repeat count */
|
||||
int count; /* repeat count of the current code */
|
||||
int curlen = -1; /* length of current code */
|
||||
|
||||
int i = 0;
|
||||
while (i < numCodes) {
|
||||
count = 1;
|
||||
int nextlen = length[i];
|
||||
if (nextlen == 0) {
|
||||
max_count = 138;
|
||||
min_count = 3;
|
||||
} else {
|
||||
max_count = 6;
|
||||
min_count = 3;
|
||||
if (curlen != nextlen) {
|
||||
blTree.WriteSymbol(nextlen);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
curlen = nextlen;
|
||||
i++;
|
||||
|
||||
while (i < numCodes && curlen == length[i]) {
|
||||
i++;
|
||||
if (++count >= max_count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count < min_count) {
|
||||
while (count-- > 0) {
|
||||
blTree.WriteSymbol(curlen);
|
||||
}
|
||||
}
|
||||
else if (curlen != 0) {
|
||||
blTree.WriteSymbol(REP_3_6);
|
||||
dh.pending.WriteBits(count - 3, 2);
|
||||
} else if (count <= 10) {
|
||||
blTree.WriteSymbol(REP_3_10);
|
||||
dh.pending.WriteBits(count - 3, 3);
|
||||
} else {
|
||||
blTree.WriteSymbol(REP_11_138);
|
||||
dh.pending.WriteBits(count - 11, 7);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DeflaterPending pending;
|
||||
private Tree literalTree, distTree, blTree;
|
||||
|
||||
private short[] d_buf;
|
||||
private byte[] l_buf;
|
||||
private int last_lit;
|
||||
private int extra_bits;
|
||||
|
||||
private static short[] staticLCodes;
|
||||
private static byte[] staticLLength;
|
||||
private static short[] staticDCodes;
|
||||
private static byte[] staticDLength;
|
||||
|
||||
/// <summary>
|
||||
/// Reverse the bits of a 16 bit value.
|
||||
/// </summary>
|
||||
public static short BitReverse(int value)
|
||||
{
|
||||
return (short) (bit4Reverse[value & 0xF] << 12
|
||||
| bit4Reverse[(value >> 4) & 0xF] << 8
|
||||
| bit4Reverse[(value >> 8) & 0xF] << 4
|
||||
| bit4Reverse[value >> 12]);
|
||||
}
|
||||
|
||||
|
||||
static DeflaterHuffman()
|
||||
{
|
||||
/* See RFC 1951 3.2.6 */
|
||||
/* Literal codes */
|
||||
staticLCodes = new short[LITERAL_NUM];
|
||||
staticLLength = new byte[LITERAL_NUM];
|
||||
int i = 0;
|
||||
while (i < 144) {
|
||||
staticLCodes[i] = BitReverse((0x030 + i) << 8);
|
||||
staticLLength[i++] = 8;
|
||||
}
|
||||
while (i < 256) {
|
||||
staticLCodes[i] = BitReverse((0x190 - 144 + i) << 7);
|
||||
staticLLength[i++] = 9;
|
||||
}
|
||||
while (i < 280) {
|
||||
staticLCodes[i] = BitReverse((0x000 - 256 + i) << 9);
|
||||
staticLLength[i++] = 7;
|
||||
}
|
||||
while (i < LITERAL_NUM) {
|
||||
staticLCodes[i] = BitReverse((0x0c0 - 280 + i) << 8);
|
||||
staticLLength[i++] = 8;
|
||||
}
|
||||
|
||||
/* Distant codes */
|
||||
staticDCodes = new short[DIST_NUM];
|
||||
staticDLength = new byte[DIST_NUM];
|
||||
for (i = 0; i < DIST_NUM; i++) {
|
||||
staticDCodes[i] = BitReverse(i << 11);
|
||||
staticDLength[i] = 5;
|
||||
}
|
||||
}
|
||||
|
||||
public DeflaterHuffman(DeflaterPending pending)
|
||||
{
|
||||
this.pending = pending;
|
||||
|
||||
literalTree = new Tree(this, LITERAL_NUM, 257, 15);
|
||||
distTree = new Tree(this, DIST_NUM, 1, 15);
|
||||
blTree = new Tree(this, BITLEN_NUM, 4, 7);
|
||||
|
||||
d_buf = new short[BUFSIZE];
|
||||
l_buf = new byte [BUFSIZE];
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
last_lit = 0;
|
||||
extra_bits = 0;
|
||||
literalTree.Reset();
|
||||
distTree.Reset();
|
||||
blTree.Reset();
|
||||
}
|
||||
|
||||
private int L_code(int len)
|
||||
{
|
||||
if (len == 255) {
|
||||
return 285;
|
||||
}
|
||||
|
||||
int code = 257;
|
||||
while (len >= 8) {
|
||||
code += 4;
|
||||
len >>= 1;
|
||||
}
|
||||
return code + len;
|
||||
}
|
||||
|
||||
private int D_code(int distance)
|
||||
{
|
||||
int code = 0;
|
||||
while (distance >= 4) {
|
||||
code += 2;
|
||||
distance >>= 1;
|
||||
}
|
||||
return code + distance;
|
||||
}
|
||||
|
||||
public void SendAllTrees(int blTreeCodes)
|
||||
{
|
||||
blTree.BuildCodes();
|
||||
literalTree.BuildCodes();
|
||||
distTree.BuildCodes();
|
||||
pending.WriteBits(literalTree.numCodes - 257, 5);
|
||||
pending.WriteBits(distTree.numCodes - 1, 5);
|
||||
pending.WriteBits(blTreeCodes - 4, 4);
|
||||
for (int rank = 0; rank < blTreeCodes; rank++) {
|
||||
pending.WriteBits(blTree.length[BL_ORDER[rank]], 3);
|
||||
}
|
||||
literalTree.WriteTree(blTree);
|
||||
distTree.WriteTree(blTree);
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// blTree.CheckEmpty();
|
||||
// }
|
||||
}
|
||||
|
||||
public void CompressBlock()
|
||||
{
|
||||
for (int i = 0; i < last_lit; i++) {
|
||||
int litlen = l_buf[i] & 0xff;
|
||||
int dist = d_buf[i];
|
||||
if (dist-- != 0) {
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.Write("["+(dist+1)+","+(litlen+3)+"]: ");
|
||||
// }
|
||||
|
||||
int lc = L_code(litlen);
|
||||
literalTree.WriteSymbol(lc);
|
||||
|
||||
int bits = (lc - 261) / 4;
|
||||
if (bits > 0 && bits <= 5) {
|
||||
pending.WriteBits(litlen & ((1 << bits) - 1), bits);
|
||||
}
|
||||
|
||||
int dc = D_code(dist);
|
||||
distTree.WriteSymbol(dc);
|
||||
|
||||
bits = dc / 2 - 1;
|
||||
if (bits > 0) {
|
||||
pending.WriteBits(dist & ((1 << bits) - 1), bits);
|
||||
}
|
||||
} else {
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// if (litlen > 32 && litlen < 127) {
|
||||
// Console.Write("("+(char)litlen+"): ");
|
||||
// } else {
|
||||
// Console.Write("{"+litlen+"}: ");
|
||||
// }
|
||||
// }
|
||||
literalTree.WriteSymbol(litlen);
|
||||
}
|
||||
}
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.Write("EOF: ");
|
||||
// }
|
||||
literalTree.WriteSymbol(EOF_SYMBOL);
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// literalTree.CheckEmpty();
|
||||
// distTree.CheckEmpty();
|
||||
// }
|
||||
}
|
||||
|
||||
public void FlushStoredBlock(byte[] stored, int stored_offset, int stored_len, bool lastBlock)
|
||||
{
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("Flushing stored block "+ stored_len);
|
||||
// }
|
||||
pending.WriteBits((DeflaterConstants.STORED_BLOCK << 1)
|
||||
+ (lastBlock ? 1 : 0), 3);
|
||||
pending.AlignToByte();
|
||||
pending.WriteShort(stored_len);
|
||||
pending.WriteShort(~stored_len);
|
||||
pending.WriteBlock(stored, stored_offset, stored_len);
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void FlushBlock(byte[] stored, int stored_offset, int stored_len, bool lastBlock)
|
||||
{
|
||||
literalTree.freqs[EOF_SYMBOL]++;
|
||||
|
||||
/* Build trees */
|
||||
literalTree.BuildTree();
|
||||
distTree.BuildTree();
|
||||
|
||||
/* Calculate bitlen frequency */
|
||||
literalTree.CalcBLFreq(blTree);
|
||||
distTree.CalcBLFreq(blTree);
|
||||
|
||||
/* Build bitlen tree */
|
||||
blTree.BuildTree();
|
||||
|
||||
int blTreeCodes = 4;
|
||||
for (int i = 18; i > blTreeCodes; i--) {
|
||||
if (blTree.length[BL_ORDER[i]] > 0) {
|
||||
blTreeCodes = i+1;
|
||||
}
|
||||
}
|
||||
int opt_len = 14 + blTreeCodes * 3 + blTree.GetEncodedLength() +
|
||||
literalTree.GetEncodedLength() + distTree.GetEncodedLength() +
|
||||
extra_bits;
|
||||
|
||||
int static_len = extra_bits;
|
||||
for (int i = 0; i < LITERAL_NUM; i++) {
|
||||
static_len += literalTree.freqs[i] * staticLLength[i];
|
||||
}
|
||||
for (int i = 0; i < DIST_NUM; i++) {
|
||||
static_len += distTree.freqs[i] * staticDLength[i];
|
||||
}
|
||||
if (opt_len >= static_len) {
|
||||
/* Force static trees */
|
||||
opt_len = static_len;
|
||||
}
|
||||
|
||||
if (stored_offset >= 0 && stored_len+4 < opt_len >> 3) {
|
||||
/* Store Block */
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("Storing, since " + stored_len + " < " + opt_len
|
||||
// + " <= " + static_len);
|
||||
// }
|
||||
FlushStoredBlock(stored, stored_offset, stored_len, lastBlock);
|
||||
} else if (opt_len == static_len) {
|
||||
/* Encode with static tree */
|
||||
pending.WriteBits((DeflaterConstants.STATIC_TREES << 1) + (lastBlock ? 1 : 0), 3);
|
||||
literalTree.SetStaticCodes(staticLCodes, staticLLength);
|
||||
distTree.SetStaticCodes(staticDCodes, staticDLength);
|
||||
CompressBlock();
|
||||
Reset();
|
||||
} else {
|
||||
/* Encode with dynamic tree */
|
||||
pending.WriteBits((DeflaterConstants.DYN_TREES << 1) + (lastBlock ? 1 : 0), 3);
|
||||
SendAllTrees(blTreeCodes);
|
||||
CompressBlock();
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFull()
|
||||
{
|
||||
return last_lit + 16 >= BUFSIZE; // HACK: This was == 'last_lit == BUFSIZE', but errors occured with DeflateFast
|
||||
}
|
||||
|
||||
public bool TallyLit(int lit)
|
||||
{
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// if (lit > 32 && lit < 127) {
|
||||
// Console.WriteLine("("+(char)lit+")");
|
||||
// } else {
|
||||
// Console.WriteLine("{"+lit+"}");
|
||||
// }
|
||||
// }
|
||||
d_buf[last_lit] = 0;
|
||||
l_buf[last_lit++] = (byte)lit;
|
||||
literalTree.freqs[lit]++;
|
||||
return IsFull();
|
||||
}
|
||||
|
||||
public bool TallyDist(int dist, int len)
|
||||
{
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("["+dist+","+len+"]");
|
||||
// }
|
||||
|
||||
d_buf[last_lit] = (short)dist;
|
||||
l_buf[last_lit++] = (byte)(len - 3);
|
||||
|
||||
int lc = L_code(len - 3);
|
||||
literalTree.freqs[lc]++;
|
||||
if (lc >= 265 && lc < 285) {
|
||||
extra_bits += (lc - 261) / 4;
|
||||
}
|
||||
|
||||
int dc = D_code(dist - 1);
|
||||
distTree.freqs[dc]++;
|
||||
if (dc >= 4) {
|
||||
extra_bits += dc / 2 - 1;
|
||||
}
|
||||
return IsFull();
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// DeflaterPending.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// This class stores the pending output of the Deflater.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class DeflaterPending : PendingBuffer
|
||||
{
|
||||
public DeflaterPending() : base(DeflaterConstants.PENDING_BUF_SIZE)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+778
@@ -0,0 +1,778 @@
|
||||
// Inflater.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// Inflater is used to decompress data that has been compressed according
|
||||
/// to the "deflate" standard described in rfc1950.
|
||||
///
|
||||
/// The usage is as following. First you have to set some input with
|
||||
/// <code>setInput()</code>, then inflate() it. If inflate doesn't
|
||||
/// inflate any bytes there may be three reasons:
|
||||
/// <ul>
|
||||
/// <li>needsInput() returns true because the input buffer is empty.
|
||||
/// You have to provide more input with <code>setInput()</code>.
|
||||
/// NOTE: needsInput() also returns true when, the stream is finished.
|
||||
/// </li>
|
||||
/// <li>needsDictionary() returns true, you have to provide a preset
|
||||
/// dictionary with <code>setDictionary()</code>.</li>
|
||||
/// <li>finished() returns true, the inflater has finished.</li>
|
||||
/// </ul>
|
||||
/// Once the first output byte is produced, a dictionary will not be
|
||||
/// needed at a later stage.
|
||||
///
|
||||
/// author of the original java version : John Leuner, Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class Inflater
|
||||
{
|
||||
/// <summary>
|
||||
/// Copy lengths for literal codes 257..285
|
||||
/// </summary>
|
||||
private static int[] CPLENS = {
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
||||
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Extra bits for literal codes 257..285
|
||||
/// </summary>
|
||||
private static int[] CPLEXT = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
||||
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Copy offsets for distance codes 0..29
|
||||
/// </summary>
|
||||
private static int[] CPDIST = {
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||
8193, 12289, 16385, 24577
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Extra bits for distance codes
|
||||
/// </summary>
|
||||
private static int[] CPDEXT = {
|
||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
||||
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
||||
12, 12, 13, 13
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// This are the state in which the inflater can be.
|
||||
/// </summary>
|
||||
private const int DECODE_HEADER = 0;
|
||||
private const int DECODE_DICT = 1;
|
||||
private const int DECODE_BLOCKS = 2;
|
||||
private const int DECODE_STORED_LEN1 = 3;
|
||||
private const int DECODE_STORED_LEN2 = 4;
|
||||
private const int DECODE_STORED = 5;
|
||||
private const int DECODE_DYN_HEADER = 6;
|
||||
private const int DECODE_HUFFMAN = 7;
|
||||
private const int DECODE_HUFFMAN_LENBITS = 8;
|
||||
private const int DECODE_HUFFMAN_DIST = 9;
|
||||
private const int DECODE_HUFFMAN_DISTBITS = 10;
|
||||
private const int DECODE_CHKSUM = 11;
|
||||
private const int FINISHED = 12;
|
||||
|
||||
/// <summary>
|
||||
/// This variable contains the current state.
|
||||
/// </summary>
|
||||
private int mode;
|
||||
|
||||
/// <summary>
|
||||
/// The adler checksum of the dictionary or of the decompressed
|
||||
/// stream, as it is written in the header resp. footer of the
|
||||
/// compressed stream.
|
||||
/// Only valid if mode is DECODE_DICT or DECODE_CHKSUM.
|
||||
/// </summary>
|
||||
private int readAdler;
|
||||
|
||||
/// <summary>
|
||||
/// The number of bits needed to complete the current state. This
|
||||
/// is valid, if mode is DECODE_DICT, DECODE_CHKSUM,
|
||||
/// DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS.
|
||||
/// </summary>
|
||||
private int neededBits;
|
||||
private int repLength, repDist;
|
||||
private int uncomprLen;
|
||||
|
||||
/// <summary>
|
||||
/// True, if the last block flag was set in the last block of the
|
||||
/// inflated stream. This means that the stream ends after the
|
||||
/// current block.
|
||||
/// </summary>
|
||||
private bool isLastBlock;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of inflated bytes.
|
||||
/// </summary>
|
||||
private int totalOut;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of bytes set with setInput(). This is not the
|
||||
/// value returned by getTotalIn(), since this also includes the
|
||||
/// unprocessed input.
|
||||
/// </summary>
|
||||
private int totalIn;
|
||||
|
||||
/// <summary>
|
||||
/// This variable stores the nowrap flag that was given to the constructor.
|
||||
/// True means, that the inflated stream doesn't contain a header nor the
|
||||
/// checksum in the footer.
|
||||
/// </summary>
|
||||
private bool nowrap;
|
||||
|
||||
private StreamManipulator input;
|
||||
private OutputWindow outputWindow;
|
||||
private InflaterDynHeader dynHeader;
|
||||
private InflaterHuffmanTree litlenTree, distTree;
|
||||
private Adler32 adler;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new inflater.
|
||||
/// </summary>
|
||||
public Inflater() : this(false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new inflater.
|
||||
/// </summary>
|
||||
/// <param name="nowrap">
|
||||
/// true if no header and checksum field appears in the
|
||||
/// stream. This is used for GZIPed input. For compatibility with
|
||||
/// Sun JDK you should provide one byte of input more than needed in
|
||||
/// this case.
|
||||
/// </param>
|
||||
public Inflater(bool nowrap)
|
||||
{
|
||||
this.nowrap = nowrap;
|
||||
this.adler = new Adler32();
|
||||
input = new StreamManipulator();
|
||||
outputWindow = new OutputWindow();
|
||||
mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the inflater so that a new stream can be decompressed. All
|
||||
/// pending input and output will be discarded.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
|
||||
totalIn = totalOut = 0;
|
||||
input.Reset();
|
||||
outputWindow.Reset();
|
||||
dynHeader = null;
|
||||
litlenTree = null;
|
||||
distTree = null;
|
||||
isLastBlock = false;
|
||||
adler.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the deflate header.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// false if more input is needed.
|
||||
/// </returns>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// if header is invalid.
|
||||
/// </exception>
|
||||
private bool DecodeHeader()
|
||||
{
|
||||
int header = input.PeekBits(16);
|
||||
if (header < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(16);
|
||||
/* The header is written in "wrong" byte order */
|
||||
header = ((header << 8) | (header >> 8)) & 0xffff;
|
||||
if (header % 31 != 0) {
|
||||
throw new FormatException("Header checksum illegal");
|
||||
}
|
||||
|
||||
if ((header & 0x0f00) != (Deflater.DEFLATED << 8)) {
|
||||
throw new FormatException("Compression Method unknown");
|
||||
}
|
||||
|
||||
/* Maximum size of the backwards window in bits.
|
||||
* We currently ignore this, but we could use it to make the
|
||||
* inflater window more space efficient. On the other hand the
|
||||
* full window (15 bits) is needed most times, anyway.
|
||||
int max_wbits = ((header & 0x7000) >> 12) + 8;
|
||||
*/
|
||||
|
||||
if ((header & 0x0020) == 0) { // Dictionary flag?
|
||||
mode = DECODE_BLOCKS;
|
||||
} else {
|
||||
mode = DECODE_DICT;
|
||||
neededBits = 32;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the dictionary checksum after the deflate header.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// false if more input is needed.
|
||||
/// </returns>
|
||||
private bool DecodeDict()
|
||||
{
|
||||
while (neededBits > 0) {
|
||||
int dictByte = input.PeekBits(8);
|
||||
if (dictByte < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(8);
|
||||
readAdler = (readAdler << 8) | dictByte;
|
||||
neededBits -= 8;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the huffman encoded symbols in the input stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// false if more input is needed, true if output window is
|
||||
/// full or the current block ends.
|
||||
/// </returns>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// if deflated stream is invalid.
|
||||
/// </exception>
|
||||
private bool DecodeHuffman()
|
||||
{
|
||||
int free = outputWindow.GetFreeSpace();
|
||||
while (free >= 258) {
|
||||
int symbol;
|
||||
switch (mode) {
|
||||
case DECODE_HUFFMAN:
|
||||
/* This is the inner loop so it is optimized a bit */
|
||||
while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) {
|
||||
outputWindow.Write(symbol);
|
||||
if (--free < 258) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (symbol < 257) {
|
||||
if (symbol < 0) {
|
||||
return false;
|
||||
} else {
|
||||
/* symbol == 256: end of block */
|
||||
distTree = null;
|
||||
litlenTree = null;
|
||||
mode = DECODE_BLOCKS;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
repLength = CPLENS[symbol - 257];
|
||||
neededBits = CPLEXT[symbol - 257];
|
||||
} catch (Exception) {
|
||||
throw new FormatException("Illegal rep length code");
|
||||
}
|
||||
goto case DECODE_HUFFMAN_LENBITS;/* fall through */
|
||||
case DECODE_HUFFMAN_LENBITS:
|
||||
if (neededBits > 0) {
|
||||
mode = DECODE_HUFFMAN_LENBITS;
|
||||
int i = input.PeekBits(neededBits);
|
||||
if (i < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(neededBits);
|
||||
repLength += i;
|
||||
}
|
||||
mode = DECODE_HUFFMAN_DIST;
|
||||
goto case DECODE_HUFFMAN_DIST;/* fall through */
|
||||
case DECODE_HUFFMAN_DIST:
|
||||
symbol = distTree.GetSymbol(input);
|
||||
if (symbol < 0) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
repDist = CPDIST[symbol];
|
||||
neededBits = CPDEXT[symbol];
|
||||
} catch (Exception) {
|
||||
throw new FormatException("Illegal rep dist code");
|
||||
}
|
||||
|
||||
goto case DECODE_HUFFMAN_DISTBITS;/* fall through */
|
||||
case DECODE_HUFFMAN_DISTBITS:
|
||||
if (neededBits > 0) {
|
||||
mode = DECODE_HUFFMAN_DISTBITS;
|
||||
int i = input.PeekBits(neededBits);
|
||||
if (i < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(neededBits);
|
||||
repDist += i;
|
||||
}
|
||||
outputWindow.Repeat(repLength, repDist);
|
||||
free -= repLength;
|
||||
mode = DECODE_HUFFMAN;
|
||||
break;
|
||||
default:
|
||||
throw new FormatException();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the adler checksum after the deflate stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// false if more input is needed.
|
||||
/// </returns>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// DataFormatException, if checksum doesn't match.
|
||||
/// </exception>
|
||||
private bool DecodeChksum()
|
||||
{
|
||||
while (neededBits > 0) {
|
||||
int chkByte = input.PeekBits(8);
|
||||
if (chkByte < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(8);
|
||||
readAdler = (readAdler << 8) | chkByte;
|
||||
neededBits -= 8;
|
||||
}
|
||||
if ((int) adler.Value != readAdler) {
|
||||
throw new FormatException("Adler chksum doesn't match: "
|
||||
+ (int)adler.Value
|
||||
+ " vs. " + readAdler);
|
||||
}
|
||||
mode = FINISHED;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the deflated stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// false if more input is needed, or if finished.
|
||||
/// </returns>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// DataFormatException, if deflated stream is invalid.
|
||||
/// </exception>
|
||||
private bool Decode()
|
||||
{
|
||||
switch (mode) {
|
||||
case DECODE_HEADER:
|
||||
return DecodeHeader();
|
||||
case DECODE_DICT:
|
||||
return DecodeDict();
|
||||
case DECODE_CHKSUM:
|
||||
return DecodeChksum();
|
||||
|
||||
case DECODE_BLOCKS:
|
||||
if (isLastBlock) {
|
||||
if (nowrap) {
|
||||
mode = FINISHED;
|
||||
return false;
|
||||
} else {
|
||||
input.SkipToByteBoundary();
|
||||
neededBits = 32;
|
||||
mode = DECODE_CHKSUM;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int type = input.PeekBits(3);
|
||||
if (type < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(3);
|
||||
|
||||
if ((type & 1) != 0) {
|
||||
isLastBlock = true;
|
||||
}
|
||||
switch (type >> 1) {
|
||||
case DeflaterConstants.STORED_BLOCK:
|
||||
input.SkipToByteBoundary();
|
||||
mode = DECODE_STORED_LEN1;
|
||||
break;
|
||||
case DeflaterConstants.STATIC_TREES:
|
||||
litlenTree = InflaterHuffmanTree.defLitLenTree;
|
||||
distTree = InflaterHuffmanTree.defDistTree;
|
||||
mode = DECODE_HUFFMAN;
|
||||
break;
|
||||
case DeflaterConstants.DYN_TREES:
|
||||
dynHeader = new InflaterDynHeader();
|
||||
mode = DECODE_DYN_HEADER;
|
||||
break;
|
||||
default:
|
||||
throw new FormatException("Unknown block type "+type);
|
||||
}
|
||||
return true;
|
||||
|
||||
case DECODE_STORED_LEN1: {
|
||||
if ((uncomprLen = input.PeekBits(16)) < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(16);
|
||||
mode = DECODE_STORED_LEN2;
|
||||
}
|
||||
goto case DECODE_STORED_LEN2; /* fall through */
|
||||
case DECODE_STORED_LEN2: {
|
||||
int nlen = input.PeekBits(16);
|
||||
if (nlen < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(16);
|
||||
if (nlen != (uncomprLen ^ 0xffff)) {
|
||||
throw new FormatException("broken uncompressed block");
|
||||
}
|
||||
mode = DECODE_STORED;
|
||||
}
|
||||
goto case DECODE_STORED;/* fall through */
|
||||
case DECODE_STORED: {
|
||||
int more = outputWindow.CopyStored(input, uncomprLen);
|
||||
uncomprLen -= more;
|
||||
if (uncomprLen == 0) {
|
||||
mode = DECODE_BLOCKS;
|
||||
return true;
|
||||
}
|
||||
return !input.IsNeedingInput;
|
||||
}
|
||||
|
||||
case DECODE_DYN_HEADER:
|
||||
if (!dynHeader.Decode(input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
litlenTree = dynHeader.BuildLitLenTree();
|
||||
distTree = dynHeader.BuildDistTree();
|
||||
mode = DECODE_HUFFMAN;
|
||||
goto case DECODE_HUFFMAN; /* fall through */
|
||||
case DECODE_HUFFMAN:
|
||||
case DECODE_HUFFMAN_LENBITS:
|
||||
case DECODE_HUFFMAN_DIST:
|
||||
case DECODE_HUFFMAN_DISTBITS:
|
||||
return DecodeHuffman();
|
||||
case FINISHED:
|
||||
return false;
|
||||
default:
|
||||
throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the preset dictionary. This should only be called, if
|
||||
/// needsDictionary() returns true and it should set the same
|
||||
/// dictionary, that was used for deflating. The getAdler()
|
||||
/// function returns the checksum of the dictionary needed.
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// the dictionary.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no dictionary is needed.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentException">
|
||||
/// if the dictionary checksum is wrong.
|
||||
/// </exception>
|
||||
public void SetDictionary(byte[] buffer)
|
||||
{
|
||||
SetDictionary(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the preset dictionary. This should only be called, if
|
||||
/// needsDictionary() returns true and it should set the same
|
||||
/// dictionary, that was used for deflating. The getAdler()
|
||||
/// function returns the checksum of the dictionary needed.
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// the dictionary.
|
||||
/// </param>
|
||||
/// <param name="off">
|
||||
/// the offset into buffer where the dictionary starts.
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the length of the dictionary.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no dictionary is needed.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentException">
|
||||
/// if the dictionary checksum is wrong.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if the off and/or len are wrong.
|
||||
/// </exception>
|
||||
public void SetDictionary(byte[] buffer, int off, int len)
|
||||
{
|
||||
if (!IsNeedingDictionary) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
adler.Update(buffer, off, len);
|
||||
if ((int) adler.Value != readAdler) {
|
||||
throw new ArgumentException("Wrong adler checksum");
|
||||
}
|
||||
adler.Reset();
|
||||
outputWindow.CopyDict(buffer, off, len);
|
||||
mode = DECODE_BLOCKS;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the input. This should only be called, if needsInput()
|
||||
/// returns true.
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// the input.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no input is needed.
|
||||
/// </exception>
|
||||
public void SetInput(byte[] buf)
|
||||
{
|
||||
SetInput(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the input. This should only be called, if needsInput()
|
||||
/// returns true.
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// the input.
|
||||
/// </param>
|
||||
/// <param name="off">
|
||||
/// the offset into buffer where the input starts.
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the length of the input.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no input is needed.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if the off and/or len are wrong.
|
||||
/// </exception>
|
||||
public void SetInput(byte[] buf, int off, int len)
|
||||
{
|
||||
input.SetInput(buf, off, len);
|
||||
totalIn += len;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflates the compressed stream to the output buffer. If this
|
||||
/// returns 0, you should check, whether needsDictionary(),
|
||||
/// needsInput() or finished() returns true, to determine why no
|
||||
/// further output is produced.
|
||||
/// </summary>
|
||||
/// <param name = "buf">
|
||||
/// the output buffer.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// the number of bytes written to the buffer, 0 if no further
|
||||
/// output can be produced.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if buf has length 0.
|
||||
/// </exception>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// if deflated stream is invalid.
|
||||
/// </exception>
|
||||
public int Inflate(byte[] buf)
|
||||
{
|
||||
return Inflate(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflates the compressed stream to the output buffer. If this
|
||||
/// returns 0, you should check, whether needsDictionary(),
|
||||
/// needsInput() or finished() returns true, to determine why no
|
||||
/// further output is produced.
|
||||
/// </summary>
|
||||
/// <param name = "buf">
|
||||
/// the output buffer.
|
||||
/// </param>
|
||||
/// <param name = "off">
|
||||
/// the offset into buffer where the output should start.
|
||||
/// </param>
|
||||
/// <param name = "len">
|
||||
/// the maximum length of the output.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// the number of bytes written to the buffer, 0 if no further output can be produced.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if len is <= 0.
|
||||
/// </exception>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if the off and/or len are wrong.
|
||||
/// </exception>
|
||||
/// <exception cref="System.FormatException">
|
||||
/// if deflated stream is invalid.
|
||||
/// </exception>
|
||||
public int Inflate(byte[] buf, int off, int len)
|
||||
{
|
||||
if (len < 0) {
|
||||
throw new ArgumentOutOfRangeException("len < 0");
|
||||
}
|
||||
// Special case: len may be zero
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
/* // Check for correct buff, off, len triple
|
||||
if (off < 0 || off + len >= buf.Length) {
|
||||
throw new ArgumentException("off/len outside buf bounds");
|
||||
}*/
|
||||
int count = 0;
|
||||
int more;
|
||||
do {
|
||||
if (mode != DECODE_CHKSUM) {
|
||||
/* Don't give away any output, if we are waiting for the
|
||||
* checksum in the input stream.
|
||||
*
|
||||
* With this trick we have always:
|
||||
* needsInput() and not finished()
|
||||
* implies more output can be produced.
|
||||
*/
|
||||
more = outputWindow.CopyOutput(buf, off, len);
|
||||
adler.Update(buf, off, more);
|
||||
off += more;
|
||||
count += more;
|
||||
totalOut += more;
|
||||
len -= more;
|
||||
if (len == 0) {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
} while (Decode() || (outputWindow.GetAvailable() > 0 &&
|
||||
mode != DECODE_CHKSUM));
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if the input buffer is empty.
|
||||
/// You should then call setInput().
|
||||
/// NOTE: This method also returns true when the stream is finished.
|
||||
/// </summary>
|
||||
public bool IsNeedingInput {
|
||||
get {
|
||||
return input.IsNeedingInput;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if a preset dictionary is needed to inflate the input.
|
||||
/// </summary>
|
||||
public bool IsNeedingDictionary {
|
||||
get {
|
||||
return mode == DECODE_DICT && neededBits == 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if the inflater has finished. This means, that no
|
||||
/// input is needed and no output can be produced.
|
||||
/// </summary>
|
||||
public bool IsFinished {
|
||||
get {
|
||||
return mode == FINISHED && outputWindow.GetAvailable() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the adler checksum. This is either the checksum of all
|
||||
/// uncompressed bytes returned by inflate(), or if needsDictionary()
|
||||
/// returns true (and thus no output was yet produced) this is the
|
||||
/// adler checksum of the expected dictionary.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the adler checksum.
|
||||
/// </returns>
|
||||
public int Adler {
|
||||
get {
|
||||
return IsNeedingDictionary ? readAdler : (int) adler.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of output bytes returned by inflate().
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the total number of output bytes.
|
||||
/// </returns>
|
||||
public int TotalOut {
|
||||
get {
|
||||
return totalOut;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of processed compressed input bytes.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the total number of bytes of processed input bytes.
|
||||
/// </returns>
|
||||
public int TotalIn {
|
||||
get {
|
||||
return totalIn - RemainingInput;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of unprocessed input. Useful, if the end of the
|
||||
/// stream is reached and you want to further process the bytes after
|
||||
/// the deflate stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the number of bytes of the input which were not processed.
|
||||
/// </returns>
|
||||
public int RemainingInput {
|
||||
get {
|
||||
return input.AvailableBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
// InflaterDynHeader.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
class InflaterDynHeader
|
||||
{
|
||||
const int LNUM = 0;
|
||||
const int DNUM = 1;
|
||||
const int BLNUM = 2;
|
||||
const int BLLENS = 3;
|
||||
const int LENS = 4;
|
||||
const int REPS = 5;
|
||||
|
||||
static readonly int[] repMin = { 3, 3, 11 };
|
||||
static readonly int[] repBits = { 2, 3, 7 };
|
||||
|
||||
byte[] blLens;
|
||||
byte[] litdistLens;
|
||||
|
||||
InflaterHuffmanTree blTree;
|
||||
|
||||
int mode;
|
||||
int lnum, dnum, blnum, num;
|
||||
int repSymbol;
|
||||
byte lastLen;
|
||||
int ptr;
|
||||
|
||||
static readonly int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
|
||||
|
||||
public InflaterDynHeader()
|
||||
{
|
||||
}
|
||||
|
||||
public bool Decode(StreamManipulator input)
|
||||
{
|
||||
decode_loop:
|
||||
for (;;) {
|
||||
switch (mode) {
|
||||
case LNUM:
|
||||
lnum = input.PeekBits(5);
|
||||
if (lnum < 0) {
|
||||
return false;
|
||||
}
|
||||
lnum += 257;
|
||||
input.DropBits(5);
|
||||
// System.err.println("LNUM: "+lnum);
|
||||
mode = DNUM;
|
||||
goto case DNUM; // fall through
|
||||
case DNUM:
|
||||
dnum = input.PeekBits(5);
|
||||
if (dnum < 0) {
|
||||
return false;
|
||||
}
|
||||
dnum++;
|
||||
input.DropBits(5);
|
||||
// System.err.println("DNUM: "+dnum);
|
||||
num = lnum+dnum;
|
||||
litdistLens = new byte[num];
|
||||
mode = BLNUM;
|
||||
goto case BLNUM; // fall through
|
||||
case BLNUM:
|
||||
blnum = input.PeekBits(4);
|
||||
if (blnum < 0) {
|
||||
return false;
|
||||
}
|
||||
blnum += 4;
|
||||
input.DropBits(4);
|
||||
blLens = new byte[19];
|
||||
ptr = 0;
|
||||
// System.err.println("BLNUM: "+blnum);
|
||||
mode = BLLENS;
|
||||
goto case BLLENS; // fall through
|
||||
case BLLENS:
|
||||
while (ptr < blnum) {
|
||||
int len = input.PeekBits(3);
|
||||
if (len < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(3);
|
||||
// System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
|
||||
blLens[BL_ORDER[ptr]] = (byte) len;
|
||||
ptr++;
|
||||
}
|
||||
blTree = new InflaterHuffmanTree(blLens);
|
||||
blLens = null;
|
||||
ptr = 0;
|
||||
mode = LENS;
|
||||
goto case LENS; // fall through
|
||||
case LENS: {
|
||||
int symbol;
|
||||
while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
|
||||
/* Normal case: symbol in [0..15] */
|
||||
|
||||
// System.err.println("litdistLens["+ptr+"]: "+symbol);
|
||||
litdistLens[ptr++] = lastLen = (byte)symbol;
|
||||
|
||||
if (ptr == num) {
|
||||
/* Finished */
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* need more input ? */
|
||||
if (symbol < 0)
|
||||
return false;
|
||||
|
||||
/* otherwise repeat code */
|
||||
if (symbol >= 17) {
|
||||
/* repeat zero */
|
||||
// System.err.println("repeating zero");
|
||||
lastLen = 0;
|
||||
} else {
|
||||
if (ptr == 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
repSymbol = symbol-16;
|
||||
}
|
||||
mode = REPS;
|
||||
goto case REPS; // fall through
|
||||
case REPS:
|
||||
{
|
||||
int bits = repBits[repSymbol];
|
||||
int count = input.PeekBits(bits);
|
||||
if (count < 0) {
|
||||
return false;
|
||||
}
|
||||
input.DropBits(bits);
|
||||
count += repMin[repSymbol];
|
||||
// System.err.println("litdistLens repeated: "+count);
|
||||
|
||||
if (ptr + count > num) {
|
||||
throw new Exception();
|
||||
}
|
||||
while (count-- > 0) {
|
||||
litdistLens[ptr++] = lastLen;
|
||||
}
|
||||
|
||||
if (ptr == num) {
|
||||
/* Finished */
|
||||
return true;
|
||||
}
|
||||
}
|
||||
mode = LENS;
|
||||
goto decode_loop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public InflaterHuffmanTree BuildLitLenTree()
|
||||
{
|
||||
byte[] litlenLens = new byte[lnum];
|
||||
Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
|
||||
return new InflaterHuffmanTree(litlenLens);
|
||||
}
|
||||
|
||||
public InflaterHuffmanTree BuildDistTree()
|
||||
{
|
||||
byte[] distLens = new byte[dnum];
|
||||
Array.Copy(litdistLens, lnum, distLens, 0, dnum);
|
||||
return new InflaterHuffmanTree(distLens);
|
||||
}
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
// InflaterHuffmanTree.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
public class InflaterHuffmanTree
|
||||
{
|
||||
private static int MAX_BITLEN = 15;
|
||||
private short[] tree;
|
||||
|
||||
public static InflaterHuffmanTree defLitLenTree, defDistTree;
|
||||
|
||||
static InflaterHuffmanTree()
|
||||
{
|
||||
try {
|
||||
byte[] codeLengths = new byte[288];
|
||||
int i = 0;
|
||||
while (i < 144) {
|
||||
codeLengths[i++] = 8;
|
||||
}
|
||||
while (i < 256) {
|
||||
codeLengths[i++] = 9;
|
||||
}
|
||||
while (i < 280) {
|
||||
codeLengths[i++] = 7;
|
||||
}
|
||||
while (i < 288) {
|
||||
codeLengths[i++] = 8;
|
||||
}
|
||||
defLitLenTree = new InflaterHuffmanTree(codeLengths);
|
||||
|
||||
codeLengths = new byte[32];
|
||||
i = 0;
|
||||
while (i < 32) {
|
||||
codeLengths[i++] = 5;
|
||||
}
|
||||
defDistTree = new InflaterHuffmanTree(codeLengths);
|
||||
} catch (Exception) {
|
||||
throw new ApplicationException("InflaterHuffmanTree: static tree length illegal");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a Huffman tree from the array of code lengths.
|
||||
/// </summary>
|
||||
/// <param name = "codeLengths">
|
||||
/// the array of code lengths
|
||||
/// </param>
|
||||
public InflaterHuffmanTree(byte[] codeLengths)
|
||||
{
|
||||
BuildTree(codeLengths);
|
||||
}
|
||||
|
||||
private void BuildTree(byte[] codeLengths)
|
||||
{
|
||||
int[] blCount = new int[MAX_BITLEN + 1];
|
||||
int[] nextCode = new int[MAX_BITLEN + 1];
|
||||
|
||||
for (int i = 0; i < codeLengths.Length; i++) {
|
||||
int bits = codeLengths[i];
|
||||
if (bits > 0)
|
||||
blCount[bits]++;
|
||||
}
|
||||
|
||||
int code = 0;
|
||||
int treeSize = 512;
|
||||
for (int bits = 1; bits <= MAX_BITLEN; bits++) {
|
||||
nextCode[bits] = code;
|
||||
code += blCount[bits] << (16 - bits);
|
||||
if (bits >= 10) {
|
||||
/* We need an extra table for bit lengths >= 10. */
|
||||
int start = nextCode[bits] & 0x1ff80;
|
||||
int end = code & 0x1ff80;
|
||||
treeSize += (end - start) >> (16 - bits);
|
||||
}
|
||||
}
|
||||
if (code != 65536) {
|
||||
throw new Exception("Code lengths don't add up properly.");
|
||||
}
|
||||
/* Now create and fill the extra tables from longest to shortest
|
||||
* bit len. This way the sub trees will be aligned.
|
||||
*/
|
||||
tree = new short[treeSize];
|
||||
int treePtr = 512;
|
||||
for (int bits = MAX_BITLEN; bits >= 10; bits--) {
|
||||
int end = code & 0x1ff80;
|
||||
code -= blCount[bits] << (16 - bits);
|
||||
int start = code & 0x1ff80;
|
||||
for (int i = start; i < end; i += 1 << 7) {
|
||||
tree[DeflaterHuffman.BitReverse(i)] = (short) ((-treePtr << 4) | bits);
|
||||
treePtr += 1 << (bits-9);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < codeLengths.Length; i++) {
|
||||
int bits = codeLengths[i];
|
||||
if (bits == 0) {
|
||||
continue;
|
||||
}
|
||||
code = nextCode[bits];
|
||||
int revcode = DeflaterHuffman.BitReverse(code);
|
||||
if (bits <= 9) {
|
||||
do {
|
||||
tree[revcode] = (short) ((i << 4) | bits);
|
||||
revcode += 1 << bits;
|
||||
} while (revcode < 512);
|
||||
} else {
|
||||
int subTree = tree[revcode & 511];
|
||||
int treeLen = 1 << (subTree & 15);
|
||||
subTree = -(subTree >> 4);
|
||||
do {
|
||||
tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
|
||||
revcode += 1 << bits;
|
||||
} while (revcode < treeLen);
|
||||
}
|
||||
nextCode[bits] = code + (1 << (16 - bits));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the next symbol from input. The symbol is encoded using the
|
||||
/// huffman tree.
|
||||
/// </summary>
|
||||
/// <param name="input">
|
||||
/// input the input source.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// the next symbol, or -1 if not enough input is available.
|
||||
/// </returns>
|
||||
public int GetSymbol(StreamManipulator input)
|
||||
{
|
||||
int lookahead, symbol;
|
||||
if ((lookahead = input.PeekBits(9)) >= 0) {
|
||||
if ((symbol = tree[lookahead]) >= 0) {
|
||||
input.DropBits(symbol & 15);
|
||||
return symbol >> 4;
|
||||
}
|
||||
int subtree = -(symbol >> 4);
|
||||
int bitlen = symbol & 15;
|
||||
if ((lookahead = input.PeekBits(bitlen)) >= 0) {
|
||||
symbol = tree[subtree | (lookahead >> 9)];
|
||||
input.DropBits(symbol & 15);
|
||||
return symbol >> 4;
|
||||
} else {
|
||||
int bits = input.AvailableBits;
|
||||
lookahead = input.PeekBits(bits);
|
||||
symbol = tree[subtree | (lookahead >> 9)];
|
||||
if ((symbol & 15) <= bits) {
|
||||
input.DropBits(symbol & 15);
|
||||
return symbol >> 4;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int bits = input.AvailableBits;
|
||||
lookahead = input.PeekBits(bits);
|
||||
symbol = tree[lookahead];
|
||||
if (symbol >= 0 && (symbol & 15) <= bits) {
|
||||
input.DropBits(symbol & 15);
|
||||
return symbol >> 4;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
// PendingBuffer.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression {
|
||||
|
||||
/// <summary>
|
||||
/// This class is general purpose class for writing data to a buffer.
|
||||
///
|
||||
/// It allows you to write bits as well as bytes
|
||||
/// Based on DeflaterPending.java
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class PendingBuffer
|
||||
{
|
||||
protected byte[] buf;
|
||||
int start;
|
||||
int end;
|
||||
|
||||
uint bits;
|
||||
int bitCount;
|
||||
|
||||
public PendingBuffer() : this( 4096 )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public PendingBuffer(int bufsize)
|
||||
{
|
||||
buf = new byte[bufsize];
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
start = end = bitCount = 0;
|
||||
}
|
||||
|
||||
public void WriteByte(int b)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0)
|
||||
throw new Exception();
|
||||
buf[end++] = (byte) b;
|
||||
}
|
||||
|
||||
public void WriteShort(int s)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
buf[end++] = (byte) s;
|
||||
buf[end++] = (byte) (s >> 8);
|
||||
}
|
||||
|
||||
public void WriteInt(int s)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
buf[end++] = (byte) s;
|
||||
buf[end++] = (byte) (s >> 8);
|
||||
buf[end++] = (byte) (s >> 16);
|
||||
buf[end++] = (byte) (s >> 24);
|
||||
}
|
||||
|
||||
public void WriteBlock(byte[] block, int offset, int len)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
System.Array.Copy(block, offset, buf, end, len);
|
||||
end += len;
|
||||
}
|
||||
|
||||
public int BitCount {
|
||||
get {
|
||||
return bitCount;
|
||||
}
|
||||
}
|
||||
|
||||
public void AlignToByte()
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
if (bitCount > 0) {
|
||||
buf[end++] = (byte) bits;
|
||||
if (bitCount > 8) {
|
||||
buf[end++] = (byte) (bits >> 8);
|
||||
}
|
||||
}
|
||||
bits = 0;
|
||||
bitCount = 0;
|
||||
}
|
||||
|
||||
public void WriteBits(int b, int count)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
// if (DeflaterConstants.DEBUGGING) {
|
||||
// Console.WriteLine("writeBits("+b+","+count+")");
|
||||
// }
|
||||
bits |= (uint)(b << bitCount);
|
||||
bitCount += count;
|
||||
if (bitCount >= 16) {
|
||||
buf[end++] = (byte) bits;
|
||||
buf[end++] = (byte) (bits >> 8);
|
||||
bits >>= 16;
|
||||
bitCount -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteShortMSB(int s)
|
||||
{
|
||||
if (DeflaterConstants.DEBUGGING && start != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
buf[end++] = (byte) (s >> 8);
|
||||
buf[end++] = (byte) s;
|
||||
}
|
||||
|
||||
public bool IsFlushed {
|
||||
get {
|
||||
return end == 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the pending buffer into the given output array. If the
|
||||
/// output array is to small, only a partial flush is done.
|
||||
/// </summary>
|
||||
/// <param name="output">
|
||||
/// the output array;
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// the offset into output array;
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// length the maximum number of bytes to store;
|
||||
/// </param>
|
||||
/// <exception name="ArgumentOutOfRangeException">
|
||||
/// IndexOutOfBoundsException if offset or length are invalid.
|
||||
/// </exception>
|
||||
public int Flush(byte[] output, int offset, int length)
|
||||
{
|
||||
if (bitCount >= 8) {
|
||||
buf[end++] = (byte) bits;
|
||||
bits >>= 8;
|
||||
bitCount -= 8;
|
||||
}
|
||||
if (length > end - start) {
|
||||
length = end - start;
|
||||
System.Array.Copy(buf, start, output, offset, length);
|
||||
start = 0;
|
||||
end = 0;
|
||||
} else {
|
||||
System.Array.Copy(buf, start, output, offset, length);
|
||||
start += length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
byte[] ret = new byte[ end - start ];
|
||||
System.Array.Copy(buf, start, ret, 0, ret.Length);
|
||||
start = 0;
|
||||
end = 0;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
// DeflaterOutputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams {
|
||||
|
||||
/// <summary>
|
||||
/// This is a special FilterOutputStream deflating the bytes that are
|
||||
/// written through it. It uses the Deflater for deflating.
|
||||
///
|
||||
/// authors of the original java version : Tom Tromey, Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class DeflaterOutputStream : Stream
|
||||
{
|
||||
/// <summary>
|
||||
/// This buffer is used temporarily to retrieve the bytes from the
|
||||
/// deflater and write them to the underlying output stream.
|
||||
/// </summary>
|
||||
protected byte[] buf;
|
||||
|
||||
/// <summary>
|
||||
/// The deflater which is used to deflate the stream.
|
||||
/// </summary>
|
||||
protected Deflater def;
|
||||
|
||||
/// <summary>
|
||||
/// base stream the deflater depends on.
|
||||
/// </summary>
|
||||
protected Stream baseOutputStream;
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanRead {
|
||||
get {
|
||||
return baseOutputStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanSeek {
|
||||
get {
|
||||
return baseOutputStream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanWrite {
|
||||
get {
|
||||
return baseOutputStream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Length {
|
||||
get {
|
||||
return baseOutputStream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Position {
|
||||
get {
|
||||
return baseOutputStream.Position;
|
||||
}
|
||||
set {
|
||||
baseOutputStream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return baseOutputStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void SetLength(long val)
|
||||
{
|
||||
baseOutputStream.SetLength(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override int ReadByte()
|
||||
{
|
||||
return baseOutputStream.ReadByte();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
return baseOutputStream.Read(b, off, len);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deflates everything in the def's input buffers. This will call
|
||||
/// <code>def.deflate()</code> until all bytes from the input buffers
|
||||
/// are processed.
|
||||
/// </summary>
|
||||
protected void deflate()
|
||||
{
|
||||
while (!def.IsNeedingInput) {
|
||||
int len = def.Deflate(buf, 0, buf.Length);
|
||||
|
||||
// System.err.println("DOS deflated " + len + " baseOutputStream of " + buf.length);
|
||||
if (len <= 0) {
|
||||
break;
|
||||
}
|
||||
baseOutputStream.Write(buf, 0, len);
|
||||
}
|
||||
|
||||
if (!def.IsNeedingInput) {
|
||||
throw new ApplicationException("Can't deflate all input?");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new DeflaterOutputStream with a default Deflater and default buffer size.
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// the output stream where deflated output should be written.
|
||||
/// </param>
|
||||
public DeflaterOutputStream(Stream baseOutputStream) : this(baseOutputStream, new Deflater(), 512)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new DeflaterOutputStream with the given Deflater and
|
||||
/// default buffer size.
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// the output stream where deflated output should be written.
|
||||
/// </param>
|
||||
/// <param name="defl">
|
||||
/// the underlying deflater.
|
||||
/// </param>
|
||||
public DeflaterOutputStream(Stream baseOutputStream, Deflater defl) :this(baseOutputStream, defl, 512)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new DeflaterOutputStream with the given Deflater and
|
||||
/// buffer size.
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// the output stream where deflated output should be written.
|
||||
/// </param>
|
||||
/// <param name="defl">
|
||||
/// the underlying deflater.
|
||||
/// </param>
|
||||
/// <param name="bufsize">
|
||||
/// the buffer size.
|
||||
/// </param>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if bufsize isn't positive.
|
||||
/// </exception>
|
||||
public DeflaterOutputStream(Stream baseOutputStream, Deflater defl, int bufsize)
|
||||
{
|
||||
this.baseOutputStream = baseOutputStream;
|
||||
if (bufsize <= 0) {
|
||||
throw new InvalidOperationException("bufsize <= 0");
|
||||
}
|
||||
buf = new byte[bufsize];
|
||||
def = defl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the stream by calling flush() on the deflater and then
|
||||
/// on the underlying stream. This ensures that all bytes are
|
||||
/// flushed.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
def.Flush();
|
||||
deflate();
|
||||
baseOutputStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finishes the stream by calling finish() on the deflater.
|
||||
/// </summary>
|
||||
public virtual void Finish()
|
||||
{
|
||||
def.Finish();
|
||||
while (!def.IsFinished) {
|
||||
int len = def.Deflate(buf, 0, buf.Length);
|
||||
if (len <= 0) {
|
||||
break;
|
||||
}
|
||||
baseOutputStream.Write(buf, 0, len);
|
||||
}
|
||||
if (!def.IsFinished) {
|
||||
throw new ApplicationException("Can't deflate all input?");
|
||||
}
|
||||
baseOutputStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calls finish () and closes the stream.
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
Finish();
|
||||
baseOutputStream.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a single byte to the compressed output stream.
|
||||
/// </summary>
|
||||
/// <param name="bval">
|
||||
/// the byte value.
|
||||
/// </param>
|
||||
public override void WriteByte(byte bval)
|
||||
{
|
||||
byte[] b = new byte[1];
|
||||
b[0] = (byte) bval;
|
||||
Write(b, 0, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a len bytes from an array to the compressed stream.
|
||||
/// </summary>
|
||||
/// <param name="buf">
|
||||
/// the byte array.
|
||||
/// </param>
|
||||
/// <param name="off">
|
||||
/// the offset into the byte array where to start.
|
||||
/// </param>
|
||||
/// <param name="len">
|
||||
/// the number of bytes to write.
|
||||
/// </param>
|
||||
public override void Write(byte[] buf, int off, int len)
|
||||
{
|
||||
// System.err.println("DOS with off " + off + " and len " + len);
|
||||
def.SetInput(buf, off, len);
|
||||
deflate();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+333
@@ -0,0 +1,333 @@
|
||||
// InflaterInputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams {
|
||||
|
||||
/// <summary>
|
||||
/// This filter stream is used to decompress data compressed baseInputStream the "deflate"
|
||||
/// format. The "deflate" format is described baseInputStream RFC 1951.
|
||||
///
|
||||
/// This stream may form the basis for other decompression filters, such
|
||||
/// as the <code>GzipInputStream</code>.
|
||||
///
|
||||
/// author of the original java version : John Leuner
|
||||
/// </summary>
|
||||
public class InflaterInputStream : Stream
|
||||
{
|
||||
//Variables
|
||||
|
||||
/// <summary>
|
||||
/// Decompressor for this filter
|
||||
/// </summary>
|
||||
protected Inflater inf;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Byte array used as a buffer
|
||||
/// </summary>
|
||||
protected byte[] buf;
|
||||
|
||||
/// <summary>
|
||||
/// Size of buffer
|
||||
/// </summary>
|
||||
protected int len;
|
||||
|
||||
//We just use this if we are decoding one byte at a time with the read() call
|
||||
private byte[] onebytebuffer = new byte[1];
|
||||
|
||||
/// <summary>
|
||||
/// base stream the inflater depends on.
|
||||
/// </summary>
|
||||
protected Stream baseInputStream;
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanRead {
|
||||
get {
|
||||
return baseInputStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanSeek {
|
||||
get {
|
||||
return baseInputStream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override bool CanWrite {
|
||||
get {
|
||||
return baseInputStream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Length {
|
||||
get {
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Position {
|
||||
get {
|
||||
return baseInputStream.Position;
|
||||
}
|
||||
set {
|
||||
baseInputStream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the baseInputStream
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
baseInputStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return baseInputStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void SetLength(long val)
|
||||
{
|
||||
baseInputStream.SetLength(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void Write(byte[] array, int offset, int count)
|
||||
{
|
||||
baseInputStream.Write(array, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I needed to implement the abstract member.
|
||||
/// </summary>
|
||||
public override void WriteByte(byte val)
|
||||
{
|
||||
baseInputStream.WriteByte(val);
|
||||
}
|
||||
|
||||
//Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create an InflaterInputStream with the default decompresseor
|
||||
/// and a default buffer size.
|
||||
/// </summary>
|
||||
/// <param name = "baseInputStream">
|
||||
/// the InputStream to read bytes from
|
||||
/// </param>
|
||||
public InflaterInputStream(Stream baseInputStream) : this(baseInputStream, new Inflater(), 4096)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an InflaterInputStream with the specified decompresseor
|
||||
/// and a default buffer size.
|
||||
/// </summary>
|
||||
/// <param name = "baseInputStream">
|
||||
/// the InputStream to read bytes from
|
||||
/// </param>
|
||||
/// <param name = "inf">
|
||||
/// the decompressor used to decompress data read from baseInputStream
|
||||
/// </param>
|
||||
public InflaterInputStream(Stream baseInputStream, Inflater inf) : this(baseInputStream, inf, 4096)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an InflaterInputStream with the specified decompresseor
|
||||
/// and a specified buffer size.
|
||||
/// </summary>
|
||||
/// <param name = "baseInputStream">
|
||||
/// the InputStream to read bytes from
|
||||
/// </param>
|
||||
/// <param name = "inf">
|
||||
/// the decompressor used to decompress data read from baseInputStream
|
||||
/// </param>
|
||||
/// <param name = "size">
|
||||
/// size of the buffer to use
|
||||
/// </param>
|
||||
public InflaterInputStream(Stream baseInputStream, Inflater inf, int size)
|
||||
{
|
||||
this.baseInputStream = baseInputStream;
|
||||
this.inf = inf;
|
||||
try {
|
||||
this.len = (int)baseInputStream.Length;
|
||||
} catch (Exception) { // the stream may not support .Length
|
||||
this.len = 0;
|
||||
}
|
||||
|
||||
if (size <= 0) {
|
||||
throw new ArgumentOutOfRangeException("size <= 0");
|
||||
}
|
||||
|
||||
buf = new byte[size]; //Create the buffer
|
||||
}
|
||||
|
||||
//Methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns 0 once the end of the stream (EOF) has been reached.
|
||||
/// Otherwise returns 1.
|
||||
/// </summary>
|
||||
public virtual int Available {
|
||||
get {
|
||||
return inf.IsFinished ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the input stream
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
baseInputStream.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills the buffer with more data to decompress.
|
||||
/// </summary>
|
||||
protected void Fill()
|
||||
{
|
||||
len = baseInputStream.Read(buf, 0, buf.Length);
|
||||
|
||||
if (len < 0) {
|
||||
throw new ApplicationException("Deflated stream ends early.");
|
||||
}
|
||||
inf.SetInput(buf, 0, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one byte of decompressed data.
|
||||
///
|
||||
/// The byte is baseInputStream the lower 8 bits of the int.
|
||||
/// </summary>
|
||||
public override int ReadByte()
|
||||
{
|
||||
int nread = Read(onebytebuffer, 0, 1); //read one byte
|
||||
if (nread > 0) {
|
||||
return onebytebuffer[0] & 0xff;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompresses data into the byte array
|
||||
/// </summary>
|
||||
/// <param name ="b">
|
||||
/// the array to read and decompress data into
|
||||
/// </param>
|
||||
/// <param name ="off">
|
||||
/// the offset indicating where the data should be placed
|
||||
/// </param>
|
||||
/// <param name ="len">
|
||||
/// the number of bytes to decompress
|
||||
/// </param>
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
for (;;) {
|
||||
int count;
|
||||
try {
|
||||
count = inf.Inflate(b, off, len);
|
||||
} catch (Exception e) {
|
||||
throw new ZipException(e.ToString());
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
return count;
|
||||
}
|
||||
|
||||
if (inf.IsNeedingDictionary) {
|
||||
throw new ZipException("Need a dictionary");
|
||||
} else if (inf.IsFinished) {
|
||||
return 0;
|
||||
} else if (inf.IsNeedingInput) {
|
||||
Fill();
|
||||
} else {
|
||||
throw new InvalidOperationException("Don't know what to do");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip specified number of bytes of uncompressed data
|
||||
/// </summary>
|
||||
/// <param name ="n">
|
||||
/// number of bytes to skip
|
||||
/// </param>
|
||||
public long Skip(long n)
|
||||
{
|
||||
if (n < 0) {
|
||||
throw new ArgumentOutOfRangeException("n");
|
||||
}
|
||||
int len = 2048;
|
||||
if (n < len) {
|
||||
len = (int) n;
|
||||
}
|
||||
byte[] tmp = new byte[len];
|
||||
return (long)baseInputStream.Read(tmp, 0, tmp.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
// OutputWindow.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams {
|
||||
|
||||
/// <summary>
|
||||
/// Contains the output from the Inflation process.
|
||||
/// We need to have a window so that we can refer backwards into the output stream
|
||||
/// to repeat stuff.
|
||||
///
|
||||
/// author of the original java version : John Leuner
|
||||
/// </summary>
|
||||
public class OutputWindow
|
||||
{
|
||||
private static int WINDOW_SIZE = 1 << 15;
|
||||
private static int WINDOW_MASK = WINDOW_SIZE - 1;
|
||||
|
||||
private byte[] window = new byte[WINDOW_SIZE]; //The window is 2^15 bytes
|
||||
private int window_end = 0;
|
||||
private int window_filled = 0;
|
||||
|
||||
public void Write(int abyte)
|
||||
{
|
||||
if (window_filled++ == WINDOW_SIZE) {
|
||||
throw new InvalidOperationException("Window full");
|
||||
}
|
||||
window[window_end++] = (byte) abyte;
|
||||
window_end &= WINDOW_MASK;
|
||||
}
|
||||
|
||||
|
||||
private void SlowRepeat(int rep_start, int len, int dist)
|
||||
{
|
||||
while (len-- > 0) {
|
||||
window[window_end++] = window[rep_start++];
|
||||
window_end &= WINDOW_MASK;
|
||||
rep_start &= WINDOW_MASK;
|
||||
}
|
||||
}
|
||||
|
||||
public void Repeat(int len, int dist)
|
||||
{
|
||||
if ((window_filled += len) > WINDOW_SIZE) {
|
||||
throw new InvalidOperationException("Window full");
|
||||
}
|
||||
|
||||
int rep_start = (window_end - dist) & WINDOW_MASK;
|
||||
int border = WINDOW_SIZE - len;
|
||||
if (rep_start <= border && window_end < border) {
|
||||
if (len <= dist) {
|
||||
System.Array.Copy(window, rep_start, window, window_end, len);
|
||||
window_end += len;
|
||||
} else {
|
||||
/* We have to copy manually, since the repeat pattern overlaps.
|
||||
*/
|
||||
while (len-- > 0) {
|
||||
window[window_end++] = window[rep_start++];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SlowRepeat(rep_start, len, dist);
|
||||
}
|
||||
}
|
||||
|
||||
public int CopyStored(StreamManipulator input, int len)
|
||||
{
|
||||
len = Math.Min(Math.Min(len, WINDOW_SIZE - window_filled), input.AvailableBytes);
|
||||
int copied;
|
||||
|
||||
int tailLen = WINDOW_SIZE - window_end;
|
||||
if (len > tailLen) {
|
||||
copied = input.CopyBytes(window, window_end, tailLen);
|
||||
if (copied == tailLen) {
|
||||
copied += input.CopyBytes(window, 0, len - tailLen);
|
||||
}
|
||||
} else {
|
||||
copied = input.CopyBytes(window, window_end, len);
|
||||
}
|
||||
|
||||
window_end = (window_end + copied) & WINDOW_MASK;
|
||||
window_filled += copied;
|
||||
return copied;
|
||||
}
|
||||
|
||||
public void CopyDict(byte[] dict, int offset, int len)
|
||||
{
|
||||
if (window_filled > 0) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
if (len > WINDOW_SIZE) {
|
||||
offset += len - WINDOW_SIZE;
|
||||
len = WINDOW_SIZE;
|
||||
}
|
||||
System.Array.Copy(dict, offset, window, 0, len);
|
||||
window_end = len & WINDOW_MASK;
|
||||
}
|
||||
|
||||
public int GetFreeSpace()
|
||||
{
|
||||
return WINDOW_SIZE - window_filled;
|
||||
}
|
||||
|
||||
public int GetAvailable()
|
||||
{
|
||||
return window_filled;
|
||||
}
|
||||
|
||||
public int CopyOutput(byte[] output, int offset, int len)
|
||||
{
|
||||
int copy_end = window_end;
|
||||
if (len > window_filled) {
|
||||
len = window_filled;
|
||||
} else {
|
||||
copy_end = (window_end - window_filled + len) & WINDOW_MASK;
|
||||
}
|
||||
|
||||
int copied = len;
|
||||
int tailLen = len - copy_end;
|
||||
|
||||
if (tailLen > 0) {
|
||||
System.Array.Copy(window, WINDOW_SIZE - tailLen,
|
||||
output, offset, tailLen);
|
||||
offset += tailLen;
|
||||
len = copy_end;
|
||||
}
|
||||
System.Array.Copy(window, copy_end - len, output, offset, len);
|
||||
window_filled -= copied;
|
||||
if (window_filled < 0) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return copied;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
window_filled = window_end = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
// StreamManipulator.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams {
|
||||
|
||||
/// <summary>
|
||||
/// This class allows us to retrieve a specified amount of bits from
|
||||
/// the input buffer, as well as copy big byte blocks.
|
||||
///
|
||||
/// It uses an int buffer to store up to 31 bits for direct
|
||||
/// manipulation. This guarantees that we can get at least 16 bits,
|
||||
/// but we only need at most 15, so this is all safe.
|
||||
///
|
||||
/// There are some optimizations in this class, for example, you must
|
||||
/// never peek more then 8 bits more than needed, and you must first
|
||||
/// peek bits before you may drop them. This is not a general purpose
|
||||
/// class but optimized for the behaviour of the Inflater.
|
||||
///
|
||||
/// authors of the original java version : John Leuner, Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class StreamManipulator
|
||||
{
|
||||
private byte[] window;
|
||||
private int window_start = 0;
|
||||
private int window_end = 0;
|
||||
|
||||
private uint buffer = 0;
|
||||
private int bits_in_buffer = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Get the next n bits but don't increase input pointer. n must be
|
||||
/// less or equal 16 and if you if this call succeeds, you must drop
|
||||
/// at least n-8 bits in the next call.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the value of the bits, or -1 if not enough bits available. */
|
||||
/// </returns>
|
||||
public int PeekBits(int n)
|
||||
{
|
||||
if (bits_in_buffer < n) {
|
||||
if (window_start == window_end) {
|
||||
return -1;
|
||||
}
|
||||
buffer |= (uint)((window[window_start++] & 0xff |
|
||||
(window[window_start++] & 0xff) << 8) << bits_in_buffer);
|
||||
bits_in_buffer += 16;
|
||||
}
|
||||
return (int)(buffer & ((1 << n) - 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops the next n bits from the input. You should have called peekBits
|
||||
/// with a bigger or equal n before, to make sure that enough bits are in
|
||||
/// the bit buffer.
|
||||
/// </summary>
|
||||
public void DropBits(int n)
|
||||
{
|
||||
buffer >>= n;
|
||||
bits_in_buffer -= n;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the next n bits and increases input pointer. This is equivalent
|
||||
/// to peekBits followed by dropBits, except for correct error handling.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the value of the bits, or -1 if not enough bits available.
|
||||
/// </returns>
|
||||
public int GetBits(int n)
|
||||
{
|
||||
int bits = PeekBits(n);
|
||||
if (bits >= 0) {
|
||||
DropBits(n);
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of bits available in the bit buffer. This must be
|
||||
/// only called when a previous peekBits() returned -1.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the number of bits available.
|
||||
/// </returns>
|
||||
public int AvailableBits {
|
||||
get {
|
||||
return bits_in_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of bytes available.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the number of bytes available.
|
||||
/// </returns>
|
||||
public int AvailableBytes {
|
||||
get {
|
||||
return window_end - window_start + (bits_in_buffer >> 3);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skips to the next byte boundary.
|
||||
/// </summary>
|
||||
public void SkipToByteBoundary()
|
||||
{
|
||||
buffer >>= (bits_in_buffer & 7);
|
||||
bits_in_buffer &= ~7;
|
||||
}
|
||||
|
||||
public bool IsNeedingInput {
|
||||
get {
|
||||
return window_start == window_end;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies length bytes from input buffer to output buffer starting
|
||||
/// at output[offset]. You have to make sure, that the buffer is
|
||||
/// byte aligned. If not enough bytes are available, copies fewer
|
||||
/// bytes.
|
||||
/// </summary>
|
||||
/// <param name="output">
|
||||
/// the buffer.
|
||||
/// </param>
|
||||
/// <param name="offset">
|
||||
/// the offset in the buffer.
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// the length to copy, 0 is allowed.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// the number of bytes copied, 0 if no byte is available.
|
||||
/// </returns>
|
||||
public int CopyBytes(byte[] output, int offset, int length)
|
||||
{
|
||||
if (length < 0) {
|
||||
throw new ArgumentOutOfRangeException("length negative");
|
||||
}
|
||||
if ((bits_in_buffer & 7) != 0) {
|
||||
/* bits_in_buffer may only be 0 or 8 */
|
||||
throw new InvalidOperationException("Bit buffer is not aligned!");
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
while (bits_in_buffer > 0 && length > 0) {
|
||||
output[offset++] = (byte) buffer;
|
||||
buffer >>= 8;
|
||||
bits_in_buffer -= 8;
|
||||
length--;
|
||||
count++;
|
||||
}
|
||||
if (length == 0) {
|
||||
return count;
|
||||
}
|
||||
|
||||
int avail = window_end - window_start;
|
||||
if (length > avail) {
|
||||
length = avail;
|
||||
}
|
||||
System.Array.Copy(window, window_start, output, offset, length);
|
||||
window_start += length;
|
||||
|
||||
if (((window_start - window_end) & 1) != 0) {
|
||||
/* We always want an even number of bytes in input, see peekBits */
|
||||
buffer = (uint)(window[window_start++] & 0xff);
|
||||
bits_in_buffer = 8;
|
||||
}
|
||||
return count + length;
|
||||
}
|
||||
|
||||
public StreamManipulator()
|
||||
{
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
buffer = (uint)(window_start = window_end = bits_in_buffer = 0);
|
||||
}
|
||||
|
||||
public void SetInput(byte[] buf, int off, int len)
|
||||
{
|
||||
if (window_start < window_end) {
|
||||
throw new InvalidOperationException("Old input was not completely processed");
|
||||
}
|
||||
|
||||
int end = off + len;
|
||||
|
||||
/* We want to throw an ArrayIndexOutOfBoundsException early. The
|
||||
* check is very tricky: it also handles integer wrap around.
|
||||
*/
|
||||
if (0 > off || off > end || end > buf.Length) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
if ((len & 1) != 0) {
|
||||
/* We always want an even number of bytes in input, see peekBits */
|
||||
buffer |= (uint)((buf[off++] & 0xff) << bits_in_buffer);
|
||||
bits_in_buffer += 8;
|
||||
}
|
||||
|
||||
window = buf;
|
||||
window_start = off;
|
||||
window_end = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
// ZipConstants.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip {
|
||||
|
||||
/// <summary>
|
||||
/// This class contains constants used for zip.
|
||||
/// </summary>
|
||||
public sealed class ZipConstants
|
||||
{
|
||||
/* The local file header */
|
||||
public const int LOCHDR = 30;
|
||||
public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24);
|
||||
|
||||
public const int LOCVER = 4;
|
||||
public const int LOCFLG = 6;
|
||||
public const int LOCHOW = 8;
|
||||
public const int LOCTIM = 10;
|
||||
public const int LOCCRC = 14;
|
||||
public const int LOCSIZ = 18;
|
||||
public const int LOCLEN = 22;
|
||||
public const int LOCNAM = 26;
|
||||
public const int LOCEXT = 28;
|
||||
|
||||
/* The Data descriptor */
|
||||
public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
|
||||
public const int EXTHDR = 16;
|
||||
|
||||
public const int EXTCRC = 4;
|
||||
public const int EXTSIZ = 8;
|
||||
public const int EXTLEN = 12;
|
||||
|
||||
/* The central directory file header */
|
||||
public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24);
|
||||
|
||||
/* The central directory file header for 64bit ZIP*/
|
||||
public const int CENSIG64 = 0x06064b50;
|
||||
|
||||
public const int CENHDR = 46;
|
||||
|
||||
public const int CENVEM = 4;
|
||||
public const int CENVER = 6;
|
||||
public const int CENFLG = 8;
|
||||
public const int CENHOW = 10;
|
||||
public const int CENTIM = 12;
|
||||
public const int CENCRC = 16;
|
||||
public const int CENSIZ = 20;
|
||||
public const int CENLEN = 24;
|
||||
public const int CENNAM = 28;
|
||||
public const int CENEXT = 30;
|
||||
public const int CENCOM = 32;
|
||||
public const int CENDSK = 34;
|
||||
public const int CENATT = 36;
|
||||
public const int CENATX = 38;
|
||||
public const int CENOFF = 42;
|
||||
|
||||
/* The entries in the end of central directory */
|
||||
public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24);
|
||||
public const int ENDHDR = 22;
|
||||
|
||||
/* The following two fields are missing in SUN JDK */
|
||||
public const int ENDNRD = 4;
|
||||
public const int ENDDCD = 6;
|
||||
|
||||
public const int ENDSUB = 8;
|
||||
public const int ENDTOT = 10;
|
||||
public const int ENDSIZ = 12;
|
||||
public const int ENDOFF = 16;
|
||||
public const int ENDCOM = 20;
|
||||
|
||||
/* Using the codepage 1252 doesn't solve the 8bit ASCII problem :/
|
||||
any help would be appreciated.
|
||||
|
||||
// get encoding for latin characters (like ö, ü, ß or ô)
|
||||
static Encoding ecp1252 = Encoding.GetEncoding(1252);
|
||||
*/
|
||||
public static string ConvertToString(byte[] data)
|
||||
{
|
||||
|
||||
return Encoding.ASCII.GetString(data);
|
||||
}
|
||||
|
||||
public static byte[] ConvertToArray(string str)
|
||||
{
|
||||
return Encoding.ASCII.GetBytes(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
// ZipEntry.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip {
|
||||
|
||||
public enum CompressionMethod
|
||||
{
|
||||
Stored = 0,
|
||||
Deflated = 8,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This class represents a member of a zip archive. ZipFile and
|
||||
/// ZipInputStream will give you instances of this class as information
|
||||
/// about the members in an archive. On the other hand ZipOutputStream
|
||||
/// needs an instance of this class to create a new member.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
public class ZipEntry : ICloneable
|
||||
{
|
||||
static int KNOWN_SIZE = 1;
|
||||
static int KNOWN_CSIZE = 2;
|
||||
static int KNOWN_CRC = 4;
|
||||
static int KNOWN_TIME = 8;
|
||||
|
||||
DateTime cal = DateTime.Now;
|
||||
|
||||
string name;
|
||||
uint size;
|
||||
ushort version;
|
||||
uint compressedSize;
|
||||
int crc;
|
||||
|
||||
ushort known = 0;
|
||||
CompressionMethod method = CompressionMethod.Deflated;
|
||||
byte[] extra = null;
|
||||
string comment = null;
|
||||
|
||||
public int zipFileIndex = -1; /* used by ZipFile */
|
||||
public int flags; /* used by ZipOutputStream */
|
||||
public int offset; /* used by ZipFile and ZipOutputStream */
|
||||
|
||||
/// <summary>
|
||||
/// Creates a zip entry with the given name.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// the name. May include directory components separated by '/'.
|
||||
/// </param>
|
||||
public ZipEntry(string name)
|
||||
{
|
||||
if (name == null) {
|
||||
throw new System.ArgumentNullException("name");
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of the given zip entry.
|
||||
/// </summary>
|
||||
/// <param name="e">
|
||||
/// the entry to copy.
|
||||
/// </param>
|
||||
public ZipEntry(ZipEntry e)
|
||||
{
|
||||
name = e.name;
|
||||
known = e.known;
|
||||
size = e.size;
|
||||
compressedSize = e.compressedSize;
|
||||
crc = e.crc;
|
||||
// time = e.time;
|
||||
method = e.method;
|
||||
extra = e.extra;
|
||||
comment = e.comment;
|
||||
}
|
||||
|
||||
public ushort Version {
|
||||
get {
|
||||
return version;
|
||||
}
|
||||
set {
|
||||
version = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int DosTime {
|
||||
get {
|
||||
// if ((known & KNOWN_TIME) == 0) {
|
||||
// return 0;
|
||||
// }
|
||||
lock (this) {
|
||||
return (cal.Year - 1980 & 0x7f) << 25 |
|
||||
(cal.Month + 1) << 21 |
|
||||
(cal.Day ) << 16 |
|
||||
(cal.Hour) << 11 |
|
||||
(cal.Minute) << 5 |
|
||||
(cal.Second) >> 1;
|
||||
}
|
||||
}
|
||||
set {
|
||||
// Guard against invalid or missing date causing
|
||||
// IndexOutOfBoundsException.
|
||||
try {
|
||||
lock (this) {
|
||||
cal = CalculateDateTime(value);
|
||||
// time = (int) (cal.Millisecond / 1000L);
|
||||
}
|
||||
known |= (ushort)KNOWN_TIME;
|
||||
} catch (Exception) {
|
||||
/* Ignore illegal time stamp */
|
||||
known &= (ushort)~KNOWN_TIME;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the time of last modification of the entry.
|
||||
/// </summary>
|
||||
public DateTime DateTime {
|
||||
get {
|
||||
return cal;
|
||||
}
|
||||
set {
|
||||
lock (this) {
|
||||
cal = value;
|
||||
// time = (int) (cal.Millisecond / 1000L);
|
||||
}
|
||||
known |= (ushort)KNOWN_TIME;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the entry name. The path components in the entry are
|
||||
/// always separated by slashes ('/').
|
||||
/// </summary>
|
||||
public string Name {
|
||||
get {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
// /// <summary>
|
||||
// /// Gets/Sets the time of last modification of the entry.
|
||||
// /// </summary>
|
||||
// /// <returns>
|
||||
// /// the time of last modification of the entry, or -1 if unknown.
|
||||
// /// </returns>
|
||||
// public long Time {
|
||||
// get {
|
||||
// return (known & KNOWN_TIME) != 0 ? time * 1000L : -1;
|
||||
// }
|
||||
// set {
|
||||
// this.time = (int) (value / 1000L);
|
||||
// this.known |= (ushort)KNOWN_TIME;
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the size of the uncompressed data.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if size is not in 0..0xffffffffL
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the size or -1 if unknown.
|
||||
/// </returns>
|
||||
public long Size {
|
||||
get {
|
||||
return (known & KNOWN_SIZE) != 0 ? (long)size : -1L;
|
||||
}
|
||||
set {
|
||||
if (((ulong)value & 0xFFFFFFFF00000000L) != 0) {
|
||||
throw new ArgumentOutOfRangeException("size");
|
||||
}
|
||||
this.size = (uint)value;
|
||||
this.known |= (ushort)KNOWN_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the size of the compressed data.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if csize is not in 0..0xffffffffL
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the size or -1 if unknown.
|
||||
/// </returns>
|
||||
public long CompressedSize {
|
||||
get {
|
||||
return (known & KNOWN_CSIZE) != 0 ? (long)compressedSize : -1L;
|
||||
}
|
||||
set {
|
||||
if (((ulong)value & 0xffffffff00000000L) != 0) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
this.compressedSize = (uint)value;
|
||||
this.known |= (ushort)KNOWN_CSIZE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the crc of the uncompressed data.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if crc is not in 0..0xffffffffL
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the crc or -1 if unknown.
|
||||
/// </returns>
|
||||
public long Crc {
|
||||
get {
|
||||
return (known & KNOWN_CRC) != 0 ? crc & 0xffffffffL : -1L;
|
||||
}
|
||||
set {
|
||||
if (((ulong)crc & 0xffffffff00000000L) != 0) {
|
||||
throw new Exception();
|
||||
}
|
||||
this.crc = (int)value;
|
||||
this.known |= (ushort)KNOWN_CRC;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the compression method. Only DEFLATED and STORED are supported.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if method is not supported.
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the compression method or -1 if unknown.
|
||||
/// </returns>
|
||||
/// <see cref="ZipOutputStream.DEFLATED"/>
|
||||
/// <see cref="ZipOutputStream.STORED"/>
|
||||
public CompressionMethod CompressionMethod {
|
||||
get {
|
||||
return method;
|
||||
}
|
||||
set {
|
||||
this.method = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the extra data.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if extra is longer than 0xffff bytes.
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the extra data or null if not set.
|
||||
/// </returns>
|
||||
public byte[] ExtraData {
|
||||
get {
|
||||
return extra;
|
||||
}
|
||||
set {
|
||||
if (value == null) {
|
||||
this.extra = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.Length > 0xffff) {
|
||||
throw new System.ArgumentOutOfRangeException();
|
||||
}
|
||||
this.extra = value;
|
||||
try {
|
||||
int pos = 0;
|
||||
while (pos < extra.Length) {
|
||||
int sig = (extra[pos++] & 0xff) | (extra[pos++] & 0xff) << 8;
|
||||
int len = (extra[pos++] & 0xff) | (extra[pos++] & 0xff) << 8;
|
||||
if (sig == 0x5455) {
|
||||
/* extended time stamp, unix format by Rainer Prem <[email protected]> */
|
||||
int flags = extra[pos];
|
||||
if ((flags & 1) != 0) {
|
||||
int iTime = ((extra[pos+1] & 0xff) |
|
||||
(extra[pos+2] & 0xff) << 8 |
|
||||
(extra[pos+3] & 0xff) << 16 |
|
||||
(extra[pos+4] & 0xff) << 24);
|
||||
|
||||
cal = (new DateTime ( 1970, 1, 1, 0, 0, 0 ) +
|
||||
new TimeSpan ( 0, 0, 0, iTime, 0 )).ToLocalTime ();
|
||||
known |= (ushort)KNOWN_TIME;
|
||||
}
|
||||
}
|
||||
pos += len;
|
||||
}
|
||||
} catch (Exception) {
|
||||
/* be lenient */
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the entry comment.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if comment is longer than 0xffff.
|
||||
/// </exception>
|
||||
/// <returns>
|
||||
/// the comment or null if not set.
|
||||
/// </returns>
|
||||
public string Comment {
|
||||
get {
|
||||
return comment;
|
||||
}
|
||||
set {
|
||||
if (value.Length > 0xffff) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
this.comment = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets true, if the entry is a directory. This is solely
|
||||
/// determined by the name, a trailing slash '/' marks a directory.
|
||||
/// </summary>
|
||||
public bool IsDirectory {
|
||||
get {
|
||||
int nlen = name.Length;
|
||||
return nlen > 0 && name[nlen - 1] == '/';
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of this zip entry.
|
||||
/// </summary>
|
||||
public object Clone()
|
||||
{
|
||||
return this.MemberwiseClone();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string representation of this ZipEntry. This is just
|
||||
/// the name as returned by getName().
|
||||
/// </summary>
|
||||
public override string ToString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
DateTime CalculateDateTime(int dosTime)
|
||||
{
|
||||
int sec = 2 * (dosTime & 0x1f);
|
||||
int min = (dosTime >> 5) & 0x3f;
|
||||
int hrs = (dosTime >> 11) & 0x1f;
|
||||
int day = (dosTime >> 16) & 0x1f;
|
||||
int mon = ((dosTime >> 21) & 0xf);
|
||||
int year = ((dosTime >> 25) & 0x7f) + 1980; /* since 1900 */
|
||||
|
||||
return new DateTime(year, mon, day, hrs, min, sec);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,521 @@
|
||||
// ZipFile.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents a Zip archive. You can ask for the contained
|
||||
/// entries, or get an input stream for a file entry. The entry is
|
||||
/// automatically decompressed.
|
||||
///
|
||||
/// This class is thread safe: You can open input streams for arbitrary
|
||||
/// entries in different threads.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// using System;
|
||||
/// using System.Text;
|
||||
/// using System.Collections;
|
||||
/// using System.IO;
|
||||
///
|
||||
/// using NZlib.Zip;
|
||||
///
|
||||
/// class MainClass
|
||||
/// {
|
||||
/// static public void Main(string[] args)
|
||||
/// {
|
||||
/// ZipFile zFile = new ZipFile(args[0]);
|
||||
/// Console.WriteLine("Listing of : " + zFile.Name);
|
||||
/// Console.WriteLine("");
|
||||
/// Console.WriteLine("Raw Size Size Date Time Name");
|
||||
/// Console.WriteLine("-------- -------- -------- ------ ---------");
|
||||
/// foreach (ZipEntry e in zFile) {
|
||||
/// DateTime d = e.DateTime;
|
||||
/// Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
|
||||
/// d.ToString("dd-MM-yy"), d.ToString("t"),
|
||||
/// e.Name);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </example>
|
||||
public class ZipFile : IEnumerable
|
||||
{
|
||||
string name;
|
||||
Stream baseStream;
|
||||
ZipEntry[] entries;
|
||||
|
||||
/// <summary>
|
||||
/// Opens a Zip file with the given name for reading.
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// IOException if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the file doesn't contain a valid zip archive.
|
||||
/// </exception>
|
||||
public ZipFile(string name) : this(File.OpenRead(name))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a Zip file reading the given FileStream
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// IOException if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the file doesn't contain a valid zip archive.
|
||||
/// </exception>
|
||||
public ZipFile(FileStream file)
|
||||
{
|
||||
this.baseStream = file;
|
||||
this.name = file.Name;
|
||||
ReadEntries();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a Zip file reading the given Stream
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// IOException if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the file doesn't contain a valid zip archive.
|
||||
/// </exception>
|
||||
public ZipFile(Stream baseStream)
|
||||
{
|
||||
this.baseStream = baseStream;
|
||||
this.name = null;
|
||||
ReadEntries();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Read an unsigned short in little endian byte order.
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="System.IO.EndOfStreamException">
|
||||
/// if the file ends prematurely
|
||||
/// </exception>
|
||||
int ReadLeShort()
|
||||
{
|
||||
return baseStream.ReadByte() | baseStream.ReadByte() << 8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an int in little endian byte order.
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="System.IO.EndOfStreamException">
|
||||
/// if the file ends prematurely
|
||||
/// </exception>
|
||||
int ReadLeInt()
|
||||
{
|
||||
return ReadLeShort() | ReadLeShort() << 16;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the central directory of a zip file and fill the entries
|
||||
/// array. This is called exactly once by the constructors.
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the central directory is malformed
|
||||
/// </exception>
|
||||
void ReadEntries()
|
||||
{
|
||||
/* Search for the End Of Central Directory. When a zip comment is
|
||||
* present the directory may start earlier.
|
||||
* FIXME: This searches the whole file in a very slow manner if the
|
||||
* file isn't a zip file.
|
||||
*/
|
||||
long pos = baseStream.Length - ZipConstants.ENDHDR;
|
||||
do {
|
||||
if (pos < 0) {
|
||||
throw new ZipException("central directory not found, probably not a zip file");
|
||||
}
|
||||
baseStream.Seek(pos--, SeekOrigin.Begin);
|
||||
} while (ReadLeInt() != ZipConstants.ENDSIG);
|
||||
|
||||
long oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.ENDTOT - ZipConstants.ENDNRD;
|
||||
|
||||
if (baseStream.Position - oldPos != ZipConstants.ENDTOT - ZipConstants.ENDNRD) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
int count = ReadLeShort();
|
||||
|
||||
oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.ENDOFF - ZipConstants.ENDSIZ;
|
||||
|
||||
if (baseStream.Position - oldPos != ZipConstants.ENDOFF - ZipConstants.ENDSIZ) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
|
||||
int centralOffset = ReadLeInt();
|
||||
|
||||
entries = new ZipEntry[count];
|
||||
baseStream.Seek(centralOffset, SeekOrigin.Begin);
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (ReadLeInt() != ZipConstants.CENSIG) {
|
||||
throw new ZipException("Wrong Central Directory signature");
|
||||
}
|
||||
|
||||
oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.CENHOW - ZipConstants.CENVEM;
|
||||
|
||||
if (baseStream.Position - oldPos != ZipConstants.CENHOW - ZipConstants.CENVEM) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
int method = ReadLeShort();
|
||||
int dostime = ReadLeInt();
|
||||
int crc = ReadLeInt();
|
||||
int csize = ReadLeInt();
|
||||
int size = ReadLeInt();
|
||||
int nameLen = ReadLeShort();
|
||||
int extraLen = ReadLeShort();
|
||||
int commentLen = ReadLeShort();
|
||||
|
||||
oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.CENOFF - ZipConstants.CENDSK;
|
||||
if (baseStream.Position - oldPos != ZipConstants.CENOFF - ZipConstants.CENDSK) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
int offset = ReadLeInt();
|
||||
|
||||
byte[] buffer = new byte[Math.Max(nameLen, commentLen)];
|
||||
|
||||
baseStream.Read(buffer, 0, nameLen);
|
||||
string name = ZipConstants.ConvertToString(buffer);
|
||||
|
||||
ZipEntry entry = new ZipEntry(name);
|
||||
entry.CompressionMethod = (CompressionMethod)method;
|
||||
entry.Crc = crc & 0xffffffffL;
|
||||
entry.Size = size & 0xffffffffL;
|
||||
entry.CompressedSize = csize & 0xffffffffL;
|
||||
entry.DosTime = dostime;
|
||||
if (extraLen > 0) {
|
||||
byte[] extra = new byte[extraLen];
|
||||
baseStream.Read(extra, 0, extraLen);
|
||||
entry.ExtraData = extra;
|
||||
}
|
||||
if (commentLen > 0) {
|
||||
baseStream.Read(buffer, 0, commentLen);
|
||||
entry.Comment = ZipConstants.ConvertToString(buffer);
|
||||
}
|
||||
entry.zipFileIndex = i;
|
||||
entry.offset = offset;
|
||||
entries[i] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the ZipFile. This also closes all input streams given by
|
||||
/// this class. After this is called, no further method should be
|
||||
/// called.
|
||||
/// </summary>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
public void Close()
|
||||
{
|
||||
entries = null;
|
||||
lock(baseStream) {
|
||||
baseStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an IEnumerator of all Zip entries in this Zip file.
|
||||
/// </summary>
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
if (entries == null) {
|
||||
throw new InvalidOperationException("ZipFile has closed");
|
||||
}
|
||||
|
||||
return new ZipEntryEnumeration(entries);
|
||||
}
|
||||
|
||||
int GetEntryIndex(string name)
|
||||
{
|
||||
for (int i = 0; i < entries.Length; i++) {
|
||||
if (name.Equals(entries[i].Name)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches for a zip entry in this archive with the given name.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// the name. May contain directory components separated by slashes ('/').
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// the zip entry, or null if no entry with that name exists.
|
||||
/// </returns>
|
||||
public ZipEntry GetEntry(string name)
|
||||
{
|
||||
if (entries == null) {
|
||||
throw new InvalidOperationException("ZipFile has closed");
|
||||
}
|
||||
int index = GetEntryIndex(name);
|
||||
return index >= 0 ? (ZipEntry) entries[index].Clone() : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks, if the local header of the entry at index i matches the
|
||||
/// central directory, and returns the offset to the data.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the start offset of the (compressed) data.
|
||||
/// </returns>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the local header doesn't match the central directory header
|
||||
/// </exception>
|
||||
long CheckLocalHeader(ZipEntry entry)
|
||||
{
|
||||
lock(baseStream) {
|
||||
baseStream.Seek(entry.offset, SeekOrigin.Begin);
|
||||
if (ReadLeInt() != ZipConstants.LOCSIG) {
|
||||
throw new ZipException("Wrong Local header signature");
|
||||
}
|
||||
|
||||
/* skip version and flags */
|
||||
long oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.LOCHOW - ZipConstants.LOCVER;
|
||||
if (baseStream.Position - oldPos != ZipConstants.LOCHOW - ZipConstants.LOCVER) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
|
||||
if (entry.CompressionMethod != (CompressionMethod)ReadLeShort()) {
|
||||
throw new ZipException("Compression method mismatch");
|
||||
}
|
||||
|
||||
/* Skip time, crc, size and csize */
|
||||
oldPos = baseStream.Position;
|
||||
baseStream.Position += ZipConstants.LOCNAM - ZipConstants.LOCTIM;
|
||||
|
||||
if (baseStream.Position - oldPos != ZipConstants.LOCNAM - ZipConstants.LOCTIM) {
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
|
||||
if (entry.Name.Length != ReadLeShort()) {
|
||||
throw new ZipException("file name length mismatch");
|
||||
}
|
||||
|
||||
int extraLen = entry.Name.Length + ReadLeShort();
|
||||
return entry.offset + ZipConstants.LOCHDR + extraLen;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an input stream reading the given zip entry as
|
||||
/// uncompressed data. Normally zip entry should be an entry
|
||||
/// returned by GetEntry().
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the input stream.
|
||||
/// </returns>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// if the Zip archive is malformed.
|
||||
/// </exception>
|
||||
public Stream GetInputStream(ZipEntry entry)
|
||||
{
|
||||
if (entries == null) {
|
||||
throw new InvalidOperationException("ZipFile has closed");
|
||||
}
|
||||
|
||||
int index = entry.zipFileIndex;
|
||||
if (index < 0 || index >= entries.Length || entries[index].Name != entry.Name) {
|
||||
index = GetEntryIndex(entry.Name);
|
||||
if (index < 0) {
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
long start = CheckLocalHeader(entries[index]);
|
||||
CompressionMethod method = entries[index].CompressionMethod;
|
||||
Stream istr = new PartialInputStream(baseStream, start, entries[index].CompressedSize);
|
||||
switch (method) {
|
||||
case CompressionMethod.Stored:
|
||||
return istr;
|
||||
case CompressionMethod.Deflated:
|
||||
return new InflaterInputStream(istr, new Inflater(true));
|
||||
default:
|
||||
throw new ZipException("Unknown compression method " + method);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the name of this zip file.
|
||||
/// </summary>
|
||||
public string Name {
|
||||
get {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of entries in this zip file.
|
||||
/// </summary>
|
||||
public int Size {
|
||||
get {
|
||||
try {
|
||||
return entries.Length;
|
||||
} catch (Exception) {
|
||||
throw new InvalidOperationException("ZipFile has closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ZipEntryEnumeration : IEnumerator
|
||||
{
|
||||
ZipEntry[] array;
|
||||
int ptr = -1;
|
||||
|
||||
public ZipEntryEnumeration(ZipEntry[] arr)
|
||||
{
|
||||
array = arr;
|
||||
}
|
||||
|
||||
public object Current {
|
||||
get {
|
||||
return array[ptr];
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
ptr = -1;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
return (++ptr < array.Length);
|
||||
}
|
||||
}
|
||||
|
||||
class PartialInputStream : InflaterInputStream
|
||||
{
|
||||
Stream baseStream;
|
||||
long filepos, end;
|
||||
|
||||
public PartialInputStream(Stream baseStream, long start, long len) : base(baseStream)
|
||||
{
|
||||
this.baseStream = baseStream;
|
||||
filepos = start;
|
||||
end = start + len;
|
||||
}
|
||||
|
||||
public override int Available {
|
||||
get {
|
||||
long amount = end - filepos;
|
||||
if (amount > Int32.MaxValue) {
|
||||
return Int32.MaxValue;
|
||||
}
|
||||
|
||||
return (int) amount;
|
||||
}
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
if (filepos == end) {
|
||||
return -1;
|
||||
}
|
||||
lock(baseStream) {
|
||||
baseStream.Seek(filepos++, SeekOrigin.Begin);
|
||||
return baseStream.ReadByte();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
if (len > end - filepos) {
|
||||
len = (int) (end - filepos);
|
||||
if (len == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
lock(baseStream) {
|
||||
baseStream.Seek(filepos, SeekOrigin.Begin);
|
||||
int count = baseStream.Read(b, off, len);
|
||||
if (count > 0) {
|
||||
filepos += len;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public long SkipBytes(long amount)
|
||||
{
|
||||
if (amount < 0) {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
if (amount > end - filepos) {
|
||||
amount = end - filepos;
|
||||
}
|
||||
filepos += amount;
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,431 @@
|
||||
// ZipInputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip {
|
||||
|
||||
/// <summary>
|
||||
/// This is a FilterInputStream that reads the files baseInputStream an zip archive
|
||||
/// one after another. It has a special method to get the zip entry of
|
||||
/// the next file. The zip entry contains information about the file name
|
||||
/// size, compressed size, CRC, etc.
|
||||
/// It includes support for STORED and DEFLATED entries.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
/// <example> This sample shows how to read a zip file
|
||||
/// <code>
|
||||
/// using System;
|
||||
/// using System.Text;
|
||||
/// using System.IO;
|
||||
///
|
||||
/// using NZlib.Zip;
|
||||
///
|
||||
/// class MainClass
|
||||
/// {
|
||||
/// public static void Main(string[] args)
|
||||
/// {
|
||||
/// ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
|
||||
///
|
||||
/// ZipEntry theEntry;
|
||||
/// while ((theEntry = s.GetNextEntry()) != null) {
|
||||
/// Console.WriteLine("File " + theEntry.Name);
|
||||
/// int size = 2048;
|
||||
/// byte[] data = new byte[2048];
|
||||
///
|
||||
/// Console.Write("Show contents (y/n) ?");
|
||||
/// if (Console.ReadLine() == "y") {
|
||||
/// while (true) {
|
||||
/// size = s.Read(data, 0, data.Length);
|
||||
/// if (size > 0) {
|
||||
/// Console.Write(new ASCIIEncoding().GetString(data, 0, size));
|
||||
/// } else {
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Console.WriteLine();
|
||||
/// }
|
||||
/// s.Close();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class ZipInputStream : InflaterInputStream
|
||||
{
|
||||
private Crc32 crc = new Crc32();
|
||||
private ZipEntry entry = null;
|
||||
|
||||
private long csize;
|
||||
private long size;
|
||||
private int method;
|
||||
private int flags;
|
||||
private long avail;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Zip input stream, reading a zip archive.
|
||||
/// </summary>
|
||||
public ZipInputStream(Stream baseInputStream) : base(baseInputStream, new Inflater(true))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void FillBuf()
|
||||
{
|
||||
avail = len = baseInputStream.Read(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
private int ReadBuf(byte[] outBuf, int offset, int length)
|
||||
{
|
||||
if (avail <= 0) {
|
||||
FillBuf();
|
||||
if (avail <= 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (length > avail) {
|
||||
length = (int)avail;
|
||||
}
|
||||
System.Array.Copy(buf, len - (int)avail, outBuf, offset, length);
|
||||
avail -= length;
|
||||
return length;
|
||||
}
|
||||
|
||||
private void ReadFully(byte[] outBuf)
|
||||
{
|
||||
int off = 0;
|
||||
int len = outBuf.Length;
|
||||
while (len > 0) {
|
||||
int count = ReadBuf(outBuf, off, len);
|
||||
if (count == -1) {
|
||||
throw new Exception();
|
||||
}
|
||||
off += count;
|
||||
len -= count;
|
||||
}
|
||||
}
|
||||
|
||||
private int ReadLeByte()
|
||||
{
|
||||
if (avail <= 0) {
|
||||
FillBuf();
|
||||
if (avail <= 0) {
|
||||
throw new ZipException("EOF in header");
|
||||
}
|
||||
}
|
||||
return buf[len - avail--] & 0xff;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an unsigned short baseInputStream little endian byte order.
|
||||
/// </summary>
|
||||
private int ReadLeShort()
|
||||
{
|
||||
return ReadLeByte() | (ReadLeByte() << 8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an int baseInputStream little endian byte order.
|
||||
/// </summary>
|
||||
private int ReadLeInt()
|
||||
{
|
||||
return ReadLeShort() | (ReadLeShort() << 16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an int baseInputStream little endian byte order.
|
||||
/// </summary>
|
||||
private long ReadLeLong()
|
||||
{
|
||||
return ReadLeInt() | (ReadLeInt() << 32);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Open the next entry from the zip archive, and return its description.
|
||||
/// If the previous entry wasn't closed, this method will close it.
|
||||
/// </summary>
|
||||
public ZipEntry GetNextEntry()
|
||||
{
|
||||
if (crc == null) {
|
||||
throw new InvalidOperationException("Closed.");
|
||||
}
|
||||
if (entry != null) {
|
||||
CloseEntry();
|
||||
}
|
||||
|
||||
int header = ReadLeInt();
|
||||
if (header == ZipConstants.CENSIG) {
|
||||
/* Central Header reached. */
|
||||
Close();
|
||||
return null;
|
||||
}
|
||||
if (header != ZipConstants.LOCSIG) {
|
||||
throw new ZipException("Wrong Local header signature" + header);
|
||||
}
|
||||
|
||||
short version = (short)ReadLeShort();
|
||||
|
||||
flags = ReadLeShort();
|
||||
method = ReadLeShort();
|
||||
int dostime = ReadLeInt();
|
||||
int crc2 = ReadLeInt();
|
||||
csize = ReadLeInt();
|
||||
size = ReadLeInt();
|
||||
int nameLen = ReadLeShort();
|
||||
int extraLen = ReadLeShort();
|
||||
|
||||
if (method == ZipOutputStream.STORED && csize != size) {
|
||||
throw new ZipException("Stored, but compressed != uncompressed");
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[nameLen];
|
||||
ReadFully(buffer);
|
||||
|
||||
string name = ZipConstants.ConvertToString(buffer);
|
||||
|
||||
entry = new ZipEntry(name);
|
||||
|
||||
entry.Version = (ushort)version;
|
||||
if (method != 0 && method != 8) {
|
||||
throw new ZipException("unknown compression method " + method);
|
||||
}
|
||||
entry.CompressionMethod = (CompressionMethod)method;
|
||||
|
||||
if ((flags & 8) == 0) {
|
||||
entry.Crc = crc2 & 0xFFFFFFFFL;
|
||||
entry.Size = size & 0xFFFFFFFFL;
|
||||
entry.CompressedSize = csize & 0xFFFFFFFFL;
|
||||
}
|
||||
entry.DosTime = dostime;
|
||||
if (extraLen > 0) {
|
||||
byte[] extra = new byte[extraLen];
|
||||
ReadFully(extra);
|
||||
entry.ExtraData = extra;
|
||||
}
|
||||
|
||||
if (method == ZipOutputStream.DEFLATED && avail > 0) {
|
||||
System.Array.Copy(buf, len - (int)avail, buf, 0, (int)avail);
|
||||
len = (int)avail;
|
||||
avail = 0;
|
||||
inf.SetInput(buf, 0, len);
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
private void ReadDataDescr()
|
||||
{
|
||||
if (ReadLeInt() != ZipConstants.EXTSIG) {
|
||||
throw new ZipException("Data descriptor signature not found");
|
||||
}
|
||||
entry.Crc = ReadLeInt() & 0xFFFFFFFFL;
|
||||
csize = ReadLeInt();
|
||||
size = ReadLeInt();
|
||||
entry.Size = size & 0xFFFFFFFFL;
|
||||
entry.CompressedSize = csize & 0xFFFFFFFFL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the current zip entry and moves to the next one.
|
||||
/// </summary>
|
||||
public void CloseEntry()
|
||||
{
|
||||
if (crc == null) {
|
||||
throw new InvalidOperationException("Closed.");
|
||||
}
|
||||
if (entry == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (method == ZipOutputStream.DEFLATED) {
|
||||
if ((flags & 8) != 0) {
|
||||
/* We don't know how much we must skip, read until end. */
|
||||
byte[] tmp = new byte[2048];
|
||||
while (Read(tmp, 0, tmp.Length) > 0)
|
||||
;
|
||||
/* read will close this entry */
|
||||
return;
|
||||
}
|
||||
csize -= inf.TotalIn;
|
||||
avail = inf.RemainingInput;
|
||||
}
|
||||
if (avail > csize && csize >= 0) {
|
||||
avail -= csize;
|
||||
} else {
|
||||
csize -= avail;
|
||||
avail = 0;
|
||||
while (csize != 0) {
|
||||
int skipped = (int)base.Skip(csize & 0xFFFFFFFFL);
|
||||
|
||||
if (skipped <= 0) {
|
||||
throw new ZipException("zip archive ends early.");
|
||||
}
|
||||
|
||||
csize -= skipped;
|
||||
}
|
||||
}
|
||||
|
||||
size = 0;
|
||||
crc.Reset();
|
||||
if (method == ZipOutputStream.DEFLATED) {
|
||||
inf.Reset();
|
||||
}
|
||||
entry = null;
|
||||
}
|
||||
|
||||
public override int Available {
|
||||
get {
|
||||
return entry != null ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a byte from the current zip entry.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the byte or -1 on EOF.
|
||||
/// </returns>
|
||||
/// <exception name="System.IO.IOException">
|
||||
/// IOException if a i/o error occured.
|
||||
/// </exception>
|
||||
/// <exception name="ICSharpCode.SharpZipLib.ZipException">
|
||||
/// ZipException if the deflated stream is corrupted.
|
||||
/// </exception>
|
||||
public override int ReadByte()
|
||||
{
|
||||
byte[] b = new byte[1];
|
||||
if (Read(b, 0, 1) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
return b[0] & 0xff;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a block of bytes from the current zip entry.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// the number of bytes read (may be smaller, even before EOF), or -1 on EOF.
|
||||
/// </returns>
|
||||
/// <exception name="Exception">
|
||||
/// IOException if a i/o error occured.
|
||||
/// ZipException if the deflated stream is corrupted.
|
||||
/// </exception>
|
||||
public override int Read(byte[] b, int off, int len)
|
||||
{
|
||||
if (crc == null) {
|
||||
throw new InvalidOperationException("Closed.");
|
||||
}
|
||||
if (entry == null) {
|
||||
return -1;
|
||||
}
|
||||
bool finished = false;
|
||||
|
||||
switch (method) {
|
||||
case ZipOutputStream.DEFLATED:
|
||||
len = base.Read(b, off, len);
|
||||
if (len < 0) {
|
||||
if (!inf.IsFinished) {
|
||||
throw new ZipException("Inflater not finished!?");
|
||||
}
|
||||
avail = inf.RemainingInput;
|
||||
if ((flags & 8) != 0) {
|
||||
ReadDataDescr();
|
||||
}
|
||||
|
||||
if (inf.TotalIn != csize || inf.TotalOut != size) {
|
||||
throw new ZipException("size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
|
||||
}
|
||||
inf.Reset();
|
||||
finished = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case ZipOutputStream.STORED:
|
||||
|
||||
if (len > csize && csize >= 0) {
|
||||
len = (int)csize;
|
||||
}
|
||||
len = ReadBuf(b, off, len);
|
||||
if (len > 0) {
|
||||
csize -= len;
|
||||
size -= len;
|
||||
}
|
||||
|
||||
if (csize == 0) {
|
||||
finished = true;
|
||||
} else {
|
||||
if (len < 0) {
|
||||
throw new ZipException("EOF in stored block");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
crc.Update(b, off, len);
|
||||
}
|
||||
|
||||
if (finished) {
|
||||
if ((crc.Value & 0xFFFFFFFFL) != entry.Crc) {
|
||||
throw new ZipException("CRC mismatch");
|
||||
}
|
||||
crc.Reset();
|
||||
entry = null;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the zip file.
|
||||
/// </summary>
|
||||
/// <exception name="Exception">
|
||||
/// if a i/o error occured.
|
||||
/// </exception>
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
crc = null;
|
||||
entry = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
// ZipOutputStream.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib.Zip {
|
||||
|
||||
/// <summary>
|
||||
/// This is a FilterOutputStream that writes the files into a zip
|
||||
/// archive one after another. It has a special method to start a new
|
||||
/// zip entry. The zip entries contains information about the file name
|
||||
/// size, compressed size, CRC, etc.
|
||||
///
|
||||
/// It includes support for STORED and DEFLATED entries.
|
||||
/// This class is not thread safe.
|
||||
///
|
||||
/// author of the original java version : Jochen Hoenicke
|
||||
/// </summary>
|
||||
/// <example> This sample shows how to create a zip file
|
||||
/// <code>
|
||||
/// using System;
|
||||
/// using System.IO;
|
||||
///
|
||||
/// using NZlib.Zip;
|
||||
///
|
||||
/// class MainClass
|
||||
/// {
|
||||
/// public static void Main(string[] args)
|
||||
/// {
|
||||
/// string[] filenames = Directory.GetFiles(args[0]);
|
||||
///
|
||||
/// ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
|
||||
///
|
||||
/// s.SetLevel(5); // 0 - store only to 9 - means best compression
|
||||
///
|
||||
/// foreach (string file in filenames) {
|
||||
/// FileStream fs = File.OpenRead(file);
|
||||
///
|
||||
/// byte[] buffer = new byte[fs.Length];
|
||||
/// fs.Read(buffer, 0, buffer.Length);
|
||||
///
|
||||
/// ZipEntry entry = new ZipEntry(file);
|
||||
///
|
||||
/// s.PutNextEntry(entry);
|
||||
///
|
||||
/// s.Write(buffer, 0, buffer.Length);
|
||||
///
|
||||
/// }
|
||||
///
|
||||
/// s.Finish();
|
||||
/// s.Close();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class ZipOutputStream : DeflaterOutputStream
|
||||
{
|
||||
private ArrayList entries = new ArrayList();
|
||||
private Crc32 crc = new Crc32();
|
||||
private ZipEntry curEntry = null;
|
||||
|
||||
private CompressionMethod curMethod;
|
||||
private int size;
|
||||
private int offset = 0;
|
||||
|
||||
private byte[] zipComment = new byte[0];
|
||||
private int defaultMethod = DEFLATED;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Our Zip version is hard coded to 1.0 resp. 2.0
|
||||
/// </summary>
|
||||
private const int ZIP_STORED_VERSION = 10;
|
||||
private const int ZIP_DEFLATED_VERSION = 20;
|
||||
|
||||
/// <summary>
|
||||
/// Compression method. This method doesn't compress at all.
|
||||
/// </summary>
|
||||
public const int STORED = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Compression method. This method uses the Deflater.
|
||||
/// </summary>
|
||||
public const int DEFLATED = 8;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Zip output stream, writing a zip archive.
|
||||
/// </summary>
|
||||
/// <param name="baseOutputStream">
|
||||
/// the output stream to which the zip archive is written.
|
||||
/// </param>
|
||||
public ZipOutputStream(Stream baseOutputStream) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the zip file comment.
|
||||
/// </summary>
|
||||
/// <param name="comment">
|
||||
/// the comment.
|
||||
/// </param>
|
||||
/// <exception name ="ArgumentException">
|
||||
/// if UTF8 encoding of comment is longer than 0xffff bytes.
|
||||
/// </exception>
|
||||
public void SetComment(string comment)
|
||||
{
|
||||
byte[] commentBytes = ZipConstants.ConvertToArray(comment);
|
||||
if (commentBytes.Length > 0xffff) {
|
||||
throw new ArgumentException("Comment too long.");
|
||||
}
|
||||
zipComment = commentBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets default compression method. If the Zip entry specifies
|
||||
/// another method its method takes precedence.
|
||||
/// </summary>
|
||||
/// <param name = "method">
|
||||
/// the method.
|
||||
/// </param>
|
||||
/// <exception name = "ArgumentException">
|
||||
/// if method is not supported.
|
||||
/// </exception>
|
||||
public void SetMethod(int method)
|
||||
{
|
||||
if (method != STORED && method != DEFLATED) {
|
||||
throw new ArgumentException("Method not supported.");
|
||||
}
|
||||
defaultMethod = method;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets default compression level. The new level will be activated
|
||||
/// immediately.
|
||||
/// </summary>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">
|
||||
/// if level is not supported.
|
||||
/// </exception>
|
||||
/// <see cref="Deflater"/>
|
||||
public void SetLevel(int level)
|
||||
{
|
||||
def.SetLevel(level);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an unsigned short in little endian byte order.
|
||||
/// </summary>
|
||||
private void WriteLeShort(int value)
|
||||
{
|
||||
baseOutputStream.WriteByte((byte)value);
|
||||
baseOutputStream.WriteByte((byte)(value >> 8));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an int in little endian byte order.
|
||||
/// </summary>
|
||||
private void WriteLeInt(int value)
|
||||
{
|
||||
WriteLeShort(value);
|
||||
WriteLeShort(value >> 16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write an int in little endian byte order.
|
||||
/// </summary>
|
||||
private void WriteLeLong(long value)
|
||||
{
|
||||
WriteLeInt((int)value);
|
||||
WriteLeInt((int)(value >> 32));
|
||||
}
|
||||
|
||||
|
||||
bool shouldWriteBack = false;
|
||||
long seekPos = -1;
|
||||
/// <summary>
|
||||
/// Starts a new Zip entry. It automatically closes the previous
|
||||
/// entry if present. If the compression method is stored, the entry
|
||||
/// must have a valid size and crc, otherwise all elements (except
|
||||
/// name) are optional, but must be correct if present. If the time
|
||||
/// is not set in the entry, the current time is used.
|
||||
/// </summary>
|
||||
/// <param name="entry">
|
||||
/// the entry.
|
||||
/// </param>
|
||||
/// <exception cref="System.IO.IOException">
|
||||
/// if an I/O error occured.
|
||||
/// </exception>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if stream was finished
|
||||
/// </exception>
|
||||
public void PutNextEntry(ZipEntry entry)
|
||||
{
|
||||
if (entries == null) {
|
||||
throw new InvalidOperationException("ZipOutputStream was finished");
|
||||
}
|
||||
|
||||
CompressionMethod method = entry.CompressionMethod;
|
||||
int flags = 0;
|
||||
|
||||
switch (method) {
|
||||
case CompressionMethod.Stored:
|
||||
if (entry.CompressedSize >= 0) {
|
||||
if (entry.Size < 0) {
|
||||
entry.Size = entry.CompressedSize;
|
||||
} else if (entry.Size != entry.CompressedSize) {
|
||||
throw new ZipException("Method STORED, but compressed size != size");
|
||||
}
|
||||
} else {
|
||||
entry.CompressedSize = entry.Size;
|
||||
}
|
||||
|
||||
if (entry.Size < 0) {
|
||||
throw new ZipException("Method STORED, but size not set");
|
||||
} else if (entry.Crc < 0) {
|
||||
throw new ZipException("Method STORED, but crc not set");
|
||||
}
|
||||
break;
|
||||
case CompressionMethod.Deflated:
|
||||
if (entry.CompressedSize < 0 || entry.Size < 0 || entry.Crc < 0) {
|
||||
flags |= 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (curEntry != null) {
|
||||
CloseEntry();
|
||||
}
|
||||
|
||||
// if (entry.DosTime < 0) {
|
||||
// entry.Time = System.Environment.TickCount;
|
||||
// }
|
||||
|
||||
entry.flags = flags;
|
||||
entry.offset = offset;
|
||||
entry.CompressionMethod = (CompressionMethod)method;
|
||||
|
||||
curMethod = method;
|
||||
// Write the local file header
|
||||
WriteLeInt(ZipConstants.LOCSIG);
|
||||
|
||||
// write ZIP version
|
||||
WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);
|
||||
if ((flags & 8) == 0) {
|
||||
WriteLeShort(flags);
|
||||
WriteLeShort((byte)method);
|
||||
WriteLeInt(entry.DosTime);
|
||||
WriteLeInt((int)entry.Crc);
|
||||
WriteLeInt((int)entry.CompressedSize);
|
||||
WriteLeInt((int)entry.Size);
|
||||
} else {
|
||||
if (baseOutputStream.CanSeek) {
|
||||
shouldWriteBack = true;
|
||||
WriteLeShort((short)(flags & ~8));
|
||||
} else {
|
||||
shouldWriteBack = false;
|
||||
WriteLeShort(flags);
|
||||
}
|
||||
WriteLeShort((byte)method);
|
||||
WriteLeInt(entry.DosTime);
|
||||
seekPos = baseOutputStream.Position;
|
||||
WriteLeInt(0);
|
||||
WriteLeInt(0);
|
||||
WriteLeInt(0);
|
||||
}
|
||||
byte[] name = ZipConstants.ConvertToArray(entry.Name);
|
||||
|
||||
if (name.Length > 0xFFFF) {
|
||||
throw new ZipException("Name too long.");
|
||||
}
|
||||
byte[] extra = entry.ExtraData;
|
||||
if (extra == null) {
|
||||
extra = new byte[0];
|
||||
}
|
||||
if (extra.Length > 0xFFFF) {
|
||||
throw new ZipException("Extra data too long.");
|
||||
}
|
||||
|
||||
WriteLeShort(name.Length);
|
||||
WriteLeShort(extra.Length);
|
||||
baseOutputStream.Write(name, 0, name.Length);
|
||||
baseOutputStream.Write(extra, 0, extra.Length);
|
||||
|
||||
offset += ZipConstants.LOCHDR + name.Length + extra.Length;
|
||||
|
||||
/* Activate the entry. */
|
||||
curEntry = entry;
|
||||
crc.Reset();
|
||||
if (method == CompressionMethod.Deflated) {
|
||||
def.Reset();
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the current entry.
|
||||
/// </summary>
|
||||
/// <exception cref="System.IO.IOException">
|
||||
/// if an I/O error occured.
|
||||
/// </exception>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no entry is active.
|
||||
/// </exception>
|
||||
public void CloseEntry()
|
||||
{
|
||||
if (curEntry == null) {
|
||||
throw new InvalidOperationException("No open entry");
|
||||
}
|
||||
|
||||
/* First finish the deflater, if appropriate */
|
||||
if (curMethod == CompressionMethod.Deflated) {
|
||||
base.Finish();
|
||||
}
|
||||
|
||||
int csize = curMethod == CompressionMethod.Deflated ? def.TotalOut : size;
|
||||
|
||||
if (curEntry.Size < 0) {
|
||||
curEntry.Size = size;
|
||||
} else if (curEntry.Size != size) {
|
||||
throw new ZipException("size was " + size +
|
||||
", but I expected " + curEntry.Size);
|
||||
}
|
||||
|
||||
if (curEntry.CompressedSize < 0) {
|
||||
curEntry.CompressedSize = csize;
|
||||
} else if (curEntry.CompressedSize != csize) {
|
||||
throw new ZipException("compressed size was " + csize +
|
||||
", but I expected " + curEntry.CompressedSize);
|
||||
}
|
||||
|
||||
if (curEntry.Crc < 0) {
|
||||
curEntry.Crc = crc.Value;
|
||||
} else if (curEntry.Crc != crc.Value) {
|
||||
throw new ZipException("crc was " + crc.Value +
|
||||
", but I expected " +
|
||||
curEntry.Crc);
|
||||
}
|
||||
|
||||
offset += csize;
|
||||
|
||||
/* Now write the data descriptor entry if needed. */
|
||||
if (curMethod == CompressionMethod.Deflated && (curEntry.flags & 8) != 0) {
|
||||
if (shouldWriteBack) {
|
||||
long curPos = baseOutputStream.Position;
|
||||
baseOutputStream.Seek(seekPos, SeekOrigin.Begin);
|
||||
WriteLeInt((int)curEntry.Crc);
|
||||
WriteLeInt((int)curEntry.CompressedSize);
|
||||
WriteLeInt((int)curEntry.Size);
|
||||
baseOutputStream.Seek(curPos, SeekOrigin.Begin);
|
||||
shouldWriteBack = false;
|
||||
} else {
|
||||
WriteLeInt(ZipConstants.EXTSIG);
|
||||
WriteLeInt((int)curEntry.Crc);
|
||||
WriteLeInt((int)curEntry.CompressedSize);
|
||||
WriteLeInt((int)curEntry.Size);
|
||||
offset += ZipConstants.EXTHDR;
|
||||
}
|
||||
}
|
||||
|
||||
entries.Add(curEntry);
|
||||
curEntry = null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the given buffer to the current entry.
|
||||
/// </summary>
|
||||
/// <exception cref="System.IO.IOException">
|
||||
/// if an I/O error occured.
|
||||
/// </exception>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// if no entry is active.
|
||||
/// </exception>
|
||||
public override void Write(byte[] b, int off, int len)
|
||||
{
|
||||
if (curEntry == null) {
|
||||
throw new InvalidOperationException("No open entry.");
|
||||
}
|
||||
|
||||
switch (curMethod) {
|
||||
case CompressionMethod.Deflated:
|
||||
base.Write(b, off, len);
|
||||
break;
|
||||
case CompressionMethod.Stored:
|
||||
baseOutputStream.Write(b, off, len);
|
||||
break;
|
||||
}
|
||||
|
||||
crc.Update(b, off, len);
|
||||
size += len;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Finishes the stream. This will write the central directory at the
|
||||
/// end of the zip file and flush the stream.
|
||||
/// </summary>
|
||||
/// <exception cref="System.IO.IOException">
|
||||
/// if an I/O error occured.
|
||||
/// </exception>
|
||||
public override void Finish()
|
||||
{
|
||||
if (entries == null) {
|
||||
return;
|
||||
}
|
||||
if (curEntry != null) {
|
||||
CloseEntry();
|
||||
}
|
||||
|
||||
int numEntries = 0;
|
||||
int sizeEntries = 0;
|
||||
|
||||
foreach (ZipEntry entry in entries) {
|
||||
// TODO : check the appnote file for compilance with the central directory standard
|
||||
CompressionMethod method = entry.CompressionMethod;
|
||||
WriteLeInt(ZipConstants.CENSIG);
|
||||
WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);
|
||||
WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);
|
||||
WriteLeShort(entry.flags);
|
||||
WriteLeShort((short)method);
|
||||
WriteLeInt(entry.DosTime);
|
||||
WriteLeInt((int)entry.Crc);
|
||||
WriteLeInt((int)entry.CompressedSize);
|
||||
WriteLeInt((int)entry.Size);
|
||||
|
||||
byte[] name = ZipConstants.ConvertToArray(entry.Name);
|
||||
|
||||
if (name.Length > 0xffff) {
|
||||
throw new ZipException("Name too long.");
|
||||
}
|
||||
byte[] extra = entry.ExtraData;
|
||||
if (extra == null) {
|
||||
extra = new byte[0];
|
||||
}
|
||||
string strComment = entry.Comment;
|
||||
byte[] comment = strComment != null ? ZipConstants.ConvertToArray(strComment) : new byte[0];
|
||||
if (comment.Length > 0xffff) {
|
||||
throw new ZipException("Comment too long.");
|
||||
}
|
||||
|
||||
WriteLeShort(name.Length);
|
||||
WriteLeShort(extra.Length);
|
||||
WriteLeShort(comment.Length);
|
||||
WriteLeShort(0); /* disk number */
|
||||
WriteLeShort(0); /* internal file attr */
|
||||
WriteLeInt(0); /* external file attr */
|
||||
WriteLeInt(entry.offset);
|
||||
|
||||
baseOutputStream.Write(name, 0, name.Length);
|
||||
baseOutputStream.Write(extra, 0, extra.Length);
|
||||
baseOutputStream.Write(comment, 0, comment.Length);
|
||||
++numEntries;
|
||||
sizeEntries += ZipConstants.CENHDR + name.Length + extra.Length + comment.Length;
|
||||
}
|
||||
|
||||
WriteLeInt(ZipConstants.ENDSIG);
|
||||
WriteLeShort(0); /* disk number */
|
||||
WriteLeShort(0); /* disk with start of central dir */
|
||||
WriteLeShort(numEntries);
|
||||
WriteLeShort(numEntries);
|
||||
WriteLeInt(sizeEntries);
|
||||
WriteLeInt(offset);
|
||||
WriteLeShort(zipComment.Length);
|
||||
baseOutputStream.Write(zipComment, 0, zipComment.Length);
|
||||
baseOutputStream.Flush();
|
||||
entries = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// ZipException.cs
|
||||
// Copyright (C) 2001 Mike Krueger
|
||||
//
|
||||
// This file was translated from java, it was part of the GNU Classpath
|
||||
// Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
//
|
||||
// Linking this library statically or dynamically with other modules is
|
||||
// making a combined work based on this library. Thus, the terms and
|
||||
// conditions of the GNU General Public License cover the whole
|
||||
// combination.
|
||||
//
|
||||
// As a special exception, the copyright holders of this library give you
|
||||
// permission to link this library with independent modules to produce an
|
||||
// executable, regardless of the license terms of these independent
|
||||
// modules, and to copy and distribute the resulting executable under
|
||||
// terms of your choice, provided that you also meet, for each linked
|
||||
// independent module, the terms and conditions of the license of that
|
||||
// module. An independent module is a module which is not derived from
|
||||
// or based on this library. If you modify this library, you may extend
|
||||
// this exception to your version of the library, but you are not
|
||||
// obligated to do so. If you do not wish to do so, delete this
|
||||
// exception statement from your version.
|
||||
|
||||
using System;
|
||||
|
||||
namespace ICSharpCode.SharpZipLib {
|
||||
|
||||
/// <summary>
|
||||
/// Is thrown during the creation or input of a zip file.
|
||||
/// </summary>
|
||||
public class ZipException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ZipException class with default properties.
|
||||
/// </summary>
|
||||
public ZipException()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ZipException class with a specified error message.
|
||||
/// </summary>
|
||||
public ZipException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
This directory contains a local copy of the Zip library from the NZipLib project.
|
||||
|
||||
I have added this as a temporary measure to simplify the building and development
|
||||
of the Mono Documentation browser, but ideally it should use the real
|
||||
library. Do not fix bugs here, fix it on the original, and incorporate them
|
||||
here.
|
||||
|
||||
This code is released under this license, from:
|
||||
|
||||
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
|
||||
|
||||
Here it is:
|
||||
|
||||
The library is released under the GPL with the following exception:
|
||||
|
||||
Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.
|
||||
|
||||
|
||||
Miguel. Jan, 2003
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
|
||||
<!-- NAnt build file for System.Data.dll -->
|
||||
|
||||
<project name="System.Data" default="build">
|
||||
<property name="debug" value="false"/>
|
||||
|
||||
<target name="build">
|
||||
<csc target="library" output="../lib/ICSharpCode.SharpZipLib.dll" debug="${debug}">
|
||||
<arg value="/nowarn:1595"/>
|
||||
<arg value="/nowarn:0067"/>
|
||||
<arg value="/nowarn:0109"/>
|
||||
<arg value="/nowarn:0169"/>
|
||||
<arg value="/nowarn:0679"/>
|
||||
<arg value="/nowarn:0649"/>
|
||||
<arg value="/unsafe"/>
|
||||
<arg value="/noconfig"/>
|
||||
<arg value="/r:System.dll"/>
|
||||
<arg value="/r:System.Xml.dll"/>
|
||||
<sources>
|
||||
<includes name="**/*.cs"/>
|
||||
<includes name="**/**/*.cs"/>
|
||||
<includes name="**/**/**/*.cs"/>
|
||||
</sources>
|
||||
<references>
|
||||
<includes name="../lib/corlib.dll"/>
|
||||
<includes name="../lib/System.dll"/>
|
||||
<includes name="../lib/System.Xml.dll"/>
|
||||
</references>
|
||||
</csc>
|
||||
</target>
|
||||
<target name="test" depends="build">
|
||||
<nant basedir="Test" target="test"/>
|
||||
</target>
|
||||
<target name="clean">
|
||||
<delete file="../lib/ICSharpCode.SharpZipLib.dll" failonerror="false"/>
|
||||
</target>
|
||||
</project>
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
./ICSharpCode.SharpZipLib/BZip2/BZip2.cs
|
||||
./ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs
|
||||
./ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
|
||||
./ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Checksums/Adler32.cs
|
||||
./ICSharpCode.SharpZipLib/Checksums/Crc32.cs
|
||||
./ICSharpCode.SharpZipLib/Checksums/IChecksum.cs
|
||||
./ICSharpCode.SharpZipLib/Checksums/StrangeCrc.cs
|
||||
./ICSharpCode.SharpZipLib/GZip/GZipConstants.cs
|
||||
./ICSharpCode.SharpZipLib/GZip/GZipInputStream.cs
|
||||
./ICSharpCode.SharpZipLib/GZip/GZipOutputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarArchive.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarEntry.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarHeader.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarInputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/ZipConstants.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/ZipFile.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
|
||||
./ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
|
||||
./ICSharpCode.SharpZipLib/AssemblyInfo.cs
|
||||
./ICSharpCode.SharpZipLib/ZipException.cs
|
||||
@@ -0,0 +1,14 @@
|
||||
topdir = ../..
|
||||
|
||||
TEST_DIR=
|
||||
LIBRARY = $(topdir)/class/lib/ICSharpCode.SharpZipLib.dll
|
||||
|
||||
LIB_LIST = list
|
||||
LIB_FLAGS = -r corlib -r System -r System.Xml
|
||||
|
||||
SOURCES_INCLUDE=*.cs
|
||||
SOURCES_EXCLUDE=
|
||||
|
||||
export MONO_PATH_PREFIX = $(topdir)/class/lib:
|
||||
|
||||
include $(topdir)/class/library.make
|
||||
@@ -0,0 +1,6 @@
|
||||
2003-03-06 Sebastien Pouliot <[email protected]>
|
||||
|
||||
* list.unix: New. All C# files required to build the assembly.
|
||||
* makefile.gnu: New. Makefile to build the assembly under Linux.
|
||||
* Mono.Security.build: New. NAnt build file.
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
2003-03-06 Sebastien Pouliot <[email protected]>
|
||||
|
||||
* SoftwarePublisherCertificate.cs: New. Class to load or create
|
||||
SPC files (which are PKCS#7 files containing only certificates
|
||||
and CRL).
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// SoftwarePublisherCertificate.cs
|
||||
// - Software Publisher Certificates Implementation
|
||||
//
|
||||
// Author:
|
||||
// Sebastien Pouliot ([email protected])
|
||||
//
|
||||
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
using Mono.Security;
|
||||
|
||||
namespace Mono.Security.Authenticode {
|
||||
|
||||
public class SoftwarePublisherCertificate {
|
||||
|
||||
private PKCS7.SignedData pkcs7;
|
||||
|
||||
public SoftwarePublisherCertificate ()
|
||||
{
|
||||
pkcs7 = new PKCS7.SignedData ();
|
||||
pkcs7.ContentInfo.ContentType = PKCS7.data;
|
||||
}
|
||||
|
||||
public SoftwarePublisherCertificate (byte[] spc) : this ()
|
||||
{
|
||||
PKCS7.ContentInfo ci = new PKCS7.ContentInfo (spc);
|
||||
if (ci.ContentType != PKCS7.signedData)
|
||||
throw new ArgumentException ("Unsupported ContentType");
|
||||
pkcs7 = new PKCS7.SignedData (ci.Content);
|
||||
}
|
||||
|
||||
public X509CertificateCollection Certificates {
|
||||
get { return pkcs7.Certificates; }
|
||||
}
|
||||
|
||||
public ArrayList CRLs {
|
||||
get { return pkcs7.CRLs; }
|
||||
}
|
||||
|
||||
public byte[] GetBytes ()
|
||||
{
|
||||
PKCS7.ContentInfo ci = new PKCS7.ContentInfo (PKCS7.signedData);
|
||||
ci.Content.Add (pkcs7.ASN1);
|
||||
return ci.GetBytes ();
|
||||
}
|
||||
|
||||
static public SoftwarePublisherCertificate CreateFromFile (string filename)
|
||||
{
|
||||
FileStream fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
byte[] data = new byte [fs.Length];
|
||||
fs.Read (data, 0, data.Length);
|
||||
fs.Close ();
|
||||
return new SoftwarePublisherCertificate (data);
|
||||
}
|
||||
}
|
||||
}
|
||||
+497
@@ -0,0 +1,497 @@
|
||||
//
|
||||
// SoftwarePublisherCertificateTest.cs
|
||||
// - NUnit Test Cases for Software Publisher Certificate
|
||||
//
|
||||
// Author:
|
||||
// Sebastien Pouliot ([email protected])
|
||||
//
|
||||
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
using Mono.Security.Authenticode;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MonoTests.Mono.Security.Authenticode {
|
||||
|
||||
// HOWTO create a SPC file
|
||||
// cert2spc cert1.cer cacert.cer cacrl.crl ... output.spc
|
||||
|
||||
[TestFixture]
|
||||
public class SoftwarePublisherCertificateFileTest {
|
||||
|
||||
static byte[] certonly = {
|
||||
0x30, 0x82, 0x03, 0x1E, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
|
||||
0x01, 0x07, 0x02, 0xA0, 0x82, 0x03, 0x0F, 0x30, 0x82, 0x03, 0x0B, 0x02,
|
||||
0x01, 0x01, 0x31, 0x00, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
|
||||
0xF7, 0x0D, 0x01, 0x07, 0x01, 0xA0, 0x82, 0x02, 0xF3, 0x30, 0x82, 0x02,
|
||||
0xEF, 0x30, 0x82, 0x02, 0x5A, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01,
|
||||
0x6C, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
|
||||
0x01, 0x05, 0x30, 0x56, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
|
||||
0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55,
|
||||
0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76,
|
||||
0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06,
|
||||
0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30,
|
||||
0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x04, 0x4E, 0x61, 0x76, 0x79,
|
||||
0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x4E,
|
||||
0x61, 0x76, 0x79, 0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x30, 0x32,
|
||||
0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x17,
|
||||
0x0D, 0x30, 0x34, 0x31, 0x30, 0x31, 0x30, 0x31, 0x33, 0x31, 0x32, 0x35,
|
||||
0x30, 0x5A, 0x30, 0x81, 0x8B, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
|
||||
0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03,
|
||||
0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F,
|
||||
0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A,
|
||||
0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x0D,
|
||||
0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x04, 0x4E, 0x61, 0x76,
|
||||
0x79, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x09,
|
||||
0x6C, 0x6F, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x73, 0x31, 0x1B, 0x30,
|
||||
0x19, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x12, 0x41, 0x6E, 0x6E, 0x61,
|
||||
0x70, 0x6F, 0x6C, 0x69, 0x73, 0x20, 0x4A, 0x75, 0x6E, 0x63, 0x74, 0x69,
|
||||
0x6F, 0x6E, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
|
||||
0x0B, 0x4E, 0x61, 0x76, 0x79, 0x20, 0x55, 0x73, 0x65, 0x72, 0x20, 0x31,
|
||||
0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
|
||||
0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81,
|
||||
0x89, 0x02, 0x81, 0x81, 0x00, 0xB7, 0x7E, 0x94, 0x5F, 0xE8, 0x2A, 0xE7,
|
||||
0xAD, 0x82, 0x16, 0x2C, 0x3D, 0x2F, 0x5E, 0x88, 0x67, 0xF0, 0x23, 0x26,
|
||||
0x15, 0x34, 0x04, 0x1F, 0x63, 0x8B, 0xFE, 0xFB, 0xBB, 0x0D, 0xC0, 0x7E,
|
||||
0xF0, 0x46, 0x82, 0x09, 0xA2, 0x91, 0xE0, 0xEA, 0xEF, 0xD0, 0x43, 0xCB,
|
||||
0x30, 0x45, 0xAC, 0x7C, 0xAC, 0xFC, 0xBE, 0x54, 0x79, 0x77, 0xA9, 0x6A,
|
||||
0x45, 0xF5, 0xBF, 0xE5, 0xEF, 0x97, 0x11, 0x63, 0xC2, 0xF7, 0x3C, 0x73,
|
||||
0x6D, 0xBA, 0x8D, 0xFE, 0xAE, 0x28, 0x4A, 0x29, 0xE4, 0xA2, 0x59, 0x0C,
|
||||
0x8F, 0x1A, 0x57, 0x86, 0xF2, 0x42, 0xF7, 0x35, 0x0B, 0xC3, 0xA5, 0x31,
|
||||
0xD8, 0x19, 0xE2, 0x97, 0x7A, 0xA1, 0xF4, 0xE5, 0xDB, 0xCA, 0xF5, 0x54,
|
||||
0x39, 0x1D, 0x0E, 0xDF, 0x78, 0x73, 0xBF, 0x86, 0x97, 0x40, 0xAA, 0x06,
|
||||
0x8E, 0x8B, 0x6B, 0x0C, 0x06, 0x98, 0xD7, 0xD2, 0x1D, 0x45, 0xAA, 0x7F,
|
||||
0xA5, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x81, 0x9A, 0x30, 0x81, 0x97,
|
||||
0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
|
||||
0x14, 0xFB, 0x96, 0xF0, 0x10, 0xC4, 0x37, 0x55, 0xF0, 0xCE, 0xB5, 0xA6,
|
||||
0xE2, 0xF1, 0x19, 0xFF, 0x99, 0x1A, 0xAE, 0x6E, 0x58, 0x30, 0x1D, 0x06,
|
||||
0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x02, 0x48, 0x78, 0xB9,
|
||||
0xCC, 0x01, 0x51, 0x31, 0x74, 0x7F, 0x39, 0x2A, 0x37, 0xC2, 0x44, 0x93,
|
||||
0x7E, 0x98, 0x69, 0x80, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x04,
|
||||
0x04, 0x03, 0x02, 0x04, 0xF0, 0x30, 0x17, 0x06, 0x03, 0x55, 0x1D, 0x20,
|
||||
0x04, 0x10, 0x30, 0x0E, 0x30, 0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01,
|
||||
0x65, 0x03, 0x02, 0x01, 0x30, 0x01, 0x30, 0x2F, 0x06, 0x03, 0x55, 0x1D,
|
||||
0x11, 0x04, 0x28, 0x30, 0x26, 0x81, 0x24, 0x4E, 0x61, 0x76, 0x79, 0x31,
|
||||
0x40, 0x77, 0x61, 0x72, 0x72, 0x65, 0x6E, 0x74, 0x6F, 0x6E, 0x2E, 0x61,
|
||||
0x74, 0x6C, 0x2E, 0x67, 0x65, 0x74, 0x72, 0x6F, 0x6E, 0x69, 0x63, 0x73,
|
||||
0x67, 0x6F, 0x76, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x0B, 0x06, 0x09, 0x2A,
|
||||
0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00,
|
||||
0x1D, 0xB0, 0x1C, 0x88, 0x4D, 0xA2, 0x68, 0x25, 0x08, 0x8F, 0xA3, 0xAC,
|
||||
0xC3, 0x18, 0xD5, 0xBF, 0x56, 0x7C, 0xA1, 0xF2, 0x7C, 0x76, 0x39, 0x8D,
|
||||
0x12, 0x42, 0x17, 0xE6, 0x49, 0x02, 0x39, 0xAE, 0xBB, 0x75, 0x70, 0x4B,
|
||||
0x65, 0xEF, 0x0E, 0x3A, 0xC2, 0x33, 0xD9, 0x94, 0xDF, 0x5F, 0xA6, 0x12,
|
||||
0x64, 0x8F, 0x04, 0x76, 0x2C, 0xAF, 0x92, 0x37, 0x4C, 0xF1, 0x94, 0x99,
|
||||
0x52, 0xFD, 0x61, 0x95, 0x00, 0x2B, 0x9D, 0x0D, 0x35, 0xB9, 0x7C, 0x6A,
|
||||
0x4C, 0xBB, 0x8D, 0x8A, 0x7B, 0x93, 0x37, 0x02, 0xC8, 0x81, 0x0B, 0xBD,
|
||||
0xB9, 0x45, 0x51, 0x03, 0xBA, 0xD3, 0xF4, 0xBD, 0x72, 0x10, 0x05, 0xE9,
|
||||
0xC1, 0x6E, 0xFE, 0xC5, 0x76, 0x2C, 0x6A, 0x6A, 0x16, 0x2F, 0x0C, 0x54,
|
||||
0x44, 0x0D, 0x15, 0xC7, 0xA5, 0x41, 0xB1, 0x05, 0xE8, 0x4B, 0xF3, 0x60,
|
||||
0x92, 0xEB, 0xD4, 0xF7, 0x93, 0xFF, 0x67, 0x4E, 0x31, 0x00 };
|
||||
|
||||
static byte[] crlonly = {
|
||||
0x30, 0x82, 0x01, 0x9B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
|
||||
0x01, 0x07, 0x02, 0xA0, 0x82, 0x01, 0x8C, 0x30, 0x82, 0x01, 0x88, 0x02,
|
||||
0x01, 0x01, 0x31, 0x00, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
|
||||
0xF7, 0x0D, 0x01, 0x07, 0x01, 0xA1, 0x82, 0x01, 0x70, 0x30, 0x82, 0x01,
|
||||
0x6C, 0x30, 0x81, 0xD8, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A,
|
||||
0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x56, 0x31, 0x0B,
|
||||
0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
|
||||
0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E,
|
||||
0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E,
|
||||
0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03,
|
||||
0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B,
|
||||
0x13, 0x04, 0x4E, 0x61, 0x76, 0x79, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
|
||||
0x55, 0x04, 0x03, 0x13, 0x07, 0x4E, 0x61, 0x76, 0x79, 0x20, 0x43, 0x41,
|
||||
0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32,
|
||||
0x35, 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x33, 0x31, 0x30, 0x31, 0x31, 0x31,
|
||||
0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x50, 0x30, 0x12, 0x02, 0x01,
|
||||
0x6D, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x34, 0x32, 0x33, 0x32, 0x31, 0x30,
|
||||
0x39, 0x32, 0x37, 0x5A, 0x30, 0x12, 0x02, 0x01, 0x6F, 0x17, 0x0D, 0x30,
|
||||
0x31, 0x30, 0x34, 0x32, 0x33, 0x32, 0x31, 0x30, 0x39, 0x32, 0x37, 0x5A,
|
||||
0x30, 0x12, 0x02, 0x01, 0x50, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x31, 0x33,
|
||||
0x30, 0x32, 0x32, 0x30, 0x38, 0x32, 0x39, 0x5A, 0x30, 0x12, 0x02, 0x01,
|
||||
0x52, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x31, 0x33, 0x30, 0x32, 0x32, 0x30,
|
||||
0x38, 0x32, 0x39, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
|
||||
0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x3A, 0xFA, 0x41,
|
||||
0x76, 0x90, 0x24, 0x6E, 0x59, 0xEE, 0xF3, 0xC4, 0xA2, 0x77, 0xE0, 0xE4,
|
||||
0x70, 0x69, 0x43, 0xA0, 0x8E, 0x42, 0x9F, 0x1F, 0x58, 0x43, 0x1D, 0xF0,
|
||||
0x4F, 0x1D, 0xE8, 0xF3, 0x36, 0x09, 0x07, 0xE5, 0x3A, 0x84, 0xBB, 0x54,
|
||||
0xBB, 0xB6, 0x55, 0x88, 0x76, 0xC2, 0x42, 0x62, 0xC1, 0xE9, 0x54, 0xA2,
|
||||
0x49, 0xEE, 0x98, 0xDD, 0x07, 0x84, 0x90, 0x5F, 0x7E, 0x94, 0x11, 0x64,
|
||||
0x35, 0x2D, 0xBA, 0x5A, 0xC7, 0x19, 0x46, 0xAF, 0x21, 0x3C, 0x3B, 0xB6,
|
||||
0x0E, 0x28, 0x2B, 0x38, 0x9A, 0xA1, 0xB6, 0x7B, 0x6A, 0xC8, 0xA8, 0xBA,
|
||||
0xC7, 0x9E, 0xD1, 0x31, 0x70, 0x5F, 0xD6, 0x15, 0x03, 0xE6, 0x6C, 0x55,
|
||||
0x85, 0x30, 0xA8, 0x45, 0xBB, 0x28, 0xF3, 0xAC, 0x97, 0x5F, 0x86, 0x21,
|
||||
0x77, 0xEF, 0xEC, 0x17, 0x92, 0xC7, 0xD6, 0xCD, 0xE1, 0x2A, 0x2E, 0xE7,
|
||||
0xF3, 0xED, 0x7F, 0x66, 0x86, 0x31, 0x00 };
|
||||
|
||||
static byte[] navy = {
|
||||
0x30, 0x82, 0x0B, 0x7F, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
|
||||
0x01, 0x07, 0x02, 0xA0, 0x82, 0x0B, 0x70, 0x30, 0x82, 0x0B, 0x6C, 0x02,
|
||||
0x01, 0x01, 0x31, 0x00, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
|
||||
0xF7, 0x0D, 0x01, 0x07, 0x01, 0xA0, 0x82, 0x08, 0xB2, 0x30, 0x82, 0x02,
|
||||
0xAD, 0x30, 0x82, 0x02, 0x18, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01,
|
||||
0x0C, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
|
||||
0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
|
||||
0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55,
|
||||
0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76,
|
||||
0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06,
|
||||
0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30,
|
||||
0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65,
|
||||
0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F,
|
||||
0x74, 0x30, 0x1E, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x30, 0x32, 0x35, 0x30,
|
||||
0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x33, 0x30, 0x31,
|
||||
0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x50, 0x31,
|
||||
0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
|
||||
0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55,
|
||||
0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65,
|
||||
0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13,
|
||||
0x03, 0x44, 0x6F, 0x44, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04,
|
||||
0x03, 0x13, 0x10, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72,
|
||||
0x63, 0x65, 0x73, 0x20, 0x49, 0x43, 0x41, 0x30, 0x81, 0x9F, 0x30, 0x0D,
|
||||
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05,
|
||||
0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00,
|
||||
0xD7, 0xA1, 0xB5, 0x18, 0x96, 0x60, 0x18, 0x4F, 0x8A, 0x94, 0x51, 0x47,
|
||||
0x4B, 0x48, 0xC6, 0xE5, 0x78, 0x53, 0x8F, 0x7E, 0x0E, 0x87, 0xD4, 0x05,
|
||||
0x15, 0x50, 0x3F, 0x92, 0x43, 0x4F, 0xBF, 0x43, 0xB5, 0x36, 0xCA, 0x69,
|
||||
0x53, 0x91, 0x1D, 0x68, 0x12, 0x8B, 0x7D, 0x19, 0xAC, 0xA4, 0xA6, 0xDD,
|
||||
0x69, 0x1B, 0x95, 0xF6, 0x68, 0x9D, 0xC1, 0x7D, 0x64, 0xC3, 0x7F, 0xA1,
|
||||
0xCA, 0x2A, 0xC5, 0xE5, 0x0D, 0x5F, 0x00, 0x6A, 0xA7, 0xCF, 0xF0, 0x25,
|
||||
0x86, 0xC4, 0xEA, 0x3B, 0x39, 0x63, 0x00, 0x46, 0x4E, 0xC7, 0xE2, 0xDD,
|
||||
0x47, 0xCF, 0xB3, 0x5E, 0x2F, 0x0B, 0x59, 0x94, 0xE2, 0xCB, 0x04, 0xFA,
|
||||
0x88, 0x30, 0xA3, 0x32, 0xE4, 0x5D, 0x17, 0x09, 0x1A, 0xE1, 0x6D, 0x27,
|
||||
0x03, 0x53, 0x65, 0xC1, 0x1F, 0xE3, 0x73, 0xA4, 0x8D, 0xD8, 0xCB, 0x5A,
|
||||
0x22, 0x07, 0xE4, 0x35, 0x61, 0x8F, 0xD6, 0x57, 0x02, 0x03, 0x01, 0x00,
|
||||
0x01, 0xA3, 0x81, 0x99, 0x30, 0x81, 0x96, 0x30, 0x1F, 0x06, 0x03, 0x55,
|
||||
0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x3A, 0xCC, 0x94, 0x65,
|
||||
0x0C, 0x85, 0xA9, 0x3C, 0xC1, 0xE0, 0xAF, 0x51, 0x33, 0x2A, 0x14, 0x48,
|
||||
0x8F, 0x9E, 0x91, 0x5B, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04,
|
||||
0x16, 0x04, 0x14, 0xDA, 0xE9, 0x92, 0x0A, 0xD6, 0x58, 0x28, 0x3A, 0x8B,
|
||||
0x60, 0xCB, 0x20, 0x76, 0x48, 0xB6, 0x5B, 0x0F, 0x10, 0x83, 0x1C, 0x30,
|
||||
0x0B, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x04, 0x04, 0x03, 0x02, 0x01, 0xEE,
|
||||
0x30, 0x33, 0x06, 0x03, 0x55, 0x1D, 0x20, 0x04, 0x2C, 0x30, 0x2A, 0x30,
|
||||
0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x02, 0x01, 0x0C, 0x01,
|
||||
0x01, 0x30, 0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x02,
|
||||
0x01, 0x30, 0x01, 0x30, 0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65,
|
||||
0x03, 0x02, 0x01, 0x30, 0x02, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1D, 0x13,
|
||||
0x01, 0x01, 0xFF, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01,
|
||||
0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
|
||||
0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x97, 0xD9, 0xE9, 0x1F, 0x79, 0x38,
|
||||
0x6C, 0xF3, 0xD0, 0x94, 0xC0, 0xDF, 0xFC, 0xBB, 0x94, 0xE5, 0x82, 0x78,
|
||||
0xA7, 0x96, 0xC1, 0x92, 0x53, 0x18, 0xED, 0x2B, 0xF0, 0xE1, 0x51, 0x72,
|
||||
0xD0, 0xAF, 0x16, 0x3B, 0xEB, 0xCA, 0x98, 0x4F, 0xE8, 0xD8, 0xA8, 0x88,
|
||||
0x62, 0xBE, 0x0A, 0xA2, 0x38, 0x61, 0x83, 0x65, 0x95, 0x6A, 0x9C, 0x13,
|
||||
0x20, 0xD1, 0x7C, 0x6A, 0xA1, 0x52, 0x5B, 0x7C, 0x49, 0x29, 0xB4, 0x85,
|
||||
0x61, 0xA0, 0x98, 0x2D, 0xDA, 0x43, 0x3E, 0xC4, 0xEF, 0x81, 0xCE, 0x5F,
|
||||
0xF6, 0xAD, 0x69, 0xE3, 0xD6, 0xB5, 0x5E, 0x17, 0x21, 0xFF, 0x1D, 0x64,
|
||||
0x18, 0xA4, 0x61, 0x07, 0x55, 0xA1, 0x93, 0x92, 0x1C, 0x8B, 0xCD, 0x9A,
|
||||
0x8F, 0x66, 0xCB, 0xCB, 0x63, 0x2B, 0x3D, 0xB2, 0x31, 0x4F, 0x3A, 0x5E,
|
||||
0x8B, 0x90, 0xCD, 0x91, 0x70, 0xB7, 0xC1, 0x66, 0xB3, 0x38, 0x5C, 0x83,
|
||||
0xB9, 0x5E, 0x30, 0x82, 0x03, 0x0A, 0x30, 0x82, 0x02, 0x75, 0xA0, 0x03,
|
||||
0x02, 0x01, 0x02, 0x02, 0x01, 0x0D, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86,
|
||||
0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x50, 0x31, 0x0B, 0x30,
|
||||
0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18,
|
||||
0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53,
|
||||
0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74,
|
||||
0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44,
|
||||
0x6F, 0x44, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
|
||||
0x10, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65,
|
||||
0x73, 0x20, 0x49, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x30, 0x30, 0x31,
|
||||
0x30, 0x32, 0x36, 0x30, 0x30, 0x31, 0x31, 0x32, 0x35, 0x5A, 0x17, 0x0D,
|
||||
0x30, 0x33, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
|
||||
0x5A, 0x30, 0x56, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
|
||||
0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
|
||||
0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65,
|
||||
0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
|
||||
0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30, 0x0B,
|
||||
0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x04, 0x4E, 0x61, 0x76, 0x79, 0x31,
|
||||
0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x4E, 0x61,
|
||||
0x76, 0x79, 0x20, 0x43, 0x41, 0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09,
|
||||
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
|
||||
0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0x8B, 0xBF,
|
||||
0x07, 0x73, 0x6C, 0x4C, 0xA1, 0x5D, 0xDC, 0xDC, 0x2E, 0x8E, 0x0C, 0xE7,
|
||||
0xBD, 0x8F, 0xC7, 0x1B, 0x31, 0xBE, 0x60, 0x85, 0x5D, 0x75, 0xE5, 0xC9,
|
||||
0xFD, 0xB2, 0x14, 0x62, 0xA8, 0xF4, 0x80, 0x88, 0x7A, 0x14, 0xD7, 0x9B,
|
||||
0x17, 0x5A, 0x79, 0x0E, 0x70, 0xB8, 0xB6, 0x58, 0xC1, 0xBE, 0xEB, 0xB1,
|
||||
0xE5, 0x43, 0x2B, 0x98, 0x63, 0xA2, 0xEB, 0xF3, 0x28, 0x0E, 0x68, 0x95,
|
||||
0x2F, 0xC5, 0x72, 0x56, 0xBF, 0x3B, 0xCF, 0xAF, 0x47, 0xC7, 0x80, 0xCA,
|
||||
0x52, 0x3C, 0x26, 0x1D, 0xAF, 0x0A, 0x39, 0x0F, 0x0B, 0xE4, 0xA7, 0x24,
|
||||
0x97, 0x23, 0x42, 0x8A, 0xAA, 0x2F, 0x2F, 0xE2, 0x16, 0xBE, 0x5F, 0xE2,
|
||||
0x7D, 0xCB, 0xD4, 0xDE, 0xD4, 0x36, 0xA2, 0x53, 0xAA, 0xE2, 0xF8, 0xD8,
|
||||
0x46, 0xEA, 0x6E, 0xF0, 0xD7, 0x66, 0xA5, 0x8E, 0x08, 0x66, 0x5E, 0x94,
|
||||
0x41, 0x27, 0x11, 0xE4, 0xFE, 0xA5, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3,
|
||||
0x81, 0xF1, 0x30, 0x81, 0xEE, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23,
|
||||
0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xDA, 0xE9, 0x92, 0x0A, 0xD6, 0x58,
|
||||
0x28, 0x3A, 0x8B, 0x60, 0xCB, 0x20, 0x76, 0x48, 0xB6, 0x5B, 0x0F, 0x10,
|
||||
0x83, 0x1C, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04,
|
||||
0x14, 0xFB, 0x96, 0xF0, 0x10, 0xC4, 0x37, 0x55, 0xF0, 0xCE, 0xB5, 0xA6,
|
||||
0xE2, 0xF1, 0x19, 0xFF, 0x99, 0x1A, 0xAE, 0x6E, 0x58, 0x30, 0x0B, 0x06,
|
||||
0x03, 0x55, 0x1D, 0x0F, 0x04, 0x04, 0x03, 0x02, 0x01, 0xEE, 0x30, 0x33,
|
||||
0x06, 0x03, 0x55, 0x1D, 0x20, 0x04, 0x2C, 0x30, 0x2A, 0x30, 0x0C, 0x06,
|
||||
0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x02, 0x01, 0x0C, 0x01, 0x01, 0x30,
|
||||
0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x02, 0x01, 0x30,
|
||||
0x01, 0x30, 0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x02,
|
||||
0x01, 0x30, 0x02, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01,
|
||||
0xFF, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x00, 0x30,
|
||||
0x56, 0x06, 0x04, 0x55, 0x1D, 0x1E, 0x01, 0x04, 0x4E, 0x30, 0x4C, 0xA0,
|
||||
0x4A, 0x30, 0x48, 0xA4, 0x46, 0x30, 0x44, 0x31, 0x0B, 0x30, 0x09, 0x06,
|
||||
0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16,
|
||||
0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20,
|
||||
0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C,
|
||||
0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44,
|
||||
0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x04, 0x4E,
|
||||
0x61, 0x76, 0x79, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
|
||||
0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x8C, 0xD1, 0xBD, 0xAD,
|
||||
0xF2, 0xE9, 0xAC, 0xAC, 0x14, 0x22, 0x7B, 0x68, 0x59, 0x09, 0x40, 0xA3,
|
||||
0x7D, 0x14, 0x18, 0x38, 0x03, 0xB9, 0xF2, 0x74, 0xD6, 0x8F, 0x75, 0xD4,
|
||||
0xBB, 0xA4, 0x53, 0xA7, 0x4A, 0x7A, 0x7E, 0x77, 0x3D, 0x0E, 0xA6, 0xB8,
|
||||
0xDE, 0x10, 0x3C, 0x14, 0xFB, 0xDE, 0x15, 0xC5, 0x81, 0xE1, 0x18, 0x31,
|
||||
0xB8, 0xB8, 0xFE, 0x5C, 0x60, 0xC5, 0x3A, 0x80, 0xAB, 0x76, 0x22, 0xF6,
|
||||
0xDF, 0x4A, 0xBD, 0x07, 0xE8, 0x76, 0x45, 0xF9, 0xF3, 0xF7, 0x9F, 0x3B,
|
||||
0x40, 0xD3, 0xAE, 0xAE, 0x64, 0x1A, 0xB0, 0x2C, 0x62, 0xB7, 0xBD, 0xBD,
|
||||
0x6F, 0x80, 0x22, 0x59, 0x99, 0x1B, 0x0E, 0xA6, 0x8E, 0xEB, 0x92, 0xF8,
|
||||
0xCA, 0xA9, 0xD9, 0x55, 0xDD, 0x60, 0x65, 0xA8, 0x5C, 0xAA, 0x56, 0xE0,
|
||||
0x36, 0xD1, 0xC0, 0x09, 0x4F, 0xBA, 0x3B, 0xD7, 0xEC, 0x6F, 0xA2, 0xE3,
|
||||
0x02, 0xBF, 0xFB, 0x45, 0x30, 0x82, 0x02, 0xEF, 0x30, 0x82, 0x02, 0x5A,
|
||||
0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x6C, 0x30, 0x0B, 0x06, 0x09,
|
||||
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x56, 0x31,
|
||||
0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
|
||||
0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55,
|
||||
0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65,
|
||||
0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13,
|
||||
0x03, 0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04,
|
||||
0x0B, 0x13, 0x04, 0x4E, 0x61, 0x76, 0x79, 0x31, 0x10, 0x30, 0x0E, 0x06,
|
||||
0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x4E, 0x61, 0x76, 0x79, 0x20, 0x43,
|
||||
0x41, 0x30, 0x1E, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31,
|
||||
0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x34, 0x31, 0x30,
|
||||
0x31, 0x30, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x81, 0x8B,
|
||||
0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
|
||||
0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F,
|
||||
0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D,
|
||||
0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B,
|
||||
0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
|
||||
0x04, 0x0B, 0x13, 0x04, 0x4E, 0x61, 0x76, 0x79, 0x31, 0x12, 0x30, 0x10,
|
||||
0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x74,
|
||||
0x69, 0x6F, 0x6E, 0x73, 0x31, 0x1B, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04,
|
||||
0x07, 0x13, 0x12, 0x41, 0x6E, 0x6E, 0x61, 0x70, 0x6F, 0x6C, 0x69, 0x73,
|
||||
0x20, 0x4A, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x31, 0x14, 0x30,
|
||||
0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0B, 0x4E, 0x61, 0x76, 0x79,
|
||||
0x20, 0x55, 0x73, 0x65, 0x72, 0x20, 0x31, 0x30, 0x81, 0x9F, 0x30, 0x0D,
|
||||
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05,
|
||||
0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00,
|
||||
0xB7, 0x7E, 0x94, 0x5F, 0xE8, 0x2A, 0xE7, 0xAD, 0x82, 0x16, 0x2C, 0x3D,
|
||||
0x2F, 0x5E, 0x88, 0x67, 0xF0, 0x23, 0x26, 0x15, 0x34, 0x04, 0x1F, 0x63,
|
||||
0x8B, 0xFE, 0xFB, 0xBB, 0x0D, 0xC0, 0x7E, 0xF0, 0x46, 0x82, 0x09, 0xA2,
|
||||
0x91, 0xE0, 0xEA, 0xEF, 0xD0, 0x43, 0xCB, 0x30, 0x45, 0xAC, 0x7C, 0xAC,
|
||||
0xFC, 0xBE, 0x54, 0x79, 0x77, 0xA9, 0x6A, 0x45, 0xF5, 0xBF, 0xE5, 0xEF,
|
||||
0x97, 0x11, 0x63, 0xC2, 0xF7, 0x3C, 0x73, 0x6D, 0xBA, 0x8D, 0xFE, 0xAE,
|
||||
0x28, 0x4A, 0x29, 0xE4, 0xA2, 0x59, 0x0C, 0x8F, 0x1A, 0x57, 0x86, 0xF2,
|
||||
0x42, 0xF7, 0x35, 0x0B, 0xC3, 0xA5, 0x31, 0xD8, 0x19, 0xE2, 0x97, 0x7A,
|
||||
0xA1, 0xF4, 0xE5, 0xDB, 0xCA, 0xF5, 0x54, 0x39, 0x1D, 0x0E, 0xDF, 0x78,
|
||||
0x73, 0xBF, 0x86, 0x97, 0x40, 0xAA, 0x06, 0x8E, 0x8B, 0x6B, 0x0C, 0x06,
|
||||
0x98, 0xD7, 0xD2, 0x1D, 0x45, 0xAA, 0x7F, 0xA5, 0x02, 0x03, 0x01, 0x00,
|
||||
0x01, 0xA3, 0x81, 0x9A, 0x30, 0x81, 0x97, 0x30, 0x1F, 0x06, 0x03, 0x55,
|
||||
0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xFB, 0x96, 0xF0, 0x10,
|
||||
0xC4, 0x37, 0x55, 0xF0, 0xCE, 0xB5, 0xA6, 0xE2, 0xF1, 0x19, 0xFF, 0x99,
|
||||
0x1A, 0xAE, 0x6E, 0x58, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04,
|
||||
0x16, 0x04, 0x14, 0x02, 0x48, 0x78, 0xB9, 0xCC, 0x01, 0x51, 0x31, 0x74,
|
||||
0x7F, 0x39, 0x2A, 0x37, 0xC2, 0x44, 0x93, 0x7E, 0x98, 0x69, 0x80, 0x30,
|
||||
0x0B, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x04, 0x04, 0x03, 0x02, 0x04, 0xF0,
|
||||
0x30, 0x17, 0x06, 0x03, 0x55, 0x1D, 0x20, 0x04, 0x10, 0x30, 0x0E, 0x30,
|
||||
0x0C, 0x06, 0x0A, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x02, 0x01, 0x30,
|
||||
0x01, 0x30, 0x2F, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x28, 0x30, 0x26,
|
||||
0x81, 0x24, 0x4E, 0x61, 0x76, 0x79, 0x31, 0x40, 0x77, 0x61, 0x72, 0x72,
|
||||
0x65, 0x6E, 0x74, 0x6F, 0x6E, 0x2E, 0x61, 0x74, 0x6C, 0x2E, 0x67, 0x65,
|
||||
0x74, 0x72, 0x6F, 0x6E, 0x69, 0x63, 0x73, 0x67, 0x6F, 0x76, 0x2E, 0x63,
|
||||
0x6F, 0x6D, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
|
||||
0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x1D, 0xB0, 0x1C, 0x88, 0x4D,
|
||||
0xA2, 0x68, 0x25, 0x08, 0x8F, 0xA3, 0xAC, 0xC3, 0x18, 0xD5, 0xBF, 0x56,
|
||||
0x7C, 0xA1, 0xF2, 0x7C, 0x76, 0x39, 0x8D, 0x12, 0x42, 0x17, 0xE6, 0x49,
|
||||
0x02, 0x39, 0xAE, 0xBB, 0x75, 0x70, 0x4B, 0x65, 0xEF, 0x0E, 0x3A, 0xC2,
|
||||
0x33, 0xD9, 0x94, 0xDF, 0x5F, 0xA6, 0x12, 0x64, 0x8F, 0x04, 0x76, 0x2C,
|
||||
0xAF, 0x92, 0x37, 0x4C, 0xF1, 0x94, 0x99, 0x52, 0xFD, 0x61, 0x95, 0x00,
|
||||
0x2B, 0x9D, 0x0D, 0x35, 0xB9, 0x7C, 0x6A, 0x4C, 0xBB, 0x8D, 0x8A, 0x7B,
|
||||
0x93, 0x37, 0x02, 0xC8, 0x81, 0x0B, 0xBD, 0xB9, 0x45, 0x51, 0x03, 0xBA,
|
||||
0xD3, 0xF4, 0xBD, 0x72, 0x10, 0x05, 0xE9, 0xC1, 0x6E, 0xFE, 0xC5, 0x76,
|
||||
0x2C, 0x6A, 0x6A, 0x16, 0x2F, 0x0C, 0x54, 0x44, 0x0D, 0x15, 0xC7, 0xA5,
|
||||
0x41, 0xB1, 0x05, 0xE8, 0x4B, 0xF3, 0x60, 0x92, 0xEB, 0xD4, 0xF7, 0x93,
|
||||
0xFF, 0x67, 0x4E, 0xA1, 0x82, 0x02, 0x9E, 0x30, 0x82, 0x01, 0x2A, 0x30,
|
||||
0x81, 0x96, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48,
|
||||
0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x50, 0x31, 0x0B, 0x30, 0x09,
|
||||
0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30,
|
||||
0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E,
|
||||
0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31,
|
||||
0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F,
|
||||
0x44, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x10,
|
||||
0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73,
|
||||
0x20, 0x49, 0x43, 0x41, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31,
|
||||
0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x33, 0x31,
|
||||
0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x14,
|
||||
0x30, 0x12, 0x02, 0x01, 0x09, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x31, 0x33,
|
||||
0x30, 0x32, 0x31, 0x31, 0x36, 0x35, 0x31, 0x5A, 0x30, 0x0B, 0x06, 0x09,
|
||||
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81,
|
||||
0x00, 0x7B, 0xC3, 0x89, 0xC4, 0x94, 0xEA, 0x2A, 0x44, 0x61, 0x96, 0xC9,
|
||||
0x82, 0x05, 0x67, 0xE4, 0x8F, 0xBC, 0xE8, 0x8A, 0x7B, 0xA6, 0xA8, 0xD6,
|
||||
0x82, 0x9A, 0x2B, 0x3D, 0x56, 0x15, 0xEA, 0x3B, 0x58, 0xAC, 0xC6, 0xED,
|
||||
0xCB, 0x67, 0x0B, 0x1F, 0x37, 0x21, 0xF2, 0x50, 0xF3, 0x41, 0x40, 0x09,
|
||||
0x9F, 0xE3, 0xF5, 0xF5, 0x20, 0x0F, 0xEA, 0xC7, 0xA4, 0xD1, 0xBA, 0xAE,
|
||||
0xB2, 0x92, 0x9E, 0x5E, 0x3D, 0xFE, 0xE5, 0xD5, 0x79, 0xAD, 0xA7, 0x29,
|
||||
0x63, 0xFC, 0x39, 0x03, 0xC2, 0x16, 0x95, 0x2C, 0xB0, 0x40, 0xED, 0x2E,
|
||||
0x09, 0xF7, 0x1C, 0x94, 0x6A, 0xB6, 0x92, 0x7D, 0x9C, 0x35, 0x83, 0xEE,
|
||||
0x0D, 0x98, 0xD0, 0xC3, 0x2E, 0xD5, 0x0C, 0xE4, 0xCE, 0x6D, 0x36, 0xC0,
|
||||
0x27, 0x16, 0x3A, 0x34, 0x33, 0x54, 0x96, 0x7D, 0xB4, 0x91, 0x03, 0x39,
|
||||
0x9E, 0x6B, 0x1B, 0x57, 0x8B, 0x9F, 0x4F, 0x10, 0xB2, 0x30, 0x82, 0x01,
|
||||
0x6C, 0x30, 0x81, 0xD8, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A,
|
||||
0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x56, 0x31, 0x0B,
|
||||
0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
|
||||
0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E,
|
||||
0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E,
|
||||
0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03,
|
||||
0x44, 0x6F, 0x44, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B,
|
||||
0x13, 0x04, 0x4E, 0x61, 0x76, 0x79, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
|
||||
0x55, 0x04, 0x03, 0x13, 0x07, 0x4E, 0x61, 0x76, 0x79, 0x20, 0x43, 0x41,
|
||||
0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32,
|
||||
0x35, 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x33, 0x31, 0x30, 0x31, 0x31, 0x31,
|
||||
0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x50, 0x30, 0x12, 0x02, 0x01,
|
||||
0x6D, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x34, 0x32, 0x33, 0x32, 0x31, 0x30,
|
||||
0x39, 0x32, 0x37, 0x5A, 0x30, 0x12, 0x02, 0x01, 0x6F, 0x17, 0x0D, 0x30,
|
||||
0x31, 0x30, 0x34, 0x32, 0x33, 0x32, 0x31, 0x30, 0x39, 0x32, 0x37, 0x5A,
|
||||
0x30, 0x12, 0x02, 0x01, 0x50, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x31, 0x33,
|
||||
0x30, 0x32, 0x32, 0x30, 0x38, 0x32, 0x39, 0x5A, 0x30, 0x12, 0x02, 0x01,
|
||||
0x52, 0x17, 0x0D, 0x30, 0x30, 0x31, 0x31, 0x33, 0x30, 0x32, 0x32, 0x30,
|
||||
0x38, 0x32, 0x39, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
|
||||
0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x3A, 0xFA, 0x41,
|
||||
0x76, 0x90, 0x24, 0x6E, 0x59, 0xEE, 0xF3, 0xC4, 0xA2, 0x77, 0xE0, 0xE4,
|
||||
0x70, 0x69, 0x43, 0xA0, 0x8E, 0x42, 0x9F, 0x1F, 0x58, 0x43, 0x1D, 0xF0,
|
||||
0x4F, 0x1D, 0xE8, 0xF3, 0x36, 0x09, 0x07, 0xE5, 0x3A, 0x84, 0xBB, 0x54,
|
||||
0xBB, 0xB6, 0x55, 0x88, 0x76, 0xC2, 0x42, 0x62, 0xC1, 0xE9, 0x54, 0xA2,
|
||||
0x49, 0xEE, 0x98, 0xDD, 0x07, 0x84, 0x90, 0x5F, 0x7E, 0x94, 0x11, 0x64,
|
||||
0x35, 0x2D, 0xBA, 0x5A, 0xC7, 0x19, 0x46, 0xAF, 0x21, 0x3C, 0x3B, 0xB6,
|
||||
0x0E, 0x28, 0x2B, 0x38, 0x9A, 0xA1, 0xB6, 0x7B, 0x6A, 0xC8, 0xA8, 0xBA,
|
||||
0xC7, 0x9E, 0xD1, 0x31, 0x70, 0x5F, 0xD6, 0x15, 0x03, 0xE6, 0x6C, 0x55,
|
||||
0x85, 0x30, 0xA8, 0x45, 0xBB, 0x28, 0xF3, 0xAC, 0x97, 0x5F, 0x86, 0x21,
|
||||
0x77, 0xEF, 0xEC, 0x17, 0x92, 0xC7, 0xD6, 0xCD, 0xE1, 0x2A, 0x2E, 0xE7,
|
||||
0xF3, 0xED, 0x7F, 0x66, 0x86, 0x31, 0x00 };
|
||||
|
||||
// because most crypto stuff works with byte[] buffers
|
||||
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
|
||||
{
|
||||
if ((array1 == null) && (array2 == null))
|
||||
return;
|
||||
if (array1 == null)
|
||||
Assertion.Fail (msg + " -> First array is NULL");
|
||||
if (array2 == null)
|
||||
Assertion.Fail (msg + " -> Second array is NULL");
|
||||
|
||||
bool a = (array1.Length == array2.Length);
|
||||
if (a) {
|
||||
for (int i = 0; i < array1.Length; i++) {
|
||||
if (array1 [i] != array2 [i]) {
|
||||
a = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array1.Length > 0) {
|
||||
msg += " -> Expected " + BitConverter.ToString (array1, 0);
|
||||
msg += " is different than " + BitConverter.ToString (array2, 0);
|
||||
}
|
||||
Assertion.Assert (msg, a);
|
||||
}
|
||||
|
||||
private const string testfile = "test.spc";
|
||||
|
||||
[TearDown]
|
||||
public void TearDown ()
|
||||
{
|
||||
File.Delete (testfile);
|
||||
}
|
||||
|
||||
private void WriteBuffer (byte[] buffer)
|
||||
{
|
||||
FileStream fs = File.Create (testfile);
|
||||
fs.Write (buffer, 0, buffer.Length);
|
||||
fs.Close ();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadCertificateOnly ()
|
||||
{
|
||||
WriteBuffer (certonly);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
Assertion.AssertEquals ("certonly.Certificates", 1, spc.Certificates.Count);
|
||||
Assertion.AssertEquals ("certonly.CRL", 0, spc.CRLs.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CompareCertificateOnly ()
|
||||
{
|
||||
WriteBuffer (certonly);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
SoftwarePublisherCertificate newspc = new SoftwarePublisherCertificate ();
|
||||
newspc.Certificates.Add (spc.Certificates [0]);
|
||||
byte[] newcertonly = newspc.GetBytes ();
|
||||
AssertEquals ("certonly.compare", certonly, newcertonly);
|
||||
|
||||
SoftwarePublisherCertificate newerspc = new SoftwarePublisherCertificate (newcertonly);
|
||||
Assertion.AssertEquals ("certonly.Certificates", 1, newerspc.Certificates.Count);
|
||||
Assertion.AssertEquals ("certonly.CRL", 0, newerspc.CRLs.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadCRLOnly ()
|
||||
{
|
||||
WriteBuffer (crlonly);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
Assertion.AssertEquals ("crlonly.Certificates", 0, spc.Certificates.Count);
|
||||
Assertion.AssertEquals ("crlonly.CRL", 1, spc.CRLs.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CompareCRLOnly ()
|
||||
{
|
||||
WriteBuffer (crlonly);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
SoftwarePublisherCertificate newspc = new SoftwarePublisherCertificate ();
|
||||
newspc.CRLs.Add (spc.CRLs [0]);
|
||||
byte[] newcrlonly = newspc.GetBytes ();
|
||||
AssertEquals ("crlonly.compare", crlonly, newcrlonly);
|
||||
|
||||
SoftwarePublisherCertificate newerspc = new SoftwarePublisherCertificate (newcrlonly);
|
||||
Assertion.AssertEquals ("crlonly.Certificates", 0, newerspc.Certificates.Count);
|
||||
Assertion.AssertEquals ("crlonly.CRL", 1, newerspc.CRLs.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadNavy ()
|
||||
{
|
||||
WriteBuffer (navy);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
Assertion.AssertEquals ("navy.Certificates", 3, spc.Certificates.Count);
|
||||
Assertion.AssertEquals ("navy.CRL", 2, spc.CRLs.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CompareReadNavy ()
|
||||
{
|
||||
WriteBuffer (navy);
|
||||
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (testfile);
|
||||
SoftwarePublisherCertificate newspc = new SoftwarePublisherCertificate ();
|
||||
foreach (X509Certificate x in spc.Certificates)
|
||||
newspc.Certificates.Add (x);
|
||||
foreach (byte[] crl in spc.CRLs)
|
||||
newspc.CRLs.Add (crl);
|
||||
byte[] newnavy = newspc.GetBytes ();
|
||||
AssertEquals ("navy.compare", navy, newnavy);
|
||||
|
||||
SoftwarePublisherCertificate newerspc = new SoftwarePublisherCertificate (newnavy);
|
||||
Assertion.AssertEquals ("navy.Certificates", 3, newerspc.Certificates.Count);
|
||||
Assertion.AssertEquals ("navy.CRL", 2, newerspc.CRLs.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
|
||||
<!-- NAnt build file for Mono.Security.dll -->
|
||||
|
||||
<project name="Mono.Security" default="build">
|
||||
<property name="debug" value="true"/>
|
||||
<property name="verbose" value="true"/>
|
||||
|
||||
<target name="build">
|
||||
<csc target="library" output="../lib/Mono.Security.dll" debug="${debug}">
|
||||
<!-- arg value="/nowarn:1595"/ -->
|
||||
<!-- arg value="/nowarn:0649"/ --> <!-- field never assigned to -->
|
||||
<!-- arg value="/nowarn:0169"/ --> <!-- field never used -->
|
||||
<!-- arg value="/nowarn:0679"/ --> <!-- internal may be overriden -->
|
||||
|
||||
<sources>
|
||||
<includes name="**/*.cs"/>
|
||||
<excludes name="Test/**"/>
|
||||
<excludes name="**/*Test.cs"/>
|
||||
</sources>
|
||||
</csc>
|
||||
</target>
|
||||
|
||||
<target name="test" depends="build">
|
||||
<nant basedir="Test" target="test"/>
|
||||
</target>
|
||||
|
||||
<target name="clean">
|
||||
<delete file="../lib/Mono.Security.dll" failonerror="false"/>
|
||||
<delete file="../lib/Mono.Security.pdb" failonerror="false"/>
|
||||
<delete file="Test/Mono.Security.dll" failonerror="false"/>
|
||||
<nant basedir="Test" target="clean"/>
|
||||
</target>
|
||||
</project>
|
||||
@@ -0,0 +1,257 @@
|
||||
//
|
||||
// ASN1.cs: Abstract Syntax Notation 1 - micro-parser and generator
|
||||
//
|
||||
// Author:
|
||||
// Sebastien Pouliot ([email protected])
|
||||
//
|
||||
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Mono.Security {
|
||||
|
||||
// References:
|
||||
// a. ITU ASN.1 standards (free download)
|
||||
// http://www.itu.int/ITU-T/studygroups/com17/languages/
|
||||
|
||||
public class ASN1 {
|
||||
|
||||
protected byte m_nTag;
|
||||
protected byte[] m_aValue;
|
||||
protected ArrayList elist;
|
||||
|
||||
public ASN1 () : this (0x00, null) {}
|
||||
|
||||
public ASN1 (byte tag) : this (tag, null) {}
|
||||
|
||||
public ASN1 (byte tag, byte[] data)
|
||||
{
|
||||
m_nTag = tag;
|
||||
m_aValue = data;
|
||||
}
|
||||
|
||||
public ASN1 (byte[] data)
|
||||
{
|
||||
m_nTag = data [0];
|
||||
|
||||
int nLenLength = 0;
|
||||
int nLength = data [1];
|
||||
|
||||
if (nLength > 0x80) {
|
||||
// composed length
|
||||
nLenLength = nLength - 0x80;
|
||||
nLength = 0;
|
||||
for (int i = 0; i < nLenLength; i++) {
|
||||
nLength *= 256;
|
||||
nLength += data [i + 2];
|
||||
}
|
||||
}
|
||||
|
||||
m_aValue = new byte [nLength];
|
||||
Array.Copy (data, (2 + nLenLength), m_aValue, 0, nLength);
|
||||
|
||||
if ((m_nTag & 0x20) == 0x20) {
|
||||
int nStart = (2 + nLenLength);
|
||||
Decode (data, ref nStart, data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count {
|
||||
get {
|
||||
if (elist == null)
|
||||
return 0;
|
||||
return elist.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public byte Tag {
|
||||
get { return m_nTag; }
|
||||
}
|
||||
|
||||
public int Length {
|
||||
get {
|
||||
if (m_aValue != null)
|
||||
return m_aValue.Length;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Value {
|
||||
get {
|
||||
if (m_aValue == null)
|
||||
GetBytes ();
|
||||
return (byte[]) m_aValue.Clone ();
|
||||
}
|
||||
set {
|
||||
if (value != null)
|
||||
m_aValue = (byte[]) value.Clone ();
|
||||
}
|
||||
}
|
||||
|
||||
private bool CompareArray (byte[] array1, byte[] array2)
|
||||
{
|
||||
bool bResult = (array1.Length == array2.Length);
|
||||
if (bResult) {
|
||||
for (int i = 0; i < array1.Length; i++) {
|
||||
if (array1[i] != array2[i])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return bResult;
|
||||
}
|
||||
|
||||
public bool Equals (byte[] asn1)
|
||||
{
|
||||
return CompareArray (this.GetBytes (), asn1);
|
||||
}
|
||||
|
||||
public bool CompareValue (byte[] aValue)
|
||||
{
|
||||
return CompareArray (m_aValue, aValue);
|
||||
}
|
||||
|
||||
public virtual ASN1 Add (ASN1 asn1)
|
||||
{
|
||||
if (asn1 != null) {
|
||||
if (elist == null)
|
||||
elist = new ArrayList ();
|
||||
elist.Add (asn1);
|
||||
}
|
||||
return asn1;
|
||||
}
|
||||
|
||||
public virtual byte[] GetBytes ()
|
||||
{
|
||||
byte[] val = null;
|
||||
if (m_aValue != null) {
|
||||
val = m_aValue;
|
||||
}
|
||||
else if (Count > 0) {
|
||||
int esize = 0;
|
||||
ArrayList al = new ArrayList ();
|
||||
foreach (ASN1 a in elist) {
|
||||
byte[] item = a.GetBytes ();
|
||||
al.Add (item);
|
||||
esize += item.Length;
|
||||
}
|
||||
val = new byte [esize];
|
||||
int pos = 0;
|
||||
for (int i=0; i < elist.Count; i++) {
|
||||
byte[] item = (byte[]) al[i];
|
||||
Array.Copy (item, 0, val, pos, item.Length);
|
||||
pos += item.Length;
|
||||
}
|
||||
}
|
||||
|
||||
byte[] der;
|
||||
int nLengthLen = 0;
|
||||
|
||||
if (val != null) {
|
||||
int nLength = val.Length;
|
||||
// special for length > 127
|
||||
if (nLength > 127) {
|
||||
if (nLength < 256) {
|
||||
der = new byte [3 + nLength];
|
||||
Array.Copy (val, 0, der, 3, nLength);
|
||||
nLengthLen += 0x81;
|
||||
der[2] = (byte)(nLength);
|
||||
}
|
||||
else {
|
||||
der = new byte [4 + nLength];
|
||||
Array.Copy (val, 0, der, 4, nLength);
|
||||
nLengthLen += 0x82;
|
||||
der[2] = (byte)(nLength / 256);
|
||||
der[3] = (byte)(nLength % 256);
|
||||
}
|
||||
}
|
||||
else {
|
||||
der = new byte [2 + nLength];
|
||||
Array.Copy (val, 0, der, 2, nLength);
|
||||
nLengthLen = nLength;
|
||||
}
|
||||
if (m_aValue == null)
|
||||
m_aValue = val;
|
||||
}
|
||||
else
|
||||
der = new byte[2];
|
||||
|
||||
der[0] = m_nTag;
|
||||
der[1] = (byte)nLengthLen;
|
||||
|
||||
return der;
|
||||
}
|
||||
|
||||
// Note: Recursive
|
||||
protected void Decode (byte[] asn1, ref int anPos, int anLength)
|
||||
{
|
||||
byte nTag;
|
||||
int nLength;
|
||||
byte[] aValue;
|
||||
|
||||
// minimum is 2 bytes (tag + length of 0)
|
||||
while (anPos < anLength - 1) {
|
||||
int nPosOri = anPos;
|
||||
DecodeTLV (asn1, ref anPos, out nTag, out nLength, out aValue);
|
||||
|
||||
ASN1 elm = Add (new ASN1 (nTag, aValue));
|
||||
|
||||
if ((nTag & 0x20) == 0x20) {
|
||||
int nConstructedPos = anPos;
|
||||
elm.Decode (asn1, ref nConstructedPos, nConstructedPos + nLength);
|
||||
}
|
||||
anPos += nLength; // value length
|
||||
}
|
||||
}
|
||||
|
||||
// TLV : Tag - Length - Value
|
||||
protected void DecodeTLV (byte[] asn1, ref int anPos, out byte anTag, out int anLength, out byte[] aValue)
|
||||
{
|
||||
anTag = asn1 [anPos++];
|
||||
anLength = asn1 [anPos++];
|
||||
|
||||
// special case where L contains the Length of the Length + 0x80
|
||||
if ((anLength & 0x80) == 0x80) {
|
||||
int nLengthLen = anLength & 0x7F;
|
||||
anLength = 0;
|
||||
for (int i = 0; i < nLengthLen; i++)
|
||||
anLength = anLength * 256 + asn1 [anPos++];
|
||||
}
|
||||
|
||||
aValue = new byte [anLength];
|
||||
Array.Copy (asn1, anPos, aValue, 0, anLength);
|
||||
}
|
||||
|
||||
public ASN1 this [int index] {
|
||||
get {
|
||||
try {
|
||||
if (index >= elist.Count)
|
||||
return null;
|
||||
return (ASN1) elist [index];
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1 Element (int index, byte anTag)
|
||||
{
|
||||
try {
|
||||
if (index >= elist.Count)
|
||||
return null;
|
||||
|
||||
ASN1 elm = (ASN1) elist [index];
|
||||
if (elm.Tag == anTag)
|
||||
return elm;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
//
|
||||
// ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines
|
||||
//
|
||||
// Author:
|
||||
// Sebastien Pouliot ([email protected])
|
||||
//
|
||||
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Mono.Security {
|
||||
|
||||
// References:
|
||||
// a. ITU ASN.1 standards (free download)
|
||||
// http://www.itu.int/ITU-T/studygroups/com17/languages/
|
||||
|
||||
public class ASN1Convert {
|
||||
|
||||
// RFC3280, section 4.2.1.5
|
||||
// CAs conforming to this profile MUST always encode certificate
|
||||
// validity dates through the year 2049 as UTCTime; certificate validity
|
||||
// dates in 2050 or later MUST be encoded as GeneralizedTime.
|
||||
static public ASN1 FromDateTime (DateTime dt)
|
||||
{
|
||||
if (dt.Year < 2050) {
|
||||
// UTCTIME
|
||||
return new ASN1 (0x17, Encoding.ASCII.GetBytes (dt.ToString ("yyMMddHHmmss") + "Z"));
|
||||
}
|
||||
else {
|
||||
// GENERALIZEDTIME
|
||||
return new ASN1 (0x18, Encoding.ASCII.GetBytes (dt.ToString ("yyyyMMddHHmmss") + "Z"));
|
||||
}
|
||||
}
|
||||
|
||||
static public ASN1 FromInt32 (Int32 value)
|
||||
{
|
||||
byte[] integer = BitConverter.GetBytes (value);
|
||||
int x = 3;
|
||||
while (integer [x] == 0x00)
|
||||
x--;
|
||||
ASN1 asn1 = new ASN1 (0x02);
|
||||
if (x == 3)
|
||||
asn1.Value = integer;
|
||||
else {
|
||||
byte[] smallerInt = new byte [x + 1];
|
||||
Array.Copy (integer, 0, smallerInt, 0, smallerInt.Length);
|
||||
asn1.Value = smallerInt;
|
||||
}
|
||||
return asn1;
|
||||
}
|
||||
|
||||
static public ASN1 FromOID (string oid)
|
||||
{
|
||||
return new ASN1 (CryptoConfig.EncodeOID (oid));
|
||||
}
|
||||
|
||||
static public ASN1 FromUnsignedBigInteger (byte[] integer)
|
||||
{
|
||||
if (integer [0] == 0x00) {
|
||||
// this first byte is added so we're sure it's an unsigned integer
|
||||
// however we can't feed it into RSAParameters or DSAParameters
|
||||
int length = integer.Length + 1;
|
||||
byte[] uinteger = new byte [length];
|
||||
Array.Copy (integer, 0, uinteger, 1, length);
|
||||
integer = uinteger;
|
||||
}
|
||||
return new ASN1 (0x02, integer);
|
||||
}
|
||||
|
||||
static public int ToInt32 (ASN1 asn1)
|
||||
{
|
||||
if (asn1.Tag != 0x02)
|
||||
throw new NotSupportedException ("Only integer can be converted");
|
||||
int x = 0;
|
||||
for (int i=0; i < asn1.Value.Length; i++)
|
||||
x = (x << 8) + asn1.Value [i];
|
||||
return x;
|
||||
}
|
||||
|
||||
// Convert a binary encoded OID to human readable string representation of
|
||||
// an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann.
|
||||
static public string ToOID (ASN1 asn1)
|
||||
{
|
||||
byte[] aOID = asn1.Value;
|
||||
StringBuilder sb = new StringBuilder ();
|
||||
// Pick apart the OID
|
||||
byte x = (byte) (aOID[0] / 40);
|
||||
byte y = (byte) (aOID[0] % 40);
|
||||
if (x > 2) {
|
||||
// Handle special case for large y if x = 2
|
||||
y += (byte) ((x - 2) * 40);
|
||||
x = 2;
|
||||
}
|
||||
sb.Append (x.ToString ());
|
||||
sb.Append (".");
|
||||
sb.Append (y.ToString ());
|
||||
ulong val = 0;
|
||||
for (x = 1; x < aOID.Length; x++) {
|
||||
val = ((val << 7) | ((byte) (aOID [x] & 0x7F)));
|
||||
if ( !((aOID [x] & 0x80) == 0x80)) {
|
||||
sb.Append (".");
|
||||
sb.Append (val.ToString ());
|
||||
val = 0;
|
||||
}
|
||||
}
|
||||
return sb.ToString ();
|
||||
}
|
||||
|
||||
static public DateTime ToDateTime (ASN1 time)
|
||||
{
|
||||
string t = Encoding.ASCII.GetString (time.Value);
|
||||
// to support both UTCTime and GeneralizedTime (and not so common format)
|
||||
string mask = null;
|
||||
switch (t.Length) {
|
||||
case 11:
|
||||
mask = "yyMMddHHmmZ"; // illegal I think ... must check
|
||||
break;
|
||||
case 13:
|
||||
// RFC3280: 4.1.2.5.1 UTCTime
|
||||
int year = Convert.ToInt16 (t.Substring (0, 2));
|
||||
// Where YY is greater than or equal to 50, the
|
||||
// year SHALL be interpreted as 19YY; and
|
||||
// Where YY is less than 50, the year SHALL be
|
||||
// interpreted as 20YY.
|
||||
if (year >= 50)
|
||||
t = "19" + t;
|
||||
else
|
||||
t = "20" + t;
|
||||
mask = "yyyyMMddHHmmssZ";
|
||||
break;
|
||||
case 15:
|
||||
mask = "yyyyMMddHHmmssZ"; // GeneralizedTime
|
||||
break;
|
||||
}
|
||||
return DateTime.ParseExact (t, mask, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
2003-03-06 Sebastien Pouliot <[email protected]>
|
||||
|
||||
* ASN1.cs: Updated ASN1 class (from corlib). This file will,
|
||||
eventually, be automagically synched with corlib.
|
||||
* ASN1Convert.cs: New. Helper class to convert from and to
|
||||
ASN1 structures.
|
||||
* PKCS7.cs: New. Basic implementation of CMS (Cryptographic
|
||||
Message Syntax Standard).
|
||||
|
||||
|
||||
@@ -0,0 +1,443 @@
|
||||
//
|
||||
// PKCS7.cs: PKCS #7 - Cryptographic Message Syntax Standard
|
||||
// http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/index.html
|
||||
//
|
||||
// Author:
|
||||
// Sebastien Pouliot ([email protected])
|
||||
//
|
||||
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
|
||||
//
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Mono.Security {
|
||||
|
||||
public class PKCS7 {
|
||||
|
||||
// pkcs 1
|
||||
public const string rsaEncryption = "1.2.840.113549.1.1.1";
|
||||
// pkcs 7
|
||||
public const string data = "1.2.840.113549.1.7.1";
|
||||
public const string signedData = "1.2.840.113549.1.7.2";
|
||||
// pkcs 9
|
||||
public const string contentType = "1.2.840.113549.1.9.3";
|
||||
public const string messageDigest = "1.2.840.113549.1.9.4";
|
||||
|
||||
public PKCS7() {
|
||||
}
|
||||
|
||||
static public ASN1 Attribute (string oid, ASN1 value)
|
||||
{
|
||||
ASN1 attr = new ASN1 (0x30);
|
||||
attr.Add (ASN1Convert.FromOID (oid));
|
||||
ASN1 aset = attr.Add (new ASN1 (0x31));
|
||||
aset.Add (value);
|
||||
return attr;
|
||||
}
|
||||
|
||||
static public ASN1 AlgorithmIdentifier (string oid)
|
||||
{
|
||||
ASN1 ai = new ASN1 (0x30);
|
||||
ai.Add (ASN1Convert.FromOID (oid));
|
||||
ai.Add (new ASN1 (0x05)); // NULL
|
||||
return ai;
|
||||
}
|
||||
|
||||
static public ASN1 AlgorithmIdentifier (string oid, ASN1 param)
|
||||
{
|
||||
ASN1 ai = new ASN1 (0x30);
|
||||
ai.Add (ASN1Convert.FromOID (oid));
|
||||
ai.Add (param);
|
||||
return ai;
|
||||
}
|
||||
|
||||
/*
|
||||
* IssuerAndSerialNumber ::= SEQUENCE {
|
||||
* issuer Name,
|
||||
* serialNumber CertificateSerialNumber
|
||||
* }
|
||||
*/
|
||||
static public ASN1 IssuerAndSerialNumber (X509Certificate x509)
|
||||
{
|
||||
ASN1 issuer = null;
|
||||
ASN1 serial = null;
|
||||
ASN1 cert = new ASN1 (x509.GetRawCertData ());
|
||||
int tbs = 0;
|
||||
bool flag = false;
|
||||
while (tbs < cert[0][0].Count) {
|
||||
ASN1 e = cert[0][0][tbs++];
|
||||
if (e.Tag == 0x02)
|
||||
serial = e;
|
||||
else if (e.Tag == 0x30) {
|
||||
if (flag) {
|
||||
issuer = e;
|
||||
break;
|
||||
}
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
ASN1 iasn = new ASN1 (0x30);
|
||||
iasn.Add (issuer);
|
||||
iasn.Add (serial);
|
||||
return iasn;
|
||||
}
|
||||
|
||||
/*
|
||||
* ContentInfo ::= SEQUENCE {
|
||||
* contentType ContentType,
|
||||
* content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
|
||||
* }
|
||||
* ContentType ::= OBJECT IDENTIFIER
|
||||
*/
|
||||
public class ContentInfo {
|
||||
|
||||
private string contentType;
|
||||
private ASN1 content;
|
||||
|
||||
public ContentInfo ()
|
||||
{
|
||||
content = new ASN1 (0xA0);
|
||||
}
|
||||
|
||||
public ContentInfo (string oid) : this ()
|
||||
{
|
||||
contentType = oid;
|
||||
}
|
||||
|
||||
public ContentInfo (byte[] data)
|
||||
: this (new ASN1 (data)) {}
|
||||
|
||||
public ContentInfo (ASN1 asn1)
|
||||
{
|
||||
// SEQUENCE with 1 or 2 elements
|
||||
if ((asn1.Tag != 0x30) || ((asn1.Count < 1) && (asn1.Count > 2)))
|
||||
throw new ArgumentException ("Invalid ASN1");
|
||||
if (asn1[0].Tag != 0x06)
|
||||
throw new ArgumentException ("Invalid contentType");
|
||||
contentType = ASN1Convert.ToOID (asn1[0]);
|
||||
if (asn1.Count > 1) {
|
||||
if (asn1[1].Tag != 0xA0)
|
||||
throw new ArgumentException ("Invalid content");
|
||||
content = asn1[1];
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1 ASN1 {
|
||||
get { return GetASN1(); }
|
||||
}
|
||||
|
||||
public ASN1 Content {
|
||||
get { return content; }
|
||||
}
|
||||
|
||||
public string ContentType {
|
||||
get { return contentType; }
|
||||
set { contentType = value; }
|
||||
}
|
||||
|
||||
internal ASN1 GetASN1 ()
|
||||
{
|
||||
// ContentInfo ::= SEQUENCE {
|
||||
ASN1 contentInfo = new ASN1 (0x30);
|
||||
// contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER
|
||||
contentInfo.Add (ASN1Convert.FromOID (contentType));
|
||||
// content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
|
||||
if ((content != null) && (content.Count > 0))
|
||||
contentInfo.Add (content);
|
||||
return contentInfo;
|
||||
}
|
||||
|
||||
public byte[] GetBytes ()
|
||||
{
|
||||
return GetASN1 ().GetBytes ();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SignedData ::= SEQUENCE {
|
||||
* version Version,
|
||||
* digestAlgorithms DigestAlgorithmIdentifiers,
|
||||
* contentInfo ContentInfo,
|
||||
* certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
|
||||
* crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
|
||||
* signerInfos SignerInfos
|
||||
* }
|
||||
*/
|
||||
public class SignedData {
|
||||
private byte version;
|
||||
private string hashAlgorithm;
|
||||
private ContentInfo contentInfo;
|
||||
private X509CertificateCollection certs;
|
||||
private ArrayList crls;
|
||||
private SignerInfo signerInfo;
|
||||
private ASN1 mda;
|
||||
|
||||
public SignedData ()
|
||||
{
|
||||
version = 1;
|
||||
contentInfo = new ContentInfo ();
|
||||
certs = new X509CertificateCollection ();
|
||||
crls = new ArrayList ();
|
||||
signerInfo = new SignerInfo ();
|
||||
}
|
||||
|
||||
public SignedData (byte[] data)
|
||||
: this (new ASN1 (data)) {}
|
||||
|
||||
public SignedData (ASN1 asn1)
|
||||
{
|
||||
if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 4))
|
||||
throw new ArgumentException ("Invalid SignedData");
|
||||
|
||||
if (asn1[0][0].Tag != 0x02)
|
||||
throw new ArgumentException ("Invalid version");
|
||||
version = asn1[0][0].Value[0];
|
||||
|
||||
// digestInfo
|
||||
|
||||
contentInfo = new ContentInfo (asn1[0][2]);
|
||||
|
||||
int n = 3;
|
||||
certs = new X509CertificateCollection ();
|
||||
if (asn1[0][n].Tag == 0xA0) {
|
||||
for (int i=0; i < asn1[0][n].Count; i++)
|
||||
certs.Add (new X509Certificate (asn1[0][n][i].GetBytes ()));
|
||||
n++;
|
||||
}
|
||||
|
||||
crls = new ArrayList ();
|
||||
if (asn1[0][n].Tag == 0xA1) {
|
||||
for (int i=0; i < asn1[0][n].Count; i++)
|
||||
crls.Add (asn1[0][n][i].GetBytes ());
|
||||
n++;
|
||||
}
|
||||
|
||||
if (asn1[0][n].Count > 0)
|
||||
signerInfo = new SignerInfo (asn1[0][n]);
|
||||
else
|
||||
signerInfo = new SignerInfo ();
|
||||
}
|
||||
|
||||
public ASN1 ASN1 {
|
||||
get { return GetASN1(); }
|
||||
}
|
||||
|
||||
public X509CertificateCollection Certificates {
|
||||
get { return certs; }
|
||||
}
|
||||
|
||||
public ContentInfo ContentInfo {
|
||||
get { return contentInfo; }
|
||||
}
|
||||
|
||||
public ArrayList CRLs {
|
||||
get { return crls; }
|
||||
}
|
||||
|
||||
public string HashName {
|
||||
get { return hashAlgorithm; }
|
||||
// todo add validation
|
||||
set {
|
||||
hashAlgorithm = value;
|
||||
signerInfo.HashName = value;
|
||||
}
|
||||
}
|
||||
|
||||
public SignerInfo SignerInfo {
|
||||
get { return signerInfo; }
|
||||
}
|
||||
|
||||
public byte Version {
|
||||
get { return version; }
|
||||
set { version = value; }
|
||||
}
|
||||
|
||||
internal ASN1 GetASN1 ()
|
||||
{
|
||||
// SignedData ::= SEQUENCE {
|
||||
ASN1 signedData = new ASN1 (0x30);
|
||||
// version Version -> Version ::= INTEGER
|
||||
byte[] ver = { version };
|
||||
signedData.Add (new ASN1 (0x02, ver));
|
||||
// digestAlgorithms DigestAlgorithmIdentifiers -> DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
|
||||
ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31));
|
||||
if (hashAlgorithm != null) {
|
||||
string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
|
||||
digestAlgorithms.Add (AlgorithmIdentifier (hashOid));
|
||||
}
|
||||
|
||||
// contentInfo ContentInfo,
|
||||
ASN1 ci = contentInfo.ASN1;
|
||||
signedData.Add (ci);
|
||||
if ((mda == null) && (hashAlgorithm != null)) {
|
||||
// automatically add the messageDigest authenticated attribute
|
||||
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
|
||||
byte[] idcHash = ha.ComputeHash (ci[1][0].Value);
|
||||
ASN1 md = new ASN1 (0x30);
|
||||
mda = Attribute (messageDigest, md.Add (new ASN1 (0x04, idcHash)));
|
||||
signerInfo.AuthenticatedAttributes.Add (mda);
|
||||
}
|
||||
|
||||
// certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
|
||||
if (certs.Count > 0) {
|
||||
ASN1 a0 = signedData.Add (new ASN1 (0xA0));
|
||||
foreach (X509Certificate x in certs)
|
||||
a0.Add (new ASN1 (x.GetRawCertData ()));
|
||||
}
|
||||
// crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
|
||||
if (crls.Count > 0) {
|
||||
ASN1 a1 = signedData.Add (new ASN1 (0xA1));
|
||||
foreach (byte[] crl in crls)
|
||||
a1.Add (new ASN1 (crl));
|
||||
}
|
||||
// signerInfos SignerInfos -> SignerInfos ::= SET OF SignerInfo
|
||||
ASN1 signerInfos = signedData.Add (new ASN1 (0x31));
|
||||
if (signerInfo.Key != null)
|
||||
signerInfos.Add (signerInfo.ASN1);
|
||||
return signedData;
|
||||
}
|
||||
|
||||
public byte[] GetBytes ()
|
||||
{
|
||||
return GetASN1 ().GetBytes ();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SignerInfo ::= SEQUENCE {
|
||||
* version Version,
|
||||
* issuerAndSerialNumber IssuerAndSerialNumber,
|
||||
* digestAlgorithm DigestAlgorithmIdentifier,
|
||||
* authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
|
||||
* digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
|
||||
* encryptedDigest EncryptedDigest,
|
||||
* unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
|
||||
* }
|
||||
*/
|
||||
public class SignerInfo {
|
||||
|
||||
private byte version;
|
||||
private X509Certificate x509;
|
||||
private string hashAlgorithm;
|
||||
private AsymmetricAlgorithm key;
|
||||
private ArrayList authenticatedAttributes;
|
||||
private ArrayList unauthenticatedAttributes;
|
||||
private byte[] signature;
|
||||
|
||||
public SignerInfo ()
|
||||
{
|
||||
version = 1;
|
||||
authenticatedAttributes = new ArrayList ();
|
||||
unauthenticatedAttributes = new ArrayList ();
|
||||
}
|
||||
|
||||
public SignerInfo (byte[] data)
|
||||
: this (new ASN1 (data)) {}
|
||||
|
||||
public SignerInfo (ASN1 asn1)
|
||||
{
|
||||
if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 5))
|
||||
throw new ArgumentException ("Invalid SignedData");
|
||||
|
||||
if (asn1[0][0].Tag != 0x02)
|
||||
throw new ArgumentException ("Invalid version");
|
||||
version = asn1[0][0].Value[0];
|
||||
|
||||
// TODO: INCOMPLETE
|
||||
}
|
||||
|
||||
public ASN1 ASN1 {
|
||||
get { return GetASN1(); }
|
||||
}
|
||||
|
||||
public ArrayList AuthenticatedAttributes {
|
||||
get { return authenticatedAttributes; }
|
||||
}
|
||||
|
||||
public X509Certificate Certificate {
|
||||
get { return x509; }
|
||||
set { x509 = value; }
|
||||
}
|
||||
|
||||
public string HashName {
|
||||
get { return hashAlgorithm; }
|
||||
// todo add validation
|
||||
set { hashAlgorithm = value; }
|
||||
}
|
||||
|
||||
public AsymmetricAlgorithm Key {
|
||||
get { return key; }
|
||||
set { key = value; }
|
||||
}
|
||||
|
||||
public byte[] Signature {
|
||||
get { return (byte[]) signature.Clone (); }
|
||||
}
|
||||
|
||||
public ArrayList UnauthenticatedAttributes {
|
||||
get { return unauthenticatedAttributes; }
|
||||
}
|
||||
|
||||
public byte Version {
|
||||
get { return version; }
|
||||
set { version = value; }
|
||||
}
|
||||
|
||||
internal ASN1 GetASN1 ()
|
||||
{
|
||||
if ((key == null) || (hashAlgorithm == null))
|
||||
return null;
|
||||
byte[] ver = { version };
|
||||
ASN1 signerInfo = new ASN1 (0x30);
|
||||
// version Version -> Version ::= INTEGER
|
||||
signerInfo.Add (new ASN1 (0x02, ver));
|
||||
// issuerAndSerialNumber IssuerAndSerialNumber,
|
||||
signerInfo.Add (PKCS7.IssuerAndSerialNumber (x509));
|
||||
// digestAlgorithm DigestAlgorithmIdentifier,
|
||||
string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
|
||||
signerInfo.Add (AlgorithmIdentifier (hashOid));
|
||||
// authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
|
||||
ASN1 aa = signerInfo.Add (new ASN1 (0xA0));
|
||||
if (authenticatedAttributes.Count > 0) {
|
||||
foreach (ASN1 attr in authenticatedAttributes)
|
||||
aa.Add (attr);
|
||||
}
|
||||
// digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
|
||||
if (key is RSA) {
|
||||
signerInfo.Add (AlgorithmIdentifier (PKCS7.rsaEncryption));
|
||||
|
||||
RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key);
|
||||
r.SetHashAlgorithm (hashAlgorithm);
|
||||
byte[] tbs = aa.GetBytes ();
|
||||
tbs [0] = 0x31; // not 0xA0 for signature
|
||||
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
|
||||
byte[] tbsHash = ha.ComputeHash (tbs);
|
||||
signature = r.CreateSignature (tbsHash);
|
||||
}
|
||||
else if (key is DSA) {
|
||||
throw new NotImplementedException ("not yet");
|
||||
}
|
||||
else
|
||||
throw new CryptographicException ("Unknown assymetric algorithm");
|
||||
// encryptedDigest EncryptedDigest,
|
||||
signerInfo.Add (new ASN1 (0x04, signature));
|
||||
// unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
|
||||
if (unauthenticatedAttributes.Count > 0) {
|
||||
ASN1 ua = signerInfo.Add (new ASN1 (0xA1));
|
||||
foreach (ASN1 attr in unauthenticatedAttributes)
|
||||
ua.Add (attr);
|
||||
}
|
||||
return signerInfo;
|
||||
}
|
||||
|
||||
public byte[] GetBytes ()
|
||||
{
|
||||
return GetASN1 ().GetBytes ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
2003-03-06 Sebastien Pouliot <[email protected]>
|
||||
|
||||
* Mono.Security_test.build: New. NAnt build file for unit tests.
|
||||
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
|
||||
<!-- NAnt build file for Mono.Security_test.dll -->
|
||||
<!-- Target build (default) builds tests -->
|
||||
<!-- Target test runs tests -->
|
||||
|
||||
<project name="Mono.Security_test" default="build">
|
||||
<property name="debug" value="true"/>
|
||||
<property name="nunit_home" value="..\..\..\nunit20"/>
|
||||
|
||||
<target name="build">
|
||||
</target>
|
||||
|
||||
<target name="assemblies">
|
||||
<csc target="library" output="Mono.Security_test.dll" debug="${debug}">
|
||||
<sources>
|
||||
<includes name="**/*.cs"/>
|
||||
</sources>
|
||||
<references basedir="..\..\..\nunit20">
|
||||
<includes name="NUnit.Framework.dll"/>
|
||||
</references>
|
||||
<arg value="/r:..\..\lib\Mono.Security.dll"/>
|
||||
<!-- arg value="/nowarn:618"/>
|
||||
<arg value="/nowarn:672"/>
|
||||
<arg value="/nowarn:1595"/ -->
|
||||
</csc>
|
||||
<copy file="../../lib/Mono.Security.dll" tofile="Mono.Security.dll"/>
|
||||
</target>
|
||||
|
||||
<target name="test" depends="assemblies">
|
||||
<exec program="..\..\..\nunit20\nunit-console.exe" commandline="Mono.Security_test.dll" failonerror="false"/>
|
||||
</target>
|
||||
|
||||
<target name="clean">
|
||||
<delete file="Mono.Security_test.dll" failonerror="false"/>
|
||||
<delete file="Mono.Security_test.pdb" failonerror="false"/>
|
||||
<delete file="Mono.Security.dll" failonerror="false"/>
|
||||
<delete file="Mono.Security.pdb" failonerror="false"/>
|
||||
</target>
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
./Mono.Security/ASN1.cs
|
||||
./Mono.Security/ASN1Convert.cs
|
||||
./Mono.Security/PKCS7.cs
|
||||
./Mono.Security.Authenticode/SoftwarePublisherCertificate.cs
|
||||
@@ -0,0 +1,16 @@
|
||||
topdir = ../..
|
||||
|
||||
TEST_DIR= Test
|
||||
LIBRARY = $(topdir)/class/lib/Mono.Security.dll
|
||||
|
||||
LIB_LIST = list.unix
|
||||
LIB_FLAGS = -r mscorlib
|
||||
|
||||
SOURCES_INCLUDE=*.cs
|
||||
SOURCES_EXCLUDE=\
|
||||
./Test*
|
||||
|
||||
|
||||
export MONO_PATH_PREFIX = $(topdir)/class/lib:
|
||||
|
||||
include $(topdir)/class/library.make
|
||||
@@ -1,3 +1,12 @@
|
||||
2003-03-06 Jackson Harper <[email protected]>
|
||||
|
||||
* makefile.gnu: Add file
|
||||
* list.unix: Add file
|
||||
|
||||
2003-03-02 Jackson Harper <[email protected]>
|
||||
|
||||
* PEAPI.cs: Do not add file extensions to output names
|
||||
|
||||
2003-03-02 Jackson Harper <[email protected]>
|
||||
|
||||
* PEAPI.cs: Add file
|
||||
|
||||
@@ -5728,7 +5728,9 @@ namespace PEAPI
|
||||
if (!dirName.EndsWith("\\")) result += "\\";
|
||||
}
|
||||
result += name;
|
||||
if (isDLL) result += ".dll"; else result += ".exe";
|
||||
|
||||
// if (isDLL) result += ".dll"; else result += ".exe";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
PEAPI.cs
|
||||
@@ -0,0 +1,14 @@
|
||||
topdir = ../..
|
||||
|
||||
TEST_DIR= Test
|
||||
LIBRARY = $(topdir)/class/lib/PEAPI.dll
|
||||
|
||||
LIB_LIST = list.unix
|
||||
LIB_FLAGS = -r corlib -r System
|
||||
|
||||
SOURCES_INCLUDE=PEAPI.cs
|
||||
SOURCES_EXCLUDE=
|
||||
|
||||
export MONO_PATH_PREFIX = $(topdir)/class/lib:
|
||||
|
||||
include $(topdir)/class/library.make
|
||||
@@ -1,3 +1,14 @@
|
||||
2003-03-06 Aleksey Demakov <[email protected]>
|
||||
|
||||
* DataTableCollection.cs: The Contains (string name),
|
||||
IndexOf (string name) methods, and item[string name] indexer
|
||||
now behave more like .NET with respect to case-sensitivity.
|
||||
That is if there is an exactly matching item then this one is used.
|
||||
If there is only one item differing in case then it used.
|
||||
If there are more than one item differing in case then
|
||||
Contains returns false, IndexOf returns -1, and item[]
|
||||
throws an ArgumentException.
|
||||
|
||||
2003-02-28 Ville Palo <[email protected]>
|
||||
|
||||
* ExpressionElement.cs: More implementation.
|
||||
|
||||
@@ -44,13 +44,9 @@ namespace System.Data {
|
||||
|
||||
public DataTable this[string name] {
|
||||
get {
|
||||
foreach (DataTable dt in list) {
|
||||
if (dt.TableName == name)
|
||||
return dt;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
int index = IndexOf (name, true);
|
||||
return index < 0 ? null : (DataTable) list[index];
|
||||
}
|
||||
}
|
||||
|
||||
protected override ArrayList List {
|
||||
@@ -104,12 +100,7 @@ namespace System.Data {
|
||||
|
||||
public bool Contains (string name)
|
||||
{
|
||||
foreach (DataTable dt in list) {
|
||||
if (dt.TableName == name)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return (-1 != IndexOf (name, false));
|
||||
}
|
||||
|
||||
public virtual int IndexOf (DataTable table)
|
||||
@@ -119,7 +110,7 @@ namespace System.Data {
|
||||
|
||||
public virtual int IndexOf (string name)
|
||||
{
|
||||
return list.IndexOf (this [name]);
|
||||
return IndexOf (name, false);
|
||||
}
|
||||
|
||||
public void Remove (DataTable table)
|
||||
@@ -157,7 +148,28 @@ namespace System.Data {
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
|
||||
private int IndexOf (string name, bool error)
|
||||
{
|
||||
int count = 0, match = -1;
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
String name2 = ((DataTable) list[i]).TableName;
|
||||
if (String.Compare (name, name2, true) == 0)
|
||||
{
|
||||
if (String.Compare (name, name2, false) == 0)
|
||||
return i;
|
||||
match = i;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 1)
|
||||
return match;
|
||||
if (count > 1 && error)
|
||||
throw new ArgumentException ("There is no match for the name in the same case and there are multiple matches in different case.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// gives name to Table (Table1, Table2, Table3,...)
|
||||
/// </summary>
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
2003-03-06 Ville Palo <[email protected]>
|
||||
|
||||
* DataTableTest.cs: More tests for Select ()
|
||||
|
||||
2003-02-28 Ville Palo <[email protected]>
|
||||
|
||||
* DataColumnCollectionTest.cs: Added more tests.
|
||||
|
||||
@@ -146,13 +146,13 @@ namespace MonoTests.System.Data
|
||||
|
||||
Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
|
||||
AssertEquals ("test#05", 5, Rows.Length);
|
||||
|
||||
|
||||
Rows = Child.Select ("age = 20 - 1");
|
||||
AssertEquals ("test#06", 1, Rows.Length);
|
||||
|
||||
|
||||
Rows = Child.Select ("age <= 20");
|
||||
AssertEquals ("test#07", 3, Rows.Length);
|
||||
|
||||
|
||||
Rows = Child.Select ("age >= 20");
|
||||
AssertEquals ("test#08", 3, Rows.Length);
|
||||
|
||||
@@ -162,7 +162,6 @@ namespace MonoTests.System.Data
|
||||
Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
|
||||
AssertEquals ("test#10", 1, Rows.Length);
|
||||
AssertEquals ("test#11", "Mack", Rows [0] [0]);
|
||||
|
||||
}
|
||||
|
||||
public void TestSelect2 ()
|
||||
@@ -263,8 +262,9 @@ namespace MonoTests.System.Data
|
||||
T.Rows.Add (Row);
|
||||
|
||||
AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
|
||||
AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
|
||||
|
||||
AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
|
||||
|
||||
try {
|
||||
T.Select ("name = 1human ");
|
||||
Fail ("test#03");
|
||||
@@ -284,6 +284,7 @@ namespace MonoTests.System.Data
|
||||
}
|
||||
|
||||
AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
|
||||
|
||||
}
|
||||
|
||||
public void TestSelectOperators ()
|
||||
@@ -314,16 +315,20 @@ namespace MonoTests.System.Data
|
||||
Row [1] = 1;
|
||||
Row [2] = 1;
|
||||
T.Rows.Add (Row);
|
||||
|
||||
|
||||
AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
|
||||
AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
|
||||
AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
|
||||
AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
|
||||
AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
|
||||
AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
|
||||
AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
|
||||
AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
|
||||
AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
|
||||
AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
|
||||
// FIXME: Somebody explain how this can be possible.
|
||||
// it seems that it is no matter between 10 - 30. The
|
||||
// result is allways 25 :-P
|
||||
AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
|
||||
|
||||
}
|
||||
|
||||
public void TestSelectExceptions ()
|
||||
@@ -503,15 +508,11 @@ namespace MonoTests.System.Data
|
||||
T.Rows.Add (Row);
|
||||
|
||||
//TODO: How to test Convert-function
|
||||
AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
|
||||
|
||||
AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
|
||||
AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
|
||||
|
||||
AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
|
||||
|
||||
AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
|
||||
|
||||
AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
|
||||
AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
|
||||
AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
|
||||
AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
|
||||
AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
|
||||
AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
|
||||
@@ -520,12 +521,166 @@ namespace MonoTests.System.Data
|
||||
|
||||
}
|
||||
|
||||
public void TestSelectRelations ()
|
||||
{
|
||||
DataSet Set = new DataSet ();
|
||||
DataTable Mom = new DataTable ("Mom");
|
||||
DataTable Child = new DataTable ("Child");
|
||||
|
||||
Set.Tables.Add (Mom);
|
||||
Set.Tables.Add (Child);
|
||||
|
||||
DataColumn Col = new DataColumn ("Name");
|
||||
DataColumn Col2 = new DataColumn ("ChildName");
|
||||
Mom.Columns.Add (Col);
|
||||
Mom.Columns.Add (Col2);
|
||||
|
||||
DataColumn Col3 = new DataColumn ("Name");
|
||||
DataColumn Col4 = new DataColumn ("Age");
|
||||
Col4.DataType = Type.GetType ("System.Int16");
|
||||
Child.Columns.Add (Col3);
|
||||
Child.Columns.Add (Col4);
|
||||
|
||||
DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
|
||||
Set.Relations.Add (Relation);
|
||||
|
||||
DataRow Row = Mom.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Nick";
|
||||
Mom.Rows.Add (Row);
|
||||
|
||||
Row = Mom.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Dick";
|
||||
Mom.Rows.Add (Row);
|
||||
|
||||
Row = Mom.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Mick";
|
||||
Mom.Rows.Add (Row);
|
||||
|
||||
Row = Mom.NewRow ();
|
||||
Row [0] = "Teresa";
|
||||
Row [1] = "Jack";
|
||||
Mom.Rows.Add (Row);
|
||||
|
||||
Row = Mom.NewRow ();
|
||||
Row [0] = "Teresa";
|
||||
Row [1] = "Mack";
|
||||
Mom.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Nick";
|
||||
Row [1] = 15;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Dick";
|
||||
Row [1] = 25;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Mick";
|
||||
Row [1] = 35;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Jack";
|
||||
Row [1] = 10;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Mack";
|
||||
Row [1] = 19;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
Row = Child.NewRow ();
|
||||
Row [0] = "Mack";
|
||||
Row [1] = 99;
|
||||
Child.Rows.Add (Row);
|
||||
|
||||
DataRow [] Rows = Child.Select ("name = Parent.Childname");
|
||||
AssertEquals ("test#01", 6, Rows.Length);
|
||||
Rows = Child.Select ("Parent.childname = 'Jack'");
|
||||
AssertEquals ("test#02", 1, Rows.Length);
|
||||
|
||||
try {
|
||||
// FIXME: LAMESPEC: Why the exception is thrown why... why...
|
||||
Mom.Select ("Child.Name = 'Jack'");
|
||||
Fail ("test#03");
|
||||
} catch (Exception e) {
|
||||
AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
|
||||
AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
|
||||
}
|
||||
|
||||
Rows = Child.Select ("Parent.name = 'Laura'");
|
||||
AssertEquals ("test#06", 3, Rows.Length);
|
||||
|
||||
DataTable Parent2 = new DataTable ("Parent2");
|
||||
Col = new DataColumn ("Name");
|
||||
Col2 = new DataColumn ("ChildName");
|
||||
|
||||
Parent2.Columns.Add (Col);
|
||||
Parent2.Columns.Add (Col2);
|
||||
Set.Tables.Add (Parent2);
|
||||
|
||||
Row = Parent2.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Nick";
|
||||
Parent2.Rows.Add (Row);
|
||||
|
||||
Row = Parent2.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Dick";
|
||||
Parent2.Rows.Add (Row);
|
||||
|
||||
Row = Parent2.NewRow ();
|
||||
Row [0] = "Laura";
|
||||
Row [1] = "Mick";
|
||||
Parent2.Rows.Add (Row);
|
||||
|
||||
Row = Parent2.NewRow ();
|
||||
Row [0] = "Teresa";
|
||||
Row [1] = "Jack";
|
||||
Parent2.Rows.Add (Row);
|
||||
|
||||
Row = Parent2.NewRow ();
|
||||
Row [0] = "Teresa";
|
||||
Row [1] = "Mack";
|
||||
Parent2.Rows.Add (Row);
|
||||
|
||||
Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
|
||||
Set.Relations.Add (Relation);
|
||||
|
||||
try {
|
||||
Rows = Child.Select ("Parent.ChildName = 'Jack'");
|
||||
Fail ("test#07");
|
||||
} catch (Exception e) {
|
||||
AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
|
||||
AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
|
||||
}
|
||||
|
||||
Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
|
||||
AssertEquals ("test#10", 1, Rows.Length);
|
||||
|
||||
Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
|
||||
AssertEquals ("test#10", 1, Rows.Length);
|
||||
|
||||
try {
|
||||
Mom.Select ("Parent.name = 'John'");
|
||||
} catch (Exception e) {
|
||||
AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
|
||||
AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void TestToString()
|
||||
{
|
||||
DataTable dt = new DataTable();
|
||||
dt.Columns.Add("Col1",typeof(int));
|
||||
|
||||
dt.TableName = "Myzable";
|
||||
dt.TableName = "Mytable";
|
||||
dt.DisplayExpression = "Col1";
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
2003-03-06 Per Arneng <[email protected]>
|
||||
* MailMessageWrapper: Added to get at more advanced mail message
|
||||
|
||||
2003-02-23 Per Arneng <[email protected]>
|
||||
|
||||
* SmtpMail.cs: Send method now implemented (needs testing)
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// MailAddress.cs
|
||||
// author: Per Arneng <[email protected]>
|
||||
using System;
|
||||
|
||||
namespace System.Web.Mail {
|
||||
|
||||
internal class MailAddress {
|
||||
|
||||
protected string user;
|
||||
protected string host;
|
||||
protected string name;
|
||||
|
||||
public string User {
|
||||
get { return user; }
|
||||
set { user = value; }
|
||||
}
|
||||
|
||||
public string Host {
|
||||
get { return host; }
|
||||
set { host = value; }
|
||||
}
|
||||
|
||||
public string Name {
|
||||
get { return name; }
|
||||
set { name = value; }
|
||||
}
|
||||
|
||||
public string Address {
|
||||
get { return String.Format( "{0}@{1}" , user , host ); }
|
||||
set {
|
||||
|
||||
string[] parts = value.Split( new char[] { '@' } );
|
||||
|
||||
if( parts.Length != 2 )
|
||||
throw new FormatException( "Email address is incorrect: " + value );
|
||||
|
||||
user = parts[ 0 ];
|
||||
host = parts[ 1 ];
|
||||
}
|
||||
}
|
||||
|
||||
public static MailAddress Parse( string str ) {
|
||||
MailAddress addr = new MailAddress();
|
||||
string address = null;
|
||||
string nameString = null;
|
||||
string[] parts = str.Split( new char[] { ' ' } );
|
||||
|
||||
// find the address: [email protected]
|
||||
// and put to gether all the parts
|
||||
// before the address as nameString
|
||||
foreach( string part in parts ) {
|
||||
|
||||
if( part.IndexOf( '@' ) > 0 ) {
|
||||
address = part;
|
||||
break;
|
||||
}
|
||||
|
||||
nameString = nameString + part + " ";
|
||||
}
|
||||
|
||||
if( address == null )
|
||||
throw new FormatException( "Email address not found in: " + str );
|
||||
|
||||
address = address.Trim( new char[] { '<' , '>' , '(' , ')' } );
|
||||
|
||||
addr.Address = address;
|
||||
|
||||
if( nameString != null ) {
|
||||
addr.Name = nameString.Trim();
|
||||
addr.Name = ( addr.Name.Length == 0 ? null : addr.Name );
|
||||
}
|
||||
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
|
||||
string retString = "";
|
||||
|
||||
if( name == null ) {
|
||||
|
||||
retString = String.Format( "<{0}>" , this.Address );
|
||||
|
||||
} else {
|
||||
|
||||
retString = String.Format( "\"{0}\" <{1}>" , this.Name , this.Address);
|
||||
|
||||
}
|
||||
|
||||
return retString;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// MailAddressCollection.cs
|
||||
// author: Per Arneng <[email protected]>
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
namespace System.Web.Mail {
|
||||
|
||||
internal class MailAddressCollection : IEnumerable {
|
||||
|
||||
protected ArrayList data = new ArrayList();
|
||||
|
||||
public MailAddress this[ int index ] {
|
||||
get { return this.Get( index ); }
|
||||
}
|
||||
|
||||
public int Count { get { return data.Count; } }
|
||||
|
||||
public void Add( MailAddress addr ) { data.Add( addr ); }
|
||||
public MailAddress Get( int index ) { return (MailAddress)data[ index ]; }
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
return data.GetEnumerator();
|
||||
}
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for( int i = 0; i <data.Count ; i++ ) {
|
||||
MailAddress addr = this.Get( i );
|
||||
|
||||
builder.Append( addr );
|
||||
|
||||
if( i != ( data.Count - 1 ) ) builder.Append( ", " );
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public static MailAddressCollection Parse( string str ) {
|
||||
|
||||
if( str == null ) throw new ArgumentNullException("Null is not allowed as an address string");
|
||||
|
||||
MailAddressCollection list = new MailAddressCollection();
|
||||
|
||||
string[] parts = str.Split( new char[] { ',' , ';' } );
|
||||
|
||||
foreach( string part in parts ) {
|
||||
list.Add( MailAddress.Parse( part ) );
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// MailMessageWrapper.cs
|
||||
// author: Per Arneng <[email protected]>
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace System.Web.Mail {
|
||||
|
||||
internal class MailMessageWrapper {
|
||||
|
||||
private MailAddressCollection bcc = new MailAddressCollection();
|
||||
private MailAddressCollection cc = new MailAddressCollection();
|
||||
private MailAddress from;
|
||||
private MailAddressCollection to = new MailAddressCollection();
|
||||
private MailMessage message;
|
||||
|
||||
// Constructor
|
||||
public MailMessageWrapper( MailMessage message )
|
||||
{
|
||||
this.message = message;
|
||||
|
||||
if(message.From != null ) from = MailAddress.Parse( message.From );
|
||||
if(message.To != null ) to = MailAddressCollection.Parse( message.To );
|
||||
if(message.Cc != null )cc = MailAddressCollection.Parse( message.Cc );
|
||||
if(message.Bcc != null )bcc = MailAddressCollection.Parse( message.Bcc );
|
||||
}
|
||||
|
||||
// Properties
|
||||
public IList Attachments {
|
||||
get { return message.Attachments; }
|
||||
}
|
||||
|
||||
public MailAddressCollection Bcc {
|
||||
get { return bcc; }
|
||||
}
|
||||
|
||||
public string Body {
|
||||
get { return message.Body; }
|
||||
set { message.Body = value; }
|
||||
}
|
||||
|
||||
public Encoding BodyEncoding {
|
||||
get { return message.BodyEncoding; }
|
||||
set { message.BodyEncoding = value; }
|
||||
}
|
||||
|
||||
public MailFormat BodyFormat {
|
||||
get { return message.BodyFormat; }
|
||||
set { message.BodyFormat = value; }
|
||||
}
|
||||
|
||||
public MailAddressCollection Cc {
|
||||
get { return cc; }
|
||||
}
|
||||
|
||||
public MailAddress From {
|
||||
get { return from; }
|
||||
}
|
||||
|
||||
public IDictionary Headers {
|
||||
get { return message.Headers; }
|
||||
}
|
||||
|
||||
public MailPriority Priority {
|
||||
get { return message.Priority; }
|
||||
set { message.Priority = value; }
|
||||
}
|
||||
|
||||
public string Subject {
|
||||
get { return message.Subject; }
|
||||
set { message.Subject = value; }
|
||||
}
|
||||
|
||||
public MailAddressCollection To {
|
||||
get { return to; }
|
||||
}
|
||||
|
||||
public string UrlContentBase {
|
||||
get { return message.UrlContentBase; }
|
||||
|
||||
}
|
||||
|
||||
public string UrlContentLocation {
|
||||
get { return message.UrlContentLocation; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,10 +43,15 @@ namespace System.Web.Mail {
|
||||
|
||||
}
|
||||
|
||||
public void Send( MailMessage msg ) {
|
||||
public void Send( MailMessageWrapper msg ) {
|
||||
|
||||
if( msg.From == null ) {
|
||||
throw new SmtpException( "From property must be set." );
|
||||
}
|
||||
|
||||
if( ( ! HasData( msg.From ) ) || ( ! HasData( msg.To ) ) )
|
||||
throw new SmtpException( "From & To properties must be set." );
|
||||
if( msg.To == null ) {
|
||||
if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
|
||||
}
|
||||
|
||||
// if no encoding is set then set the system
|
||||
// default encoding
|
||||
@@ -58,10 +63,17 @@ namespace System.Web.Mail {
|
||||
smtp.WriteRset();
|
||||
|
||||
// write the mail from command
|
||||
smtp.WriteMailFrom( msg.From );
|
||||
|
||||
// write the rcpt to command
|
||||
smtp.WriteRcptTo( msg.To );
|
||||
smtp.WriteMailFrom( msg.From.Address );
|
||||
|
||||
// write the rcpt to command for the To addresses
|
||||
foreach( MailAddress addr in msg.To ) {
|
||||
smtp.WriteRcptTo( addr.Address );
|
||||
}
|
||||
|
||||
// write the rcpt to command for the Cc addresses
|
||||
foreach( MailAddress addr in msg.Cc ) {
|
||||
smtp.WriteRcptTo( addr.Address );
|
||||
}
|
||||
|
||||
// write the data command and then
|
||||
// send the email
|
||||
@@ -84,7 +96,7 @@ namespace System.Web.Mail {
|
||||
}
|
||||
|
||||
// sends a single part mail to the server
|
||||
private void SendSinglepartMail( MailMessage msg ) {
|
||||
private void SendSinglepartMail( MailMessageWrapper msg ) {
|
||||
|
||||
// create the headers
|
||||
IDictionary headers = CreateHeaders( msg );
|
||||
@@ -97,7 +109,7 @@ namespace System.Web.Mail {
|
||||
}
|
||||
|
||||
// sends a multipart mail to the server
|
||||
private void SendMultipartMail( MailMessage msg ) {
|
||||
private void SendMultipartMail( MailMessageWrapper msg ) {
|
||||
|
||||
// create the headers
|
||||
IDictionary headers = CreateHeaders( msg );
|
||||
@@ -177,15 +189,15 @@ namespace System.Web.Mail {
|
||||
|
||||
// send the standard headers
|
||||
// and the custom in MailMessage
|
||||
private IDictionary CreateHeaders( MailMessage msg ) {
|
||||
private IDictionary CreateHeaders( MailMessageWrapper msg ) {
|
||||
Hashtable headers = new Hashtable();
|
||||
|
||||
headers[ "From" ] = msg.From;
|
||||
headers[ "To" ] = msg.To;
|
||||
headers[ "From" ] = msg.From.ToString();
|
||||
headers[ "To" ] = msg.To.ToString();
|
||||
|
||||
if( HasData( msg.Cc ) ) headers[ "Cc" ] = msg.Cc;
|
||||
if( msg.Cc.Count > 0 ) headers[ "Cc" ] = msg.Cc.ToString();
|
||||
|
||||
if( HasData( msg.Bcc ) ) headers[ "Bcc" ] = msg.Bcc;
|
||||
if( msg.Bcc.Count > 0 ) headers[ "Bcc" ] = msg.Bcc.ToString();
|
||||
|
||||
if( HasData( msg.Subject ) ) {
|
||||
|
||||
|
||||
@@ -34,12 +34,14 @@ namespace System.Web.Mail
|
||||
|
||||
public static void Send (MailMessage message)
|
||||
{
|
||||
|
||||
|
||||
try {
|
||||
|
||||
MailMessageWrapper messageWrapper = new MailMessageWrapper( message );
|
||||
|
||||
SmtpClient smtp = new SmtpClient (smtpServer);
|
||||
|
||||
smtp.Send (message);
|
||||
smtp.Send (messageWrapper);
|
||||
|
||||
smtp.Close ();
|
||||
|
||||
@@ -54,7 +56,9 @@ namespace System.Web.Mail
|
||||
|
||||
throw new HttpException (ex.Message);
|
||||
|
||||
}
|
||||
} catch (FormatException ex) {
|
||||
throw new HttpException (ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,9 @@ System.Web.Mail/SmtpStream.cs
|
||||
System.Web.Mail/SmtpException.cs
|
||||
System.Web.Mail/SmtpResponse.cs
|
||||
System.Web.Mail/MailEncoder.cs
|
||||
System.Web.Mail/MailAddress.cs
|
||||
System.Web.Mail/MailAddressCollection.cs
|
||||
System.Web.Mail/MailMessageWrapper.cs
|
||||
System.Web.UI/BuildMethod.cs
|
||||
System.Web.UI/BuildTemplateMethod.cs
|
||||
System.Web.UI/Control.cs
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
2003-03-07 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* HttpWebRequest.cs: implemented IDisposable, make the request stream
|
||||
read only. Changed the way of disposing the socket.
|
||||
|
||||
* HttpWebResponse.cs: wrapped the socket in a write-only NetworkStream,
|
||||
handle chunked transfer encoding (no more hangs), added serialization
|
||||
stuff, call CheckDisposed at the beginning of methods/properties (not
|
||||
in a finally clause).
|
||||
|
||||
2003-02-17 Nick Drochak <[email protected]>
|
||||
|
||||
* WebClient.cs : Implemented Credentials property.
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
//
|
||||
// System.Net.HttpWebRequest
|
||||
//
|
||||
// Author:
|
||||
// Lawrence Pit ([email protected])
|
||||
// Authors:
|
||||
// Lawrence Pit ([email protected])
|
||||
// Gonzalo Paniagua Javier ([email protected])
|
||||
//
|
||||
// (c) 2002 Lawrence Pit
|
||||
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
@@ -12,12 +16,13 @@ using System.Net.Sockets;
|
||||
using System.Runtime.Remoting.Messaging;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace System.Net
|
||||
{
|
||||
[Serializable]
|
||||
public class HttpWebRequest : WebRequest, ISerializable
|
||||
public class HttpWebRequest : WebRequest, ISerializable, IDisposable
|
||||
{
|
||||
private Uri requestUri;
|
||||
private Uri actualUri = null;
|
||||
@@ -43,11 +48,15 @@ namespace System.Net
|
||||
private ServicePoint servicePoint = null;
|
||||
private int timeout = System.Threading.Timeout.Infinite;
|
||||
|
||||
private Stream requestStream = null;
|
||||
private HttpWebRequestStream requestStream = null;
|
||||
private HttpWebResponse webResponse = null;
|
||||
private AutoResetEvent requestEndEvent = null;
|
||||
private bool requesting = false;
|
||||
private bool asyncResponding = false;
|
||||
private Socket socket;
|
||||
private bool requestClosed = false;
|
||||
private bool responseClosed = false;
|
||||
private bool disposed;
|
||||
|
||||
// Constructors
|
||||
|
||||
@@ -457,8 +466,12 @@ namespace System.Net
|
||||
|
||||
internal Stream GetRequestStreamInternal ()
|
||||
{
|
||||
if (this.requestStream == null)
|
||||
this.requestStream = new HttpWebStream (this);
|
||||
if (this.requestStream == null) {
|
||||
this.requestStream = new HttpWebRequestStream (this,
|
||||
GetSocket (),
|
||||
new EventHandler (OnClose));
|
||||
}
|
||||
|
||||
return this.requestStream;
|
||||
}
|
||||
|
||||
@@ -507,17 +520,19 @@ namespace System.Net
|
||||
return EndGetResponse (asyncResult);
|
||||
}
|
||||
|
||||
public WebResponse GetResponseInternal ()
|
||||
WebResponse GetResponseInternal ()
|
||||
{
|
||||
if (webResponse != null)
|
||||
return webResponse;
|
||||
|
||||
Stream responseStream = this.requestStream == null ?
|
||||
new HttpWebStream (this) : this.requestStream;
|
||||
do {
|
||||
this.webResponse = new HttpWebResponse (this.actualUri, method, responseStream);
|
||||
} while (this.webResponse.StatusCode == HttpStatusCode.Continue);
|
||||
return (WebResponse) this.webResponse;
|
||||
GetRequestStreamInternal (); // Make sure we do the request
|
||||
|
||||
webResponse = new HttpWebResponse (this.actualUri,
|
||||
method,
|
||||
GetSocket (),
|
||||
timeout,
|
||||
new EventHandler (OnClose));
|
||||
return webResponse;
|
||||
}
|
||||
|
||||
[MonoTODO]
|
||||
@@ -534,6 +549,32 @@ namespace System.Net
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
~HttpWebRequest ()
|
||||
{
|
||||
Dispose (false);
|
||||
}
|
||||
|
||||
void IDisposable.Dispose ()
|
||||
{
|
||||
Dispose (true);
|
||||
GC.SuppressFinalize (this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose (bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
disposed = true;
|
||||
if (disposing) {
|
||||
Close ();
|
||||
}
|
||||
|
||||
requestStream = null;
|
||||
webResponse = null;
|
||||
socket = null;
|
||||
}
|
||||
|
||||
// Private Methods
|
||||
|
||||
private void CheckRequestStarted ()
|
||||
@@ -544,10 +585,11 @@ namespace System.Net
|
||||
|
||||
internal void Close ()
|
||||
{
|
||||
// already done in class below
|
||||
// if (requestStream != null) {
|
||||
// requestStream.Close ();
|
||||
// }
|
||||
if (requestStream != null)
|
||||
requestStream.Close ();
|
||||
|
||||
if (webResponse != null)
|
||||
webResponse.Close ();
|
||||
|
||||
lock (this) {
|
||||
requesting = false;
|
||||
@@ -557,50 +599,94 @@ namespace System.Net
|
||||
}
|
||||
}
|
||||
|
||||
void OnClose (object sender, EventArgs args)
|
||||
{
|
||||
if (sender == requestStream)
|
||||
requestClosed = true;
|
||||
else if (sender == webResponse)
|
||||
responseClosed = true;
|
||||
|
||||
// TODO: if both have been used, wait until both are Closed. That's what MS does.
|
||||
if (requestClosed && responseClosed && socket != null && socket.Connected)
|
||||
socket.Close ();
|
||||
}
|
||||
|
||||
Socket GetSocket ()
|
||||
{
|
||||
if (socket != null)
|
||||
return socket;
|
||||
|
||||
IPAddress hostAddr = Dns.Resolve (actualUri.Host).AddressList[0];
|
||||
IPEndPoint endPoint = new IPEndPoint (hostAddr, actualUri.Port);
|
||||
socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
socket.Connect (endPoint);
|
||||
if (!socket.Poll (timeout, SelectMode.SelectWrite))
|
||||
throw new WebException("Connection timed out.", WebExceptionStatus.Timeout);
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
// Private Classes
|
||||
|
||||
// to catch the Close called on the NetworkStream
|
||||
internal class HttpWebStream : NetworkStream
|
||||
internal class HttpWebRequestStream : NetworkStream
|
||||
{
|
||||
HttpWebRequest webRequest;
|
||||
bool disposed;
|
||||
EventHandler onClose;
|
||||
|
||||
internal HttpWebStream (HttpWebRequest webRequest)
|
||||
: base (HttpWebStream.CreateSocket (webRequest), true)
|
||||
internal HttpWebRequestStream (HttpWebRequest webRequest,
|
||||
Socket socket,
|
||||
EventHandler onClose)
|
||||
: base (socket, FileAccess.Write, false)
|
||||
{
|
||||
StreamWriter webWriter = null;
|
||||
this.onClose = onClose;
|
||||
StringBuilder sb = new StringBuilder ();
|
||||
sb.Append (webRequest.Method + " " +
|
||||
webRequest.actualUri.PathAndQuery +
|
||||
" HTTP/" +
|
||||
webRequest.version.ToString (2) +
|
||||
"\r\n");
|
||||
|
||||
webWriter = new StreamWriter (this);
|
||||
|
||||
webWriter.Write (webRequest.Method + " " +
|
||||
webRequest.actualUri.PathAndQuery + " HTTP/" + webRequest.version.ToString(2) + "\r\n");
|
||||
|
||||
foreach (string header in webRequest.webHeaders)
|
||||
webWriter.Write (header + ": " + webRequest.webHeaders[header] + "\r\n");
|
||||
foreach (string header in webRequest.webHeaders) {
|
||||
sb.Append (header + ": " + webRequest.webHeaders[header] + "\r\n");
|
||||
}
|
||||
|
||||
// FIXME: write cookie headers (CookieContainer not yet implemented)
|
||||
|
||||
webWriter.Write ("\r\n");
|
||||
webWriter.Flush();
|
||||
|
||||
this.webRequest = webRequest;
|
||||
sb.Append ("\r\n");
|
||||
byte [] bytes = Encoding.ASCII.GetBytes (sb.ToString ());
|
||||
if (!socket.Poll (webRequest.Timeout, SelectMode.SelectWrite))
|
||||
throw new WebException("The request timed out", WebExceptionStatus.Timeout);
|
||||
|
||||
this.Write (bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
private static Socket CreateSocket (HttpWebRequest webRequest)
|
||||
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
IPAddress hostAddr = Dns.Resolve (webRequest.actualUri.Host).AddressList[0];
|
||||
IPEndPoint endPoint = new IPEndPoint (hostAddr, webRequest.actualUri.Port);
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
|
||||
ProtocolType.Tcp);
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
socket.Connect (endPoint);
|
||||
return socket;
|
||||
disposed = true;
|
||||
if (disposing) {
|
||||
/* This does not work !??
|
||||
if (socket.Connected)
|
||||
socket.Shutdown (SocketShutdown.Send);
|
||||
*/
|
||||
if (onClose != null)
|
||||
onClose (this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
onClose = null;
|
||||
base.Dispose (disposing);
|
||||
}
|
||||
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close ();
|
||||
webRequest.Close ();
|
||||
GC.SuppressFinalize (this);
|
||||
Dispose (true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
//
|
||||
// System.Net.HttpWebResponse
|
||||
//
|
||||
// Author:
|
||||
// Lawrence Pit ([email protected])
|
||||
// Authors:
|
||||
// Lawrence Pit ([email protected])
|
||||
// Gonzalo Paniagua Javier ([email protected])
|
||||
//
|
||||
// (c) 2002 Lawrence Pit
|
||||
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
|
||||
namespace System.Net
|
||||
{
|
||||
@@ -21,13 +27,14 @@ namespace System.Net
|
||||
private Version version = null;
|
||||
private HttpStatusCode statusCode;
|
||||
private string statusDescription = null;
|
||||
bool chunked;
|
||||
|
||||
private Stream responseStream;
|
||||
private HttpWebResponseStream responseStream;
|
||||
private bool disposed = false;
|
||||
|
||||
// Constructors
|
||||
|
||||
internal HttpWebResponse (Uri uri, string method, Stream responseStream)
|
||||
internal HttpWebResponse (Uri uri, string method, Socket socket, int timeout, EventHandler onClose)
|
||||
{
|
||||
Text.StringBuilder value = null;
|
||||
string last = null;
|
||||
@@ -36,10 +43,13 @@ namespace System.Net
|
||||
|
||||
this.uri = uri;
|
||||
this.method = method;
|
||||
this.responseStream = responseStream;
|
||||
this.webHeaders = new WebHeaderCollection();
|
||||
|
||||
line = ReadHttpLine(responseStream);
|
||||
responseStream = new HttpWebResponseStream (socket, onClose);
|
||||
if (!socket.Poll (timeout, SelectMode.SelectRead))
|
||||
throw new WebException("The request timed out", WebExceptionStatus.Timeout);
|
||||
|
||||
line = ReadHttpLine (responseStream);
|
||||
protocol = line.Split (' ');
|
||||
|
||||
switch (protocol[0]) {
|
||||
@@ -50,11 +60,11 @@ namespace System.Net
|
||||
this.version = HttpVersion.Version11;
|
||||
break;
|
||||
default:
|
||||
throw new WebException ("Unrecognized HTTP Version");
|
||||
throw new WebException ("Unrecognized HTTP Version: " + line);
|
||||
}
|
||||
|
||||
this.statusCode = (HttpStatusCode) Int32.Parse (protocol[1]);
|
||||
while ((line = ReadHttpLine(responseStream)).Length != 0) {
|
||||
while ((line = ReadHttpLine (responseStream)).Length != 0) {
|
||||
if (!Char.IsWhiteSpace (line[0])) { // new header
|
||||
header = line.Split (new char[] {':'}, 2);
|
||||
if (header.Length != 2)
|
||||
@@ -69,17 +79,29 @@ namespace System.Net
|
||||
}
|
||||
last = header[0];
|
||||
value = new Text.StringBuilder (header[1].Trim());
|
||||
if (last == "Transfer-Encoding" && header [1].IndexOf ("chunked") != -1)
|
||||
chunked = true;
|
||||
}
|
||||
else
|
||||
value.Append (line.Trim());
|
||||
}
|
||||
|
||||
responseStream.Chunked = chunked;
|
||||
this.webHeaders[last] = value.ToString(); // otherwise we miss the last header
|
||||
}
|
||||
|
||||
protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
|
||||
{
|
||||
throw new NotSupportedException ();
|
||||
uri = (Uri) serializationInfo.GetValue ("uri", typeof (Uri));
|
||||
webHeaders = (WebHeaderCollection) serializationInfo.GetValue ("webHeaders",
|
||||
typeof (WebHeaderCollection));
|
||||
cookieCollection = (CookieCollection) serializationInfo.GetValue ("cookieCollection",
|
||||
typeof (CookieCollection));
|
||||
method = serializationInfo.GetString ("method");
|
||||
version = (Version) serializationInfo.GetValue ("version", typeof (Version));
|
||||
statusCode = (HttpStatusCode) serializationInfo.GetValue ("statusCode", typeof (HttpStatusCode));
|
||||
statusDescription = serializationInfo.GetString ("statusDescription");
|
||||
chunked = serializationInfo.GetBoolean ("chunked");
|
||||
}
|
||||
|
||||
// Properties
|
||||
@@ -90,48 +112,44 @@ namespace System.Net
|
||||
// parameter = attribute "=" value
|
||||
// 3.7.1. default is ISO-8859-1
|
||||
get {
|
||||
try {
|
||||
string contentType = ContentType;
|
||||
if (contentType == null)
|
||||
return "ISO-8859-1";
|
||||
string val = contentType.ToLower ();
|
||||
int pos = val.IndexOf ("charset=");
|
||||
if (pos == -1)
|
||||
return "ISO-8859-1";
|
||||
pos += 8;
|
||||
int pos2 = val.IndexOf (';', pos);
|
||||
return (pos2 == -1)
|
||||
? contentType.Substring (pos)
|
||||
: contentType.Substring (pos, pos2 - pos);
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
CheckDisposed ();
|
||||
string contentType = ContentType;
|
||||
if (contentType == null)
|
||||
return "ISO-8859-1";
|
||||
string val = contentType.ToLower ();
|
||||
int pos = val.IndexOf ("charset=");
|
||||
if (pos == -1)
|
||||
return "ISO-8859-1";
|
||||
pos += 8;
|
||||
int pos2 = val.IndexOf (';', pos);
|
||||
return (pos2 == -1)
|
||||
? contentType.Substring (pos)
|
||||
: contentType.Substring (pos, pos2 - pos);
|
||||
}
|
||||
}
|
||||
|
||||
public string ContentEncoding {
|
||||
get {
|
||||
try { return webHeaders ["Content-Encoding"]; }
|
||||
finally { CheckDisposed (); }
|
||||
CheckDisposed ();
|
||||
return webHeaders ["Content-Encoding"];
|
||||
}
|
||||
}
|
||||
|
||||
public override long ContentLength {
|
||||
get {
|
||||
CheckDisposed ();
|
||||
try {
|
||||
return Int64.Parse (webHeaders ["Content-Length"]);
|
||||
} catch (Exception) {
|
||||
return -1;
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string ContentType {
|
||||
get {
|
||||
try { return webHeaders ["Content-Type"]; }
|
||||
finally { CheckDisposed (); }
|
||||
get {
|
||||
CheckDisposed ();
|
||||
return webHeaders ["Content-Type"];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,11 +210,8 @@ namespace System.Net
|
||||
|
||||
public string Server {
|
||||
get {
|
||||
try {
|
||||
return webHeaders ["Server"];
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
CheckDisposed ();
|
||||
return webHeaders ["Server"];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,39 +233,36 @@ namespace System.Net
|
||||
|
||||
public override int GetHashCode ()
|
||||
{
|
||||
try {
|
||||
return base.GetHashCode ();
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
CheckDisposed ();
|
||||
return base.GetHashCode ();
|
||||
}
|
||||
|
||||
public string GetResponseHeader (string headerName)
|
||||
{
|
||||
try {
|
||||
return webHeaders [headerName];
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
CheckDisposed ();
|
||||
return webHeaders [headerName];
|
||||
}
|
||||
|
||||
public override Stream GetResponseStream ()
|
||||
{
|
||||
try {
|
||||
if (method.Equals ("HEAD")) // see par 4.3 & 9.4
|
||||
return Stream.Null;
|
||||
return responseStream;
|
||||
} finally {
|
||||
CheckDisposed ();
|
||||
}
|
||||
CheckDisposed ();
|
||||
if (method.Equals ("HEAD")) // see par 4.3 & 9.4
|
||||
return Stream.Null;
|
||||
return responseStream;
|
||||
}
|
||||
|
||||
[MonoTODO]
|
||||
void ISerializable.GetObjectData (SerializationInfo serializationInfo,
|
||||
StreamingContext streamingContext)
|
||||
{
|
||||
CheckDisposed ();
|
||||
throw new NotImplementedException ();
|
||||
serializationInfo.AddValue ("uri", uri);
|
||||
serializationInfo.AddValue ("webHeaders", webHeaders);
|
||||
serializationInfo.AddValue ("cookieCollection", cookieCollection);
|
||||
serializationInfo.AddValue ("method", method);
|
||||
serializationInfo.AddValue ("version", version);
|
||||
serializationInfo.AddValue ("statusCode", statusCode);
|
||||
serializationInfo.AddValue ("statusDescription", statusDescription);
|
||||
serializationInfo.AddValue ("chunked", chunked);
|
||||
}
|
||||
|
||||
|
||||
@@ -285,7 +297,6 @@ namespace System.Net
|
||||
cookieCollection = null;
|
||||
method = null;
|
||||
version = null;
|
||||
// statusCode = null;
|
||||
statusDescription = null;
|
||||
}
|
||||
|
||||
@@ -293,7 +304,7 @@ namespace System.Net
|
||||
Stream stream = responseStream;
|
||||
responseStream = null;
|
||||
if (stream != null)
|
||||
stream.Close (); // also closes webRequest
|
||||
stream.Close ();
|
||||
}
|
||||
|
||||
private void CheckDisposed ()
|
||||
@@ -304,19 +315,19 @@ namespace System.Net
|
||||
|
||||
private static string ReadHttpLine (Stream stream)
|
||||
{
|
||||
Text.StringBuilder line = new Text.StringBuilder();
|
||||
StringBuilder line = new StringBuilder();
|
||||
byte last = (byte)'\n';
|
||||
bool read_last = false;
|
||||
byte[] buf = new byte[1]; // one at a time to not snarf too much
|
||||
int c;
|
||||
|
||||
while (stream.Read (buf, 0, buf.Length) != 0) {
|
||||
if (buf[0] == '\r') {
|
||||
if ((last = (byte)stream.ReadByte ()) == '\n') // headers; not at EOS
|
||||
while ((c = stream.ReadByte ()) != -1) {
|
||||
if (c == '\r') {
|
||||
if ((last = (byte) stream.ReadByte ()) == '\n') // headers; not at EOS
|
||||
break;
|
||||
read_last = true;
|
||||
}
|
||||
|
||||
line.Append (Convert.ToChar(buf[0]));
|
||||
line.Append ((char) c);
|
||||
if (read_last) {
|
||||
line.Append (Convert.ToChar (last));
|
||||
read_last = false;
|
||||
@@ -397,5 +408,204 @@ namespace System.Net
|
||||
SetCookie (cookie_str);
|
||||
|
||||
}
|
||||
|
||||
class HttpWebResponseStream : NetworkStream
|
||||
{
|
||||
bool disposed;
|
||||
bool chunked;
|
||||
int chunkSize;
|
||||
int chunkLeft;
|
||||
bool readingChunkSize;
|
||||
EventHandler onClose;
|
||||
|
||||
public HttpWebResponseStream (Socket socket, EventHandler onClose)
|
||||
: base (socket, FileAccess.Read, false)
|
||||
{
|
||||
this.onClose = onClose;
|
||||
chunkSize = -1;
|
||||
chunkLeft = 0;
|
||||
}
|
||||
|
||||
public bool Chunked {
|
||||
get { return chunked; }
|
||||
set { chunked = value; }
|
||||
}
|
||||
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
disposed = true;
|
||||
if (disposing) {
|
||||
/* This does not work !??
|
||||
if (Socket.Connected)
|
||||
Socket.Shutdown (SocketShutdown.Receive);
|
||||
*/
|
||||
|
||||
if (onClose != null)
|
||||
onClose (this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
onClose = null;
|
||||
base.Dispose (disposing);
|
||||
}
|
||||
|
||||
void ReadChunkSize ()
|
||||
{
|
||||
bool cr = false;
|
||||
bool lf = false;
|
||||
int size = 0;
|
||||
// 8 hex digits should be enough
|
||||
for (int i = 0; i < 10; i++) {
|
||||
char c = Char.ToUpper ((char) ReadByte ());
|
||||
if (c == '\r') {
|
||||
if (!cr) {
|
||||
cr = true;
|
||||
continue;
|
||||
}
|
||||
throw new IOException ("Bad stream: 2 CR");
|
||||
}
|
||||
|
||||
if (c == '\n' && cr == true) {
|
||||
if (!lf) {
|
||||
lf = true;
|
||||
break;
|
||||
}
|
||||
|
||||
throw new IOException ("Bad stream: got LF but no CR");
|
||||
}
|
||||
|
||||
if (i < 8 && ((c >= '0' && c <= '9') || c >= 'A' && c <= 'F')) {
|
||||
size = size << 4;
|
||||
if (c >= 'A' && c <= 'F')
|
||||
size += c - 'A' + 10;
|
||||
else
|
||||
size += c - '0';
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new IOException ("Bad stream: got " + c);
|
||||
}
|
||||
|
||||
if (!cr || !lf)
|
||||
throw new IOException ("Bad stream: no CR or LF after chunk size");
|
||||
|
||||
chunkSize = size;
|
||||
chunkLeft = size;
|
||||
}
|
||||
|
||||
int GetMaxSizeFromChunkLeft (int requestedSize)
|
||||
{
|
||||
if (!chunked)
|
||||
return requestedSize;
|
||||
|
||||
if (chunkSize == -1 || chunkLeft == 0) {
|
||||
lock (this) {
|
||||
if (chunkSize == -1 || chunkLeft == 0) {
|
||||
readingChunkSize = true;
|
||||
try {
|
||||
ReadChunkSize ();
|
||||
} finally {
|
||||
readingChunkSize = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (chunkLeft < requestedSize) ? chunkLeft : requestedSize;
|
||||
}
|
||||
|
||||
public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
|
||||
AsyncCallback callback, object state)
|
||||
{
|
||||
CheckDisposed ();
|
||||
IAsyncResult retval;
|
||||
|
||||
if (buffer == null)
|
||||
throw new ArgumentNullException ("buffer is null");
|
||||
|
||||
int len = buffer.Length;
|
||||
if (offset < 0 || offset >= len)
|
||||
throw new ArgumentOutOfRangeException ("offset exceeds the size of buffer");
|
||||
|
||||
if (offset + size < 0 || offset+size > len)
|
||||
throw new ArgumentOutOfRangeException ("offset+size exceeds the size of buffer");
|
||||
|
||||
if (!readingChunkSize)
|
||||
size = GetMaxSizeFromChunkLeft (size);
|
||||
|
||||
try {
|
||||
retval = base.BeginRead (buffer, offset, size, callback, state);
|
||||
} catch {
|
||||
throw new IOException ("BeginReceive failure");
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
public override int EndRead (IAsyncResult ar)
|
||||
{
|
||||
CheckDisposed ();
|
||||
int res;
|
||||
|
||||
if (ar == null)
|
||||
throw new ArgumentNullException ("async result is null");
|
||||
|
||||
try {
|
||||
res = base.EndRead (ar);
|
||||
} catch (Exception e) {
|
||||
throw new IOException ("EndRead failure", e);
|
||||
}
|
||||
|
||||
AdjustChunkLeft (res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public override int Read (byte [] buffer, int offset, int size)
|
||||
{
|
||||
CheckDisposed ();
|
||||
int res;
|
||||
|
||||
if (buffer == null)
|
||||
throw new ArgumentNullException ("buffer is null");
|
||||
|
||||
if (offset < 0 || offset >= buffer.Length)
|
||||
throw new ArgumentOutOfRangeException ("offset exceeds the size of buffer");
|
||||
|
||||
if (offset + size < 0 || offset + size > buffer.Length)
|
||||
throw new ArgumentOutOfRangeException ("offset+size exceeds the size of buffer");
|
||||
|
||||
if (!readingChunkSize)
|
||||
size = GetMaxSizeFromChunkLeft (size);
|
||||
|
||||
try {
|
||||
res = base.Read (buffer, offset, size);
|
||||
} catch (Exception e) {
|
||||
throw new IOException ("Read failure", e);
|
||||
}
|
||||
|
||||
AdjustChunkLeft (res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void CheckDisposed ()
|
||||
{
|
||||
if (disposed)
|
||||
throw new ObjectDisposedException (GetType ().FullName);
|
||||
}
|
||||
|
||||
void AdjustChunkLeft (int read)
|
||||
{
|
||||
if (!chunked)
|
||||
return;
|
||||
|
||||
chunkLeft -= read;
|
||||
if (chunkLeft < 0)
|
||||
chunkLeft = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
2003-03-05 Miguel de Icaza <[email protected]>
|
||||
|
||||
* category.cs (CategoryUtils.CategoryFromName): Use StartsWith
|
||||
("Is") instead of a substring for (0,2) which was throwing an
|
||||
exception causing Category.None to be returned
|
||||
|
||||
2003-01-17 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* collections.cs: fixed bug #30091.
|
||||
|
||||
@@ -180,7 +180,7 @@ namespace System.Text.RegularExpressions {
|
||||
class CategoryUtils {
|
||||
public static Category CategoryFromName (string name) {
|
||||
try {
|
||||
if (name.Substring (0, 2).Equals ("Is")) // remove prefix from block range
|
||||
if (name.StartsWith ("Is")) // remove prefix from block range
|
||||
name = name.Substring (2);
|
||||
|
||||
return (Category)Enum.Parse (typeof (Category), "Unicode" + name);
|
||||
|
||||
@@ -236,6 +236,7 @@ System.ComponentModel/TypeConverterAttribute.cs
|
||||
System.ComponentModel/TypeDescriptor.cs
|
||||
System.ComponentModel/RefreshEventHandler.cs
|
||||
System.ComponentModel/RefreshEventArgs.cs
|
||||
System.ComponentModel/RunInstallerAttribute.cs
|
||||
System.ComponentModel/StringConverter.cs
|
||||
System.ComponentModel/WarningException.cs
|
||||
System.ComponentModel/InvalidEnumArgumentException.cs
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
2003-03-07 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* StreamReader.cs: fixed bug #39280.
|
||||
|
||||
2003-03-07 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* Directory.cs: readded mkdir -p behavior. Thanks to kiwnix for
|
||||
pointing it out.
|
||||
|
||||
2003-03-05 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* MemoryStream.cs: general fixes and reformatted. Passes all tests in
|
||||
|
||||
@@ -42,13 +42,22 @@ namespace System.IO
|
||||
if (path == ":")
|
||||
throw new NotSupportedException ("Only ':' In path");
|
||||
|
||||
return CreateDirectoriesInternal (path);
|
||||
}
|
||||
|
||||
static DirectoryInfo CreateDirectoriesInternal (string path)
|
||||
{
|
||||
DirectoryInfo info = new DirectoryInfo (path);
|
||||
if (info.Parent != null && !info.Parent.Exists)
|
||||
info.Parent.Create ();
|
||||
|
||||
MonoIOError error;
|
||||
if (!MonoIO.CreateDirectory (path, out error)) {
|
||||
if (error != MonoIOError.ERROR_ALREADY_EXISTS)
|
||||
throw MonoIO.GetException (error);
|
||||
}
|
||||
|
||||
return new DirectoryInfo (path);
|
||||
return info;
|
||||
}
|
||||
|
||||
public static void Delete (string path)
|
||||
|
||||
@@ -56,6 +56,7 @@ namespace System.IO {
|
||||
private Decoder decoder;
|
||||
|
||||
private Stream base_stream;
|
||||
private bool mayBlock;
|
||||
|
||||
private class NullStreamReader : StreamReader {
|
||||
public override int Peek ()
|
||||
@@ -268,6 +269,7 @@ namespace System.IO {
|
||||
if (cbEncoded == 0)
|
||||
return 0;
|
||||
|
||||
mayBlock = (cbEncoded < buffer_size);
|
||||
if (do_checks > 0){
|
||||
Encoding old = encoding;
|
||||
parse_start = DoChecks (cbEncoded);
|
||||
@@ -287,10 +289,7 @@ namespace System.IO {
|
||||
|
||||
public override int Peek ()
|
||||
{
|
||||
if (!base_stream.CanSeek)
|
||||
return -1;
|
||||
|
||||
if (pos >= decoded_count && ReadBuffer () == 0)
|
||||
if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
|
||||
return -1;
|
||||
|
||||
return decoded_buffer [pos];
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
2003-03-06 Zoltan Varga <[email protected]>
|
||||
|
||||
* ILGenerator.cs (Emit): Do not pop the arguments off the stack for
|
||||
ldftn etc. Fixes bug #39196.
|
||||
|
||||
2003-02-28 Zoltan Varga <[email protected]>
|
||||
|
||||
* ILGenerator.cs (Emit): Add fixup for already created methods and
|
||||
|
||||
@@ -213,6 +213,7 @@ namespace System.Reflection.Emit {
|
||||
}
|
||||
if (max_stack < cur_stack)
|
||||
max_stack = cur_stack;
|
||||
|
||||
/*
|
||||
* Note that we adjust for the pop behaviour _after_ setting max_stack.
|
||||
*/
|
||||
@@ -357,7 +358,7 @@ namespace System.Reflection.Emit {
|
||||
add_token_fixup (constructor);
|
||||
emit_int (token);
|
||||
ParameterInfo[] mparams = constructor.GetParameters();
|
||||
if (mparams != null)
|
||||
if ((opcode.StackBehaviourPop == StackBehaviour.Varpop) && (mparams != null))
|
||||
cur_stack -= mparams.Length;
|
||||
}
|
||||
public virtual void Emit (OpCode opcode, double val) {
|
||||
@@ -517,7 +518,7 @@ namespace System.Reflection.Emit {
|
||||
if (method.ReturnType != void_type)
|
||||
cur_stack ++;
|
||||
ParameterInfo[] mparams = method.GetParameters();
|
||||
if (mparams != null)
|
||||
if ((opcode.StackBehaviourPop == StackBehaviour.Varpop) && (mparams != null))
|
||||
cur_stack -= mparams.Length;
|
||||
}
|
||||
[CLSCompliant(false)]
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
AssemblyBuilder.cs
|
||||
AssemblyBuilderAccess.cs
|
||||
ConstructorBuilder.cs
|
||||
EventToken.cs
|
||||
FieldToken.cs
|
||||
FlowControl.cs
|
||||
ILGenerator.cs
|
||||
Label.cs
|
||||
MethodToken.cs
|
||||
ModuleBuilder.cs
|
||||
OpCode.cs
|
||||
OpCodes.cs
|
||||
OpCodeType.cs
|
||||
OperandType.cs
|
||||
PackingSize.cs
|
||||
ParameterToken.cs
|
||||
PEFileKinds.cs
|
||||
PropertyToken.cs
|
||||
SignatureToken.cs
|
||||
StackBehaviour.cs
|
||||
StringToken.cs
|
||||
TypeBuilder.cs
|
||||
TypeToken.cs
|
||||
@@ -1,25 +0,0 @@
|
||||
Assembly.cs
|
||||
AssemblyNameFlags.cs
|
||||
BindingFlags.cs
|
||||
CallingConventions.cs
|
||||
ConstructorInfo.cs
|
||||
DefaultMemberAttribute.cs
|
||||
EventAttributes.cs
|
||||
EventInfo.cs
|
||||
FieldAttributes.cs
|
||||
FieldInfo.cs
|
||||
ICustomAttributeProvider.cs
|
||||
MemberFilter.cs
|
||||
MemberInfo.cs
|
||||
MemberTypes.cs
|
||||
MethodAttributes.cs
|
||||
MethodBase.cs
|
||||
MethodImplAttributes.cs
|
||||
MethodInfo.cs
|
||||
Module.cs
|
||||
ParameterAttributes.cs
|
||||
PropertyAttributes.cs
|
||||
PropertyInfo.cs
|
||||
ResourceAttributes.cs
|
||||
ResourceLocation.cs
|
||||
TypeAttributes.cs
|
||||
@@ -169,7 +169,10 @@ public class ASCIIEncoding : Encoding
|
||||
}
|
||||
int count = byteCount;
|
||||
while (count-- > 0) {
|
||||
chars [charIndex++] = (char)(bytes [byteIndex++]);
|
||||
char c = (char)(bytes [byteIndex++]);
|
||||
if (c > 127)
|
||||
c = '?';
|
||||
chars [charIndex++] = c;
|
||||
}
|
||||
return byteCount;
|
||||
}
|
||||
@@ -206,6 +209,8 @@ public class ASCIIEncoding : Encoding
|
||||
if (count < 0 || count > (bytes.Length - index)) {
|
||||
throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
|
||||
}
|
||||
if (count == 0)
|
||||
return String.Empty;
|
||||
unsafe {
|
||||
fixed (byte *ss = &bytes [0]) {
|
||||
return new String ((sbyte*)ss, index, count);
|
||||
@@ -218,6 +223,8 @@ public class ASCIIEncoding : Encoding
|
||||
throw new ArgumentNullException ("bytes");
|
||||
}
|
||||
int count = bytes.Length;
|
||||
if (count == 0)
|
||||
return String.Empty;
|
||||
unsafe {
|
||||
fixed (byte *ss = &bytes [0]) {
|
||||
return new String ((sbyte*)ss, 0, count);
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
2003-03-05 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* ASCIIEncoding.cs: fixed bug #38984.
|
||||
|
||||
2003-03-05 Aleksey Demakov <[email protected]>
|
||||
|
||||
* ASCIIEncoding.cs:
|
||||
* Latin1Encoding.cs: fix GetString (byte[]) and
|
||||
GetString (byte[], int, int) for zero-length case.
|
||||
|
||||
2003-02-19 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* Latin1Encoding.cs: added Serializable attribute.
|
||||
|
||||
@@ -214,6 +214,8 @@ internal class Latin1Encoding : Encoding
|
||||
if (count < 0 || count > (bytes.Length - index)) {
|
||||
throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
|
||||
}
|
||||
if (count == 0)
|
||||
return String.Empty;
|
||||
unsafe {
|
||||
fixed (byte *ss = &bytes [0]) {
|
||||
return new String ((sbyte*)ss, index, count);
|
||||
@@ -226,6 +228,8 @@ internal class Latin1Encoding : Encoding
|
||||
throw new ArgumentNullException ("bytes");
|
||||
}
|
||||
int count = bytes.Length;
|
||||
if (count == 0)
|
||||
return String.Empty;
|
||||
unsafe {
|
||||
fixed (byte *ss = &bytes [0]) {
|
||||
return new String ((sbyte*)ss, 0, count);
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
2003-03-06 Nick Drochak <[email protected]>
|
||||
|
||||
* TimeSpan.cs (Negate): Throw exception when value is MinValue.
|
||||
|
||||
2003-03-04 Dick Porter <[email protected]>
|
||||
|
||||
* Single.cs:
|
||||
|
||||
@@ -266,9 +266,9 @@ public struct TimeSpan : IComparable {
|
||||
|
||||
public TimeSpan Negate ()
|
||||
{
|
||||
checked {
|
||||
return new TimeSpan (-_ticks);
|
||||
}
|
||||
if (_ticks == long.MinValue)
|
||||
throw new OverflowException ("This TimeSpan value is MinValue and cannot be negated.");
|
||||
return new TimeSpan (-_ticks);
|
||||
}
|
||||
|
||||
public static TimeSpan Parse (string s)
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
2003-03-07 Gonzalo Paniagua Javier <[email protected]>
|
||||
|
||||
* StreamReaderTest.cs: added test for exception when the nase stream is
|
||||
closed. Make the reader.CurrentEncoding test work.
|
||||
|
||||
2003-03-05 Dick Porter <[email protected]>
|
||||
|
||||
* MemoryStreamTest.cs: Some more tests
|
||||
|
||||
@@ -354,7 +354,7 @@ public class StreamReaderTest : TestCase
|
||||
MemoryStream m = new MemoryStream(b);
|
||||
StreamReader r = new StreamReader(m);
|
||||
AssertEquals("wrong encoding",
|
||||
Encoding.UTF8, r.CurrentEncoding);
|
||||
Encoding.UTF8.GetType (), r.CurrentEncoding.GetType ());
|
||||
} catch (Exception e) {
|
||||
Fail ("Unexpected exception thrown: " + e.ToString());
|
||||
}
|
||||
@@ -547,5 +547,21 @@ public class StreamReaderTest : TestCase
|
||||
AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
|
||||
AssertEquals("line doesn't match", "", r.ReadToEnd());
|
||||
}
|
||||
|
||||
public void TestBaseStreamClosed ()
|
||||
{
|
||||
byte [] b = {};
|
||||
MemoryStream m = new MemoryStream (b);
|
||||
StreamReader r = new StreamReader (m);
|
||||
m.Close ();
|
||||
bool thrown = false;
|
||||
try {
|
||||
r.Peek ();
|
||||
} catch (ObjectDisposedException) {
|
||||
thrown = true;
|
||||
}
|
||||
|
||||
AssertEquals ("#01", true, thrown);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,6 +164,13 @@ namespace MonoTests.System.Text {
|
||||
AssertEquals (testchars[1], (char) bytes[0]);
|
||||
}
|
||||
|
||||
public void TestZero ()
|
||||
{
|
||||
Encoding encoding = Encoding.ASCII;
|
||||
AssertEquals ("#01", encoding.GetString (new byte [0]), "");
|
||||
AssertEquals ("#02", encoding.GetString (new byte [0], 0, 0), "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loaded 100 of 149 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user