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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 org.eclipse.jgit.lib.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.lib.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 org.eclipse.jgit.errors.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 scans a
  691. * multi-directory path string such as {@code "src/main.c"}.
  692. *
  693. * @param path
  694. * path string to scan.
  695. * @throws org.eclipse.jgit.errors.CorruptObjectException
  696. * path is invalid.
  697. * @since 3.6
  698. */
  699. public void checkPath(String path) throws CorruptObjectException {
  700. byte[] buf = Constants.encode(path);
  701. checkPath(buf, 0, buf.length);
  702. }
  703. /**
  704. * Check tree path entry for validity.
  705. * <p>
  706. * Unlike {@link #checkPathSegment(byte[], int, int)}, this version scans a
  707. * multi-directory path string such as {@code "src/main.c"}.
  708. *
  709. * @param raw
  710. * buffer to scan.
  711. * @param ptr
  712. * offset to first byte of the name.
  713. * @param end
  714. * offset to one past last byte of name.
  715. * @throws org.eclipse.jgit.errors.CorruptObjectException
  716. * path is invalid.
  717. * @since 3.6
  718. */
  719. public void checkPath(byte[] raw, int ptr, int end)
  720. throws CorruptObjectException {
  721. int start = ptr;
  722. for (; ptr < end; ptr++) {
  723. if (raw[ptr] == '/') {
  724. checkPathSegment(raw, start, ptr);
  725. start = ptr + 1;
  726. }
  727. }
  728. checkPathSegment(raw, start, end);
  729. }
  730. /**
  731. * Check tree path entry for validity.
  732. *
  733. * @param raw
  734. * buffer to scan.
  735. * @param ptr
  736. * offset to first byte of the name.
  737. * @param end
  738. * offset to one past last byte of name.
  739. * @throws org.eclipse.jgit.errors.CorruptObjectException
  740. * name is invalid.
  741. * @since 3.4
  742. */
  743. public void checkPathSegment(byte[] raw, int ptr, int end)
  744. throws CorruptObjectException {
  745. int e = scanPathSegment(raw, ptr, end, null);
  746. if (e < end && raw[e] == 0)
  747. throw new CorruptObjectException(
  748. JGitText.get().corruptObjectNameContainsNullByte);
  749. checkPathSegment2(raw, ptr, end, null);
  750. }
  751. private void checkPathSegment2(byte[] raw, int ptr, int end,
  752. @Nullable AnyObjectId id) throws CorruptObjectException {
  753. if (ptr == end) {
  754. report(EMPTY_NAME, id, JGitText.get().corruptObjectNameZeroLength);
  755. return;
  756. }
  757. if (raw[ptr] == '.') {
  758. switch (end - ptr) {
  759. case 1:
  760. report(HAS_DOT, id, JGitText.get().corruptObjectNameDot);
  761. break;
  762. case 2:
  763. if (raw[ptr + 1] == '.') {
  764. report(HAS_DOTDOT, id,
  765. JGitText.get().corruptObjectNameDotDot);
  766. }
  767. break;
  768. case 4:
  769. if (isGit(raw, ptr + 1)) {
  770. report(HAS_DOTGIT, id, String.format(
  771. JGitText.get().corruptObjectInvalidName,
  772. RawParseUtils.decode(raw, ptr, end)));
  773. }
  774. break;
  775. default:
  776. if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end)) {
  777. report(HAS_DOTGIT, id, String.format(
  778. JGitText.get().corruptObjectInvalidName,
  779. RawParseUtils.decode(raw, ptr, end)));
  780. }
  781. }
  782. } else if (isGitTilde1(raw, ptr, end)) {
  783. report(HAS_DOTGIT, id, String.format(
  784. JGitText.get().corruptObjectInvalidName,
  785. RawParseUtils.decode(raw, ptr, end)));
  786. }
  787. if (macosx && isMacHFSGit(raw, ptr, end, id)) {
  788. report(HAS_DOTGIT, id, String.format(
  789. JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
  790. RawParseUtils.decode(raw, ptr, end)));
  791. }
  792. if (windows) {
  793. // Windows ignores space and dot at end of file name.
  794. if (raw[end - 1] == ' ' || raw[end - 1] == '.') {
  795. report(WIN32_BAD_NAME, id, String.format(
  796. JGitText.get().corruptObjectInvalidNameEnd,
  797. Character.valueOf(((char) raw[end - 1]))));
  798. }
  799. if (end - ptr >= 3) {
  800. checkNotWindowsDevice(raw, ptr, end, id);
  801. }
  802. }
  803. }
  804. // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
  805. // to ".git" therefore we should prevent such names
  806. private boolean isMacHFSPath(byte[] raw, int ptr, int end, byte[] path,
  807. @Nullable AnyObjectId id) throws CorruptObjectException {
  808. boolean ignorable = false;
  809. int g = 0;
  810. while (ptr < end) {
  811. switch (raw[ptr]) {
  812. case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
  813. if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
  814. return false;
  815. }
  816. switch (raw[ptr + 1]) {
  817. case (byte) 0x80:
  818. switch (raw[ptr + 2]) {
  819. case (byte) 0x8c: // U+200C 0xe2808c ZERO WIDTH NON-JOINER
  820. case (byte) 0x8d: // U+200D 0xe2808d ZERO WIDTH JOINER
  821. case (byte) 0x8e: // U+200E 0xe2808e LEFT-TO-RIGHT MARK
  822. case (byte) 0x8f: // U+200F 0xe2808f RIGHT-TO-LEFT MARK
  823. case (byte) 0xaa: // U+202A 0xe280aa LEFT-TO-RIGHT EMBEDDING
  824. case (byte) 0xab: // U+202B 0xe280ab RIGHT-TO-LEFT EMBEDDING
  825. case (byte) 0xac: // U+202C 0xe280ac POP DIRECTIONAL FORMATTING
  826. case (byte) 0xad: // U+202D 0xe280ad LEFT-TO-RIGHT OVERRIDE
  827. case (byte) 0xae: // U+202E 0xe280ae RIGHT-TO-LEFT OVERRIDE
  828. ignorable = true;
  829. ptr += 3;
  830. continue;
  831. default:
  832. return false;
  833. }
  834. case (byte) 0x81:
  835. switch (raw[ptr + 2]) {
  836. case (byte) 0xaa: // U+206A 0xe281aa INHIBIT SYMMETRIC SWAPPING
  837. case (byte) 0xab: // U+206B 0xe281ab ACTIVATE SYMMETRIC SWAPPING
  838. case (byte) 0xac: // U+206C 0xe281ac INHIBIT ARABIC FORM SHAPING
  839. case (byte) 0xad: // U+206D 0xe281ad ACTIVATE ARABIC FORM SHAPING
  840. case (byte) 0xae: // U+206E 0xe281ae NATIONAL DIGIT SHAPES
  841. case (byte) 0xaf: // U+206F 0xe281af NOMINAL DIGIT SHAPES
  842. ignorable = true;
  843. ptr += 3;
  844. continue;
  845. default:
  846. return false;
  847. }
  848. default:
  849. return false;
  850. }
  851. case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024
  852. if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
  853. return false;
  854. }
  855. // U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE
  856. if ((raw[ptr + 1] == (byte) 0xbb)
  857. && (raw[ptr + 2] == (byte) 0xbf)) {
  858. ignorable = true;
  859. ptr += 3;
  860. continue;
  861. }
  862. return false;
  863. default:
  864. if (g == path.length) {
  865. return false;
  866. }
  867. if (toLower(raw[ptr++]) != path[g++]) {
  868. return false;
  869. }
  870. }
  871. }
  872. if (g == path.length && ignorable) {
  873. return true;
  874. }
  875. return false;
  876. }
  877. private boolean isMacHFSGit(byte[] raw, int ptr, int end,
  878. @Nullable AnyObjectId id) throws CorruptObjectException {
  879. byte[] git = new byte[] { '.', 'g', 'i', 't' };
  880. return isMacHFSPath(raw, ptr, end, git, id);
  881. }
  882. private boolean isMacHFSGitmodules(byte[] raw, int ptr, int end,
  883. @Nullable AnyObjectId id) throws CorruptObjectException {
  884. return isMacHFSPath(raw, ptr, end, dotGitmodules, id);
  885. }
  886. private boolean checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end,
  887. @Nullable AnyObjectId id) throws CorruptObjectException {
  888. if ((ptr + 2) >= end) {
  889. report(BAD_UTF8, id, MessageFormat.format(
  890. JGitText.get().corruptObjectInvalidNameInvalidUtf8,
  891. toHexString(raw, ptr, end)));
  892. return false;
  893. }
  894. return true;
  895. }
  896. private static String toHexString(byte[] raw, int ptr, int end) {
  897. StringBuilder b = new StringBuilder("0x"); //$NON-NLS-1$
  898. for (int i = ptr; i < end; i++)
  899. b.append(String.format("%02x", Byte.valueOf(raw[i]))); //$NON-NLS-1$
  900. return b.toString();
  901. }
  902. private void checkNotWindowsDevice(byte[] raw, int ptr, int end,
  903. @Nullable AnyObjectId id) throws CorruptObjectException {
  904. switch (toLower(raw[ptr])) {
  905. case 'a': // AUX
  906. if (end - ptr >= 3
  907. && toLower(raw[ptr + 1]) == 'u'
  908. && toLower(raw[ptr + 2]) == 'x'
  909. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  910. report(WIN32_BAD_NAME, id,
  911. JGitText.get().corruptObjectInvalidNameAux);
  912. }
  913. break;
  914. case 'c': // CON, COM[1-9]
  915. if (end - ptr >= 3
  916. && toLower(raw[ptr + 2]) == 'n'
  917. && toLower(raw[ptr + 1]) == 'o'
  918. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  919. report(WIN32_BAD_NAME, id,
  920. JGitText.get().corruptObjectInvalidNameCon);
  921. }
  922. if (end - ptr >= 4
  923. && toLower(raw[ptr + 2]) == 'm'
  924. && toLower(raw[ptr + 1]) == 'o'
  925. && isPositiveDigit(raw[ptr + 3])
  926. && (end - ptr == 4 || raw[ptr + 4] == '.')) {
  927. report(WIN32_BAD_NAME, id, String.format(
  928. JGitText.get().corruptObjectInvalidNameCom,
  929. Character.valueOf(((char) raw[ptr + 3]))));
  930. }
  931. break;
  932. case 'l': // LPT[1-9]
  933. if (end - ptr >= 4
  934. && toLower(raw[ptr + 1]) == 'p'
  935. && toLower(raw[ptr + 2]) == 't'
  936. && isPositiveDigit(raw[ptr + 3])
  937. && (end - ptr == 4 || raw[ptr + 4] == '.')) {
  938. report(WIN32_BAD_NAME, id, String.format(
  939. JGitText.get().corruptObjectInvalidNameLpt,
  940. Character.valueOf(((char) raw[ptr + 3]))));
  941. }
  942. break;
  943. case 'n': // NUL
  944. if (end - ptr >= 3
  945. && toLower(raw[ptr + 1]) == 'u'
  946. && toLower(raw[ptr + 2]) == 'l'
  947. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  948. report(WIN32_BAD_NAME, id,
  949. JGitText.get().corruptObjectInvalidNameNul);
  950. }
  951. break;
  952. case 'p': // PRN
  953. if (end - ptr >= 3
  954. && toLower(raw[ptr + 1]) == 'r'
  955. && toLower(raw[ptr + 2]) == 'n'
  956. && (end - ptr == 3 || raw[ptr + 3] == '.')) {
  957. report(WIN32_BAD_NAME, id,
  958. JGitText.get().corruptObjectInvalidNamePrn);
  959. }
  960. break;
  961. }
  962. }
  963. private static boolean isInvalidOnWindows(byte c) {
  964. // Windows disallows "special" characters in a path component.
  965. switch (c) {
  966. case '"':
  967. case '*':
  968. case ':':
  969. case '<':
  970. case '>':
  971. case '?':
  972. case '\\':
  973. case '|':
  974. return true;
  975. }
  976. return 1 <= c && c <= 31;
  977. }
  978. private static boolean isGit(byte[] buf, int p) {
  979. return toLower(buf[p]) == 'g'
  980. && toLower(buf[p + 1]) == 'i'
  981. && toLower(buf[p + 2]) == 't';
  982. }
  983. /**
  984. * Check if the filename contained in buf[start:end] could be read as a
  985. * .gitmodules file when checked out to the working directory.
  986. *
  987. * This ought to be a simple comparison, but some filesystems have peculiar
  988. * rules for normalizing filenames:
  989. *
  990. * NTFS has backward-compatibility support for 8.3 synonyms of long file
  991. * names (see
  992. * https://web.archive.org/web/20160318181041/https://usn.pw/blog/gen/2015/06/09/filenames/
  993. * for details). NTFS is also case-insensitive.
  994. *
  995. * MacOS's HFS+ folds away ignorable Unicode characters in addition to case
  996. * folding.
  997. *
  998. * @param buf
  999. * byte array to decode
  1000. * @param start
  1001. * position where a supposed filename is starting
  1002. * @param end
  1003. * position where a supposed filename is ending
  1004. * @param id
  1005. * object id for error reporting
  1006. *
  1007. * @return true if the filename in buf could be a ".gitmodules" file
  1008. * @throws CorruptObjectException
  1009. */
  1010. private boolean isGitmodules(byte[] buf, int start, int end, @Nullable AnyObjectId id)
  1011. throws CorruptObjectException {
  1012. // Simple cases first.
  1013. if (end - start < 8) {
  1014. return false;
  1015. }
  1016. return (end - start == dotGitmodules.length
  1017. && RawParseUtils.match(buf, start, dotGitmodules) != -1)
  1018. || (macosx && isMacHFSGitmodules(buf, start, end, id))
  1019. || (windows && isNTFSGitmodules(buf, start, end));
  1020. }
  1021. private boolean matchLowerCase(byte[] b, int ptr, byte[] src) {
  1022. if (ptr + src.length > b.length) {
  1023. return false;
  1024. }
  1025. for (int i = 0; i < src.length; i++, ptr++) {
  1026. if (toLower(b[ptr]) != src[i]) {
  1027. return false;
  1028. }
  1029. }
  1030. return true;
  1031. }
  1032. // .gitmodules, case-insensitive, or an 8.3 abbreviation of the same.
  1033. private boolean isNTFSGitmodules(byte[] buf, int start, int end) {
  1034. if (end - start == 11) {
  1035. return matchLowerCase(buf, start, dotGitmodules);
  1036. }
  1037. if (end - start != 8) {
  1038. return false;
  1039. }
  1040. // "gitmod" or a prefix of "gi7eba", followed by...
  1041. byte[] gitmod = new byte[]{'g', 'i', 't', 'm', 'o', 'd', '~'};
  1042. if (matchLowerCase(buf, start, gitmod)) {
  1043. start += 6;
  1044. } else {
  1045. byte[] gi7eba = new byte[]{'g', 'i', '7', 'e', 'b', 'a'};
  1046. for (int i = 0; i < gi7eba.length; i++, start++) {
  1047. byte c = (byte) toLower(buf[start]);
  1048. if (c == '~') {
  1049. break;
  1050. }
  1051. if (c != gi7eba[i]) {
  1052. return false;
  1053. }
  1054. }
  1055. }
  1056. // ... ~ and a number
  1057. if (end - start < 2) {
  1058. return false;
  1059. }
  1060. if (buf[start] != '~') {
  1061. return false;
  1062. }
  1063. start++;
  1064. if (buf[start] < '1' || buf[start] > '9') {
  1065. return false;
  1066. }
  1067. start++;
  1068. for (; start != end; start++) {
  1069. if (buf[start] < '0' || buf[start] > '9') {
  1070. return false;
  1071. }
  1072. }
  1073. return true;
  1074. }
  1075. private static boolean isGitTilde1(byte[] buf, int p, int end) {
  1076. if (end - p != 5)
  1077. return false;
  1078. return toLower(buf[p]) == 'g' && toLower(buf[p + 1]) == 'i'
  1079. && toLower(buf[p + 2]) == 't' && buf[p + 3] == '~'
  1080. && buf[p + 4] == '1';
  1081. }
  1082. private static boolean isNormalizedGit(byte[] raw, int ptr, int end) {
  1083. if (isGit(raw, ptr)) {
  1084. int dots = 0;
  1085. boolean space = false;
  1086. int p = end - 1;
  1087. for (; (ptr + 2) < p; p--) {
  1088. if (raw[p] == '.')
  1089. dots++;
  1090. else if (raw[p] == ' ')
  1091. space = true;
  1092. else
  1093. break;
  1094. }
  1095. return p == ptr + 2 && (dots == 1 || space);
  1096. }
  1097. return false;
  1098. }
  1099. private boolean match(byte[] b, byte[] src) {
  1100. int r = RawParseUtils.match(b, bufPtr.value, src);
  1101. if (r < 0) {
  1102. return false;
  1103. }
  1104. bufPtr.value = r;
  1105. return true;
  1106. }
  1107. private static char toLower(byte b) {
  1108. if ('A' <= b && b <= 'Z')
  1109. return (char) (b + ('a' - 'A'));
  1110. return (char) b;
  1111. }
  1112. private static boolean isPositiveDigit(byte b) {
  1113. return '1' <= b && b <= '9';
  1114. }
  1115. /**
  1116. * Create a new {@link org.eclipse.jgit.lib.BlobObjectChecker}.
  1117. *
  1118. * @return new BlobObjectChecker or null if it's not provided.
  1119. * @since 4.9
  1120. */
  1121. @Nullable
  1122. public BlobObjectChecker newBlobObjectChecker() {
  1123. return null;
  1124. }
  1125. /**
  1126. * Check a blob for errors.
  1127. *
  1128. * <p>
  1129. * This may not be called from PackParser in some cases. Use
  1130. * {@link #newBlobObjectChecker} instead.
  1131. *
  1132. * @param raw
  1133. * the blob data. The array is never modified.
  1134. * @throws org.eclipse.jgit.errors.CorruptObjectException
  1135. * if any error was detected.
  1136. */
  1137. public void checkBlob(byte[] raw) throws CorruptObjectException {
  1138. // We can always assume the blob is valid.
  1139. }
  1140. private String normalize(byte[] raw, int ptr, int end) {
  1141. String n = RawParseUtils.decode(raw, ptr, end).toLowerCase(Locale.US);
  1142. return macosx ? Normalizer.normalize(n, Normalizer.Form.NFC) : n;
  1143. }
  1144. /**
  1145. * Get the list of".gitmodules" files found in the pack. For each, report
  1146. * its blob id (e.g. to validate its contents) and the tree where it was
  1147. * found (e.g. to check if it is in the root)
  1148. *
  1149. * @return List of pairs of ids <tree, blob>
  1150. *
  1151. * @since 4.7.5
  1152. */
  1153. public List<GitmoduleEntry> getGitsubmodules() {
  1154. return gitsubmodules;
  1155. }
  1156. }