Esempio n. 1
0
 /**
  * Creates the version 1 pack bitmap index files.
  *
  * @param dst the output stream to which the index will be written.
  */
 public PackBitmapIndexWriterV1(final OutputStream dst) {
   out =
       new DigestOutputStream(
           dst instanceof BufferedOutputStream ? dst : new SafeBufferedOutputStream(dst),
           Constants.newMessageDigest());
   dataOutput = new SimpleDataOutput(out);
 }
Esempio n. 2
0
  void writeTo(final OutputStream os) throws IOException {
    final MessageDigest foot = Constants.newMessageDigest();
    final DigestOutputStream dos = new DigestOutputStream(os, foot);

    boolean extended = false;
    for (int i = 0; i < entryCnt; i++) extended |= sortedEntries[i].isExtended();

    // Write the header.
    //
    final byte[] tmp = new byte[128];
    System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length);
    NB.encodeInt32(tmp, 4, extended ? 3 : 2);
    NB.encodeInt32(tmp, 8, entryCnt);
    dos.write(tmp, 0, 12);

    // Write the individual file entries.

    final int smudge_s;
    final int smudge_ns;
    if (myLock != null) {
      // For new files we need to smudge the index entry
      // if they have been modified "now". Ideally we'd
      // want the timestamp when we're done writing the index,
      // so we use the current timestamp as a approximation.
      myLock.createCommitSnapshot();
      snapshot = myLock.getCommitSnapshot();
      smudge_s = (int) (snapshot.lastModified() / 1000);
      smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;
    } else {
      // Used in unit tests only
      smudge_ns = 0;
      smudge_s = 0;
    }

    // Check if tree is non-null here since calling updateSmudgedEntries
    // will automatically build it via creating a DirCacheIterator
    final boolean writeTree = tree != null;

    if (repository != null && entryCnt > 0) updateSmudgedEntries();

    for (int i = 0; i < entryCnt; i++) {
      final DirCacheEntry e = sortedEntries[i];
      if (e.mightBeRacilyClean(smudge_s, smudge_ns)) e.smudgeRacilyClean();
      e.write(dos);
    }

    if (writeTree) {
      final TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
      tree.write(tmp, bb);
      bb.close();

      NB.encodeInt32(tmp, 0, EXT_TREE);
      NB.encodeInt32(tmp, 4, (int) bb.length());
      dos.write(tmp, 0, 8);
      bb.writeTo(dos, null);
    }
    writeIndexChecksum = foot.digest();
    os.write(writeIndexChecksum);
    os.close();
  }
Esempio n. 3
0
 private static void assertHash(RevObject id, byte[] bin) {
   MessageDigest md = Constants.newMessageDigest();
   md.update(Constants.encodedTypeString(id.getType()));
   md.update((byte) ' ');
   md.update(Constants.encodeASCII(bin.length));
   md.update((byte) 0);
   md.update(bin);
   assertEquals(id, ObjectId.fromRaw(md.digest()));
 }
Esempio n. 4
0
  private String rand(final HttpServletRequest req, final String suffix)
      throws UnsupportedEncodingException {
    // Produce a random suffix that is difficult (or nearly impossible)
    // for an attacker to guess in advance. This reduces the risk that
    // an attacker could upload a *.class file and have us send a ZIP
    // that can be invoked through an applet tag in the victim's browser.
    //
    final MessageDigest md = Constants.newMessageDigest();
    final byte[] buf = new byte[8];

    NB.encodeInt32(buf, 0, req.getRemotePort());
    md.update(req.getRemoteAddr().getBytes("UTF-8"));
    md.update(buf, 0, 4);

    NB.encodeInt64(buf, 0, System.currentTimeMillis());
    md.update(buf, 0, 8);

    rng.nextBytes(buf);
    md.update(buf, 0, 8);

    return suffix + "-" + ObjectId.fromRaw(md.digest()).name();
  }
Esempio n. 5
0
 private PackedRefList readPackedRefs() throws IOException {
   int maxStaleRetries = 5;
   int retries = 0;
   while (true) {
     final FileSnapshot snapshot = FileSnapshot.save(packedRefsFile);
     final BufferedReader br;
     final MessageDigest digest = Constants.newMessageDigest();
     try {
       br =
           new BufferedReader(
               new InputStreamReader(
                   new DigestInputStream(new FileInputStream(packedRefsFile), digest), CHARSET));
     } catch (FileNotFoundException noPackedRefs) {
       if (packedRefsFile.exists()) {
         throw noPackedRefs;
       }
       // Ignore it and leave the new list empty.
       return PackedRefList.NO_PACKED_REFS;
     }
     try {
       return new PackedRefList(parsePackedRefs(br), snapshot, ObjectId.fromRaw(digest.digest()));
     } catch (IOException e) {
       if (FileUtils.isStaleFileHandle(e) && retries < maxStaleRetries) {
         if (LOG.isDebugEnabled()) {
           LOG.debug(
               MessageFormat.format(
                   JGitText.get().packedRefsHandleIsStale, Integer.valueOf(retries)),
               e);
         }
         retries++;
         continue;
       }
       throw e;
     } finally {
       br.close();
     }
   }
 }
