本文整理汇总了Golang中encoding/binary.Size函数的典型用法代码示例。如果您正苦于以下问题:Golang Size函数的具体用法?Golang Size怎么用?Golang Size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Size函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: ReadDataFrom
// ReadDataFrom reads a binary representation of the Hll data written
// by WriteDataTo() from io stream. It returns the number of bytes read
// and error.
// If serialized Hll configuration is different it returns error with expected params
func (h *HyperLogLog) ReadDataFrom(stream io.Reader) (int, error) {
var m uint64
// read register number first
err := binary.Read(stream, binary.LittleEndian, &m)
if err != nil {
return 0, err
}
// check if register number is appropriate
// hll register number should be same with serialized hll
if uint64(h.m) != m {
return 0, fmt.Errorf("expected hll register number %d", m)
}
// set other values
err = binary.Read(stream, binary.LittleEndian, &h.b)
if err != nil {
return 0, err
}
err = binary.Read(stream, binary.LittleEndian, &h.alpha)
if err != nil {
return 0, err
}
err = binary.Read(stream, binary.LittleEndian, h.registers)
if err != nil {
return 0, err
}
// count size of data in registers + m, b, alpha
size := int(h.m)*binary.Size(uint8(0)) + binary.Size(uint64(0)) + binary.Size(uint32(0)) + binary.Size(float64(0))
return size, err
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:37,代码来源:hyperloglog.go
示例17: 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
示例18: UnMarshal
func (d *Data) UnMarshal(buf []byte) error {
n := len(buf)
r := bytes.NewReader(buf)
err := binary.Read(r, binary.BigEndian, &d.Head)
if err != nil {
return err
}
d.SubData = []SubData{}
for i := binary.Size(d.Head); i < n; {
sub := SubData{}
err = binary.Read(r, binary.BigEndian, &sub.Head)
if err != nil {
return err
}
i += int(binary.Size(sub.Head))
sub.Params = []tlv.TLV{}
for j := 0; j < int(sub.Head.ParamsCount); j++ {
param := tlv.TLV{}
param.FromBinary(r)
i += int(param.Length())
sub.Params = append(sub.Params, param)
}
d.SubData = append(d.SubData, sub)
}
return nil
}
开发者ID:lg0491986,项目名称:pando-cloud,代码行数:27,代码来源:protocol.go
示例19: readBlockList
func (bli *blockListInterface) readBlockList(reader io.ReaderAt, start int64) (*blockListManager, error) {
var read int64
header := new(blockListHeaderData)
err := readFrom(reader, start, header)
if err != nil {
return nil, err
}
blm := new(blockListManager)
blm.header = header
blm.headerStart = start
read += int64(binary.Size(header))
for i := int64(0); i < blm.header.Size; i++ {
data := new(blockListArrayEntryData)
err := readFrom(reader, start+read, data)
if err != nil {
return blm, err
}
info := blockListInfo{data, start + read}
if data.Size == 0 {
bli.Freeentries.PushBack(&info)
} else if data.Free > 0 {
bli.Freeentries.PushBack(&info)
}
bli.BlockListInfos[info.Location] = &info
read += int64(binary.Size(data))
}
return blm, nil
}
开发者ID:finder,项目名称:gokvlite,代码行数:33,代码来源:filemanager.go
示例20: newFile
func newFile(path string) (*os.File, *blockListInterface, error) {
//This function creates a new file and writes out the header and initial block list
bli := new(blockListInterface)
bli.BlockListInfos = make(map[int64]*blockListInfo)
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.FileMode(0666))
if err != nil {
return file, nil, err
}
bli.file = file
header := fileHeaderData{0, 0}
bli.fileheader = &header
manager, written, err := bli.newBlockList(file, int64(binary.Size(header)), freeBlockSize)
if err != nil {
return file, bli, err
}
bli.Blocklists.PushBack(manager)
header.Freeblock_start = int64(binary.Size(header))
header.Data_start = header.Freeblock_start + written
err = writeTo(file, 0, header)
return file, bli, err
}
开发者ID:finder,项目名称:gokvlite,代码行数:25,代码来源:filemanager.go
注:本文中的encoding/binary.Size函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论