示例#1
0
func drain(r io.ByteReader, results chan int) {
	var i int
	for _, e := r.ReadByte(); e == nil; _, e = r.ReadByte() {
		i++
	}
	results <- i
}
示例#2
0
文件: utils.go 项目: scritchley/orc
func unrolledUnPack4(buffer []int64, offset, len int, r io.ByteReader) error {
	numHops := 2
	remainder := len % numHops
	endOffset := offset + len
	endUnroll := endOffset - remainder
	var val uint64
	for i := offset; i < endUnroll; i = i + numHops {
		byt, err := r.ReadByte()
		if err != nil {
			return err
		}
		val = uint64(byt)
		buffer[i] = int64((val >> 4) & 15)
		buffer[i+1] = int64(val & 15)
	}

	if remainder > 0 {
		startShift := 4
		byt, err := r.ReadByte()
		if err != nil {
			return err
		}
		val = uint64(byt)
		for i := endUnroll; i < endOffset; i++ {
			buffer[i] = int64((val >> uint64(startShift)) & 15)
			startShift -= 4
		}
	}
	return nil
}
示例#3
0
// eats a single token returning:
// a bool, if it is an xml processing instruction
// a tag and optional default namespace, if an element
// and an error
func eatToken(in io.ByteReader) (bool, string, string, error) {
	c, err := in.ReadByte()
	if err != nil {
		return false, "", "", ErrInvalid
	}
	switch c {
	default:
		valid, tag, ns := eatElement(in, c)
		if valid {
			return false, tag, ns, nil
		}
	case '!':
		// can be a comment or a DOCTYPE
		if eatComment(in) {
			return false, "", "", nil
		}
	case '?':
		valid, decl := eatPI(in)
		if valid {
			return decl, "", "", nil
		}
	case '<', '>', ' ', '\r', '\n', '\t':
	}
	return false, "", "", ErrInvalid
}
示例#4
0
func (i InstReadFromInput) Eval(t Tape, in io.ByteReader, out io.ByteWriter) {
	b, _ := in.ReadByte()
	if b == byte(0) {
		return
	}
	t.SetByte(b)
}
示例#5
0
// Assumes that even partial matches may not overlap. Stops at match or newline.
func ScanUntil(r io.ByteReader, target string, ignorespace bool) (scanned string, pos int, err error) {
	var i, j, k int
	var b byte
	buf := make([]byte, 0, len(target))
	pos = -1
	for {
		b, err = r.ReadByte()
		if err != nil {
			return
		} else if b == '\n' {
			return
		}
		switch target[j] {
		case b:
			buf = append(buf, b)
			j++
		default:
			if j > 0 && (b == ' ' || b == '\t') {
				buf = append(buf, b)
				k++
			} else {
				j = 0
			}
		}
		if j >= len(target) {
			scanned = string(buf)
			pos = i - j - k + 1
			return
		}
		i++
	}
	return
}
示例#6
0
func eatDOCTYPE(in io.ByteReader) bool {
	expect := make([]byte, 6)
	for i := range expect {
		c, err := in.ReadByte()
		if err != nil {
			return false
		}
		expect[i] = c
	}
	if string(expect) != "OCTYPE" {
		return false
	}
	var depth int
	for {
		c, err := in.ReadByte()
		if err != nil {
			return false
		}
		switch c {
		case '>':
			if depth == 0 {
				return true
			}
			depth--
		case '<':
			depth++
		}
	}
}
示例#7
0
// eats an element returning:
// a bool to signfify if a valid element
// the tag name
// the default (xmlns=) namespace.
func eatElement(in io.ByteReader, c byte) (bool, string, string) {
	buf := make([]byte, 32)
	var (
		err     error
		tag, ns string
		idx     int
	)
	for ; err == nil; c, err = in.ReadByte() {
		switch c {
		default:
			if idx >= len(buf) {
				cp := make([]byte, len(buf)*2)
				copy(cp, buf)
				buf = cp
			}
			buf[idx] = c
			idx++
		case '>', ' ', '\r', '\n', '\t':
			if tag == "" {
				tag = string(buf[:idx])
			} else if ns == "" {
				ns = extractNS(buf[:idx])
			}
			if c == '>' {
				return true, tag, ns
			}
			idx = 0
		}
	}
	return false, "", ""
}
示例#8
0
文件: utils.go 项目: scritchley/orc
func readBitPackedInts(buffer []int64, offset int, length int, bitSize int, r io.ByteReader) error {
	var bitsLeft int
	var current int
	for i := offset; i < (offset + length); i++ {
		var result int64
		bitsLeftToRead := bitSize
		for bitsLeftToRead > bitsLeft {
			result <<= uint64(bitsLeft)
			result |= int64(current & ((1 << uint64(bitsLeft)) - 1))
			bitsLeftToRead -= bitsLeft
			b, err := r.ReadByte()
			if err != nil {
				return err
			}
			current = int(b)
			bitsLeft = 8
		}
		// handle the left over bits
		if bitsLeftToRead > 0 {
			result <<= uint64(bitsLeftToRead)
			bitsLeft -= bitsLeftToRead
			result |= int64((current >> uint64(bitsLeft)) & ((1 << uint64(bitsLeftToRead)) - 1))
		}
		buffer[i] = result
	}
	return nil
}
示例#9
0
func (st *StringTable) Read(reader io.ByteReader) error {
	var (
		err           error
		currentString string = ""
		currentIndex  uint32 = 0
		b             byte
	)

	st.index = make(map[string]uint32)
	st.length, err = readMultiByteUint32(reader)

	if err == nil && st.length > 0 {
		st.content = make([]byte, st.length)

		var index uint32
		for index = 0; index < st.length; index++ {
			b, err = reader.ReadByte()
			if err != nil {
				break
			}

			st.content[index] = b
			if b > 0 {
				currentString += fmt.Sprintf("%c", b)
			} else {
				st.index[currentString] = currentIndex
				currentString = ""
				currentIndex = index + 1
			}
		}
	}

	return err
}
示例#10
0
文件: reader.go 项目: achanda/go
func readByte(r io.ByteReader) (byte, error) {
	b, err := r.ReadByte()
	if err == io.EOF {
		err = io.ErrUnexpectedEOF
	}
	return b, err
}
示例#11
0
// ReadBytes reads an encoded []byte from buf, returning the number of bytes
// read, and any read error encountered.
func ReadBytes(buf io.ByteReader) (ret []byte, n int, err error) {
	tmpBuf := bytes.Buffer{}
	acc := byte(0)
	for i := 0; i < ReadByteLimit; i++ {
		o := byte(0)
		if o, err = buf.ReadByte(); err != nil {
			return
		}
		n++

		b := o & 0xfe // user data
		m := uint(i % 8)

		if m == 0 {
			acc = b
		} else {
			// ignore err since bytes.Buffer.WriteByte can never return one.
			_ = tmpBuf.WriteByte(acc | (b >> (8 - m)))
			acc = (b << m)
		}

		if o&1 == 0 { // stop bit is 0
			ret = tmpBuf.Bytes()
			return
		}
	}
	err = ErrByteLimitExceeded
	return
}
示例#12
0
文件: cmp.go 项目: rkoesters/goblin
func cmp(br1, br2 io.ByteReader, f1, f2 string) {
	status := 0
	char := int64(0)
	line := int64(1)

	for {
		b1, e1 := br1.ReadByte()
		b2, e2 := br2.ReadByte()
		char++

		if e1 != nil || e2 != nil {
			if e1 == e2 {
				break
			}
			if e1 == io.EOF {
				fmt.Printf("EOF on %v\n", f1)
			}
			if e2 == io.EOF {
				fmt.Printf("EOF on %v\n", f2)
			}
			break
		}

		if b1 == '\n' {
			line++
		}

		if b1 != b2 {
			printDiff(f1, f2, char, line, b1, b2)
			status = 1
		}
	}
	os.Exit(status)
}
示例#13
0
func SkipLine(r io.ByteReader) error {
	for {
		if b, err := r.ReadByte(); err != nil || b == '\n' {
			return err
		}
	}
	return nil
}
示例#14
0
文件: aho.go 项目: yflau/ahocorasick
// match implements the internal logic for running a single match, reading
// in each byte of the ByteReader and traversing the internal AC tree to look
// for matches.
func (t *acTree) match(reader io.ByteReader, output chan Match) {
	m := t.Matcher()
	for b, err := reader.ReadByte(); err != io.EOF; b, err = reader.ReadByte() {
		for _, match := range m.Next(b) {
			output <- match
		}
	}
	close(output)
}
示例#15
0
文件: ebml.go 项目: jmptrader/jra-go
func more(r io.ByteReader, more byte) (x uint64, err error) {
	x = 0
	for more != 0 {
		var b byte
		if b, err = r.ReadByte(); err != nil {
			return
		}
		more--
		x += uint64(b) << (8 * more)
	}
	return
}
示例#16
0
文件: util.go 项目: dsnet/golib
// Performs like io.ReadFull, but uses a ByteReader instead.
func ByteReadFull(rd io.ByteReader, buf []byte) (cnt int, err error) {
	for cnt = 0; cnt < len(buf); cnt++ {
		buf[cnt], err = rd.ReadByte()
		if err == io.EOF && cnt > 0 {
			err = io.ErrUnexpectedEOF
		}
		if err != nil {
			return
		}
	}
	return
}
示例#17
0
文件: utils.go 项目: scritchley/orc
func readInts(buffer []int64, offset, len, bitSize int, r io.ByteReader) error {
	bitsLeft := 0
	current := 0
	switch bitSize {
	case 1:
		return unrolledUnPack1(buffer, offset, len, r)
	case 2:
		return unrolledUnPack2(buffer, offset, len, r)
	case 4:
		return unrolledUnPack4(buffer, offset, len, r)
	case 8:
		return unrolledUnPack8(buffer, offset, len, r)
	case 16:
		return unrolledUnPack16(buffer, offset, len, r)
	case 24:
		return unrolledUnPack24(buffer, offset, len, r)
	case 32:
		return unrolledUnPack32(buffer, offset, len, r)
	case 40:
		return unrolledUnPack40(buffer, offset, len, r)
	case 48:
		return unrolledUnPack48(buffer, offset, len, r)
	case 56:
		return unrolledUnPack56(buffer, offset, len, r)
	case 64:
		return unrolledUnPack64(buffer, offset, len, r)
	}

	for i := offset; i < (offset + len); i++ {
		var result int64
		bitsLeftToRead := bitSize
		for bitsLeftToRead > bitsLeft {
			result <<= uint64(bitsLeft)
			result |= int64(current & ((1 << uint64(bitsLeft)) - 1))
			bitsLeftToRead -= bitsLeft
			byt, err := r.ReadByte()
			if err != nil {
				return err
			}
			current = int(byt)
			bitsLeft = 8
		}
		// handle the left over bits
		if bitsLeftToRead > 0 {
			result <<= uint64(bitsLeftToRead)
			bitsLeft -= bitsLeftToRead
			result |= int64((current >> uint64(bitsLeft)) & ((1 << uint64(bitsLeftToRead)) - 1))
		}
		buffer[i] = result
	}

	return nil
}
示例#18
0
文件: key.go 项目: cronosun/buranv1
func encodeSingle(input io.ByteReader, output io.ByteWriter) (noMoreInput bool,
	err error) {
	byteOne, err := input.ReadByte()
	if err != nil {
		// No more to read
		return true, nil
	}
	if byteOne == 0x00 {
		// Special cases
		// Maybe special case
		nextByte, err := input.ReadByte()
		if err != nil {
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			return true, nil
		}
		if nextByte == 0x00 || nextByte == 0x01 {
			// Need to escape that
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(0x01)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(nextByte)
			if err != nil {
				return false, err
			}
		} else {
			// Ah, no, no special case
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(nextByte)
			if err != nil {
				return false, err
			}
		}
	} else {
		// Standard case
		err = output.WriteByte(byteOne)
		if err != nil {
			return false, err
		}
	}
	return false, nil
}
示例#19
0
文件: parser.go 项目: rubyist/gohat
func readString(r io.ByteReader) string {
	l := readUvarint(r)
	var by []byte
	for i := 0; i < int(l); i++ {
		b, err := r.ReadByte()
		if err != nil {
			fmt.Println("Error reading string:", err)
			os.Exit(1)
		}
		by = append(by, b)
	}
	return string(by)
}
示例#20
0
文件: utils.go 项目: scritchley/orc
func bytesToLongBE(r io.ByteReader, n int) (int64, error) {
	var out int64
	var val int64
	for n > 0 {
		n--
		byt, err := r.ReadByte()
		if err != nil {
			return 0, err
		}
		val = int64(byt)
		out |= val << uint64(n*8)
	}
	return out, nil
}
示例#21
0
文件: pack.go 项目: emil2k/go-git
func readOffset(r io.ByteReader) (uint64, error) {
	var offset uint64
	for {
		b, err := r.ReadByte()
		if err != nil {
			return 0, err
		}
		offset |= uint64(b & 0x7f)
		if b&0x80 == 0 {
			return offset, nil
		}
		offset = (offset + 1) << 7
	}
}
示例#22
0
文件: util.go 项目: dsnet/golib
func ByteCopyN(dst io.ByteWriter, src io.ByteReader, num int64) (cnt int64, err error) {
	var val byte
	for cnt = 0; cnt < num; cnt++ {
		if val, err = src.ReadByte(); err != nil {
			break
		}
		if err = dst.WriteByte(val); err != nil {
			break
		}
	}
	if cnt == num { // This is expected
		err = nil
	}
	return
}
示例#23
0
文件: util.go 项目: dsnet/golib
func ByteCopy(dst io.ByteWriter, src io.ByteReader) (cnt int64, err error) {
	var val byte
	for cnt = 0; true; cnt++ {
		if val, err = src.ReadByte(); err != nil {
			break
		}
		if err = dst.WriteByte(val); err != nil {
			break
		}
	}
	if err == io.EOF { // This is expected
		err = nil
	}
	return
}
示例#24
0
// ReadULeb128 reads and decodes an unsigned LEB128 value from a ByteReader to an unsigned int32 value. Returns the result as a uint32
func ReadULeb128(reader io.ByteReader) uint32 {
	var result uint32
	var ctr uint
	var cur byte = 0x80
	var err error
	for (cur&0x80 == 0x80) && ctr < 5 {
		cur, err = reader.ReadByte()
		if err != nil {
			panic(err)
		}
		result += uint32((cur & 0x7f)) << (ctr * 7)
		ctr++
	}
	return result
}
示例#25
0
func (h *Header) Read(reader io.ByteReader) error {
	var err error
	h.versionNumber, err = reader.ReadByte()
	if err == nil {
		h.publicIdentifier, err = readMultiByteUint32(reader)
		if err == nil {
			h.charSet, err = readMultiByteUint32(reader)
			if err == nil {
				h.charSetAsString, _ = GetCharsetStringByCode(h.charSet)
				err = h.stringTable.Read(reader)
			}
		}
	}

	return err
}
示例#26
0
文件: vlq.go 项目: bsm/go-vlq
// ReadUint reads an encoded unsigned integer from r and returns it as a uint64.
func ReadUint(r io.ByteReader) (uint64, error) {
	var x uint64
	for i := 0; ; i++ {
		b, err := r.ReadByte()
		if err != nil {
			return x, err
		}
		x = (x << 7) | uint64(b&0x7f)

		if b < 0x80 {
			return x, nil
		} else if i == 9 {
			return 0, overflow
		}
	}
}
示例#27
0
// interpret reads from r and writes to w. While reading any color escape codes detected
// are replaced by the result of calling subst with the escape code.
func interpret(r io.ByteReader, w io.Writer, subst func(s string) []byte) error {
	inEscape := false
	escape := &bytes.Buffer{}

	for {
		c, err := r.ReadByte()
		if err != nil {
			// EOF
			break
		}

		if inEscape {
			if rune(c) == '{' && escape.Len() == 0 {
				// False alarm: this was the sequence {{ which means the user wanted to
				// output {.
				_, err = w.Write([]byte("{"))
				escape.Reset()
				inEscape = false
			} else if rune(c) == '}' {
				_, err = w.Write(subst(escape.String()))
				escape.Reset()
				inEscape = false
			} else {
				escape.WriteByte(c)
				if MaxEscapeCodeLen > 0 && uint(escape.Len()) > MaxEscapeCodeLen {
					// Escape code too long
					w.Write([]byte("{"))
					_, err = w.Write(escape.Bytes())
					inEscape = false
				}
			}
		} else {
			if rune(c) == '{' {
				inEscape = true
			} else {
				_, err = w.Write([]byte{c})
			}
		}

		if err != nil {
			// Write error occurred
			return err
		}
	}

	return nil
}
示例#28
0
文件: utils.go 项目: scritchley/orc
func readVulong(r io.ByteReader) (int64, error) {
	var result int64
	var offset int
	b := int64(0x80)
	for (b & 0x80) != 0 {
		nb, err := r.ReadByte()
		if err != nil {
			return result, err
		}
		b = int64(nb)
		if b == -1 {
			return result, ErrEOFUnsignedVInt
		}
		result |= (b & 0x7f) << uint64(offset)
		offset += 7
	}
	return result, nil
}
示例#29
0
文件: varint.go 项目: 2thetop/go
// ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.
func ReadUvarint(r io.ByteReader) (uint64, error) {
	var x uint64
	var s uint
	for i := 0; ; i++ {
		b, err := r.ReadByte()
		if err != nil {
			return x, err
		}
		if b < 0x80 {
			if i > 9 || i == 9 && b > 1 {
				return x, overflow
			}
			return x | uint64(b)<<s, nil
		}
		x |= uint64(b&0x7f) << s
		s += 7
	}
}
示例#30
0
func decodeOffset(src io.ByteReader, steps int) (int, error) {
	b, err := src.ReadByte()
	if err != nil {
		return 0, err
	}
	var offset = int(b & 0x7f)
	for (b & 0x80) != 0 {
		offset += 1 // WHY?
		b, err = src.ReadByte()
		if err != nil {
			return 0, err
		}
		offset = (offset << 7) + int(b&0x7f)
	}
	// offset needs to be aware of the bytes we read for `o.typ` and `o.size`
	offset += steps
	return -offset, nil
}