You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectChecker.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import static org.eclipse.jgit.util.RawParseUtils.match;
  46. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  47. import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
  48. import org.eclipse.jgit.errors.CorruptObjectException;
  49. import org.eclipse.jgit.util.MutableInteger;
  50. /**
  51. * Verifies that an object is formatted correctly.
  52. * <p>
  53. * Verifications made by this class only check that the fields of an object are
  54. * formatted correctly. The ObjectId checksum of the object is not verified, and
  55. * connectivity links between objects are also not verified. Its assumed that
  56. * the caller can provide both of these validations on its own.
  57. * <p>
  58. * Instances of this class are not thread safe, but they may be reused to
  59. * perform multiple object validations.
  60. */
  61. public class ObjectChecker {
  62. /** Header "tree " */
  63. public static final byte[] tree = Constants.encodeASCII("tree ");
  64. /** Header "parent " */
  65. public static final byte[] parent = Constants.encodeASCII("parent ");
  66. /** Header "author " */
  67. public static final byte[] author = Constants.encodeASCII("author ");
  68. /** Header "committer " */
  69. public static final byte[] committer = Constants.encodeASCII("committer ");
  70. /** Header "encoding " */
  71. public static final byte[] encoding = Constants.encodeASCII("encoding ");
  72. /** Header "object " */
  73. public static final byte[] object = Constants.encodeASCII("object ");
  74. /** Header "type " */
  75. public static final byte[] type = Constants.encodeASCII("type ");
  76. /** Header "tag " */
  77. public static final byte[] tag = Constants.encodeASCII("tag ");
  78. /** Header "tagger " */
  79. public static final byte[] tagger = Constants.encodeASCII("tagger ");
  80. private final MutableObjectId tempId = new MutableObjectId();
  81. private final MutableInteger ptrout = new MutableInteger();
  82. /**
  83. * Check an object for parsing errors.
  84. *
  85. * @param objType
  86. * type of the object. Must be a valid object type code in
  87. * {@link Constants}.
  88. * @param raw
  89. * the raw data which comprises the object. This should be in the
  90. * canonical format (that is the format used to generate the
  91. * ObjectId of the object). The array is never modified.
  92. * @throws CorruptObjectException
  93. * if an error is identified.
  94. */
  95. public void check(final int objType, final byte[] raw)
  96. throws CorruptObjectException {
  97. switch (objType) {
  98. case Constants.OBJ_COMMIT:
  99. checkCommit(raw);
  100. break;
  101. case Constants.OBJ_TAG:
  102. checkTag(raw);
  103. break;
  104. case Constants.OBJ_TREE:
  105. checkTree(raw);
  106. break;
  107. case Constants.OBJ_BLOB:
  108. checkBlob(raw);
  109. break;
  110. default:
  111. throw new CorruptObjectException("Invalid object type: " + objType);
  112. }
  113. }
  114. private int id(final byte[] raw, final int ptr) {
  115. try {
  116. tempId.fromString(raw, ptr);
  117. return ptr + Constants.OBJECT_ID_STRING_LENGTH;
  118. } catch (IllegalArgumentException e) {
  119. return -1;
  120. }
  121. }
  122. private int personIdent(final byte[] raw, int ptr) {
  123. final int emailB = nextLF(raw, ptr, '<');
  124. if (emailB == ptr || raw[emailB - 1] != '<')
  125. return -1;
  126. final int emailE = nextLF(raw, emailB, '>');
  127. if (emailE == emailB || raw[emailE - 1] != '>')
  128. return -1;
  129. if (emailE == raw.length || raw[emailE] != ' ')
  130. return -1;
  131. parseBase10(raw, emailE + 1, ptrout); // when
  132. ptr = ptrout.value;
  133. if (emailE + 1 == ptr)
  134. return -1;
  135. if (ptr == raw.length || raw[ptr] != ' ')
  136. return -1;
  137. parseBase10(raw, ptr + 1, ptrout); // tz offset
  138. if (ptr + 1 == ptrout.value)
  139. return -1;
  140. return ptrout.value;
  141. }
  142. /**
  143. * Check a commit for errors.
  144. *
  145. * @param raw
  146. * the commit data. The array is never modified.
  147. * @throws CorruptObjectException
  148. * if any error was detected.
  149. */
  150. public void checkCommit(final byte[] raw) throws CorruptObjectException {
  151. int ptr = 0;
  152. if ((ptr = match(raw, ptr, tree)) < 0)
  153. throw new CorruptObjectException("no tree header");
  154. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  155. throw new CorruptObjectException("invalid tree");
  156. while (match(raw, ptr, parent) >= 0) {
  157. ptr += parent.length;
  158. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  159. throw new CorruptObjectException("invalid parent");
  160. }
  161. if ((ptr = match(raw, ptr, author)) < 0)
  162. throw new CorruptObjectException("no author");
  163. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  164. throw new CorruptObjectException("invalid author");
  165. if ((ptr = match(raw, ptr, committer)) < 0)
  166. throw new CorruptObjectException("no committer");
  167. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  168. throw new CorruptObjectException("invalid committer");
  169. }
  170. /**
  171. * Check an annotated tag for errors.
  172. *
  173. * @param raw
  174. * the tag data. The array is never modified.
  175. * @throws CorruptObjectException
  176. * if any error was detected.
  177. */
  178. public void checkTag(final byte[] raw) throws CorruptObjectException {
  179. int ptr = 0;
  180. if ((ptr = match(raw, ptr, object)) < 0)
  181. throw new CorruptObjectException("no object header");
  182. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  183. throw new CorruptObjectException("invalid object");
  184. if ((ptr = match(raw, ptr, type)) < 0)
  185. throw new CorruptObjectException("no type header");
  186. ptr = nextLF(raw, ptr);
  187. if ((ptr = match(raw, ptr, tag)) < 0)
  188. throw new CorruptObjectException("no tag header");
  189. ptr = nextLF(raw, ptr);
  190. if ((ptr = match(raw, ptr, tagger)) > 0) {
  191. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  192. throw new CorruptObjectException("invalid tagger");
  193. }
  194. }
  195. private static int lastPathChar(final int mode) {
  196. return FileMode.TREE.equals(mode) ? '/' : '\0';
  197. }
  198. private static int pathCompare(final byte[] raw, int aPos, final int aEnd,
  199. final int aMode, int bPos, final int bEnd, final int bMode) {
  200. while (aPos < aEnd && bPos < bEnd) {
  201. final int cmp = (raw[aPos++] & 0xff) - (raw[bPos++] & 0xff);
  202. if (cmp != 0)
  203. return cmp;
  204. }
  205. if (aPos < aEnd)
  206. return (raw[aPos] & 0xff) - lastPathChar(bMode);
  207. if (bPos < bEnd)
  208. return lastPathChar(aMode) - (raw[bPos] & 0xff);
  209. return 0;
  210. }
  211. private static boolean duplicateName(final byte[] raw,
  212. final int thisNamePos, final int thisNameEnd) {
  213. final int sz = raw.length;
  214. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  215. for (;;) {
  216. int nextMode = 0;
  217. for (;;) {
  218. if (nextPtr >= sz)
  219. return false;
  220. final byte c = raw[nextPtr++];
  221. if (' ' == c)
  222. break;
  223. nextMode <<= 3;
  224. nextMode += c - '0';
  225. }
  226. final int nextNamePos = nextPtr;
  227. for (;;) {
  228. if (nextPtr == sz)
  229. return false;
  230. final byte c = raw[nextPtr++];
  231. if (c == 0)
  232. break;
  233. }
  234. if (nextNamePos + 1 == nextPtr)
  235. return false;
  236. final int cmp = pathCompare(raw, thisNamePos, thisNameEnd,
  237. FileMode.TREE.getBits(), nextNamePos, nextPtr - 1, nextMode);
  238. if (cmp < 0)
  239. return false;
  240. else if (cmp == 0)
  241. return true;
  242. nextPtr += Constants.OBJECT_ID_LENGTH;
  243. }
  244. }
  245. /**
  246. * Check a canonical formatted tree for errors.
  247. *
  248. * @param raw
  249. * the raw tree data. The array is never modified.
  250. * @throws CorruptObjectException
  251. * if any error was detected.
  252. */
  253. public void checkTree(final byte[] raw) throws CorruptObjectException {
  254. final int sz = raw.length;
  255. int ptr = 0;
  256. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  257. while (ptr < sz) {
  258. int thisMode = 0;
  259. for (;;) {
  260. if (ptr == sz)
  261. throw new CorruptObjectException("truncated in mode");
  262. final byte c = raw[ptr++];
  263. if (' ' == c)
  264. break;
  265. if (c < '0' || c > '7')
  266. throw new CorruptObjectException("invalid mode character");
  267. if (thisMode == 0 && c == '0')
  268. throw new CorruptObjectException("mode starts with '0'");
  269. thisMode <<= 3;
  270. thisMode += c - '0';
  271. }
  272. if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
  273. throw new CorruptObjectException("invalid mode " + thisMode);
  274. final int thisNameB = ptr;
  275. for (;;) {
  276. if (ptr == sz)
  277. throw new CorruptObjectException("truncated in name");
  278. final byte c = raw[ptr++];
  279. if (c == 0)
  280. break;
  281. if (c == '/')
  282. throw new CorruptObjectException("name contains '/'");
  283. }
  284. if (thisNameB + 1 == ptr)
  285. throw new CorruptObjectException("zero length name");
  286. if (raw[thisNameB] == '.') {
  287. final int nameLen = (ptr - 1) - thisNameB;
  288. if (nameLen == 1)
  289. throw new CorruptObjectException("invalid name '.'");
  290. if (nameLen == 2 && raw[thisNameB + 1] == '.')
  291. throw new CorruptObjectException("invalid name '..'");
  292. }
  293. if (duplicateName(raw, thisNameB, ptr - 1))
  294. throw new CorruptObjectException("duplicate entry names");
  295. if (lastNameB != 0) {
  296. final int cmp = pathCompare(raw, lastNameB, lastNameE,
  297. lastMode, thisNameB, ptr - 1, thisMode);
  298. if (cmp > 0)
  299. throw new CorruptObjectException("incorrectly sorted");
  300. }
  301. lastNameB = thisNameB;
  302. lastNameE = ptr - 1;
  303. lastMode = thisMode;
  304. ptr += Constants.OBJECT_ID_LENGTH;
  305. if (ptr > sz)
  306. throw new CorruptObjectException("truncated in object id");
  307. }
  308. }
  309. /**
  310. * Check a blob for errors.
  311. *
  312. * @param raw
  313. * the blob data. The array is never modified.
  314. * @throws CorruptObjectException
  315. * if any error was detected.
  316. */
  317. public void checkBlob(final byte[] raw) throws CorruptObjectException {
  318. // We can always assume the blob is valid.
  319. }
  320. }