This repository was archived by the owner on Jan 12, 2024. It is now read-only.
forked from xenserver/dotnet-packages
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatch-sharpziplib-ca-63358-TarInputStream.diff
317 lines (307 loc) · 9.5 KB
/
patch-sharpziplib-ca-63358-TarInputStream.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright (c) Citrix Systems, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "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 COPYRIGHT HOLDER OR
# CONTRIBUTORS 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.
--- src/Tar/TarInputStream.cs 2011-06-17 23:09:32.000000000 +0100
+++ ../SharpZipLib-0.85.4.403/src/Tar/TarInputStream.cs 2011-08-01 22:47:08.000000000 +0100
@@ -215,81 +215,22 @@
public override int Read(byte[] buffer, int offset, int count)
{
if ( buffer == null )
- {
throw new ArgumentNullException("buffer");
- }
- int totalRead = 0;
-
- if (this.entryOffset >= this.entrySize)
- {
- return 0;
- }
-
- long numToRead = count;
-
- if ((numToRead + this.entryOffset) > this.entrySize)
- {
- numToRead = this.entrySize - this.entryOffset;
- }
-
- if (this.readBuffer != null)
- {
- int sz = (numToRead > this.readBuffer.Length) ? this.readBuffer.Length : (int)numToRead;
-
- Array.Copy(this.readBuffer, 0, buffer, offset, sz);
-
- if (sz >= this.readBuffer.Length)
- {
- this.readBuffer = null;
- }
- else
- {
- int newLen = this.readBuffer.Length - sz;
- byte[] newBuf = new byte[newLen];
- Array.Copy(this.readBuffer, sz, newBuf, 0, newLen);
- this.readBuffer = newBuf;
- }
-
- totalRead += sz;
- numToRead -= sz;
- offset += sz;
- }
-
- while (numToRead > 0)
- {
- byte[] rec = this.buffer.ReadBlock();
- if (rec == null)
- {
- // Unexpected EOF!
- throw new TarException("unexpected EOF with " + numToRead + " bytes unread");
- }
-
- int sz = (int)numToRead;
- int recLen = rec.Length;
-
- if (recLen > sz)
- {
- Array.Copy(rec, 0, buffer, offset, sz);
- this.readBuffer = new byte[recLen - sz];
- Array.Copy(rec, sz, this.readBuffer, 0, recLen - sz);
- }
- else
- {
- sz = recLen;
- Array.Copy(rec, 0, buffer, offset, recLen);
- }
-
- totalRead += sz;
- numToRead -= sz;
- offset += sz;
- }
-
- this.entryOffset += totalRead;
-
- return totalRead;
+ if ((this.entryOffset + count) > this.entrySize)
+ count = (int)(this.entrySize - this.entryOffset);
+
+ if (count <= 0)
+ return 0;
+
+ int bytesRead = inputStream.Read(buffer, offset, count);
+
+ this.entryOffset += bytesRead;
+
+ return bytesRead;
}
-
+
+
/// <summary>
/// Closes this stream. Calls the TarBuffer's close() method.
/// The underlying stream is closed by the TarBuffer.
@@ -345,36 +286,8 @@
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="skipCount">
- /// The number of bytes to skip.
- /// </param>
- public void Skip(long skipCount)
- {
- // TODO: REVIEW efficiency of TarInputStream.Skip
- // This is horribly inefficient, but it ensures that we
- // properly skip over bytes via the TarBuffer...
- //
- byte[] skipBuf = new byte[8 * 1024];
-
- for (long num = skipCount; num > 0;) {
- int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num;
- int numRead = this.Read(skipBuf, 0, toRead);
-
- if (numRead == -1) {
- break;
- }
-
- num -= numRead;
- }
- }
-
+
+
/// <summary>
/// Return a value of true if marking is supported; false otherwise.
/// </summary>
@@ -384,8 +297,17 @@
return false;
}
}
-
- /// <summary>
+
+
+ /// <summary>
+ /// Get the position within the current entry.
+ /// </summary>
+ public long EntryPosition
+ {
+ get { return this.entryOffset; }
+ }
+
+ /// <summary>
/// Since we do not support marking just yet, we do nothing.
/// </summary>
/// <param name ="markLimit">
@@ -401,6 +323,29 @@
public void Reset()
{
}
+
+ /// <summary>
+ /// Read a tar entry header.
+ /// </summary>
+ protected byte[] ReadHeader()
+ {
+ byte[] headerBuffer = new byte[TarBuffer.BlockSize];
+
+ Array.Clear(headerBuffer, 0, headerBuffer.Length);
+
+ int bytesRead = inputStream.Read(headerBuffer, 0, headerBuffer.Length);
+
+ // Account for an improper archive that does not include terminating records
+ // such as those from Virtual Box.
+ // Fabricate a terminating record.
+ if (bytesRead == 0)
+ return headerBuffer;
+
+ if (bytesRead != headerBuffer.Length)
+ throw new TarException("Header is incomplete.");
+
+ return headerBuffer;
+ }
/// <summary>
/// Get the next entry in this tar archive. This will skip
@@ -425,7 +370,7 @@
SkipToNextEntry();
}
- byte[] headerBuf = this.buffer.ReadBlock();
+ byte[] headerBuf = ReadHeader();
if (headerBuf == null) {
this.hasHitEOF = true;
@@ -467,25 +412,25 @@
}
SkipToNextEntry();
- headerBuf = this.buffer.ReadBlock();
+ headerBuf = ReadHeader();
} else if (header.TypeFlag == TarHeader.LF_GHDR) { // POSIX global extended header
// Ignore things we dont understand completely for now
SkipToNextEntry();
- headerBuf = this.buffer.ReadBlock();
+ headerBuf = ReadHeader();
} else if (header.TypeFlag == TarHeader.LF_XHDR) { // POSIX extended header
// Ignore things we dont understand completely for now
SkipToNextEntry();
- headerBuf = this.buffer.ReadBlock();
+ headerBuf = ReadHeader();
} else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) {
// TODO: could show volume name when verbose
SkipToNextEntry();
- headerBuf = this.buffer.ReadBlock();
+ headerBuf = ReadHeader();
} else if (header.TypeFlag != TarHeader.LF_NORMAL &&
header.TypeFlag != TarHeader.LF_OLDNORM &&
header.TypeFlag != TarHeader.LF_DIR) {
// Ignore things we dont understand completely for now
SkipToNextEntry();
- headerBuf = this.buffer.ReadBlock();
+ headerBuf = ReadHeader();
}
if (this.entryFactory == null) {
@@ -538,15 +483,39 @@
void SkipToNextEntry()
{
- long numToSkip = this.entrySize - this.entryOffset;
-
- if (numToSkip > 0)
- {
- this.Skip(numToSkip);
- }
-
- this.readBuffer = null;
- }
+ // Skip the rest of the content.
+ long bytesToSkip = this.entrySize - this.entryOffset;
+
+ // Account for the pad out to a block.
+ long bytesInLastBlock = this.entrySize % TarBuffer.BlockSize;
+
+ if (bytesInLastBlock > 0)
+ {
+ bytesToSkip += TarBuffer.BlockSize - bytesInLastBlock;
+ }
+
+ // Skip by seeking if at all possible.
+ if (inputStream.CanSeek)
+ {
+ inputStream.Seek(bytesToSkip, SeekOrigin.Current);
+ return;
+ }
+
+ // Last resort is to skip by reading.
+ byte[] skipBuf = new byte[8 * 1024];
+
+ while (bytesToSkip > 0)
+ {
+ int bytesToRead = (bytesToSkip > skipBuf.Length) ? skipBuf.Length : (int)bytesToSkip;
+
+ int bytesRead = inputStream.Read(skipBuf, 0, bytesToRead);
+
+ if (bytesRead <= 0)
+ return;
+
+ bytesToSkip -= bytesRead;
+ }
+ }
/// <summary>
/// This interface is provided, along with the method <see cref="SetEntryFactory"/>, to allow
@@ -638,11 +607,6 @@
/// Number of bytes read for this entry so far
/// </summary>
protected long entryOffset;
-
- /// <summary>
- /// Buffer used with calls to <code>Read()</code>
- /// </summary>
- protected byte[] readBuffer;
/// <summary>
/// Working buffer