~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Patch/BinaryHunk.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using NGit;
 
45
using NGit.Patch;
 
46
using NGit.Util;
 
47
using Sharpen;
 
48
 
 
49
namespace NGit.Patch
 
50
{
 
51
        /// <summary>Part of a "GIT binary patch" to describe the pre-image or post-image</summary>
 
52
        public class BinaryHunk
 
53
        {
 
54
                private static readonly byte[] LITERAL = Constants.EncodeASCII("literal ");
 
55
 
 
56
                private static readonly byte[] DELTA = Constants.EncodeASCII("delta ");
 
57
 
 
58
                /// <summary>Type of information stored in a binary hunk.</summary>
 
59
                /// <remarks>Type of information stored in a binary hunk.</remarks>
 
60
                public enum Type
 
61
                {
 
62
                        LITERAL_DEFLATED,
 
63
                        DELTA_DEFLATED
 
64
                }
 
65
 
 
66
                private readonly FileHeader file;
 
67
 
 
68
                /// <summary>
 
69
                /// Offset within
 
70
                /// <see cref="file">file</see>
 
71
                /// .buf to the "literal" or "delta " line.
 
72
                /// </summary>
 
73
                internal readonly int startOffset;
 
74
 
 
75
                /// <summary>
 
76
                /// Position 1 past the end of this hunk within
 
77
                /// <see cref="file">file</see>
 
78
                /// 's buf.
 
79
                /// </summary>
 
80
                internal int endOffset;
 
81
 
 
82
                /// <summary>Type of the data meaning.</summary>
 
83
                /// <remarks>Type of the data meaning.</remarks>
 
84
                private BinaryHunk.Type type;
 
85
 
 
86
                /// <summary>Inflated length of the data.</summary>
 
87
                /// <remarks>Inflated length of the data.</remarks>
 
88
                private int length;
 
89
 
 
90
                internal BinaryHunk(FileHeader fh, int offset)
 
91
                {
 
92
                        file = fh;
 
93
                        startOffset = offset;
 
94
                }
 
95
 
 
96
                /// <returns>header for the file this hunk applies to</returns>
 
97
                public virtual FileHeader GetFileHeader()
 
98
                {
 
99
                        return file;
 
100
                }
 
101
 
 
102
                /// <returns>the byte array holding this hunk's patch script.</returns>
 
103
                public virtual byte[] GetBuffer()
 
104
                {
 
105
                        return file.buf;
 
106
                }
 
107
 
 
108
                /// <returns>
 
109
                /// offset the start of this hunk in
 
110
                /// <see cref="GetBuffer()">GetBuffer()</see>
 
111
                /// .
 
112
                /// </returns>
 
113
                public virtual int GetStartOffset()
 
114
                {
 
115
                        return startOffset;
 
116
                }
 
117
 
 
118
                /// <returns>
 
119
                /// offset one past the end of the hunk in
 
120
                /// <see cref="GetBuffer()">GetBuffer()</see>
 
121
                /// .
 
122
                /// </returns>
 
123
                public virtual int GetEndOffset()
 
124
                {
 
125
                        return endOffset;
 
126
                }
 
127
 
 
128
                /// <returns>type of this binary hunk</returns>
 
129
                public virtual BinaryHunk.Type GetType()
 
130
                {
 
131
                        return type;
 
132
                }
 
133
 
 
134
                /// <returns>inflated size of this hunk's data</returns>
 
135
                public virtual int GetSize()
 
136
                {
 
137
                        return length;
 
138
                }
 
139
 
 
140
                internal virtual int ParseHunk(int ptr, int end)
 
141
                {
 
142
                        byte[] buf = file.buf;
 
143
                        if (RawParseUtils.Match(buf, ptr, LITERAL) >= 0)
 
144
                        {
 
145
                                type = BinaryHunk.Type.LITERAL_DEFLATED;
 
146
                                length = RawParseUtils.ParseBase10(buf, ptr + LITERAL.Length, null);
 
147
                        }
 
148
                        else
 
149
                        {
 
150
                                if (RawParseUtils.Match(buf, ptr, DELTA) >= 0)
 
151
                                {
 
152
                                        type = BinaryHunk.Type.DELTA_DEFLATED;
 
153
                                        length = RawParseUtils.ParseBase10(buf, ptr + DELTA.Length, null);
 
154
                                }
 
155
                                else
 
156
                                {
 
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.
 
160
                                        //
 
161
                                        return -1;
 
162
                                }
 
163
                        }
 
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.
 
168
                        //
 
169
                        while (ptr < end)
 
170
                        {
 
171
                                bool empty = buf[ptr] == '\n';
 
172
                                ptr = RawParseUtils.NextLF(buf, ptr);
 
173
                                if (empty)
 
174
                                {
 
175
                                        break;
 
176
                                }
 
177
                        }
 
178
                        return ptr;
 
179
                }
 
180
        }
 
181
}