CodeSampleX

示例

github.com/klauspost/compress v1.19.1: zlib.NewReaderDict

已验证示例 — golang github.com/klauspost/compress v1.19.1: zlib.NewReaderDict. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:1326ef7c20d3b23ec164dfb0c3faf525dc5a866a4233f1ade67a69163d387404

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-11

案例

HOW
目标
verify github.com/klauspost/compress/zlib.NewReaderDict in pkg:golang/github.com/klauspost/compress@v1.19.1
包
符号
  • github.com/klauspost/compress/zlib.NewReaderDict
环境
go 1.26.6
创建时间
2026-10-11T06:58:16Z

契约

  1. NewReaderDict successfully decompresses data compressed with a matching preset dictionary
  2. NewReaderDict returns ErrDictionary when the compressed data requires a different preset dictionary
  3. NewReaderDict decompresses standard zlib streams without a dictionary when nil dict is provided
  4. NewReaderDict returns ErrHeader when provided invalid zlib header bytes
  5. NewReaderDict returns an io.ReadCloser that implements the Resetter interface for reusing the decompressor

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/klauspost/compress/zlib.NewReaderDict in pkg:golang/github.com/klauspost/compress@v1.19.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.19.1
Demonstrate these symbols/APIs:
  - github.com/klauspost/compress/zlib.NewReaderDict

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:3f04228853406251d569367cd9dc06fd22c66cdd40d95f01076410e99a782e85","contract":["NewReaderDict successfully decompresses data compressed with a matching preset dictionary","NewReaderDict returns ErrDictionary when the compressed data requires a different preset dictionary","NewReaderDict decompresses standard zlib streams without a dictionary when nil dict is provided","NewReaderDict returns ErrHeader when provided invalid zlib header bytes","NewReaderDict returns an io.ReadCloser that implements the Resetter interface for reusing the decompressor"],"goal":"verify github.com/klauspost/compress/zlib.NewReaderDict in pkg:golang/github.com/klauspost/compress@v1.19.1","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"symbols":["github.com/klauspost/compress/zlib.NewReaderDict"]},"contractCommand":["go","run","./test"],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.19.1","symbols":["github.com/klauspost/compress/zlib.NewReaderDict"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/klauspost/compress v1.19.1
go.sum
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/zlib"
)

func main() {
	dict := []byte("shared-zlib-preset-dictionary")
	rawMessage := []byte("Demonstrating zlib decompression with preset dictionary using klauspost/compress")

	var buf bytes.Buffer
	writer, err := zlib.NewWriterLevelDict(&buf, zlib.DefaultCompression, dict)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create zlib writer: %v\n", err)
		os.Exit(1)
	}
	if _, err := writer.Write(rawMessage); err != nil {
		fmt.Fprintf(os.Stderr, "failed to write data: %v\n", err)
		os.Exit(1)
	}
	if err := writer.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "failed to close writer: %v\n", err)
		os.Exit(1)
	}

	reader, err := zlib.NewReaderDict(&buf, dict)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create zlib reader with dict: %v\n", err)
		os.Exit(1)
	}
	defer reader.Close()

	decompressed, err := io.ReadAll(reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to read decompressed data: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Successfully decompressed %d bytes: %s\n", len(decompressed), string(decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/klauspost/compress/zlib.NewReaderDict in pkg:golang/github.com/klauspost/compress@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.19.1"
  ],
  "symbols": [
    "github.com/klauspost/compress/zlib.NewReaderDict"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/zlib"
)

