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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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.lib.Constants.DOT_GIT_MODULES;
  46. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  47. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  48. import static org.eclipse.jgit.lib.Constants.OBJ_BAD;
  49. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  50. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  51. import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
  52. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  53. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_DATE;
  54. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_EMAIL;
  55. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_OBJECT_SHA1;
  56. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_PARENT_SHA1;
  57. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_TIMEZONE;
  58. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_TREE_SHA1;
  59. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_UTF8;
  60. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.DUPLICATE_ENTRIES;
  61. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.EMPTY_NAME;
  62. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.FULL_PATHNAME;
  63. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOT;
  64. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTDOT;
  65. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTGIT;
  66. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_AUTHOR;
  67. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_COMMITTER;
  68. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_EMAIL;
  69. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_OBJECT;
  70. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_SPACE_BEFORE_DATE;
  71. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TAG_ENTRY;
  72. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TREE;
  73. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TYPE_ENTRY;
  74. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.NULL_SHA1;
  75. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.TREE_NOT_SORTED;
  76. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.UNKNOWN_TYPE;
  77. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.WIN32_BAD_NAME;
  78. import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE;
  79. import static org.eclipse.jgit.util.Paths.compare;
  80. import static org.eclipse.jgit.util.Paths.compareSameName;
  81. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  82. import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
  83. import java.text.MessageFormat;
  84. import java.text.Normalizer;
  85. import java.util.ArrayList;
  86. import java.util.EnumSet;
  87. import java.util.HashSet;
  88. import java.util.List;
  89. import java.util.Locale;
  90. import java.util.Set;
  91. import org.eclipse.jgit.annotations.NonNull;
  92. import org.eclipse.jgit.annotations.Nullable;
  93. import org.eclipse.jgit.errors.CorruptObjectException;
  94. import org.eclipse.jgit.internal.JGitText;
  95. import org.eclipse.jgit.util.MutableInteger;
  96. import org.eclipse.jgit.util.RawParseUtils;
  97. import org.eclipse.jgit.util.StringUtils;
  98. /**
  99. * Verifies that an object is formatted correctly.
  100. * <p>
  101. * Verifications made by this class only check that the fields of an object are
  102. * formatted correctly. The ObjectId checksum of the object is not verified, and
  103. * connectivity links between objects are also not verified. Its assumed that
  104. * the caller can provide both of these validations on its own.
  105. * <p>
  106. * Instances of this class are not thread safe, but they may be reused to
  107. * perform multiple object validations.
  108. */
  109. public class ObjectChecker {
  110. /** Header "tree " */
  111. public static final byte[] tree = Constants.encodeASCII("tree "); //$NON-NLS-1$
  112. /** Header "parent " */
  113. public static final byte[] parent = Constants.encodeASCII("parent "); //$NON-NLS-1$
  114. /** Header "author " */
  115. public static final byte[] author = Constants.encodeASCII("author "); //$NON-NLS-1$
  116. /** Header "committer " */
  117. public static final byte[] committer = Constants.encodeASCII("committer "); //$NON-NLS-1$
  118. /** Header "encoding " */
  119. public static final byte[] encoding = Constants.encodeASCII("encoding "); //$NON-NLS-1$
  120. /** Header "object " */
  121. public static final byte[] object = Constants.encodeASCII("object "); //$NON-NLS-1$
  122. /** Header "type " */
  123. public static final byte[] type = Constants.encodeASCII("type "); //$NON-NLS-1$
  124. /** Header "tag " */
  125. public static final byte[] tag = Constants.encodeASCII("tag "); //$NON-NLS-1$
  126. /** Header "tagger " */
  127. public static final byte[] tagger = Constants.encodeASCII("tagger "); //$NON-NLS-1$
  128. /** Path ".gitmodules" */
  129. private static final byte[] dotGitmodules = Constants.encodeASCII(DOT_GIT_MODULES);
  130. /**
  131. * Potential issues identified by the checker.
  132. *
  133. * @since 4.2
  134. */
  135. public enum ErrorType {
  136. // @formatter:off
  137. // These names match git-core so that fsck section keys also match.
  138. /***/ NULL_SHA1,
  139. /***/ DUPLICATE_ENTRIES,
  140. /***/ TREE_NOT_SORTED,
  141. /***/ ZERO_PADDED_FILEMODE,
  142. /***/ EMPTY_NAME,
  143. /***/ FULL_PATHNAME,
  144. /***/ HAS_DOT,
  145. /***/ HAS_DOTDOT,
  146. /***/ HAS_DOTGIT,
  147. /***/ BAD_OBJECT_SHA1,
  148. /***/ BAD_PARENT_SHA1,
  149. /***/ BAD_TREE_SHA1,
  150. /***/ MISSING_AUTHOR,
  151. /***/ MISSING_COMMITTER,
  152. /***/ MISSING_OBJECT,
  153. /***/ MISSING_TREE,
  154. /***/ MISSING_TYPE_ENTRY,
  155. /***/ MISSING_TAG_ENTRY,
  156. /***/ BAD_DATE,
  157. /***/ BAD_EMAIL,
  158. /***/ BAD_TIMEZONE,
  159. /***/ MISSING_EMAIL,
  160. /***/ MISSING_SPACE_BEFORE_DATE,
  161. /***/ UNKNOWN_TYPE,
  162. // These are unique to JGit.
  163. /***/ WIN32_BAD_NAME,
  164. /***/ BAD_UTF8;
  165. // @formatter:on
  166. /** @return camelCaseVersion of the name. */
  167. public String getMessageId() {
  168. String n = name();
  169. StringBuilder r = new StringBuilder(n.length());
  170. for (int i = 0; i < n.length(); i++) {
  171. char c = n.charAt(i);
  172. if (c != '_') {
  173. r.append(StringUtils.toLowerCase(c));
  174. } else {
  175. r.append(n.charAt(++i));
  176. }
  177. }
  178. return r.toString();
  179. }
  180. }
  181. private final MutableObjectId tempId = new MutableObjectId();
  182. private final MutableInteger bufPtr = new MutableInteger();
  183. private EnumSet<ErrorType> errors = EnumSet.allOf(ErrorType.class);
  184. private ObjectIdSet skipList;
  185. private boolean allowInvalidPersonIdent;
  186. private boolean windows;
  187. private boolean macosx;
  188. private final List<GitmoduleEntry> gitsubmodules = new ArrayList<>();
  189. /**
  190. * Enable accepting specific malformed (but not horribly broken) objects.
  191. *
  192. * @param objects
  193. * collection of object names known to be broken in a non-fatal
  194. * way that should be ignored by the checker.
  195. * @return {@code this}
  196. * @since 4.2
  197. */
  198. public ObjectChecker setSkipList(@Nullable ObjectIdSet objects) {
  199. skipList = objects;
  200. return this;
  201. }
  202. /**
  203. * Configure error types to be ignored across all objects.
  204. *
  205. * @param ids
  206. * error types to ignore. The caller's set is copied.
  207. * @return {@code this}
  208. * @since 4.2
  209. */
  210. public ObjectChecker setIgnore(@Nullable Set<ErrorType> ids) {
  211. errors = EnumSet.allOf(ErrorType.class);
  212. if (ids != null) {
  213. errors.removeAll(ids);
  214. }
  215. return this;
  216. }
  217. /**
  218. * Add message type to be ignored across all objects.
  219. *
  220. * @param id
  221. * error type to ignore.
  222. * @param ignore
  223. * true to ignore this error; false to treat the error as an
  224. * error and throw.
  225. * @return {@code this}
  226. * @since 4.2
  227. */
  228. public ObjectChecker setIgnore(ErrorType id, boolean ignore) {
  229. if (ignore) {
  230. errors.remove(id);
  231. } else {
  232. errors.add(id);
  233. }
  234. return this;
  235. }
  236. /**
  237. * Enable accepting leading zero mode in tree entries.
  238. * <p>
  239. * Some broken Git libraries generated leading zeros in the mode part of
  240. * tree entries. This is technically incorrect but gracefully allowed by
  241. * git-core. JGit rejects such trees by default, but may need to accept
  242. * them on broken histories.
  243. * <p>
  244. * Same as {@code setIgnore(ZERO_PADDED_FILEMODE, allow)}.
  245. *
  246. * @param allow allow leading zero mode.
  247. * @return {@code this}.
  248. * @since 3.4
  249. */
  250. public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) {
  251. return setIgnore(ZERO_PADDED_FILEMODE, allow);
  252. }
  253. /**
  254. * Enable accepting invalid author, committer and tagger identities.
  255. * <p>
  256. * Some broken Git versions/libraries allowed users to create commits and
  257. * tags with invalid formatting between the name, email and timestamp.
  258. *
  259. * @param allow
  260. * if true accept invalid person identity strings.
  261. * @return {@code this}.
  262. * @since 4.0
  263. */
  264. public ObjectChecker setAllowInvalidPersonIdent(boolean allow) {
  265. allowInvalidPersonIdent = allow;
  266. return this;
  267. }
  268. /**
  269. * Restrict trees to only names legal on Windows platforms.
  270. * <p>
  271. * Also rejects any mixed case forms of reserved names ({@code .git}).
  272. *
  273. * @param win true if Windows name checking should be performed.
  274. * @return {@code this}.
  275. * @since 3.4
  276. */
  277. public ObjectChecker setSafeForWindows(boolean win) {
  278. windows = win;
  279. return this;
  280. }
  281. /**
  282. * Restrict trees to only names legal on Mac OS X platforms.
  283. * <p>
  284. * Rejects any mixed case forms of reserved names ({@code .git})
  285. * for users working on HFS+ in case-insensitive (default) mode.
  286. *
  287. * @param mac true if Mac OS X name checking should be performed.
  288. * @return {@code this}.
  289. * @since 3.4
  290. */
  291. public ObjectChecker setSafeForMacOS(boolean mac) {
  292. macosx = mac;
  293. return this;
  294. }
  295. /**
  296. * Check an object for parsing errors.
  297. *
  298. * @param objType
  299. * type of the object. Must be a valid object type code in
  300. * {@link Constants}.
  301. * @param raw
  302. * the raw data which comprises the object. This should be in the
  303. * canonical format (that is the format used to generate the
  304. * ObjectId of the object). The array is never modified.
  305. * @throws CorruptObjectException
  306. * if an error is identified.
  307. */
  308. public void check(int objType, byte[] raw)
  309. throws CorruptObjectException {
  310. check(idFor(objType, raw), objType, raw);
  311. }
  312. /**
  313. * Check an object for parsing errors.
  314. *
  315. * @param id
  316. * identify of the object being checked.
  317. * @param objType
  318. * type of the object. Must be a valid object type code in
  319. * {@link Constants}.
  320. * @param raw
  321. * the raw data which comprises the object. This should be in the
  322. * canonical format (that is the format used to generate the
  323. * ObjectId of the object). The array is never modified.
  324. * @throws CorruptObjectException
  325. * if an error is identified.
  326. * @since 4.2
  327. */
  328. public void check(@Nullable AnyObjectId id, int objType, byte[] raw)
  329. throws CorruptObjectException {
  330. switch (objType) {
  331. case OBJ_COMMIT:
  332. checkCommit(id, raw);
  333. break;
  334. case OBJ_TAG:
  335. checkTag(id, raw);
  336. break;
  337. case OBJ_TREE:
  338. checkTree(id, raw);
  339. break;
  340. case OBJ_BLOB:
  341. BlobObjectChecker checker = newBlobObjectChecker();
  342. if (checker == null) {
  343. checkBlob(raw);
  344. } else {
  345. checker.update(raw, 0, raw.length);
  346. checker.endBlob(id);
  347. }
  348. break;
  349. default:
  350. report(UNKNOWN_TYPE, id, MessageFormat.format(
  351. JGitText.get().corruptObjectInvalidType2,
  352. Integer.valueOf(objType)));
  353. }
  354. }
  355. private boolean checkId(byte[] raw) {
  356. int p = bufPtr.value;
  357. try {
  358. tempId.fromString(raw, p);
  359. } catch (IllegalArgumentException e) {
  360. bufPtr.value = nextLF(raw, p);
  361. return false;
  362. }
  363. p += OBJECT_ID_STRING_LENGTH;
  364. if (raw[p] == '\n') {
  365. bufPtr.value = p + 1;
  366. return true;
  367. }
  368. bufPtr.value = nextLF(raw, p);
  369. return false;
  370. }
  371. private void checkPersonIdent(byte[] raw, @Nullable AnyObjectId id)
  372. throws CorruptObjectException {
  373. if (allowInvalidPersonIdent) {
  374. bufPtr.value = nextLF(raw, bufPtr.value);
  375. return;
  376. }
  377. final int emailB = nextLF(raw, bufPtr.value, '<');
  378. if (emailB == bufPtr.value || raw[emailB - 1] != '<') {
  379. report(MISSING_EMAIL, id, JGitText.get().corruptObjectMissingEmail);
  380. bufPtr.value = nextLF(raw, bufPtr.value);
  381. return;
  382. }
  383. final int emailE = nextLF(raw, emailB, '>');
  384. if (emailE == emailB || raw[emailE - 1] != '>') {
  385. report(BAD_EMAIL, id, JGitText.get().corruptObjectBadEmail);
  386. bufPtr.value = nextLF(raw, bufPtr.value);
  387. return;
  388. }
  389. if (emailE == raw.length || raw[emailE] != ' ') {
  390. report(MISSING_SPACE_BEFORE_DATE, id,
  391. JGitText.get().corruptObjectBadDate);
  392. bufPtr.value = nextLF(raw, bufPtr.value);
  393. return;
  394. }
  395. parseBase10(raw, emailE + 1, bufPtr); // when
  396. if (emailE + 1 == bufPtr.value || bufPtr.value == raw.length
  397. || raw[bufPtr.value] != ' ') {
  398. report(BAD_DATE, id, JGitText.get().corruptObjectBadDate);
  399. bufPtr.value = nextLF(raw, bufPtr.value);
  400. return;
  401. }
  402. int p = bufPtr.value + 1;
  403. parseBase10(raw, p, bufPtr); // tz offset
  404. if (p == bufPtr.value) {
  405. report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
  406. bufPtr.value = nextLF(raw, bufPtr.value);
  407. return;
  408. }
  409. p = bufPtr.value;
  410. if (raw[p] == '\n') {
  411. bufPtr.value = p + 1;
  412. } else {
  413. report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
  414. bufPtr.value = nextLF(raw, p);
  415. }
  416. }
  417. /**
  418. * Check a commit for errors.
  419. *
  420. * @param raw
  421. * the commit data. The array is never modified.
  422. * @throws CorruptObjectException
  423. * if any error was detected.
  424. */
  425. public void checkCommit(byte[] raw) throws CorruptObjectException {
  426. checkCommit(idFor(OBJ_COMMIT, raw), raw);
  427. }
  428. /**
  429. * Check a commit for errors.
  430. *
  431. * @param id
  432. * identity of the object being checked.
  433. * @param raw
  434. * the commit data. The array is never modified.
  435. * @throws CorruptObjectException
  436. * if any error was detected.
  437. * @since 4.2
  438. */
  439. public void checkCommit(@Nullable AnyObjectId id, byte[] raw)
  440. throws CorruptObjectException {
  441. bufPtr.value = 0;
  442. if (!match(raw, tree)) {
  443. report(MISSING_TREE, id, JGitText.get().corruptObjectNotreeHeader);
  444. } else if (!checkId(raw)) {
  445. report(BAD_TREE_SHA1, id, JGitText.get().corruptObjectInvalidTree);
  446. }
  447. while (match(raw, parent)) {
  448. if (!checkId(raw)) {
  449. report(BAD_PARENT_SHA1, id,
  450. JGitText.get().corruptObjectInvalidParent);
  451. }
  452. }
  453. if (match(raw, author)) {
  454. checkPersonIdent(raw, id);
  455. } else {
  456. report(MISSING_AUTHOR, id, JGitText.get().corruptObjectNoAuthor);
  457. }
  458. if (match(raw, committer)) {
  459. checkPersonIdent(raw, id);
  460. } else {
  461. report(MISSING_COMMITTER, id,
  462. JGitText.get().corruptObjectNoCommitter);
  463. }
  464. }
  465. /**
  466. * Check an annotated tag for errors.
  467. *
  468. * @param raw
  469. * the tag data. The array is never modified.
  470. * @throws CorruptObjectException
  471. * if any error was detected.
  472. */
  473. public void checkTag(byte[] raw) throws CorruptObjectException {
  474. checkTag(idFor(OBJ_TAG, raw), raw);
  475. }
  476. /**
  477. * Check an annotated tag for errors.
  478. *
  479. * @param id
  480. * identity of the object being checked.
  481. * @param raw
  482. * the tag data. The array is never modified.
  483. * @throws CorruptObjectException
  484. * if any error was detected.
  485. * @since 4.2
  486. */
  487. public void checkTag(@Nullable AnyObjectId id, byte[] raw)
  488. throws CorruptObjectException {
  489. bufPtr.value = 0;
  490. if (!match(raw, object)) {
  491. report(MISSING_OBJECT, id,
  492. JGitText.get().corruptObjectNoObjectHeader);
  493. } else if (!checkId(raw)) {
  494. report(BAD_OBJECT_SHA1, id,
  495. JGitText.get().corruptObjectInvalidObject);
  496. }
  497. if (!match(raw, type)) {
  498. report(MISSING_TYPE_ENTRY, id,
  499. JGitText.get().corruptObjectNoTypeHeader);
  500. }
  501. bufPtr.value = nextLF(raw, bufPtr.value);
  502. if (!match(raw, tag)) {
  503. report(MISSING_TAG_ENTRY, id,
  504. JGitText.get().corruptObjectNoTagHeader);
  505. }
  506. bufPtr.value = nextLF(raw, bufPtr.value);
  507. if (match(raw, tagger)) {
  508. checkPersonIdent(raw, id);
  509. }
  510. }
  511. private static boolean duplicateName(final byte[] raw,
  512. final int thisNamePos, final int thisNameEnd) {
  513. final int sz = raw.length;
  514. int nextPtr = thisNameEnd + 1 + Constants.OBJECT_ID_LENGTH;
  515. for (;;) {
  516. int nextMode = 0;
  517. for (;;) {
  518. if (nextPtr >= sz)
  519. return false;
  520. final byte c = raw[nextPtr++];
  521. if (' ' == c)
  522. break;
  523. nextMode <<= 3;
  524. nextMode += c - '0';
  525. }
  526. final int nextNamePos = nextPtr;
  527. for (;;) {
  528. if (nextPtr == sz)
  529. return false;
  530. final byte c = raw[nextPtr++];
  531. if (c == 0)
  532. break;
  533. }
  534. if (nextNamePos + 1 == nextPtr)
  535. return false;
  536. int cmp = compareSameName(
  537. raw, thisNamePos, thisNameEnd,
  538. raw, nextNamePos, nextPtr - 1, nextMode);
  539. if (cmp < 0)
  540. return false;
  541. else if (cmp == 0)
  542. return true;
  543. nextPtr += Constants.OBJECT_ID_LENGTH;
  544. }
  545. }
  546. /**
  547. * Check a canonical formatted tree for errors.
  548. *
  549. * @param raw
  550. * the raw tree data. The array is never modified.
  551. * @throws CorruptObjectException
  552. * if any error was detected.
  553. */
  554. public void checkTree(byte[] raw) throws CorruptObjectException {
  555. checkTree(idFor(OBJ_TREE, raw), raw);
  556. }
  557. /**
  558. * Check a canonical formatted tree for errors.
  559. *
  560. * @param id
  561. * identity of the object being checked.
  562. * @param raw
  563. * the raw tree data. The array is never modified.
  564. * @throws CorruptObjectException
  565. * if any error was detected.
  566. * @since 4.2
  567. */
  568. public void checkTree(@Nullable AnyObjectId id, byte[] raw)
  569. throws CorruptObjectException {
  570. final int sz = raw.length;
  571. int ptr = 0;
  572. int lastNameB = 0, lastNameE = 0, lastMode = 0;
  573. Set<String> normalized = windows || macosx
  574. ? new HashSet<>()
  575. : null;
  576. while (ptr < sz) {
  577. int thisMode = 0;
  578. for (;;) {
  579. if (ptr == sz) {
  580. throw new CorruptObjectException(
  581. JGitText.get().corruptObjectTruncatedInMode);
  582. }
  583. final byte c = raw[ptr++];
  584. if (' ' == c)
  585. break;
  586. if (c < '0' || c > '7') {
  587. throw new CorruptObjectException(
  588. JGitText.get().corruptObjectInvalidModeChar);
  589. }
  590. if (thisMode == 0 && c == '0') {
  591. report(ZERO_PADDED_FILEMODE, id,
  592. JGitText.get().corruptObjectInvalidModeStartsZero);
  593. }
  594. thisMode <<= 3;
  595. thisMode += c - '0';
  596. }
  597. if (FileMode.fromBits(thisMode).getObjectType() == OBJ_BAD) {
  598. throw new CorruptObjectException(MessageFormat.format(
  599. JGitText.get().corruptObjectInvalidMode2,
  600. Integer.valueOf(thisMode)));
  601. }
  602. final int thisNameB = ptr;
  603. ptr = scanPathSegment(raw, ptr, sz, id);
  604. if (ptr == sz || raw[ptr] != 0) {
  605. throw new CorruptObjectException(
  606. JGitText.get().corruptObjectTruncatedInName);
  607. }
  608. checkPathSegment2(raw, thisNameB, ptr, id);
  609. if (normalized != null) {
  610. if (!normalized.add(normalize(raw, thisNameB, ptr))) {
  611. report(DUPLICATE_ENTRIES, id,
  612. JGitText.get().corruptObjectDuplicateEntryNames);
  613. }
  614. } else if (duplicateName(raw, thisNameB, ptr)) {
  615. report(DUPLICATE_ENTRIES, id,
  616. JGitText.get().corruptObjectDuplicateEntryNames);
  617. }
  618. if (lastNameB != 0) {
  619. int cmp = compare(
  620. raw, lastNameB, lastNameE, lastMode,
  621. raw, thisNameB, ptr, thisMode);
  622. if (cmp > 0) {
  623. report(TREE_NOT_SORTED, id,
  624. JGitText.get().corruptObjectIncorrectSorting);
  625. }
  626. }
  627. lastNameB = thisNameB;
  628. lastNameE = ptr;
  629. lastMode = thisMode;
  630. ptr += 1 + OBJECT_ID_LENGTH;
  631. if (ptr > sz) {
  632. throw new CorruptObjectException(
  633. JGitText.get().corruptObjectTruncatedInObjectId);
  634. }
  635. if (ObjectId.zeroId().compareTo(raw, ptr - OBJECT_ID_LENGTH) == 0) {
  636. report(NULL_SHA1, id, JGitText.get().corruptObjectZeroId);
  637. }
  638. if (id != null && isGitmodules(raw, lastNameB, lastNameE, id)) {
  639. ObjectId blob = ObjectId.fromRaw(raw, ptr - OBJECT_ID_LENGTH);
  640. gitsubmodules.add(new GitmoduleEntry(id, blob));
  641. }
  642. }
  643. }
  644. private int scanPathSegment(byte[] raw, int ptr, int end,
  645. @Nullable AnyObjectId id) throws CorruptObjectException {
  646. for (; ptr < end; ptr++) {
  647. byte c = raw[ptr];
  648. if (c == 0) {
  649. return ptr;
  650. }
  651. if (c == '/') {
  652. report(FULL_PATHNAME, id,
  653. JGitText.get().corruptObjectNameContainsSlash);
  654. }
  655. if (windows && isInvalidOnWindows(c)) {
  656. if (c > 31) {
  657. throw new CorruptObjectException(String.format(
  658. JGitText.get().corruptObjectNameContainsChar,
  659. Byte.valueOf(c)));
  660. }
  661. throw new CorruptObjectException(String.format(
  662. JGitText.get().corruptObjectNameContainsByte,
  663. Integer.valueOf(c & 0xff)));
  664. }
  665. }
  666. return ptr;
  667. }
  668. @Nullable
  669. private ObjectId idFor(int objType, byte[] raw) {
  670. if (skipList != null) {
  671. try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
  672. return fmt.idFor(objType, raw);
  673. }
  674. }
  675. return null;
  676. }
  677. private void report(@NonNull ErrorType err, @Nullable AnyObjectId id,
  678. String why) throws CorruptObjectException {
  679. if (errors.contains(err)
  680. && (id == null || skipList == null || !skipList.contains(id))) {
  681. if (id != null) {
  682. throw new CorruptObjectException(err, id, why);
  683. }
  684. throw new CorruptObjectException(why);
  685. }
  686. }
  687. /**
  688. * Check tree path entry for validity.
  689. * <p>
  690. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  691. * scans a multi-directory path string such as {@code "src/main.c"}.
  692. *
  693. * @param path path string to scan.
  694. * @throws CorruptObjectException path is invalid.
  695. * @since 3.6
  696. */
  697. public void checkPath(String path) throws CorruptObjectException {
  698. byte[] buf = Constants.encode(path);
  699. checkPath(buf, 0, buf.length);
  700. }
  701. /**
  702. * Check tree path entry for validity.
  703. * <p>
  704. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version
  705. * scans a multi-directory path string such as {@code "src/main.c"}.
  706. *
  707. * @param raw buffer to scan.
  708. * @param ptr offset to first byte of the name.
  709. * @param end offset to one past last byte of name.
  710. * @throws CorruptObjectException path is invalid.
  711. * @since 3.6
  712. */
  713. public void checkPath(byte[] raw, int ptr, int end)
  714. throws CorruptObjectException {
  715. int start = ptr;
  716. for (; ptr < end; ptr++) {
  717. if (raw[ptr] == '/') {
  718. checkPathSegment(raw, start, ptr);
  719. start = ptr + 1;
  720. }
  721. }
  722. checkPathSegment(raw, start, end);
  723. }
  724. /**
  725. * Check tree path entry for validity.
  726. *
  727. * @param raw buffer to scan.
  728. * @param ptr offset to first byte of the name.
  729. * @param end offset to one past last byte of name.
  730. * @throws CorruptObjectException name is invalid.
  731. * @since 3.4
  732. */
  733. public void checkPathSegment(byte[] raw, int ptr, int end)
  734. throws CorruptObjectException {
  735. int e = scanPathSegment(raw, ptr, end, null);
  736. if (e < end && raw[e] == 0)
  737. throw new CorruptObjectException(
  738. JGitText.get().corruptObjectNameContainsNullByte);
  739. checkPathSegment2(raw, ptr, end, null);
  740. }
  741. private void checkPathSegment2(byte[] raw, int ptr, int end,
  742. @Nullable AnyObjectId id) throws CorruptObjectException {
  743. if (ptr == end) {
  744. report(EMPTY_NAME, id, JGitText.get().corruptObjectNameZeroLength);
  745. return;
  746. }
  747. if (raw[ptr] == '.') {
  748. switch (end - ptr) {
  749. case 1:
  750. report(HAS_DOT, id, JGitText.get().corruptObjectNameDot);
  751. break;
  752. case 2:
  753. if (raw[ptr + 1] == '.') {
  754. report(HAS_DOTDOT, id,
  755. JGitText.get().corruptObjectNameDotDot);
  756. }
  757. break;
  758. case 4:
  759. if (isGit(raw, ptr + 1)) {
  760. report(HAS_DOTGIT, id, String.format(
  761. JGitText.get().corruptObjectInvalidName,
  762. RawParseUtils.decode(raw, ptr, end)));
  763. }
  764. break;
  765. default:
  766. if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end)) {
  767. report(HAS_DOTGIT, id, String.format(
  768. JGitText.get().corruptObjectInvalidName,
  769. RawParseUtils.decode(raw, ptr, end)));
  770. }
  771. }
  772. } else if (isGitTilde1(raw, ptr, end)) {
  773. report(HAS_DOTGIT, id, String.format(
  774. JGitText.get().corruptObjectInvalidName,
  775. RawParseUtils.decode(raw, ptr, end)));
  776. }
  777. if (macosx && isMacHFSGit(raw, ptr, end, id)) {
  778. report(HAS_DOTGIT, id, String.format(
  779. JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
  780. RawParseUtils.decode(raw, ptr, end)));
  781. }
  782. if (windows) {
  783. // Windows ignores space and dot at end of file name.
  784. if (raw[end - 1] == ' ' || raw[end - 1] == '.') {
  785. report(WIN32_BAD_NAME, id, String.format(
  786. JGitText.get().corruptObjectInvalidNameEnd,
  787. Character.valueOf(((char) raw[end - 1]))));
  788. }
  789. if (end - ptr >= 3) {
  790. checkNotWindowsDevice(raw, ptr, end, id);
  791. }
  792. }
  793. }
  794. // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
  795. // to ".git" therefore we should prevent such names
  796. private boolean isMacHFSPath(byte[] raw, int ptr, int end, byte[] path,
  797. @Nullable AnyObjectId id) throws CorruptObjectException {
  798. boolean ignorable = false;
  799. int g = 0;
  800. while (ptr < end) {
  801. switch (raw[ptr]) {
  802. case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
  803. if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
  804. return false;
  805. }
  806. switch (raw[ptr + 1]) {
  807. case (byte) 0x80:
  808. switch (raw[ptr + 2]) {
  809. case (byte) 0x8c: // U+200C 0xe2808c ZERO WIDTH NON-JOINER
  810. case (byte) 0x8d: // U+200D 0xe2808d ZERO WIDTH JOINER
  811. case (byte) 0x8e: // U+200E 0xe2808e LEFT-TO-RIGHT MARK
  812. case (byte) 0x8f: // U+200F 0xe2808f RIGHT-TO-LEFT MARK
  813. case (byte) 0xaa: // U+202A 0xe280aa LEFT-TO-RIGHT EMBEDDING
  814. case (byte) 0xab: // U+202B 0xe280ab RIGHT-TO-LEFT EMBEDDING
  815. case (byte) 0xac: // U+202C 0xe280ac POP DIRECTIONAL FORMATTING
  816. case (byte) 0xad: // U+202D 0xe280ad LEFT-TO-RIGHT OVERRIDE
  817. case (byte) 0xae: // U+202E 0xe280ae RIGHT-TO-LEFT OVERRIDE
  818. ignorable = true;
  819. ptr += 3;
  820. continue;
  821. default:
  822. return false;
  823. }
  824. case (byte) 0x81:
  825. switch (raw[ptr + 2]) {
  826. case (byte) 0xaa: // U+206A 0xe281aa INHIBIT SYMMETRIC SWAPPING
  827. case (byte) 0xab: // U+206B 0xe281ab ACTIVATE SYMMETRIC SWAPPING
  828. case (byte) 0xac: // U+206C 0xe281ac INHIBIT ARABIC FORM SHAPING
  829. case (byte) 0xad: // U+206D 0xe281ad ACTIVATE ARABIC FORM SHAPING
  830. case (byte) 0xae: // U+206E 0xe281ae NATIONAL DIGIT SHAPES
  831. case (byte) 0xaf: // U+206F 0xe281af NOMINAL DIGIT SHAPES
  832. ignorable = true;
  833. ptr += 3;
  834. continue;
  835. default:
  836. return false;
  837. }
  838. default:
  839. return false;
  840. }
  841. case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024
  842. if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
  843. return false;
  844. }
  845. // U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE
  846. if ((raw[ptr + 1] == (byte) 0xbb)
  847. && (raw[ptr + 2] == (byte) 0xbf)) {
  848. ignorable = true;
  849. ptr += 3;
  850. continue;
  851. }
  852. return false;
  853. default:
  854. if (g == path.length) {
  855. return false;
  856. }
  857. if (toLower(raw[ptr++]) != path[g++]) {
  858. return false;
  859. }
  860. }
  861. }
  862. if (g == path.length && ignorable) {
  863. return true;
  864. }
  865. return false;
  866. }
  867. private boolean isMacHFSGit(byte[] raw, int ptr, int end,
  868. @Nullable AnyObjectId id) throws CorruptObjectException {
  869. byte[] git = new byte[] { '.', 'g', 'i', 't' };
  870. return isMacHFSPath(raw, ptr, end, git, id);
  871. }
  872. private boolean isMacHFSGitmodules(byte[] raw, int ptr, int end,
  873. @Nullable AnyObjectId id) throws CorruptObjectException {
  874. return isMacHFSPath(raw, ptr, end, dotGitmodules, id);
  875. }
  876. private boolean checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end,
  877. @Nullable AnyObjectId id) throws CorruptObjectException {
  878. if ((ptr + 2) >= end) {
  879. report(BAD_UTF8, id, MessageFormat.format(
  880. JGitText.get().corruptObjectInvalidNameInvalidUtf8,
  881. toHexString(raw, ptr, end)));
  882. return false;
  883. }
  884. return true;
  885. }
  886. private static String toHexString(byte[] raw, int ptr, int end) {
  887. StringBuilder b = new StringBuilder("0x"); //$NON-NLS-1$
  888. for (int i = ptr; i < end; i++)
  889. b.append(String.format("%02x", Byte.valueOf(raw[i]))); //$NON-NLS-1$
  890. return b.toString();
  891. }
  892. private void checkNotWindowsDevice(byte[] raw, int ptr, int end,
  893. @Nullable AnyObjectId id) throws CorruptObjectException {
  894. switch (toLower(raw[ptr])) {
  895. case 'a': // AUX
  896. if (end - ptr >= 3
  897. && toLower(raw[ptr + 1]) == 'u'
  898. && toLower(raw[ptr + 2]) == 'x'
  899. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  900. report(WIN32_BAD_NAME, id,
  901. JGitText.get().corruptObjectInvalidNameAux);
  902. }
  903. break;
  904. case 'c': // CON, COM[1-9]
  905. if (end - ptr >= 3
  906. && toLower(raw[ptr + 2]) == 'n'
  907. && toLower(raw[ptr + 1]) == 'o'
  908. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  909. report(WIN32_BAD_NAME, id,
  910. JGitText.get().corruptObjectInvalidNameCon);
  911. }
  912. if (end - ptr >= 4
  913. && toLower(raw[ptr + 2]) == 'm'
  914. && toLower(raw[ptr + 1]) == 'o'
  915. && isPositiveDigit(raw[ptr + 3])
  916. && (end - ptr == 4 || raw[ptr + 4] == '.')) {
  917. report(WIN32_BAD_NAME, id, String.format(
  918. JGitText.get().corruptObjectInvalidNameCom,
  919. Character.valueOf(((char) raw[ptr + 3]))));
  920. }
  921. break;
  922. case 'l': // LPT[1-9]
  923. if (end - ptr >= 4
  924. && toLower(raw[ptr + 1]) == 'p'
  925. && toLower(raw[ptr + 2]) == 't'
  926. && isPositiveDigit(raw[ptr + 3])
  927. && (end - ptr == 4 || raw[ptr + 4] == '.')) {
  928. report(WIN32_BAD_NAME, id, String.format(
  929. JGitText.get().corruptObjectInvalidNameLpt,
  930. Character.valueOf(((char) raw[ptr + 3]))));
  931. }
  932. break;
  933. case 'n': // NUL
  934. if (end - ptr >= 3
  935. && toLower(raw[ptr + 1]) == 'u'
  936. && toLower(raw[ptr + 2]) == 'l'
  937. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  938. report(WIN32_BAD_NAME, id,
  939. JGitText.get().corruptObjectInvalidNameNul);
  940. }
  941. break;
  942. case 'p': // PRN
  943. if (end - ptr >= 3
  944. && toLower(raw[ptr + 1]) == 'r'
  945. && toLower(raw[ptr + 2]) == 'n'
  946. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  947. report(WIN32_BAD_NAME, id,
  948. JGitText.get().corruptObjectInvalidNamePrn);
  949. }
  950. break;
  951. }
  952. }
  953. private static boolean isInvalidOnWindows(byte c) {
  954. // Windows disallows "special" characters in a path component.
  955. switch (c) {
  956. case '"':
  957. case '*':
  958. case ':':
  959. case '<':
  960. case '>':
  961. case '?':
  962. case '\\':
  963. case '|':
  964. return true;
  965. }
  966. return 1 <= c && c <= 31;
  967. }
  968. private static boolean isGit(byte[] buf, int p) {
  969. return toLower(buf[p]) == 'g'
  970. && toLower(buf[p + 1]) == 'i'
  971. && toLower(buf[p + 2]) == 't';
  972. }
  973. /**
  974. * Check if the filename contained in buf[start:end] could be read as a
  975. * .gitmodules file when checked out to the working directory.
  976. *
  977. * This ought to be a simple comparison, but some filesystems have peculiar
  978. * rules for normalizing filenames:
  979. *
  980. * NTFS has backward-compatibility support for 8.3 synonyms of long file
  981. * names (see
  982. * https://web.archive.org/web/20160318181041/https://usn.pw/blog/gen/2015/06/09/filenames/
  983. * for details). NTFS is also case-insensitive.
  984. *
  985. * MacOS's HFS+ folds away ignorable Unicode characters in addition to case
  986. * folding.
  987. *
  988. * @param buf
  989. * byte array to decode
  990. * @param start
  991. * position where a supposed filename is starting
  992. * @param end
  993. * position where a supposed filename is ending
  994. * @param id
  995. * object id for error reporting
  996. *
  997. * @return true if the filename in buf could be a ".gitmodules" file
  998. * @throws CorruptObjectException
  999. */
  1000. private boolean isGitmodules(byte[] buf, int start, int end, @Nullable AnyObjectId id)
  1001. throws CorruptObjectException {
  1002. // Simple cases first.
  1003. if (end - start < 8) {
  1004. return false;
  1005. }
  1006. return (end - start == dotGitmodules.length
  1007. && RawParseUtils.match(buf, start, dotGitmodules) != -1)
  1008. || (macosx && isMacHFSGitmodules(buf, start, end, id))
  1009. || (windows && isNTFSGitmodules(buf, start, end));
  1010. }
  1011. private boolean matchLowerCase(byte[] b, int ptr, byte[] src) {
  1012. if (ptr + src.length > b.length) {
  1013. return false;
  1014. }
  1015. for (int i = 0; i < src.length; i++, ptr++) {
  1016. if (toLower(b[ptr]) != src[i]) {
  1017. return false;
  1018. }
  1019. }
  1020. return true;
  1021. }
  1022. // .gitmodules, case-insensitive, or an 8.3 abbreviation of the same.
  1023. private boolean isNTFSGitmodules(byte[] buf, int start, int end) {
  1024. if (end - start == 11) {
  1025. return matchLowerCase(buf, start, dotGitmodules);
  1026. }
  1027. if (end - start != 8) {
  1028. return false;
  1029. }
  1030. // "gitmod" or a prefix of "gi7eba", followed by...
  1031. byte[] gitmod = new byte[]{'g', 'i', 't', 'm', 'o', 'd', '~'};
  1032. if (matchLowerCase(buf, start, gitmod)) {
  1033. start += 6;
  1034. } else {
  1035. byte[] gi7eba = new byte[]{'g', 'i', '7', 'e', 'b', 'a'};
  1036. for (int i = 0; i < gi7eba.length; i++, start++) {
  1037. byte c = (byte) toLower(buf[start]);
  1038. if (c == '~') {
  1039. break;
  1040. }
  1041. if (c != gi7eba[i]) {
  1042. return false;
  1043. }
  1044. }
  1045. }
  1046. // ... ~ and a number
  1047. if (end - start < 2) {
  1048. return false;
  1049. }
  1050. if (buf[start] != '~') {
  1051. return false;
  1052. }
  1053. start++;
  1054. if (buf[start] < '1' || buf[start] > '9') {
  1055. return false;
  1056. }
  1057. start++;
  1058. for (; start != end; start++) {
  1059. if (buf[start] < '0' || buf[start] > '9') {
  1060. return false;
  1061. }
  1062. }
  1063. return true;
  1064. }
  1065. private static boolean isGitTilde1(byte[] buf, int p, int end) {
  1066. if (end - p != 5)
  1067. return false;
  1068. return toLower(buf[p]) == 'g' && toLower(buf[p + 1]) == 'i'
  1069. && toLower(buf[p + 2]) == 't' && buf[p + 3] == '~'
  1070. && buf[p + 4] == '1';
  1071. }
  1072. private static boolean isNormalizedGit(byte[] raw, int ptr, int end) {
  1073. if (isGit(raw, ptr)) {
  1074. int dots = 0;
  1075. boolean space = false;
  1076. int p = end - 1;
  1077. for (; (ptr + 2) < p; p--) {
  1078. if (raw[p] == '.')
  1079. dots++;
  1080. else if (raw[p] == ' ')
  1081. space = true;
  1082. else
  1083. break;
  1084. }
  1085. return p == ptr + 2 && (dots == 1 || space);
  1086. }
  1087. return false;
  1088. }
  1089. private boolean match(byte[] b, byte[] src) {
  1090. int r = RawParseUtils.match(b, bufPtr.value, src);
  1091. if (r < 0) {
  1092. return false;
  1093. }
  1094. bufPtr.value = r;
  1095. return true;
  1096. }
  1097. private static char toLower(byte b) {
  1098. if ('A' <= b && b <= 'Z')
  1099. return (char) (b + ('a' - 'A'));
  1100. return (char) b;
  1101. }
  1102. private static boolean isPositiveDigit(byte b) {
  1103. return '1' <= b && b <= '9';
  1104. }
  1105. /**
  1106. * Create a new {@link BlobObjectChecker}.
  1107. *
  1108. * @return new BlobObjectChecker or null if it's not provided.
  1109. * @since 4.9
  1110. */
  1111. @Nullable
  1112. public BlobObjectChecker newBlobObjectChecker() {
  1113. return null;
  1114. }
  1115. /**
  1116. * Check a blob for errors.
  1117. *
  1118. * <p>This may not be called from PackParser in some cases. Use {@link
  1119. * #newBlobObjectChecker} instead.
  1120. *
  1121. * @param raw
  1122. * the blob data. The array is never modified.
  1123. * @throws CorruptObjectException
  1124. * if any error was detected.
  1125. */
  1126. public void checkBlob(final byte[] raw) throws CorruptObjectException {
  1127. // We can always assume the blob is valid.
  1128. }
  1129. private String normalize(byte[] raw, int ptr, int end) {
  1130. String n = RawParseUtils.decode(raw, ptr, end).toLowerCase(Locale.US);
  1131. return macosx ? Normalizer.normalize(n, Normalizer.Form.NFC) : n;
  1132. }
  1133. /**
  1134. * Get the list of".gitmodules" files found in the pack. For each, report
  1135. * its blob id (e.g. to validate its contents) and the tree where it was
  1136. * found (e.g. to check if it is in the root)
  1137. *
  1138. * @return List of pairs of ids <tree, blob>
  1139. *
  1140. * @since 4.7.5
  1141. */
  1142. public List<GitmoduleEntry> getGitsubmodules() {
  1143. return gitsubmodules;
  1144. }
  1145. }