Example #1
0
func (d *Decoder) Decode(in *bufio.Reader, out *bytes.Buffer) (err error) {
	for {
		b, err := in.ReadByte()
		if err != nil {
			break
		}
		switch b {
		case '=':
			char := make([]byte, 2)
			in.Read(char)
			code, _ := strconv.ParseUint(string(char), 16, 8)
			out.WriteByte(byte(code))
		case '_':
			err = out.WriteByte(' ')
		case ' ':
			return errors.New("Invalid char ` `")
		case '?':
			b, err = in.ReadByte()
			if b == '=' {
				return nil
			}
			return errors.New("Unexpected `?`")
		default:
			err = out.WriteByte(b)
		}
	}
	return err
}
Example #2
0
func readBulkData(conn *bufio.Reader, len int64) ([]byte, os.Error) {
	buff := make([]byte, len)

	_, e := io.ReadFull(conn, buff)
	if e != nil {
		return nil, NewErrorWithCause(SYSTEM_ERR, "Error while attempting read of bulkdata", e)
	}
	//	fmt.Println ("Read ", n, " bytes.  data: ", buff);

	crb, e1 := conn.ReadByte()
	if e1 != nil {
		return nil, os.NewError("Error while attempting read of bulkdata terminal CR:" + e1.String())
	}
	if crb != CR_BYTE {
		return nil, os.NewError("<BUG> bulkdata terminal was not CR as expected")
	}
	lfb, e2 := conn.ReadByte()
	if e2 != nil {
		return nil, os.NewError("Error while attempting read of bulkdata terminal LF:" + e2.String())
	}
	if lfb != LF_BYTE {
		return nil, os.NewError("<BUG> bulkdata terminal was not LF as expected.")
	}

	return buff, nil
}
Example #3
0
func newMessageReader(bufr *bufio.Reader) (*messageReader, error) {
	if _, err := bufr.ReadBytes('"'); err != nil {
		return nil, err
	}
	msg, err := bufr.ReadBytes('"')
	if err != nil {
		return nil, err
	}
	for {
		b, err := bufr.Peek(1)
		if err != nil {
			return nil, err
		}
		if b[0] == ',' {
			bufr.ReadByte()
			break
		}
		if b[0] != ' ' {
			break
		}
		bufr.ReadByte()
	}
	return &messageReader{
		reader:    bufr,
		message:   string(msg[:len(msg)-1]),
		firstRead: true,
	}, nil
}
Example #4
0
func readArgument(r *bufio.Reader) ([]byte, error) {

	line, err := r.ReadString('\n')
	if err != nil {
		return nil, malformed("$<argumentLength>", line)
	}
	var argSize int
	if _, err := fmt.Sscanf(line, "$%d\r", &argSize); err != nil {
		return nil, malformed("$<argumentSize>", line)
	}

	// I think int is safe here as the max length of request
	// should be less then max int value?
	data, err := ioutil.ReadAll(io.LimitReader(r, int64(argSize)))
	if err != nil {
		return nil, err
	}

	if len(data) != argSize {
		return nil, malformedLength(argSize, len(data))
	}

	// Now check for trailing CR
	if b, err := r.ReadByte(); err != nil || b != '\r' {
		return nil, malformedMissingCRLF()
	}

	// And LF
	if b, err := r.ReadByte(); err != nil || b != '\n' {
		return nil, malformedMissingCRLF()
	}

	return data, nil
}
Example #5
0
func readArray(r *bufio.Reader) (interface{}, error) {
	_, err := r.ReadByte()
	if err != nil {
		return EMPTY_STRING, err
	}
	countAsStr, err := ReadLine(r)
	if err != nil {
		return EMPTY_STRING, err
	}

	arrLen, err := strconv.Atoi(countAsStr)
	if err != nil {
		return EMPTY_STRING, err
	}

	l := list.New()
	for i := 0; i < arrLen; i++ {
		val, err := readType(r)
		if err != nil {
			return EMPTY_STRING, err
		}
		l.PushBack(val)
	}
	return l, nil
}
Example #6
0
func readByte(r *bufio.Reader) byte {
	b, err := r.ReadByte()
	if err != nil {
		fmt.Println("error reading byte: ", err)
	}
	return b
}
Example #7
0
func read_symbol(b *bufio.Reader) (goscm.SCMT, error) {
	ret := ""
	var c byte
	var err error

	for {
		c, err = b.ReadByte()
		if err != nil {
			break
		}
		if c == ')' {
			b.UnreadByte()
			break
		}
		if c == ';' {
			read_comment(b)
			continue
		}
		if is_whitespace(c) {
			break
		}
		ret = string(append([]byte(ret), c))
	}
	return goscm.NewSymbol(ret), err
}
Example #8
0
func (state *scpUploadState) Protocol(in *bufio.Reader, out io.Writer) error {
	for {
		b, err := in.ReadByte()
		if err != nil {
			return err
		}
		switch b {
		case 'T':
			err := state.TimeProtocol(in, out)
			if err != nil {
				return err
			}
		case 'C':
			return state.FileProtocol(in, out)
		case 'E':
			state.dir = filepath.Dir(state.dir)
			fmt.Fprintf(out, scpOK)
			return nil
		case 'D':
			return state.DirProtocol(in, out)
		default:
			fmt.Fprintf(out, scpEmptyError)
			return fmt.Errorf("unexpected message: %c", b)
		}
	}
}
Example #9
0
func peek(r *bufio.Reader) (c byte, e error) {
	c, e = r.ReadByte()
	if e != io.EOF {
		r.UnreadByte()
	}
	return
}
Example #10
0
func newLexer(in *bufio.Reader) *lexer {
	l := &lexer{src: in, line: 1}
	if b, err := in.ReadByte(); err == nil {
		l.current = b
	}
	return l
}
Example #11
0
func readArgument(reader *bufio.Reader) ([]byte, error) {
	line, err := reader.ReadString('\n')
	if err != nil {
		return nil, Malformed("$<ArgumentLength>", line)
	}

	var argLength int
	if _, err := fmt.Sscanf(line, "$%d\r", &argLength); err != nil {
		return nil, Malformed("$<ArgumentLength>", line)
	}

	data, err := ioutil.ReadAll(io.LimitReader(reader, int64(argLength)))
	if err != nil {
		return nil, err
	}
	if len(data) != argLength {
		return nil, MalformedLength(argLength, len(data))
	}
	if b, err := reader.ReadByte(); err != nil || b != '\r' {
		return nil, MalformedMissingCRLF()
	}
	if b, err := reader.ReadByte(); err != nil || b != '\n' {
		return nil, MalformedMissingCRLF()
	}

	return data, nil
}
Example #12
0
// Reads a single word from a file, assuming space + tab + EOL to be word boundaries
func ReadWord(fin *bufio.Reader) (word string, err error) {
	var a int = 0
	var ch byte
	var buf bytes.Buffer
	for {
		ch, err = fin.ReadByte()
		if err == io.EOF {
			break
		}
		if ch == 13 {
			continue
		}
		if (ch == ' ') || (ch == '\t') || (ch == '\n') {
			if a > 0 {
				if ch == '\n' {
					fin.UnreadByte()
				}
				break
			}
			if ch == '\n' {
				word = "</s>"
				return
			} else {
				continue
			}
		}
		buf.WriteByte(ch)
		a++
	}
	if a >= MAX_STRING { // Truncate too long words
		buf.Truncate(MAX_STRING)
	}
	word = buf.String()
	return
}
Example #13
0
func (g *CGIRefVar) RefByte(strand int, ref_stream *bufio.Reader) (byte, error) {

	if strand <= 0 {
		ref_bp, e := ref_stream.ReadByte()
		if e != nil {
			return ref_bp, e
		}
		for (ref_bp == '\n') || (ref_bp == ' ') || (ref_bp == '\r') || (ref_bp == '\t') {
			ref_bp, e = ref_stream.ReadByte()
			if e != nil {
				return ref_bp, e
			}
		}
		g.RefBuf = append(g.RefBuf, ref_bp)
		g.RefPos[0]++
		return ref_bp, e
	}

	if g.RefPos[1] >= len(g.RefBuf) {
		panic(fmt.Sprintf("g.RefPos[1] %d >= len(g.RefBuf) %d (RefPos:%v, RefBuf:%s, line %d '%s')", g.RefPos[1], len(g.RefBuf), g.RefPos, g.RefBuf, g.LCounter, g.CGIVarLine))
	}

	ref_bp := g.RefBuf[g.RefPos[1]]
	g.RefPos[1]++

	return ref_bp, nil

}
Example #14
0
func Decode(input *bufio.Reader) (_ string, err error) {
	buf := bytes.NewBufferString("")
	for {
		b, err := input.ReadByte()
		if err != nil {
			break
		}
		if b != '=' {
			err = buf.WriteByte(b)
		} else {
			input.UnreadByte()
			charset, err := readCharset(input)
			if err != nil {
				return "", err
			}
			encoding, err := readEncoding(input)
			if err != nil {
				return "", err
			}
			(&Decoder{encoding, charset}).Decode(input, buf)
		}
	}
	if err == io.EOF {
		return buf.String(), nil
	}
	return buf.String(), err
}
Example #15
0
func groupLs(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) {

	if len(args) != 2 {
		fmt.Println("Wrong number of arguments for call to group-ls:")
		fmt.Println("Format should be: group-ls GID")
		return nil
	}

	err = w.WriteByte(23)
	w.Flush()
	if err != nil {
		return err
	}

	// send the group id
	w.WriteString(args[1] + "\n")
	w.Flush()

	// get success (1) or fail (2)
	success, _ := r.ReadByte()
	if success != 1 {
		fmt.Println("The group does not exist")
		return err
	}

	// get uuids of the group
	result, _ := r.ReadString('\n')
	result = strings.TrimSuffix(strings.TrimSpace(result), ",")

	fmt.Println("Members of Group " + args[1] + ": " + result)

	return err
}
Example #16
0
// ReadWhitespace will read from the reader until no whitespace is encountered
func ReadWhitespace(r *bufio.Reader) {
	c, e := peek(r)
	for e != io.EOF && isWhitespace(c) {
		r.ReadByte()
		c, e = peek(r)
	}
}
Example #17
0
// decodeBinary decodes BSON Binary element.
func decodeBinary(rd *bufio.Reader) (string, Binary, error) {
	// name
	name, err := readCstring(rd)
	if err != nil {
		return "", nil, err
	}

	// value
	dataLen, err := readInt32(rd)
	if err != nil {
		return "", nil, err
	}

	// discard subtype
	_, err = rd.ReadByte()
	if err != nil {
		return "", nil, err
	}
	b := make([]byte, dataLen)
	_, err = io.ReadFull(rd, b)
	if err != nil {
		return "", nil, err
	}
	return name, Binary(b), nil
}
Example #18
0
File: index.go Project: jbrukh/ggit
func parseIndexEntry(r *bufio.Reader) (entry *IndexEntry, err error) {
	var binEntry indexEntry
	err = binary.Read(r, ord, &binEntry)
	if err != nil {
		return nil, err
	}

	// TODO: what if it is corrupted and too long?
	name, e := r.ReadBytes(token.NUL)
	if e != nil {
		return nil, e
	}
	name = util.TrimLastByte(name) // get rid of NUL

	// don't ask me how I figured this out after
	// a 14 hour workday
	leftOver := 7 - (len(name)+6)%8
	for j := 0; j < leftOver; j++ {
		// TODO: read the bytes at once somehow
		if _, err = r.ReadByte(); err != nil {
			return nil, err
		}
	}

	// record the entry
	return toIndexEntry(&binEntry, string(name)), nil
}
Example #19
0
func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	// Send C0+C1
	err = bw.WriteByte(0x03)
	c1 := CreateRandomBlock(RTMP_SIG_SIZE)
	for i := 0; i < 8; i++ {
		c1[i] = 0
	}
	bw.Write(c1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C0+C1")
	// Read S0+S1+S2
	s0, err := br.ReadByte()
	CheckError(err, "Handshake() Read S0")
	if s0 != 0x03 {
		return errors.New(fmt.Sprintf("Handshake() Got S0: %x", s0))
	}
	s1 := make([]byte, RTMP_SIG_SIZE)
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S1")
	bw.Write(s1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C2")
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S2")
	return
}
Example #20
0
func preprocess(buf []byte, r *bufio.Reader) (int, error) {
	i := 0
	for c, n, err := r.ReadRune(); err == nil; c, n, err = r.ReadRune() {
		if i+n > len(buf) {
			// We don't have room so unread the rune and return.
			r.UnreadRune()
			return i, err
		}
		switch c {
		case '\x00':
			if len(buf)-1 < i+len(unknownRune) {
				copy(buf[i:len(unknownRune)], unknownRune)
			} else {
				// We don't have room so unread the rune and
				// return.
				r.UnreadRune()
				return i + n, err
			}
		case '\r':
			buf[i] = '\n'
			nxt, err := r.Peek(1)
			if err == nil && len(nxt) == 1 && nxt[0] == '\n' {
				r.ReadByte()
			}
		case '\f':
			buf[i] = '\n'
		default:
			copy(buf[i:i+n], []byte(string(c)))
		}
		i += n
	}
	return i, nil
}
Example #21
0
func readBulkStr(r *bufio.Reader) (Resp, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return Resp{}, err
	}
	size, err := strconv.ParseInt(string(b[1:len(b)-2]), 10, 64)
	if err != nil {
		return Resp{}, errParse
	}
	if size < 0 {
		return Resp{typ: Nil}, nil
	}
	total := make([]byte, size)
	b2 := total
	var n int
	for len(b2) > 0 {
		n, err = r.Read(b2)
		if err != nil {
			return Resp{}, err
		}
		b2 = b2[n:]
	}

	// There's a hanging \r\n there, gotta read past it
	trail := make([]byte, 2)
	for i := 0; i < 2; i++ {
		c, err := r.ReadByte()
		if err != nil {
			return Resp{}, err
		}
		trail[i] = c
	}

	return Resp{typ: BulkStr, val: total}, nil
}
Example #22
0
func readHexInt(r *bufio.Reader) (int, error) {
	n := 0
	i := 0
	var k int
	for {
		c, err := r.ReadByte()
		if err != nil {
			if err == io.EOF && i > 0 {
				return n, nil
			}
			return -1, err
		}
		k = hexbyte2int(c)
		if k < 0 {
			if i == 0 {
				return -1, errEmptyHexNum
			}
			r.UnreadByte()
			return n, nil
		}
		if i >= maxHexIntChars {
			return -1, errTooLargeHexNum
		}
		n = (n << 4) | k
		i++
	}
}
Example #23
0
func readMultiBulk(br *bufio.Reader) ([][]byte, error) {
	count, err := readInt(br)
	switch {
	case err != nil:
		return nil, err
	case count < 0:
		return nil, PROTOCOL_ERROR
	}

	data := make([][]byte, 0, count)
	for count > 0 {
		f, err := br.ReadByte()
		var b []byte
		switch f {
		case '$':
			b, err = readBulk(br)
			if err != nil {
				return nil, err
			}
		case ':':
			// coerce an integer into mere bytes
			b, err = readLine(br)
			if err != nil {
				return nil, err
			}
		default:
			return nil, PROTOCOL_ERROR
		}
		data = append(data, b)
		count--
	}

	return data, nil
}
Example #24
0
func (binaryNode *BinaryNode) unserialize(reader *bufio.Reader) (err error) {
	for {
		var what uint8
		if what, err = reader.ReadByte(); err != nil {
			// Most likely an EOF and we don't care.
			return nil
		}

		switch what {
		case NodeStart:
			var newNode BinaryNode
			if err = newNode.unserialize(reader); err != nil {
				return
			}

			binaryNode.children = append(binaryNode.children, newNode)
		case NodeEnd:
			return nil
		case EscapeChar:
			var b uint8
			if b, err = reader.ReadByte(); err != nil {
				return
			}

			binaryNode.data = append(binaryNode.data, b)
		default:
			binaryNode.data = append(binaryNode.data, what)
		}
	}

	return nil
}
Example #25
0
func readBulkString(r *bufio.Reader) (interface{}, error) {
	_, err := r.ReadByte()
	if err != nil {
		return EMPTY_STRING, err
	}
	countAsStr, err := ReadLine(r)
	if err != nil {
		return EMPTY_STRING, err
	}
	count, err := strconv.Atoi(countAsStr)
	if err != nil {
		return EMPTY_STRING, err
	}
	if count == -1 {
		return countAsStr, nil
	}
	line, err := ReadLine(r)
	if err != nil {
		return EMPTY_STRING, err
	}

	if len(line) != count {
		return EMPTY_STRING, errors.New("Expected " + countAsStr + " characters in string and got " + line)
	}
	return line, nil
}
Example #26
0
func _StringDecode(in *bufio.Reader, stringkeys bool) interface{} {
	assertNextByte(in, BYTE_STRING)

	str := bytes.Buffer{}
	escape := false
	for {
		character, err := in.ReadByte()
		if err == io.EOF {
			panic("Expected unescaped \\1 to end string! (Got EOF)")
		} else if escape {
			// handle escape condition for next character
			if character == 0x3 {
				str.WriteByte(0x0)
			} else {
				str.WriteByte(character)
			}
		} else if character == 0x2 {
			escape = true
		} else if character == BYTE_END {
			return str.String()

		} else {
			str.WriteByte(character)
		}
	}
	return str.String()
}
Example #27
0
func ReadTillEOL(reader *bufio.Reader, buf []byte) int {
	i := 0
	for {
		ch, err := reader.ReadByte()
		if err != nil {
			return -1
		}
		if i >= len(buf) {
			return i
		}

		if ch == '\r' {
			ch, err := reader.ReadByte()
			if err != nil {
				return -1
			}
			if ch == '\n' {
				return i
			} else {
				buf[i] = '\r'
				buf[i+1] = ch
				i += 1
			}
		} else {
			buf[i] = ch
		}
		i += 1
	}
	return i
}
Example #28
0
func deleteGroup(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) {

	if len(args) < 2 {
		fmt.Println("Not enough arguments for call to group-remove:")
		fmt.Println("Format should be: delete-group GID")
		return nil
	}

	err = w.WriteByte(24)
	w.Flush()
	if err != nil {
		return err
	}

	w.WriteString(args[1] + "\n")
	w.Flush()

	// get success (1) or fail (2)
	success, _ := r.ReadByte()
	if success != 1 {
		fmt.Println("You cannot remove this group. Does it exist/are you the owner?")
		return err
	}

	fmt.Println("Removed: " + args[1])

	return err
}
Example #29
0
func readHexInt(r *bufio.Reader) (int, error) {
	n := 0
	i := 0
	var k int
	for {
		c, err := r.ReadByte()
		if err != nil {
			if err == io.EOF && i > 0 {
				return n, nil
			}
			return -1, err
		}
		k = hexbyte2int(c)
		if k < 0 {
			if i == 0 {
				return -1, fmt.Errorf("cannot read hex num from empty string")
			}
			r.UnreadByte()
			return n, nil
		}
		if i >= maxHexIntChars {
			return -1, fmt.Errorf("cannot read hex num with more than %d digits", maxHexIntChars)
		}
		n = (n << 4) | k
		i++
	}
}
Example #30
0
func readLine(r *bufio.Reader) (fname string, types []string, data string, err error) {
	var line []byte
	for len(line) == 0 {
		var short bool
		line, short, err = r.ReadLine()
		if short {
			err = fmt.Errorf("line too long")
			return
		}
		if err != nil {
			return
		}
		line = bytes.TrimSpace(line)
	}

	b, err := r.ReadByte()
	if err == io.EOF || b != ' ' {
		r.UnreadByte()
		return parseLine(string(line))
	}
	for b == ' ' && err != io.EOF {
		r.UnreadByte()
		stub, short, err := r.ReadLine()
		if !short {
			err = fmt.Errorf("line too long")
			return fname, types, data, err
		}
		line = append(line, bytes.TrimSpace(stub)...)
		b, err = r.ReadByte()
	}
	r.UnreadByte()
	return parseLine(string(line))
}