本文整理汇总了Golang中encoding/binary.Read函数的典型用法代码示例。如果您正苦于以下问题:Golang Read函数的具体用法?Golang Read怎么用?Golang Read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Read函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: convertIPPDateToTime
// convertIPPDateToTime converts an RFC 2579 date to a time.Time object.
func convertIPPDateToTime(date *C.ipp_uchar_t) time.Time {
r := bytes.NewReader(C.GoBytes(unsafe.Pointer(date), 11))
var year uint16
var month, day, hour, min, sec, dsec uint8
binary.Read(r, binary.BigEndian, &year)
binary.Read(r, binary.BigEndian, &month)
binary.Read(r, binary.BigEndian, &day)
binary.Read(r, binary.BigEndian, &hour)
binary.Read(r, binary.BigEndian, &min)
binary.Read(r, binary.BigEndian, &sec)
binary.Read(r, binary.BigEndian, &dsec)
var utcDirection, utcHour, utcMin uint8
binary.Read(r, binary.BigEndian, &utcDirection)
binary.Read(r, binary.BigEndian, &utcHour)
binary.Read(r, binary.BigEndian, &utcMin)
var utcOffset time.Duration
utcOffset += time.Duration(utcHour) * time.Hour
utcOffset += time.Duration(utcMin) * time.Minute
var loc *time.Location
if utcDirection == '-' {
loc = time.FixedZone("", -int(utcOffset.Seconds()))
} else {
loc = time.FixedZone("", int(utcOffset.Seconds()))
}
nsec := int(dsec) * 100 * int(time.Millisecond)
return time.Date(int(year), time.Month(month), int(day), int(hour), int(min), int(sec), nsec, loc)
}
开发者ID:TechEdge01,项目名称:cups-connector,代码行数:32,代码来源:cups.go
示例2: ReadNewInvalidState
func ReadNewInvalidState(in io.Reader) *NewInvalidState {
var reason, gameMode byte
binary.Read(in, ByteOrder, &reason)
binary.Read(in, ByteOrder, &gameMode)
ptr := NewInvalidState{reason, gameMode}
return &ptr
}
开发者ID:mvm,项目名称:ChinoFarmer,代码行数:7,代码来源:net.go
示例3: ReadFrom
func (e *indexEntry) ReadFrom(r io.Reader) (n int64, err error) {
var deleted byte
if err = binary.Read(r, binary.BigEndian, &deleted); err != nil {
return 0, err
}
e.deleted = (deleted != 0)
var value_len uint32
if err = binary.Read(r, binary.BigEndian, &value_len); err != nil {
return 0, err
}
value := make([]byte, int(value_len))
for i := 0; i < int(value_len); i++ {
var b byte
if err = binary.Read(r, binary.BigEndian, &b); err != nil {
return 0, err
}
value[i] = b
}
e.value = string(value)
var id int64
if err = binary.Read(r, binary.BigEndian, &id); err != nil {
return 0, err
}
e.id = id
return int64(binary.Size(deleted) + binary.Size(value) + binary.Size(id)), nil
}
开发者ID:screscent,项目名称:epos,代码行数:30,代码来源:index.go
示例4: ReadTimestampedEntryInto
// ReadTimestampedEntryInto parses the byte-stream representation of a
// TimestampedEntry from |r| and populates the struct |t| with the data. See
// RFC section 3.4 for details on the format.
// Returns a non-nil error if there was a problem.
func ReadTimestampedEntryInto(r io.Reader, t *TimestampedEntry) error {
var err error
if err = binary.Read(r, binary.BigEndian, &t.Timestamp); err != nil {
return err
}
if err = binary.Read(r, binary.BigEndian, &t.EntryType); err != nil {
return err
}
switch t.EntryType {
case X509LogEntryType:
if t.X509Entry, err = readVarBytes(r, CertificateLengthBytes); err != nil {
return err
}
case PrecertLogEntryType:
if err := binary.Read(r, binary.BigEndian, &t.PrecertEntry.IssuerKeyHash); err != nil {
return err
}
if t.PrecertEntry.TBSCertificate, err = readVarBytes(r, PreCertificateLengthBytes); err != nil {
return err
}
case XJSONLogEntryType:
if t.JSONData, err = readVarBytes(r, JSONLengthBytes); err != nil {
return err
}
default:
return fmt.Errorf("unknown EntryType: %d", t.EntryType)
}
t.Extensions, err = readVarBytes(r, ExtensionsLengthBytes)
return nil
}
开发者ID:jfrazelle,项目名称:cfssl,代码行数:34,代码来源:serialization.go
示例5: readStats
func readStats(r io.Reader, typ string) (*ReferenceStats, error) {
var (
vOff uint64
stats ReferenceStats
err error
)
err = binary.Read(r, binary.LittleEndian, &vOff)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats chunk begin virtual offset: %v", typ, err)
}
stats.Chunk.Begin = makeOffset(vOff)
err = binary.Read(r, binary.LittleEndian, &vOff)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats chunk end virtual offset: %v", typ, err)
}
stats.Chunk.End = makeOffset(vOff)
err = binary.Read(r, binary.LittleEndian, &stats.Mapped)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats mapped count: %v", typ, err)
}
err = binary.Read(r, binary.LittleEndian, &stats.Unmapped)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats unmapped count: %v", typ, err)
}
return &stats, nil
}
开发者ID:pombredanne,项目名称:hts,代码行数:26,代码来源:index_read.go
示例6: handleEventAll
func (w *Watcher) handleEventAll(data []byte) {
buf := bytes.NewBuffer(data)
msg := &cnMsg{}
hdr := &procEventHeader{}
binary.Read(buf, byteOrder, msg)
binary.Read(buf, byteOrder, hdr)
switch hdr.What {
case PROC_EVENT_FORK:
event := &forkProcEvent{}
binary.Read(buf, byteOrder, event)
ppid := int(event.ParentTgid)
pid := int(event.ChildTgid)
w.Fork <- &ProcEventFork{ParentPid: ppid, ChildPid: pid}
case PROC_EVENT_EXEC:
event := &execProcEvent{}
binary.Read(buf, byteOrder, event)
pid := int(event.ProcessTgid)
w.Exec <- &ProcEventExec{Pid: pid}
case PROC_EVENT_EXIT:
event := &exitProcEvent{}
binary.Read(buf, byteOrder, event)
pid := int(event.ProcessTgid)
w.Exit <- &ProcEventExit{Pid: pid}
}
}
开发者ID:cloudimmunity,项目名称:pdiscover,代码行数:30,代码来源:pevents_linux.go
示例7: Parse
// Parse initialises the read loop and begins parsing the incoming request
func (p *Parser) Parse() {
b := make([]byte, 2)
Read:
for {
n, err := p.Conn.Read(b)
switch {
case err == io.EOF:
break Read
case n == 0:
goto Read
}
switch string(b) {
case "1W": // window length
binary.Read(p.Conn, binary.BigEndian, &p.wlen)
case "1C": // frame length
binary.Read(p.Conn, binary.BigEndian, &p.plen)
if err := p.read(); err != nil {
log.Printf("[%s] error parsing %v", p.Conn.RemoteAddr().String(), err)
break Read
}
if err := p.ack(); err != nil {
log.Printf("[%s] error acking %v", p.Conn.RemoteAddr().String(), err)
break Read
}
default:
// This really shouldn't happen
log.Printf("[%s] Received unknown type", p.Conn.RemoteAddr().String(), err)
break Read
}
}
}
开发者ID:stanhu,项目名称:logslam,代码行数:35,代码来源:parser.go
示例8: handleHead
func handleHead(reader io.Reader) (seq []byte, flags uint8, count uint16, err error) {
var seqLength uint16
err = binary.Read(reader, binary.BigEndian, &seqLength)
if err != nil {
return
}
seq = make([]byte, seqLength)
_, err = io.ReadFull(reader, seq)
if err != nil {
fmt.Println("read seq")
return
}
err = binary.Read(reader, binary.BigEndian, &flags)
if err != nil {
fmt.Println("read flags")
return
}
err = binary.Read(reader, binary.BigEndian, &count)
if err != nil {
fmt.Println("read count")
return
}
return
}
开发者ID:badfortrains,项目名称:spotcontrol,代码行数:26,代码来源:mercury.go
示例9: Decode
// Decode fills the packet withinformation of the packet.
func (r *wireMasterResponse) Decode(packet io.Reader, n int) error {
if r.Ips == nil {
r.Ips = make([]wireIP, 0, 50)
}
err := binary.Read(packet, byteOrder, &r.Head)
if err != nil {
return err
}
if !reflect.DeepEqual(r.Head.Magic, masterResponseHeader) {
return errors.New("Header does not match.")
}
remaining := n - binary.Size(r.Head.Magic)
ipsize := binary.Size(wireIP{})
for ; remaining >= ipsize; remaining -= ipsize {
ip := wireIP{}
// Normal little endian read.
if err := binary.Read(packet, byteOrder, &ip.Oct); err != nil {
return err
}
// Seperate read because of big endian requirement
if err := binary.Read(packet, binary.BigEndian, &ip.Port); err != nil {
return err
}
r.Ips = append(r.Ips, ip)
}
return nil
}
开发者ID:hfern,项目名称:goseq,代码行数:33,代码来源:master.go
示例10: Parse
func Parse(d []byte) (*Key, error) {
if len(d) != 78 {
return nil, fmt.Errorf("Input of wrong length %d (expected 78).", len(d))
}
b := bytes.NewBuffer(d)
var version uint32
binary.Read(b, binary.BigEndian, &version)
k := Key{version: version}
binary.Read(b, binary.BigEndian, &k.depth)
k.parent = make([]byte, 4)
b.Read(k.parent)
binary.Read(b, binary.BigEndian, &k.index)
k.code = make([]byte, 32)
b.Read(k.code)
ser := make([]byte, 33)
b.Read(ser)
switch version {
case BitcoinExtendedPrivateKeyVersion, BitcoinTestnetExtendedPrivateKeyVersion:
k.prvKey = parse256(ser[1:33])
case BitcoinExtendedPublicKeyVersion, BitcoinTestnetExtendedPublicKeyVersion:
k.pubKey = ser
default:
return nil, fmt.Errorf("Input has unrecognized version %x.", version)
}
return &k, nil
}
开发者ID:runeaune,项目名称:hdkeys,代码行数:27,代码来源:hd.go
示例11: makeUniqueMcs
func makeUniqueMcs(catRange uint32) (string, error) {
var (
n uint32
c1, c2 uint32
mcs string
tries = 1000000
err error
)
for i := 0; i < tries; i++ {
binary.Read(rand.Reader, binary.LittleEndian, &n)
c1 = n % catRange
binary.Read(rand.Reader, binary.LittleEndian, &n)
c2 = n % catRange
if c1 == c2 {
continue
}
if c1 > c2 {
c1, c2 = c2, c1
}
mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
err = mcsAdd(mcs)
if err == nil {
return mcs, nil
}
}
return "", fmt.Errorf("couldn't generate unique MCS after %d tries! (last err=%v)", tries, err)
}
开发者ID:intelsdi-x,项目名称:rkt,代码行数:28,代码来源:selinux.go
示例12: readProtoMessage
// Read a protobuf message from a client
func (client *Client) readProtoMessage() (msg *Message, err error) {
var (
length uint32
kind uint16
)
// Read the message type (16-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &kind)
if err != nil {
return
}
// Read the message length (32-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &length)
if err != nil {
return
}
buf := make([]byte, length)
_, err = io.ReadFull(client.reader, buf)
if err != nil {
return
}
msg = &Message{
buf: buf,
kind: kind,
client: client,
}
return
}
开发者ID:carriercomm,项目名称:grumble,代码行数:33,代码来源:client.go
示例13: readVendorID
func readVendorID(b []byte) (leftovers []byte, vendorID uint16, err error) {
buf := bytes.NewBuffer(b)
leftovers = b
var openByte, appByte, closeByte byte
if openByte, err = buf.ReadByte(); openByte != 0x3e {
return
}
if appByte, err = buf.ReadByte(); appByte != 0x22 && appByte != 0x21 {
return
}
if appByte == 0x22 {
if err = binary.Read(buf, binary.BigEndian, &vendorID); err != nil {
return
}
} else {
var vendorIDByte byte
if err = binary.Read(buf, binary.BigEndian, &vendorIDByte); err != nil {
return
}
vendorID = uint16(vendorIDByte)
}
if closeByte, err = buf.ReadByte(); closeByte != 0x3f {
return
}
bytesRead := len(b) - buf.Len()
leftovers = b[bytesRead:]
return
}
开发者ID:awesome-security,项目名称:zgrab,代码行数:28,代码来源:objects.go
示例14: Read
func (pbp *PBP) Read(rc io.ReadCloser) error {
binary.Read(rc, binary.LittleEndian, &pbp.cookie)
if pbp.cookie == 0x464C457f {
fmt.Printf("File is an elf, converting to empty PBP")
bytes, _ := ioutil.ReadAll(rc)
pbp.data[6] = append([]byte{0x7f, 0x45, 0x4c, 0x46}[:], bytes...)
pbp.cookie = 0x50425000
pbp.version = 0x00010000
return nil
}
if pbp.cookie != 0x50425000 {
return errors.New("bad cookie")
}
binary.Read(rc, binary.LittleEndian, &pbp.version)
for i := 0; i < 8; i++ {
binary.Read(rc, binary.LittleEndian, &pbp.offsets[i])
}
for i := 0; i < 7; i++ {
pbp.data[i] = make([]byte, pbp.offsets[i+1]-pbp.offsets[i])
if len(pbp.data[i]) > 0 {
_, err := rc.Read(pbp.data[i])
if err != nil {
return err
}
}
}
var err error
pbp.data[7], err = ioutil.ReadAll(rc)
return err
}
开发者ID:hrydgard,项目名称:storeutil,代码行数:31,代码来源:pbp.go
示例15: UnpackageData
// 解析内部数据包
func UnpackageData(body []byte) (PkgHead, []byte, InnerPkgTail, error) {
p := bytes.NewReader(body)
head := PkgHead{}
tail := InnerPkgTail{}
var jsonStr []byte
err := binary.Read(p, binary.BigEndian, &head)
if err != nil {
return head, nil, tail, errors.New("read pkghead error!!!", err, body)
}
if int(head.PkgLen)+SIZEOF_INNERTAIL != len(body) {
return head, nil, tail, errors.New("data package len error!!!", head.PkgLen, len(body))
}
jsonStr = make([]byte, int(head.PkgLen)-SIZEOF_PKGHEAD)
if err := binary.Read(p, binary.BigEndian, &jsonStr); err != nil {
return head, nil, tail, errors.New("read pkgbody error!!!", err, body)
}
if err := binary.Read(p, binary.BigEndian, &tail); err != nil {
return head, nil, tail, errors.New("read pkgtail error!!!", err, body)
}
return head, jsonStr, tail, nil
}
开发者ID:wxaxiaoyao,项目名称:linux,代码行数:27,代码来源:comm.go
示例16: readPacket
// readPacket reads a packet from r.rd into r.buf. EOF is returned if
// r.rd.Read() returns it. Nothing is appended to r.buf if err != nil.
func (r *SecureReader) readPacket() error {
// 1. Read the packet header
hdr := &PacketHdr{}
if err := binary.Read(r.rd, binary.BigEndian, hdr); err != nil {
return err
}
// 2. Read the encrypted data
encData := make([]byte, hdr.Length)
if _, err := io.ReadFull(r.rd, encData); err != nil {
return err
}
// 3. Decrypt and verify the data
b, ok := box.OpenAfterPrecomputation(nil, encData, &hdr.Nonce, r.key)
if !ok {
return ErrFailedVerify
}
buf := bytes.NewReader(b)
// 4. Extract the sequence number and match it to the internal counter
var seq uint32
if err := binary.Read(buf, binary.BigEndian, &seq); err != nil {
return err
}
if seq != r.seq {
return ErrOutOfOrder
}
r.seq += 2
// Success! Note: this append will cause the reallocation of r.buf when
// it grows too much, and with that the old returned data will be discarded
r.buf = append(r.buf, b[len(b)-buf.Len():]...)
return nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:37,代码来源:secureconn.go
示例17: Unmarshal
// Unmarshals the message.
func (g *Goodbye) Unmarshal(frames ...[]byte) error {
if frames == nil {
return errors.New("Can't unmarshal empty message")
}
frame := frames[0]
frames = frames[1:]
buffer := bytes.NewBuffer(frame)
// Get and check protocol signature
var signature uint16
binary.Read(buffer, binary.BigEndian, &signature)
if signature != Signature {
return errors.New("invalid signature")
}
// Get message id and parse per message type
var id uint8
binary.Read(buffer, binary.BigEndian, &id)
if id != GoodbyeId {
return errors.New("malformed Goodbye message")
}
return nil
}
开发者ID:hintjens,项目名称:zccp,代码行数:27,代码来源:goodbye.go
示例18: Write
func (u *UDP) Write(b []byte) (n int, err error) {
buf := bytes.NewBuffer(b)
if err = binary.Read(buf, binary.BigEndian, &u.PortSrc); err != nil {
return
}
n += 2
if err = binary.Read(buf, binary.BigEndian, &u.PortDst); err != nil {
return
}
n += 2
if err = binary.Read(buf, binary.BigEndian, &u.Length); err != nil {
return
}
n += 2
if err = binary.Read(buf, binary.BigEndian, &u.Checksum); err != nil {
return
}
n += 2
if u.Length > 8 {
u.Data = make([]byte, u.Length-8)
}
if u.Length == 0 {
u.Data = make([]byte, buf.Len())
}
m, err := io.ReadFull(buf, u.Data)
n += m
return
}
开发者ID:postfix,项目名称:ogo,代码行数:28,代码来源:udp.go
示例19: uniqMcs
func uniqMcs(catRange uint32) string {
var (
n uint32
c1, c2 uint32
mcs string
)
for {
binary.Read(rand.Reader, binary.LittleEndian, &n)
c1 = n % catRange
binary.Read(rand.Reader, binary.LittleEndian, &n)
c2 = n % catRange
if c1 == c2 {
continue
} else {
if c1 > c2 {
t := c1
c1 = c2
c2 = t
}
}
mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
if err := mcsAdd(mcs); err != nil {
continue
}
break
}
return mcs
}
开发者ID:Altiscale,项目名称:runc,代码行数:29,代码来源:selinux.go
示例20: NewFS
func NewFS(r io.ReadSeeker) *FS {
bpb32 := &BPB32{}
err := binary.Read(r, binary.LittleEndian, bpb32)
if err != nil {
// TODO error handling
}
fs := &FS{bpb32, UnknownType, r}
t := fs.DetermineType()
switch t {
case FAT32:
fs.Type = FAT32
return fs
case FAT12, FAT16:
// reread the BPB, this time for the correct fs type
bpb16 := &BPB16{}
r.Seek(0, 0)
err := binary.Read(r, binary.LittleEndian, bpb16)
if err != nil {
// TODO error handling
}
bpb32 = &BPB32{bpb16.BPBBase, BPB32Base{0, 0, 0, 0, 0, 0, [12]byte{}}, bpb16.BPB16Base}
fs = &FS{bpb32, t, r}
}
return fs
}
开发者ID:dominikh,项目名称:go-vfat,代码行数:27,代码来源:vfat.go
注:本文中的encoding/binary.Read函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论