Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: common/cmpbin/string.go

Issue 1219323002: Rename funnybase and add more serializations. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: fixes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/cmpbin/number_test.go ('k') | common/cmpbin/string_test.go » ('j') | 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 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package cmpbin
6
7 import (
8 "bytes"
9 "errors"
10 "io"
11 "math"
12 )
13
14 // ReadByteLimit is the limit of how many bytes ReadBytes and ReadString are
15 // willing to deserialize before returning ErrByteLimitExceeded. It is currently
16 // set to allow 2MB of user data (taking encoding size overhead into account).
17 var ReadByteLimit = int(math.Ceil(2 * 1024 * 1024 * 8 / 7))
18
19 // ErrByteLimitExceeded is returned from ReadBytes and ReadString when they
20 // attempt to read more than ReadByteLimit bytes.
21 var ErrByteLimitExceeded = errors.New("cmbpin: too big! tried to read > cmpbin.R eadByteLimit")
22
23 // WriteString writes an encoded string to buf, returning the number of bytes
24 // written, and any write error encountered.
25 func WriteString(buf io.ByteWriter, s string) (n int, err error) {
26 return WriteBytes(buf, []byte(s))
27 }
28
29 // ReadString reads an encoded string from buf, returning the number of bytes
30 // read, and any read error encountered.
31 func ReadString(buf io.ByteReader) (ret string, n int, err error) {
32 b, n, err := ReadBytes(buf)
33 if err != nil {
34 return
35 }
36 ret = string(b)
37 return
38 }
39
40 // WriteBytes writes an encoded []byte to buf, returning the number of bytes
41 // written, and any write error encountered.
42 func WriteBytes(buf io.ByteWriter, data []byte) (n int, err error) {
43 wb := func(b byte) (err error) {
44 if err = buf.WriteByte(b); err == nil {
45 n++
46 }
47 return
48 }
49
50 acc := byte(0)
51 for i := 0; i < len(data); i++ {
52 m := uint(i % 7)
53 b := data[i]
54 if err = wb(acc | 1 | ((b & (0xff << (m + 1))) >> m)); err != ni l {
55 return
56 }
57 acc = (b << (7 - m))
58 if m == 6 {
59 if err = wb(acc | 1); err != nil {
60 return
61 }
62 acc = 0
63 }
64 }
65 err = wb(acc)
66 return
67 }
68
69 // ReadBytes reads an encoded []byte from buf, returning the number of bytes
70 // read, and any read error encountered.
71 func ReadBytes(buf io.ByteReader) (ret []byte, n int, err error) {
72 tmpBuf := bytes.Buffer{}
73 acc := byte(0)
74 for i := 0; i < ReadByteLimit; i++ {
75 o := byte(0)
76 if o, err = buf.ReadByte(); err != nil {
77 return
78 }
79 n++
80
81 b := o & 0xfe // user data
82 m := uint(i % 8)
83
84 if m == 0 {
85 acc = b
86 } else {
87 // ignore err since bytes.Buffer.WriteByte can never ret urn one.
88 _ = tmpBuf.WriteByte(acc | (b >> (8 - m)))
89 acc = (b << m)
90 }
91
92 if o&1 == 0 { // stop bit is 0
93 ret = tmpBuf.Bytes()
94 return
95 }
96 }
97 err = ErrByteLimitExceeded
98 return
99 }
OLDNEW
« no previous file with comments | « common/cmpbin/number_test.go ('k') | common/cmpbin/string_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698