| 1 |
// Package sbuf implements a byte buffer that can be wiped. The underlying |
| 2 |
// byte slice is wiped on read before being declaimed, and when the |
| 3 |
// buffer is closed, its storage is zeroised. |
| 4 |
package sbuf |
| 5 |
|
| 6 |
import "io" |
| 7 |
|
| 8 |
func zero(in []byte, n int) { |
| 9 |
if in == nil { |
| 10 |
return |
| 11 |
} |
| 12 |
|
| 13 |
stop := n |
| 14 |
if stop > len(in) || stop == 0 { |
| 15 |
stop = len(in) |
| 16 |
} |
| 17 |
|
| 18 |
for i := 0; i < stop; i++ { |
| 19 |
in[i] ^= in[i] |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// A Buffer is a variable-sized buffer of bytes with Read and Write |
| 24 |
// methods. The zero value for Buffer is an empty buffer ready to use. |
| 25 |
type Buffer struct { |
| 26 |
buf []byte |
| 27 |
} |
| 28 |
|
| 29 |
// NewBuffer creates a new buffer with the specified capacity. |
| 30 |
func NewBuffer(n int) *Buffer { |
| 31 |
return &Buffer{ |
| 32 |
buf: make([]byte, 0, n), |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// NewBufferFrom creates a new buffer from the byte slice passed in. The |
| 37 |
// original data will be wiped. |
| 38 |
func NewBufferFrom(p []byte) *Buffer { |
| 39 |
buf := NewBuffer(len(p)) |
| 40 |
buf.Write(p) |
| 41 |
zero(p, len(p)) |
| 42 |
return buf |
| 43 |
} |
| 44 |
|
| 45 |
// Read reads the next len(p) bytes from the buffer or until the buffer |
| 46 |
// is drained. The return value n is the number of bytes read. If the |
| 47 |
// buffer has no data to return, err is io.EOF (unless len(p) is zero); |
| 48 |
// otherwise it is nil. |
| 49 |
func (buf *Buffer) Read(p []byte) (int, error) { |
| 50 |
if len(buf.buf) == 0 { |
| 51 |
if len(p) == 0 { |
| 52 |
return 0, nil |
| 53 |
} |
| 54 |
return 0, io.EOF |
| 55 |
} |
| 56 |
|
| 57 |
copyLength := len(p) |
| 58 |
if copyLength > len(buf.buf) { |
| 59 |
copyLength = len(buf.buf) |
| 60 |
} |
| 61 |
|
| 62 |
copy(p, buf.buf) |
| 63 |
zero(buf.buf, len(p)) |
| 64 |
buf.buf = buf.buf[copyLength:] |
| 65 |
return copyLength, nil |
| 66 |
} |
| 67 |
|
| 68 |
// ReadByte reads the next byte from the buffer. If the buffer has no |
| 69 |
// data to return, err is io.EOF; otherwise it is nil. |
| 70 |
func (buf *Buffer) ReadByte() (byte, error) { |
| 71 |
if len(buf.buf) == 0 { |
| 72 |
return 0, io.EOF |
| 73 |
} |
| 74 |
|
| 75 |
c := buf.buf[0] |
| 76 |
buf.buf[0] = 0 |
| 77 |
buf.buf = buf.buf[1:] |
| 78 |
return c, nil |
| 79 |
} |
| 80 |
|
| 81 |
func (buf *Buffer) grow(n int) { |
| 82 |
tmp := make([]byte, len(buf.buf), len(buf.buf)+n) |
| 83 |
copy(tmp, buf.buf) |
| 84 |
zero(buf.buf, len(buf.buf)) |
| 85 |
buf.buf = tmp |
| 86 |
} |
| 87 |
|
| 88 |
// Write appends the contents of p to the buffer, growing the buffer |
| 89 |
// as needed. The return value n is the length of p; err is always nil. |
| 90 |
func (buf *Buffer) Write(p []byte) (int, error) { |
| 91 |
r := len(buf.buf) + len(p) |
| 92 |
if cap(buf.buf) < r { |
| 93 |
l := r |
| 94 |
for { |
| 95 |
if l > r { |
| 96 |
break |
| 97 |
} |
| 98 |
l *= 2 |
| 99 |
} |
| 100 |
buf.grow(l - cap(buf.buf)) |
| 101 |
} |
| 102 |
buf.buf = append(buf.buf, p...) |
| 103 |
return len(p), nil |
| 104 |
} |
| 105 |
|
| 106 |
// WriteByte adds the byte c to the buffer, growing the buffer as needed. |
| 107 |
func (buf *Buffer) WriteByte(c byte) error { |
| 108 |
r := len(buf.buf) + 1 |
| 109 |
if cap(buf.buf) < r { |
| 110 |
l := r * 2 |
| 111 |
buf.grow(l - cap(buf.buf)) |
| 112 |
} |
| 113 |
buf.buf = append(buf.buf, c) |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
// Close destroys and zeroises the buffer. The buffer will be re-opened |
| 118 |
// on the next write. |
| 119 |
func (buf *Buffer) Close() { |
| 120 |
zero(buf.buf, len(buf.buf)) |
| 121 |
buf.buf = nil |
| 122 |
} |
| 123 |
|
| 124 |
// Len returns the length of the buffer. |
| 125 |
func (buf *Buffer) Len() int { |
| 126 |
return len(buf.buf) |
| 127 |
} |
| 128 |
|
| 129 |
// Cap returns the capacity of the buffer. |
| 130 |
func (buf *Buffer) Cap() int { |
| 131 |
return cap(buf.buf) |
| 132 |
} |
| 133 |
|
| 134 |
// Bytes returns the bytes currently in the buffer, and closes itself. |
| 135 |
func (buf *Buffer) Bytes() []byte { |
| 136 |
if buf.buf == nil { |
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
p := make([]byte, buf.Len()) |
| 141 |
buf.Read(p) |
| 142 |
buf.Close() |
| 143 |
return p |
| 144 |
} |