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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 java.text.MessageFormat;
  49. import org.eclipse.jgit.JGitText;
  50. import org.eclipse.jgit.errors.CorruptObjectException;
  51. import org.eclipse.jgit.util.MutableInteger;
  52. /**
  53. * Verifies that an object is formatted correctly.
  54. * <p>
  55. * Verifications made by this class only check that the fields of an object are
  56. * formatted correctly. The ObjectId checksum of the object is not verified, and
  57. * connectivity links between objects are also not verified. Its assumed that
  58. * the caller can provide both of these validations on its own.
  59. * <p>
  60. * Instances of this class are not thread safe, but they may be reused to
  61. * perform multiple object validations.
  62. */
  63. public class ObjectChecker {
  64. /** Header "tree " */
  65. public static final byte[] tree = Constants.encodeASCII("tree ");
  66. /** Header "parent " */
  67. public static final byte[] parent = Constants.encodeASCII("parent ");
  68. /** Header "author " */
  69. public static final byte[] author = Constants.encodeASCII("author ");
  70. /** Header "committer " */
  71. public static final byte[] committer = Constants.encodeASCII("committer ");
  72. /** Header "encoding " */
  73. public static final byte[] encoding = Constants.encodeASCII("encoding ");
  74. /** Header "object " */
  75. public static final byte[] object = Constants.encodeASCII("object ");
  76. /** Header "type " */
  77. public static final byte[] type = Constants.encodeASCII("type ");
  78. /** Header "tag " */
  79. public static final byte[] tag = Constants.encodeASCII("tag ");
  80. /** Header "tagger " */
  81. public static final byte[] tagger = Constants.encodeASCII("tagger ");
  82. private final MutableObjectId tempId = new MutableObjectId();
  83. private final MutableInteger ptrout = new MutableInteger();
  84. /**
  85. * Check an object for parsing errors.
  86. *
  87. * @param objType
  88. * type of the object. Must be a valid object type code in
  89. * {@link Constants}.
  90. * @param raw
  91. * the raw data which comprises the object. This should be in the
  92. * canonical format (that is the format used to generate the
  93. * ObjectId of the object). The array is never modified.
  94. * @throws CorruptObjectException
  95. * if an error is identified.
  96. */
  97. public void check(final int objType, final byte[] raw)
  98. throws CorruptObjectException {
  99. switch (objType) {
  100. case Constants.OBJ_COMMIT:
  101. checkCommit(raw);
  102. break;
  103. case Constants.OBJ_TAG:
  104. checkTag(raw);
  105. break;
  106. case Constants.OBJ_TREE:
  107. checkTree(raw);
  108. break;
  109. case Constants.OBJ_BLOB:
  110. checkBlob(raw);
  111. break;
  112. default:
  113. throw new CorruptObjectException(MessageFormat.format(
  114. JGitText.get().corruptObjectInvalidType2, objType));
  115. }
  116. }
  117. private int id(final byte[] raw, final int ptr) {
  118. try {
  119. tempId.fromString(raw, ptr);
  120. return ptr + Constants.OBJECT_ID_STRING_LENGTH;
  121. } catch (IllegalArgumentException e) {
  122. return -1;
  123. }
  124. }
  125. private int personIdent(final byte[] raw, int ptr) {
  126. final int emailB = nextLF(raw, ptr, '<');
  127. if (emailB == ptr || raw[emailB - 1] != '<')
  128. return -1;
  129. final int emailE = nextLF(raw, emailB, '>');
  130. if (emailE == emailB || raw[emailE - 1] != '>')
  131. return -1;
  132. if (emailE == raw.length || raw[emailE] != ' ')
  133. return -1;
  134. parseBase10(raw, emailE + 1, ptrout); // when
  135. ptr = ptrout.value;
  136. if (emailE + 1 == ptr)
  137. return -1;
  138. if (ptr == raw.length || raw[ptr] != ' ')
  139. return -1;
  140. parseBase10(raw, ptr + 1, ptrout); // tz offset
  141. if (ptr + 1 == ptrout.value)
  142. return -1;
  143. return ptrout.value;
  144. }
  145. /**
  146. * Check a commit for errors.
  147. *
  148. * @param raw
  149. * the commit data. The array is never modified.
  150. * @throws CorruptObjectException
  151. * if any error was detected.
  152. */
  153. public void checkCommit(final byte[] raw) throws CorruptObjectException {
  154. int ptr = 0;
  155. if ((ptr = match(raw, ptr, tree)) < 0)
  156. throw new CorruptObjectException("no tree header");
  157. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  158. throw new CorruptObjectException("invalid tree");
  159. while (match(raw, ptr, parent) >= 0) {
  160. ptr += parent.length;
  161. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  162. throw new CorruptObjectException("invalid parent");
  163. }
  164. if ((ptr = match(raw, ptr, author)) < 0)
  165. throw new CorruptObjectException("no author");
  166. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  167. throw new CorruptObjectException("invalid author");
  168. if ((ptr = match(raw, ptr, committer)) < 0)
  169. throw new CorruptObjectException("no committer");
  170. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  171. throw new CorruptObjectException("invalid committer");
  172. }
  173. /**
  174. * Check an annotated tag for errors.
  175. *
  176. * @param raw
  177. * the tag data. The array is never modified.
  178. * @throws CorruptObjectException
  179. * if any error was detected.
  180. */
  181. public void checkTag(final byte[] raw) throws CorruptObjectException {
  182. int ptr = 0;
  183. if ((ptr = match(raw, ptr, object)) < 0)
  184. throw new CorruptObjectException("no object header");
  185. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  186. throw new CorruptObjectException("invalid object");
  187. if ((ptr = match(raw, ptr, type)) < 0)
  188. throw new CorruptObjectException("no type header");
  189. ptr = nextLF(raw, ptr);
  190. if ((ptr = match(raw, ptr, tag)) < 0)
  191. throw new CorruptObjectException("no tag header");
  192. ptr = nextLF(raw, ptr);
  193. if ((ptr = match(raw, ptr, tagger)) > 0) {
  194. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  195. throw new CorruptObjectException("invalid tagger");
  196. }
  197. }
  198. private static int lastPathChar(final int mode) {
  199. return FileMode.TREE.equals(mode) ? '/' : '\0';
  200. }
  201. private static int pathCompare(final byte[] raw, int aPos, final int aEnd,
  202. final int aMode, int bPos, final int bEnd, final int bMode) {
  203. while (aPos < aEnd && bPos < bEnd) {
  204. final int cmp = (raw[aPos++] & 0xff) - (raw[bPos++] & 0xff);
  205. if (cmp != 0)
  206. return cmp;
  207. }
  208. if (aPos < aEnd)
  209. return (raw[aPos] & 0xff) - lastPathChar(bMode);
  210. if (bPos < bEnd)
  211. return lastPathChar(aMode) - (raw[bPos] & 0xff);
  212. return 0;
  213. }
  214. private static boolean duplicateName(final byte[] raw,
  215. final int thisNamePos, final int thisNameEnd) {
  216. final int sz = raw.length;
  217. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  218. for (;;) {
  219. int nextMode = 0;
  220. for (;;) {
  221. if (nextPtr >= sz)
  222. return false;
  223. final byte c = raw[nextPtr++];
  224. if (' ' == c)
  225. break;
  226. nextMode <<= 3;
  227. nextMode += c - '0';
  228. }
  229. final int nextNamePos = nextPtr;
  230. for (;;) {
  231. if (nextPtr == sz)
  232. return false;
  233. final byte c = raw[nextPtr++];
  234. if (c == 0)
  235. break;
  236. }
  237. if (nextNamePos + 1 == nextPtr)
  238. return false;
  239. final int cmp = pathCompare(raw, thisNamePos, thisNameEnd,
  240. FileMode.TREE.getBits(), nextNamePos, nextPtr - 1, nextMode);
  241. if (cmp < 0)
  242. return false;
  243. else if (cmp == 0)
  244. return true;
  245. nextPtr += Constants.OBJECT_ID_LENGTH;
  246. }
  247. }
  248. /**
  249. * Check a canonical formatted tree for errors.
  250. *
  251. * @param raw
  252. * the raw tree data. The array is never modified.
  253. * @throws CorruptObjectException
  254. * if any error was detected.
  255. */
  256. public void checkTree(final byte[] raw) throws CorruptObjectException {
  257. final int sz = raw.length;
  258. int ptr = 0;
  259. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  260. while (ptr < sz) {
  261. int thisMode = 0;
  262. for (;;) {
  263. if (ptr == sz)
  264. throw new CorruptObjectException("truncated in mode");
  265. final byte c = raw[ptr++];
  266. if (' ' == c)
  267. break;
  268. if (c < '0' || c > '7')
  269. throw new CorruptObjectException("invalid mode character");
  270. if (thisMode == 0 && c == '0')
  271. throw new CorruptObjectException("mode starts with '0'");
  272. thisMode <<= 3;
  273. thisMode += c - '0';
  274. }
  275. if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
  276. throw new CorruptObjectException("invalid mode " + thisMode);
  277. final int thisNameB = ptr;
  278. for (;;) {
  279. if (ptr == sz)
  280. throw new CorruptObjectException("truncated in name");
  281. final byte c = raw[ptr++];
  282. if (c == 0)
  283. break;
  284. if (c == '/')
  285. throw new CorruptObjectException("name contains '/'");
  286. }
  287. if (thisNameB + 1 == ptr)
  288. throw new CorruptObjectException("zero length name");
  289. if (raw[thisNameB] == '.') {
  290. final int nameLen = (ptr - 1) - thisNameB;
  291. if (nameLen == 1)
  292. throw new CorruptObjectException("invalid name '.'");
  293. if (nameLen == 2 && raw[thisNameB + 1] == '.')
  294. throw new CorruptObjectException("invalid name '..'");
  295. }
  296. if (duplicateName(raw, thisNameB, ptr - 1))
  297. throw new CorruptObjectException("duplicate entry names");
  298. if (lastNameB != 0) {
  299. final int cmp = pathCompare(raw, lastNameB, lastNameE,
  300. lastMode, thisNameB, ptr - 1, thisMode);
  301. if (cmp > 0)
  302. throw new CorruptObjectException("incorrectly sorted");
  303. }
  304. lastNameB = thisNameB;
  305. lastNameE = ptr - 1;
  306. lastMode = thisMode;
  307. ptr += Constants.OBJECT_ID_LENGTH;
  308. if (ptr > sz)
  309. throw new CorruptObjectException("truncated in object id");
  310. }
  311. }
  312. /**
  313. * Check a blob for errors.
  314. *
  315. * @param raw
  316. * the blob data. The array is never modified.
  317. * @throws CorruptObjectException
  318. * if any error was detected.
  319. */
  320. public void checkBlob(final byte[] raw) throws CorruptObjectException {
  321. // We can always assume the blob is valid.
  322. }
  323. }