Chromium Code Reviews| Index: go/src/infra/gae/libs/wrapper/memory/binutils.go |
| diff --git a/go/src/infra/gae/libs/wrapper/memory/binutils.go b/go/src/infra/gae/libs/wrapper/memory/binutils.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..45d141c43c5421ca9aed942d4e4837e982286248 |
| --- /dev/null |
| +++ b/go/src/infra/gae/libs/wrapper/memory/binutils.go |
| @@ -0,0 +1,75 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package memory |
| + |
| +import ( |
| + "bytes" |
| + "encoding/binary" |
| + "fmt" |
| + "math" |
| + |
| + "github.com/luci/luci-go/common/funnybase" |
| +) |
| + |
| +func writeString(buf *bytes.Buffer, s string) { |
|
M-A Ruel
2015/05/27 20:14:47
Why not a io.Writer? I'd be much more idiomatic.
iannucci
2015/05/27 21:36:22
because io.Writer.Write returns an error... since
|
| + funnybase.WriteUint(buf, uint64(len(s))) |
| + buf.WriteString(s) |
| +} |
| + |
| +func readString(buf *bytes.Buffer) (string, error) { |
| + b, err := readBytes(buf) |
| + if err != nil { |
| + return "", err |
| + } |
| + return string(b), nil |
| +} |
| + |
| +func writeBytes(buf *bytes.Buffer, b []byte) { |
| + funnybase.WriteUint(buf, uint64(len(b))) |
| + buf.Write(b) |
| +} |
| + |
| +func readBytes(buf *bytes.Buffer) ([]byte, error) { |
| + val, err := funnybase.ReadUint(buf) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if val > 2*1024*1024 { // 2MB |
| + return nil, fmt.Errorf("readBytes: tried to read %d bytes (> 2MB)", val) |
| + } |
| + retBuf := make([]byte, val) |
| + n, _ := buf.Read(retBuf) // err is either io.EOF or nil for bytes.Buffer |
| + if uint64(n) != val { |
| + return nil, fmt.Errorf("readBytes: expected %d bytes but read %d", val, n) |
| + } |
| + return retBuf, err |
| +} |
| + |
| +func writeFloat64(buf *bytes.Buffer, v float64) { |
| + // byte-ordered floats http://stereopsis.com/radix.html |
| + bits := math.Float64bits(v) |
| + bits = bits ^ (-(bits >> 63) | (1 << 63)) |
| + data := make([]byte, 8) |
| + binary.BigEndian.PutUint64(data, bits) |
| + buf.Write(data) |
| +} |
| + |
| +func readFloat64(buf *bytes.Buffer) (float64, error) { |
| + // byte-ordered floats http://stereopsis.com/radix.html |
| + data := make([]byte, 8) |
| + _, err := buf.Read(data) |
| + if err != nil { |
| + return 0, err |
| + } |
| + bits := binary.BigEndian.Uint64(data) |
| + return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), nil |
| +} |
| + |
| +func btoi(b bool) byte { |
| + if b { |
| + return 1 |
| + } |
| + return 0 |
| +} |