OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [ascii85/] [ascii85.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 ascii85 implements the ascii85 data encoding
6
// as used in the btoa tool and Adobe's PostScript and PDF document formats.
7
package ascii85
8
 
9
import (
10
        "io"
11
        "strconv"
12
)
13
 
14
/*
15
 * Encoder
16
 */
17
 
18
// Encode encodes src into at most MaxEncodedLen(len(src))
19
// bytes of dst, returning the actual number of bytes written.
20
//
21
// The encoding handles 4-byte chunks, using a special encoding
22
// for the last fragment, so Encode is not appropriate for use on
23
// individual blocks of a large data stream.  Use NewEncoder() instead.
24
//
25
// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
26
// Encode does not add these.
27
func Encode(dst, src []byte) int {
28
        if len(src) == 0 {
29
                return 0
30
        }
31
 
32
        n := 0
33
        for len(src) > 0 {
34
                dst[0] = 0
35
                dst[1] = 0
36
                dst[2] = 0
37
                dst[3] = 0
38
                dst[4] = 0
39
 
40
                // Unpack 4 bytes into uint32 to repack into base 85 5-byte.
41
                var v uint32
42
                switch len(src) {
43
                default:
44
                        v |= uint32(src[3])
45
                        fallthrough
46
                case 3:
47
                        v |= uint32(src[2]) << 8
48
                        fallthrough
49
                case 2:
50
                        v |= uint32(src[1]) << 16
51
                        fallthrough
52
                case 1:
53
                        v |= uint32(src[0]) << 24
54
                }
55
 
56
                // Special case: zero (!!!!!) shortens to z.
57
                if v == 0 && len(src) >= 4 {
58
                        dst[0] = 'z'
59
                        dst = dst[1:]
60
                        n++
61
                        continue
62
                }
63
 
64
                // Otherwise, 5 base 85 digits starting at !.
65
                for i := 4; i >= 0; i-- {
66
                        dst[i] = '!' + byte(v%85)
67
                        v /= 85
68
                }
69
 
70
                // If src was short, discard the low destination bytes.
71
                m := 5
72
                if len(src) < 4 {
73
                        m -= 4 - len(src)
74
                        src = nil
75
                } else {
76
                        src = src[4:]
77
                }
78
                dst = dst[m:]
79
                n += m
80
        }
81
        return n
82
}
83
 
84
// MaxEncodedLen returns the maximum length of an encoding of n source bytes.
85
func MaxEncodedLen(n int) int { return (n + 3) / 4 * 5 }
86
 
87
// NewEncoder returns a new ascii85 stream encoder.  Data written to
88
// the returned writer will be encoded and then written to w.
89
// Ascii85 encodings operate in 32-bit blocks; when finished
90
// writing, the caller must Close the returned encoder to flush any
91
// trailing partial block.
92
func NewEncoder(w io.Writer) io.WriteCloser { return &encoder{w: w} }
93
 
94
type encoder struct {
95
        err  error
96
        w    io.Writer
97
        buf  [4]byte    // buffered data waiting to be encoded
98
        nbuf int        // number of bytes in buf
99
        out  [1024]byte // output buffer
100
}
101
 
102
func (e *encoder) Write(p []byte) (n int, err error) {
103
        if e.err != nil {
104
                return 0, e.err
105
        }
106
 
107
        // Leading fringe.
108
        if e.nbuf > 0 {
109
                var i int
110
                for i = 0; i < len(p) && e.nbuf < 4; i++ {
111
                        e.buf[e.nbuf] = p[i]
112
                        e.nbuf++
113
                }
114
                n += i
115
                p = p[i:]
116
                if e.nbuf < 4 {
117
                        return
118
                }
119
                nout := Encode(e.out[0:], e.buf[0:])
120
                if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
121
                        return n, e.err
122
                }
123
                e.nbuf = 0
124
        }
125
 
126
        // Large interior chunks.
127
        for len(p) >= 4 {
128
                nn := len(e.out) / 5 * 4
129
                if nn > len(p) {
130
                        nn = len(p)
131
                }
132
                nn -= nn % 4
133
                if nn > 0 {
134
                        nout := Encode(e.out[0:], p[0:nn])
135
                        if _, e.err = e.w.Write(e.out[0:nout]); e.err != nil {
136
                                return n, e.err
137
                        }
138
                }
139
                n += nn
140
                p = p[nn:]
141
        }
142
 
143
        // Trailing fringe.
144
        for i := 0; i < len(p); i++ {
145
                e.buf[i] = p[i]
146
        }
147
        e.nbuf = len(p)
148
        n += len(p)
149
        return
150
}
151
 
