OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / binary / binary.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 binary implements translation between
6 // unsigned integer values and byte sequences
7 // and the reading and writing of fixed-size values.
8 package binary
9
10 import (
11         "math"
12         "io"
13         "os"
14         "reflect"
15 )
16
17 // A ByteOrder specifies how to convert byte sequences into
18 // 16-, 32-, or 64-bit unsigned integers.
19 type ByteOrder interface {
20         Uint16(b []byte) uint16
21         Uint32(b []byte) uint32
22         Uint64(b []byte) uint64
23         PutUint16([]byte, uint16)
24         PutUint32([]byte, uint32)
25         PutUint64([]byte, uint64)
26         String() string
27 }
28
29 // This is byte instead of struct{} so that it can be compared,
30 // allowing, e.g., order == binary.LittleEndian.
31 type unused byte
32
33 // LittleEndian is the little-endian implementation of ByteOrder.
34 var LittleEndian littleEndian
35
36 // BigEndian is the big-endian implementation of ByteOrder.
37 var BigEndian bigEndian
38
39 type littleEndian unused
40
41 func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 }
42
43 func (littleEndian) PutUint16(b []byte, v uint16) {
44         b[0] = byte(v)
45         b[1] = byte(v >> 8)
46 }
47
48 func (littleEndian) Uint32(b []byte) uint32 {
49         return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
50 }
51
52 func (littleEndian) PutUint32(b []byte, v uint32) {
53         b[0] = byte(v)
54         b[1] = byte(v >> 8)
55         b[2] = byte(v >> 16)
56         b[3] = byte(v >> 24)
57 }
58
59 func (littleEndian) Uint64(b []byte) uint64 {
60         return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
61                 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
62 }
63
64 func (littleEndian) PutUint64(b []byte, v uint64) {
65         b[0] = byte(v)
66         b[1] = byte(v >> 8)
67         b[2] = byte(v >> 16)
68         b[3] = byte(v >> 24)
69         b[4] = byte(v >> 32)
70         b[5] = byte(v >> 40)
71         b[6] = byte(v >> 48)
72         b[7] = byte(v >> 56)
73 }
74
75 func (littleEndian) String() string { return "LittleEndian" }
76
77 func (littleEndian) GoString() string { return "binary.LittleEndian" }
78
79 type bigEndian unused
80
81 func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 }
82
83 func (bigEndian) PutUint16(b []byte, v uint16) {
84         b[0] = byte(v >> 8)
85         b[1] = byte(v)
86 }
87
88 func (bigEndian) Uint32(b []byte) uint32 {
89         return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
90 }
91
92 func (bigEndian) PutUint32(b []byte, v uint32) {
93         b[0] = byte(v >> 24)
94         b[1] = byte(v >> 16)
95         b[2] = byte(v >> 8)
96         b[3] = byte(v)
97 }
98
99 func (bigEndian) Uint64(b []byte) uint64 {
100         return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
101                 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
102 }
103
104 func (bigEndian) PutUint64(b []byte, v uint64) {
105         b[0] = byte(v >> 56)
106         b[1] = byte(v >> 48)
107         b[2] = byte(v >> 40)
108         b[3] = byte(v >> 32)
109         b[4] = byte(v >> 24)
110         b[5] = byte(v >> 16)
111         b[6] = byte(v >> 8)
112         b[7] = byte(v)
113 }
114
115 func (bigEndian) String() string { return "BigEndian" }
116
117 func (bigEndian) GoString() string { return "binary.BigEndian" }
118
119 // Read reads structured binary data from r into data.
120 // Data must be a pointer to a fixed-size value or a slice
121 // of fixed-size values.
122 // A fixed-size value is either a fixed-size arithmetic
123 // type (int8, uint8, int16, float32, complex64, ...)
124 // or an array or struct containing only fixed-size values.
125 // Bytes read from r are decoded using the specified byte order
126 // and written to successive fields of the data.
127 func Read(r io.Reader, order ByteOrder, data interface{}) os.Error {
128         var v reflect.Value
129         switch d := reflect.ValueOf(data); d.Kind() {
130         case reflect.Ptr:
131                 v = d.Elem()
132         case reflect.Slice:
133                 v = d
134         default:
135                 return os.NewError("binary.Read: invalid type " + d.Type().String())
136         }
137         size := TotalSize(v)
138         if size < 0 {
139                 return os.NewError("binary.Read: invalid type " + v.Type().String())
140         }
141         d := &decoder{order: order, buf: make([]byte, size)}
142         if _, err := io.ReadFull(r, d.buf); err != nil {
143                 return err
144         }
145         d.value(v)
146         return nil
147 }
148
149 // Write writes the binary representation of data into w.
150 // Data must be a fixed-size value or a pointer to
151 // a fixed-size value.
152 // A fixed-size value is either a fixed-size arithmetic
153 // type (int8, uint8, int16, float32, complex64, ...)
154 // or an array or struct containing only fixed-size values.
155 // Bytes written to w are encoded using the specified byte order
156 // and read from successive fields of the data.
157 func Write(w io.Writer, order ByteOrder, data interface{}) os.Error {
158         v := reflect.Indirect(reflect.ValueOf(data))
159         size := TotalSize(v)
160         if size < 0 {
161                 return os.NewError("binary.Write: invalid type " + v.Type().String())
162         }
163         buf := make([]byte, size)
164         e := &encoder{order: order, buf: buf}
165         e.value(v)
166         _, err := w.Write(buf)
167         return err
168 }
169
170 func TotalSize(v reflect.Value) int {
171         if v.Kind() == reflect.Slice {
172                 elem := sizeof(v.Type().Elem())
173                 if elem < 0 {
174                         return -1
175                 }
176                 return v.Len() * elem
177         }
178         return sizeof(v.Type())
179 }
180
181 func sizeof(t reflect.Type) int {
182         switch t.Kind() {
183         case reflect.Array:
184                 n := sizeof(t.Elem())
185                 if n < 0 {
186                         return -1
187                 }
188                 return t.Len() * n
189
190         case reflect.Struct:
191                 sum := 0
192                 for i, n := 0, t.NumField(); i < n; i++ {
193                         s := sizeof(t.Field(i).Type)
194                         if s < 0 {
195                                 return -1
196                         }
197                         sum += s
198                 }
199                 return sum
200
201         case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
202                 reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
203                 reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
204                 return int(t.Size())
205         }
206         return -1
207 }
208
209 type decoder struct {
210         order ByteOrder
211         buf   []byte
212 }
213
214 type encoder struct {
215         order ByteOrder
216         buf   []byte
217 }
218
219 func (d *decoder) uint8() uint8 {
220         x := d.buf[0]
221         d.buf = d.buf[1:]
222         return x
223 }
224
225 func (e *encoder) uint8(x uint8) {
226         e.buf[0] = x
227         e.buf = e.buf[1:]
228 }
229
230 func (d *decoder) uint16() uint16 {
231         x := d.order.Uint16(d.buf[0:2])
232         d.buf = d.buf[2:]
233         return x
234 }
235
236 func (e *encoder) uint16(x uint16) {
237         e.order.PutUint16(e.buf[0:2], x)
238         e.buf = e.buf[2:]
239 }
240
241 func (d *decoder) uint32() uint32 {
242         x := d.order.Uint32(d.buf[0:4])
243         d.buf = d.buf[4:]
244         return x
245 }
246
247 func (e *encoder) uint32(x uint32) {
248         e.order.PutUint32(e.buf[0:4], x)
249         e.buf = e.buf[4:]
250 }
251
252 func (d *decoder) uint64() uint64 {
253         x := d.order.Uint64(d.buf[0:8])
254         d.buf = d.buf[8:]
255         return x
256 }
257
258 func (e *encoder) uint64(x uint64) {
259         e.order.PutUint64(e.buf[0:8], x)
260         e.buf = e.buf[8:]
261 }
262
263 func (d *decoder) int8() int8 { return int8(d.uint8()) }
264
265 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
266
267 func (d *decoder) int16() int16 { return int16(d.uint16()) }
268
269 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
270
271 func (d *decoder) int32() int32 { return int32(d.uint32()) }
272
273 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
274
275 func (d *decoder) int64() int64 { return int64(d.uint64()) }
276
277 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
278
279 func (d *decoder) value(v reflect.Value) {
280         switch v.Kind() {
281         case reflect.Array:
282                 l := v.Len()
283                 for i := 0; i < l; i++ {
284                         d.value(v.Index(i))
285                 }
286         case reflect.Struct:
287                 l := v.NumField()
288                 for i := 0; i < l; i++ {
289                         d.value(v.Field(i))
290                 }
291
292         case reflect.Slice:
293                 l := v.Len()
294                 for i := 0; i < l; i++ {
295                         d.value(v.Index(i))
296                 }
297
298         case reflect.Int8:
299                 v.SetInt(int64(d.int8()))
300         case reflect.Int16:
301                 v.SetInt(int64(d.int16()))
302         case reflect.Int32:
303                 v.SetInt(int64(d.int32()))
304         case reflect.Int64:
305                 v.SetInt(d.int64())
306
307         case reflect.Uint8:
308                 v.SetUint(uint64(d.uint8()))
309         case reflect.Uint16:
310                 v.SetUint(uint64(d.uint16()))
311         case reflect.Uint32:
312                 v.SetUint(uint64(d.uint32()))
313         case reflect.Uint64:
314                 v.SetUint(d.uint64())
315
316         case reflect.Float32:
317                 v.SetFloat(float64(math.Float32frombits(d.uint32())))
318         case reflect.Float64:
319                 v.SetFloat(math.Float64frombits(d.uint64()))
320
321         case reflect.Complex64:
322                 v.SetComplex(complex(
323                         float64(math.Float32frombits(d.uint32())),
324                         float64(math.Float32frombits(d.uint32())),
325                 ))
326         case reflect.Complex128:
327                 v.SetComplex(complex(
328                         math.Float64frombits(d.uint64()),
329                         math.Float64frombits(d.uint64()),
330                 ))
331         }
332 }
333
334 func (e *encoder) value(v reflect.Value) {
335         switch v.Kind() {
336         case reflect.Array:
337                 l := v.Len()
338                 for i := 0; i < l; i++ {
339                         e.value(v.Index(i))
340                 }
341         case reflect.Struct:
342                 l := v.NumField()
343                 for i := 0; i < l; i++ {
344                         e.value(v.Field(i))
345                 }
346         case reflect.Slice:
347                 l := v.Len()
348                 for i := 0; i < l; i++ {
349                         e.value(v.Index(i))
350                 }
351
352         case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
353                 switch v.Type().Kind() {
354                 case reflect.Int8:
355                         e.int8(int8(v.Int()))
356                 case reflect.Int16:
357                         e.int16(int16(v.Int()))
358                 case reflect.Int32:
359                         e.int32(int32(v.Int()))
360                 case reflect.Int64:
361                         e.int64(v.Int())
362                 }
363
364         case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
365                 switch v.Type().Kind() {
366                 case reflect.Uint8:
367                         e.uint8(uint8(v.Uint()))
368                 case reflect.Uint16:
369                         e.uint16(uint16(v.Uint()))
370                 case reflect.Uint32:
371                         e.uint32(uint32(v.Uint()))
372                 case reflect.Uint64:
373                         e.uint64(v.Uint())
374                 }
375
376         case reflect.Float32, reflect.Float64:
377                 switch v.Type().Kind() {
378                 case reflect.Float32:
379                         e.uint32(math.Float32bits(float32(v.Float())))
380                 case reflect.Float64:
381                         e.uint64(math.Float64bits(v.Float()))
382                 }
383
384         case reflect.Complex64, reflect.Complex128:
385                 switch v.Type().Kind() {
386                 case reflect.Complex64:
387                         x := v.Complex()
388                         e.uint32(math.Float32bits(float32(real(x))))
389                         e.uint32(math.Float32bits(float32(imag(x))))
390                 case reflect.Complex128:
391                         x := v.Complex()
392                         e.uint64(math.Float64bits(real(x)))
393                         e.uint64(math.Float64bits(imag(x)))
394                 }
395         }
396 }