summaryrefslogtreecommitdiff
path: root/src/pkg/encoding
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/encoding')
-rw-r--r--src/pkg/encoding/ascii85/Makefile11
-rw-r--r--src/pkg/encoding/ascii85/ascii85.go311
-rw-r--r--src/pkg/encoding/ascii85/ascii85_test.go193
-rw-r--r--src/pkg/encoding/base64/Makefile11
-rw-r--r--src/pkg/encoding/base64/base64.go334
-rw-r--r--src/pkg/encoding/base64/base64_test.go201
-rw-r--r--src/pkg/encoding/binary/Makefile11
-rw-r--r--src/pkg/encoding/binary/binary.go222
-rw-r--r--src/pkg/encoding/binary/binary_test.go86
-rw-r--r--src/pkg/encoding/git85/Makefile11
-rw-r--r--src/pkg/encoding/git85/git.go284
-rw-r--r--src/pkg/encoding/git85/git_test.go199
12 files changed, 1874 insertions, 0 deletions
diff --git a/src/pkg/encoding/ascii85/Makefile b/src/pkg/encoding/ascii85/Makefile
new file mode 100644
index 000000000..5a7bc176c
--- /dev/null
+++ b/src/pkg/encoding/ascii85/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=encoding/ascii85
+GOFILES=\
+ ascii85.go\
+
+include $(GOROOT)/src/Make.pkg
diff --git a/src/pkg/encoding/ascii85/ascii85.go b/src/pkg/encoding/ascii85/ascii85.go
new file mode 100644
index 000000000..27256eeca
--- /dev/null
+++ b/src/pkg/encoding/ascii85/ascii85.go
@@ -0,0 +1,311 @@
+// Copyright 2009 The Go 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 ascii85 implements the ascii85 data encoding
+// as used in the btoa tool and Adobe's PostScript and PDF document formats.
+package ascii85
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "strconv";
+)
+
+/*
+ * Encoder
+ */
+
+// Encode encodes src into at most MaxEncodedLen(len(src))
+// bytes of dst, returning the actual number of bytes written.
+//
+// The encoding handles 4-byte chunks, using a special encoding
+// for the last fragment, so Encode is not appropriate for use on
+// individual blocks of a large data stream. Use NewEncoder() instead.
+//
+// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
+// Encode does not add these.
+func Encode(src, dst []byte) int {
+ if len(src) == 0 {
+ return 0;
+ }
+
+ n := 0;
+ for len(src) > 0 {
+ dst[0] = 0;
+ dst[1] = 0;
+ dst[2] = 0;
+ dst[3] = 0;
+ dst[4] = 0;
+
+ // Unpack 4 bytes into uint32 to repack into base 85 5-byte.
+ var v uint32;
+ switch len(src) {
+ default:
+ v |= uint32(src[3]);
+ fallthrough;
+ case 3:
+ v |= uint32(src[2])<<8;
+ fallthrough;
+ case 2:
+ v |= uint32(src[1])<<16;
+ fallthrough;
+ case 1:
+ v |= uint32(src[0])<<24;
+ }
+
+ // Special case: zero (!!!!!) shortens to z.
+ if v == 0 && len(src) >= 4 {
+ dst[0] = 'z';
+ dst = dst[1:len(dst)];
+ n++;
+ continue;
+ }
+
+ // Otherwise, 5 base 85 digits starting at !.
+ for i := 4; i >= 0; i-- {
+ dst[i] = '!' + byte(v%85);
+ v /= 85;
+ }
+
+ // If src was short, discard the low destination bytes.
+ m := 5;
+ if len(src) < 4 {
+ m -= 4 - len(src);
+ src = nil;
+ } else {
+ src = src[4:len(src)];
+ }
+ dst = dst[m:len(dst)];
+ n += m;
+ }
+ return n;
+}
+
+// MaxEncodedLen returns the maximum length of an encoding of n source bytes.
+func MaxEncodedLen(n int) int {
+ return (n+3)/4*5;
+}
+
+// NewEncoder returns a new ascii85 stream encoder. Data written to
+// the returned writer will be encoded and then written to w.
+// Ascii85 encodings operate in 32-bit blocks; when finished
+// writing, the caller must Close the returned encoder to flush any
+// trailing partial block.
+func NewEncoder(w io.Writer) io.WriteCloser {
+ return &encoder{w: w};
+}
+
+type encoder struct {
+ err os.Error;
+ w io.Writer;
+ buf [4]byte; // buffered data waiting to be encoded
+ nbuf int; // number of bytes in buf
+ out [1024]byte; // output buffer
+}
+
+func (e *encoder) Write(p []byte) (n int, err os.Error) {
+ if e.err != nil {
+ return 0, e.err;
+ }
+
+ // Leading fringe.
+ if e.nbuf > 0 {
+ var i int;
+ for i = 0; i < len(p) && e.nbuf < 4; i++ {
+ e.buf[e.nbuf] = p[i];
+ e.nbuf++;
+ }
+ n += i;
+ p = p[i:len(p)];
+ if e.nbuf < 4 {
+ return;
+ }
+ nout := Encode(&e.buf, &e.out);
+ var _ int;
+ if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
+ return n, e.err;
+ }
+ e.nbuf = 0;
+ }
+
+ // Large interior chunks.
+ for len(p) >= 4 {
+ nn := len(e.out)/5*4;
+ if nn > len(p) {
+ nn = len(p);
+ }
+ nn -= nn%4;
+ if nn > 0 {
+ nout := Encode(p[0:nn], &e.out);
+ var _ int;
+ if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
+ return n, e.err;
+ }
+ }
+ n += nn;
+ p = p[nn:len(p)];
+ }
+
+ // Trailing fringe.
+ for i := 0; i < len(p); i++ {
+ e.buf[i] = p[i];
+ }
+ e.nbuf = len(p);
+ n += len(p);
+ return;
+}
+
+// Close flushes any pending output from the encoder.
+// It is an error to call Write after calling Close.
+func (e *encoder) Close() os.Error {
+ // If there's anything left in the buffer, flush it out
+ if e.err == nil && e.nbuf > 0 {
+ nout := Encode(e.buf[0:e.nbuf], &e.out);
+ e.nbuf = 0;
+ var _ int;
+ _, e.err = e.w.Write(e.out[0:nout]);
+ }
+ return e.err;
+}
+
+/*
+ * Decoder
+ */
+
+type CorruptInputError int64
+
+func (e CorruptInputError) String() string {
+ return "illegal ascii85 data at input byte" + strconv.Itoa64(int64(e));
+}
+
+// Decode decodes src into dst, returning both the number
+// of bytes consumed from src and the number written to dst.
+// If src contains invalid ascii85 data, Decode will return the
+// number of bytes successfully written and a CorruptInputError.
+// Decode ignores space and control characters in src.
+// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
+// Decode expects these to have been stripped by the caller.
+//
+// If flush is true, Decode assumes that src represents the
+// end of the input stream and processes it completely rather
+// than wait for the completion of another 32-bit block.
+//
+// NewDecoder wraps an io.Reader interface around Decode.
+//
+func Decode(src, dst []byte, flush bool) (nsrc, ndst int, err os.Error) {
+ var v uint32;
+ var nb int;
+ for i, b := range src {
+ if len(dst) - ndst < 4 {
+ return;
+ }
+ switch {
+ case b <= ' ':
+ continue;
+ case b == 'z' && nb == 0:
+ nb = 5;
+ v = 0;
+ case '!' <= b && b <= 'u':
+ v = v*85 + uint32(b - '!');
+ nb++;
+ default:
+ return 0, 0, CorruptInputError(i);
+ }
+ if nb == 5 {
+ nsrc = i+1;
+ dst[ndst] = byte(v>>24);
+ dst[ndst+1] = byte(v>>16);
+ dst[ndst+2] = byte(v>>8);
+ dst[ndst+3] = byte(v);
+ ndst += 4;
+ nb = 0;
+ v = 0;
+ }
+ }
+ if flush {
+ nsrc = len(src);
+ if nb > 0 {
+ // The number of output bytes in the last fragment
+ // is the number of leftover input bytes - 1:
+ // the extra byte provides enough bits to cover
+ // the inefficiency of the encoding for the block.
+ if nb == 1 {
+ return 0, 0, CorruptInputError(len(src));
+ }
+ for i := nb; i < 5; i++ {
+ // The short encoding truncated the output value.
+ // We have to assume the worst case values (digit 84)
+ // in order to ensure that the top bits are correct.
+ v = v*85 + 84;
+ }
+ for i := 0; i < nb-1; i++ {
+ dst[ndst] = byte(v>>24);
+ v <<= 8;
+ ndst++;
+ }
+ }
+ }
+ return;
+}
+
+// NewDecoder constructs a new ascii85 stream decoder.
+func NewDecoder(r io.Reader) io.Reader {
+ return &decoder{r: r};
+}
+
+type decoder struct {
+ err os.Error;
+ readErr os.Error;
+ r io.Reader;
+ end bool; // saw end of message
+ buf [1024]byte; // leftover input
+ nbuf int;
+ out []byte; // leftover decoded output
+ outbuf [1024]byte;
+}
+
+func (d *decoder) Read(p []byte) (n int, err os.Error) {
+ if len(p) == 0 {
+ return 0, nil;
+ }
+ if d.err != nil {
+ return 0, d.err;
+ }
+
+ for {
+ // Copy leftover output from last decode.
+ if len(d.out) > 0 {
+ n = bytes.Copy(p, d.out);
+ d.out = d.out[n:len(d.out)];
+ return;
+ }
+
+ // Decode leftover input from last read.
+ var nn, nsrc, ndst int;
+ if d.nbuf > 0 {
+ nsrc, ndst, d.err = Decode(d.buf[0:d.nbuf], &d.outbuf, d.readErr != nil);
+ if ndst > 0 {
+ d.out = d.outbuf[0:ndst];
+ d.nbuf = bytes.Copy(&d.buf, d.buf[nsrc:d.nbuf]);
+ continue; // copy out and return
+ }
+ }
+
+ // Out of input, out of decoded output. Check errors.
+ if d.err != nil {
+ return 0, d.err;
+ }
+ if d.readErr != nil {
+ d.err = d.readErr;
+ return 0, d.err;
+ }
+
+ // Read more data.
+ nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)]);
+ d.nbuf += nn;
+ }
+ panic("unreachable");
+}
+
diff --git a/src/pkg/encoding/ascii85/ascii85_test.go b/src/pkg/encoding/ascii85/ascii85_test.go
new file mode 100644
index 000000000..0264333ab
--- /dev/null
+++ b/src/pkg/encoding/ascii85/ascii85_test.go
@@ -0,0 +1,193 @@
+// Copyright 2009 The Go 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 ascii85
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "reflect";
+ "strings";
+ "testing";
+)
+
+type testpair struct {
+ decoded, encoded string;
+}
+
+var pairs = []testpair{
+ // Wikipedia example
+ testpair{
+ "Man is distinguished, not only by his reason, but by this singular passion from "
+ "other animals, which is a lust of the mind, that by a perseverance of delight in "
+ "the continued and indefatigable generation of knowledge, exceeds the short "
+ "vehemence of any carnal pleasure.",
+ "9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,\n"
+ "O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n"
+ "i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n"
+ "l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n"
+ ">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n"
+ }
+}
+
+var bigtest = pairs[len(pairs)-1];
+
+func testEqual(t *testing.T, msg string, args ...) bool {
+ v := reflect.NewValue(args).(*reflect.StructValue);
+ v1 := v.Field(v.NumField() - 2);
+ v2 := v.Field(v.NumField() - 1);
+ if v1.Interface() != v2.Interface() {
+ t.Errorf(msg, args);
+ return false;
+ }
+ return true;
+}
+
+func strip85(s string) string {
+ t := make([]byte, len(s));
+ w := 0;
+ for r := 0; r < len(s); r++ {
+ c := s[r];
+ if c > ' ' {
+ t[w] = c;
+ w++;
+ }
+ }
+ return string(t[0:w]);
+}
+
+func TestEncode(t *testing.T) {
+ for _, p := range pairs {
+ buf := make([]byte, MaxEncodedLen(len(p.decoded)));
+ n := Encode(strings.Bytes(p.decoded), buf);
+ buf = buf[0:n];
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, strip85(string(buf)), strip85(p.encoded));
+ }
+}
+
+func TestEncoder(t *testing.T) {
+ for _, p := range pairs {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(bb);
+ encoder.Write(strings.Bytes(p.decoded));
+ encoder.Close();
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, strip85(bb.String()), strip85(p.encoded));
+ }
+}
+
+func TestEncoderBuffering(t *testing.T) {
+ input := strings.Bytes(bigtest.decoded);
+ for bs := 1; bs <= 12; bs++ {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(bb);
+ for pos := 0; pos < len(input); pos += bs {
+ end := pos+bs;
+ if end > len(input) {
+ end = len(input);
+ }
+ n, err := encoder.Write(input[pos:end]);
+ testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, os.Error(nil));
+ testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos);
+ }
+ err := encoder.Close();
+ testEqual(t, "Close gave error %v, want %v", err, os.Error(nil));
+ testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, strip85(bb.String()), strip85(bigtest.encoded));
+ }
+}
+
+func TestDecode(t *testing.T) {
+ for _, p := range pairs {
+ dbuf := make([]byte, 4*len(p.encoded));
+ nsrc, ndst, err := Decode(strings.Bytes(p.encoded), dbuf, true);
+ testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, os.Error(nil));
+ testEqual(t, "Decode(%q) = nsrc %v, want %v", p.encoded, nsrc, len(p.encoded));
+ testEqual(t, "Decode(%q) = ndst %v, want %v", p.encoded, ndst, len(p.decoded));
+ testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:ndst]), p.decoded);
+ }
+}
+
+func TestDecoder(t *testing.T) {
+ for _, p := range pairs {
+ decoder := NewDecoder(bytes.NewBufferString(p.encoded));
+ dbuf, err := io.ReadAll(decoder);
+ if err != nil {
+ t.Fatal("Read failed", err);
+ }
+ testEqual(t, "Read from %q = length %v, want %v", p.encoded, len(dbuf), len(p.decoded));
+ testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf), p.decoded);
+ if err != nil {
+ testEqual(t, "Read from %q = %v, want %v", p.encoded, err, os.EOF);
+ }
+ }
+}
+
+func TestDecoderBuffering(t *testing.T) {
+ for bs := 1; bs <= 12; bs++ {
+ decoder := NewDecoder(bytes.NewBufferString(bigtest.encoded));
+ buf := make([]byte, len(bigtest.decoded)+12);
+ var total int;
+ for total = 0; total < len(bigtest.decoded); {
+ n, err := decoder.Read(buf[total : total+bs]);
+ testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, os.Error(nil));
+ total += n;
+ }
+ testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded);
+ }
+}
+
+func TestDecodeCorrupt(t *testing.T) {
+ type corrupt struct {
+ e string;
+ p int;
+ }
+ examples := []corrupt{
+ corrupt{"v", 0},
+ corrupt{"!z!!!!!!!!!", 1},
+ };
+
+ for _, e := range examples {
+ dbuf := make([]byte, 4*len(e.e));
+ _, _, err := Decode(strings.Bytes(e.e), dbuf, true);
+ switch err := err.(type) {
+ case CorruptInputError:
+ testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p);
+ default:
+ t.Error("Decoder failed to detect corruption in", e);
+ }
+ }
+}
+
+func TestBig(t *testing.T) {
+ n := 3*1000 + 1;
+ raw := make([]byte, n);
+ const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ for i := 0; i < n; i++ {
+ raw[i] = alpha[i%len(alpha)];
+ }
+ encoded := new(bytes.Buffer);
+ w := NewEncoder(encoded);
+ nn, err := w.Write(raw);
+ if nn != n || err != nil {
+ t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n);
+ }
+ err = w.Close();
+ if err != nil {
+ t.Fatalf("Encoder.Close() = %v want nil", err);
+ }
+ decoded, err := io.ReadAll(NewDecoder(encoded));
+ if err != nil {
+ t.Fatalf("io.ReadAll(NewDecoder(...)): %v", err);
+ }
+
+ if !bytes.Equal(raw, decoded) {
+ var i int;
+ for i = 0; i < len(decoded) && i < len(raw); i++ {
+ if decoded[i] != raw[i] {
+ break;
+ }
+ }
+ t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i);
+ }
+}
diff --git a/src/pkg/encoding/base64/Makefile b/src/pkg/encoding/base64/Makefile
new file mode 100644
index 000000000..1afb0ebb8
--- /dev/null
+++ b/src/pkg/encoding/base64/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=encoding/base64
+GOFILES=\
+ base64.go\
+
+include $(GOROOT)/src/Make.pkg
diff --git a/src/pkg/encoding/base64/base64.go b/src/pkg/encoding/base64/base64.go
new file mode 100644
index 000000000..b4daee3cc
--- /dev/null
+++ b/src/pkg/encoding/base64/base64.go
@@ -0,0 +1,334 @@
+// Copyright 2009 The Go 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 base64 implements base64 encoding as specified by RFC 4648.
+package base64
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "strconv";
+)
+
+/*
+ * Encodings
+ */
+
+// An Encoding is a radix 64 encoding/decoding scheme, defined by a
+// 64-character alphabet. The most common encoding is the "base64"
+// encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
+// (RFC 1421). RFC 4648 also defines an alternate encoding, which is
+// the standard encoding with - and _ substituted for + and /.
+type Encoding struct {
+ encode string;
+ decodeMap [256]byte;
+}
+
+const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
+
+// NewEncoding returns a new Encoding defined by the given alphabet,
+// which must be a 64-byte string.
+func NewEncoding(encoder string) *Encoding {
+ e := new(Encoding);
+ e.encode = encoder;
+ for i := 0; i < len(e.decodeMap); i++ {
+ e.decodeMap[i] = 0xFF;
+ }
+ for i := 0; i < len(encoder); i++ {
+ e.decodeMap[encoder[i]] = byte(i);
+ }
+ return e;
+}
+
+// StdEncoding is the standard base64 encoding, as defined in
+// RFC 4648.
+var StdEncoding = NewEncoding(encodeStd)
+
+// URLEncoding is the alternate base64 encoding defined in RFC 4648.
+// It is typically used in URLs and file names.
+var URLEncoding = NewEncoding(encodeURL)
+
+/*
+ * Encoder
+ */
+
+// Encode encodes src using the encoding enc, writing
+// EncodedLen(len(src)) bytes to dst.
+//
+// The encoding pads the output to a multiple of 4 bytes,
+// so Encode is not appropriate for use on individual blocks
+// of a large data stream. Use NewEncoder() instead.
+func (enc *Encoding) Encode(src, dst []byte) {
+ if len(src) == 0 {
+ return;
+ }
+
+ for len(src) > 0 {
+ dst[0] = 0;
+ dst[1] = 0;
+ dst[2] = 0;
+ dst[3] = 0;
+
+ // Unpack 4x 6-bit source blocks into a 4 byte
+ // destination quantum
+ switch len(src) {
+ default:
+ dst[3] |= src[2]&0x3F;
+ dst[2] |= src[2]>>6;
+ fallthrough;
+ case 2:
+ dst[2] |= (src[1]<<2)&0x3F;
+ dst[1] |= src[1]>>4;
+ fallthrough;
+ case 1:
+ dst[1] |= (src[0]<<4)&0x3F;
+ dst[0] |= src[0]>>2;
+ }
+
+ // Encode 6-bit blocks using the base64 alphabet
+ for j := 0; j < 4; j++ {
+ dst[j] = enc.encode[dst[j]];
+ }
+
+ // Pad the final quantum
+ if len(src) < 3 {
+ dst[3] = '=';
+ if len(src) < 2 {
+ dst[2] = '=';
+ }
+ break;
+ }
+
+ src = src[3:len(src)];
+ dst = dst[4:len(dst)];
+ }
+}
+
+type encoder struct {
+ err os.Error;
+ enc *Encoding;
+ w io.Writer;
+ buf [3]byte; // buffered data waiting to be encoded
+ nbuf int; // number of bytes in buf
+ out [1024]byte; // output buffer
+}
+
+func (e *encoder) Write(p []byte) (n int, err os.Error) {
+ if e.err != nil {
+ return 0, e.err;
+ }
+
+ // Leading fringe.
+ if e.nbuf > 0 {
+ var i int;
+ for i = 0; i < len(p) && e.nbuf < 3; i++ {
+ e.buf[e.nbuf] = p[i];
+ e.nbuf++;
+ }
+ n += i;
+ p = p[i:len(p)];
+ if e.nbuf < 3 {
+ return;
+ }
+ e.enc.Encode(&e.buf, &e.out);
+ if _, e.err = e.w.Write(e.out[0:4]); e.err != nil {
+ return n, e.err;
+ }
+ e.nbuf = 0;
+ }
+
+ // Large interior chunks.
+ for len(p) >= 3 {
+ nn := len(e.out)/4*3;
+ if nn > len(p) {
+ nn = len(p);
+ }
+ nn -= nn%3;
+ if nn > 0 {
+ e.enc.Encode(p[0:nn], &e.out);
+ if _, e.err = e.w.Write(e.out[0 : nn/3*4]); e.err != nil {
+ return n, e.err;
+ }
+ }
+ n += nn;
+ p = p[nn:len(p)];
+ }
+
+ // Trailing fringe.
+ for i := 0; i < len(p); i++ {
+ e.buf[i] = p[i];
+ }
+ e.nbuf = len(p);
+ n += len(p);
+ return;
+}
+
+// Close flushes any pending output from the encoder.
+// It is an error to call Write after calling Close.
+func (e *encoder) Close() os.Error {
+ // If there's anything left in the buffer, flush it out
+ if e.err == nil && e.nbuf > 0 {
+ e.enc.Encode(e.buf[0 : e.nbuf], &e.out);
+ e.nbuf = 0;
+ _, e.err = e.w.Write(e.out[0:4]);
+ }
+ return e.err;
+}
+
+// NewEncoder returns a new base64 stream encoder. Data written to
+// the returned writer will be encoded using enc and then written to w.
+// Base64 encodings operate in 4-byte blocks; when finished
+// writing, the caller must Close the returned encoder to flush any
+// partially written blocks.
+func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
+ return &encoder{enc: enc, w: w};
+}
+
+// EncodedLen returns the length in bytes of the base64 encoding
+// of an input buffer of length n.
+func (enc *Encoding) EncodedLen(n int) int {
+ return (n+2)/3*4;
+}
+
+/*
+ * Decoder
+ */
+
+type CorruptInputError int64
+
+func (e CorruptInputError) String() string {
+ return "illegal base64 data at input byte" + strconv.Itoa64(int64(e));
+}
+
+// decode is like Decode but returns an additional 'end' value, which
+// indicates if end-of-message padding was encountered and thus any
+// additional data is an error. decode also assumes len(src)%4==0,
+// since it is meant for internal use.
+func (enc *Encoding) decode(src, dst []byte) (n int, end bool, err os.Error) {
+ for i := 0; i < len(src)/4 && !end; i++ {
+ // Decode quantum using the base64 alphabet
+ var dbuf [4]byte;
+ dlen := 4;
+
+ dbufloop:
+ for j := 0; j < 4; j++ {
+ in := src[i*4 + j];
+ if in == '=' && j >= 2 && i == len(src)/4 - 1 {
+ // We've reached the end and there's
+ // padding
+ if src[i*4 + 3] != '=' {
+ return n, false, CorruptInputError(i*4 + 2);
+ }
+ dlen = j;
+ end = true;
+ break dbufloop;
+ }
+ dbuf[j] = enc.decodeMap[in];
+ if dbuf[j] == 0xFF {
+ return n, false, CorruptInputError(i*4 + j);
+ }
+ }
+
+ // Pack 4x 6-bit source blocks into 3 byte destination
+ // quantum
+ switch dlen {
+ case 4:
+ dst[i*3 + 2] = dbuf[2]<<6 | dbuf[3];
+ fallthrough;
+ case 3:
+ dst[i*3 + 1] = dbuf[1]<<4 | dbuf[2]>>2;
+ fallthrough;
+ case 2:
+ dst[i*3 + 0] = dbuf[0]<<2 | dbuf[1]>>4;
+ }
+ n += dlen-1;
+ }
+
+ return n, end, nil;
+}
+
+// Decode decodes src using the encoding enc. It writes at most
+// DecodedLen(len(src)) bytes to dst and returns the number of bytes
+// written. If src contains invalid base64 data, it will return the
+// number of bytes successfully written and CorruptInputError.
+func (enc *Encoding) Decode(src, dst []byte) (n int, err os.Error) {
+ if len(src)%4 != 0 {
+ return 0, CorruptInputError(len(src)/4*4);
+ }
+
+ n, _, err = enc.decode(src, dst);
+ return;
+}
+
+type decoder struct {
+ err os.Error;
+ enc *Encoding;
+ r io.Reader;
+ end bool; // saw end of message
+ buf [1024]byte; // leftover input
+ nbuf int;
+ out []byte; // leftover decoded output
+ outbuf [1024/4*3]byte;
+}
+
+func (d *decoder) Read(p []byte) (n int, err os.Error) {
+ if d.err != nil {
+ return 0, d.err;
+ }
+
+ // Use leftover decoded output from last read.
+ if len(d.out) > 0 {
+ n = bytes.Copy(p, d.out);
+ d.out = d.out[n:len(d.out)];
+ return n, nil;
+ }
+
+ // Read a chunk.
+ nn := len(p)/3*4;
+ if nn < 4 {
+ nn = 4;
+ }
+ if nn > len(d.buf) {
+ nn = len(d.buf);
+ }
+ nn, d.err = io.ReadAtLeast(d.r, d.buf[d.nbuf : nn], 4 - d.nbuf);
+ d.nbuf += nn;
+ if d.nbuf < 4 {
+ return 0, d.err;
+ }
+
+ // Decode chunk into p, or d.out and then p if p is too small.
+ nr := d.nbuf / 4 * 4;
+ nw := d.nbuf / 4 * 3;
+ if nw > len(p) {
+ nw, d.end, d.err = d.enc.decode(d.buf[0:nr], &d.outbuf);
+ d.out = d.outbuf[0:nw];
+ n = bytes.Copy(p, d.out);
+ d.out = d.out[n:len(d.out)];
+ } else {
+ n, d.end, d.err = d.enc.decode(d.buf[0:nr], p);
+ }
+ d.nbuf -= nr;
+ for i := 0; i < d.nbuf; i++ {
+ d.buf[i] = d.buf[i+nr];
+ }
+
+ if d.err == nil {
+ d.err = err;
+ }
+ return n, d.err;
+}
+
+// NewDecoder constructs a new base64 stream decoder.
+func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
+ return &decoder{enc: enc, r: r};
+}
+
+// DecodeLen returns the maximum length in bytes of the decoded data
+// corresponding to n bytes of base64-encoded data.
+func (enc *Encoding) DecodedLen(n int) int {
+ return n/4*3;
+}
diff --git a/src/pkg/encoding/base64/base64_test.go b/src/pkg/encoding/base64/base64_test.go
new file mode 100644
index 000000000..54d2326f5
--- /dev/null
+++ b/src/pkg/encoding/base64/base64_test.go
@@ -0,0 +1,201 @@
+// Copyright 2009 The Go 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 base64
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "reflect";
+ "strings";
+ "testing";
+)
+
+type testpair struct {
+ decoded, encoded string;
+}
+
+var pairs = []testpair{
+ // RFC 3548 examples
+ testpair{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
+ testpair{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
+ testpair{"\x14\xfb\x9c\x03", "FPucAw=="},
+
+ // RFC 4648 examples
+ testpair{"", ""},
+ testpair{"f", "Zg=="},
+ testpair{"fo", "Zm8="},
+ testpair{"foo", "Zm9v"},
+ testpair{"foob", "Zm9vYg=="},
+ testpair{"fooba", "Zm9vYmE="},
+ testpair{"foobar", "Zm9vYmFy"},
+
+ // Wikipedia examples
+ testpair{"sure.", "c3VyZS4="},
+ testpair{"sure", "c3VyZQ=="},
+ testpair{"sur", "c3Vy"},
+ testpair{"su", "c3U="},
+ testpair{"leasure.", "bGVhc3VyZS4="},
+ testpair{"easure.", "ZWFzdXJlLg=="},
+ testpair{"asure.", "YXN1cmUu"},
+ testpair{"sure.", "c3VyZS4="},
+}
+
+var bigtest = testpair{
+ "Twas brillig, and the slithy toves",
+ "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
+}
+
+func testEqual(t *testing.T, msg string, args ...) bool {
+ v := reflect.NewValue(args).(*reflect.StructValue);
+ v1 := v.Field(v.NumField() - 2);
+ v2 := v.Field(v.NumField() - 1);
+ if v1.Interface() != v2.Interface() {
+ t.Errorf(msg, args);
+ return false;
+ }
+ return true;
+}
+
+func TestEncode(t *testing.T) {
+ for _, p := range pairs {
+ buf := make([]byte, StdEncoding.EncodedLen(len(p.decoded)));
+ StdEncoding.Encode(strings.Bytes(p.decoded), buf);
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, string(buf), p.encoded);
+ }
+}
+
+func TestEncoder(t *testing.T) {
+ for _, p := range pairs {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(StdEncoding, bb);
+ encoder.Write(strings.Bytes(p.decoded));
+ encoder.Close();
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded);
+ }
+}
+
+func TestEncoderBuffering(t *testing.T) {
+ input := strings.Bytes(bigtest.decoded);
+ for bs := 1; bs <= 12; bs++ {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(StdEncoding, bb);
+ for pos := 0; pos < len(input); pos += bs {
+ end := pos+bs;
+ if end > len(input) {
+ end = len(input);
+ }
+ n, err := encoder.Write(input[pos:end]);
+ testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, os.Error(nil));
+ testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos);
+ }
+ err := encoder.Close();
+ testEqual(t, "Close gave error %v, want %v", err, os.Error(nil));
+ testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, bb.String(), bigtest.encoded);
+ }
+}
+
+func TestDecode(t *testing.T) {
+ for _, p := range pairs {
+ dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)));
+ count, end, err := StdEncoding.decode(strings.Bytes(p.encoded), dbuf);
+ testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, os.Error(nil));
+ testEqual(t, "Decode(%q) = length %v, want %v", p.encoded, count, len(p.decoded));
+ if len(p.encoded) > 0 {
+ testEqual(t, "Decode(%q) = end %v, want %v", p.encoded, end, (p.encoded[len(p.encoded)-1] == '='));
+ }
+ testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded);
+ }
+}
+
+func TestDecoder(t *testing.T) {
+ for _, p := range pairs {
+ decoder := NewDecoder(StdEncoding, bytes.NewBufferString(p.encoded));
+ dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)));
+ count, err := decoder.Read(dbuf);
+ if err != nil && err != os.EOF {
+ t.Fatal("Read failed", err);
+ }
+ testEqual(t, "Read from %q = length %v, want %v", p.encoded, count, len(p.decoded));
+ testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded);
+ if err != os.EOF {
+ count, err = decoder.Read(dbuf);
+ }
+ testEqual(t, "Read from %q = %v, want %v", p.encoded, err, os.EOF);
+ }
+}
+
+func TestDecoderBuffering(t *testing.T) {
+ for bs := 1; bs <= 12; bs++ {
+ decoder := NewDecoder(StdEncoding, bytes.NewBufferString(bigtest.encoded));
+ buf := make([]byte, len(bigtest.decoded)+12);
+ var total int;
+ for total = 0; total < len(bigtest.decoded); {
+ n, err := decoder.Read(buf[total : total+bs]);
+ testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, os.Error(nil));
+ total += n;
+ }
+ testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded);
+ }
+}
+
+func TestDecodeCorrupt(t *testing.T) {
+ type corrupt struct {
+ e string;
+ p int;
+ }
+ examples := []corrupt{
+ corrupt{"!!!!", 0},
+ corrupt{"x===", 1},
+ corrupt{"AA=A", 2},
+ corrupt{"AAA=AAAA", 3},
+ corrupt{"AAAAA", 4},
+ corrupt{"AAAAAA", 4},
+ };
+
+ for _, e := range examples {
+ dbuf := make([]byte, StdEncoding.DecodedLen(len(e.e)));
+ _, err := StdEncoding.Decode(strings.Bytes(e.e), dbuf);
+ switch err := err.(type) {
+ case CorruptInputError:
+ testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p);
+ default:
+ t.Error("Decoder failed to detect corruption in", e);
+ }
+ }
+}
+
+func TestBig(t *testing.T) {
+ n := 3*1000 + 1;
+ raw := make([]byte, n);
+ const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ for i := 0; i < n; i++ {
+ raw[i] = alpha[i%len(alpha)];
+ }
+ encoded := new(bytes.Buffer);
+ w := NewEncoder(StdEncoding, encoded);
+ nn, err := w.Write(raw);
+ if nn != n || err != nil {
+ t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n);
+ }
+ err = w.Close();
+ if err != nil {
+ t.Fatalf("Encoder.Close() = %v want nil", err);
+ }
+ decoded, err := io.ReadAll(NewDecoder(StdEncoding, encoded));
+ if err != nil {
+ t.Fatalf("io.ReadAll(NewDecoder(...)): %v", err);
+ }
+
+ if !bytes.Equal(raw, decoded) {
+ var i int;
+ for i = 0; i < len(decoded) && i < len(raw); i++ {
+ if decoded[i] != raw[i] {
+ break;
+ }
+ }
+ t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i);
+ }
+}
diff --git a/src/pkg/encoding/binary/Makefile b/src/pkg/encoding/binary/Makefile
new file mode 100644
index 000000000..69fd57c67
--- /dev/null
+++ b/src/pkg/encoding/binary/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=encoding/binary
+GOFILES=\
+ binary.go\
+
+include $(GOROOT)/src/Make.pkg
diff --git a/src/pkg/encoding/binary/binary.go b/src/pkg/encoding/binary/binary.go
new file mode 100644
index 000000000..836a43df0
--- /dev/null
+++ b/src/pkg/encoding/binary/binary.go
@@ -0,0 +1,222 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This package implements translation between
+// unsigned integer values and byte sequences.
+package binary
+
+import (
+ "math";
+ "io";
+ "os";
+ "reflect";
+)
+
+// A ByteOrder specifies how to convert byte sequences into
+// 16-, 32-, or 64-bit unsigned integers.
+type ByteOrder interface {
+ Uint16(b []byte) uint16;
+ Uint32(b []byte) uint32;
+ Uint64(b []byte) uint64;
+ String() string;
+}
+
+// This is byte instead of struct{} so that it can be compared,
+// allowing, e.g., order == binary.LittleEndian.
+type unused byte
+
+var LittleEndian ByteOrder = littleEndian(0)
+var BigEndian ByteOrder = bigEndian(0)
+
+type littleEndian unused
+
+func (littleEndian) Uint16(b []byte) uint16 {
+ return uint16(b[0]) | uint16(b[1])<<8;
+}
+
+func (littleEndian) Uint32(b []byte) uint32 {
+ return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24;
+}
+
+func (littleEndian) Uint64(b []byte) uint64 {
+ return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
+ uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56;
+}
+
+func (littleEndian) String() string {
+ return "LittleEndian";
+}
+
+func (littleEndian) GoString() string {
+ return "binary.LittleEndian";
+}
+
+type bigEndian unused
+
+func (bigEndian) Uint16(b []byte) uint16 {
+ return uint16(b[1]) | uint16(b[0])<<8;
+}
+
+func (bigEndian) Uint32(b []byte) uint32 {
+ return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24;
+}
+
+func (bigEndian) Uint64(b []byte) uint64 {
+ return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
+ uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56;
+}
+
+func (bigEndian) String() string {
+ return "BigEndian";
+}
+
+func (bigEndian) GoString() string {
+ return "binary.BigEndian";
+}
+
+// Read reads structured binary data from r into data.
+// Data must be a pointer to a fixed-size value.
+// A fixed-size value is either a fixed-size integer
+// (int8, uint8, int16, uint16, ...) or an array or struct
+// containing only fixed-size values. Bytes read from
+// r are decoded using order and written to successive
+// fields of the data.
+func Read(r io.Reader, order ByteOrder, data interface{}) os.Error {
+ v := reflect.NewValue(data).(*reflect.PtrValue).Elem();
+ size := sizeof(v.Type());
+ if size < 0 {
+ return os.NewError("binary.Read: invalid type " + v.Type().String());
+ }
+ d := &decoder{order: order, buf: make([]byte, size)};
+ if _, err := io.ReadFull(r, d.buf); err != nil {
+ return err;
+ }
+ d.value(v);
+ return nil;
+}
+
+func sizeof(t reflect.Type) int {
+ switch t := t.(type) {
+ case *reflect.ArrayType:
+ n := sizeof(t.Elem());
+ if n < 0 {
+ return -1;
+ }
+ return t.Len() * n;
+
+ case *reflect.StructType:
+ sum := 0;
+ for i, n := 0, t.NumField(); i < n; i++ {
+ s := sizeof(t.Field(i).Type);
+ if s < 0 {
+ return -1;
+ }
+ sum += s;
+ }
+ return sum;
+
+ case *reflect.Uint8Type:
+ return 1;
+ case *reflect.Uint16Type:
+ return 2;
+ case *reflect.Uint32Type:
+ return 4;
+ case *reflect.Uint64Type:
+ return 8;
+ case *reflect.Int8Type:
+ return 1;
+ case *reflect.Int16Type:
+ return 2;
+ case *reflect.Int32Type:
+ return 4;
+ case *reflect.Int64Type:
+ return 8;
+ case *reflect.Float32Type:
+ return 4;
+ case *reflect.Float64Type:
+ return 8;
+ }
+ return -1;
+}
+
+type decoder struct {
+ order ByteOrder;
+ buf []byte;
+}
+
+func (d *decoder) uint8() uint8 {
+ x := d.buf[0];
+ d.buf = d.buf[1:len(d.buf)];
+ return x;
+}
+
+func (d *decoder) uint16() uint16 {
+ x := d.order.Uint16(d.buf[0:2]);
+ d.buf = d.buf[2:len(d.buf)];
+ return x;
+}
+
+func (d *decoder) uint32() uint32 {
+ x := d.order.Uint32(d.buf[0:4]);
+ d.buf = d.buf[4:len(d.buf)];
+ return x;
+}
+
+func (d *decoder) uint64() uint64 {
+ x := d.order.Uint64(d.buf[0:8]);
+ d.buf = d.buf[8:len(d.buf)];
+ return x;
+}
+
+func (d *decoder) int8() int8 {
+ return int8(d.uint8());
+}
+
+func (d *decoder) int16() int16 {
+ return int16(d.uint16());
+}
+
+func (d *decoder) int32() int32 {
+ return int32(d.uint32());
+}
+
+func (d *decoder) int64() int64 {
+ return int64(d.uint64());
+}
+
+func (d *decoder) value(v reflect.Value) {
+ switch v := v.(type) {
+ case *reflect.ArrayValue:
+ l := v.Len();
+ for i := 0; i < l; i++ {
+ d.value(v.Elem(i));
+ }
+ case *reflect.StructValue:
+ l := v.NumField();
+ for i := 0; i < l; i++ {
+ d.value(v.Field(i));
+ }
+
+ case *reflect.Uint8Value:
+ v.Set(d.uint8());
+ case *reflect.Uint16Value:
+ v.Set(d.uint16());
+ case *reflect.Uint32Value:
+ v.Set(d.uint32());
+ case *reflect.Uint64Value:
+ v.Set(d.uint64());
+ case *reflect.Int8Value:
+ v.Set(d.int8());
+ case *reflect.Int16Value:
+ v.Set(d.int16());
+ case *reflect.Int32Value:
+ v.Set(d.int32());
+ case *reflect.Int64Value:
+ v.Set(d.int64());
+ case *reflect.Float32Value:
+ v.Set(math.Float32frombits(d.uint32()));
+ case *reflect.Float64Value:
+ v.Set(math.Float64frombits(d.uint64()));
+ }
+}
diff --git a/src/pkg/encoding/binary/binary_test.go b/src/pkg/encoding/binary/binary_test.go
new file mode 100644
index 000000000..a04684b72
--- /dev/null
+++ b/src/pkg/encoding/binary/binary_test.go
@@ -0,0 +1,86 @@
+// Copyright 2009 The Go 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 binary
+
+import (
+ "bytes";
+ "math";
+ "reflect";
+ "testing";
+)
+
+type Struct struct {
+ Int8 int8;
+ Int16 int16;
+ Int32 int32;
+ Int64 int64;
+ Uint8 uint8;
+ Uint16 uint16;
+ Uint32 uint32;
+ Uint64 uint64;
+ Float64 float64;
+ Array [4]uint8;
+}
+
+var s = Struct{
+ 0x01,
+ 0x0203,
+ 0x04050607,
+ 0x08090a0b0c0d0e0f,
+ 0x10,
+ 0x1112,
+ 0x13141516,
+ 0x1718191a1b1c1d1e,
+ math.Float64frombits(0x1f20212223242526),
+ [4]uint8{0x27, 0x28, 0x29, 0x2a},
+}
+
+var big = []byte{
+ 1,
+ 2, 3,
+ 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16,
+ 17, 18,
+ 19, 20, 21, 22,
+ 23, 24, 25, 26, 27, 28, 29, 30,
+ 31, 32, 33, 34, 35, 36, 37, 38,
+ 39, 40, 41, 42,
+}
+
+var little = []byte{
+ 1,
+ 3, 2,
+ 7, 6, 5, 4,
+ 15, 14, 13, 12, 11, 10, 9, 8,
+ 16,
+ 18, 17,
+ 22, 21, 20, 19,
+ 30, 29, 28, 27, 26, 25, 24, 23,
+ 38, 37, 36, 35, 34, 33, 32, 31,
+ 39, 40, 41, 42,
+}
+
+func TestRead(t *testing.T) {
+ var sl, sb Struct;
+
+ err := Read(bytes.NewBuffer(big), BigEndian, &sb);
+ if err != nil {
+ t.Errorf("Read big-endian: %v", err);
+ goto little;
+ }
+ if !reflect.DeepEqual(sb, s) {
+ t.Errorf("Read big-endian:\n\thave %+v\n\twant %+v", sb, s);
+ }
+
+little:
+ err = Read(bytes.NewBuffer(little), LittleEndian, &sl);
+ if err != nil {
+ t.Errorf("Read little-endian: %v", err);
+ }
+ if !reflect.DeepEqual(sl, s) {
+ t.Errorf("Read big-endian:\n\thave %+v\n\twant %+v", sl, s);
+ }
+}
diff --git a/src/pkg/encoding/git85/Makefile b/src/pkg/encoding/git85/Makefile
new file mode 100644
index 000000000..10587743a
--- /dev/null
+++ b/src/pkg/encoding/git85/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=encoding/git85
+GOFILES=\
+ git.go\
+
+include $(GOROOT)/src/Make.pkg
diff --git a/src/pkg/encoding/git85/git.go b/src/pkg/encoding/git85/git.go
new file mode 100644
index 000000000..2dcd4d877
--- /dev/null
+++ b/src/pkg/encoding/git85/git.go
@@ -0,0 +1,284 @@
+// Copyright 2009 The Go 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 git85 implements the radix 85 data encoding
+// used in the GIT version control system.
+package git85
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "strconv";
+)
+
+type CorruptInputError int64
+
+func (e CorruptInputError) String() string {
+ return "illegal git85 data at input byte" + strconv.Itoa64(int64(e));
+}
+
+const encode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
+
+// The decodings are 1+ the actual value, so that the
+// default zero value can be used to mean "not valid".
+var decode = [256]uint8{
+ '0': 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
+ 'A': 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
+ 'a': 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
+ '!': 63,
+ '#': 64, 65, 66, 67,
+ '(': 68, 69, 70, 71,
+ '-': 72,
+ ';': 73,
+ '<': 74, 75, 76, 77,
+ '@': 78,
+ '^': 79, 80, 81,
+ '{': 82, 83, 84, 85
+}
+
+// Encode encodes src into EncodedLen(len(src))
+// bytes of dst. As a convenience, it returns the number
+// of bytes written to dst, but this value is always EncodedLen(len(src)).
+// Encode implements the radix 85 encoding used in the
+// Git version control tool.
+//
+// The encoding splits src into chunks of at most 52 bytes
+// and encodes each chunk on its own line.
+func Encode(src, dst []byte) int {
+ ndst := 0;
+ for len(src) > 0 {
+ n := len(src);
+ if n > 52 {
+ n = 52;
+ }
+ if n <= 27 {
+ dst[ndst] = byte('A' + n - 1);
+ } else {
+ dst[ndst] = byte('a' + n - 26 - 1);
+ }
+ ndst++;
+ for i := 0; i < n; i += 4 {
+ var v uint32;
+ for j := 0; j < 4 && i+j < n; j++ {
+ v |= uint32(src[i+j]) << uint(24 - j*8);
+ }
+ for j := 4; j >= 0; j-- {
+ dst[ndst+j] = encode[v%85];
+ v /= 85;
+ }
+ ndst += 5;
+ }
+ dst[ndst] = '\n';
+ ndst++;
+ src = src[n:len(src)];
+ }
+ return ndst;
+}
+
+// EncodedLen returns the length of an encoding of n source bytes.
+func EncodedLen(n int) int {
+ if n == 0 {
+ return 0;
+ }
+ // 5 bytes per 4 bytes of input, rounded up.
+ // 2 extra bytes for each line of 52 src bytes, rounded up.
+ return (n+3)/4*5 + (n+51)/52*2;
+}
+
+var newline = []byte{'\n'}
+
+// Decode decodes src into at most MaxDecodedLen(len(src))
+// bytes, returning the actual number of bytes written to dst.
+//
+// If Decode encounters invalid input, it returns a CorruptInputError.
+//
+func Decode(src, dst []byte) (n int, err os.Error) {
+ ndst := 0;
+ nsrc := 0;
+ for nsrc < len(src) {
+ var l int;
+ switch ch := int(src[nsrc]); {
+ case 'A' <= ch && ch <= 'Z':
+ l = ch - 'A' + 1;
+ case 'a' <= ch && ch <= 'z':
+ l = ch - 'a' + 26 + 1;
+ default:
+ return ndst, CorruptInputError(nsrc);
+ }
+ if nsrc+1+l > len(src) {
+ return ndst, CorruptInputError(nsrc);
+ }
+ el := (l+3)/4*5; // encoded len
+ if nsrc+1+el+1 > len(src) || src[nsrc+1+el] != '\n' {
+ return ndst, CorruptInputError(nsrc);
+ }
+ line := src[nsrc+1:nsrc+1+el];
+ for i := 0; i < el; i += 5 {
+ var v uint32;
+ for j := 0; j < 5; j++ {
+ ch := decode[line[i+j]];
+ if ch == 0 {
+ return ndst, CorruptInputError(nsrc+1+i+j);
+ }
+ v = v*85 + uint32(ch-1);
+ }
+ for j := 0; j < 4; j++ {
+ dst[ndst] = byte(v>>24);
+ v <<= 8;
+ ndst++;
+ }
+ }
+ // Last fragment may have run too far (but there was room in dst).
+ // Back up.
+ if l%4 != 0 {
+ ndst -= 4 - l%4;
+ }
+ nsrc += 1+el+1;
+ }
+ return ndst, nil;
+}
+
+func MaxDecodedLen(n int) int {
+ return n/5*4;
+}
+
+// NewEncoder returns a new Git base85 stream encoder. Data written to
+// the returned writer will be encoded and then written to w.
+// The Git encoding operates on 52-byte blocks; when finished
+// writing, the caller must Close the returned encoder to flush any
+// partially written blocks.
+func NewEncoder(w io.Writer) io.WriteCloser {
+ return &encoder{w: w};
+}
+
+type encoder struct {
+ w io.Writer;
+ err os.Error;
+ buf [52]byte;
+ nbuf int;
+ out [1024]byte;
+ nout int;
+}
+
+func (e *encoder) Write(p []byte) (n int, err os.Error) {
+ if e.err != nil {
+ return 0, e.err;
+ }
+
+ // Leading fringe.
+ if e.nbuf > 0 {
+ var i int;
+ for i = 0; i < len(p) && e.nbuf < 52; i++ {
+ e.buf[e.nbuf] = p[i];
+ e.nbuf++;
+ }
+ n += i;
+ p = p[i:len(p)];
+ if e.nbuf < 52 {
+ return;
+ }
+ nout := Encode(&e.buf, &e.out);
+ if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
+ return n, e.err;
+ }
+ e.nbuf = 0;
+ }
+
+ // Large interior chunks.
+ for len(p) >= 52 {
+ nn := len(e.out)/(1+52/4*5+1) * 52;
+ if nn > len(p) {
+ nn = len(p)/52 * 52;
+ }
+ if nn > 0 {
+ nout := Encode(p[0:nn], &e.out);
+ if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
+ return n, e.err;
+ }
+ }
+ n += nn;
+ p = p[nn:len(p)];
+ }
+
+ // Trailing fringe.
+ for i := 0; i < len(p); i++ {
+ e.buf[i] = p[i];
+ }
+ e.nbuf = len(p);
+ n += len(p);
+ return;
+}
+
+func (e *encoder) Close() os.Error {
+ // If there's anything left in the buffer, flush it out
+ if e.err == nil && e.nbuf > 0 {
+ nout := Encode(e.buf[0:e.nbuf], &e.out);
+ e.nbuf = 0;
+ _, e.err = e.w.Write(e.out[0:nout]);
+ }
+ return e.err;
+}
+
+// NewDecoder returns a new Git base85 stream decoder.
+func NewDecoder(r io.Reader) io.Reader {
+ return &decoder{r: r};
+}
+
+type decoder struct {
+ r io.Reader;
+ err os.Error;
+ readErr os.Error;
+ buf [1024]byte;
+ nbuf int;
+ out []byte;
+ outbuf [1024]byte;
+ off int64;
+}
+
+func (d *decoder) Read(p []byte) (n int, err os.Error) {
+ if len(p) == 0 {
+ return 0, nil;
+ }
+
+ for {
+ // Copy leftover output from last decode.
+ if len(d.out) > 0 {
+ n = bytes.Copy(p, d.out);
+ d.out = d.out[n:len(d.out)];
+ return;
+ }
+
+ // Out of decoded output. Check errors.
+ if d.err != nil {
+ return 0, d.err;
+ }
+ if d.readErr != nil {
+ d.err = d.readErr;
+ return 0, d.err;
+ }
+
+ // Read and decode more input.
+ var nn int;
+ nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)]);
+ d.nbuf += nn;
+
+ // Send complete lines to Decode.
+ nl := bytes.LastIndex(d.buf[0:d.nbuf], newline);
+ if nl < 0 {
+ continue;
+ }
+ nn, d.err = Decode(d.buf[0:nl+1], &d.outbuf);
+ if e, ok := d.err.(CorruptInputError); ok {
+ d.err = CorruptInputError(int64(e)+d.off);
+ }
+ d.out = d.outbuf[0:nn];
+ d.nbuf = bytes.Copy(&d.buf, d.buf[nl+1:d.nbuf]);
+ d.off += int64(nl+1);
+ }
+ panic("unreacahable");
+}
+
diff --git a/src/pkg/encoding/git85/git_test.go b/src/pkg/encoding/git85/git_test.go
new file mode 100644
index 000000000..e83e941f1
--- /dev/null
+++ b/src/pkg/encoding/git85/git_test.go
@@ -0,0 +1,199 @@
+// Copyright 2009 The Go 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 git85
+
+import (
+ "bytes";
+ "io";
+ "os";
+ "reflect";
+ "strings";
+ "testing";
+)
+
+type testpair struct {
+ decoded, encoded string;
+}
+
+func testEqual(t *testing.T, msg string, args ...) bool {
+ v := reflect.NewValue(args).(*reflect.StructValue);
+ v1 := v.Field(v.NumField() - 2);
+ v2 := v.Field(v.NumField() - 1);
+ if v1.Interface() != v2.Interface() {
+ t.Errorf(msg, args);
+ return false;
+ }
+ return true;
+}
+
+func TestGitTable(t *testing.T) {
+ var saw [256]bool;
+ for i, c := range encode {
+ if decode[c] != uint8(i+1) {
+ t.Errorf("decode['%c'] = %d, want %d", c, decode[c], i+1);
+ }
+ saw[c] = true;
+ }
+ for i, b := range saw {
+ if !b && decode[i] != 0 {
+ t.Errorf("decode[%d] = %d, want 0", i, decode[i]);
+ }
+ }
+}
+
+var gitPairs = []testpair{
+ // Wikipedia example, adapted.
+ testpair{
+ "Man is distinguished, not only by his reason, but by this singular passion from "
+ "other animals, which is a lust of the mind, that by a perseverance of delight in "
+ "the continued and indefatigable generation of knowledge, exceeds the short "
+ "vehemence of any carnal pleasure.",
+
+ "zO<`^zX>%ZCX>)XGZfA9Ab7*B`EFf-gbRchTY<VDJc_3(Mb0BhMVRLV8EFfZabRc4R\n"
+ "zAarPHb0BkRZfA9DVR9gFVRLh7Z*CxFa&K)QZ**v7av))DX>DO_b1WctXlY|;AZc?T\n"
+ "zVIXXEb95kYW*~HEWgu;7Ze%PVbZB98AYyqSVIXj2a&u*NWpZI|V`U(3W*}r`Y-wj`\n"
+ "zbRcPNAarPDAY*TCbZKsNWn>^>Ze$>7Ze(R<VRUI{VPb4$AZKN6WpZJ3X>V>IZ)PBC\n"
+ "zZf|#NWn^b%EFfigV`XJzb0BnRWgv5CZ*p`Xc4cT~ZDnp_Wgu^6AYpEKAY);2ZeeU7\n"
+ "IaBO8^b9HiME&u=k\n"
+ }
+}
+
+var gitBigtest = gitPairs[len(gitPairs)-1];
+
+func TestEncode(t *testing.T) {
+ for _, p := range gitPairs {
+ buf := make([]byte, EncodedLen(len(p.decoded)));
+ n := Encode(strings.Bytes(p.decoded), buf);
+ if n != len(buf) {
+ t.Errorf("EncodedLen does not agree with Encode");
+ }
+ buf = buf[0:n];
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, string(buf), p.encoded);
+ }
+}
+
+func TestEncoder(t *testing.T) {
+ for _, p := range gitPairs {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(bb);
+ encoder.Write(strings.Bytes(p.decoded));
+ encoder.Close();
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded);
+ }
+}
+
+func TestEncoderBuffering(t *testing.T) {
+ input := strings.Bytes(gitBigtest.decoded);
+ for bs := 1; bs <= 12; bs++ {
+ bb := &bytes.Buffer{};
+ encoder := NewEncoder(bb);
+ for pos := 0; pos < len(input); pos += bs {
+ end := pos+bs;
+ if end > len(input) {
+ end = len(input);
+ }
+ n, err := encoder.Write(input[pos:end]);
+ testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, os.Error(nil));
+ testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos);
+ }
+ err := encoder.Close();
+ testEqual(t, "Close gave error %v, want %v", err, os.Error(nil));
+ testEqual(t, "Encoding/%d of %q = %q, want %q", bs, gitBigtest.decoded, bb.String(), gitBigtest.encoded);
+ }
+}
+
+func TestDecode(t *testing.T) {
+ for _, p := range gitPairs {
+ dbuf := make([]byte, 4*len(p.encoded));
+ ndst, err := Decode(strings.Bytes(p.encoded), dbuf);
+ testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, os.Error(nil));
+ testEqual(t, "Decode(%q) = ndst %v, want %v", p.encoded, ndst, len(p.decoded));
+ testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:ndst]), p.decoded);
+ }
+}
+
+func TestDecoder(t *testing.T) {
+ for _, p := range gitPairs {
+ decoder := NewDecoder(bytes.NewBufferString(p.encoded));
+ dbuf, err := io.ReadAll(decoder);
+ if err != nil {
+ t.Fatal("Read failed", err);
+ }
+ testEqual(t, "Read from %q = length %v, want %v", p.encoded, len(dbuf), len(p.decoded));
+ testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf), p.decoded);
+ if err != nil {
+ testEqual(t, "Read from %q = %v, want %v", p.encoded, err, os.EOF);
+ }
+ }
+}
+
+func TestDecoderBuffering(t *testing.T) {
+ for bs := 1; bs <= 12; bs++ {
+ decoder := NewDecoder(bytes.NewBufferString(gitBigtest.encoded));
+ buf := make([]byte, len(gitBigtest.decoded)+12);
+ var total int;
+ for total = 0; total < len(gitBigtest.decoded); {
+ n, err := decoder.Read(buf[total : total+bs]);
+ testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", gitBigtest.encoded, total, n, err, os.Error(nil));
+ total += n;
+ }
+ testEqual(t, "Decoding/%d of %q = %q, want %q", bs, gitBigtest.encoded, string(buf[0:total]), gitBigtest.decoded);
+ }
+}
+
+func TestDecodeCorrupt(t *testing.T) {
+ type corrupt struct {
+ e string;
+ p int;
+ }
+ examples := []corrupt{
+ corrupt{"v", 0},
+ corrupt{"!z!!!!!!!!!", 0},
+ };
+
+ for _, e := range examples {
+ dbuf := make([]byte, 2*len(e.e));
+ _, err := Decode(strings.Bytes(e.e), dbuf);
+ switch err := err.(type) {
+ case CorruptInputError:
+ testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p);
+ default:
+ t.Error("Decoder failed to detect corruption in", e);
+ }
+ }
+}
+
+func TestGitBig(t *testing.T) {
+ n := 3*1000 + 1;
+ raw := make([]byte, n);
+ const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ for i := 0; i < n; i++ {
+ raw[i] = alpha[i%len(alpha)];
+ }
+ encoded := new(bytes.Buffer);
+ w := NewEncoder(encoded);
+ nn, err := w.Write(raw);
+ if nn != n || err != nil {
+ t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n);
+ }
+ err = w.Close();
+ if err != nil {
+ t.Fatalf("Encoder.Close() = %v want nil", err);
+ }
+ decoded, err := io.ReadAll(NewDecoder(encoded));
+ if err != nil {
+ t.Fatalf("io.ReadAll(NewDecoder(...)): %v", err);
+ }
+
+ if !bytes.Equal(raw, decoded) {
+ var i int;
+ for i = 0; i < len(decoded) && i < len(raw); i++ {
+ if decoded[i] != raw[i] {
+ break;
+ }
+ }
+ t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i);
+ }
+}