func main() {
	// Assertion 1: NewReaderDict successfully decompresses data compressed with a matching preset dictionary
	{
		dict := []byte("contract-test-dictionary-preset-12345")
		original := []byte("The quick brown fox jumps over the lazy dog. Zlib compression with dictionary verification.")

		var buf bytes.Buffer
		w, err := zlib.NewWriterLevelDict(&buf, zlib.DefaultCompression, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 setup failed: NewWriterLevelDict: %v\n", err)
			os.Exit(1)
		}
		if _, err := w.Write(original); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 setup failed: Write: %v\n", err)
			os.Exit(1)
		}
		if err := w.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 setup failed: Close: %v\n", err)
			os.Exit(1)
		}

		r, err := zlib.NewReaderDict(&buf, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: NewReaderDict returned error: %v\n", err)
			os.Exit(1)
		}

		decompressed, err := io.ReadAll(r)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: io.ReadAll returned error: %v\n", err)
			os.Exit(1)
		}
		if err := r.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: r.Close returned error: %v\n", err)
			os.Exit(1)
		}

		if !bytes.Equal(decompressed, original) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: decompressed data does not match original\n")
			os.Exit(1)
		}
	}

	// Assertion 2: NewReaderDict returns ErrDictionary when the compressed data requires a different preset dictionary
	{
		dictA := []byte("dictionary-alpha-key-spec")
		dictB := []byte("dictionary-beta-different-key")
		payload := []byte("Payload compressed with dictionary alpha.")

		var buf bytes.Buffer
		w, err := zlib.NewWriterLevelDict(&buf, zlib.DefaultCompression, dictA)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 setup failed: NewWriterLevelDict: %v\n", err)
			os.Exit(1)
		}
		if _, err := w.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 setup failed: Write: %v\n", err)
			os.Exit(1)
		}
		if err := w.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 setup failed: Close: %v\n", err)
			os.Exit(1)
		}

		_, err = zlib.NewReaderDict(&buf, dictB)
		if !errors.Is(err, zlib.ErrDictionary) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected ErrDictionary, got: %v\n", err)
			os.Exit(1)
		}
	}

	// Assertion 3: NewReaderDict decompresses standard zlib streams without a dictionary when nil dict is provided
	{
		payload := []byte("Standard zlib stream without preset dictionary.")

		var buf bytes.Buffer
		w := zlib.NewWriter(&buf)
		if _, err := w.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 setup failed: Write: %v\n", err)
			os.Exit(1)
		}
		if err := w.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 setup failed: Close: %v\n", err)
			os.Exit(1)
		}

		r, err := zlib.NewReaderDict(&buf, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: NewReaderDict with nil dict returned error: %v\n", err)
			os.Exit(1)
		}

		decompressed, err := io.ReadAll(r)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: io.ReadAll returned error: %v\n", err)
			os.Exit(1)
		}
		if err := r.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: r.Close returned error: %v\n", err)
			os.Exit(1)
		}

		if !bytes.Equal(decompressed, payload) {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: decompressed data mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 4: NewReaderDict returns ErrHeader when provided invalid zlib header bytes
	{
		invalidHeader := []byte{0x00, 0x00, 0x00, 0x00}
		_, err := zlib.NewReaderDict(bytes.NewReader(invalidHeader), nil)
		if !errors.Is(err, zlib.ErrHeader) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected ErrHeader, got: %v\n", err)
			os.Exit(1)
		}
	}

	// Assertion 5: NewReaderDict returns an io.ReadCloser that implements the Resetter interface for reusing the decompressor
	{
		dict1 := []byte("dictionary-first-run-777")
		dict2 := []byte("dictionary-second-run-888")
		msg1 := []byte("Message number one for reuse test.")
		msg2 := []byte("Message number two for reuse test with different dictionary.")

		var buf1 bytes.Buffer
		w1, _ := zlib.NewWriterLevelDict(&buf1, zlib.DefaultCompression, dict1)
		w1.Write(msg1)
		w1.Close()

		var buf2 bytes.Buffer
		w2, _ := zlib.NewWriterLevelDict(&buf2, zlib.DefaultCompression, dict2)
		w2.Write(msg2)
		w2.Close()

		r, err := zlib.NewReaderDict(&buf1, dict1)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: initial NewReaderDict: %v\n", err)
			os.Exit(1)
		}

		resetter, ok := r.(zlib.Resetter)
		if !ok {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: returned reader does not implement zlib.Resetter\n")
			os.Exit(1)
		}

		out1, err := io.ReadAll(r)
		if err != nil || !bytes.Equal(out1, msg1) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: reading first stream: %v\n", err)
			os.Exit(1)
		}

		if err := resetter.Reset(&buf2, dict2); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: resetter.Reset: %v\n", err)
			os.Exit(1)
		}

		out2, err := io.ReadAll(r)
		if err != nil || !bytes.Equal(out2, msg2) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: reading second stream after reset: %v\n", err)
			os.Exit(1)
		}

		if err := r.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: r.Close: %v\n", err)
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

原始种子者

匿名