Example #1
0
        public void testDecodeUInt32()
        {
            Assert.AreEqual(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
            Assert.AreEqual(0L, NB.decodeUInt32(Padb(3, 0, 0, 0, 0), 3));

            Assert.AreEqual(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
            Assert.AreEqual(3L, NB.decodeUInt32(Padb(3, 0, 0, 0, 3), 3));

            Assert.AreEqual(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
            Assert.AreEqual(0xdeadbeefL, NB.decodeUInt32(Padb(3, 0xde, 0xad, 0xbe,
                                                              0xef), 3));

            Assert.AreEqual(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
            Assert.AreEqual(0x0310adefL, NB.decodeUInt32(Padb(3, 0x03, 0x10, 0xad,
                                                              0xef), 3));

            Assert.AreEqual(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
            Assert.AreEqual(0xffffffffL, NB.decodeUInt32(Padb(3, 0xff, 0xff, 0xff,
                                                              0xff), 3));
        }
Example #2
0
        private void ReadPackHeader()
        {
            int hdrln = Constants.PACK_SIGNATURE.Length + 4 + 4;
            int p     = FillFromInput(hdrln);

            for (int k = 0; k < Constants.PACK_SIGNATURE.Length; k++)
            {
                if (_buffer[p + k] != Constants.PACK_SIGNATURE[k])
                {
                    throw new IOException("Not a PACK file.");
                }
            }

            long vers = NB.DecodeUInt32(_buffer, p + 4);

            if (vers != 2 && vers != 3)
            {
                throw new IOException("Unsupported pack version " + vers + ".");
            }

            _objectCount = NB.decodeUInt32(_buffer, p + 8);
            Use(hdrln);
        }
Example #3
0
        private void OnOpenPack()
        {
            PackIndex idx = LoadPackIndex();
            var       buf = new byte[20];

            IO.ReadFully(_fd, 0, buf, 0, 12);
            if (RawParseUtils.match(buf, 0, Constants.PACK_SIGNATURE) != 4)
            {
                throw new IOException("Not a PACK file.");
            }

            long vers    = NB.decodeUInt32(buf, 4);
            long packCnt = NB.decodeUInt32(buf, 8);

            if (vers != 2 && vers != 3)
            {
                throw new IOException("Unsupported pack version " + vers + ".");
            }

            if (packCnt != idx.ObjectCount)
            {
                throw new PackMismatchException("Pack object count mismatch:"
                                                + " pack " + packCnt
                                                + " index " + idx.ObjectCount
                                                + ": " + File.FullName);
            }

            IO.ReadFully(_fd, Length - 20, buf, 0, 20);

            if (!buf.SequenceEqual(_packChecksum))
            {
                throw new PackMismatchException("Pack checksum mismatch:"
                                                + " pack " + ObjectId.FromRaw(buf)
                                                + " index " + ObjectId.FromRaw(idx.PackChecksum)
                                                + ": " + File.FullName);
            }
        }
Example #4
0
        public PackIndexV1(Stream fd, byte[] hdr)
        {
            byte[] fanoutTable = new byte[IdxHdrLen];
            Array.Copy(hdr, 0, fanoutTable, 0, hdr.Length);
            IO.ReadFully(fd, fanoutTable, hdr.Length, IdxHdrLen - hdr.Length);

            _idxHeader = new long[256];
            for (int k = 0; k < _idxHeader.Length; k++)
            {
                _idxHeader[k] = NB.decodeUInt32(fanoutTable, k * 4);
            }

            _idxdata = new byte[_idxHeader.Length][];
            for (int k = 0; k < _idxHeader.Length; k++)
            {
                uint n;
                if (k == 0)
                {
                    n = (uint)(_idxHeader[k]);
                }
                else
                {
                    n = (uint)(_idxHeader[k] - _idxHeader[k - 1]);
                }
                if (n > 0)
                {
                    _idxdata[k] = new byte[n * (Constants.OBJECT_ID_LENGTH + 4)];
                    IO.ReadFully(fd, _idxdata[k], 0, _idxdata[k].Length);
                }
            }

            ObjectCount  = _idxHeader[255];
            PackChecksum = new byte[20];
            IO.ReadFully(fd, PackChecksum, 0, PackChecksum.Length);



            /*var fanoutTable = new byte[IDX_HDR_LEN];
             * Array.Copy(hdr, 0, fanoutTable, 0, hdr.Length);
             * NB.ReadFully(fd, fanoutTable, hdr.Length, IDX_HDR_LEN - hdr.Length);
             *
             * idxHeader = new long[256]; // really unsigned 32-bit...
             * for (int k = 0; k < idxHeader.Length; k++)
             *      idxHeader[k] = NB.DecodeUInt32(fanoutTable, k * 4);
             * idxdata = new byte[idxHeader.Length][];
             * for (int k = 0; k < idxHeader.Length; k++)
             * {
             *      _idxHeader[k] = NB.DecodeUInt32(fanoutTable, k * 4);
             * }
             *
             * _idxdata = new byte[_idxHeader.Length][];
             * for (int k = 0; k < _idxHeader.Length; k++)
             * {
             *      int n;
             *      if (k == 0)
             *      {
             *              n = (int)(_idxHeader[k]);
             *      }
             *      else
             *      {
             *              n = (int)(_idxHeader[k] - _idxHeader[k - 1]);
             *      }
             *
             *      if (n <= 0) continue;
             *
             *      _idxdata[k] = new byte[n * (AnyObjectId.ObjectIdLength + 4)];
             *      NB.ReadFully(fd, _idxdata[k], 0, _idxdata[k].Length);
             * }
             *
             * ObjectCount = _idxHeader[255];
             *
             * _packChecksum = new byte[20];
             * NB.ReadFully(fd, _packChecksum, 0, _packChecksum.Length);
             * */
        }
Example #5
0
 internal long ReadUInt32()
 {
     ReadFully(_intbuf, 0, 4);
     return(NB.decodeUInt32(_intbuf, 0));
 }
Example #6
0
        private void ReadFrom(Stream inStream)
        {
            var           @in = new StreamReader(inStream);
            MessageDigest md  = Constants.newMessageDigest();

            // Read the index header and verify we understand it.
            //
            var hdr = new byte[20];

            IO.ReadFully(inStream, hdr, 0, 12);
            md.Update(hdr, 0, 12);
            if (!IsDIRC(hdr))
            {
                throw new CorruptObjectException("Not a DIRC file.");
            }

            int ver = NB.DecodeInt32(hdr, 4);

            if (ver != 2)
            {
                throw new CorruptObjectException("Unknown DIRC version " + ver);
            }

            _entryCnt = NB.DecodeInt32(hdr, 8);
            if (_entryCnt < 0)
            {
                throw new CorruptObjectException("DIRC has too many entries.");
            }

            // Load the individual file entries.
            //
            var infos = new byte[InfoLen * _entryCnt];

            _sortedEntries = new DirCacheEntry[_entryCnt];
            for (int i = 0; i < _entryCnt; i++)
            {
                _sortedEntries[i] = new DirCacheEntry(infos, i * InfoLen, inStream, md);
            }
            _lastModified = _liveFile.lastModified();

            // After the file entries are index extensions, and then a footer.
            //
            while (true)
            {
                var pos = inStream.Position;
                IO.ReadFully(inStream, hdr, 0, 20);

                if (inStream.ReadByte() < 0)
                {
                    // No extensions present; the file ended where we expected.
                    //
                    break;
                }
                inStream.Seek(pos, SeekOrigin.Begin);
                md.Update(hdr, 0, 8);
                IO.skipFully(inStream, 8);

                long sz = NB.decodeUInt32(hdr, 4);

                switch (NB.DecodeInt32(hdr, 0))
                {
                case ExtTree:
                    if (int.MaxValue < sz)
                    {
                        throw new CorruptObjectException("DIRC extension "
                                                         + formatExtensionName(hdr) + " is too large at "
                                                         + sz + " bytes.");
                    }
                    byte[] raw = new byte[(int)sz];
                    IO.ReadFully(inStream, raw, 0, raw.Length);
                    md.Update(raw, 0, raw.Length);
                    _cacheTree = new DirCacheTree(raw, new MutableInteger(), null);
                    break;

                default:
                    if (hdr[0] >= (byte)'A' && hdr[0] <= (byte)'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(inStream, 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("DIRC extension "
                                                         + formatExtensionName(hdr)
                                                         + " not supported by this version.");
                    }

                    break;
                }
            }

            byte[] exp = md.Digest();
            if (!exp.SequenceEqual(hdr))
            {
                throw new CorruptObjectException("DIRC checksum mismatch");
            }
        }