Esempio n. 1
0
File: imp.go Progetto: kpmy/ot
func initBinary() {
	Binary = &ForeignTemplate{TemplateName: "binary"}
	tm["zbase32"] = Binary
	Binary.Classes = make(map[string]*ForeignClass)
	Binary.Classes["z32"] = &ForeignClass{Template: Binary, Class: "z32",
		Applicator: func(c *ForeignClass, o otm.Object) (post func(*ForeignClass, otm.Object) error, err error) {
			c.Entity = &BinaryDecodeEntity{}
			post = func(c *ForeignClass, _o otm.Object) error {
				if o, ok := _o.(*object); ok {
					for i, _c := range o.vl {
						switch c := _c.(type) {
						case string:
							if c != "" {
								if data, err := zbase32.DecodeString(c); err == nil {
									o.vl[i] = data
								} else {
									return err
								}
							}
						case otm.Object:
							if c.Qualident().Template == "" || c.Qualident().Template == "zbase32" {
								if data, err := zbase32.DecodeString(c.Qualident().Class); err == nil {
									o.vl[i] = data
								} else {
									return err
								}
							}
						}
					}
				}
				return nil
			}
			return
		}}
}
Esempio n. 2
0
func main() {
	log.SetFlags(0)
	log.SetPrefix(prog + ": ")

	flag.Usage = usage
	flag.Parse()

	if flag.NArg() > 0 {
		for _, input := range flag.Args() {
			decoded, err := zbase32.DecodeString(input)
			if err != nil {
				log.Fatal(err)
			}
			if _, err := os.Stdout.Write(decoded); err != nil {
				log.Fatal(err)
			}
		}
	} else {
		scanner := bufio.NewScanner(os.Stdin)
		for scanner.Scan() {
			input := scanner.Text()
			decoded, err := zbase32.DecodeString(input)
			if err != nil {
				log.Fatalf("decoding input: %q: %v", input, err)
			}
			if _, err = os.Stdout.Write(decoded); err != nil {
				log.Fatal(err)
			}
		}
		if err := scanner.Err(); err != nil {
			log.Fatalf("reading standard input:", err)
		}
	}
}
Esempio n. 3
0
func TestQuickPythonDecodeBytes(t *testing.T) {
	if !*python {
		t.Skip("Skipping, use -python to enable")
	}
	us := func(orig []byte) ([]byte, error) {
		encoded := zbase32.EncodeToString(orig)
		return zbase32.DecodeString(encoded)
	}
	them := func(orig []byte) ([]byte, error) {
		// the python library raises IndexError on zero-length input
		if len(orig) == 0 {
			return []byte{}, nil
		}
		encoded := zbase32.EncodeToString(orig)

		cmd := exec.Command("python", "-c", `
import sys, zbase32
enc = sys.stdin.read()
sys.stdout.write(zbase32.a2b(enc))
`)
		cmd.Stdin = strings.NewReader(encoded)
		cmd.Stderr = os.Stderr
		output, err := cmd.Output()
		if err != nil {
			return nil, fmt.Errorf("cannot run python: %v", err)
		}
		return output, nil
	}
	if err := quick.CheckEqual(us, them, nil); err != nil {
		t.Fatal(err)
	}
}
Esempio n. 4
0
func TestDecodeBad(t *testing.T) {
	input := `foo!bar`
	_, err := zbase32.DecodeString(input)
	switch err := err.(type) {
	case nil:
		t.Fatalf("expected error from bad decode")
	case zbase32.CorruptInputError:
		if g, e := err.Error(), `illegal z-base-32 data at input byte 3`; g != e {
			t.Fatalf("wrong error: %q != %q", g, e)
		}
	default:
		t.Fatalf("wrong error from bad decode: %T: %v", err, err)
	}
}
Esempio n. 5
0
func TestQuickRoundtripBytes(t *testing.T) {
	fn := func(orig []byte) bool {
		encoded := zbase32.EncodeToString(orig)
		decoded, err := zbase32.DecodeString(encoded)
		if err != nil {
			t.Logf("orig=\t%x", orig)
			t.Logf("enc=\t%q", encoded)
			t.Fatalf("encode-decode roundtrip gave error: %v", err)
		}
		if !bytes.Equal(orig, decoded) {
			t.Logf("orig=\t%x", orig)
			t.Logf("dec=\t%x", decoded)
			t.Logf("enc=\t%q", encoded)
			return false
		}
		return true
	}
	if err := quick.Check(fn, nil); err != nil {
		t.Fatal(err)
	}
}