当前位置: 首页>>代码示例>>Golang>>正文


Golang binary.Size函数代码示例

本文整理汇总了Golang中encoding/binary.Size函数的典型用法代码示例。如果您正苦于以下问题:Golang Size函数的具体用法?Golang Size怎么用?Golang Size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: pwdCrypt

func (rad *RadClientType) pwdCrypt(auth, passwd []byte) ([]byte, error) {

	//padding the password if the password not multiple of 16 octets
	text := make([]byte, 16*int(math.Ceil(float64(binary.Size(passwd))/16.0)))
	copy(text, passwd)
	for i := binary.Size(passwd); i < len(text); i++ {
		text[i] = 0
	}
	if len(text) > 128 {
		return nil, errors.New("Password exceeds maxiums of 128 bytes")
	}

	var result = make([]byte, 0)
	last := make([]byte, len(auth))

	copy(last, auth)
	for len(text) > 0 {
		hash := md5.Sum(bytes.Join([][]byte{rad.secret, last}, nil))

		for i := range hash {
			result = append(result, hash[i]^text[i])
		}
		last = result[len(result)-16:]
		text = text[16:]
	}
	return result, nil
}
开发者ID:johndoe26,项目名称:btunnel-go,代码行数:27,代码来源:radiusauth.go

示例2: parseSFNT

func parseSFNT(r io.ReaderAt, headerOffset int64, table map[int64]Table) (SFNT, error) {
	header := new(SfntHeader)
	headerSize := int64(binary.Size(header))
	sr := io.NewSectionReader(r, headerOffset, headerSize)
	if err := binary.Read(sr, binary.BigEndian, header); err != nil {
		return nil, err
	}
	numTables := header.NumTables
	offsetTable := make([]OffsetEntry, numTables)
	sr = io.NewSectionReader(r, headerOffset+headerSize, int64(binary.Size(offsetTable)))
	if err := binary.Read(sr, binary.BigEndian, offsetTable); err != nil {
		return nil, err
	}
	tableMap := make(SFNT)
	for _, entry := range offsetTable {
		tag := entry.Tag.String()
		offset := int64(entry.Offset)
		size := int64(entry.Length)
		if v, ok := table[offset]; ok {
			tableMap[tag] = v
		} else {
			v = &DefaultTable{entry.Tag, io.NewSectionReader(r, offset, size)}
			table[offset] = v
			tableMap[tag] = v
		}
	}
	for _, p := range DefaultParser {
		for i, v := range tableMap {
			tableMap[i] = p.Parse(tableMap, v)
		}
	}
	return tableMap, nil
}
开发者ID:mandel59,项目名称:gofont,代码行数:33,代码来源:parse.go

示例3: parseOptionsTemplateFlowSet

func parseOptionsTemplateFlowSet(data []byte, header *FlowSetHeader) (interface{}, error) {
	var set OptionsTemplateFlowSet
	var t OptionsTemplateRecord

	set.Id = header.Id
	set.Length = header.Length

	buf := bytes.NewBuffer(data)
	headerLen := binary.Size(t.TemplateId) + binary.Size(t.ScopeLength) + binary.Size(t.OptionLength)
	for buf.Len() >= 4 { // Padding aligns to 4 byte boundary
		if buf.Len() < headerLen {
			return nil, errorMissingData(headerLen - buf.Len())
		}
		binary.Read(buf, binary.BigEndian, &t.TemplateId)
		binary.Read(buf, binary.BigEndian, &t.ScopeLength)
		binary.Read(buf, binary.BigEndian, &t.OptionLength)

		if buf.Len() < int(t.ScopeLength)+int(t.OptionLength) {
			return nil, errorMissingData(int(t.ScopeLength) + int(t.OptionLength) - buf.Len())
		}

		scopeCount := int(t.ScopeLength) / binary.Size(Field{})
		optionCount := int(t.OptionLength) / binary.Size(Field{})

		t.Scopes = parseFieldList(buf, scopeCount)
		t.Options = parseFieldList(buf, optionCount)

		set.Records = append(set.Records, t)
	}

	return set, nil
}
开发者ID:ttaanngg,项目名称:nf9packet,代码行数:32,代码来源:decode.go

示例4: readData0

