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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 java.text.Normalizer;
  50. import java.util.HashSet;
  51. import java.util.Locale;
  52. import java.util.Set;
  53. import org.eclipse.jgit.errors.CorruptObjectException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.util.MutableInteger;
  56. import org.eclipse.jgit.util.RawParseUtils;
  57. /**
  58. * Verifies that an object is formatted correctly.
  59. * <p>
  60. * Verifications made by this class only check that the fields of an object are
  61. * formatted correctly. The ObjectId checksum of the object is not verified, and
  62. * connectivity links between objects are also not verified. Its assumed that
  63. * the caller can provide both of these validations on its own.
  64. * <p>
  65. * Instances of this class are not thread safe, but they may be reused to
  66. * perform multiple object validations.
  67. */
  68. public class ObjectChecker {
  69. /** Header "tree " */
  70. public static final byte[] tree = Constants.encodeASCII("tree "); //$NON-NLS-1$
  71. /** Header "parent " */
  72. public static final byte[] parent = Constants.encodeASCII("parent "); //$NON-NLS-1$
  73. /** Header "author " */
  74. public static final byte[] author = Constants.encodeASCII("author "); //$NON-NLS-1$
  75. /** Header "committer " */
  76. public static final byte[] committer = Constants.encodeASCII("committer "); //$NON-NLS-1$
  77. /** Header "encoding " */
  78. public static final byte[] encoding = Constants.encodeASCII("encoding "); //$NON-NLS-1$
  79. /** Header "object " */
  80. public static final byte[] object = Constants.encodeASCII("object "); //$NON-NLS-1$
  81. /** Header "type " */
  82. public static final byte[] type = Constants.encodeASCII("type "); //$NON-NLS-1$
  83. /** Header "tag " */
  84. public static final byte[] tag = Constants.encodeASCII("tag "); //$NON-NLS-1$
  85. /** Header "tagger " */
  86. public static final byte[] tagger = Constants.encodeASCII("tagger "); //$NON-NLS-1$
  87. private final MutableObjectId tempId = new MutableObjectId();
  88. private final MutableInteger ptrout = new MutableInteger();
  89. private boolean allowZeroMode;
  90. private boolean allowInvalidPersonIdent;
  91. private boolean windows;
  92. private boolean macosx;
  93. /**
  94. * Enable accepting leading zero mode in tree entries.
  95. * <p>
  96. * Some broken Git libraries generated leading zeros in the mode part of
  97. * tree entries. This is technically incorrect but gracefully allowed by
  98. * git-core. JGit rejects such trees by default, but may need to accept
  99. * them on broken histories.
  100. *
  101. * @param allow allow leading zero mode.
  102. * @return {@code this}.
  103. * @since 3.4
  104. */
  105. public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) {
  106. allowZeroMode = allow;
  107. return this;
  108. }
  109. /**
  110. * Enable accepting invalid author, committer and tagger identities.
  111. * <p>
  112. * Some broken Git versions/libraries allowed users to create commits and
  113. * tags with invalid formatting between the name, email and timestamp.
  114. *
  115. * @param allow
  116. * if true accept invalid person identity strings.
  117. * @return {@code this}.
  118. * @since 4.0
  119. */
  120. public ObjectChecker setAllowInvalidPersonIdent(boolean allow) {
  121. allowInvalidPersonIdent = allow;
  122. return this;
  123. }
  124. /**
  125. * Restrict trees to only names legal on Windows platforms.
  126. * <p>
  127. * Also rejects any mixed case forms of reserved names ({@code .git}).
  128. *
  129. * @param win true if Windows name checking should be performed.
  130. * @return {@code this}.
  131. * @since 3.4
  132. */
  133. public ObjectChecker setSafeForWindows(boolean win) {
  134. windows = win;
  135. return this;
  136. }
  137. /**
  138. * Restrict trees to only names legal on Mac OS X platforms.
  139. * <p>
  140. * Rejects any mixed case forms of reserved names ({@code .git})
  141. * for users working on HFS+ in case-insensitive (default) mode.
  142. *
  143. * @param mac true if Mac OS X name checking should be performed.
  144. * @return {@code this}.
  145. * @since 3.4
  146. */
  147. public ObjectChecker setSafeForMacOS(boolean mac) {
  148. macosx = mac;
  149. return this;
  150. }
  151. /**
  152. * Check an object for parsing errors.
  153. *
  154. * @param objType
  155. * type of the object. Must be a valid object type code in
  156. * {@link Constants}.
  157. * @param raw
  158. * the raw data which comprises the object. This should be in the
  159. * canonical format (that is the format used to generate the
  160. * ObjectId of the object). The array is never modified.
  161. * @throws CorruptObjectException
  162. * if an error is identified.
  163. */
  164. public void check(final int objType, final byte[] raw)
  165. throws CorruptObjectException {
  166. switch (objType) {
  167. case Constants.OBJ_COMMIT:
  168. checkCommit(raw);
  169. break;
  170. case Constants.OBJ_TAG:
  171. checkTag(raw);
  172. break;
  173. case Constants.OBJ_TREE:
  174. checkTree(raw);
  175. break;
  176. case Constants.OBJ_BLOB:
  177. checkBlob(raw);
  178. break;
  179. default:
  180. throw new CorruptObjectException(MessageFormat.format(
  181. JGitText.get().corruptObjectInvalidType2,
  182. Integer.valueOf(objType)));
  183. }
  184. }
  185. private int id(final byte[] raw, final int ptr) {
  186. try {
  187. tempId.fromString(raw, ptr);
  188. return ptr + Constants.OBJECT_ID_STRING_LENGTH;
  189. } catch (IllegalArgumentException e) {
  190. return -1;
  191. }
  192. }
  193. private int personIdent(final byte[] raw, int ptr) {
  194. if (allowInvalidPersonIdent)
  195. return nextLF(raw, ptr) - 1;
  196. final int emailB = nextLF(raw, ptr, '<');
  197. if (emailB == ptr || raw[emailB - 1] != '<')
  198. return -1;
  199. final int emailE = nextLF(raw, emailB, '>');
  200. if (emailE == emailB || raw[emailE - 1] != '>')
  201. return -1;
  202. if (emailE == raw.length || raw[emailE] != ' ')
  203. return -1;
  204. parseBase10(raw, emailE + 1, ptrout); // when
  205. ptr = ptrout.value;
  206. if (emailE + 1 == ptr)
  207. return -1;
  208. if (ptr == raw.length || raw[ptr] != ' ')
  209. return -1;
  210. parseBase10(raw, ptr + 1, ptrout); // tz offset
  211. if (ptr + 1 == ptrout.value)
  212. return -1;
  213. return ptrout.value;
  214. }
  215. /**
  216. * Check a commit for errors.
  217. *
  218. * @param raw
  219. * the commit data. The array is never modified.
  220. * @throws CorruptObjectException
  221. * if any error was detected.
  222. */
  223. public void checkCommit(final byte[] raw) throws CorruptObjectException {
  224. int ptr = 0;
  225. if ((ptr = match(raw, ptr, tree)) < 0)
  226. throw new CorruptObjectException(
  227. JGitText.get().corruptObjectNotreeHeader);
  228. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  229. throw new CorruptObjectException(
  230. JGitText.get().corruptObjectInvalidTree);
  231. while (match(raw, ptr, parent) >= 0) {
  232. ptr += parent.length;
  233. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  234. throw new CorruptObjectException(
  235. JGitText.get().corruptObjectInvalidParent);
  236. }
  237. if ((ptr = match(raw, ptr, author)) < 0)
  238. throw new CorruptObjectException(
  239. JGitText.get().corruptObjectNoAuthor);
  240. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  241. throw new CorruptObjectException(
  242. JGitText.get().corruptObjectInvalidAuthor);
  243. if ((ptr = match(raw, ptr, committer)) < 0)
  244. throw new CorruptObjectException(
  245. JGitText.get().corruptObjectNoCommitter);
  246. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  247. throw new CorruptObjectException(
  248. JGitText.get().corruptObjectInvalidCommitter);
  249. }
  250. /**
  251. * Check an annotated tag for errors.
  252. *
  253. * @param raw
  254. * the tag data. The array is never modified.
  255. * @throws CorruptObjectException
  256. * if any error was detected.
  257. */
  258. public void checkTag(final byte[] raw) throws CorruptObjectException {
  259. int ptr = 0;
  260. if ((ptr = match(raw, ptr, object)) < 0)
  261. throw new CorruptObjectException(
  262. JGitText.get().corruptObjectNoObjectHeader);
  263. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  264. throw new CorruptObjectException(
  265. JGitText.get().corruptObjectInvalidObject);
  266. if ((ptr = match(raw, ptr, type)) < 0)
  267. throw new CorruptObjectException(
  268. JGitText.get().corruptObjectNoTypeHeader);
  269. ptr = nextLF(raw, ptr);
  270. if ((ptr = match(raw, ptr, tag)) < 0)
  271. throw new CorruptObjectException(
  272. JGitText.get().corruptObjectNoTagHeader);
  273. ptr = nextLF(raw, ptr);
  274. if ((ptr = match(raw, ptr, tagger)) > 0) {
  275. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  276. throw new CorruptObjectException(
  277. JGitText.get().corruptObjectInvalidTagger);
  278. }
  279. }
  280. private static int lastPathChar(final int mode) {
  281. return FileMode.TREE.equals(mode) ? '/' : '\0';
  282. }
  283. private static int pathCompare(final byte[] raw, int aPos, final int aEnd,
  284. final int aMode, int bPos, final int bEnd, final int bMode) {
  285. while (aPos < aEnd && bPos < bEnd) {
  286. final int cmp = (raw[aPos++] & 0xff) - (raw[bPos++] & 0xff);
  287. if (cmp != 0)
  288. return cmp;
  289. }
  290. if (aPos < aEnd)
  291. return (raw[aPos] & 0xff) - lastPathChar(bMode);
  292. if (bPos < bEnd)
  293. return lastPathChar(aMode) - (raw[bPos] & 0xff);
  294. return 0;
  295. }
  296. private static boolean duplicateName(final byte[] raw,
  297. final int thisNamePos, final int thisNameEnd) {
  298. final int sz = raw.length;
  299. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  300. for (;;) {
  301. int nextMode = 0;
  302. for (;;) {
  303. if (nextPtr >= sz)
  304. return false;
  305. final byte c = raw[nextPtr++];
  306. if (' ' == c)
  307. break;
  308. nextMode <<= 3;
  309. nextMode += c - '0';
  310. }
  311. final int nextNamePos = nextPtr;
  312. for (;;) {
  313. if (nextPtr == sz)
  314. return false;
  315. final byte c = raw[nextPtr++];
  316. if (c == 0)
  317. break;
  318. }
  319. if (nextNamePos + 1 == nextPtr)
  320. return false;
  321. final int cmp = pathCompare(raw, thisNamePos, thisNameEnd,
  322. FileMode.TREE.getBits(), nextNamePos, nextPtr - 1, nextMode);
  323. if (cmp < 0)
  324. return false;
  325. else if (cmp == 0)
  326. return true;
  327. nextPtr += Constants.OBJECT_ID_LENGTH;
  328. }
  329. }
  330. /**
  331. * Check a canonical formatted tree for errors.
  332. *
  333. * @param raw
  334. * the raw tree data. The array is never modified.
  335. * @throws CorruptObjectException
  336. * if any error was detected.
  337. */
  338. public void checkTree(final byte[] raw) throws CorruptObjectException {
  339. final int sz = raw.length;
  340. int ptr = 0;
  341. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  342. Set<String> normalized = windows || macosx
  343. ? new HashSet<String>()
  344. : null;
  345. while (ptr < sz) {
  346. int thisMode = 0;
  347. for (;;) {
  348. if (ptr == sz)
  349. throw new CorruptObjectException(
  350. JGitText.get().corruptObjectTruncatedInMode);
  351. final byte c = raw[ptr++];
  352. if (' ' == c)
  353. break;
  354. if (c < '0' || c > '7')
  355. throw new CorruptObjectException(
  356. JGitText.get().corruptObjectInvalidModeChar);
  357. if (thisMode == 0 && c == '0' && !allowZeroMode)
  358. throw new CorruptObjectException(
  359. JGitText.get().corruptObjectInvalidModeStartsZero);
  360. thisMode <<= 3;
  361. thisMode += c - '0';
  362. }
  363. if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
  364. throw new CorruptObjectException(MessageFormat.format(
  365. JGitText.get().corruptObjectInvalidMode2,
  366. Integer.valueOf(thisMode)));
  367. final int thisNameB = ptr;
  368. ptr = scanPathSegment(raw, ptr, sz);
  369. if (ptr == sz || raw[ptr] != 0)
  370. throw new CorruptObjectException(
  371. JGitText.get().corruptObjectTruncatedInName);
  372. checkPathSegment2(raw, thisNameB, ptr);
  373. if (normalized != null) {
  374. if (!normalized.add(normalize(raw, thisNameB, ptr)))
  375. throw new CorruptObjectException(
  376. JGitText.get().corruptObjectDuplicateEntryNames);
  377. } else if (duplicateName(raw, thisNameB, ptr))
  378. throw new CorruptObjectException(
  379. JGitText.get().corruptObjectDuplicateEntryNames);
  380. if (lastNameB != 0) {
  381. final int cmp = pathCompare(raw, lastNameB, lastNameE,
  382. lastMode, thisNameB, ptr, thisMode);
  383. if (cmp > 0)
  384. throw new CorruptObjectException(
  385. JGitText.get().corruptObjectIncorrectSorting);
  386. }
  387. lastNameB = thisNameB;
  388. lastNameE = ptr;
  389. lastMode = thisMode;
  390. ptr += 1 + Constants.OBJECT_ID_LENGTH;
  391. if (ptr > sz)
  392. throw new CorruptObjectException(
  393. JGitText.get().corruptObjectTruncatedInObjectId);
  394. }
  395. }
  396. private int scanPathSegment(byte[] raw, int ptr, int end)
  397. throws CorruptObjectException {
  398. for (; ptr < end; ptr++) {
  399. byte c = raw[ptr];
  400. if (c == 0)
  401. return ptr;
  402. if (c == '/')
  403. throw new CorruptObjectException(
  404. JGitText.get().corruptObjectNameContainsSlash);
  405. if (windows && isInvalidOnWindows(c)) {
  406. if (c > 31)
  407. throw new CorruptObjectException(String.format(
  408. JGitText.get().corruptObjectNameContainsChar,
  409. Byte.valueOf(c)));
  410. throw new CorruptObjectException(String.format(
  411. JGitText.get().corruptObjectNameContainsByte,
  412. Integer.valueOf(c & 0xff)));
  413. }
  414. }
  415. return ptr;
  416. }
  417. /**
  418. * Check tree path entry for validity.
  419. * <p>
  420. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  421. * scans a multi-directory path string such as {@code "src/main.c"}.
  422. *
  423. * @param path path string to scan.
  424. * @throws CorruptObjectException path is invalid.
  425. * @since 3.6
  426. */
  427. public void checkPath(String path) throws CorruptObjectException {
  428. byte[] buf = Constants.encode(path);
  429. checkPath(buf, 0, buf.length);
  430. }
  431. /**
  432. * Check tree path entry for validity.
  433. * <p>
  434. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  435. * scans a multi-directory path string such as {@code "src/main.c"}.
  436. *
  437. * @param raw buffer to scan.
  438. * @param ptr offset to first byte of the name.
  439. * @param end offset to one past last byte of name.
  440. * @throws CorruptObjectException path is invalid.
  441. * @since 3.6
  442. */
  443. public void checkPath(byte[] raw, int ptr, int end)
  444. throws CorruptObjectException {
  445. int start = ptr;
  446. for (; ptr < end; ptr++) {
  447. if (raw[ptr] == '/') {
  448. checkPathSegment(raw, start, ptr);
  449. start = ptr + 1;
  450. }
  451. }
  452. checkPathSegment(raw, start, end);
  453. }
  454. /**
  455. * Check tree path entry for validity.
  456. *
  457. * @param raw buffer to scan.
  458. * @param ptr offset to first byte of the name.
  459. * @param end offset to one past last byte of name.
  460. * @throws CorruptObjectException name is invalid.
  461. * @since 3.4
  462. */
  463. public void checkPathSegment(byte[] raw, int ptr, int end)
  464. throws CorruptObjectException {
  465. int e = scanPathSegment(raw, ptr, end);
  466. if (e < end && raw[e] == 0)
  467. throw new CorruptObjectException(
  468. JGitText.get().corruptObjectNameContainsNullByte);
  469. checkPathSegment2(raw, ptr, end);
  470. }
  471. private void checkPathSegment2(byte[] raw, int ptr, int end)
  472. throws CorruptObjectException {
  473. if (ptr == end)
  474. throw new CorruptObjectException(
  475. JGitText.get().corruptObjectNameZeroLength);
  476. if (raw[ptr] == '.') {
  477. switch (end - ptr) {
  478. case 1:
  479. throw new CorruptObjectException(
  480. JGitText.get().corruptObjectNameDot);
  481. case 2:
  482. if (raw[ptr + 1] == '.')
  483. throw new CorruptObjectException(
  484. JGitText.get().corruptObjectNameDotDot);
  485. break;
  486. case 4:
  487. if (isGit(raw, ptr + 1))
  488. throw new CorruptObjectException(String.format(
  489. JGitText.get().corruptObjectInvalidName,
  490. RawParseUtils.decode(raw, ptr, end)));
  491. break;
  492. default:
  493. if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end))
  494. throw new CorruptObjectException(String.format(
  495. JGitText.get().corruptObjectInvalidName,
  496. RawParseUtils.decode(raw, ptr, end)));
  497. }
  498. } else if (isGitTilde1(raw, ptr, end)) {
  499. throw new CorruptObjectException(String.format(
  500. JGitText.get().corruptObjectInvalidName,
  501. RawParseUtils.decode(raw, ptr, end)));
  502. }
  503. if (macosx && isMacHFSGit(raw, ptr, end))
  504. throw new CorruptObjectException(String.format(
  505. JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
  506. RawParseUtils.decode(raw, ptr, end)));
  507. if (windows) {
  508. // Windows ignores space and dot at end of file name.
  509. if (raw[end - 1] == ' ' || raw[end - 1] == '.')
  510. throw new CorruptObjectException(String.format(
  511. JGitText.get().corruptObjectInvalidNameEnd,
  512. Character.valueOf(((char) raw[end - 1]))));
  513. if (end - ptr >= 3)
  514. checkNotWindowsDevice(raw, ptr, end);
  515. }
  516. }
  517. // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
  518. // to ".git" therefore we should prevent such names
  519. private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
  520. throws CorruptObjectException {
  521. boolean ignorable = false;
  522. byte[] git = new byte[] { '.', 'g', 'i', 't' };
  523. int g = 0;
  524. while (ptr < end) {
  525. switch (raw[ptr]) {
  526. case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
  527. checkTruncatedIgnorableUTF8(raw, ptr, end);
  528. switch (raw[ptr + 1]) {
  529. case (byte) 0x80:
  530. switch (raw[ptr + 2]) {
  531. case (byte) 0x8c: // U+200C 0xe2808c ZERO WIDTH NON-JOINER
  532. case (byte) 0x8d: // U+200D 0xe2808d ZERO WIDTH JOINER
  533. case (byte) 0x8e: // U+200E 0xe2808e LEFT-TO-RIGHT MARK
  534. case (byte) 0x8f: // U+200F 0xe2808f RIGHT-TO-LEFT MARK
  535. case (byte) 0xaa: // U+202A 0xe280aa LEFT-TO-RIGHT EMBEDDING
  536. case (byte) 0xab: // U+202B 0xe280ab RIGHT-TO-LEFT EMBEDDING
  537. case (byte) 0xac: // U+202C 0xe280ac POP DIRECTIONAL FORMATTING
  538. case (byte) 0xad: // U+202D 0xe280ad LEFT-TO-RIGHT OVERRIDE
  539. case (byte) 0xae: // U+202E 0xe280ae RIGHT-TO-LEFT OVERRIDE
  540. ignorable = true;
  541. ptr += 3;
  542. continue;
  543. default:
  544. return false;
  545. }
  546. case (byte) 0x81:
  547. switch (raw[ptr + 2]) {
  548. case (byte) 0xaa: // U+206A 0xe281aa INHIBIT SYMMETRIC SWAPPING
  549. case (byte) 0xab: // U+206B 0xe281ab ACTIVATE SYMMETRIC SWAPPING
  550. case (byte) 0xac: // U+206C 0xe281ac INHIBIT ARABIC FORM SHAPING
  551. case (byte) 0xad: // U+206D 0xe281ad ACTIVATE ARABIC FORM SHAPING
  552. case (byte) 0xae: // U+206E 0xe281ae NATIONAL DIGIT SHAPES
  553. case (byte) 0xaf: // U+206F 0xe281af NOMINAL DIGIT SHAPES
  554. ignorable = true;
  555. ptr += 3;
  556. continue;
  557. default:
  558. return false;
  559. }
  560. default:
  561. return false;
  562. }
  563. case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024
  564. checkTruncatedIgnorableUTF8(raw, ptr, end);
  565. // U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE
  566. if ((raw[ptr + 1] == (byte) 0xbb)
  567. && (raw[ptr + 2] == (byte) 0xbf)) {
  568. ignorable = true;
  569. ptr += 3;
  570. continue;
  571. }
  572. return false;
  573. default:
  574. if (g == 4)
  575. return false;
  576. if (raw[ptr++] != git[g++])
  577. return false;
  578. }
  579. }
  580. if (g == 4 && ignorable)
  581. return true;
  582. return false;
  583. }
  584. private static void checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end)
  585. throws CorruptObjectException {
  586. if ((ptr + 2) >= end)
  587. throw new CorruptObjectException(MessageFormat.format(
  588. JGitText.get().corruptObjectInvalidNameInvalidUtf8,
  589. toHexString(raw, ptr, end)));
  590. }
  591. private static String toHexString(byte[] raw, int ptr, int end) {
  592. StringBuilder b = new StringBuilder("0x"); //$NON-NLS-1$
  593. for (int i = ptr; i < end; i++)
  594. b.append(String.format("%02x", Byte.valueOf(raw[i]))); //$NON-NLS-1$
  595. return b.toString();
  596. }
  597. private static void checkNotWindowsDevice(byte[] raw, int ptr, int end)
  598. throws CorruptObjectException {
  599. switch (toLower(raw[ptr])) {
  600. case 'a': // AUX
  601. if (end - ptr >= 3
  602. && toLower(raw[ptr + 1]) == 'u'
  603. && toLower(raw[ptr + 2]) == 'x'
  604. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  605. throw new CorruptObjectException(
  606. JGitText.get().corruptObjectInvalidNameAux);
  607. break;
  608. case 'c': // CON, COM[1-9]
  609. if (end - ptr >= 3
  610. && toLower(raw[ptr + 2]) == 'n'
  611. && toLower(raw[ptr + 1]) == 'o'
  612. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  613. throw new CorruptObjectException(
  614. JGitText.get().corruptObjectInvalidNameCon);
  615. if (end - ptr >= 4
  616. && toLower(raw[ptr + 2]) == 'm'
  617. && toLower(raw[ptr + 1]) == 'o'
  618. && isPositiveDigit(raw[ptr + 3])
  619. && (end - ptr == 4 || raw[ptr + 4] == '.'))
  620. throw new CorruptObjectException(String.format(
  621. JGitText.get().corruptObjectInvalidNameCom,
  622. Character.valueOf(((char) raw[ptr + 3]))));
  623. break;
  624. case 'l': // LPT[1-9]
  625. if (end - ptr >= 4
  626. && toLower(raw[ptr + 1]) == 'p'
  627. && toLower(raw[ptr + 2]) == 't'
  628. && isPositiveDigit(raw[ptr + 3])
  629. && (end - ptr == 4 || raw[ptr + 4] == '.'))
  630. throw new CorruptObjectException(String.format(
  631. JGitText.get().corruptObjectInvalidNameLpt,
  632. Character.valueOf(((char) raw[ptr + 3]))));
  633. break;
  634. case 'n': // NUL
  635. if (end - ptr >= 3
  636. && toLower(raw[ptr + 1]) == 'u'
  637. && toLower(raw[ptr + 2]) == 'l'
  638. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  639. throw new CorruptObjectException(
  640. JGitText.get().corruptObjectInvalidNameNul);
  641. break;
  642. case 'p': // PRN
  643. if (end - ptr >= 3
  644. && toLower(raw[ptr + 1]) == 'r'
  645. && toLower(raw[ptr + 2]) == 'n'
  646. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  647. throw new CorruptObjectException(
  648. JGitText.get().corruptObjectInvalidNamePrn);
  649. break;
  650. }
  651. }
  652. private static boolean isInvalidOnWindows(byte c) {
  653. // Windows disallows "special" characters in a path component.
  654. switch (c) {
  655. case '"':
  656. case '*':
  657. case ':':
  658. case '<':
  659. case '>':
  660. case '?':
  661. case '\\':
  662. case '|':
  663. return true;
  664. }
  665. return 1 <= c && c <= 31;
  666. }
  667. private static boolean isGit(byte[] buf, int p) {
  668. return toLower(buf[p]) == 'g'
  669. && toLower(buf[p + 1]) == 'i'
  670. && toLower(buf[p + 2]) == 't';
  671. }
  672. private static boolean isGitTilde1(byte[] buf, int p, int end) {
  673. if (end - p != 5)
  674. return false;
  675. return toLower(buf[p]) == 'g' && toLower(buf[p + 1]) == 'i'
  676. && toLower(buf[p + 2]) == 't' && buf[p + 3] == '~'
  677. && buf[p + 4] == '1';
  678. }
  679. private static boolean isNormalizedGit(byte[] raw, int ptr, int end) {
  680. if (isGit(raw, ptr)) {
  681. int dots = 0;
  682. boolean space = false;
  683. int p = end - 1;
  684. for (; (ptr + 2) < p; p--) {
  685. if (raw[p] == '.')
  686. dots++;
  687. else if (raw[p] == ' ')
  688. space = true;
  689. else
  690. break;
  691. }
  692. return p == ptr + 2 && (dots == 1 || space);
  693. }
  694. return false;
  695. }
  696. private static char toLower(byte b) {
  697. if ('A' <= b && b <= 'Z')
  698. return (char) (b + ('a' - 'A'));
  699. return (char) b;
  700. }
  701. private static boolean isPositiveDigit(byte b) {
  702. return '1' <= b && b <= '9';
  703. }
  704. /**
  705. * Check a blob for errors.
  706. *
  707. * @param raw
  708. * the blob data. The array is never modified.
  709. * @throws CorruptObjectException
  710. * if any error was detected.
  711. */
  712. public void checkBlob(final byte[] raw) throws CorruptObjectException {
  713. // We can always assume the blob is valid.
  714. }
  715. private String normalize(byte[] raw, int ptr, int end) {
  716. String n = RawParseUtils.decode(raw, ptr, end).toLowerCase(Locale.US);
  717. return macosx ? Normalizer.normalize(n, Normalizer.Form.NFC) : n;
  718. }
  719. }