<commit_before>package encoding

import (
	"bytes"
	"encoding/base32"
	"encoding/base64"
	"encoding/gob"
	"encoding/json"
	"encoding/xml"
	"runtime"
	fuzz "github.com/AdaLogics/go-fuzz-headers"
)

func FuzzEncoding(data []byte) int {
	f := fuzz.NewConsumer(data)
	decType, err := f.GetInt()
	if err != nil {
		return 0
	}
	b1, err := f.GetBytes()
	if err != nil {
		return 0
	}
	b2, err := f.GetBytes()
	if err != nil {
		return 0
	}
	defer func() {
		if r := recover(); r != nil {
		}
		runtime.GC()
	}()
	switch decType%5 {
	case 0:
		e, err := f.GetString()
		if err != nil || len(e) != 32 {
			return 0
		}
		enc := base32.NewEncoding(e)
		d := base32.NewDecoder(enc, bytes.NewReader(b1))
		_, _ = d.Read(b2)
		return 1
	case 1:
		e, err := f.GetString()
		if err != nil || len(e) != 64 {
			return 0
		}
		for i := 0; i < len(e); i++ {
			if e[i] == '\n' || e[i] == '\r' {
				return 0
			}
		}
		enc := base64.NewEncoding(e)
		d := base64.NewDecoder(enc, bytes.NewReader(b1))
		_, _ = d.Read(b2)
		return 1
	case 2:
		d := gob.NewDecoder(bytes.NewReader(b1))
		_ = d.Decode(b2)
		return 1
	case 3:
		d := json.NewDecoder(bytes.NewReader(b1))
		_ = d.Decode(b2)
		return 1
	case 4:
		d := xml.NewDecoder(bytes.NewReader(b1))
		_, _ = d.Token()
		return 1
	}
	return 1
}
<commit_msg>golang: refactor encoding fuzzer (#8841)<commit_after>package encoding

import (
	"bytes"
	"encoding/base32"
	"encoding/base64"
	"encoding/gob"
	"encoding/json"
	"encoding/xml"
	fuzz "github.com/AdaLogics/go-fuzz-headers"
)

func FuzzEncoding(data []byte) int {
	f := fuzz.NewConsumer(data)
	decType, err := f.GetInt()
	if err != nil {
		return 0
	}
	b1, err := f.GetBytes()
	if err != nil {
		return 0
	}
	switch decType % 5 {
	case 0:
		e, err := f.GetString()
		if err != nil || len(e) != 32 {
			return 0
		}
		enc := base32.NewEncoding(e)
		d := base32.NewDecoder(enc, bytes.NewReader(b1))
		dbuf := make([]byte, enc.DecodedLen(len(e)))
		_, _ = d.Read(dbuf)
	case 1:
		e, err := f.GetString()
		if err != nil || len(e) != 64 {
			return 0
		}
		for i := 0; i < len(e); i++ {
			if e[i] == '\n' || e[i] == '\r' {
				return 0
			}
		}
		enc := base64.NewEncoding(e)
		d := base64.NewDecoder(enc, bytes.NewReader(b1))
		dbuf := make([]byte, enc.DecodedLen(len(e)))
		_, _ = d.Read(dbuf)
	case 2:
		b2, err := f.GetBytes()
		if err != nil {
			return 0
		}
		d := gob.NewDecoder(bytes.NewReader(b1))
		_ = d.Decode(b2)
	case 3:
		b2, err := f.GetBytes()
		if err != nil {
			return 0
		}
		d := json.NewDecoder(bytes.NewReader(b1))
		_ = d.Decode(b2)
	case 4:
		d := xml.NewDecoder(bytes.NewReader(b1))
		_, _ = d.Token()
	}
	return 1
}

{'fraction_non_alphanumeric': 0.18500190331176247, 'fraction_numerical': 0.034640274076893796, 'mean_word_length': 2.8761061946902653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7300880', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1141, 'n_words': 256}