The Go Programming Language

Source file src/pkg/encoding/git85/git.go

     1	// Copyright 2009 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package git85 implements the radix 85 data encoding
     6	// used in the Git version control system.
     7	package git85
     8	
     9	import (
    10		"bytes"
    11		"io"
    12		"os"
    13		"strconv"
    14	)
    15	
    16	type CorruptInputError int64
    17	
    18	func (e CorruptInputError) String() string {
    19		return "illegal git85 data at input byte " + strconv.Itoa64(int64(e))
    20	}
    21	
    22	const encode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
    23	
    24	// The decodings are 1+ the actual value, so that the
    25	// default zero value can be used to mean "not valid".
    26	var decode = [256]uint8{
    27		'0': 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    28		'A': 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
    29		24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
    30		'a': 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
    31		50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
    32		'!': 63,
    33		'#': 64, 65, 66, 67,
    34		'(': 68, 69, 70, 71,
    35		'-': 72,
    36		';': 73,
    37		'<': 74, 75, 76, 77,
    38		'@': 78,
    39		'^': 79, 80, 81,
    40		'{': 82, 83, 84, 85,
    41	}
    42	
    43	// Encode encodes src into EncodedLen(len(src))
    44	// bytes of dst.  As a convenience, it returns the number
    45	// of bytes written to dst, but this value is always EncodedLen(len(src)).
    46	// Encode implements the radix 85 encoding used in the
    47	// Git version control tool.
    48	//
    49	// The encoding splits src into chunks of at most 52 bytes
    50	// and encodes each chunk on its own line.
    51	func Encode(dst, src []byte) int {
    52		ndst := 0
    53		for len(src) > 0 {
    54			n := len(src)
    55			if n > 52 {
    56				n = 52
    57			}
    58			if n <= 27 {
    59				dst[ndst] = byte('A' + n - 1)
    60			} else {
    61				dst[ndst] = byte('a' + n - 26 - 1)
    62			}
    63			ndst++
    64			for i := 0; i < n; i += 4 {
    65				var v uint32
    66				for j := 0; j < 4 && i+j < n; j++ {
    67					v |= uint32(src[i+j]) << uint(24-j*8)
    68				}
    69				for j := 4; j >= 0; j-- {
    70					dst[ndst+j] = encode[v%85]
    71					v /= 85
    72				}
    73				ndst += 5
    74			}
    75			dst[ndst] = '\n'
    76			ndst++
    77			src = src[n:]
    78		}
    79		return ndst
    80	}
    81	
    82	// EncodedLen returns the length of an encoding of n source bytes.
    83	func EncodedLen(n int) int {
    84		if n == 0 {
    85			return 0
    86		}
    87		// 5 bytes per 4 bytes of input, rounded up.
    88		// 2 extra bytes for each line of 52 src bytes, rounded up.
    89		return (n+3)/4*5 + (n+51)/52*2
    90	}
    91	
    92	var newline = []byte{'\n'}
    93	
    94	// Decode decodes src into at most MaxDecodedLen(len(src))
    95	// bytes, returning the actual number of bytes written to dst.
    96	//
    97	// If Decode encounters invalid input, it returns a CorruptInputError.
    98	//
    99	func Decode(dst, src []byte) (n int, err os.Error) {
   100		ndst := 0
   101		nsrc := 0
   102		for nsrc < len(src) {
   103			var l int
   104			switch ch := int(src[nsrc]); {
   105			case 'A' <= ch && ch <= 'Z':
   106				l = ch - 'A' + 1
   107			case 'a' <= ch && ch <= 'z':
   108				l = ch - 'a' + 26 + 1
   109			default:
   110				return ndst, CorruptInputError(nsrc)
   111			}
   112			if nsrc+1+l > len(src) {
   113				return ndst, CorruptInputError(nsrc)
   114			}
   115			el := (l + 3) / 4 * 5 // encoded len
   116			if nsrc+1+el+1 > len(src) || src[nsrc+1+el] != '\n' {
   117				return ndst, CorruptInputError(nsrc)
   118			}
   119			line := src[nsrc+1 : nsrc+1+el]
   120			for i := 0; i < el; i += 5 {
   121				var v uint32
   122				for j := 0; j < 5; j++ {
   123					ch := decode[line[i+j]]
   124					if ch == 0 {
   125						return ndst, CorruptInputError(nsrc + 1 + i + j)
   126					}
   127					v = v*85 + uint32(ch-1)
   128				}
   129				for j := 0; j < 4; j++ {
   130					dst[ndst] = byte(v >> 24)
   131					v <<= 8
   132					ndst++
   133				}
   134			}
   135			// Last fragment may have run too far (but there was room in dst).
   136			// Back up.
   137			if l%4 != 0 {
   138				ndst -= 4 - l%4
   139			}
   140			nsrc += 1 + el + 1
   141		}
   142		return ndst, nil
   143	}
   144	
   145	func MaxDecodedLen(n int) int { return n / 5 * 4 }
   146	
   147	// NewEncoder returns a new Git base85 stream encoder.  Data written to
   148	// the returned writer will be encoded and then written to w.
   149	// The Git encoding operates on 52-byte blocks; when finished
   150	// writing, the caller must Close the returned encoder to flush any
   151	// partially written blocks.
   152	func NewEncoder(w io.Writer) io.WriteCloser { return &encoder{w: w} }
   153	
   154	type encoder struct {
   155		w    io.Writer
   156		err  os.Error
   157		buf  [52]byte
   158		nbuf int
   159		out  [1024]byte
   160		nout int
   161	}
   162	
   163	func (e *encoder) Write(p []byte) (n int, err os.Error) {
   164		if e.err != nil {
   165			return 0, e.err
   166		}
   167	
   168		// Leading fringe.
   169		if e.nbuf > 0 {
   170			var i int
   171			for i = 0; i < len(p) && e.nbuf < 52; i++ {
   172				e.buf[e.nbuf] = p[i]
   173				e.nbuf++
   174			}
   175			n += i
   176			p = p[i:]
   177			if e.nbuf < 52 {
   178				return
   179			}
   180			nout := Encode(e.out[0:], e.buf[0:])
   181			if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
   182				return n, e.err
   183			}
   184			e.nbuf = 0
   185		}
   186	
   187		// Large interior chunks.
   188		for len(p) >= 52 {
   189			nn := len(e.out) / (1 + 52/4*5 + 1) * 52
   190			if nn > len(p) {
   191				nn = len(p) / 52 * 52
   192			}
   193			if nn > 0 {
   194				nout := Encode(e.out[0:], p[0:nn])
   195				if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
   196					return n, e.err
   197				}
   198			}
   199			n += nn
   200			p = p[nn:]
   201		}
   202	
   203		// Trailing fringe.
   204		for i := 0; i < len(p); i++ {
   205			e.buf[i] = p[i]
   206		}
   207		e.nbuf = len(p)
   208		n += len(p)
   209		return
   210	}
   211	
   212	func (e *encoder) Close() os.Error {
   213		// If there's anything left in the buffer, flush it out
   214		if e.err == nil && e.nbuf > 0 {
   215			nout := Encode(e.out[0:], e.buf[0:e.nbuf])
   216			e.nbuf = 0
   217			_, e.err = e.w.Write(e.out[0:nout])
   218		}
   219		return e.err
   220	}
   221	
   222	// NewDecoder returns a new Git base85 stream decoder.
   223	func NewDecoder(r io.Reader) io.Reader { return &decoder{r: r} }
   224	
   225	type decoder struct {
   226		r       io.Reader
   227		err     os.Error
   228		readErr os.Error
   229		buf     [1024]byte
   230		nbuf    int
   231		out     []byte
   232		outbuf  [1024]byte
   233		off     int64
   234	}
   235	
   236	func (d *decoder) Read(p []byte) (n int, err os.Error) {
   237		if len(p) == 0 {
   238			return 0, nil
   239		}
   240	
   241		for {
   242			// Copy leftover output from last decode.
   243			if len(d.out) > 0 {
   244				n = copy(p, d.out)
   245				d.out = d.out[n:]
   246				return
   247			}
   248	
   249			// Out of decoded output.  Check errors.
   250			if d.err != nil {
   251				return 0, d.err
   252			}
   253			if d.readErr != nil {
   254				d.err = d.readErr
   255				return 0, d.err
   256			}
   257	
   258			// Read and decode more input.
   259			var nn int
   260			nn, d.readErr = d.r.Read(d.buf[d.nbuf:])
   261			d.nbuf += nn
   262	
   263			// Send complete lines to Decode.
   264			nl := bytes.LastIndex(d.buf[0:d.nbuf], newline)
   265			if nl < 0 {
   266				continue
   267			}
   268			nn, d.err = Decode(d.outbuf[0:], d.buf[0:nl+1])
   269			if e, ok := d.err.(CorruptInputError); ok {
   270				d.err = CorruptInputError(int64(e) + d.off)
   271			}
   272			d.out = d.outbuf[0:nn]
   273			d.nbuf = copy(d.buf[0:], d.buf[nl+1:d.nbuf])
   274			d.off += int64(nl + 1)
   275		}
   276		panic("unreachable")
   277	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.