2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
/// <summary>Part of a "GIT binary patch" to describe the pre-image or post-image</summary>
52
public class BinaryHunk
54
private static readonly byte[] LITERAL = Constants.EncodeASCII("literal ");
56
private static readonly byte[] DELTA = Constants.EncodeASCII("delta ");
58
/// <summary>Type of information stored in a binary hunk.</summary>
59
/// <remarks>Type of information stored in a binary hunk.</remarks>
66
private readonly FileHeader file;
70
/// <see cref="file">file</see>
71
/// .buf to the "literal" or "delta " line.
73
internal readonly int startOffset;
76
/// Position 1 past the end of this hunk within
77
/// <see cref="file">file</see>
80
internal int endOffset;
82
/// <summary>Type of the data meaning.</summary>
83
/// <remarks>Type of the data meaning.</remarks>
84
private BinaryHunk.Type type;
86
/// <summary>Inflated length of the data.</summary>
87
/// <remarks>Inflated length of the data.</remarks>
90
internal BinaryHunk(FileHeader fh, int offset)
96
/// <returns>header for the file this hunk applies to</returns>
97
public virtual FileHeader GetFileHeader()
102
/// <returns>the byte array holding this hunk's patch script.</returns>
103
public virtual byte[] GetBuffer()
109
/// offset the start of this hunk in
110
/// <see cref="GetBuffer()">GetBuffer()</see>
113
public virtual int GetStartOffset()
119
/// offset one past the end of the hunk in
120
/// <see cref="GetBuffer()">GetBuffer()</see>
123
public virtual int GetEndOffset()
128
/// <returns>type of this binary hunk</returns>
129
public virtual BinaryHunk.Type GetType()
134
/// <returns>inflated size of this hunk's data</returns>
135
public virtual int GetSize()
140
internal virtual int ParseHunk(int ptr, int end)
142
byte[] buf = file.buf;
143
if (RawParseUtils.Match(buf, ptr, LITERAL) >= 0)
145
type = BinaryHunk.Type.LITERAL_DEFLATED;
146
length = RawParseUtils.ParseBase10(buf, ptr + LITERAL.Length, null);
150
if (RawParseUtils.Match(buf, ptr, DELTA) >= 0)
152
type = BinaryHunk.Type.DELTA_DEFLATED;
153
length = RawParseUtils.ParseBase10(buf, ptr + DELTA.Length, null);
157
// Not a valid binary hunk. Signal to the caller that
158
// we cannot parse any further and that this line should
159
// be treated otherwise.
164
ptr = RawParseUtils.NextLF(buf, ptr);
165
// Skip until the first blank line; that is the end of the binary
166
// encoded information in this hunk. To save time we don't do a
167
// validation of the binary data at this point.
171
bool empty = buf[ptr] == '\n';
172
ptr = RawParseUtils.NextLF(buf, ptr);