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.
52
/// <summary>Verifies that an object is formatted correctly.</summary>
54
/// Verifies that an object is formatted correctly.
56
/// Verifications made by this class only check that the fields of an object are
57
/// formatted correctly. The ObjectId checksum of the object is not verified, and
58
/// connectivity links between objects are also not verified. Its assumed that
59
/// the caller can provide both of these validations on its own.
61
/// Instances of this class are not thread safe, but they may be reused to
62
/// perform multiple object validations.
64
public class ObjectChecker
66
/// <summary>Header "tree "</summary>
67
public static readonly byte[] tree = Constants.EncodeASCII("tree ");
69
/// <summary>Header "parent "</summary>
70
public static readonly byte[] parent = Constants.EncodeASCII("parent ");
72
/// <summary>Header "author "</summary>
73
public static readonly byte[] author = Constants.EncodeASCII("author ");
75
/// <summary>Header "committer "</summary>
76
public static readonly byte[] committer = Constants.EncodeASCII("committer ");
78
/// <summary>Header "encoding "</summary>
79
public static readonly byte[] encoding = Constants.EncodeASCII("encoding ");
81
/// <summary>Header "object "</summary>
82
public static readonly byte[] @object = Constants.EncodeASCII("object ");
84
/// <summary>Header "type "</summary>
85
public static readonly byte[] type = Constants.EncodeASCII("type ");
87
/// <summary>Header "tag "</summary>
88
public static readonly byte[] tag = Constants.EncodeASCII("tag ");
90
/// <summary>Header "tagger "</summary>
91
public static readonly byte[] tagger = Constants.EncodeASCII("tagger ");
93
private readonly MutableObjectId tempId = new MutableObjectId();
95
private readonly MutableInteger ptrout = new MutableInteger();
97
/// <summary>Check an object for parsing errors.</summary>
98
/// <remarks>Check an object for parsing errors.</remarks>
99
/// <param name="objType">
100
/// type of the object. Must be a valid object type code in
101
/// <see cref="Constants">Constants</see>
104
/// <param name="raw">
105
/// the raw data which comprises the object. This should be in the
106
/// canonical format (that is the format used to generate the
107
/// ObjectId of the object). The array is never modified.
109
/// <exception cref="NGit.Errors.CorruptObjectException">if an error is identified.</exception>
110
public virtual void Check(int objType, byte[] raw)
114
case Constants.OBJ_COMMIT:
120
case Constants.OBJ_TAG:
126
case Constants.OBJ_TREE:
132
case Constants.OBJ_BLOB:
140
throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().corruptObjectInvalidType2
146
private int Id(byte[] raw, int ptr)
150
tempId.FromString(raw, ptr);
151
return ptr + Constants.OBJECT_ID_STRING_LENGTH;
153
catch (ArgumentException)
159
private int PersonIdent(byte[] raw, int ptr)
161
int emailB = RawParseUtils.NextLF(raw, ptr, '<');
162
if (emailB == ptr || raw[emailB - 1] != '<')
166
int emailE = RawParseUtils.NextLF(raw, emailB, '>');
167
if (emailE == emailB || raw[emailE - 1] != '>')
171
if (emailE == raw.Length || raw[emailE] != ' ')
175
RawParseUtils.ParseBase10(raw, emailE + 1, ptrout);
178
if (emailE + 1 == ptr)
182
if (ptr == raw.Length || raw[ptr] != ' ')
186
RawParseUtils.ParseBase10(raw, ptr + 1, ptrout);
188
if (ptr + 1 == ptrout.value)
195
/// <summary>Check a commit for errors.</summary>
196
/// <remarks>Check a commit for errors.</remarks>
197
/// <param name="raw">the commit data. The array is never modified.</param>
198
/// <exception cref="NGit.Errors.CorruptObjectException">if any error was detected.</exception>
199
public virtual void CheckCommit(byte[] raw)
202
if ((ptr = RawParseUtils.Match(raw, ptr, tree)) < 0)
204
throw new CorruptObjectException("no tree header");
206
if ((ptr = Id(raw, ptr)) < 0 || raw[ptr++] != '\n')
208
throw new CorruptObjectException("invalid tree");
210
while (RawParseUtils.Match(raw, ptr, parent) >= 0)
212
ptr += parent.Length;
213
if ((ptr = Id(raw, ptr)) < 0 || raw[ptr++] != '\n')
215
throw new CorruptObjectException("invalid parent");
218
if ((ptr = RawParseUtils.Match(raw, ptr, author)) < 0)
220
throw new CorruptObjectException("no author");
222
if ((ptr = PersonIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
224
throw new CorruptObjectException("invalid author");
226
if ((ptr = RawParseUtils.Match(raw, ptr, committer)) < 0)
228
throw new CorruptObjectException("no committer");
230
if ((ptr = PersonIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
232
throw new CorruptObjectException("invalid committer");
236
/// <summary>Check an annotated tag for errors.</summary>
237
/// <remarks>Check an annotated tag for errors.</remarks>
238
/// <param name="raw">the tag data. The array is never modified.</param>
239
/// <exception cref="NGit.Errors.CorruptObjectException">if any error was detected.</exception>
240
public virtual void CheckTag(byte[] raw)
243
if ((ptr = RawParseUtils.Match(raw, ptr, @object)) < 0)
245
throw new CorruptObjectException("no object header");
247
if ((ptr = Id(raw, ptr)) < 0 || raw[ptr++] != '\n')
249
throw new CorruptObjectException("invalid object");
251
if ((ptr = RawParseUtils.Match(raw, ptr, type)) < 0)
253
throw new CorruptObjectException("no type header");
255
ptr = RawParseUtils.NextLF(raw, ptr);
256
if ((ptr = RawParseUtils.Match(raw, ptr, tag)) < 0)
258
throw new CorruptObjectException("no tag header");
260
ptr = RawParseUtils.NextLF(raw, ptr);
261
if ((ptr = RawParseUtils.Match(raw, ptr, tagger)) > 0)
263
if ((ptr = PersonIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
265
throw new CorruptObjectException("invalid tagger");
270
private static int LastPathChar(int mode)
272
return FileMode.TREE.Equals(mode) ? '/' : '\0';
275
private static int PathCompare(byte[] raw, int aPos, int aEnd, int aMode, int bPos
276
, int bEnd, int bMode)
278
while (aPos < aEnd && bPos < bEnd)
280
int cmp = (raw[aPos++] & unchecked((int)(0xff))) - (raw[bPos++] & unchecked((int)
289
return (raw[aPos] & unchecked((int)(0xff))) - LastPathChar(bMode);
293
return LastPathChar(aMode) - (raw[bPos] & unchecked((int)(0xff)));
298
private static bool DuplicateName(byte[] raw, int thisNamePos, int thisNameEnd)
301
int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
311
byte c = raw[nextPtr++];
319
int nextNamePos = nextPtr;
326
byte c = raw[nextPtr++];
332
if (nextNamePos + 1 == nextPtr)
336
int cmp = PathCompare(raw, thisNamePos, thisNameEnd, FileMode.TREE.GetBits(), nextNamePos
337
, nextPtr - 1, nextMode);
349
nextPtr += Constants.OBJECT_ID_LENGTH;
353
/// <summary>Check a canonical formatted tree for errors.</summary>
354
/// <remarks>Check a canonical formatted tree for errors.</remarks>
355
/// <param name="raw">the raw tree data. The array is never modified.</param>
356
/// <exception cref="NGit.Errors.CorruptObjectException">if any error was detected.</exception>
357
public virtual void CheckTree(byte[] raw)
371
throw new CorruptObjectException("truncated in mode");
378
if (((sbyte)c) < '0' || c > '7')
380
throw new CorruptObjectException("invalid mode character");
382
if (thisMode == 0 && c == '0')
384
throw new CorruptObjectException("mode starts with '0'");
389
if (FileMode.FromBits(thisMode).GetObjectType() == Constants.OBJ_BAD)
391
throw new CorruptObjectException("invalid mode " + thisMode);
398
throw new CorruptObjectException("truncated in name");
407
throw new CorruptObjectException("name contains '/'");
410
if (thisNameB + 1 == ptr)
412
throw new CorruptObjectException("zero length name");
414
if (raw[thisNameB] == '.')
416
int nameLen = (ptr - 1) - thisNameB;
419
throw new CorruptObjectException("invalid name '.'");
421
if (nameLen == 2 && raw[thisNameB + 1] == '.')
423
throw new CorruptObjectException("invalid name '..'");
426
if (DuplicateName(raw, thisNameB, ptr - 1))
428
throw new CorruptObjectException("duplicate entry names");
432
int cmp = PathCompare(raw, lastNameB, lastNameE, lastMode, thisNameB, ptr - 1, thisMode
436
throw new CorruptObjectException("incorrectly sorted");
439
lastNameB = thisNameB;
442
ptr += Constants.OBJECT_ID_LENGTH;
445
throw new CorruptObjectException("truncated in object id");
450
/// <summary>Check a blob for errors.</summary>
451
/// <remarks>Check a blob for errors.</remarks>
452
/// <param name="raw">the blob data. The array is never modified.</param>
453
/// <exception cref="NGit.Errors.CorruptObjectException">if any error was detected.</exception>
454
public virtual void CheckBlob(byte[] raw)
457
// We can always assume the blob is valid.