Example #1
0
func readWhile(f func(r rune) bool, in *bufio.Reader) (read []byte, err error) {
	read = make([]byte, 0, 1)
	next := make([]byte, 1)
	var b byte
	next, _ = in.Peek(1)

	for len(next) > 0 && f(rune(next[0])) {
		b, err = in.ReadByte()
		if err != nil {
			return
		}
		if b == '\\' {
			b, err = in.ReadByte()
			actualByte, ok := escapedLetters[b]
			if ok {
				b = actualByte
			}
			if err != nil {
				return
			}
		}
		read = append(read, b)
		next, _ = in.Peek(1)
	}
	return
}
func (self ProtobufProbe) DeserializeByReader(reader *bufio.Reader) (*protocol.MobileSuiteModel, int32, error) {
	lengthByte, _ := reader.Peek(4)
	lengthBuff := bytes.NewBuffer(lengthByte)
	var length int32
	err := binary.Read(lengthBuff, binary.LittleEndian, &length)
	if err != nil {
		log.Error("when deserializeByReader:", err.Error())
		return nil, -1, err
	}

	if int32(reader.Buffered()) < length+4 {
		log.Error("int32(reader.Buffered()) < length + 4")
		return nil, -1, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		log.Error("when deserializeByReader:", err.Error())
		return nil, -1, err
	}
	var dst protocol.MobileSuiteModel
	var msgType int32
	msgType, err = self.Deserialize(pack, &dst)
	log.Debug(length, msgType, dst)
	return &dst, msgType, 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 IsXM(reader *bufio.Reader) bool {
	header, e := reader.Peek(17)
	if e != nil {
		return false
	}
	return bytes.Equal(header[0:17], xmHeader)
}
Example #5
0
func isEof(r *bufio.Reader) bool {
	_, err := r.Peek(1)
	if err == io.EOF {
		return true
	}
	return false
}
Example #6
0
func isID3Tag(reader *bufio.Reader) bool {
	data, err := reader.Peek(3)
	if len(data) < 3 || err != nil {
		return false
	}
	return data[0] == 'I' && data[1] == 'D' && data[2] == '3'
}
Example #7
0
func (u *udpgwPacket) read(r *bufio.Reader) error {
	bs, err := r.Peek(2)
	if nil != err {
		return err
	}
	u.length = binary.LittleEndian.Uint16(bs)
	//binary.Read(r, binary.BigEndian, &u.length)
	r.Discard(2)
	//log.Printf("###First %d  %d %d %p", u.length, binary.BigEndian.Uint16(bs), len(bs), r)
	_, err = r.Peek(int(u.length))
	if nil != err {
		//log.Printf("### %v", err)
		return err
	}
	bodylen := u.length
	binary.Read(r, binary.BigEndian, &u.flags)
	binary.Read(r, binary.BigEndian, &u.conid)
	bodylen -= 3
	if bodylen > 0 {
		if (u.flags & flagIPv6) != 0 {
			u.addr.ip = make(net.IP, 16)

		} else {
			u.addr.ip = make(net.IP, 4)
		}
		r.Read(u.addr.ip)
		bodylen -= uint16(len(u.addr.ip))
		binary.Read(r, binary.BigEndian, &u.addr.port)
		bodylen -= 2
		u.content = make([]byte, int(bodylen))
		r.Read(u.content)
	}
	return nil
}
Example #8
0
// Reads the attributes, including the '[' and ']' characters and the
// terminating line.
func readAtts(buf *bufio.Reader) (atts map[string]string, err error) {
	err = mustRead(buf, '[')
	if err != nil {
		return
	}

	atts = make(map[string]string)
	for {
		var p []byte
		p, err = buf.Peek(1)
		if err != nil {
			return
		}
		if p[0] == ']' {
			err = mustRead(buf, ']')
			if err == nil {
				err = mustRead(buf, '\n')
			}
			return
		}

		var key, value string
		key, err = readName(buf)
		if err != nil {
			return
		}
		value, err = readName(buf)
		if err != nil {
			return
		}

		atts[key] = value
	}
	panic("Not reached")
}
Example #9
0
func Unpack(reader *bufio.Reader) ([]byte, error) {

	return reader.ReadBytes('\n')
	//if global.PackSplitType =="breakline" {

	//	return reader.ReadBytes('\n')
	//}

	lengthByte, _ := reader.Peek(4)
	lengthBuff := bytes.NewBuffer(lengthByte)
	var length int32
	err := binary.Read(lengthBuff, binary.LittleEndian, &length)
	if err != nil {
		return nil, err
	}
	fmt.Println("Get length :", length)
	if int32(reader.Buffered()) < length+4 {
		return nil, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		return nil, err
	}
	return pack[4:], nil
}
Example #10
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 #11
0
func decodeIndex(rbuf *bufio.Reader, f *File) error {
	c, err := rbuf.Peek(len(blockIndex))
	if err != nil {
		if err == io.EOF {
			return nil
		}
		fmt.Printf("index-peek failed: %v\n", err)
		return err
	}
	if !bytes.Equal(c, blockIndex) {
		return nil
	}
	var ybuf []byte
	for {
		bline, err := rbuf.ReadBytes('\n')
		if err != nil {
			return err
		}
		ybuf = append(ybuf, bline...)
		if bytes.HasPrefix(bline, []byte("...")) {
			break
		}
	}
	err = yaml.Unmarshal(ybuf, &f.Index)
	return err
}
Example #12
0
File: decode.go Project: nf/ics
func decodeLine(r *bufio.Reader) (key, value string, err error) {
	var buf bytes.Buffer
	for {
		// get full line
		b, isPrefix, err := r.ReadLine()
		if err != nil {
			if err == io.EOF {
				err = io.ErrUnexpectedEOF
			}
			return "", "", err
		}
		if isPrefix {
			return "", "", errors.New("unexpected long line")
		}
		if len(b) == 0 {
			return "", "", errors.New("unexpected blank line")
		}
		if b[0] == ' ' {
			b = b[1:]
		}
		buf.Write(b)

		b, err = r.Peek(1)
		if err != nil || b[0] != ' ' {
			break
		}
	}
	p := strings.SplitN(buf.String(), ":", 2)
	if len(p) != 2 {
		return "", "", errors.New("bad line, couldn't find key:value")
	}
	return p[0], p[1], nil
}
Example #13
0
func chompFrontmatterEndComment(r *bufio.Reader) (err error) {
	candidate, err := r.Peek(32)
	if err != nil {
		return err
	}

	str := string(candidate)
	lineEnd := strings.IndexAny(str, "\n")
	if lineEnd == -1 {
		return nil
	}
	testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
	if strings.Index(testStr, HTMLCommentStart) != -1 {
		return nil
	}

	//TODO: if we can't find it, Peek more?
	if strings.HasSuffix(testStr, HTMLCommentEnd) {
		buf := make([]byte, lineEnd)
		if _, err = r.Read(buf); err != nil {
			return
		}
		if err = chompWhitespace(r); err != nil {
			return err
		}
	}

	return nil
}
Example #14
0
func mustPeekBuffered(r *bufio.Reader) []byte {
	buf, err := r.Peek(r.Buffered())
	if len(buf) == 0 || err != nil {
		panic(fmt.Sprintf("bufio.Reader.Peek() returned unexpected data (%q, %v)", buf, err))
	}
	return buf
}
Example #15
0
func (h *ResponseHeader) tryRead(r *bufio.Reader, n int) error {
	h.Reset()
	b, err := r.Peek(n)
	if len(b) == 0 {
		// treat all errors on the first byte read as EOF
		if n == 1 || err == io.EOF {
			return io.EOF
		}
		if err == bufio.ErrBufferFull {
			err = bufferFullError(r)
		}
		return fmt.Errorf("error when reading response headers: %s", err)
	}
	isEOF := (err != nil)
	b = mustPeekBuffered(r)
	var headersLen int
	if headersLen, err = h.parse(b); err != nil {
		if err == errNeedMore {
			if !isEOF {
				return err
			}

			// Buggy servers may leave trailing CRLFs after response body.
			// Treat this case as EOF.
			if isOnlyCRLF(b) {
				return io.EOF
			}
		}
		bStart, bEnd := bufferStartEnd(b)
		return fmt.Errorf("error when reading response headers: %s. buf=%q...%q", err, bStart, bEnd)
	}
	mustDiscard(r, headersLen)
	return nil
}
Example #16
0
func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
	h.Reset()
	b, err := r.Peek(n)
	if len(b) == 0 {
		// treat all errors on the first byte read as EOF
		if n == 1 || err == io.EOF {
			return io.EOF
		}
		return fmt.Errorf("error when reading request headers: %s", err)
	}
	isEOF := (err != nil)
	b = mustPeekBuffered(r)
	var headersLen int
	if headersLen, err = h.parse(b); err != nil {
		if err == errNeedMore {
			if !isEOF {
				return err
			}

			// Buggy clients may leave trailing CRLFs after the request body.
			// Treat this case as EOF.
			if isOnlyCRLF(b) {
				return io.EOF
			}
		}
		return fmt.Errorf("error when reading request headers: %s. buf=%q", err, b)
	}
	mustDiscard(r, headersLen)
	return nil
}
Example #17
0
func hasID3v2Tag(reader *bufio.Reader) bool {
	data, err := reader.Peek(3)
	if err != nil || len(data) < 3 {
		return false
	}
	return string(data) == "ID3"
}
Example #18
0
File: xz.go Project: wallyqs/util
func (c *XZDecompressor) Detect(br *bufio.Reader) bool {
	data, err := br.Peek(6)
	if err != nil {
		return false
	}
	return bytes.Equal(data, []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00})
}
Example #19
0
func (c *Bzip2Decompressor) Detect(br *bufio.Reader) bool {
	data, err := br.Peek(2)
	if err != nil {
		return false
	}
	return bytes.Equal(data, []byte{0x42, 0x5a})
}
Example #20
0
func readRawRecord(reader *bufio.Reader) (data []byte, length int, err error) {
	// parse rec_type and data length
	meta, err := reader.Peek(5)
	if err != nil {
		return
	}

	dataLength, offset, err := parseBase128Int(meta, 1)
	if err != nil {
		return
	}

	// read data
	n, err := skip(reader, offset)
	if err != nil {
		return
	}
	length += n

	data, n, err = read(reader, dataLength)
	if err != nil {
		return
	}
	length += n

	return
}
Example #21
0
func isBinaryRequest(reader *bufio.Reader) (bool, error) {
	headerByte, err := reader.Peek(1)
	if err != nil {
		return false, err
	}
	return int(headerByte[0]) == binprot.MAGIC_REQUEST, nil
}
func deserializeByReader(reader *bufio.Reader) (*pb.MobileSuiteProtobuf, error) {
	buff, _ := reader.Peek(4)
	data := bytes.NewBuffer(buff)
	var length int32
	err := binary.Read(data, binary.BigEndian, &length)
	if err != nil {
		return nil, err
	}
	fmt.Println(length)
	if int32(reader.Buffered()) < length+4 {
		return nil, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		return nil, err
	}
	msg := pack[4:]
	var dst pb.MobileSuiteProtobuf
	proto.Unmarshal(msg, &dst)
	fmt.Println(&dst)

	var testMsg pb.TestMessage
	proto.Unmarshal(dst.Message, &testMsg)
	fmt.Println(&testMsg)

	return &dst, nil
}
Example #23
0
// ParseFrom read from bufio.Reader and parse needle.
// used in scan block by bufio.Reader.
func (n *Needle) ParseFrom(rd *bufio.Reader) (err error) {
	var (
		dataOffset   int32
		footerOffset int32
		endOffset    int32
		data         []byte
	)
	// header
	if data, err = rd.Peek(_headerSize); err != nil {
		return
	}
	if err = n.parseHeader(data); err != nil {
		return
	}
	dataOffset = _headerSize
	footerOffset = dataOffset + n.Size
	endOffset = footerOffset + n.FooterSize
	// no discard, get all needle buffer
	if data, err = rd.Peek(int(n.TotalSize)); err != nil {
		return
	}
	if err = n.parseData(data[dataOffset:footerOffset]); err != nil {
		return
	}
	// footer
	if err = n.parseFooter(data[footerOffset:endOffset]); err != nil {
		return
	}
	n.buffer = data
	_, err = rd.Discard(int(n.TotalSize))
	return
}
Example #24
0
func socksPeekByte(r *bufio.Reader) (b byte, err error) {
	var byteSlice []byte
	if byteSlice, err = r.Peek(1); err != nil {
		return
	}
	b = byteSlice[0]
	return
}
Example #25
0
func detectMimeType(r *bufio.Reader) string {
	// If the server didn't tell us the content type, we will have to guess.
	// Note that I am purposefully ignoring the error from Peek.  The guesser looks
	// at up to 512 bytes.  We just get as many as we can and make our best
	// guess with that.
	data, _ := r.Peek(512)
	return http.DetectContentType(data)
}
Example #26
0
func objectShouldContinue(r *bufio.Reader) (bool, error) {
	b, err := r.Peek(len(ObjectEndSeq))
	if err != nil {
		return false, err
	}

	return !bytes.Equal(ObjectEndSeq, b), nil
}
Example #27
0
func peekByte(b *bufio.Reader) (byte, error) {
	bytes, err := b.Peek(1)
	if err != nil {
		return 0, err
	} else {
		return bytes[0], nil
	}
}
Example #28
0
File: index.go Project: kourge/ggit
func tryReadSha1(r *bufio.Reader) (sha1 core.Sha1, err error) {
	sha1Size := 20
	if tentativeSha1, _ := r.Peek(sha1Size + 1); len(tentativeSha1) == sha1Size {
		return core.Sha1FromByteSlice(tentativeSha1), nil
	} else {
		return core.Sha1{}, errSha1NotYetReached
	}
}
Example #29
0
func IsS3M(reader *bufio.Reader) bool {
	header, e := reader.Peek(48)
	if e != nil {
		return false
	}

	return binary.LittleEndian.Uint32(header[44:]) == 0x4d524353
}
Example #30
0
func checkNextByte(reader *bufio.Reader, b byte, g *byte) bool {
	nextByte, _ := reader.Peek(1)
	*g = nextByte[0]
	if *g != b {
		return false
	}
	return true
}