Esempio n. 1
0
func parseIndex(sql []byte) (indexName string, indexId interface{}, userId uint64) {
	var err error
	keyspaceIndex := bytes.Index(sql, KEYSPACE_ID_COMMENT)
	if keyspaceIndex == -1 {
		panic(NewBinlogParseError(fmt.Sprintf("Error parsing index comment, doesn't contain keyspace id %v", string(sql))))
	}
	keyspaceIdComment := sql[keyspaceIndex+len(KEYSPACE_ID_COMMENT):]
	indexCommentStart := bytes.Index(keyspaceIdComment, INDEX_COMMENT)
	if indexCommentStart != -1 {
		indexCommentParts := bytes.SplitN(keyspaceIdComment[indexCommentStart:], COLON_BYTE, 2)
		userId, err = strconv.ParseUint(string(bytes.SplitN(indexCommentParts[1], mysqlctl.SPACE, 2)[0]), 10, 64)
		if err != nil {
			panic(NewBinlogParseError(fmt.Sprintf("Error converting user_id %v", string(sql))))
		}
		indexNameId := bytes.Split(indexCommentParts[0], DOT_BYTE)
		indexName = string(indexNameId[1])
		if indexName == "username" {
			indexId = string(bytes.TrimRight(indexNameId[2], COLON))
		} else {
			indexId, err = strconv.ParseUint(string(bytes.TrimRight(indexNameId[2], COLON)), 10, 64)
			if err != nil {
				panic(NewBinlogParseError(fmt.Sprintf("Error converting index id %v %v", string(bytes.TrimRight(indexNameId[2], COLON)), string(sql))))
			}
		}
	}
	return
}
Esempio n. 2
0
// ParseKey reads the given RSA private key and create a public one for it.
func ParseKey(pem string) (*Key, error) {
	p, err := ioutil.ReadFile(pem)
	if err != nil {
		return nil, err
	}
	key, err := ssh.ParseRawPrivateKey(p)
	if err != nil {
		return nil, err
	}
	rsaKey, ok := key.(*rsa.PrivateKey)
	if !ok {
		return nil, fmt.Errorf("%q is not a RSA key", pem)
	}
	pub, err := ssh.NewPublicKey(&rsaKey.PublicKey)
	if err != nil {
		return nil, err
	}
	// Compute key fingerprint.
	var buf bytes.Buffer
	for _, b := range md5.Sum(pub.Marshal()) {
		fmt.Fprintf(&buf, "%0.2x:", b)
	}
	return &Key{
		Label:       strings.TrimSuffix(filepath.Base(pem), ".pem"),               // trim .pem file extension
		Key:         string(bytes.TrimRight(ssh.MarshalAuthorizedKey(pub), "\n")), // trim newline
		Fingerprint: string(bytes.TrimRight(buf.Bytes(), ":")),                    // trim dangling colon
		Note:        "{}",
		Tags:        make(Tags),
	}, nil
}
Esempio n. 3
0
func DetectAtxHeader(first, second Line, detectors Detectors) Handler {
	if !bytes.HasPrefix(first.Bytes, []byte("#")) {
		return nil
	}
	done := false
	return HandlerFunc(func(line Line, ctx Context) (bool, error) {
		if done {
			return false, nil
		}
		done = true
		block := md.AtxHeaderBlock{
			Raw: md.Raw{md.Run(line)},
		}
		text := bytes.TrimRight(line.Bytes, "\n")
		text = bytes.Trim(text, "#")
		if len(text) > 0 {
			block.Level, _ = mdutils.OffsetIn(line.Bytes, text)
		} else {
			block.Level = len(bytes.TrimRight(line.Bytes, "\n"))
		}
		if block.Level > 6 {
			block.Level = 6
		}

		spanRegion := md.Raw{md.Run{
			Line:  line.Line,
			Bytes: bytes.Trim(text, mdutils.Whites),
		}}
		ctx.Emit(block)
		parseSpans(spanRegion, ctx)
		ctx.Emit(md.End{})
		return true, nil
	})
}
Esempio n. 4
0
func (p *parser) readLine() []byte {
	if !p.linenoFixed {
		p.lineno = p.elineno + 1
	}
	var line []byte
	for !p.done {
		buf, err := p.rd.ReadBytes('\n')
		if !p.linenoFixed {
			p.elineno++
		}
		if err == io.EOF {
			p.done = true
		} else if err != nil {
			p.err = fmt.Errorf("readline %s: %v", p.srcpos(), err)
			p.done = true
		}
		line = append(line, buf...)
		buf = bytes.TrimRight(buf, "\r\n")
		backslash := false
		for len(buf) > 1 && buf[len(buf)-1] == '\\' {
			buf = buf[:len(buf)-1]
			backslash = !backslash
		}
		if !backslash {
			break
		}
	}
	line = bytes.TrimRight(line, "\r\n")
	return line
}
Esempio n. 5
0
func main() {
	input_buffer := bufio.NewReader(os.Stdin)
	fmt.Println("Type your settings from https://themis.cossacklabs.com/interactive-simulator/setup/")

	fmt.Println("JSON endpoint: ")
	endpoint, err := input_buffer.ReadString('\n')
	endpoint = strings.TrimRight(endpoint, "\n\r")

	fmt.Println("Your private key in base64 format:")
	client_private, err := input_buffer.ReadBytes('\n')
	client_private, err = base64.StdEncoding.DecodeString(string(client_private))
	if err != nil {
		fmt.Println("Incorrect base64 format for private key")
		return
	}

	fmt.Println("Server public key in base64 format:")
	server_public, err := input_buffer.ReadBytes('\n')
	server_public = bytes.TrimRight(server_public, "\r\n")
	server_public, err = base64.StdEncoding.DecodeString(string(server_public))
	secure_message := message.New(
		&keys.PrivateKey{bytes.TrimRight(client_private, "\r\n")},
		&keys.PublicKey{server_public})

	for {
		fmt.Println("Print message to send (or quit to stop):")
		line, _, err := input_buffer.ReadLine()
		if err != nil {
			fmt.Println(err)
			return
		}
		if bytes.Equal(line, []byte("quit")) {
			return
		}

		wrapped, err := secure_message.Wrap(line)
		if err != nil {
			fmt.Println("Error in wraping", err)
			return
		}
		data, err := send_message(wrapped, endpoint)
		if err != nil {
			fmt.Println("Error occured:", err)
			return
		}
		unwrapped, err := secure_message.Unwrap(data)
		fmt.Println(string(unwrapped))
	}
}
Esempio n. 6
0
func formatQuoted(text []byte) []byte {
	if bytes.Equal(text, noOutputOld) {
		return noOutputNew
	}
	nonZero := bytes.HasSuffix(text, nonZeroOld)
	if nonZero {
		text = text[:len(text)-len(nonZeroOld)]
	}

	var buf bytes.Buffer
	buf.Grow(512)
	fmt.Fprintf(&buf, "%q", text)
	if buf.Len() > maxTextLen {
		truncateMaxLen(&buf, 1)
		buf.Truncate(len(bytes.TrimRight(buf.Bytes(), "\\")))
		buf.WriteString(`" + `)
		buf.WriteString(more)
	}

	if nonZero {
		buf.WriteString(split)
		buf.WriteString(nonZeroNew)
	}
	return buf.Bytes()
}
Esempio n. 7
0
func main() {
	flag.Parse()
	if *show_key {
		if flag.NArg() < 1 {
			flag.Usage()
			os.Exit(1)
		}
		for _, seed := range flag.Args() {
			key, err := issh.GetAuthorizedKey(seed)
			if err != nil {
				panic("Failed to create a key: " + err.Error())
			}
			fmt.Printf("public key for \"%s\"\n"+
				"command=\"exit 1\",no-port-forwarding,no-X11-forwarding,no-pty %s "+
				"issh generated from seed: '%s'\n",
				seed, bytes.TrimRight(key, "\r\n \t"), seed)
		}
		return
	}

	if flag.NArg() != 2 {
		flag.Usage()
		os.Exit(1)
	}

	user, host, port := parseUserHostPort(flag.Arg(1))
	stdout, exitcode, err := issh.Run(user, host, port, flag.Arg(0))
	if err != nil {
		panic("Failed to execute remote command: " + err.Error())
	}

	os.Stdout.Write(stdout)
	os.Exit(exitcode)
}
Esempio n. 8
0
func desDecode(b []byte) ([]byte, error) {
	td, err := des.NewTripleDESCipher(deskey)
	if err != nil {
		logger.Println(err)
		return nil, err
	}
	// blockMode := cipher.NewCBCDecrypter(block, key)
	// orig := make([]byte, len(b))
	// blockMode.CryptBlocks(orig, b)
	// logger.Println(string(orig))

	n := len(b) / td.BlockSize()
	var rb []byte
	for i := 0; i < n; i++ {
		dst := make([]byte, td.BlockSize())
		td.Decrypt(dst, b[i*8:(i+1)*8])
		rb = append(rb, dst[:]...)
	}

	lastValue := int(rb[len(rb)-1])
	logger.Println(string(rb[0 : len(rb)-lastValue]))

	// 移除最后的0
	return bytes.TrimRight(rb, string([]byte{0})), nil
}
Esempio n. 9
0
func (S) TestPatterns(c *C) {
	const sep = " "
	var input bytes.Buffer

	key := genPublicKey(c)
	keyBytes := bytes.TrimRight(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)), "\n")

	// format: pattern
	input.WriteString("*.example")
	input.WriteString(sep)
	input.Write(keyBytes)
	input.WriteString("\n")

	// format: negated pattern
	input.WriteString("!*.example.or?")
	input.WriteString(sep)
	input.Write(keyBytes)
	input.WriteString("\n")

	k, err := Unmarshal(bytes.NewReader(input.Bytes()))
	c.Assert(err, IsNil)

	// Test HostKeyCallback
	addr := &net.TCPAddr{
		Port: 22,
	}
	c.Assert(k.HostKeyCallback("foo.example:22", addr, key), IsNil)                         // pattern match
	c.Assert(k.HostKeyCallback("foo.example.org:22", addr, key), Equals, HostNotFoundError) // negated pattern match
	c.Assert(k.HostKeyCallback("anything.example.com:22", addr, key), IsNil)                // negated pattern miss

	// Make sure output is the same as input
	var output bytes.Buffer
	c.Assert(k.Marshal(&output), IsNil)
	c.Assert(output.String(), Equals, input.String())
}
Esempio n. 10
0
// read reads events from an inotify file descriptor. It does not handle errors
// returned from read(2) function since they are not critical to watcher logic.
func (i *inotify) read() (es []*event) {
	n, err := syscall.Read(int(i.fd), i.buffer[:])
	if err != nil || n < syscall.SizeofInotifyEvent {
		return
	}
	var sys *syscall.InotifyEvent
	nmin := n - syscall.SizeofInotifyEvent
	for pos, path := 0, ""; pos <= nmin; {
		sys = (*syscall.InotifyEvent)(unsafe.Pointer(&i.buffer[pos]))
		pos += syscall.SizeofInotifyEvent
		if path = ""; sys.Len > 0 {
			endpos := pos + int(sys.Len)
			path = string(bytes.TrimRight(i.buffer[pos:endpos], "\x00"))
			pos = endpos
		}
		es = append(es, &event{
			sys: syscall.InotifyEvent{
				Wd:     sys.Wd,
				Mask:   sys.Mask,
				Cookie: sys.Cookie,
			},
			path: path,
		})
	}
	return
}
Esempio n. 11
0
// Client processor blockingly reads everything remote client sends,
// splits messages by CRLF and send them to Daemon gorouting for processing
// it futher. Also it can signalize that client is unavailable (disconnected).
func (client *Client) Processor(sink chan<- ClientEvent) {
	var bufNet []byte
	buf := make([]byte, 0)
	log.Println(client, "New client")
	sink <- ClientEvent{client, EventNew, ""}
	for {
		bufNet = make([]byte, BufSize)
		_, err := client.conn.Read(bufNet)
		if err != nil {
			sink <- ClientEvent{client, EventDel, ""}
			break
		}
		bufNet = bytes.TrimRight(bufNet, "\x00")
		buf = append(buf, bufNet...)
		if !bytes.HasSuffix(buf, []byte(CRLF)) {
			continue
		}
		for _, msg := range bytes.Split(buf[:len(buf)-2], []byte(CRLF)) {
			if len(msg) > 0 {
				sink <- ClientEvent{client, EventMsg, string(msg)}
			}
		}
		buf = []byte{}
	}
}
func TestFlushOnClose(t *testing.T) {
	l, err := newUDPListener("127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer l.Close()

	c, err := NewBufferedClient(l.LocalAddr().String(), "test", 1*time.Second, 1024)
	if err != nil {
		t.Fatal(err)
	}

	c.Inc("count", int64(1), 1.0)
	c.Close()

	expected := "test.count:1|c"

	data := make([]byte, 1024)
	_, _, err = l.ReadFrom(data)
	if err != nil {
		t.Fatal(err)
	}

	data = bytes.TrimRight(data, "\x00")
	if bytes.Equal(data, []byte(expected)) != true {
		fmt.Println(data)
		fmt.Println([]byte(expected))
		t.Fatalf("got '%s' expected '%s'", data, expected)
	}
}
Esempio n. 13
0
func (peer Peer) downloadFile(file File, conn *net.TCPConn) {
	if f, ok := status.status["local"].files[file.FileName]; ok {
		if f.Chunks[file.Chunks[1]] == 1 {
			return
		}
	} else {
		chunks := make([]int, file.Chunks[0])
		for chunk := range chunks {
			chunks[chunk] = 0
		}
		chunks[file.Chunks[1]] = 1
		status.status["local"].files[file.FileName] = File{
			FileName: file.FileName,
			Chunks:   chunks,
		}
	}
	status.status["local"].files[file.FileName].Chunks[file.Chunks[1]] = 1
	incrementChunkReplication(file.FileName, file.Chunks[1], file.Chunks[0])

	err := conn.SetReadBuffer(ChunkSize)
	checkError(err)

	readBuffer := make([]byte, ChunkSize)
	_, err = conn.Read(readBuffer)
	checkError(err)
	conn.Close()

	status.mu.Lock()
	basepath := path.Dir(file.FileName)
	fileName := path.Base(file.FileName)
	err = os.MkdirAll(basepath, 0777)
	checkError(err)

	filePath := path.Join(basepath, fileName)

	localFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0777)
	if err != nil {
		for {
			localFile, err = os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0777)
			if err == nil {
				break
			}
		}
	}

	writeOffset := int64(file.Chunks[1] * ChunkSize)
	_, err = localFile.WriteAt(bytes.TrimRight(readBuffer, "\x00"), writeOffset)
	checkError(err)

	err = localFile.Close()
	checkError(err)

	status.mu.Unlock()
	fmt.Printf("Downloaded file %s:%d \n\n", file.FileName, file.Chunks[1])

	fileList := []File{file}
	haveMessage := encodeMessage(peer.host, peer.port, Have, fileList)
	sendToAll(haveMessage)
	return
}
Esempio n. 14
0
func DetectSetextHeader(first, second Line, detectors Detectors) Handler {
	if second.EOF() {
		return nil
	}
	if !reSetextHeader.Match(bytes.TrimRight(second.Bytes, "\n")) {
		return nil
	}
	block := md.SetextHeaderBlock{}
	switch second.Bytes[0] {
	case '=':
		block.Level = 1
	case '-':
		block.Level = 2
	}
	done := 0
	return HandlerFunc(func(next Line, ctx Context) (bool, error) {
		if done == 2 {
			ctx.Emit(block)
			parseSpans(trim(md.Raw{block.Raw[0]}), ctx)
			ctx.Emit(md.End{})
			return false, nil
		}
		done++
		block.Raw = append(block.Raw, md.Run(next))
		return true, nil
	})
}
Esempio n. 15
0
func main() {
	flag.Parse()
	if *fPeers == "" {
		flagbad("-peers is empty\n")
	}
	if *fTopic == "" {
		flagbad("-topic is empty\n")
	}

	kfk, err := kafka.New("kafka-producer", logger, strings.Split(*fPeers, ","))
	if err != nil {
		logger.Panicf("Failed to start kafka: %v", err)
	}
	defer kfk.Close()

	br := bufio.NewReader(os.Stdin)
	for {
		line, err := br.ReadBytes('\n')
		if err == io.EOF && len(line) == 0 {
			break
		}
		if err != nil {
			logger.Panicf("Reading from stdin: %v", err)
		}
		line = bytes.TrimRight(line, "\n")
		part, offset, err := kfk.Send(nil, line, *fTopic)
		if err != nil {
			logger.Panicf("Sending message: %v", err)
		}
		if *fVerbose {
			fmt.Printf("send (len=%d, part=%d, offset=%d)\n", len(line), part, offset)
		}
	}
}
Esempio n. 16
0
// Serve a connection by reading and writing what was read.  That's right, this
// is an echo service.  Stop reading and writing if anything is received on the
// service's channel but only after writing what was read.
func (self *Service) serve(connection *Connection) {
	defer connection.Close()
	defer self.waitGroup.Done()
	for {
		select {
		case <-self.done:
			log.Println("Disconnecting", connection.TCPConn.RemoteAddr())
			return
		default:
		}

		connection.TCPConn.SetDeadline(time.Now().Add(30 * time.Second))
		buf := make([]byte, 4096)
		if _, err := connection.TCPConn.Read(buf); err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Println("Error reading from connection:", err)
			delete(self.dataMap, connection.Id)
			break
		}

		payload := bytes.TrimRight(buf, string([]byte{0x00, '\n', '\r'}))
		self.Output <- [][]byte{[]byte(connection.Id), payload}

		data := <-connection.Input
		data = append(data, '\n', '\r')
		if _, err := connection.TCPConn.Write(data); err != nil {
			log.Println(err)
			break
		}
	}
}
Esempio n. 17
0
func (y *yencReader) nextLine() error {
	if y.eof {
		return io.EOF
	}

	// read a whole line
	line, err := y.r.ReadBytes('\n')
	if err != nil {
		return err
	}

	// chomp the line ending
	line = bytes.TrimRight(line, "\r\n")

	if !y.readHeader {
		// expect a =ybegin line
		if len(line) >= 7 && string(line[:7]) == "=ybegin" {
			y.readHeader = true
			return nil
		} else {
			return fmt.Errorf("expected =ybegin, got %q", string(line))
		}
	}

	// are we at the end of the yenc blob?
	if len(line) >= 5 && string(line[:5]) == "=yend" {
		// remember this and signal the caller
		y.eof = true
		return io.EOF
	}

	y.buf = y.decode(line)
	return nil
}
Esempio n. 18
0
func ParseMessage(message string) *Message {
	// :<prefix> <command> <params> :<trailing>
	msg := []byte(message)
	var prefix, params, trailing []byte

	if bytes.HasPrefix(msg, []byte(":")) {
		index := bytes.Index(msg, []byte(" "))
		prefix = msg[1:index]
		msg = msg[index+1:]
	}

	cmdEndIndex := bytes.Index(msg, []byte(" "))
	command := msg[:cmdEndIndex]
	msg = msg[cmdEndIndex+1:]
	trailingStartIndex := bytes.Index(msg, []byte(":"))
	if trailingStartIndex < 0 {
		params = msg
	} else {
		params = bytes.TrimRight(msg[:trailingStartIndex], " ")
		trailing = msg[trailingStartIndex+1:]
	}

	return &Message{
		raw:      message,
		Prefix:   prefix,
		Command:  command,
		Params:   params,
		Trailing: trailing,
	}
}
Esempio n. 19
0
// Adds a new part to the given multipart writer, containing the given revision.
// The revision will be written as a nested multipart body if it has attachments.
func (db *Database) WriteRevisionAsPart(revBody Body, isError bool, compressPart bool, writer *multipart.Writer) error {
	partHeaders := textproto.MIMEHeader{}
	docID, _ := revBody["_id"].(string)
	revID, _ := revBody["_rev"].(string)
	if len(docID) > 0 {
		partHeaders.Set("X-Doc-ID", docID)
		partHeaders.Set("X-Rev-ID", revID)
	}

	if hasInlineAttachments(revBody) {
		// Write as multipart, including attachments:
		// OPT: Find a way to do this w/o having to buffer the MIME body in memory!
		var buffer bytes.Buffer
		docWriter := multipart.NewWriter(&buffer)
		contentType := fmt.Sprintf("multipart/related; boundary=%q",
			docWriter.Boundary())
		partHeaders.Set("Content-Type", contentType)
		db.WriteMultipartDocument(revBody, docWriter, compressPart)
		docWriter.Close()
		content := bytes.TrimRight(buffer.Bytes(), "\r\n")

		part, err := writer.CreatePart(partHeaders)
		if err == nil {
			_, err = part.Write(content)
		}
		return err
	} else {
		// Write as JSON:
		contentType := "application/json"
		if isError {
			contentType += `; error="true"`
		}
		return writeJSONPart(writer, contentType, revBody, compressPart)
	}
}
Esempio n. 20
0
func (h *handler) writeMultipart(subtype string, callback func(*multipart.Writer) error) error {
	if !h.requestAccepts("multipart/") {
		return base.HTTPErrorf(http.StatusNotAcceptable, "Response is multipart")
	}

	// Get the output stream. Due to a CouchDB bug, if we're sending to it we need to buffer the
	// output in memory so we can trim the final bytes.
	var output io.Writer
	var buffer bytes.Buffer
	if h.userAgentIs("CouchDB") {
		output = &buffer
	} else {
		output = h.response
	}

	writer := multipart.NewWriter(output)
	h.setHeader("Content-Type",
		fmt.Sprintf("multipart/%s; boundary=%q", subtype, writer.Boundary()))

	err := callback(writer)
	writer.Close()

	if err == nil && output == &buffer {
		// Trim trailing newline; CouchDB is allergic to it:
		_, err = h.response.Write(bytes.TrimRight(buffer.Bytes(), "\r\n"))
	}
	return err
}
Esempio n. 21
0
func (d *decoder) readBody() error {
	// ready the part body
	d.part.Body = make([]byte, 0)
	// reset special
	d.awaitingSpecial = false
	// setup crc hash
	d.part.crcHash = crc32.NewIEEE()
	// each line
	for {
		line, err := d.buf.ReadBytes('\n')
		if err != nil {
			return err
		}
		// strip linefeeds (some use CRLF some LF)
		line = bytes.TrimRight(line, "\r\n")
		// check for =yend
		if len(line) >= 5 && string(line[:5]) == "=yend" {
			return d.parseTrailer(string(line))
		}
		// decode
		b := d.decode(line)
		// update hashs
		d.part.crcHash.Write(b)
		d.crcHash.Write(b)
		// decode
		d.part.Body = append(d.part.Body, b...)
	}
	return nil
}
Esempio n. 22
0
func (w *trimWriter) Write(p []byte) (int, error) {
	out := bytes.TrimRight(p, w.trimChars)
	buf := p[len(out):]
	var written int
	if (len(out) > 0) && (w.trimBuf != nil) {
		var err error
		if written, err = w.output.Write(w.trimBuf); err != nil {
			return 0, err
		}
		w.trimBuf = nil
	}
	if w.trimBuf != nil {
		w.trimBuf = append(w.trimBuf, buf...)
	} else {
		w.trimBuf = buf
	}
	if len(p) == 0 {
		return written, nil
	}
	ret, err := w.output.Write(out)
	if err != nil {
		return 0, err
	}
	return written + ret, nil
}
Esempio n. 23
0
func (r *Repository) execAndParseCols(subcmd string) ([][2]string, error) {
	cmd := exec.Command("hg", "-v", "--debug", subcmd)
	cmd.Dir = r.Dir
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("exec `hg -v --debug %s` failed: %s. Output was:\n\n%s", subcmd, err, out)
	}

	out = bytes.TrimSuffix(out, []byte("\n")) // remove trailing newline
	lines := bytes.Split(out, []byte("\n"))
	sort.Sort(byteSlices(lines)) // sort for consistency
	refs := make([][2]string, len(lines))
	for i, line := range lines {
		line = bytes.TrimSuffix(line, []byte(" (inactive)"))

		// format: "NAME      SEQUENCE:ID" (arbitrary amount of whitespace between NAME and SEQUENCE)
		if len(line) <= 41 {
			return nil, fmt.Errorf("unexpectedly short (<=41 bytes) line in `hg -v --debug %s` output", subcmd)
		}
		id := line[len(line)-40:]

		// find where the SEQUENCE begins
		seqIdx := bytes.LastIndex(line, []byte(" "))
		if seqIdx == -1 {
			return nil, fmt.Errorf("unexpectedly no whitespace in line in `hg -v --debug %s` output", subcmd)
		}
		name := bytes.TrimRight(line[:seqIdx], " ")
		refs[i] = [2]string{string(id), string(name)}
	}
	return refs, nil
}
Esempio n. 24
0
// test builds the test in the given file.
// If want is non-empty, test also runs the test
// and checks that the output matches the regexp want.
func test(file, want string) error {
	// Build the program.
	cmd := exec.Command("go", "build", file+".go")
	out, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("go build %s.go failed: %v\nOutput:\n%s", file, err, out)
	}
	defer os.Remove(file)

	// Only run the test if we have output to check.
	if want == "" {
		return nil
	}

	cmd = exec.Command("./" + file)
	out, err = cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("./%s failed: %v\nOutput:\n%s", file, err, out)
	}

	// Canonicalize output.
	out = bytes.TrimRight(out, "\n")
	out = bytes.Replace(out, []byte{'\n'}, []byte{' '}, -1)

	// Check the result.
	match, err := regexp.Match(want, out)
	if err != nil {
		return fmt.Errorf("failed to parse regexp %q: %v", want, err)
	}
	if !match {
		return fmt.Errorf("%s.go:\n%q\ndoes not match %s", file, out, want)
	}

	return nil
}
Esempio n. 25
0
func (peers *Peers) connectPeer(hostName string, portNumber int, conn *net.TCPConn) {
	peer, err := peers.getPeer(hostName, portNumber)
	checkError(err)

	err = conn.SetReadBuffer(ChunkSize)
	checkError(err)

	readBuffer := make([]byte, ChunkSize)
	_, err = conn.Read(readBuffer)
	checkError(err)

	var fileList FileList
	readBuffer = bytes.TrimRight(readBuffer, "\x00")

	err = json.Unmarshal(readBuffer, &fileList)
	checkError(err)

	if peer.currentState != Connected {
		updateStatus(hostName, portNumber, fileList.Files)
		peers.numPeers += 1
	}

	peer.currentState = Connected
	return
}
Esempio n. 26
0
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
func (p *VixProperty) UnmarshalBinary(data []byte) error {
	buf := bytes.NewBuffer(data)

	err := binary.Read(buf, binary.LittleEndian, &p.header)
	if err != nil {
		return err
	}

	switch p.header.Kind {
	case vixPropertyTypeBool:
		return binary.Read(buf, binary.LittleEndian, &p.data.Bool)
	case vixPropertyTypeInt32:
		return binary.Read(buf, binary.LittleEndian, &p.data.Int32)
	case vixPropertyTypeInt64:
		return binary.Read(buf, binary.LittleEndian, &p.data.Int64)
	case vixPropertyTypeString:
		s := make([]byte, p.header.Length)
		if _, err := buf.Read(s); err != nil {
			return err
		}

		p.data.String = string(bytes.TrimRight(s, "\x00"))
	case vixPropertyTypeBlob:
		p.data.Blob = make([]byte, p.header.Length)
		if _, err := buf.Read(p.data.Blob); err != nil {
			return err
		}
	default:
		return errors.New("VIX_E_UNRECOGNIZED_PROPERTY")
	}

	return nil
}
Esempio n. 27
0
func readTrimmedLine(r *bufio.Reader) ([]byte, error) {
	if line, err := r.ReadBytes('\n'); err != nil {
		return nil, err
	} else {
		return bytes.TrimRight(line, "\r\n"), nil
	}
}
Esempio n. 28
0
// Dispatch an incoming RPC request to a Handler
func (s *Service) Dispatch(request []byte) []byte {
	msg := bytes.SplitN(request, []byte{' '}, 2)
	name := msg[0]

	// Trim NULL byte terminator
	name = bytes.TrimRight(name, "\x00")

	handler, ok := s.handlers[string(name)]

	if !ok {
		log.Printf("unknown command: %q\n", name)
		return []byte("Unknown Command")
	}

	var args []byte
	if len(msg) == 2 {
		args = msg[1]
	}

	response, err := handler(args)
	if err == nil {
		response = append([]byte("OK "), response...)
	} else {
		log.Printf("error calling %s: %s\n", name, err)
		response = append([]byte("ERR "), response...)
	}

	return response
}
Esempio n. 29
0
// fmtP appends the string of x in the format 0x." mantissa "p" exponent
// with a hexadecimal mantissa and a binary exponent, or 0" if x is zero,
// ad returns the extended buffer.
// The mantissa is normalized such that 0.5 <= 0.mantissa < 1.0.
// The sign of x is ignored, and x must not be an Inf.
func (x *Float) fmtP(buf []byte) []byte {
	if x.form == zero {
		return append(buf, '0')
	}

	if debugFloat && x.form != finite {
		panic("non-finite float")
	}
	// x != 0

	// remove trailing 0 words early
	// (no need to convert to hex 0's and trim later)
	m := x.mant
	i := 0
	for i < len(m) && m[i] == 0 {
		i++
	}
	m = m[i:]

	buf = append(buf, "0x."...)
	buf = append(buf, bytes.TrimRight(m.utoa(16), "0")...)
	buf = append(buf, 'p')
	if x.exp >= 0 {
		buf = append(buf, '+')
	}
	return strconv.AppendInt(buf, int64(x.exp), 10)
}
Esempio n. 30
0
func (test *RESTHandlerTest) check(t *testing.T,
	record *httptest.ResponseRecorder) {
	desc := test.Desc
	if desc == "" {
		desc = test.Path + " " + test.Method
	}

	if got, want := record.Code, test.Status; got != want {
		t.Errorf("%s: response code = %d, want %d", desc, got, want)
		t.Errorf("%s: response body = %s", desc, record.Body)
	}
	got := bytes.TrimRight(record.Body.Bytes(), "\n")
	if test.ResponseBody != nil {
		if !reflect.DeepEqual(got, test.ResponseBody) {
			t.Errorf("%s: expected: '%s', got: '%s'",
				desc, test.ResponseBody, got)
		}
	}
	for pattern, shouldMatch := range test.ResponseMatch {
		didMatch := bytes.Contains(got, []byte(pattern))
		if didMatch != shouldMatch {
			t.Errorf("%s: expected match %t for pattern %s, got %t",
				desc, shouldMatch, pattern, didMatch)
			t.Errorf("%s: response body was: %s", desc, got)
		}
	}
}