Beispiel #1
0
// index returns the array index for the given data.
func (i *InverseBloomFilter) index(data []byte) uint32 {
	hash := i.hashPool.Get().(hash.Hash32)
	hash.Write(data)
	index := hash.Sum32() % uint32(i.capacity)
	hash.Reset()
	i.hashPool.Put(hash)
	return index
}
Beispiel #2
0
// readBlockHeader returns the next block header in the archive.
// It will return io.EOF if there were no bytes read.
func (a *archive15) readBlockHeader() (*blockHeader15, error) {
	var err error
	b := a.buf[:7]
	r := a.v
	if a.encrypted {
		salt := a.buf[:saltSize]
		_, err = io.ReadFull(r, salt)
		if err != nil {
			return nil, err
		}
		key, iv := a.getKeys(salt)
		r = newAesDecryptReader(r, key, iv)
		err = readFull(r, b)
	} else {
		_, err = io.ReadFull(r, b)
	}
	if err != nil {
		return nil, err
	}

	crc := b.uint16()
	hash := crc32.NewIEEE()
	hash.Write(b)
	h := new(blockHeader15)
	h.htype = b.byte()
	h.flags = b.uint16()
	size := b.uint16()
	if size < 7 {
		return nil, errCorruptHeader
	}
	size -= 7
	if int(size) > cap(a.buf) {
		a.buf = readBuf(make([]byte, size))
	}
	h.data = a.buf[:size]
	if err := readFull(r, h.data); err != nil {
		return nil, err
	}
	hash.Write(h.data)
	if crc != uint16(hash.Sum32()) {
		return nil, errBadHeaderCrc
	}
	if h.flags&blockHasData > 0 {
		if len(h.data) < 4 {
			return nil, errCorruptHeader
		}
		h.dataSize = int64(h.data.uint32())
	}
	if h.htype == blockService && h.flags&fileLargeData > 0 {
		if len(h.data) < 25 {
			return nil, errCorruptHeader
		}
		b := h.data[21:25]
		h.dataSize |= int64(b.uint32()) << 32
	}
	return h, nil
}
func (self *BlockStore) LocalChecksum(block BlockID) (string, error) {
	file, err := self.OpenBlock(block)
	if err != nil {
		return "", err
	}
	defer file.Close()

	hash := crc32.NewIEEE()
	if _, err = io.Copy(hash, file); err != nil {
		return "", err
	}
	return fmt.Sprint(hash.Sum32()), nil
}
Beispiel #4
0
func (a *archive50) readBlockHeader() (*blockHeader50, error) {
	r := a.v
	if a.blockKey != nil {
		// block is encrypted
		iv := a.buf[:16]
		if err := readFull(r, iv); err != nil {
			return nil, err
		}
		r = newAesDecryptReader(r, a.blockKey, iv)
	}

	b := a.buf[:minHeaderSize]
	if err := readFull(r, b); err != nil {
		return nil, err
	}
	crc := b.uint32()

	hash := crc32.NewIEEE()
	hash.Write(b)

	size := int(b.uvarint()) // header size
	if size > cap(a.buf) {
		a.buf = readBuf(make([]byte, size))
	} else {
		a.buf = a.buf[:size]
	}
	n := copy(a.buf, b)                            // copy left over bytes
	if err := readFull(r, a.buf[n:]); err != nil { // read rest of header
		return nil, err
	}

	// check header crc
	hash.Write(a.buf[n:])
	if crc != hash.Sum32() {
		return nil, errBadHeaderCrc
	}

	b = a.buf
	h := new(blockHeader50)
	h.htype = b.uvarint()
	h.flags = b.uvarint()

	var extraSize int
	if h.flags&block5HasExtra > 0 {
		extraSize = int(b.uvarint())
	}
	if h.flags&block5HasData > 0 {
		h.dataSize = int64(b.uvarint())
	}
	if len(b) < extraSize {
		return nil, errCorruptHeader
	}
	h.data = b.bytes(len(b) - extraSize)

	// read header extra records
	for len(b) > 0 {
		size = int(b.uvarint())
		if len(b) < size {
			return nil, errCorruptHeader
		}
		data := readBuf(b.bytes(size))
		ftype := data.uvarint()
		h.extra = append(h.extra, extra{ftype, data})
	}

	return h, nil
}