You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectChecker.java 36KB

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