152
// Close flushes any pending output from the encoder.
153
// It is an error to call Write after calling Close.
154
func (e *encoder) Close() error {
155
        // If there's anything left in the buffer, flush it out
156
        if e.err == nil && e.nbuf > 0 {
157
                nout := Encode(e.out[0:], e.buf[0:e.nbuf])
158
                e.nbuf = 0
159
                _, e.err = e.w.Write(e.out[0:nout])
160
        }
161
        return e.err
162
}
163
 
164
/*
165
 * Decoder
166
 */
167
 
168
type CorruptInputError int64
169
 
170
func (e CorruptInputError) Error() string {
171
        return "illegal ascii85 data at input byte " + strconv.FormatInt(int64(e), 10)
172
}
173
 
174
// Decode decodes src into dst, returning both the number
175
// of bytes written to dst and the number consumed from src.
176
// If src contains invalid ascii85 data, Decode will return the
177
// number of bytes successfully written and a CorruptInputError.
178
// Decode ignores space and control characters in src.
179
// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
180
// Decode expects these to have been stripped by the caller.
181
//
182
// If flush is true, Decode assumes that src represents the
183
// end of the input stream and processes it completely rather
184
// than wait for the completion of another 32-bit block.
185
//
186
// NewDecoder wraps an io.Reader interface around Decode.
187
//
188
func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err error) {
189
        var v uint32
190
        var nb int
191
        for i, b := range src {
192
                if len(dst)-ndst < 4 {
193
                        return
194
                }
195
                switch {
196
                case b <= ' ':
197
                        continue
198
                case b == 'z' && nb == 0:
199
                        nb = 5
200
                        v = 0
201
                case '!' <= b && b <= 'u':
202
                        v = v*85 + uint32(b-'!')
203
                        nb++
204
                default:
205
                        return 0, 0, CorruptInputError(i)
206
                }
207
                if nb == 5 {
208
                        nsrc = i + 1
209
                        dst[ndst] = byte(v >> 24)
210
                        dst[ndst+1] = byte(v >> 16)
211
                        dst[ndst+2] = byte(v >> 8)
212
                        dst[ndst+3] = byte(v)
213
                        ndst += 4
214
                        nb = 0
215
                        v = 0
216
                }
217
        }
218
        if flush {
219
                nsrc = len(src)
220
                if nb > 0 {
221
                        // The number of output bytes in the last fragment
222
                        // is the number of leftover input bytes - 1:
223
                        // the extra byte provides enough bits to cover
224
                        // the inefficiency of the encoding for the block.
225
                        if nb == 1 {
226
                                return 0, 0, CorruptInputError(len(src))
227
                        }
228
                        for i := nb; i < 5; i++ {
229
                                // The short encoding truncated the output value.
230
                                // We have to assume the worst case values (digit 84)
231
                                // in order to ensure that the top bits are correct.
232
                                v = v*85 + 84
233
                        }
234
                        for i := 0; i < nb-1; i++ {
235
                                dst[ndst] = byte(v >> 24)
236
                                v <<= 8
237
                                ndst++
238
                        }
239
                }
240
        }
241
        return
242
}
243
 
244
// NewDecoder constructs a new ascii85 stream decoder.
245
func NewDecoder(r io.Reader) io.Reader { return &decoder{r: r} }
246
 
247
type decoder struct {
248
        err     error
249
        readErr error
250
        r       io.Reader
251
        end     bool       // saw end of message
252
        buf     [1024]byte // leftover input
253
        nbuf    int
254
        out     []byte // leftover decoded output
255
        outbuf  [1024]byte
256
}
257
 
258
func (d *decoder) Read(p []byte) (n int, err error) {
259
        if len(p) == 0 {
260
                return 0, nil
261
        }
262
        if d.err != nil {
263
                return 0, d.err
264
        }
265
 
266
        for {
267
                // Copy leftover output from last decode.
268
                if len(d.out) > 0 {
269
                        n = copy(p, d.out)
270
                        d.out = d.out[n:]
271
                        return
272
                }
273
 
274
                // Decode leftover input from last read.
275
                var nn, nsrc, ndst int
276
                if d.nbuf > 0 {
277
                        ndst, nsrc, d.err = Decode(d.outbuf[0:], d.buf[0:d.nbuf], d.readErr != nil)
278
                        if ndst > 0 {
279
                                d.out = d.outbuf[0:ndst]
280
                                d.nbuf = copy(d.buf[0:], d.buf[nsrc:d.nbuf])
281
                                continue // copy out and return
282
                        }
283
                }
284
 
285
                // Out of input, out of decoded output.  Check errors.
286
                if d.err != nil {
287
                        return 0, d.err
288
                }
289
                if d.readErr != nil {
290
                        d.err = d.readErr
291
                        return 0, d.err
292
                }
293
 
294
                // Read more data.
295
                nn, d.readErr = d.r.Read(d.buf[d.nbuf:])
296
                d.nbuf += nn
297
        }
298
        panic("unreachable")
299
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.