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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [binary/] [binary.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 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
        "errors"
12
        "io"
13
        "math"
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([]byte) uint16
21
        Uint32([]byte) uint32
22
        Uint64([]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{}) error {
128
        // Fast path for basic types.
129
        if n := intDestSize(data); n != 0 {
130
                var b [8]byte
131
                bs := b[:n]
132
                if _, err := io.ReadFull(r, bs); err != nil {
133
                        return err
134
                }
135
                switch v := data.(type) {
136
                case *int8:
137
                        *v = int8(b[0])
138
                case *uint8:
139
                        *v = b[0]
140
                case *int16:
141
                        *v = int16(order.Uint16(bs))
142
                case *uint16:
143
                        *v = order.Uint16(bs)
144
                case *int32:
145
                        *v = int32(order.Uint32(bs))
146
                case *uint32:
147
                        *v = order.Uint32(bs)
148
                case *int64:
149
                        *v = int64(order.Uint64(bs))
150
                case *uint64:
151
                        *v = order.Uint64(bs)
152
                }
153
                return nil
154
        }
155
 
156
        // Fallback to reflect-based.
157
        var v reflect.Value
158
        switch d := reflect.ValueOf(data); d.Kind() {
159
        case reflect.Ptr:
160
                v = d.Elem()
161
        case reflect.Slice:
162
                v = d
163
        default:
164
                return errors.New("binary.Read: invalid type " + d.Type().String())
165
        }
166
        size := dataSize(v)
167
        if size < 0 {
168
                return errors.New("binary.Read: invalid type " + v.Type().String())
169
        }
170
        d := &decoder{order: order, buf: make([]byte, size)}
171
        if _, err := io.ReadFull(r, d.buf); err != nil {
172
                return err
173
        }
174
        d.value(v)
175
        return nil
176
}
177
 
178
// Write writes the binary representation of data into w.
179
// Data must be a fixed-size value or a pointer to
180
// a fixed-size value.
181
// A fixed-size value is either a fixed-size arithmetic
182
// type (int8, uint8, int16, float32, complex64, ...)
183
// or an array or struct containing only fixed-size values.
184
// Bytes written to w are encoded using the specified byte order
185
// and read from successive fields of the data.
186
func Write(w io.Writer, order ByteOrder, data interface{}) error {
187
        // Fast path for basic types.
188
        var b [8]byte
189
        var bs []byte
190
        switch v := data.(type) {
191
        case *int8:
192
                bs = b[:1]
193
                b[0] = byte(*v)
194
        case int8:
195
                bs = b[:1]
196
                b[0] = byte(v)
197
        case *uint8:
198
                bs = b[:1]
199
                b[0] = *v
200
        case uint8:
201
                bs = b[:1]
202
                b[0] = byte(v)
203
        case *int16:
204
                bs = b[:2]
205
                order.PutUint16(bs, uint16(*v))
206
        case int16:
207
                bs = b[:2]
208
                order.PutUint16(bs, uint16(v))
209
        case *uint16:
210
                bs = b[:2]
211
                order.PutUint16(bs, *v)
212
        case uint16:
213
                bs = b[:2]
214
                order.PutUint16(bs, v)
215
        case *int32:
216
                bs = b[:4]
217
                order.PutUint32(bs, uint32(*v))
218
        case int32:
219
                bs = b[:4]
220
                order.PutUint32(bs, uint32(v))
221
        case *uint32:
222
                bs = b[:4]
223
                order.PutUint32(bs, *v)
224
        case uint32:
225
                bs = b[:4]
226
                order.PutUint32(bs, v)
227
        case *int64:
228
                bs = b[:8]
229
                order.PutUint64(bs, uint64(*v))
230
        case int64:
231
                bs = b[:8]
232
                order.PutUint64(bs, uint64(v))
233
        case *uint64:
234
                bs = b[:8]
235
                order.PutUint64(bs, *v)
236
        case uint64:
237
                bs = b[:8]
238
                order.PutUint64(bs, v)
239
        }
240
        if bs != nil {
241
                _, err := w.Write(bs)
242
                return err
243
        }
244
        v := reflect.Indirect(reflect.ValueOf(data))
245
        size := dataSize(v)
246
        if size < 0 {
247
                return errors.New("binary.Write: invalid type " + v.Type().String())
248
        }
249
        buf := make([]byte, size)
250
        e := &encoder{order: order, buf: buf}
251
        e.value(v)
252
        _, err := w.Write(buf)
253
        return err
254
}
255
 
256
// dataSize returns the number of bytes the actual data represented by v occupies in memory.
257
// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
258
// it returns the length of the slice times the element size and does not count the memory
259
// occupied by the header.
260
func dataSize(v reflect.Value) int {
261
        if v.Kind() == reflect.Slice {
262
                elem := sizeof(v.Type().Elem())
263
                if elem < 0 {
264
                        return -1
265
                }
266
                return v.Len() * elem
267
        }
268
        return sizeof(v.Type())
269
}
270
 
271
func sizeof(t reflect.Type) int {
272
        switch t.Kind() {
273
        case reflect.Array:
274
                n := sizeof(t.Elem())
275
                if n < 0 {
276
                        return -1
277
                }
278
                return t.Len() * n
279
 
280
        case reflect.Struct:
281
                sum := 0
282
                for i, n := 0, t.NumField(); i < n; i++ {
283
                        s := sizeof(t.Field(i).Type)
284
                        if s < 0 {
285
                                return -1
286
                        }
287
                        sum += s
288
                }
289
                return sum
290
 
291
        case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
292
                reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
293
                reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
294
                return int(t.Size())
295
        }
296
        return -1
297
}
298
 
299
type decoder struct {
300
        order ByteOrder
301
        buf   []byte
302
}
303
 
304
type encoder struct {
305
        order ByteOrder
306
        buf   []byte
307
}
308
 
309
func (d *decoder) uint8() uint8 {
310
        x := d.buf[0]
311
        d.buf = d.buf[1:]
312
        return x
313
}
314
 
315
func (e *encoder) uint8(x uint8) {
316
        e.buf[0] = x
317
        e.buf = e.buf[1:]
318
}
319
 
320
func (d *decoder) uint16() uint16 {
321
        x := d.order.Uint16(d.buf[0:2])
322
        d.buf = d.buf[2:]
323
        return x
324
}
325
 
326
func (e *encoder) uint16(x uint16) {
327
        e.order.PutUint16(e.buf[0:2], x)
328
        e.buf = e.buf[2:]
329
}
330
 
331
func (d *decoder) uint32() uint32 {
332
        x := d.order.Uint32(d.buf[0:4])
333
        d.buf = d.buf[4:]
334
        return x
335
}
336
 
337
func (e *encoder) uint32(x uint32) {
338
        e.order.PutUint32(e.buf[0:4], x)
339
        e.buf = e.buf[4:]
340
}
341
 
342
func (d *decoder) uint64() uint64 {
343
        x := d.order.Uint64(d.buf[0:8])
344
        d.buf = d.buf[8:]
345
        return x
346
}
347
 
348
func (e *encoder) uint64(x uint64) {
349
        e.order.PutUint64(e.buf[0:8], x)
350
        e.buf = e.buf[8:]
351
}
352
 
353
func (d *decoder) int8() int8 { return int8(d.uint8()) }
354
 
355
func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
356
 
357
func (d *decoder) int16() int16 { return int16(d.uint16()) }
358
 
359
func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
360
 
361
func (d *decoder) int32() int32 { return int32(d.uint32()) }
362
 
363
func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
364
 
365
func (d *decoder) int64() int64 { return int64(d.uint64()) }
366
 
367
func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
368
 
369
func (d *decoder) value(v reflect.Value) {
370
        switch v.Kind() {
371
        case reflect.Array:
372
                l := v.Len()
373
                for i := 0; i < l; i++ {
374
                        d.value(v.Index(i))
375
                }
376
        case reflect.Struct:
377
                l := v.NumField()
378
                for i := 0; i < l; i++ {
379
                        d.value(v.Field(i))
380
                }
381
 
382
        case reflect.Slice:
383
                l := v.Len()
384
                for i := 0; i < l; i++ {
385
                        d.value(v.Index(i))
386
                }
387
 
388
        case reflect.Int8:
389
                v.SetInt(int64(d.int8()))
390
        case reflect.Int16:
391
                v.SetInt(int64(d.int16()))
392
        case reflect.Int32:
393
                v.SetInt(int64(d.int32()))
394
        case reflect.Int64:
395
                v.SetInt(d.int64())
396
 
397
        case reflect.Uint8:
398
                v.SetUint(uint64(d.uint8()))
399
        case reflect.Uint16:
400
                v.SetUint(uint64(d.uint16()))
401
        case reflect.Uint32:
402
                v.SetUint(uint64(d.uint32()))
403
        case reflect.Uint64:
404
                v.SetUint(d.uint64())
405
 
406
        case reflect.Float32:
407
                v.SetFloat(float64(math.Float32frombits(d.uint32())))
408
        case reflect.Float64:
409
                v.SetFloat(math.Float64frombits(d.uint64()))
410
 
411
        case reflect.Complex64:
412
                v.SetComplex(complex(
413
                        float64(math.Float32frombits(d.uint32())),
414
                        float64(math.Float32frombits(d.uint32())),
415
                ))
416
        case reflect.Complex128:
417
                v.SetComplex(complex(
418
                        math.Float64frombits(d.uint64()),
419
                        math.Float64frombits(d.uint64()),
420
                ))
421
        }
422
}
423
 
424
func (e *encoder) value(v reflect.Value) {
425
        switch v.Kind() {
426
        case reflect.Array:
427
                l := v.Len()
428
                for i := 0; i < l; i++ {
429
                        e.value(v.Index(i))
430
                }
431
        case reflect.Struct:
432
                l := v.NumField()
433
                for i := 0; i < l; i++ {
434
                        e.value(v.Field(i))
435
                }
436
        case reflect.Slice:
437
                l := v.Len()
438
                for i := 0; i < l; i++ {
439
                        e.value(v.Index(i))
440
                }
441
 
442
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
443
                switch v.Type().Kind() {
444
                case reflect.Int8:
445
                        e.int8(int8(v.Int()))
446
                case reflect.Int16:
447
                        e.int16(int16(v.Int()))
448
                case reflect.Int32:
449
                        e.int32(int32(v.Int()))
450
                case reflect.Int64:
451
                        e.int64(v.Int())
452
                }
453
 
454
        case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
455
                switch v.Type().Kind() {
456
                case reflect.Uint8:
457
                        e.uint8(uint8(v.Uint()))
458
                case reflect.Uint16:
459
                        e.uint16(uint16(v.Uint()))
460
                case reflect.Uint32:
461
                        e.uint32(uint32(v.Uint()))
462
                case reflect.Uint64:
463
                        e.uint64(v.Uint())
464
                }
465
 
466
        case reflect.Float32, reflect.Float64:
467
                switch v.Type().Kind() {
468
                case reflect.Float32:
469
                        e.uint32(math.Float32bits(float32(v.Float())))
470
                case reflect.Float64:
471
                        e.uint64(math.Float64bits(v.Float()))
472
                }
473
 
474
        case reflect.Complex64, reflect.Complex128:
475
                switch v.Type().Kind() {
476
                case reflect.Complex64:
477
                        x := v.Complex()
478
                        e.uint32(math.Float32bits(float32(real(x))))
479
                        e.uint32(math.Float32bits(float32(imag(x))))
480
                case reflect.Complex128:
481
                        x := v.Complex()
482
                        e.uint64(math.Float64bits(real(x)))
483
                        e.uint64(math.Float64bits(imag(x)))
484
                }
485
        }
486
}
487
 
488
// intDestSize returns the size of the integer that ptrType points to,
489
// or 0 if the type is not supported.
490
func intDestSize(ptrType interface{}) int {
491
        switch ptrType.(type) {
492
        case *int8, *uint8:
493
                return 1
494
        case *int16, *uint16:
495
                return 2
496
        case *int32, *uint32:
497
                return 4
498
        case *int64, *uint64:
499
                return 8
500
        }
501
        return 0
502
}

powered by: WebSVN 2.1.0

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