Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(765)

Side by Side Diff: probe/varint.go

Issue 60550047: code review 60550047: ogle/probe: handle read requests from network (Closed)
Patch Set: Created 10 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« probe/size_amd64.go ('K') | « probe/size_amd64.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This file contains an implementation of "varint" encoding and decoding.
nigeltao 2014/02/10 04:05:56 Move this file-level comment below the package cla
r 2014/02/11 01:38:55 Done.
6 // Code is adapted from encoding/binary/varint.go, copied here to avoid dependen cies,
7 // simplified somewhat, and made local to the package.
8 // It handles unsigned integers only.
9
10 package probe
11
12 import (
13 "errors"
14 "io"
15 )
16
17 // maxVarintLenN is the maximum length of a varint-encoded N-bit integer.
18 const (
19 maxVarintLen16 = 3
20 maxVarintLen32 = 5
21 maxVarintLen64 = 10
22 )
23
24 // putUvarint encodes a uint64 into buf and returns the number of bytes written.
25 // If the buffer is too small, putUvarint will panic.
26 func putUvarint(buf []byte, x uint64) int {
27 i := 0
28 for x >= 0x80 {
29 buf[i] = byte(x) | 0x80
30 x >>= 7
31 i++
32 }
33 buf[i] = byte(x)
34 return i + 1
35 }
36
37 // getUvarint decodes a uint64 from buf and returns that value and the
38 // number of bytes read (> 0). If an error occurred, the value is 0
39 // and the number of bytes n is <= 0 meaning:
40 //
41 // n == 0: buf too small
42 // n < 0: value larger than 64 bits (overflow)
43 // and -n is the number of bytes read
44 //
45 // TODO: Unused. Delete if it doesn't get used.
46 func getUvarint(buf []byte) (uint64, int) {
47 var x uint64
48 var s uint
49 for i, b := range buf {
50 if b < 0x80 {
51 if i > 9 || i == 9 && b > 1 {
52 return 0, -(i + 1) // overflow
53 }
54 return x | uint64(b)<<s, i + 1
55 }
56 x |= uint64(b&0x7f) << s
57 s += 7
58 }
59 return 0, 0
60 }
61
62 var overflow = errors.New("binary: varint overflows a 64-bit integer")
nigeltao 2014/02/10 04:05:56 s/binary/probe/ ?
r 2014/02/11 01:38:55 Done.
63
64 // readUvarint reads an encoded unsigned integer from r and returns it as a uint 64.
65 func readUvarint(r io.ByteReader) (uint64, error) {
66 var x uint64
67 var s uint
68 for i := 0; ; i++ {
69 b, err := r.ReadByte()
70 if err != nil {
71 return x, err
72 }
73 if b < 0x80 {
74 if i > 9 || i == 9 && b > 1 {
75 return x, overflow
76 }
77 return x | uint64(b)<<s, nil
78 }
79 x |= uint64(b&0x7f) << s
80 s += 7
81 }
82 }
OLDNEW
« probe/size_amd64.go ('K') | « probe/size_amd64.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b