Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
0f656652
Commit
0f656652
authored
Dec 27, 2013
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented decoding rlp
parent
323ba368
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
7 deletions
+59
-7
rlp.go
rlp.go
+47
-4
rlp_test.go
rlp_test.go
+12
-3
No files found.
rlp.go
View file @
0f656652
package
main
package
main
import
(
import
(
_
"fmt"
"fmt"
"bytes"
"bytes"
"math"
"math"
)
)
...
@@ -40,11 +40,53 @@ func FromBin(data []byte) uint64 {
...
@@ -40,11 +40,53 @@ func FromBin(data []byte) uint64 {
return
FromBin
(
data
[
:
len
(
data
)
-
1
])
*
256
+
uint64
(
data
[
len
(
data
)
-
1
])
return
FromBin
(
data
[
:
len
(
data
)
-
1
])
*
256
+
uint64
(
data
[
len
(
data
)
-
1
])
}
}
func
Decode
(
data
[]
byte
,
pos
int
)
{
func
Decode
(
data
[]
byte
,
pos
int
)
(
interface
{},
int
)
{
char
:=
int
(
data
[
pos
])
char
:=
int
(
data
[
pos
])
slice
:=
make
([]
interface
{},
0
)
switch
{
switch
{
case
char
<
24
:
case
char
<
24
:
return
append
(
slice
,
data
[
pos
]),
pos
+
1
case
char
<
56
:
b
:=
int
(
data
[
pos
])
-
23
return
FromBin
(
data
[
pos
+
1
:
pos
+
1
+
b
]),
pos
+
1
+
b
case
char
<
64
:
b
:=
int
(
data
[
pos
])
-
55
b2
:=
int
(
FromBin
(
data
[
pos
+
1
:
pos
+
1
+
b
]))
return
FromBin
(
data
[
pos
+
1
+
b
:
pos
+
1
+
b
+
b2
]),
pos
+
1
+
b
+
b2
case
char
<
120
:
b
:=
int
(
data
[
pos
])
-
64
return
data
[
pos
+
1
:
pos
+
1
+
b
],
pos
+
1
+
b
case
char
<
128
:
b
:=
int
(
data
[
pos
])
-
119
b2
:=
int
(
FromBin
(
data
[
pos
+
1
:
pos
+
1
+
b
]))
return
data
[
pos
+
1
+
b
:
pos
+
1
+
b
+
b2
],
pos
+
1
+
b
+
b2
case
char
<
184
:
b
:=
int
(
data
[
pos
])
-
128
pos
++
for
i
:=
0
;
i
<
b
;
i
++
{
var
obj
interface
{}
obj
,
pos
=
Decode
(
data
,
pos
)
slice
=
append
(
slice
,
obj
)
}
return
slice
,
pos
case
char
<
192
:
b
:=
int
(
data
[
pos
])
-
183
//b2 := int(FromBin(data[pos+1 : pos+1+b])) (ref imprementation has an unused variable)
pos
=
pos
+
1
+
b
for
i
:=
0
;
i
<
b
;
i
++
{
var
obj
interface
{}
obj
,
pos
=
Decode
(
data
,
pos
)
slice
=
append
(
slice
,
obj
)
}
return
slice
,
pos
default
:
panic
(
fmt
.
Sprintf
(
"byte not supported: %q"
,
char
))
}
}
return
slice
,
0
}
}
func
Encode
(
object
interface
{})
[]
byte
{
func
Encode
(
object
interface
{})
[]
byte
{
...
@@ -86,10 +128,11 @@ func Encode(object interface{}) []byte {
...
@@ -86,10 +128,11 @@ func Encode(object interface{}) []byte {
// Inline function for writing the slice header
// Inline function for writing the slice header
WriteSliceHeader
:=
func
(
length
int
)
{
WriteSliceHeader
:=
func
(
length
int
)
{
if
length
<
56
{
if
length
<
56
{
buff
.
Write
String
(
string
(
length
+
128
))
buff
.
Write
Byte
(
byte
(
length
+
128
))
}
else
{
}
else
{
b2
:=
ToBin
(
uint64
(
length
),
0
)
b2
:=
ToBin
(
uint64
(
length
),
0
)
buff
.
WriteString
(
string
(
len
(
b2
)
+
183
)
+
b2
)
buff
.
WriteByte
(
byte
(
len
(
b2
)
+
183
))
buff
.
WriteString
(
b2
)
}
}
}
}
...
...
rlp_test.go
View file @
0f656652
...
@@ -7,14 +7,23 @@ import (
...
@@ -7,14 +7,23 @@ import (
func
TestEncode
(
t
*
testing
.
T
)
{
func
TestEncode
(
t
*
testing
.
T
)
{
strRes
:=
"Cdog"
strRes
:=
"Cdog"
str
:=
string
(
Encode
(
"dog"
))
bytes
:=
Encode
(
"dog"
)
str
:=
string
(
bytes
)
if
str
!=
strRes
{
if
str
!=
strRes
{
t
.
Error
(
fmt
.
Sprintf
(
"Expected %q, got %q"
,
strRes
,
str
))
t
.
Error
(
fmt
.
Sprintf
(
"Expected %q, got %q"
,
strRes
,
str
))
}
}
dec
,
_
:=
Decode
(
bytes
,
0
)
fmt
.
Printf
(
"raw: %v encoded: %q == %v
\n
"
,
dec
,
str
,
"dog"
)
sliceRes
:=
"
\u0083
CdogCgodCcat"
slice
:=
string
(
Encode
([]
string
{
"dog"
,
"god"
,
"cat"
}))
sliceRes
:=
"
\x83
CdogCgodCcat"
strs
:=
[]
string
{
"dog"
,
"god"
,
"cat"
}
bytes
=
Encode
(
strs
)
slice
:=
string
(
bytes
)
if
slice
!=
sliceRes
{
if
slice
!=
sliceRes
{
t
.
Error
(
fmt
.
Sprintf
(
"Expected %q, got %q"
,
sliceRes
,
slice
))
t
.
Error
(
fmt
.
Sprintf
(
"Expected %q, got %q"
,
sliceRes
,
slice
))
}
}
dec
,
_
=
Decode
(
bytes
,
0
)
fmt
.
Printf
(
"raw: %v encoded: %q == %v
\n
"
,
dec
,
slice
,
strs
)
}
}
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