예제 #1
0
func (e *Engine) ascii85() error {
	b := e.stack.Pop()
	enc := make([]byte, ascii85.MaxEncodedLen(len(b)))
	sz := ascii85.Encode(enc, b)
	e.stack.Push(enc[0:sz])
	return nil
}
예제 #2
0
파일: xml.go 프로젝트: Katharine/go-llsd
func (x *XMLLLSDParser) parseBinary(encoding string) []byte {
	for {
		token, err := x.decoder.Token()
		if err != nil {
			panic(err)
		}
		switch t := token.(type) {
		case xml.StartElement:
			panic("Unexpected start element in <binary>")
		case xml.CharData:
			defer x.decoder.Skip()
			switch encoding {
			case "base64", "":
				b64, _ := base64.StdEncoding.DecodeString(string(t))
				return b64
			case "base85":
				b85 := make([]byte, ascii85.MaxEncodedLen(len(t)))
				n, _, _ := ascii85.Decode(b85, t, true)
				return b85[:n]
			case "base16":
				output := make([]byte, len(t)/2)
				for i := 0; i < len(t); i += 2 {
					n, _ := strconv.ParseInt(string(t[i:i+2]), 16, 8)
					output[i/2] = byte(n)
				}
				return output
			}
		case xml.EndElement:
			return make([]byte, 0)
		}
	}
	panic("unreachable")
}
예제 #3
0
파일: password.go 프로젝트: pmoroney/poplop
func poplop(master string, n Scheme) (string, error) {
	h := sha512.New()
	io.WriteString(h, master)
	io.WriteString(h, n.Name)
	if n.Counter > 0 {
		io.WriteString(h, strconv.Itoa(n.Counter))
	}
	hash := h.Sum(nil)
	dst := make([]byte, ascii85.MaxEncodedLen(len(hash)))
	ascii85.Encode(dst, hash)

	str := string(dst)
	if n.Forbidden != "" {
		str = strings.Map(n.mapping(), str)
	}

	length := 20
	if n.MaxLength > 0 {
		length = n.MaxLength
	}

	if len(str) < length {
		return "", errors.New("hash not long enough " + str)
	}
	return str[:length], nil
}
예제 #4
0
func (x *DNSTransportDownstreamCodec) Encode(msg []byte, header DNSCodecHeader) string {
	header_bytes := x.header_codec.EncodeToBytes(&header)

	dst := make([]byte, ascii85.MaxEncodedLen(4+len(msg)))

	ret_len := ascii85.Encode(dst[0:5], header_bytes[:])
	ret_len += ascii85.Encode(dst[5:], msg)

	return string(dst[:ret_len])
}
예제 #5
0
func insertAssets(w io.Writer, fc *fileCollection, c *config) (err error) {
	// NewAsset(name string, content []byte, size int64, mode os.FileMode, modTime time.Time) *Asset

	_, err = fmt.Fprint(w, "\n\tvar assets = map[string]magpie.Asset{\n")
	if err != nil {
		return
	}
	for _, a := range fc.assets {
		fmt.Printf("Hash length:%v\n", a.hash)
		hash := make([]byte, ascii85.MaxEncodedLen(len(a.hash)))
		l := ascii85.Encode(hash, a.hash)
		_, err = fmt.Fprintf(w, "\t\t%q: magpie.NewAsset(%q, read(_%s), %d, os.FileMode(%d), time.Unix(%d, 0), %q),\n", a.name, a.name, a.constant, a.info.Size(), a.info.Mode(), a.info.ModTime().Unix(), string(hash[:l]))
		if err != nil {
			return
		}

	}
	_, err = fmt.Fprint(w, "\n\t}\n")
	return
}
예제 #6
0
파일: main.go 프로젝트: reusee/memory
func main() {
	// lock
	ln, err := net.Listen("tcp", "127.0.0.1:61297")
	if err != nil {
		fmt.Printf("lock failed.\n")
		return
	}
	defer ln.Close()

	mem := &Memory{
		Concepts: make(map[string]*Concept),
		Connects: make(map[string]*Connect),
	}
	mem.Load()

	getPendingConnect := func(now time.Time) []*Connect {
		var connects []*Connect
		for _, connect := range mem.Connects {
			from := mem.Concepts[connect.From]
			if (from.What == WORD || from.What == SENTENCE) && from.Incomplete {
				continue
			}

			lastHistory := connect.Histories[len(connect.Histories)-1]
			if lastHistory.Time.Add(LevelTime[lastHistory.Level]).After(now) {
				continue
			}

			connects = append(connects, connect)
		}
		return connects
	}

	var cmd string
	if len(os.Args) == 1 {
		cmd = "train"
	} else {
		cmd = os.Args[1]
	}
	max := 22
	if len(os.Args) == 2 && regexp.MustCompile(`[0-9]+`).MatchString(os.Args[1]) {
		max, _ = strconv.Atoi(os.Args[1])
		cmd = "train"
	}

	// add audio files
	if cmd == "add" {
		if len(os.Args) < 3 {
			fmt.Printf("word or sentence?\n")
			os.Exit(0)
		}
		t := os.Args[2]
		var what int
		if strings.HasPrefix(t, "w") {
			what = WORD
		} else if strings.HasPrefix(t, "s") {
			what = SENTENCE
		} else {
			fmt.Printf("unknown type\n")
			os.Exit(0)
		}

		p := filepath.Join(rootPath, "files")
		fmt.Printf("files directory: %s\n", p)

		hasher := sha512.New()
		for _, f := range os.Args[2:] {
			data, err := ioutil.ReadFile(f)
			if err != nil {
				continue
			}
			f, _ = filepath.Abs(f)
			f = strings.TrimPrefix(f, p)
			hasher.Reset()
			hasher.Write(data)
			buf := make([]byte, ascii85.MaxEncodedLen(sha512.Size))
			l := ascii85.Encode(buf, hasher.Sum(nil))

			concept := &Concept{
				What:     AUDIO,
				File:     f,
				FileHash: string(buf[:l]),
			}
			exists := mem.AddConcept(concept)
			if exists {
				fmt.Printf("skip %s\n", f)
			} else {
				fmt.Printf("add %s %s\n", f, concept.FileHash)
				// add text concept
				textConcept := &Concept{
					What:       what,
					Incomplete: true,
					Serial:     mem.NextSerial(),
				}
				mem.AddConcept(textConcept)
				// add connect
				mem.AddConnect(&Connect{
					From: concept.Key(),
					To:   textConcept.Key(),
					Histories: []History{
						{Level: 0, Time: time.Now()},
					},
				})
				if what == WORD {
					mem.AddConnect(&Connect{
						From: textConcept.Key(),
						To:   concept.Key(),
						Histories: []History{
							{Level: 0, Time: time.Now()},
						},
					})
				}
			}
		}

		// show stat
		fmt.Printf("%d concepts, %d connects\n", len(mem.Concepts), len(mem.Connects))
		mem.Save()

		// train
	} else if cmd == "train" {
		connects := getPendingConnect(time.Now())
		// sort
		sort.Sort(ConnectSorter{connects, mem})
		if len(connects) > max {
			connects = connects[:max]
		}
		// ui
		//ui_qt(connects, mem)
		ui_gtk(connects, mem)

		// complete connection
	} else if cmd == "complete" {
		for _, connect := range mem.Connects {
			from := mem.Concepts[connect.From]
			if !from.Incomplete && from.Text != "" {
				continue
			}
			if from.What != WORD {
				continue
			}
			to := mem.Concepts[connect.To]
			fmt.Printf("%s\n", to.File)
			to.Play()
			fmt.Scanf("%s\n", &from.Text)
			if from.Text == "" {
				continue
			}
			from.Incomplete = false
			mem.Save()
		}

		// train history
	} else if cmd == "history" {
		counter := make(map[string]int)
		for _, connect := range mem.Connects {
			for _, entry := range connect.Histories {
				t := entry.Time
				counter[fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())]++
			}
		}
		var dates []string
		for date := range counter {
			dates = append(dates, date)
		}
		sort.Strings(dates)
		for _, date := range dates {
			fmt.Printf("%s %d\n", date, counter[date])
		}
	}

}