<commit_before>// Copyright (c) 2012, <NAME> <<EMAIL>>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package table

import (
	"encoding/binary"
	"io"

	"code.google.com/p/snappy-go/snappy"

	"github.com/syndtr/goleveldb/leveldb/errors"
	"github.com/syndtr/goleveldb/leveldb/hash"
)

// bInfo holds information about where and how long a block is
type bInfo struct {
	offset, size uint64
}

func (p *bInfo) decodeFrom(b []byte) (int, error) {
	var n, m int
	p.offset, n = binary.Uvarint(b)
	if n > 0 {
		p.size, m = binary.Uvarint(b[n:])
	}
	if n <= 0 || m <= 0 {
		return 0, errors.ErrCorrupt("bad block handle")
	}
	return n + m, nil
}

// Encode encode bInfo, bInfo encoded into varints
func (p *bInfo) encode() []byte {
	b := make([]byte, binary.MaxVarintLen64*2)
	return b[:p.encodeTo(b)]
}

func (p *bInfo) encodeTo(b []byte) int {
	n := binary.PutUvarint(b, p.offset)
	m := binary.PutUvarint(b[n:], p.size)
	return n + m
}

// readAll read entire referenced block.
func (p *bInfo) readAll(r io.ReaderAt, checksum bool) (b []byte, err error) {
	raw := make([]byte, p.size+5)
	_, err = r.ReadAt(raw, int64(p.offset))
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return
	}

	crcb := raw[len(raw)-4:]
	raw = raw[:len(raw)-4]

	if checksum {
		sum := binary.LittleEndian.Uint32(crcb)
		sum = hash.UnmaskCRC32(sum)
		crc := hash.NewCRC32C()
		crc.Write(raw)
		if crc.Sum32() != sum {
			err = errors.ErrCorrupt("block checksum mismatch")
			return
		}
	}

	compression := raw[len(raw)-1]
	b = raw[:len(raw)-1]

	switch compression {
	case kNoCompression:
	case kSnappyCompression:
		return snappy.Decode(nil, b)
	default:
		err = errors.ErrCorrupt("bad block type")
	}

	return
}<commit_msg>Remove naked returns from leveldb/table/block.go.<commit_after>// Copyright (c) 2012, <NAME> <<EMAIL>>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package table

import (
	"encoding/binary"
	"io"

	"code.google.com/p/snappy-go/snappy"

	"github.com/syndtr/goleveldb/leveldb/errors"
	"github.com/syndtr/goleveldb/leveldb/hash"
)

// bInfo holds information about where and how long a block is
type bInfo struct {
	offset, size uint64
}

func (p *bInfo) decodeFrom(b []byte) (int, error) {
	var n, m int
	p.offset, n = binary.Uvarint(b)
	if n > 0 {
		p.size, m = binary.Uvarint(b[n:])
	}
	if n <= 0 || m <= 0 {
		return 0, errors.ErrCorrupt("bad block handle")
	}
	return n + m, nil
}

// Encode encode bInfo, bInfo encoded into varints
func (p *bInfo) encode() []byte {
	b := make([]byte, binary.MaxVarintLen64*2)
	return b[:p.encodeTo(b)]
}

func (p *bInfo) encodeTo(b []byte) int {
	n := binary.PutUvarint(b, p.offset)
	m := binary.PutUvarint(b[n:], p.size)
	return n + m
}

// readAll read entire referenced block.
func (p *bInfo) readAll(r io.ReaderAt, checksum bool) ([]byte, error) {
	raw := make([]byte, p.size+5)
	if _, err := r.ReadAt(raw, int64(p.offset)); err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return nil, err
	}

	crcb := raw[len(raw)-4:]
	raw = raw[:len(raw)-4]

	if checksum {
		sum := binary.LittleEndian.Uint32(crcb)
		sum = hash.UnmaskCRC32(sum)
		crc := hash.NewCRC32C()
		crc.Write(raw)
		if crc.Sum32() != sum {
			return nil, errors.ErrCorrupt("block checksum mismatch")
		}
	}

	compression := raw[len(raw)-1]
	b := raw[:len(raw)-1]

	switch compression {
	case kNoCompression:
	case kSnappyCompression:
		return snappy.Decode(nil, b)
	default:
		return nil, errors.ErrCorrupt("bad block type")
	}

	return b, nil
}
{'fraction_non_alphanumeric': 0.15710382513661203, 'fraction_numerical': 0.015300546448087432, 'mean_word_length': 3.5534825870646767, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673160', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1443, 'n_words': 387}