mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-12-12 06:08:21 +00:00
- terrible code - lots needs changing - can build a Moby from a config yaml that boots Signed-off-by: Justin Cormack <justin.cormack@docker.com>
47 lines
801 B
Go
47 lines
801 B
Go
package pad4
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
)
|
|
|
|
// A Writer is an io.WriteCloser. Writes are padded with zeros to 4 byte boundary
|
|
type Writer struct {
|
|
w io.Writer
|
|
count int
|
|
}
|
|
|
|
// Write writes output
|
|
func (pad Writer) Write(p []byte) (int, error) {
|
|
n, err := pad.w.Write(p)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
pad.count += n
|
|
return n, nil
|
|
}
|
|
|
|
// Close adds the padding
|
|
func (pad Writer) Close() error {
|
|
mod4 := pad.count & 3
|
|
if mod4 == 0 {
|
|
return nil
|
|
}
|
|
zero := make([]byte, 4-mod4)
|
|
buf := bytes.NewBuffer(zero)
|
|
n, err := io.Copy(pad.w, buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pad.count += int(n)
|
|
return nil
|
|
}
|
|
|
|
// NewWriter provides a new io.WriteCloser that zero pads the
|
|
// output to a multiple of four bytes
|
|
func NewWriter(w io.Writer) *Writer {
|
|
pad := new(Writer)
|
|
pad.w = w
|
|
return pad
|
|
}
|