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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.Pack/DeltaEncoder.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 System;
 
45
using NGit;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit.Storage.Pack
 
49
{
 
50
        /// <summary>
 
51
        /// Encodes an instruction stream for
 
52
        /// <see cref="BinaryDelta">BinaryDelta</see>
 
53
        /// .
 
54
        /// </summary>
 
55
        public class DeltaEncoder
 
56
        {
 
57
                /// <summary>Maximum number of bytes to be copied in pack v2 format.</summary>
 
58
                /// <remarks>
 
59
                /// Maximum number of bytes to be copied in pack v2 format.
 
60
                /// <p>
 
61
                /// Historical limitations have this at 64k, even though current delta
 
62
                /// decoders recognize larger copy instructions.
 
63
                /// </remarks>
 
64
                private const int MAX_V2_COPY = unchecked((int)(0x10000));
 
65
 
 
66
                /// <summary>Maximum number of bytes used by a copy instruction.</summary>
 
67
                /// <remarks>Maximum number of bytes used by a copy instruction.</remarks>
 
68
                private const int MAX_COPY_CMD_SIZE = 8;
 
69
 
 
70
                /// <summary>Maximum length that an an insert command can encode at once.</summary>
 
71
                /// <remarks>Maximum length that an an insert command can encode at once.</remarks>
 
72
                private const int MAX_INSERT_DATA_SIZE = 127;
 
73
 
 
74
                private readonly OutputStream @out;
 
75
 
 
76
                private readonly byte[] buf = new byte[MAX_COPY_CMD_SIZE * 4];
 
77
 
 
78
                private readonly int limit;
 
79
 
 
80
                private int size;
 
81
 
 
82
                /// <summary>Create an encoder with no upper bound on the instruction stream size.</summary>
 
83
                /// <remarks>Create an encoder with no upper bound on the instruction stream size.</remarks>
 
84
                /// <param name="out">buffer to store the instructions written.</param>
 
85
                /// <param name="baseSize">size of the base object, in bytes.</param>
 
86
                /// <param name="resultSize">
 
87
                /// size of the resulting object, after applying this instruction
 
88
                /// stream to the base object, in bytes.
 
89
                /// </param>
 
90
                /// <exception cref="System.IO.IOException">
 
91
                /// the output buffer cannot store the instruction stream's
 
92
                /// header with the size fields.
 
93
                /// </exception>
 
94
                public DeltaEncoder(OutputStream @out, long baseSize, long resultSize) : this(@out
 
95
                        , baseSize, resultSize, 0)
 
96
                {
 
97
                }
 
98
 
 
99
                /// <summary>Create an encoder with an upper limit on the instruction size.</summary>
 
100
                /// <remarks>Create an encoder with an upper limit on the instruction size.</remarks>
 
101
                /// <param name="out">buffer to store the instructions written.</param>
 
102
                /// <param name="baseSize">size of the base object, in bytes.</param>
 
103
                /// <param name="resultSize">
 
104
                /// size of the resulting object, after applying this instruction
 
105
                /// stream to the base object, in bytes.
 
106
                /// </param>
 
107
                /// <param name="limit">
 
108
                /// maximum number of bytes to write to the out buffer declaring
 
109
                /// the stream is over limit and should be discarded. May be 0 to
 
110
                /// specify an infinite limit.
 
111
                /// </param>
 
112
                /// <exception cref="System.IO.IOException">
 
113
                /// the output buffer cannot store the instruction stream's
 
114
                /// header with the size fields.
 
115
                /// </exception>
 
116
                public DeltaEncoder(OutputStream @out, long baseSize, long resultSize, int limit)
 
117
                {
 
118
                        // private static final int MAX_V3_COPY = (0xff << 16) | (0xff << 8) | 0xff;
 
119
                        this.@out = @out;
 
120
                        this.limit = limit;
 
121
                        WriteVarint(baseSize);
 
122
                        WriteVarint(resultSize);
 
123
                }
 
124
 
 
125
                /// <exception cref="System.IO.IOException"></exception>
 
126
                private void WriteVarint(long sz)
 
127
                {
 
128
                        int p = 0;
 
129
                        while (sz >= unchecked((int)(0x80)))
 
130
                        {
 
131
                                buf[p++] = unchecked((byte)(unchecked((int)(0x80)) | (((int)sz) & unchecked((int)
 
132
                                        (0x7f)))));
 
133
                                sz = (long)(((ulong)sz) >> 7);
 
134
                        }
 
135
                        buf[p++] = unchecked((byte)(((int)sz) & unchecked((int)(0x7f))));
 
136
                        size += p;
 
137
                        if (limit <= 0 || size < limit)
 
138
                        {
 
139
                                @out.Write(buf, 0, p);
 
140
                        }
 
141
                }
 
142
 
 
143
                /// <returns>current size of the delta stream, in bytes.</returns>
 
144
                public virtual int GetSize()
 
145
                {
 
146
                        return size;
 
147
                }
 
148
 
 
149
                /// <summary>Insert a literal string of text, in UTF-8 encoding.</summary>
 
150
                /// <remarks>Insert a literal string of text, in UTF-8 encoding.</remarks>
 
151
                /// <param name="text">the string to insert.</param>
 
152
                /// <returns>
 
153
                /// true if the insert fits within the limit; false if the insert
 
154
                /// would cause the instruction stream to exceed the limit.
 
155
                /// </returns>
 
156
                /// <exception cref="System.IO.IOException">the instruction buffer can't store the instructions.
 
157
                ///     </exception>
 
158
                public virtual bool Insert(string text)
 
159
                {
 
160
                        return Insert(Constants.Encode(text));
 
161
                }
 
162
 
 
163
                /// <summary>Insert a literal binary sequence.</summary>
 
164
                /// <remarks>Insert a literal binary sequence.</remarks>
 
165
                /// <param name="text">the binary to insert.</param>
 
166
                /// <returns>
 
167
                /// true if the insert fits within the limit; false if the insert
 
168
                /// would cause the instruction stream to exceed the limit.
 
169
                /// </returns>
 
170
                /// <exception cref="System.IO.IOException">the instruction buffer can't store the instructions.
 
171
                ///     </exception>
 
172
                public virtual bool Insert(byte[] text)
 
173
                {
 
174
                        return Insert(text, 0, text.Length);
 
175
                }
 
176
 
 
177
                /// <summary>Insert a literal binary sequence.</summary>
 
178
                /// <remarks>Insert a literal binary sequence.</remarks>
 
179
                /// <param name="text">the binary to insert.</param>
 
180
                /// <param name="off">
 
181
                /// offset within
 
182
                /// <code>text</code>
 
183
                /// to start copying from.
 
184
                /// </param>
 
185
                /// <param name="cnt">number of bytes to insert.</param>
 
186
                /// <returns>
 
187
                /// true if the insert fits within the limit; false if the insert
 
188
                /// would cause the instruction stream to exceed the limit.
 
189
                /// </returns>
 
190
                /// <exception cref="System.IO.IOException">the instruction buffer can't store the instructions.
 
191
                ///     </exception>
 
192
                public virtual bool Insert(byte[] text, int off, int cnt)
 
193
                {
 
194
                        if (cnt <= 0)
 
195
                        {
 
196
                                return true;
 
197
                        }
 
198
                        if (0 < limit)
 
199
                        {
 
200
                                int hdrs = cnt / MAX_INSERT_DATA_SIZE;
 
201
                                if (cnt % MAX_INSERT_DATA_SIZE != 0)
 
202
                                {
 
203
                                        hdrs++;
 
204
                                }
 
205
                                if (limit < size + hdrs + cnt)
 
206
                                {
 
207
                                        return false;
 
208
                                }
 
209
                        }
 
210
                        do
 
211
                        {
 
212
                                int n = Math.Min(MAX_INSERT_DATA_SIZE, cnt);
 
213
                                @out.Write(unchecked((byte)n));
 
214
                                @out.Write(text, off, n);
 
215
                                off += n;
 
216
                                cnt -= n;
 
217
                                size += 1 + n;
 
218
                        }
 
219
                        while (0 < cnt);
 
220
                        return true;
 
221
                }
 
222
 
 
223
                /// <summary>Create a copy instruction to copy from the base object.</summary>
 
224
                /// <remarks>Create a copy instruction to copy from the base object.</remarks>
 
225
                /// <param name="offset">
 
226
                /// position in the base object to copy from. This is absolute,
 
227
                /// from the beginning of the base.
 
228
                /// </param>
 
229
                /// <param name="cnt">number of bytes to copy.</param>
 
230
                /// <returns>
 
231
                /// true if the copy fits within the limit; false if the copy
 
232
                /// would cause the instruction stream to exceed the limit.
 
233
                /// </returns>
 
234
                /// <exception cref="System.IO.IOException">the instruction buffer cannot store the instructions.
 
235
                ///     </exception>
 
236
                public virtual bool Copy(long offset, int cnt)
 
237
                {
 
238
                        if (cnt == 0)
 
239
                        {
 
240
                                return true;
 
241
                        }
 
242
                        int p = 0;
 
243
                        // We cannot encode more than MAX_V2_COPY bytes in a single
 
244
                        // command, so encode that much and start a new command.
 
245
                        // This limit is imposed by the pack file format rules.
 
246
                        //
 
247
                        while (MAX_V2_COPY < cnt)
 
248
                        {
 
249
                                p = EncodeCopy(p, offset, MAX_V2_COPY);
 
250
                                offset += MAX_V2_COPY;
 
251
                                cnt -= MAX_V2_COPY;
 
252
                                if (buf.Length < p + MAX_COPY_CMD_SIZE)
 
253
                                {
 
254
                                        if (0 < limit && limit < size + p)
 
255
                                        {
 
256
                                                return false;
 
257
                                        }
 
258
                                        @out.Write(buf, 0, p);
 
259
                                        size += p;
 
260
                                        p = 0;
 
261
                                }
 
262
                        }
 
263
                        p = EncodeCopy(p, offset, cnt);
 
264
                        if (0 < limit && limit < size + p)
 
265
                        {
 
266
                                return false;
 
267
                        }
 
268
                        @out.Write(buf, 0, p);
 
269
                        size += p;
 
270
                        return true;
 
271
                }
 
272
 
 
273
                private int EncodeCopy(int p, long offset, int cnt)
 
274
                {
 
275
                        int cmd = unchecked((int)(0x80));
 
276
                        int cmdPtr = p++;
 
277
                        // save room for the command
 
278
                        if ((offset & unchecked((int)(0xff))) != 0)
 
279
                        {
 
280
                                cmd |= unchecked((int)(0x01));
 
281
                                buf[p++] = unchecked((byte)(offset & unchecked((int)(0xff))));
 
282
                        }
 
283
                        if ((offset & (unchecked((int)(0xff)) << 8)) != 0)
 
284
                        {
 
285
                                cmd |= unchecked((int)(0x02));
 
286
                                buf[p++] = unchecked((byte)(((long)(((ulong)offset) >> 8)) & unchecked((int)(0xff
 
287
                                        ))));
 
288
                        }
 
289
                        if ((offset & (unchecked((int)(0xff)) << 16)) != 0)
 
290
                        {
 
291
                                cmd |= unchecked((int)(0x04));
 
292
                                buf[p++] = unchecked((byte)(((long)(((ulong)offset) >> 16)) & unchecked((int)(0xff
 
293
                                        ))));
 
294
                        }
 
295
                        if ((offset & (unchecked((int)(0xff)) << 24)) != 0)
 
296
                        {
 
297
                                cmd |= unchecked((int)(0x08));
 
298
                                buf[p++] = unchecked((byte)(((long)(((ulong)offset) >> 24)) & unchecked((int)(0xff
 
299
                                        ))));
 
300
                        }
 
301
                        if (cnt != MAX_V2_COPY)
 
302
                        {
 
303
                                if ((cnt & unchecked((int)(0xff))) != 0)
 
304
                                {
 
305
                                        cmd |= unchecked((int)(0x10));
 
306
                                        buf[p++] = unchecked((byte)(cnt & unchecked((int)(0xff))));
 
307
                                }
 
308
                                if ((cnt & (unchecked((int)(0xff)) << 8)) != 0)
 
309
                                {
 
310
                                        cmd |= unchecked((int)(0x20));
 
311
                                        buf[p++] = unchecked((byte)(((int)(((uint)cnt) >> 8)) & unchecked((int)(0xff))));
 
312
                                }
 
313
                                if ((cnt & (unchecked((int)(0xff)) << 16)) != 0)
 
314
                                {
 
315
                                        cmd |= unchecked((int)(0x40));
 
316
                                        buf[p++] = unchecked((byte)(((int)(((uint)cnt) >> 16)) & unchecked((int)(0xff))));
 
317
                                }
 
318
                        }
 
319
                        buf[cmdPtr] = unchecked((byte)cmd);
 
320
                        return p;
 
321
                }
 
322
        }
 
323
}