Esempio n. 6
0
 void initializeDigestAndReadBuffer() {
   if (contentDigest == null) {
     contentDigest = Constants.newMessageDigest();
     contentReadBuffer = new byte[BUFFER_SIZE];
   }
 }
Esempio n. 7
0
 private static ObjectId hash(final byte[] rawText) {
   return ObjectId.fromRaw(Constants.newMessageDigest().digest(rawText));
 }
 private static void digest(TemporaryBuffer.Heap buf) throws IOException {
   MessageDigest md = Constants.newMessageDigest();
   md.update(buf.toByteArray());
   buf.write(md.digest());
 }
Esempio n. 9
0
  private void readFrom(final InputStream inStream) throws IOException, CorruptObjectException {
    final BufferedInputStream in = new BufferedInputStream(inStream);
    final MessageDigest md = Constants.newMessageDigest();

    // Read the index header and verify we understand it.
    //
    final byte[] hdr = new byte[20];
    IO.readFully(in, hdr, 0, 12);
    md.update(hdr, 0, 12);
    if (!is_DIRC(hdr)) throw new CorruptObjectException(JGitText.get().notADIRCFile);
    final int ver = NB.decodeInt32(hdr, 4);
    boolean extended = false;
    if (ver == 3) extended = true;
    else if (ver != 2)
      throw new CorruptObjectException(
          MessageFormat.format(JGitText.get().unknownDIRCVersion, Integer.valueOf(ver)));
    entryCnt = NB.decodeInt32(hdr, 8);
    if (entryCnt < 0) throw new CorruptObjectException(JGitText.get().DIRCHasTooManyEntries);

    snapshot = FileSnapshot.save(liveFile);
    int smudge_s = (int) (snapshot.lastModified() / 1000);
    int smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;

    // Load the individual file entries.
    //
    final int infoLength = DirCacheEntry.getMaximumInfoLength(extended);
    final byte[] infos = new byte[infoLength * entryCnt];
    sortedEntries = new DirCacheEntry[entryCnt];

    final MutableInteger infoAt = new MutableInteger();
    for (int i = 0; i < entryCnt; i++)
      sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md, smudge_s, smudge_ns);

    // After the file entries are index extensions, and then a footer.
    //
    for (; ; ) {
      in.mark(21);
      IO.readFully(in, hdr, 0, 20);
      if (in.read() < 0) {
        // No extensions present; the file ended where we expected.
        //
        break;
      }

      in.reset();
      md.update(hdr, 0, 8);
      IO.skipFully(in, 8);

      long sz = NB.decodeUInt32(hdr, 4);
      switch (NB.decodeInt32(hdr, 0)) {
        case EXT_TREE:
          {
            if (Integer.MAX_VALUE < sz) {
              throw new CorruptObjectException(
                  MessageFormat.format(
                      JGitText.get().DIRCExtensionIsTooLargeAt,
                      formatExtensionName(hdr),
                      Long.valueOf(sz)));
            }
            final byte[] raw = new byte[(int) sz];
            IO.readFully(in, raw, 0, raw.length);
            md.update(raw, 0, raw.length);
            tree = new DirCacheTree(raw, new MutableInteger(), null);
            break;
          }
        default:
          if (hdr[0] >= 'A' && hdr[0] <= 'Z') {
            // The extension is optional and is here only as
            // a performance optimization. Since we do not
            // understand it, we can safely skip past it, after
            // we include its data in our checksum.
            //
            skipOptionalExtension(in, md, hdr, sz);
          } else {
            // The extension is not an optimization and is
            // _required_ to understand this index format.
            // Since we did not trap it above we must abort.
            //
            throw new CorruptObjectException(
                MessageFormat.format(
                    JGitText.get().DIRCExtensionNotSupportedByThisVersion,
                    formatExtensionName(hdr)));
          }
      }
    }

    readIndexChecksum = md.digest();
    if (!Arrays.equals(readIndexChecksum, hdr)) {
      throw new CorruptObjectException(JGitText.get().DIRCChecksumMismatch);
    }
  }