func (this *DataManager) readData0(inId InIdType) (BigFileIndex, error) {
	var d0 BigFileIndex
	var err error
	filenoSize := uint32(binary.Size(d0.FileNo))
	offsetSize := uint32(binary.Size(d0.Offset))
	lengthSize := uint32(binary.Size(d0.Length))

	// read fileno
	pos := uint32(inId) * uint32(binary.Size(BigFileIndex{}))
	fileno, err := this.data0.ReadNum(pos, filenoSize)
	if err != nil {
		return d0, err
	}

	// read fileoffset
	pos += filenoSize
	offset, err := this.data0.ReadNum(pos, offsetSize)
	if err != nil {
		return d0, err
	}

	// read filelength
	pos += offsetSize
	length, err := this.data0.ReadNum(pos, lengthSize)
	if err != nil {
		return d0, err
	}

	d0.FileNo = uint8(fileno)
	d0.Offset = uint32(offset)
	d0.Length = uint32(length)

	return d0, nil
}
开发者ID:caohao2008,项目名称:goose,代码行数:34,代码来源:datamanager.go

示例5: parseTemplateFlowSet

func parseTemplateFlowSet(data []byte, header *FlowSetHeader) (interface{}, error) {
	var set TemplateFlowSet
	var t TemplateRecord

	set.Id = header.Id
	set.Length = header.Length

	buf := bytes.NewBuffer(data)
	headerLen := binary.Size(t.TemplateId) + binary.Size(t.FieldCount)

	for buf.Len() >= 4 { // Padding aligns to 4 byte boundary
		if buf.Len() < headerLen {
			return nil, errorMissingData(headerLen - buf.Len())
		}
		binary.Read(buf, binary.BigEndian, &t.TemplateId)
		binary.Read(buf, binary.BigEndian, &t.FieldCount)

		fieldsLen := int(t.FieldCount) * binary.Size(Field{})
		if fieldsLen > buf.Len() {
			return nil, errorMissingData(fieldsLen - buf.Len())
		}
		t.Fields = parseFieldList(buf, int(t.FieldCount))

		set.Records = append(set.Records, t)
	}
	return set, nil

}
开发者ID:ttaanngg,项目名称:nf9packet,代码行数:28,代码来源:decode.go

示例6: verifyBlobs

func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, rd io.ReadSeeker, packSize uint) {
	written := 0
	for _, l := range lengths {
		written += l
	}
	// header length
	written += binary.Size(uint32(0))
	// header
	written += len(lengths) * (binary.Size(pack.BlobType(0)) + binary.Size(uint32(0)) + backend.IDSize)
	// header crypto
	written += crypto.Extension

	// check length
	Equals(t, uint(written), packSize)

	// read and parse it again
	np, err := pack.NewUnpacker(k, rd)
	OK(t, err)
	Equals(t, len(np.Entries), len(bufs))

	for i, b := range bufs {
		e := np.Entries[i]
		Equals(t, b.id, e.ID)

		brd, err := e.GetReader(rd)
		OK(t, err)
		data, err := ioutil.ReadAll(brd)
		OK(t, err)

		Assert(t, bytes.Equal(b.data, data),
			"data for blob %v doesn't match", i)
	}
}
开发者ID:MirkoDziadzka,项目名称:restic,代码行数:33,代码来源:pack_test.go

示例7: readHeader

// readHeader reads the header at the end of rd. size is the length of the
// whole data accessible in rd.
func readHeader(rd io.ReaderAt, size int64) ([]byte, error) {
	hl, err := readHeaderLength(rd, size)
	if err != nil {
		return nil, err
	}

	if int64(hl) > size-int64(binary.Size(hl)) {
		return nil, errors.New("header is larger than file")
	}

	if int64(hl) > maxHeaderSize {
		return nil, errors.New("header is larger than maxHeaderSize")
	}

	buf := make([]byte, int(hl))
	n, err := rd.ReadAt(buf, size-int64(hl)-int64(binary.Size(hl)))
	if err != nil {
		return nil, errors.Wrap(err, "ReadAt")
	}

	if n != len(buf) {
		return nil, errors.New("not enough bytes read")
	}

	return buf, nil
}
开发者ID:ckemper67,项目名称:restic,代码行数:28,代码来源:pack.go

示例8: NewPVRTexture

