<commit_before>package client

import (
	"bytes"
	"errors"
	"fmt"
	"strconv"

	ct "github.com/google/certificate-transparency/go"
	"golang.org/x/net/context"
)

// GetRawEntries exposes the /ct/v1/get-entries result with only the JSON parsing done.
func (c *LogClient) GetRawEntries(ctx context.Context, start, end int64) (*ct.GetEntriesResponse, error) {
	if end < 0 {
		return nil, errors.New("end should be >= 0")
	}
	if end < start {
		return nil, errors.New("start should be <= end")
	}

	params := map[string]string{
		"start": strconv.FormatInt(start, 10),
		"end":   strconv.FormatInt(end, 10),
	}
	if ctx == nil {
		ctx = context.TODO()
	}

	var resp ct.GetEntriesResponse
	_, err := c.GetAndParse(ctx, ct.GetEntriesPath, params, &resp)
	if err != nil {
		return nil, err
	}

	return &resp, nil
}

// GetEntries attempts to retrieve the entries in the sequence [|start|, |end|] from the CT log server. (see section 4.6.)
// Returns a slice of LeafInputs or a non-nil error.
func (c *LogClient) GetEntries(start, end int64) ([]ct.LogEntry, error) {
	resp, err := c.GetRawEntries(context.TODO(), start, end)
	if err != nil {
		return nil, err
	}
	entries := make([]ct.LogEntry, len(resp.Entries))
	for index, entry := range resp.Entries {
		leaf, err := ct.ReadMerkleTreeLeaf(bytes.NewBuffer(entry.LeafInput))
		if err != nil {
			return nil, err
		}
		entries[index].Leaf = *leaf

		var chain []ct.ASN1Cert
		switch leaf.TimestampedEntry.EntryType {
		case ct.X509LogEntryType:
			chain, err = ct.UnmarshalX509ChainArray(entry.ExtraData)

		case ct.PrecertLogEntryType:
			chain, err = ct.UnmarshalPrecertChainArray(entry.ExtraData)

		default:
			return nil, fmt.Errorf("saw unknown entry type: %v", leaf.TimestampedEntry.EntryType)
		}
		if err != nil {
			return nil, err
		}
		entries[index].Chain = chain
		entries[index].Index = start + int64(index)<commit_msg>go/client: use tls library to parse entries<commit_after>package client

import (
	"errors"
	"fmt"
	"strconv"

	ct "github.com/google/certificate-transparency/go"
	"github.com/google/certificate-transparency/go/tls"
	"golang.org/x/net/context"
)

// GetRawEntries exposes the /ct/v1/get-entries result with only the JSON parsing done.
func (c *LogClient) GetRawEntries(ctx context.Context, start, end int64) (*ct.GetEntriesResponse, error) {
	if end < 0 {
		return nil, errors.New("end should be >= 0")
	}
	if end < start {
		return nil, errors.New("start should be <= end")
	}

	params := map[string]string{
		"start": strconv.FormatInt(start, 10),
		"end":   strconv.FormatInt(end, 10),
	}
	if ctx == nil {
		ctx = context.TODO()
	}

	var resp ct.GetEntriesResponse
	_, err := c.GetAndParse(ctx, ct.GetEntriesPath, params, &resp)
	if err != nil {
		return nil, err
	}

	return &resp, nil
}

// GetEntries attempts to retrieve the entries in the sequence [|start|, |end|] from the CT log server. (see section 4.6.)
// Returns a slice of LeafInputs or a non-nil error.
func (c *LogClient) GetEntries(start, end int64) ([]ct.LogEntry, error) {
	resp, err := c.GetRawEntries(context.TODO(), start, end)
	if err != nil {
		return nil, err
	}
	entries := make([]ct.LogEntry, len(resp.Entries))
	for index, entry := range resp.Entries {
		var leaf ct.MerkleTreeLeaf
		if rest, err := tls.Unmarshal(entry.LeafInput, &leaf); err != nil {
			return nil, err
		} else if len(rest) > 0 {
			return nil, fmt.Errorf("trailing data (%d bytes) after MerkleTreeLeaf", len(rest))
		}
		entries[index].Leaf = leaf

		var chain []ct.ASN1Cert
		switch leaf.TimestampedEntry.EntryType {
		case ct.X509LogEntryType:
			var certChain ct.CertificateChain
			if rest, err := tls.Unmarshal(entry.ExtraData, &certChain); err != nil {
				return nil, err
			} else if len(rest) > 0 {
				return nil, fmt.Errorf("trailing data (%d bytes) after CertificateChain", len(rest))
			}
			chain = certChain.Entries

		case ct.PrecertLogEntryType:
			var precertChain ct.PrecertChainEntry
			if rest, err := tls.Unmarshal(entry.ExtraData, &precertChain); err != nil {
				return nil, err
			} else if len(rest) > 0 {
				return nil, fmt.Errorf("trailing data (%d bytes) after PrecertChainEntry", len(rest))
			}
			chain = append(chain, precertChain.PreCertificate)
			chain = append(chain, precertChain.CertificateChain...)

		default:
			return nil, fmt.Errorf("saw unknown entry type: %v", leaf.TimestampedEntry.EntryType)
		}
		entries[index].Chain = chain
		entries[index].Index = start + int64(index)
{'fraction_non_alphanumeric': 0.13385826771653545, 'fraction_numerical': 0.009898762654668167, 'mean_word_length': 3.9072847682119205, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 8, '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': '6232794', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1515, 'n_words': 430}