File: main.go

package info (click to toggle)
golang-github-segmentio-asm 1.2.0%2Bgit20231107.1cfacc8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: asm: 6,093; makefile: 32
file content (61 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strconv"
	"strings"
	stdlib "unicode/utf8"

	"github.com/segmentio/asm/ascii"
	"github.com/segmentio/asm/utf8"
)

func main() {
	var data []byte
	if len(os.Args) > 1 {
		s := os.Args[1]
		s, err := strconv.Unquote(`"` + s + `"`)
		if err != nil {
			panic(err)
		}
		data = []byte(s)
	} else {
		var err error
		data, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			panic(err)
		}
	}

	s := string(data)
	lines := strings.Split(s, "\n")
	if len(lines) > 0 && strings.HasPrefix(lines[0], "go test fuzz") {
		fmt.Println("Got fuzzer input")
		// TODO: parse with go/parse instead of regexp?
		r := regexp.MustCompile(`^\[\]byte\((.+)\)`)
		results := r.FindStringSubmatch(lines[1])
		s, err := strconv.Unquote(results[1])
		if err != nil {
			panic(err)
		}
		data = []byte(s)
	}

	fmt.Println(string(data))
	fmt.Println(data)
	fmt.Println(len(data), "bytes")

	uref := stdlib.Valid(data)
	aref := ascii.Valid(data)
	fmt.Println("stdlib: utf8:", uref, "ascii:", aref)

	v := utf8.Validate(data)
	fmt.Println("valid:  utf8:", v.IsUTF8(), "ascii:", v.IsASCII(), "v:", v)

	if uref != v.IsUTF8() || aref != v.IsASCII() {
		os.Exit(1)
	}
}