func NewPVRTexture(r io.ReadSeeker) (*tisPvrTexture, error) {
	tex := tisPvrTexture{}
	var destLen uint32

	binary.Read(r, binary.LittleEndian, &destLen)

	zr, err := zlib.NewReader(r)
	if err != nil {
		return nil, err
	}
	uncompressed, err := ioutil.ReadAll(zr)
	data := bytes.NewReader(uncompressed)
	data.Seek(0, os.SEEK_SET)
	binary.Read(data, binary.LittleEndian, &tex.Header)
	log.Printf("Header: %+v %d\n", tex.Header, binary.Size(tex.Header))

	switch tex.Header.PixelFormatlo {
	case 0: // 2BPP RGB PVRTC
	case 1: // 2BPP RGBA PVRTC
	case 2: // 4BPP RGB PVRTC
	case 3: // 4BPP RGBA PVRTC
	case 7: // DXT1
		tex.decompressDXT1(uncompressed[binary.Size(tex.Header):])
	case 9: // DXT3
	case 11: // DXT5
	}

	return &tex, nil
}
开发者ID:MasterScrat,项目名称:bgfileformats,代码行数:29,代码来源:tis.go

示例9: WriteTo

func (e *indexEntry) WriteTo(w io.Writer) (n int64, err error) {
	deleted := byte(0)
	if e.deleted {
		deleted = 1
	}

	if err = binary.Write(w, binary.BigEndian, deleted); err != nil {
		return 0, err
	}

	value_len := uint32(len(e.value))
	if err = binary.Write(w, binary.BigEndian, value_len); err != nil {
		return 0, err
	}

	if err = binary.Write(w, binary.BigEndian, []byte(e.value)); err != nil {
		return 0, err
	}

	if err = binary.Write(w, binary.BigEndian, e.id); err != nil {
		return 0, err
	}

	return int64(binary.Size(deleted) + binary.Size([]byte(e.value)) + binary.Size(e.id)), nil
}
开发者ID:screscent,项目名称:epos,代码行数:25,代码来源:index.go

示例10: makeElfHdr

