Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
60222bf5
Commit
60222bf5
authored
Aug 31, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
package debug/binary
R=austin DELTA=320 (320 added, 0 deleted, 0 changed) OCL=33983 CL=34143
parent
81954398
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
320 additions
and
0 deletions
+320
-0
Make.deps
src/pkg/Make.deps
+1
-0
Makefile
src/pkg/Makefile
+1
-0
Makefile
src/pkg/debug/binary/Makefile
+11
-0
binary.go
src/pkg/debug/binary/binary.go
+220
-0
binary_test.go
src/pkg/debug/binary/binary_test.go
+87
-0
No files found.
src/pkg/Make.deps
View file @
60222bf5
...
@@ -16,6 +16,7 @@ crypto/hmac.install: crypto/md5.install crypto/sha1.install hash.install os.inst
...
@@ -16,6 +16,7 @@ crypto/hmac.install: crypto/md5.install crypto/sha1.install hash.install os.inst
crypto/md5.install: hash.install os.install
crypto/md5.install: hash.install os.install
crypto/sha1.install: hash.install os.install
crypto/sha1.install: hash.install os.install
datafmt.install: bytes.install container/vector.install fmt.install go/scanner.install go/token.install io.install os.install reflect.install runtime.install strconv.install strings.install
datafmt.install: bytes.install container/vector.install fmt.install go/scanner.install go/token.install io.install os.install reflect.install runtime.install strconv.install strings.install
debug/binary.install: io.install math.install os.install reflect.install
debug/elf.install: fmt.install io.install os.install strconv.install
debug/elf.install: fmt.install io.install os.install strconv.install
ebnf.install: container/vector.install fmt.install go/scanner.install go/token.install os.install strconv.install strings.install unicode.install utf8.install
ebnf.install: container/vector.install fmt.install go/scanner.install go/token.install os.install strconv.install strings.install unicode.install utf8.install
exec.install: os.install strings.install
exec.install: os.install strings.install
...
...
src/pkg/Makefile
View file @
60222bf5
...
@@ -30,6 +30,7 @@ DIRS=\
...
@@ -30,6 +30,7 @@ DIRS=\
crypto/md5
\
crypto/md5
\
crypto/sha1
\
crypto/sha1
\
datafmt
\
datafmt
\
debug/binary
\
debug/elf
\
debug/elf
\
ebnf
\
ebnf
\
exec
\
exec
\
...
...
src/pkg/debug/binary/Makefile
0 → 100644
View file @
60222bf5
# 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
=
debug/binary
GOFILES
=
\
binary.go
\
include
$(GOROOT)/src/Make.pkg
src/pkg/debug/binary/binary.go
0 → 100644
View file @
60222bf5
// 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
(
"math"
;
"io"
;
"os"
;
"reflect"
;
)
// A ByteOrder specifies how to convert byte sequences into
// 16-, 32-, or 64-bit 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
n
,
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
()));
}
}
src/pkg/debug/binary/binary_test.go
0 → 100644
View file @
60222bf5
// 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\t
have %+v
\n\t
want %+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\t
have %+v
\n\t
want %+v"
,
sl
,
s
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment