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

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