func (cc *CoreConvert) makeElfHdr() {
	var hdr elf.Header32
	var phdr elf.Prog32
	var shdr elf.Section32

	copy(hdr.Ident[:], elf.ELFMAG)
	hdr.Ident[elf.EI_CLASS] = byte(elf.ELFCLASS32)
	hdr.Ident[elf.EI_DATA] = byte(elf.ELFDATA2LSB)
	hdr.Ident[elf.EI_VERSION] = byte(elf.EV_CURRENT)
	hdr.Ident[elf.EI_OSABI] = byte(elf.ELFOSABI_NONE)
	hdr.Ident[elf.EI_ABIVERSION] = 0
	hdr.Ident[elf.EI_PAD] = 0
	hdr.Type = uint16(elf.ET_CORE)
	hdr.Machine = uint16(elf.EM_ARM)
	hdr.Version = uint32(elf.EV_CURRENT)
	hdr.Entry = 0
	hdr.Phoff = uint32(binary.Size(hdr))
	hdr.Shoff = 0
	hdr.Flags = 0
	hdr.Ehsize = uint16(binary.Size(hdr))
	hdr.Phentsize = uint16(binary.Size(phdr))
	hdr.Phnum = uint16(len(cc.phdrs))
	hdr.Shentsize = uint16(binary.Size(shdr))
	hdr.Shnum = 0
	hdr.Shstrndx = uint16(elf.SHN_UNDEF)

	cc.elfHdr = &hdr
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:28,代码来源:core_convert.go

示例11: readTlv

func (cc *CoreConvert) readTlv() (*CoreDumpTlv, error) {
	var tlv CoreDumpTlv

	tlv_buf := make([]byte, binary.Size(tlv))
	if tlv_buf == nil {
		return nil, util.NewNewtError("Out of memory")
	}

	cnt, err := cc.Source.Read(tlv_buf)
	if err == io.EOF {
		return nil, nil
	}
	if err != nil {
		return nil, util.NewNewtError(fmt.Sprintf("Error reading: %s",
			err.Error()))
	}
	if cnt == 0 {
		return nil, nil
	}
	if cnt != binary.Size(tlv) {
		return nil, util.NewNewtError("Short read")
	}

	tlv.Type = uint8(tlv_buf[0])
	tlv.pad = uint8(tlv_buf[1])
	tlv.Len = binary.LittleEndian.Uint16(tlv_buf[2:4])
	tlv.Off = binary.LittleEndian.Uint32(tlv_buf[4:8])

	return &tlv, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:30,代码来源:core_convert.go

示例12: NextData

// NextData returns the next data packet or an error.
func (r *reader) NextData() (raw RawData, err error) {
	var (
		length   uint16
		expected = binary.Size(raw)
		skipped  int
	)

	// Look for the beginning of a navdata packet as indicated by the payload
	// size. This is hacky and will break if parrot increases the payload size,
	// but unfortunately I've been unable with a better sync mechanism, including
	// a very fancy attempt to stop the aquisition, drain the tty buffer in
	// non-blocking mode, and then restart the aquisition. Better ideas are
	// welcome!
	//
	// BUG: Sometimes even this mechanism seems to fail, I suspect due to an odd
	// number of bytes being read if Read() is interrupted by a signal or
	// similar.
	for {
		if err = binary.Read(r.bufReader, binary.LittleEndian, &length); err != nil {
			return
		}
		if int(length) == expected {
			break
		}
		if skipped > expected*2 {
			err = fmt.Errorf("Failed to find payload. skipped=%d", skipped)
			return
		}
		skipped += binary.Size(length)
	}
	err = binary.Read(r.bufReader, binary.LittleEndian, &raw)
	return
}
开发者ID:patrickToca,项目名称:godrone,代码行数:34,代码来源:reader.go

示例13: ConvertToUTF8

func (t *TLK) ConvertToUTF8(w io.WriteSeeker) error {
	w.Seek(0, os.SEEK_SET)
	err := binary.Write(w, binary.LittleEndian, t.header)
	if err != nil {
		return err
	}
	curStringOffset := 0
	strArray := []string{}
	for i := 0; i < len(t.entries); i++ {
		str, err := t.String(i)
		if err != nil {
			return err
		}
		strArray = append(strArray, str)
		t.entries[i].Offset = uint32(curStringOffset)
		t.entries[i].Length = uint32(len(str))
		curStringOffset += int(t.entries[i].Length)
		w.Seek(int64(binary.Size(t.header)+binary.Size(t.entries[0])*i), os.SEEK_SET)
		err = binary.Write(w, binary.LittleEndian, t.entries[i])
		if err != nil {
			return err
		}
	}
	w.Seek(int64(t.header.StringOffset), os.SEEK_SET)
	for _, str := range strArray {
		w.Write([]byte(str))
	}

	return nil
}
开发者ID:MasterScrat,项目名称:bgfileformats,代码行数:30,代码来源:tlk.go

示例14: Peinit

func Peinit() {
	var l int

	switch Thearch.Thechar {
	// 64-bit architectures
	case '6':
		pe64 = 1

		l = binary.Size(&oh64)
		dd = oh64.DataDirectory[:]

		// 32-bit architectures
	default:
		l = binary.Size(&oh)

		dd = oh.DataDirectory[:]
	}

	PEFILEHEADR = int32(Rnd(int64(len(dosstub)+binary.Size(&fh)+l+binary.Size(&sh)), PEFILEALIGN))
	PESECTHEADR = int32(Rnd(int64(PEFILEHEADR), PESECTALIGN))
	nextsectoff = int(PESECTHEADR)
	nextfileoff = int(PEFILEHEADR)

	// some mingw libs depend on this symbol, for example, FindPESectionByName
	xdefine("__image_base__", SDATA, PEBASE)

	xdefine("_image_base__", SDATA, PEBASE)
}
开发者ID:klueska,项目名称:go-akaros,代码行数:28,代码来源:pe.go

示例15: flushIndex

func (w *Writer) flushIndex() error {
	w.trailer.DataIndexOffset = w.curOffset
	w.trailer.DataIndexCount = uint32(len(w.blocks))

	w.fp.Write(IndexMagic)
	w.curOffset += uint64(len(IndexMagic))

	for _, b := range w.blocks {
		if err := binary.Write(w.fp, binary.BigEndian, b.offset); err != nil {
			return err
		}
		w.curOffset += uint64(binary.Size(b.offset))

		if err := binary.Write(w.fp, binary.BigEndian, b.size); err != nil {
			return err
		}
		w.curOffset += uint64(binary.Size(b.size))

		if i, err := writeUvarint(w.fp, uint64(len(b.firstKeyBytes))); err != nil {
			return err
		} else {
			w.curOffset += uint64(i)
		}

		if i, err := w.fp.Write(b.firstKeyBytes); err != nil {
			return err
		} else {
			w.curOffset += uint64(i)
		}
	}

	return nil
}
开发者ID:jcdavis,项目名称:quiver,代码行数:33,代码来源:writer.go


注:本文中的encoding/binary.Size函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。