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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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.lang.reflect.InvocationTargetException;
  49. import java.lang.reflect.Method;
  50. import java.text.MessageFormat;
  51. import java.util.HashSet;
  52. import java.util.Locale;
  53. import java.util.Set;
  54. import org.eclipse.jgit.errors.CorruptObjectException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.util.MutableInteger;
  57. import org.eclipse.jgit.util.RawParseUtils;
  58. /**
  59. * Verifies that an object is formatted correctly.
  60. * <p>
  61. * Verifications made by this class only check that the fields of an object are
  62. * formatted correctly. The ObjectId checksum of the object is not verified, and
  63. * connectivity links between objects are also not verified. Its assumed that
  64. * the caller can provide both of these validations on its own.
  65. * <p>
  66. * Instances of this class are not thread safe, but they may be reused to
  67. * perform multiple object validations.
  68. */
  69. public class ObjectChecker {
  70. /** Header "tree " */
  71. public static final byte[] tree = Constants.encodeASCII("tree "); //$NON-NLS-1$
  72. /** Header "parent " */
  73. public static final byte[] parent = Constants.encodeASCII("parent "); //$NON-NLS-1$
  74. /** Header "author " */
  75. public static final byte[] author = Constants.encodeASCII("author "); //$NON-NLS-1$
  76. /** Header "committer " */
  77. public static final byte[] committer = Constants.encodeASCII("committer "); //$NON-NLS-1$
  78. /** Header "encoding " */
  79. public static final byte[] encoding = Constants.encodeASCII("encoding "); //$NON-NLS-1$
  80. /** Header "object " */
  81. public static final byte[] object = Constants.encodeASCII("object "); //$NON-NLS-1$
  82. /** Header "type " */
  83. public static final byte[] type = Constants.encodeASCII("type "); //$NON-NLS-1$
  84. /** Header "tag " */
  85. public static final byte[] tag = Constants.encodeASCII("tag "); //$NON-NLS-1$
  86. /** Header "tagger " */
  87. public static final byte[] tagger = Constants.encodeASCII("tagger "); //$NON-NLS-1$
  88. private final MutableObjectId tempId = new MutableObjectId();
  89. private final MutableInteger ptrout = new MutableInteger();
  90. private boolean allowZeroMode;
  91. private boolean allowInvalidPersonIdent;
  92. private boolean windows;
  93. private boolean macosx;
  94. /**
  95. * Enable accepting leading zero mode in tree entries.
  96. * <p>
  97. * Some broken Git libraries generated leading zeros in the mode part of
  98. * tree entries. This is technically incorrect but gracefully allowed by
  99. * git-core. JGit rejects such trees by default, but may need to accept
  100. * them on broken histories.
  101. *
  102. * @param allow allow leading zero mode.
  103. * @return {@code this}.
  104. * @since 3.4
  105. */
  106. public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) {
  107. allowZeroMode = allow;
  108. return this;
  109. }
  110. /**
  111. * Enable accepting invalid author, committer and tagger identities.
  112. * <p>
  113. * Some broken Git versions/libraries allowed users to create commits and
  114. * tags with invalid formatting between the name, email and timestamp.
  115. *
  116. * @param allow
  117. * if true accept invalid person identity strings.
  118. * @return {@code this}.
  119. * @since 4.0
  120. */
  121. public ObjectChecker setAllowInvalidPersonIdent(boolean allow) {
  122. allowInvalidPersonIdent = allow;
  123. return this;
  124. }
  125. /**
  126. * Restrict trees to only names legal on Windows platforms.
  127. * <p>
  128. * Also rejects any mixed case forms of reserved names ({@code .git}).
  129. *
  130. * @param win true if Windows name checking should be performed.
  131. * @return {@code this}.
  132. * @since 3.4
  133. */
  134. public ObjectChecker setSafeForWindows(boolean win) {
  135. windows = win;
  136. return this;
  137. }
  138. /**
  139. * Restrict trees to only names legal on Mac OS X platforms.
  140. * <p>
  141. * Rejects any mixed case forms of reserved names ({@code .git})
  142. * for users working on HFS+ in case-insensitive (default) mode.
  143. *
  144. * @param mac true if Mac OS X name checking should be performed.
  145. * @return {@code this}.
  146. * @since 3.4
  147. */
  148. public ObjectChecker setSafeForMacOS(boolean mac) {
  149. macosx = mac;
  150. return this;
  151. }
  152. /**
  153. * Check an object for parsing errors.
  154. *
  155. * @param objType
  156. * type of the object. Must be a valid object type code in
  157. * {@link Constants}.
  158. * @param raw
  159. * the raw data which comprises the object. This should be in the
  160. * canonical format (that is the format used to generate the
  161. * ObjectId of the object). The array is never modified.
  162. * @throws CorruptObjectException
  163. * if an error is identified.
  164. */
  165. public void check(final int objType, final byte[] raw)
  166. throws CorruptObjectException {
  167. switch (objType) {
  168. case Constants.OBJ_COMMIT:
  169. checkCommit(raw);
  170. break;
  171. case Constants.OBJ_TAG:
  172. checkTag(raw);
  173. break;
  174. case Constants.OBJ_TREE:
  175. checkTree(raw);
  176. break;
  177. case Constants.OBJ_BLOB:
  178. checkBlob(raw);
  179. break;
  180. default:
  181. throw new CorruptObjectException(MessageFormat.format(
  182. JGitText.get().corruptObjectInvalidType2,
  183. Integer.valueOf(objType)));
  184. }
  185. }
  186. private int id(final byte[] raw, final int ptr) {
  187. try {
  188. tempId.fromString(raw, ptr);
  189. return ptr + Constants.OBJECT_ID_STRING_LENGTH;
  190. } catch (IllegalArgumentException e) {
  191. return -1;
  192. }
  193. }
  194. private int personIdent(final byte[] raw, int ptr) {
  195. if (allowInvalidPersonIdent)
  196. return nextLF(raw, ptr) - 1;
  197. final int emailB = nextLF(raw, ptr, '<');
  198. if (emailB == ptr || raw[emailB - 1] != '<')
  199. return -1;
  200. final int emailE = nextLF(raw, emailB, '>');
  201. if (emailE == emailB || raw[emailE - 1] != '>')
  202. return -1;
  203. if (emailE == raw.length || raw[emailE] != ' ')
  204. return -1;
  205. parseBase10(raw, emailE + 1, ptrout); // when
  206. ptr = ptrout.value;
  207. if (emailE + 1 == ptr)
  208. return -1;
  209. if (ptr == raw.length || raw[ptr] != ' ')
  210. return -1;
  211. parseBase10(raw, ptr + 1, ptrout); // tz offset
  212. if (ptr + 1 == ptrout.value)
  213. return -1;
  214. return ptrout.value;
  215. }
  216. /**
  217. * Check a commit for errors.
  218. *
  219. * @param raw
  220. * the commit data. The array is never modified.
  221. * @throws CorruptObjectException
  222. * if any error was detected.
  223. */
  224. public void checkCommit(final byte[] raw) throws CorruptObjectException {
  225. int ptr = 0;
  226. if ((ptr = match(raw, ptr, tree)) < 0)
  227. throw new CorruptObjectException(
  228. JGitText.get().corruptObjectNotreeHeader);
  229. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  230. throw new CorruptObjectException(
  231. JGitText.get().corruptObjectInvalidTree);
  232. while (match(raw, ptr, parent) >= 0) {
  233. ptr += parent.length;
  234. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  235. throw new CorruptObjectException(
  236. JGitText.get().corruptObjectInvalidParent);
  237. }
  238. if ((ptr = match(raw, ptr, author)) < 0)
  239. throw new CorruptObjectException(
  240. JGitText.get().corruptObjectNoAuthor);
  241. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  242. throw new CorruptObjectException(
  243. JGitText.get().corruptObjectInvalidAuthor);
  244. if ((ptr = match(raw, ptr, committer)) < 0)
  245. throw new CorruptObjectException(
  246. JGitText.get().corruptObjectNoCommitter);
  247. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  248. throw new CorruptObjectException(
  249. JGitText.get().corruptObjectInvalidCommitter);
  250. }
  251. /**
  252. * Check an annotated tag for errors.
  253. *
  254. * @param raw
  255. * the tag data. The array is never modified.
  256. * @throws CorruptObjectException
  257. * if any error was detected.
  258. */
  259. public void checkTag(final byte[] raw) throws CorruptObjectException {
  260. int ptr = 0;
  261. if ((ptr = match(raw, ptr, object)) < 0)
  262. throw new CorruptObjectException(
  263. JGitText.get().corruptObjectNoObjectHeader);
  264. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  265. throw new CorruptObjectException(
  266. JGitText.get().corruptObjectInvalidObject);
  267. if ((ptr = match(raw, ptr, type)) < 0)
  268. throw new CorruptObjectException(
  269. JGitText.get().corruptObjectNoTypeHeader);
  270. ptr = nextLF(raw, ptr);
  271. if ((ptr = match(raw, ptr, tag)) < 0)
  272. throw new CorruptObjectException(
  273. JGitText.get().corruptObjectNoTagHeader);
  274. ptr = nextLF(raw, ptr);
  275. if ((ptr = match(raw, ptr, tagger)) > 0) {
  276. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  277. throw new CorruptObjectException(
  278. JGitText.get().corruptObjectInvalidTagger);
  279. }
  280. }
  281. private static int lastPathChar(final int mode) {
  282. return FileMode.TREE.equals(mode) ? '/' : '\0';
  283. }
  284. private static int pathCompare(final byte[] raw, int aPos, final int aEnd,
  285. final int aMode, int bPos, final int bEnd, final int bMode) {
  286. while (aPos < aEnd && bPos < bEnd) {
  287. final int cmp = (raw[aPos++] & 0xff) - (raw[bPos++] & 0xff);
  288. if (cmp != 0)
  289. return cmp;
  290. }
  291. if (aPos < aEnd)
  292. return (raw[aPos] & 0xff) - lastPathChar(bMode);
  293. if (bPos < bEnd)
  294. return lastPathChar(aMode) - (raw[bPos] & 0xff);
  295. return 0;
  296. }
  297. private static boolean duplicateName(final byte[] raw,
  298. final int thisNamePos, final int thisNameEnd) {
  299. final int sz = raw.length;
  300. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  301. for (;;) {
  302. int nextMode = 0;
  303. for (;;) {
  304. if (nextPtr >= sz)
  305. return false;
  306. final byte c = raw[nextPtr++];
  307. if (' ' == c)
  308. break;
  309. nextMode <<= 3;
  310. nextMode += c - '0';
  311. }
  312. final int nextNamePos = nextPtr;
  313. for (;;) {
  314. if (nextPtr == sz)
  315. return false;
  316. final byte c = raw[nextPtr++];
  317. if (c == 0)
  318. break;
  319. }
  320. if (nextNamePos + 1 == nextPtr)
  321. return false;
  322. final int cmp = pathCompare(raw, thisNamePos, thisNameEnd,
  323. FileMode.TREE.getBits(), nextNamePos, nextPtr - 1, nextMode);
  324. if (cmp < 0)
  325. return false;
  326. else if (cmp == 0)
  327. return true;
  328. nextPtr += Constants.OBJECT_ID_LENGTH;
  329. }
  330. }
  331. /**
  332. * Check a canonical formatted tree for errors.
  333. *
  334. * @param raw
  335. * the raw tree data. The array is never modified.
  336. * @throws CorruptObjectException
  337. * if any error was detected.
  338. */
  339. public void checkTree(final byte[] raw) throws CorruptObjectException {
  340. final int sz = raw.length;
  341. int ptr = 0;
  342. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  343. Set<String> normalized = windows || macosx
  344. ? new HashSet<String>()
  345. : null;
  346. while (ptr < sz) {
  347. int thisMode = 0;
  348. for (;;) {
  349. if (ptr == sz)
  350. throw new CorruptObjectException(
  351. JGitText.get().corruptObjectTruncatedInMode);
  352. final byte c = raw[ptr++];
  353. if (' ' == c)
  354. break;
  355. if (c < '0' || c > '7')
  356. throw new CorruptObjectException(
  357. JGitText.get().corruptObjectInvalidModeChar);
  358. if (thisMode == 0 && c == '0' && !allowZeroMode)
  359. throw new CorruptObjectException(
  360. JGitText.get().corruptObjectInvalidModeStartsZero);
  361. thisMode <<= 3;
  362. thisMode += c - '0';
  363. }
  364. if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
  365. throw new CorruptObjectException(MessageFormat.format(
  366. JGitText.get().corruptObjectInvalidMode2,
  367. Integer.valueOf(thisMode)));
  368. final int thisNameB = ptr;
  369. ptr = scanPathSegment(raw, ptr, sz);
  370. if (ptr == sz || raw[ptr] != 0)
  371. throw new CorruptObjectException(
  372. JGitText.get().corruptObjectTruncatedInName);
  373. checkPathSegment2(raw, thisNameB, ptr);
  374. if (normalized != null) {
  375. if (!normalized.add(normalize(raw, thisNameB, ptr)))
  376. throw new CorruptObjectException(
  377. JGitText.get().corruptObjectDuplicateEntryNames);
  378. } else if (duplicateName(raw, thisNameB, ptr))
  379. throw new CorruptObjectException(
  380. JGitText.get().corruptObjectDuplicateEntryNames);
  381. if (lastNameB != 0) {
  382. final int cmp = pathCompare(raw, lastNameB, lastNameE,
  383. lastMode, thisNameB, ptr, thisMode);
  384. if (cmp > 0)
  385. throw new CorruptObjectException(
  386. JGitText.get().corruptObjectIncorrectSorting);
  387. }
  388. lastNameB = thisNameB;
  389. lastNameE = ptr;
  390. lastMode = thisMode;
  391. ptr += 1 + Constants.OBJECT_ID_LENGTH;
  392. if (ptr > sz)
  393. throw new CorruptObjectException(
  394. JGitText.get().corruptObjectTruncatedInObjectId);
  395. }
  396. }
  397. private int scanPathSegment(byte[] raw, int ptr, int end)
  398. throws CorruptObjectException {
  399. for (; ptr < end; ptr++) {
  400. byte c = raw[ptr];
  401. if (c == 0)
  402. return ptr;
  403. if (c == '/')
  404. throw new CorruptObjectException(
  405. JGitText.get().corruptObjectNameContainsSlash);
  406. if (windows && isInvalidOnWindows(c)) {
  407. if (c > 31)
  408. throw new CorruptObjectException(String.format(
  409. JGitText.get().corruptObjectNameContainsChar,
  410. Byte.valueOf(c)));
  411. throw new CorruptObjectException(String.format(
  412. JGitText.get().corruptObjectNameContainsByte,
  413. Integer.valueOf(c & 0xff)));
  414. }
  415. }
  416. return ptr;
  417. }
  418. /**
  419. * Check tree path entry for validity.
  420. * <p>
  421. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  422. * scans a multi-directory path string such as {@code "src/main.c"}.
  423. *
  424. * @param path path string to scan.
  425. * @throws CorruptObjectException path is invalid.
  426. * @since 3.6
  427. */
  428. public void checkPath(String path) throws CorruptObjectException {
  429. byte[] buf = Constants.encode(path);
  430. checkPath(buf, 0, buf.length);
  431. }
  432. /**
  433. * Check tree path entry for validity.
  434. * <p>
  435. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  436. * scans a multi-directory path string such as {@code "src/main.c"}.
  437. *
  438. * @param raw buffer to scan.
  439. * @param ptr offset to first byte of the name.
  440. * @param end offset to one past last byte of name.
  441. * @throws CorruptObjectException path is invalid.
  442. * @since 3.6
  443. */
  444. public void checkPath(byte[] raw, int ptr, int end)
  445. throws CorruptObjectException {
  446. int start = ptr;
  447. for (; ptr < end; ptr++) {
  448. if (raw[ptr] == '/') {
  449. checkPathSegment(raw, start, ptr);
  450. start = ptr + 1;
  451. }
  452. }
  453. checkPathSegment(raw, start, end);
  454. }
  455. /**
  456. * Check tree path entry for validity.
  457. *
  458. * @param raw buffer to scan.
  459. * @param ptr offset to first byte of the name.
  460. * @param end offset to one past last byte of name.
  461. * @throws CorruptObjectException name is invalid.
  462. * @since 3.4
  463. */
  464. public void checkPathSegment(byte[] raw, int ptr, int end)
  465. throws CorruptObjectException {
  466. int e = scanPathSegment(raw, ptr, end);
  467. if (e < end && raw[e] == 0)
  468. throw new CorruptObjectException(
  469. JGitText.get().corruptObjectNameContainsNullByte);
  470. checkPathSegment2(raw, ptr, end);
  471. }
  472. private void checkPathSegment2(byte[] raw, int ptr, int end)
  473. throws CorruptObjectException {
  474. if (ptr == end)
  475. throw new CorruptObjectException(
  476. JGitText.get().corruptObjectNameZeroLength);
  477. if (raw[ptr] == '.') {
  478. switch (end - ptr) {
  479. case 1:
  480. throw new CorruptObjectException(
  481. JGitText.get().corruptObjectNameDot);
  482. case 2:
  483. if (raw[ptr + 1] == '.')
  484. throw new CorruptObjectException(
  485. JGitText.get().corruptObjectNameDotDot);
  486. break;
  487. case 4:
  488. if (isGit(raw, ptr + 1))
  489. throw new CorruptObjectException(String.format(
  490. JGitText.get().corruptObjectInvalidName,
  491. RawParseUtils.decode(raw, ptr, end)));
  492. break;
  493. default:
  494. if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end))
  495. throw new CorruptObjectException(String.format(
  496. JGitText.get().corruptObjectInvalidName,
  497. RawParseUtils.decode(raw, ptr, end)));
  498. }
  499. } else if (isGitTilde1(raw, ptr, end)) {
  500. throw new CorruptObjectException(String.format(
  501. JGitText.get().corruptObjectInvalidName,
  502. RawParseUtils.decode(raw, ptr, end)));
  503. }
  504. if (macosx && isMacHFSGit(raw, ptr, end))
  505. throw new CorruptObjectException(String.format(
  506. JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
  507. RawParseUtils.decode(raw, ptr, end)));
  508. if (windows) {
  509. // Windows ignores space and dot at end of file name.
  510. if (raw[end - 1] == ' ' || raw[end - 1] == '.')
  511. throw new CorruptObjectException(String.format(
  512. JGitText.get().corruptObjectInvalidNameEnd,
  513. Character.valueOf(((char) raw[end - 1]))));
  514. if (end - ptr >= 3)
  515. checkNotWindowsDevice(raw, ptr, end);
  516. }
  517. }
  518. // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
  519. // to ".git" therefore we should prevent such names
  520. private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
  521. throws CorruptObjectException {
  522. boolean ignorable = false;
  523. byte[] git = new byte[] { '.', 'g', 'i', 't' };
  524. int g = 0;
  525. while (ptr < end) {
  526. switch (raw[ptr]) {
  527. case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
  528. checkTruncatedIgnorableUTF8(raw, ptr, end);
  529. switch (raw[ptr + 1]) {
  530. case (byte) 0x80:
  531. switch (raw[ptr + 2]) {
  532. case (byte) 0x8c: // U+200C 0xe2808c ZERO WIDTH NON-JOINER
  533. case (byte) 0x8d: // U+200D 0xe2808d ZERO WIDTH JOINER
  534. case (byte) 0x8e: // U+200E 0xe2808e LEFT-TO-RIGHT MARK
  535. case (byte) 0x8f: // U+200F 0xe2808f RIGHT-TO-LEFT MARK
  536. case (byte) 0xaa: // U+202A 0xe280aa LEFT-TO-RIGHT EMBEDDING
  537. case (byte) 0xab: // U+202B 0xe280ab RIGHT-TO-LEFT EMBEDDING
  538. case (byte) 0xac: // U+202C 0xe280ac POP DIRECTIONAL FORMATTING
  539. case (byte) 0xad: // U+202D 0xe280ad LEFT-TO-RIGHT OVERRIDE
  540. case (byte) 0xae: // U+202E 0xe280ae RIGHT-TO-LEFT OVERRIDE
  541. ignorable = true;
  542. ptr += 3;
  543. continue;
  544. default:
  545. return false;
  546. }
  547. case (byte) 0x81:
  548. switch (raw[ptr + 2]) {
  549. case (byte) 0xaa: // U+206A 0xe281aa INHIBIT SYMMETRIC SWAPPING
  550. case (byte) 0xab: // U+206B 0xe281ab ACTIVATE SYMMETRIC SWAPPING
  551. case (byte) 0xac: // U+206C 0xe281ac INHIBIT ARABIC FORM SHAPING
  552. case (byte) 0xad: // U+206D 0xe281ad ACTIVATE ARABIC FORM SHAPING
  553. case (byte) 0xae: // U+206E 0xe281ae NATIONAL DIGIT SHAPES
  554. case (byte) 0xaf: // U+206F 0xe281af NOMINAL DIGIT SHAPES
  555. ignorable = true;
  556. ptr += 3;
  557. continue;
  558. default:
  559. return false;
  560. }
  561. }
  562. break;
  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) : n;
  718. }
  719. private static class Normalizer {
  720. // TODO Simplify invocation to Normalizer after dropping Java 5.
  721. private static final Method normalize;
  722. private static final Object nfc;
  723. static {
  724. Method method;
  725. Object formNfc;
  726. try {
  727. Class<?> formClazz = Class.forName("java.text.Normalizer$Form"); //$NON-NLS-1$
  728. formNfc = formClazz.getField("NFC").get(null); //$NON-NLS-1$
  729. method = Class.forName("java.text.Normalizer") //$NON-NLS-1$
  730. .getMethod("normalize", CharSequence.class, formClazz); //$NON-NLS-1$
  731. } catch (ClassNotFoundException e) {
  732. method = null;
  733. formNfc = null;
  734. } catch (NoSuchFieldException e) {
  735. method = null;
  736. formNfc = null;
  737. } catch (NoSuchMethodException e) {
  738. method = null;
  739. formNfc = null;
  740. } catch (SecurityException e) {
  741. method = null;
  742. formNfc = null;
  743. } catch (IllegalArgumentException e) {
  744. method = null;
  745. formNfc = null;
  746. } catch (IllegalAccessException e) {
  747. method = null;
  748. formNfc = null;
  749. }
  750. normalize = method;
  751. nfc = formNfc;
  752. }
  753. static String normalize(String in) {
  754. if (normalize == null)
  755. return in;
  756. try {
  757. return (String) normalize.invoke(null, in, nfc);
  758. } catch (IllegalAccessException e) {
  759. return in;
  760. } catch (InvocationTargetException e) {
  761. if (e.getCause() instanceof RuntimeException)
  762. throw (RuntimeException) e.getCause();
  763. if (e.getCause() instanceof Error)
  764. throw (Error) e.getCause();
  765. return in;
  766. }
  767. }
  768. }
  769. }