// readHeader parses a packet header and returns an io.Reader which will return
// the contents of the packet. See RFC 4880, section 4.2.
429741
func readHeader(r io.Reader) (tag packetType, length int64, contents io.Reader, err error) {
	var buf [4]byte
	_, err = io.ReadFull(r, buf[:1])
	if err != nil {
		return
	}
	if buf[0]&0x80 == 0 {
		err = errors.StructuralError("tag byte does not have MSB set")
		return
	}
	if buf[0]&0x40 == 0 {
		// Old format packet
		tag = packetType((buf[0] & 0x3f) >> 2)
		lengthType := buf[0] & 3
		if lengthType == 3 {
			length = -1
			contents = r
			return
		}
		lengthBytes := 1 << lengthType
		_, err = readFull(r, buf[0:lengthBytes])
		if err != nil {
			return
		}
		for i := 0; i < lengthBytes; i++ {
			length <<= 8
			length |= int64(buf[i])
		}
		contents = &spanReader{r, length}
		return
	}

	// New format packet
	tag = packetType(buf[0] & 0x3f)
	length, isPartial, err := readLength(r)
	if err != nil {
		return
	}
	if isPartial {
		contents = &partialLengthReader{
			remaining: length,
			isPartial: true,
			r:         r,
		}
		length = -1
	} else {
		contents = &spanReader{r, length}
	}
	return
}
121870
func (d *Decoder) Header() []string {
	header := make([]string, len(d.header))
	copy(header, d.header)
	return header
}
165330
func (s *streamPacketCipher) readPacket(seqNum uint32, r io.Reader) ([]byte, error) {
	if _, err := io.ReadFull(r, s.prefix[:]); err != nil {
		return nil, err
	}

	s.cipher.XORKeyStream(s.prefix[:], s.prefix[:])
	length := binary.BigEndian.Uint32(s.prefix[0:4])
	paddingLength := uint32(s.prefix[4])

	var macSize uint32
	if s.mac != nil {
		s.mac.Reset()
		binary.BigEndian.PutUint32(s.seqNumBytes[:], seqNum)
		s.mac.Write(s.seqNumBytes[:])
		s.mac.Write(s.prefix[:])
		macSize = uint32(s.mac.Size())
	}

	if length <= paddingLength+1 {
		return nil, errors.New("ssh: invalid packet length, packet too small")
	}

	if length > maxPacket {
		return nil, errors.New("ssh: invalid packet length, packet too large")
	}

	// the maxPacket check above ensures that length-1+macSize
	// does not overflow.
	if uint32(cap(s.packetData)) < length-1+macSize {
		s.packetData = make([]byte, length-1+macSize)
	} else {
		s.packetData = s.packetData[:length-1+macSize]
	}

	if _, err := io.ReadFull(r, s.packetData); err != nil {
		return nil, err
	}
	mac := s.packetData[length-1:]
	data := s.packetData[:length-1]
	s.cipher.XORKeyStream(data, data)

	if s.mac != nil {
		s.mac.Write(data)
		s.macResult = s.mac.Sum(s.macResult[:0])
		if subtle.ConstantTimeCompare(s.macResult, mac) != 1 {
			return nil, errors.New("ssh: MAC failure")
		}
	}

	return s.packetData[:length-paddingLength-1], nil
}
767304
func (hc *HeaderChain) HasHeader(hash common.Hash, number uint64) bool {
	if hc.numberCache.Contains(hash) || hc.headerCache.Contains(hash) {
		return true
	}
	return rawdb.HasHeader(hc.chainDb, hash, number)
}
423641
func ParseHeader(s string) http.Header {
	h := http.Header{}
	lines := strings.Split(s, "\n")
	rx := regexp.MustCompile(`^([^\s+]+):\s*(.*)$`)
	for _, line := range lines {
		m := rx.FindStringSubmatch(line)
		if len(m) == 3 {
			key := strings.TrimSpace(m[1])
			val := strings.TrimSpace(m[2])
			if len(key) > 0 {
				h.Add(key, val)
			}
		}
	}
	return h
}
151530
func (t *Tag) ReadHeader(th TagHeader) error {
	// id3 tag ID
	if !th.IsValidID() {
		return ErrInvalidTagHeader
	}
	// version
	t.Header = &Header{
		Version: th.ReadVersion(),
		Flags:   th.ReadFlags(),
	}
	size, err := th.ReadSize()
	if err != nil {
		return err
	}
	t.Header.Size = size

	return nil
}
882414
func (c *UDPConnection) ReadPacket() *packet.Packet {
	select {
	case p := <-c.PacketQueue:
		return p
	default:
		return nil
	}
}
1277912
func readGroupHeader(fpr io.Reader) ([]byte, uint32, error) {
	return _readGroupHeader(fpr, false)
}
132934
func (d *Decoder) findHeader() (int, error) {
	header := uint32(0)
	consumed := 0

retry:
	for header&0xfff00000 != 0xfff00000 {
		b, err := d.stream.readByte()
		if err != nil {
			if err == io.EOF {
				if header&0xfff000 == 0xfff000 || header&0xfff0 == 0xfff0 {
					err = io.ErrUnexpectedEOF
				}
			}
			return 0, err
		}

		consumed++
		header = header<<8 | uint32(b)
	}

	// Free format headers have a high false positive rate, so we only accept
	// them under some fairly restricted circumstances.
	ffAllowed := consumed == 4 || d.layer != 0 && d.bitrateIndex == 0

	if !d.trueHeader(header, ffAllowed) {
		header &^= 0xff << 24
		goto retry
	}

	return int(header), nil
}
1092620
func readAuthHeaderV1(r io.Reader) (sender Identity, tstamp time.Time, payload []byte, err error) {

	// Read in the length of everything up to the payload length
	var allLen uint32
	if err = binary.Read(r, byteOrder, &allLen); err != nil {
		return
	}

	// Read the signature
	var sig = make([]byte, 256)
	if err = binary.Read(r, byteOrder, sig); err != nil {
		return
	}

	var (
		signable  bytes.Buffer
		remaining = allLen
	)

	// Read into a tee reader so we can verify the signature on the original
	// data later
	teed := io.TeeReader(r, &signable)

	// Read and validate the timestamp
	var ts timestamp
	remaining -= uint32(binary.Size(ts))
	if err = binary.Read(teed, byteOrder, &ts); err != nil {
		err = eatBytesAndGetPayloadError(teed, remaining, err)
		return
	}
	tstamp = time.Unix(int64(ts), 0).UTC()
	cutoff := jwt.TimeFunc().UTC().Add(-ClockDriftDelta)
	if tstamp.Before(cutoff) {
		err = eatBytesAndGetPayloadError(teed, remaining, ErrHeaderExpired)
		return
	}

	// Read the token and validate it
	token := make([]byte, int(remaining))
	if err = binary.Read(teed, byteOrder, &token); err != nil {
		err = eatBytesAndGetPayloadError(teed, 0, err)
		return
	}
	sender, err = ParseJWTIdentity(string(token))
	if err != nil {
		err = eatBytesAndGetPayloadError(teed, 0, err)
		return
	}
	if sender == nil {
		err = eatBytesAndGetPayloadError(teed, 0, ErrBadToken)
		return
	}

	payload, err = ReadLengthAndBytes(teed)
	if err != nil {
		return
	}

	// Freeze the signable content for verification. Even though we don't plan
	// to read from the tee reader anymore, might as well be clear as to our
	// intentions.
	signableContent := signable.Bytes()

	// Verify the signature of the signable content
	var verifier Verifier
	verifier, err = sender.Verifier()
	if err != nil {
		err = &AuthHeaderError{err, payload}
		return
	}
	err = verifier.Verify(signableContent, sig)
	if err != nil {
		err = &AuthHeaderError{err, payload}
	}
	return
}
889118
func (d *Decoder) readHeaders() error {
	// prevent the headers to be re-read
	if d.Version > 0 {
		return nil
	}
	var n int64
	size := 8 // 4 + 2 + 2
	src := bytes.NewBuffer(make([]byte, 0, size))
	n, d.err = io.CopyN(src, d.r, int64(size))
	if n < int64(size) {
		src.Truncate(int(n))
	}

	// format
	if _, d.err = src.Read(d.Format[:]); d.err != nil {
		return d.err
	}
	if d.Format != fileHeaderID {
		return fmt.Errorf("%s %s", string(d.Format[:]), ErrFmtNotSupported)
	}

	// version
	if d.err = binary.Read(src, binary.BigEndian, &d.Version); d.err != nil {
		return d.err
	}
	if d.Version > 1 {
		return fmt.Errorf("CAF v%d - %v", d.Version, ErrFmtNotSupported)
	}

	// The Audio Description chunk is required and must appear in a CAF file immediately following the file header. It describes the format of the audio data in the Audio Data chunk.
	cType, _, err := d.iDnSize()
	if err != nil {
		return err
	}
	if cType != StreamDescriptionChunkID {
		return fmt.Errorf("%s - Expected description chunk", ErrUnexpectedData)
	}
	if err := d.parseDescChunk(); err != nil {
		return err
	}

	return d.err
}
546197
func (c *IdentityClient) SetHeader(header *http.Header) {
	c.Client.RequestHeader = header
}
358392
func (p *Parser) parseHeader() (header, error) {
	hdr := header{}

	pri, err := p.parsePriority()
	if err != nil {
		return hdr, err
	}

	hdr.priority = pri

	ver, err := p.parseVersion()
	if err != nil {
		return hdr, err
	}
	hdr.version = ver
	p.cursor++

	ts, err := p.parseTimestamp()
	if err != nil {
		return hdr, err
	}

	hdr.timestamp = ts
	p.cursor++

	host, err := p.parseHostname()
	if err != nil {
		return hdr, err
	}

	hdr.hostname = host
	p.cursor++

	appName, err := p.parseAppName()
	if err != nil {
		return hdr, err
	}

	hdr.appName = appName
	p.cursor++

	procId, err := p.parseProcId()
	if err != nil {
		return hdr, nil
	}

	hdr.procId = procId
	p.cursor++

	msgId, err := p.parseMsgId()
	if err != nil {
		return hdr, nil
	}

	hdr.msgId = msgId
	p.cursor++

	return hdr, nil
}
190501
func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
	// Since readElements doesn't return the amount of bytes read, attempt
	// to read the entire header into a buffer first in case there is a
	// short read so the proper amount of read bytes are known.  This works
	// since the header is a fixed size.
	var headerBytes [MessageHeaderSize]byte
	n, err := io.ReadFull(r, headerBytes[:])
	if err != nil {
		return n, nil, err
	}
	hr := bytes.NewReader(headerBytes[:])

	// Create and populate a messageHeader struct from the raw header bytes.
	hdr := messageHeader{}
	var command [CommandSize]byte
	readElements(hr, &hdr.magic, &command, &hdr.length, &hdr.checksum)

	// Strip trailing zeros from command string.
	hdr.command = string(bytes.TrimRight(command[:], string(0)))

	return n, &hdr, nil
}
779083
func (z *Reader) readHeader() (hdr Header, err error) {
	if _, err = io.ReadFull(z.r, z.buf[:10]); err != nil {
		// RFC 1952, section 2.2, says the following:
		//	A gzip file consists of a series of "members" (compressed data sets).
		//
		// Other than this, the specification does not clarify whether a
		// "series" is defined as "one or more" or "zero or more". To err on the
		// side of caution, Go interprets this to mean "zero or more".
		// Thus, it is okay to return io.EOF here.
		return hdr, err
	}
	if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate {
		return hdr, ErrHeader
	}
	flg := z.buf[3]
	hdr.ModTime = time.Unix(int64(le.Uint32(z.buf[4:8])), 0)
	// z.buf[8] is XFL and is currently ignored.
	hdr.OS = z.buf[9]
	z.digest = crc32.ChecksumIEEE(z.buf[:10])

	if flg&flagExtra != 0 {
		if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil {
			return hdr, noEOF(err)
		}
		z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:2])
		data := make([]byte, le.Uint16(z.buf[:2]))
		if _, err = io.ReadFull(z.r, data); err != nil {
			return hdr, noEOF(err)
		}
		z.digest = crc32.Update(z.digest, crc32.IEEETable, data)
		hdr.Extra = data
	}

	var s string
	if flg&flagName != 0 {
		if s, err = z.readString(); err != nil {
			return hdr, err
		}
		hdr.Name = s
	}

	if flg&flagComment != 0 {
		if s, err = z.readString(); err != nil {
			return hdr, err
		}
		hdr.Comment = s
	}

	if flg&flagHdrCrc != 0 {
		if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil {
			return hdr, noEOF(err)
		}
		digest := le.Uint16(z.buf[:2])
		if digest != uint16(z.digest) {
			return hdr, ErrHeader
		}
	}

	z.digest = 0
	if z.decompressor == nil {
		z.decompressor = flate.NewReader(z.r)
	} else {
		z.decompressor.(flate.Resetter).Reset(z.r, nil)
	}
	return hdr, nil
}
1030852
func (c *Client) Read() (*Packet, *ethernet.Frame, error) {
	buf := make([]byte, 128)
	for {
		n, _, err := c.p.ReadFrom(buf)
		if err != nil {
			return nil, nil, err
		}

		p, eth, err := parsePacket(buf[:n])
		if err != nil {
			if err == errInvalidARPPacket {
				continue
			}
			return nil, nil, err
		}
		return p, eth, nil
	}
}
1060575
func (c *client) ReadResponse() (*Response, error) {
	version, code, msg, err := c.ReadStatusLine()
	var headers []Header
	if err != nil {
		return nil, fmt.Errorf("ReadStatusLine: %v", err)
	}
	for {
		var key, value string
		var done bool
		key, value, done, err = c.ReadHeader()
		if err != nil || done {
			break
		}
		if key == "" {
			// empty header values are valid, rfc 2616 s4.2.
			err = errors.New("invalid header")
			break
		}
		headers = append(headers, Header{key, value})
	}
	var resp = Response{
		Version: version,
		Status:  Status{code, msg},
		Headers: headers,
		Body:    c.ReadBody(),
	}
	if l := resp.ContentLength(); l >= 0 {
		resp.Body = io.LimitReader(resp.Body, l)
	} else if resp.TransferEncoding() == "chunked" {
		resp.Body = httputil.NewChunkedReader(resp.Body)
	}
	return &resp, err
}