func (bin *mBinaryProto) readVarint(reader io.Reader) (int64, error) { if br, ok := reader.(io.ByteReader); ok { return binary.ReadVarint(br) } br := newByteReader(reader, bin.readBuffer) return binary.ReadVarint(br) }
// 0. embedded video // 1. picture // 2. pictures // 3. title // 4. tags // 5. categories // 6. pornstars // 7. duration in seconds // 8. views // 9. upvotes // 10. downvotes func SaveRow(row []byte) { var video Video // Split the columns b := bytes.Split(row, []byte(`|`)) video.SourceName = "pornhub" video.Username = "******" // video.EmbedCode = EscapeStrings(string(b[0])) video.EmbedCode = EscapeString(string(b[0])) video.Picture = string(b[1]) video.Pictures = string(b[2]) video.Title = string(b[3]) video.Description = "Watch " + string(b[3]) video.Tags = string(b[4]) video.Categories = string(b[5]) video.Pornstars = string(b[6]) video.Duration, _ = binary.ReadVarint(bytes.NewBuffer(b[7])) video.ViewsCount, _ = binary.ReadVarint(bytes.NewBuffer(b[8])) video.Upvotes, _ = binary.ReadVarint(bytes.NewBuffer(b[9])) video.Downvotes, _ = binary.ReadVarint(bytes.NewBuffer(b[10])) video.CreatedAt = time.Now() video.UpdatedAt = time.Now() insertVideo(video) }
// UnmarshalBinary implements encoding.BinaryUnmarshaler. func (tr *TimeRange) UnmarshalBinary(buf []byte) error { r := bytes.NewReader(buf) first, err := binary.ReadVarint(r) if err != nil { return err } last, err := binary.ReadVarint(r) if err != nil { return err } tr.First = clientmodel.Timestamp(first) tr.Last = clientmodel.Timestamp(last) return nil }
func UnmarshalDeltaNodes(buf []byte, nodes []element.Node) ([]element.Node, error) { r := bytes.NewBuffer(buf) length, err := binary.ReadVarint(r) if err != nil { return nil, err } if int64(cap(nodes)) < length { nodes = make([]element.Node, length) } else { nodes = nodes[:length] } lastId := int64(0) for i := 0; int64(i) < length; i++ { id, err := binary.ReadVarint(r) if err != nil { return nil, err } id = lastId + id nodes[i].Id = id lastId = id } lastLong := int64(0) for i := 0; int64(i) < length; i++ { long, err := binary.ReadVarint(r) if err != nil { return nil, err } long = lastLong + long nodes[i].Long = IntToCoord(uint32(long)) lastLong = long } lastLat := int64(0) for i := 0; int64(i) < length; i++ { lat, err := binary.ReadVarint(r) if err != nil { return nil, err } lat = lastLat + lat nodes[i].Lat = IntToCoord(uint32(lat)) lastLat = lat } return nodes, nil }
func (t *Commit) Unmarshal(rr io.Reader) error { var wire byteReader var ok bool if wire, ok = rr.(byteReader); !ok { wire = bufio.NewReader(rr) } var b [24]byte var bs []byte bs = b[:12] if _, err := io.ReadAtLeast(wire, bs, 12); err != nil { return err } t.LeaderId = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.Replica = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24))) t.Instance = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24))) alen1, err := binary.ReadVarint(wire) if err != nil { return err } t.Command = make([]state.Command, alen1) for i := int64(0); i < alen1; i++ { t.Command[i].Unmarshal(wire) } bs = b[:24] if _, err := io.ReadAtLeast(wire, bs, 24); err != nil { return err } t.Seq = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.Deps[0] = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24))) t.Deps[1] = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24))) t.Deps[2] = int32((uint32(bs[12]) | (uint32(bs[13]) << 8) | (uint32(bs[14]) << 16) | (uint32(bs[15]) << 24))) t.Deps[3] = int32((uint32(bs[16]) | (uint32(bs[17]) << 8) | (uint32(bs[18]) << 16) | (uint32(bs[19]) << 24))) t.Deps[4] = int32((uint32(bs[20]) | (uint32(bs[21]) << 8) | (uint32(bs[22]) << 16) | (uint32(bs[23]) << 24))) return nil }
// rawInt64 should only be used by low-level decoders func (p *importer) rawInt64() int64 { i, err := binary.ReadVarint(p) if err != nil { panic(fmt.Sprintf("read error: %v", err)) } return i }
func (t *Accept) Unmarshal(rr io.Reader) error { var wire byteReader var ok bool if wire, ok = rr.(byteReader); !ok { wire = bufio.NewReader(rr) } var b [12]byte var bs []byte bs = b[:12] if _, err := io.ReadAtLeast(wire, bs, 12); err != nil { return err } t.LeaderId = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.Instance = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24))) t.Ballot = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24))) alen1, err := binary.ReadVarint(wire) if err != nil { return err } t.Command = make([]state.Command, alen1) for i := int64(0); i < alen1; i++ { t.Command[i].Unmarshal(wire) } if _, err := io.ReadAtLeast(wire, bs, 12); err != nil { return err } t.LeaseInstance = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.OriginReplica = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24))) t.PropId = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24))) return nil }
func (b *BoltDB) GetValue() int64 { var retVal int64 err := b.db.View(func(tx *bolt.Tx) error { b := tx.Bucket(demoBucket) if b == nil { retVal = -1 return nil } v := b.Get(demoKey) if v == nil { retVal = 0 } else { i, err := binary.ReadVarint(bytes.NewBuffer(v)) if err != nil { return err } retVal = i } return nil }) if err != nil { log.Fatal(err) } return retVal }
// rawInt64 should only be used by low-level decoders func (p *importer) rawInt64() int64 { i, err := binary.ReadVarint(p) if err != nil { Fatalf("importer: read error: %v", err) } return i }
func DecodeOld(str string) (int64, error) { data, err := base64.StdEncoding.DecodeString(str) if err != nil { return 0, err } return binary.ReadVarint(bytes.NewReader(data)) }
// Testing out how to hash some object func main() { fmt.Println("Hello, playground") // sha := sha256.New() result := sha256.Sum256([]byte("foobar")) length := len(result) fmt.Printf("%d\n", length) fmt.Printf("%x\n", result) part0 := result[:16] fmt.Printf("%d\n", len(part0)) fmt.Printf("%x\n", part0) // converting byte array to signed int buf := bytes.NewBuffer(part0) intPart0, _ := binary.ReadVarint(buf) // note: probably want unsigned int instead... fmt.Printf("%d\n\n\n", intPart0) part1 := result[16:] fmt.Printf("%d\n", len(part1)) fmt.Printf("%x\n", part1) b := NewBloomFilterStringKeyed(32, 10) fmt.Println(b.byteCapacity) fmt.Println(b.numHashes) fmt.Println(b.bitHashTable) b.AddKey("foo") }
func (t *PrepareReply) Unmarshal(rr io.Reader) error { var wire byteReader var ok bool if wire, ok = rr.(byteReader); !ok { wire = bufio.NewReader(rr) } var b [10]byte var bs []byte bs = b[:9] if _, err := io.ReadAtLeast(wire, bs, 9); err != nil { return err } t.Balnum = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.OK = uint8(bs[4]) t.Ballot = int32((uint32(bs[5]) | (uint32(bs[6]) << 8) | (uint32(bs[7]) << 16) | (uint32(bs[8]) << 24))) alen1, err := binary.ReadVarint(wire) if err != nil { return err } t.Cstruct = make([]int32, alen1) for i := int64(0); i < alen1; i++ { bs = b[:4] if _, err := io.ReadAtLeast(wire, bs, 4); err != nil { return err } t.Cstruct[i] = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) } return nil }
//The go routine here, keep waiting messages from connected client Blocking. func (client *Client) Read(mp *MessagePasser) { for { //line, err := client.reader.ReadBytes('\xfe') length, err := binary.ReadVarint(client.reader) if err != nil { client.rethrowError(mp) mp.RemoveMapping(client.IP) mp.RemoveMapping(client.name) return } buffer := make([]byte, length) client.reader.Read(buffer) msg := new(Message) msg.Deserialize(buffer) err = msg.Deserialize(buffer) if err != nil { fmt.Println("err is " + err.Error()) } client.addToClients(msg, mp.connections.clients) mp.Incoming <- msg // Since there is only one socket, directly put all the received // msgs into the global receiving channel (message queue) } }
func (bed BinaryVarintEncoderDecoder) Decode(r io.Reader, n *big.Float) error { var isInteger int8 var f float64 var exponent int32 n.SetUint64(0) if err := binary.Read(r, binary.BigEndian, &isInteger); err != nil { return err } if isInteger <= 0 { var x int64 var err error if x, err = binary.ReadVarint(miniByteReader{r}); err != nil { return err } n.SetInt64(x) n.SetPrec(ENCODER_DECODER_PREC) return nil } else { if err := binary.Read(r, binary.BigEndian, &f); err != nil { return err } if err := binary.Read(r, binary.BigEndian, &exponent); err != nil { return err } bed.tmp.SetFloat64(f) bed.tmp.SetPrec(ENCODER_DECODER_PREC) n.SetMantExp(bed.tmp, int(exponent)) return nil } }
func init() { // seed math.random from crypto.random seedBytes, _ := makeRandom(8) seedBuf := bytes.NewBuffer(seedBytes) n64, _ := binary.ReadVarint(seedBuf) mrand.Seed(n64) }
// rawInt64 should only be used by low-level decoders. func (p *importer) rawInt64() int64 { i, err := binary.ReadVarint(p) if err != nil { formatErrorf("read error: %v", err) } return i }
// ReadVarIntAndDecode64 reads a varint from r to a uint64 // and then zigzag decodes it to an int64 value. func (r *Reader) ReadVarint() int64 { v, err := binary.ReadVarint(r.br) if err != nil { r.err = err } return v }
// Reads an encrypted message from the reader expecting the following structure: // The first two bytes are the length(x) of the encrypted message. // The next 24 bytes are the nonce. // The next x bytes is the encrypted message. func (s *SecureReader) Read(p []byte) (int, error) { er := &errReader{r: s.reader} plen := make([]byte, 2) er.read(plen) payloadLen, err := binary.ReadVarint(bytes.NewBuffer(plen)) if err != nil { return 0, errors.New("Invalid length bytes") } nonce := make([]byte, 24) er.read(nonce) payload := make([]byte, payloadLen) er.read(payload) if er.err != nil { return 0, er.err } nonceArray := &[24]byte{} copy(nonceArray[:], nonce) message, success := box.Open(nil, payload, nonceArray, s.pub, s.priv) if !success { return 0, errors.New("Decrypt failure") } copy(p, message) return len(message), nil }
func (b *BoltDB) Increment() int64 { buf := make([]byte, 8) var retVal int64 err := b.db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists(demoBucket) if err != nil { log.Fatal(err) } v := b.Get(demoKey) if v == nil { binary.PutVarint(buf, 0) retVal = 0 } else { i, err := binary.ReadVarint(bytes.NewBuffer(v)) if err != nil { log.Fatal(err) } i++ retVal = i binary.PutVarint(buf, i) } err = b.Put(demoKey, buf) return err }) if err != nil { log.Fatal(err) } return retVal }
func (t *Commit) Unmarshal(rr io.Reader) error { var wire byteReader var ok bool if wire, ok = rr.(byteReader); !ok { wire = bufio.NewReader(rr) } var b [12]byte var bs []byte bs = b[:12] if _, err := io.ReadAtLeast(wire, bs, 12); err != nil { return err } t.LeaderId = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) t.Instance = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24))) t.Ballot = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24))) alen1, err := binary.ReadVarint(wire) if err != nil { return err } t.LeaseUpdate = make([]qleaseproto.LeaseMetadata, alen1) for i := int64(0); i < alen1; i++ { t.LeaseUpdate[i].Unmarshal(wire) } return nil }
func BytesToInt(buf []byte) int64 { nbuf := bytes.NewBuffer(buf) n, err := binary.ReadVarint(nbuf) if err != nil { return -1 } return n }
func validatePrivateKey(key []byte) error { keyInt, _ := binary.ReadVarint(bytes.NewBuffer(key)) if keyInt == 0 || bytes.Compare(key, curveParams.N.Bytes()) >= 0 { return errors.New("Invalid seed") } return nil }
// decodeMsg decodes either a *Chunk, *Sync or *Ack object from the byte array. func decodeMsg(q []byte) (interface{}, error) { r := bytes.NewReader(q) // MsgKind msgkind, err := r.ReadByte() if err != nil { return nil, err } msgKind := MsgKind(msgkind) // Switch switch msgKind { case CHUNK: msg := &Chunk{} // SeqNo seqno, err := binary.ReadVarint(r) if err != nil { return nil, err } msg.seqno = SeqNo(seqno) // Chunk msg.chunk, err = ioutil.ReadAll(r) if err != nil { panic("u") } return msg, nil case SYNC: msg := &Sync{} nackd, err := binary.ReadVarint(r) if err != nil { return nil, err } msg.NAckd = SeqNo(nackd) return msg, nil case ACK: msg := &Ack{} nackd, err := binary.ReadVarint(r) if err != nil { return nil, err } msg.NAckd = SeqNo(nackd) return msg, nil } return nil, chain.ErrMisbehave }
func OpenEmbedFs(file *os.File) (*embedFs, error) { stat, err := file.Stat() if err != nil { return nil, err } _, err = file.Seek(-4, os.SEEK_END) if err != nil { return nil, err } offset, err := binary.ReadVarint(bufio.NewReader(file)) if err != nil { panic(err) } emfs := &embedFs{ files: make([]*embedFsEntry, 0), index: make(map[string]*embedFsEntry, 0), origin: file, tarOffset: offset, tarWriter: tar.NewWriter(file), } if offset >= stat.Size() || offset <= 0 { emfs.tarOffset = stat.Size() } file.Seek(emfs.tarOffset, os.SEEK_SET) tr := tar.NewReader(file) for { hdr, err := tr.Next() if err == io.EOF { break } if err != nil { emfs.tarOffset = stat.Size() return emfs, nil } seek, _ := file.Seek(0, os.SEEK_CUR) entry := &embedFsEntry{ name: hdr.Name, offset: seek, header: hdr, } emfs.files = append(emfs.files, entry) emfs.index[entry.name] = entry } return emfs, nil }
func (t *Trace) readRecord() error { deltaPC, err := binary.ReadVarint(t.blockReader) if err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } return err } deltaEA, err := binary.ReadVarint(t.blockReader) if err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } return err } t.rec.N = t.recno t.rec.PC += uint64(deltaPC) t.rec.EA += uint64(deltaEA) t.recno++ return nil }
func UnmarshalIdRefsBunch(buf []byte) []element.IdRefs { r := bytes.NewBuffer(buf) n, err := binary.ReadUvarint(r) if err != nil { return nil } idRefs := make([]element.IdRefs, n) last := int64(0) for i := 0; uint64(i) < n; i++ { idRefs[i].Id, err = binary.ReadVarint(r) if err != nil { panic(err) } idRefs[i].Id += last last = idRefs[i].Id } var numRefs uint64 for i := 0; uint64(i) < n; i++ { numRefs, err = binary.ReadUvarint(r) if err != nil { panic(err) } idRefs[i].Refs = make([]int64, numRefs) } last = 0 for idIdx := 0; uint64(idIdx) < n; idIdx++ { for refIdx := 0; refIdx < len(idRefs[idIdx].Refs); refIdx++ { idRefs[idIdx].Refs[refIdx], err = binary.ReadVarint(r) if err != nil { panic(err) } idRefs[idIdx].Refs[refIdx] += last last = idRefs[idIdx].Refs[refIdx] } } return idRefs }
// decodeString decodes a string encoded by encodeString. func decodeString(b byteReader) (string, error) { length, err := binary.ReadVarint(b) if err != nil { return "", err } buf := getBuf(int(length)) defer putBuf(buf) if _, err := io.ReadFull(b, buf); err != nil { return "", err } return string(buf), nil }
func init() { if urandom, err := os.Open("/dev/urandom"); err != nil { return } else { buf := make([]byte, 8) if _, err := urandom.Read(buf); err == nil { buf_reader := bytes.NewReader(buf) if seed, err := binary.ReadVarint(buf_reader); err == nil { rand.Seed(seed) } } urandom.Close() } }
func (t *LeaseMetadata) Unmarshal(rr io.Reader) error { var wire byteReader var ok bool if wire, ok = rr.(byteReader); !ok { wire = bufio.NewReader(rr) } var b [10]byte var bs []byte alen1, err := binary.ReadVarint(wire) if err != nil { return err } t.Quorum = make([]int32, alen1) for i := int64(0); i < alen1; i++ { bs = b[:4] if _, err := io.ReadAtLeast(wire, bs, 4); err != nil { return err } t.Quorum[i] = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24))) } alen2, err := binary.ReadVarint(wire) if err != nil { return err } t.ObjectKeys = make([]state.Key, alen2) for i := int64(0); i < alen2; i++ { t.ObjectKeys[i].Unmarshal(wire) } bs = b[:2] if _, err := io.ReadAtLeast(wire, bs, 2); err != nil { return err } t.IgnoreReplicas = uint8(bs[0]) t.ReinstateReplicas = uint8(bs[1]) return nil }
func (n *Node) extractMeta() (*nodeMeta, error) { md := &nodeMeta{} if len(n.Node.Meta) < minMdLen { return nil, fmt.Errorf("Not enough bytes to extract metadata") } // ready md.ready = n.Node.Meta[0] == 1 // sortBy var err error if md.sortBy, err = binary.ReadVarint(bytes.NewReader(n.Node.Meta[1:])); err != nil { return nil, fmt.Errorf("extractMeta(): sortBy: %v", err) } // user md.user = n.Node.Meta[minMdLen:] return md, nil }