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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 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. * Restrict trees to only names legal on Windows platforms.
  111. * <p>
  112. * Also rejects any mixed case forms of reserved names ({@code .git}).
  113. *
  114. * @param win true if Windows name checking should be performed.
  115. * @return {@code this}.
  116. * @since 3.4
  117. */
  118. public ObjectChecker setSafeForWindows(boolean win) {
  119. windows = win;
  120. return this;
  121. }
  122. /**
  123. * Restrict trees to only names legal on Mac OS X platforms.
  124. * <p>
  125. * Rejects any mixed case forms of reserved names ({@code .git})
  126. * for users working on HFS+ in case-insensitive (default) mode.
  127. *
  128. * @param mac true if Mac OS X name checking should be performed.
  129. * @return {@code this}.
  130. * @since 3.4
  131. */
  132. public ObjectChecker setSafeForMacOS(boolean mac) {
  133. macosx = mac;
  134. return this;
  135. }
  136. /**
  137. * Check an object for parsing errors.
  138. *
  139. * @param objType
  140. * type of the object. Must be a valid object type code in
  141. * {@link Constants}.
  142. * @param raw
  143. * the raw data which comprises the object. This should be in the
  144. * canonical format (that is the format used to generate the
  145. * ObjectId of the object). The array is never modified.
  146. * @throws CorruptObjectException
  147. * if an error is identified.
  148. */
  149. public void check(final int objType, final byte[] raw)
  150. throws CorruptObjectException {
  151. switch (objType) {
  152. case Constants.OBJ_COMMIT:
  153. checkCommit(raw);
  154. break;
  155. case Constants.OBJ_TAG:
  156. checkTag(raw);
  157. break;
  158. case Constants.OBJ_TREE:
  159. checkTree(raw);
  160. break;
  161. case Constants.OBJ_BLOB:
  162. checkBlob(raw);
  163. break;
  164. default:
  165. throw new CorruptObjectException(MessageFormat.format(
  166. JGitText.get().corruptObjectInvalidType2,
  167. Integer.valueOf(objType)));
  168. }
  169. }
  170. private int id(final byte[] raw, final int ptr) {
  171. try {
  172. tempId.fromString(raw, ptr);
  173. return ptr + Constants.OBJECT_ID_STRING_LENGTH;
  174. } catch (IllegalArgumentException e) {
  175. return -1;
  176. }
  177. }
  178. private int personIdent(final byte[] raw, int ptr) {
  179. final int emailB = nextLF(raw, ptr, '<');
  180. if (emailB == ptr || raw[emailB - 1] != '<')
  181. return -1;
  182. final int emailE = nextLF(raw, emailB, '>');
  183. if (emailE == emailB || raw[emailE - 1] != '>')
  184. return -1;
  185. if (emailE == raw.length || raw[emailE] != ' ')
  186. return -1;
  187. parseBase10(raw, emailE + 1, ptrout); // when
  188. ptr = ptrout.value;
  189. if (emailE + 1 == ptr)
  190. return -1;
  191. if (ptr == raw.length || raw[ptr] != ' ')
  192. return -1;
  193. parseBase10(raw, ptr + 1, ptrout); // tz offset
  194. if (ptr + 1 == ptrout.value)
  195. return -1;
  196. return ptrout.value;
  197. }
  198. /**
  199. * Check a commit for errors.
  200. *
  201. * @param raw
  202. * the commit data. The array is never modified.
  203. * @throws CorruptObjectException
  204. * if any error was detected.
  205. */
  206. public void checkCommit(final byte[] raw) throws CorruptObjectException {
  207. int ptr = 0;
  208. if ((ptr = match(raw, ptr, tree)) < 0)
  209. throw new CorruptObjectException("no tree header");
  210. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  211. throw new CorruptObjectException("invalid tree");
  212. while (match(raw, ptr, parent) >= 0) {
  213. ptr += parent.length;
  214. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  215. throw new CorruptObjectException("invalid parent");
  216. }
  217. if ((ptr = match(raw, ptr, author)) < 0)
  218. throw new CorruptObjectException("no author");
  219. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  220. throw new CorruptObjectException("invalid author");
  221. if ((ptr = match(raw, ptr, committer)) < 0)
  222. throw new CorruptObjectException("no committer");
  223. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  224. throw new CorruptObjectException("invalid committer");
  225. }
  226. /**
  227. * Check an annotated tag for errors.
  228. *
  229. * @param raw
  230. * the tag data. The array is never modified.
  231. * @throws CorruptObjectException
  232. * if any error was detected.
  233. */
  234. public void checkTag(final byte[] raw) throws CorruptObjectException {
  235. int ptr = 0;
  236. if ((ptr = match(raw, ptr, object)) < 0)
  237. throw new CorruptObjectException("no object header");
  238. if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
  239. throw new CorruptObjectException("invalid object");
  240. if ((ptr = match(raw, ptr, type)) < 0)
  241. throw new CorruptObjectException("no type header");
  242. ptr = nextLF(raw, ptr);
  243. if ((ptr = match(raw, ptr, tag)) < 0)
  244. throw new CorruptObjectException("no tag header");
  245. ptr = nextLF(raw, ptr);
  246. if ((ptr = match(raw, ptr, tagger)) > 0) {
  247. if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
  248. throw new CorruptObjectException("invalid tagger");
  249. }
  250. }
  251. private static int lastPathChar(final int mode) {
  252. return FileMode.TREE.equals(mode) ? '/' : '\0';
  253. }
  254. private static int pathCompare(final byte[] raw, int aPos, final int aEnd,
  255. final int aMode, int bPos, final int bEnd, final int bMode) {
  256. while (aPos < aEnd && bPos < bEnd) {
  257. final int cmp = (raw[aPos++] & 0xff) - (raw[bPos++] & 0xff);
  258. if (cmp != 0)
  259. return cmp;
  260. }
  261. if (aPos < aEnd)
  262. return (raw[aPos] & 0xff) - lastPathChar(bMode);
  263. if (bPos < bEnd)
  264. return lastPathChar(aMode) - (raw[bPos] & 0xff);
  265. return 0;
  266. }
  267. private static boolean duplicateName(final byte[] raw,
  268. final int thisNamePos, final int thisNameEnd) {
  269. final int sz = raw.length;
  270. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  271. for (;;) {
  272. int nextMode = 0;
  273. for (;;) {
  274. if (nextPtr >= sz)
  275. return false;
  276. final byte c = raw[nextPtr++];
  277. if (' ' == c)
  278. break;
  279. nextMode <<= 3;
  280. nextMode += c - '0';
  281. }
  282. final int nextNamePos = nextPtr;
  283. for (;;) {
  284. if (nextPtr == sz)
  285. return false;
  286. final byte c = raw[nextPtr++];
  287. if (c == 0)
  288. break;
  289. }
  290. if (nextNamePos + 1 == nextPtr)
  291. return false;
  292. final int cmp = pathCompare(raw, thisNamePos, thisNameEnd,
  293. FileMode.TREE.getBits(), nextNamePos, nextPtr - 1, nextMode);
  294. if (cmp < 0)
  295. return false;
  296. else if (cmp == 0)
  297. return true;
  298. nextPtr += Constants.OBJECT_ID_LENGTH;
  299. }
  300. }
  301. /**
  302. * Check a canonical formatted tree for errors.
  303. *
  304. * @param raw
  305. * the raw tree data. The array is never modified.
  306. * @throws CorruptObjectException
  307. * if any error was detected.
  308. */
  309. public void checkTree(final byte[] raw) throws CorruptObjectException {
  310. final int sz = raw.length;
  311. int ptr = 0;
  312. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  313. Set<String> normalized = windows || macosx
  314. ? new HashSet<String>()
  315. : null;
  316. while (ptr < sz) {
  317. int thisMode = 0;
  318. for (;;) {
  319. if (ptr == sz)
  320. throw new CorruptObjectException("truncated in mode");
  321. final byte c = raw[ptr++];
  322. if (' ' == c)
  323. break;
  324. if (c < '0' || c > '7')
  325. throw new CorruptObjectException("invalid mode character");
  326. if (thisMode == 0 && c == '0' && !allowZeroMode)
  327. throw new CorruptObjectException("mode starts with '0'");
  328. thisMode <<= 3;
  329. thisMode += c - '0';
  330. }
  331. if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
  332. throw new CorruptObjectException("invalid mode " + thisMode);
  333. final int thisNameB = ptr;
  334. ptr = scanPathSegment(raw, ptr, sz);
  335. if (ptr == sz || raw[ptr] != 0)
  336. throw new CorruptObjectException("truncated in name");
  337. checkPathSegment2(raw, thisNameB, ptr);
  338. if (normalized != null) {
  339. if (!normalized.add(normalize(raw, thisNameB, ptr)))
  340. throw new CorruptObjectException("duplicate entry names");
  341. } else if (duplicateName(raw, thisNameB, ptr))
  342. throw new CorruptObjectException("duplicate entry names");
  343. if (lastNameB != 0) {
  344. final int cmp = pathCompare(raw, lastNameB, lastNameE,
  345. lastMode, thisNameB, ptr, thisMode);
  346. if (cmp > 0)
  347. throw new CorruptObjectException("incorrectly sorted");
  348. }
  349. lastNameB = thisNameB;
  350. lastNameE = ptr;
  351. lastMode = thisMode;
  352. ptr += 1 + Constants.OBJECT_ID_LENGTH;
  353. if (ptr > sz)
  354. throw new CorruptObjectException("truncated in object id");
  355. }
  356. }
  357. private int scanPathSegment(byte[] raw, int ptr, int end)
  358. throws CorruptObjectException {
  359. for (; ptr < end; ptr++) {
  360. byte c = raw[ptr];
  361. if (c == 0)
  362. return ptr;
  363. if (c == '/')
  364. throw new CorruptObjectException("name contains '/'");
  365. if (windows && isInvalidOnWindows(c)) {
  366. if (c > 31)
  367. throw new CorruptObjectException(String.format(
  368. "name contains '%c'", c));
  369. throw new CorruptObjectException(String.format(
  370. "name contains byte 0x%x", c & 0xff));
  371. }
  372. }
  373. return ptr;
  374. }
  375. /**
  376. * Check tree path entry for validity.
  377. *
  378. * @param raw buffer to scan.
  379. * @param ptr offset to first byte of the name.
  380. * @param end offset to one past last byte of name.
  381. * @throws CorruptObjectException name is invalid.
  382. * @since 3.4
  383. */
  384. public void checkPathSegment(byte[] raw, int ptr, int end)
  385. throws CorruptObjectException {
  386. int e = scanPathSegment(raw, ptr, end);
  387. if (e < end && raw[e] == 0)
  388. throw new CorruptObjectException("name contains byte 0x00");
  389. checkPathSegment2(raw, ptr, end);
  390. }
  391. private void checkPathSegment2(byte[] raw, int ptr, int end)
  392. throws CorruptObjectException {
  393. if (ptr == end)
  394. throw new CorruptObjectException("zero length name");
  395. if (raw[ptr] == '.') {
  396. switch (end - ptr) {
  397. case 1:
  398. throw new CorruptObjectException("invalid name '.'");
  399. case 2:
  400. if (raw[ptr + 1] == '.')
  401. throw new CorruptObjectException("invalid name '..'");
  402. break;
  403. case 4:
  404. if (isGit(raw, ptr + 1))
  405. throw new CorruptObjectException(String.format(
  406. "invalid name '%s'",
  407. RawParseUtils.decode(raw, ptr, end)));
  408. break;
  409. default:
  410. if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end))
  411. throw new CorruptObjectException(String.format(
  412. "invalid name '%s'",
  413. RawParseUtils.decode(raw, ptr, end)));
  414. }
  415. } else if (isGitTilde1(raw, ptr, end)) {
  416. throw new CorruptObjectException(String.format("invalid name '%s'",
  417. RawParseUtils.decode(raw, ptr, end)));
  418. }
  419. if (macosx && isMacHFSGit(raw, ptr, end))
  420. throw new CorruptObjectException(String.format(
  421. "invalid name '%s' contains ignorable Unicode characters",
  422. RawParseUtils.decode(raw, ptr, end)));
  423. if (windows) {
  424. // Windows ignores space and dot at end of file name.
  425. if (raw[end - 1] == ' ' || raw[end - 1] == '.')
  426. throw new CorruptObjectException("invalid name ends with '"
  427. + ((char) raw[end - 1]) + "'");
  428. if (end - ptr >= 3)
  429. checkNotWindowsDevice(raw, ptr, end);
  430. }
  431. }
  432. // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
  433. // to ".git" therefore we should prevent such names
  434. private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
  435. throws CorruptObjectException {
  436. boolean ignorable = false;
  437. byte[] git = new byte[] { '.', 'g', 'i', 't' };
  438. int g = 0;
  439. while (ptr < end) {
  440. switch (raw[ptr]) {
  441. case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
  442. checkTruncatedIgnorableUTF8(raw, ptr, end);
  443. switch (raw[ptr + 1]) {
  444. case (byte) 0x80:
  445. switch (raw[ptr + 2]) {
  446. case (byte) 0x8c: // U+200C 0xe2808c ZERO WIDTH NON-JOINER
  447. case (byte) 0x8d: // U+200D 0xe2808d ZERO WIDTH JOINER
  448. case (byte) 0x8e: // U+200E 0xe2808e LEFT-TO-RIGHT MARK
  449. case (byte) 0x8f: // U+200F 0xe2808f RIGHT-TO-LEFT MARK
  450. case (byte) 0xaa: // U+202A 0xe280aa LEFT-TO-RIGHT EMBEDDING
  451. case (byte) 0xab: // U+202B 0xe280ab RIGHT-TO-LEFT EMBEDDING
  452. case (byte) 0xac: // U+202C 0xe280ac POP DIRECTIONAL FORMATTING
  453. case (byte) 0xad: // U+202D 0xe280ad LEFT-TO-RIGHT OVERRIDE
  454. case (byte) 0xae: // U+202E 0xe280ae RIGHT-TO-LEFT OVERRIDE
  455. ignorable = true;
  456. ptr += 3;
  457. continue;
  458. default:
  459. return false;
  460. }
  461. case (byte) 0x81:
  462. switch (raw[ptr + 2]) {
  463. case (byte) 0xaa: // U+206A 0xe281aa INHIBIT SYMMETRIC SWAPPING
  464. case (byte) 0xab: // U+206B 0xe281ab ACTIVATE SYMMETRIC SWAPPING
  465. case (byte) 0xac: // U+206C 0xe281ac INHIBIT ARABIC FORM SHAPING
  466. case (byte) 0xad: // U+206D 0xe281ad ACTIVATE ARABIC FORM SHAPING
  467. case (byte) 0xae: // U+206E 0xe281ae NATIONAL DIGIT SHAPES
  468. case (byte) 0xaf: // U+206F 0xe281af NOMINAL DIGIT SHAPES
  469. ignorable = true;
  470. ptr += 3;
  471. continue;
  472. default:
  473. return false;
  474. }
  475. }
  476. break;
  477. case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024
  478. checkTruncatedIgnorableUTF8(raw, ptr, end);
  479. // U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE
  480. if ((raw[ptr + 1] == (byte) 0xbb)
  481. && (raw[ptr + 2] == (byte) 0xbf)) {
  482. ignorable = true;
  483. ptr += 3;
  484. continue;
  485. }
  486. return false;
  487. default:
  488. if (g == 4)
  489. return false;
  490. if (raw[ptr++] != git[g++])
  491. return false;
  492. }
  493. }
  494. if (g == 4 && ignorable)
  495. return true;
  496. return false;
  497. }
  498. private static void checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end)
  499. throws CorruptObjectException {
  500. if ((ptr + 2) >= end)
  501. throw new CorruptObjectException(MessageFormat.format(
  502. "invalid name contains byte sequence ''{0}'' which is not a valid UTF-8 character",
  503. toHexString(raw, ptr, end)));
  504. }
  505. private static String toHexString(byte[] raw, int ptr, int end) {
  506. StringBuilder b = new StringBuilder("0x"); //$NON-NLS-1$
  507. for (int i = ptr; i < end; i++)
  508. b.append(String.format("%02x", Byte.valueOf(raw[i]))); //$NON-NLS-1$
  509. return b.toString();
  510. }
  511. private static void checkNotWindowsDevice(byte[] raw, int ptr, int end)
  512. throws CorruptObjectException {
  513. switch (toLower(raw[ptr])) {
  514. case 'a': // AUX
  515. if (end - ptr >= 3
  516. && toLower(raw[ptr + 1]) == 'u'
  517. && toLower(raw[ptr + 2]) == 'x'
  518. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  519. throw new CorruptObjectException("invalid name 'AUX'");
  520. break;
  521. case 'c': // CON, COM[1-9]
  522. if (end - ptr >= 3
  523. && toLower(raw[ptr + 2]) == 'n'
  524. && toLower(raw[ptr + 1]) == 'o'
  525. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  526. throw new CorruptObjectException("invalid name 'CON'");
  527. if (end - ptr >= 4
  528. && toLower(raw[ptr + 2]) == 'm'
  529. && toLower(raw[ptr + 1]) == 'o'
  530. && isPositiveDigit(raw[ptr + 3])
  531. && (end - ptr == 4 || raw[ptr + 4] == '.'))
  532. throw new CorruptObjectException("invalid name 'COM"
  533. + ((char) raw[ptr + 3]) + "'");
  534. break;
  535. case 'l': // LPT[1-9]
  536. if (end - ptr >= 4
  537. && toLower(raw[ptr + 1]) == 'p'
  538. && toLower(raw[ptr + 2]) == 't'
  539. && isPositiveDigit(raw[ptr + 3])
  540. && (end - ptr == 4 || raw[ptr + 4] == '.'))
  541. throw new CorruptObjectException("invalid name 'LPT"
  542. + ((char) raw[ptr + 3]) + "'");
  543. break;
  544. case 'n': // NUL
  545. if (end - ptr >= 3
  546. && toLower(raw[ptr + 1]) == 'u'
  547. && toLower(raw[ptr + 2]) == 'l'
  548. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  549. throw new CorruptObjectException("invalid name 'NUL'");
  550. break;
  551. case 'p': // PRN
  552. if (end - ptr >= 3
  553. && toLower(raw[ptr + 1]) == 'r'
  554. && toLower(raw[ptr + 2]) == 'n'
  555. && (end - ptr == 3 || raw[ptr + 3] == '.'))
  556. throw new CorruptObjectException("invalid name 'PRN'");
  557. break;
  558. }
  559. }
  560. private static boolean isInvalidOnWindows(byte c) {
  561. // Windows disallows "special" characters in a path component.
  562. switch (c) {
  563. case '"':
  564. case '*':
  565. case ':':
  566. case '<':
  567. case '>':
  568. case '?':
  569. case '\\':
  570. case '|':
  571. return true;
  572. }
  573. return 1 <= c && c <= 31;
  574. }
  575. private static boolean isGit(byte[] buf, int p) {
  576. return toLower(buf[p]) == 'g'
  577. && toLower(buf[p + 1]) == 'i'
  578. && toLower(buf[p + 2]) == 't';
  579. }
  580. private static boolean isGitTilde1(byte[] buf, int p, int end) {
  581. if (end - p != 5)
  582. return false;
  583. return toLower(buf[p]) == 'g' && toLower(buf[p + 1]) == 'i'
  584. && toLower(buf[p + 2]) == 't' && buf[p + 3] == '~'
  585. && buf[p + 4] == '1';
  586. }
  587. private static boolean isNormalizedGit(byte[] raw, int ptr, int end) {
  588. if (isGit(raw, ptr)) {
  589. int dots = 0;
  590. boolean space = false;
  591. int p = end - 1;
  592. for (; (ptr + 2) < p; p--) {
  593. if (raw[p] == '.')
  594. dots++;
  595. else if (raw[p] == ' ')
  596. space = true;
  597. else
  598. break;
  599. }
  600. return p == ptr + 2 && (dots == 1 || space);
  601. }
  602. return false;
  603. }
  604. private static char toLower(byte b) {
  605. if ('A' <= b && b <= 'Z')
  606. return (char) (b + ('a' - 'A'));
  607. return (char) b;
  608. }
  609. private static boolean isPositiveDigit(byte b) {
  610. return '1' <= b && b <= '9';
  611. }
  612. /**
  613. * Check a blob for errors.
  614. *
  615. * @param raw
  616. * the blob data. The array is never modified.
  617. * @throws CorruptObjectException
  618. * if any error was detected.
  619. */
  620. public void checkBlob(final byte[] raw) throws CorruptObjectException {
  621. // We can always assume the blob is valid.
  622. }
  623. private String normalize(byte[] raw, int ptr, int end) {
  624. String n = RawParseUtils.decode(raw, ptr, end).toLowerCase(Locale.US);
  625. return macosx ? Normalizer.normalize(n) : n;
  626. }
  627. private static class Normalizer {
  628. // TODO Simplify invocation to Normalizer after dropping Java 5.
  629. private static final Method normalize;
  630. private static final Object nfc;
  631. static {
  632. Method method;
  633. Object formNfc;
  634. try {
  635. Class<?> formClazz = Class.forName("java.text.Normalizer$Form"); //$NON-NLS-1$
  636. formNfc = formClazz.getField("NFC").get(null); //$NON-NLS-1$
  637. method = Class.forName("java.text.Normalizer") //$NON-NLS-1$
  638. .getMethod("normalize", CharSequence.class, formClazz); //$NON-NLS-1$
  639. } catch (ClassNotFoundException e) {
  640. method = null;
  641. formNfc = null;
  642. } catch (NoSuchFieldException e) {
  643. method = null;
  644. formNfc = null;
  645. } catch (NoSuchMethodException e) {
  646. method = null;
  647. formNfc = null;
  648. } catch (SecurityException e) {
  649. method = null;
  650. formNfc = null;
  651. } catch (IllegalArgumentException e) {
  652. method = null;
  653. formNfc = null;
  654. } catch (IllegalAccessException e) {
  655. method = null;
  656. formNfc = null;
  657. }
  658. normalize = method;
  659. nfc = formNfc;
  660. }
  661. static String normalize(String in) {
  662. if (normalize == null)
  663. return in;
  664. try {
  665. return (String) normalize.invoke(null, in, nfc);
  666. } catch (IllegalAccessException e) {
  667. return in;
  668. } catch (InvocationTargetException e) {
  669. if (e.getCause() instanceof RuntimeException)
  670. throw (RuntimeException) e.getCause();
  671. if (e.getCause() instanceof Error)
  672. throw (Error) e.getCause();
  673. return in;
  674. }
  675. }
  676. }
  677. }