FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
feathercore-lib/lib/encoding/bufferreader.js at master · FeatherCoin/feathercore-lib · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
FeatherCoin
/
feathercore-lib
Public
forked from
bitpay/bitcore-lib
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
feathercore-lib
/
lib
/
encoding
/
bufferreader.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
198 lines (176 loc) · 5.03 KB
Breadcrumbs
feathercore-lib
/
lib
/
encoding
/
bufferreader.js
Copy path
File metadata and controls
198 lines (176 loc) · 5.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict'
;
var
_
=
require
(
'lodash'
)
;
var
$
=
require
(
'../util/preconditions'
)
;
var
BufferUtil
=
require
(
'../util/buffer'
)
;
var
BN
=
require
(
'../crypto/bn'
)
;
var
BufferReader
=
function
BufferReader
(
buf
)
{
if
(
!
(
this
instanceof
BufferReader
)
)
{
return
new
BufferReader
(
buf
)
;
}
if
(
_
.
isUndefined
(
buf
)
)
{
return
;
}
if
(
Buffer
.
isBuffer
(
buf
)
)
{
this
.
set
(
{
buf
:
buf
}
)
;
}
else
if
(
_
.
isString
(
buf
)
)
{
this
.
set
(
{
buf
:
new
Buffer
(
buf
,
'hex'
)
,
}
)
;
}
else
if
(
_
.
isObject
(
buf
)
)
{
var
obj
=
buf
;
this
.
set
(
obj
)
;
}
else
{
throw
new
TypeError
(
'Unrecognized argument for BufferReader'
)
;
}
}
;
BufferReader
.
prototype
.
set
=
function
(
obj
)
{
this
.
buf
=
obj
.
buf
||
this
.
buf
||
undefined
;
this
.
pos
=
obj
.
pos
||
this
.
pos
||
0
;
return
this
;
}
;
BufferReader
.
prototype
.
eof
=
function
(
)
{
return
this
.
pos
>=
this
.
buf
.
length
;
}
;
BufferReader
.
prototype
.
finished
=
BufferReader
.
prototype
.
eof
;
BufferReader
.
prototype
.
read
=
function
(
len
)
{
$
.
checkArgument
(
!
_
.
isUndefined
(
len
)
,
'Must specify a length'
)
;
var
buf
=
this
.
buf
.
slice
(
this
.
pos
,
this
.
pos
+
len
)
;
this
.
pos
=
this
.
pos
+
len
;
return
buf
;
}
;
BufferReader
.
prototype
.
readAll
=
function
(
)
{
var
buf
=
this
.
buf
.
slice
(
this
.
pos
,
this
.
buf
.
length
)
;
this
.
pos
=
this
.
buf
.
length
;
return
buf
;
}
;
BufferReader
.
prototype
.
readUInt8
=
function
(
)
{
var
val
=
this
.
buf
.
readUInt8
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
1
;
return
val
;
}
;
BufferReader
.
prototype
.
readUInt16BE
=
function
(
)
{
var
val
=
this
.
buf
.
readUInt16BE
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
2
;
return
val
;
}
;
BufferReader
.
prototype
.
readUInt16LE
=
function
(
)
{
var
val
=
this
.
buf
.
readUInt16LE
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
2
;
return
val
;
}
;
BufferReader
.
prototype
.
readUInt32BE
=
function
(
)
{
var
val
=
this
.
buf
.
readUInt32BE
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
4
;
return
val
;
}
;
BufferReader
.
prototype
.
readUInt32LE
=
function
(
)
{
var
val
=
this
.
buf
.
readUInt32LE
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
4
;
return
val
;
}
;
BufferReader
.
prototype
.
readInt32LE
=
function
(
)
{
var
val
=
this
.
buf
.
readInt32LE
(
this
.
pos
)
;
this
.
pos
=
this
.
pos
+
4
;
return
val
;
}
;
BufferReader
.
prototype
.
readUInt64BEBN
=
function
(
)
{
var
buf
=
this
.
buf
.
slice
(
this
.
pos
,
this
.
pos
+
8
)
;
var
bn
=
BN
.
fromBuffer
(
buf
)
;
this
.
pos
=
this
.
pos
+
8
;
return
bn
;
}
;
BufferReader
.
prototype
.
readUInt64LEBN
=
function
(
)
{
var
second
=
this
.
buf
.
readUInt32LE
(
this
.
pos
)
;
var
first
=
this
.
buf
.
readUInt32LE
(
this
.
pos
+
4
)
;
var
combined
=
(
first
*
0x100000000
)
+
second
;
// Instantiating an instance of BN with a number is faster than with an
// array or string. However, the maximum safe number for a double precision
// floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use
// non-floating point numbers less than this amount (52 bits). And in the case
// that the number is larger, we can instatiate an instance of BN by passing
// an array from the buffer (slower) and specifying the endianness.
var
bn
;
if
(
combined
<=
0x1fffffffffffff
)
{
bn
=
new
BN
(
combined
)
;
}
else
{
var
data
=
Array
.
prototype
.
slice
.
call
(
this
.
buf
,
this
.
pos
,
this
.
pos
+
8
)
;
bn
=
new
BN
(
data
,
10
,
'le'
)
;
}
this
.
pos
=
this
.
pos
+
8
;
return
bn
;
}
;
BufferReader
.
prototype
.
readVarintNum
=
function
(
)
{
var
first
=
this
.
readUInt8
(
)
;
switch
(
first
)
{
case
0xFD
:
return
this
.
readUInt16LE
(
)
;
case
0xFE
:
return
this
.
readUInt32LE
(
)
;
case
0xFF
:
var
bn
=
this
.
readUInt64LEBN
(
)
;
var
n
=
bn
.
toNumber
(
)
;
if
(
n
<=
Math
.
pow
(
2
,
53
)
)
{
return
n
;
}
else
{
throw
new
Error
(
'number too large to retain precision - use readVarintBN'
)
;
}
break
;
default
:
return
first
;
}
}
;
/**
* reads a length prepended buffer
*/
BufferReader
.
prototype
.
readVarLengthBuffer
=
function
(
)
{
var
len
=
this
.
readVarintNum
(
)
;
var
buf
=
this
.
read
(
len
)
;
$
.
checkState
(
buf
.
length
===
len
,
'Invalid length while reading varlength buffer. '
+
'Expected to read: '
+
len
+
' and read '
+
buf
.
length
)
;
return
buf
;
}
;
BufferReader
.
prototype
.
readVarintBuf
=
function
(
)
{
var
first
=
this
.
buf
.
readUInt8
(
this
.
pos
)
;
switch
(
first
)
{
case
0xFD
:
return
this
.
read
(
1
+
2
)
;
case
0xFE
:
return
this
.
read
(
1
+
4
)
;
case
0xFF
:
return
this
.
read
(
1
+
8
)
;
default
:
return
this
.
read
(
1
)
;
}
}
;
BufferReader
.
prototype
.
readVarintBN
=
function
(
)
{
var
first
=
this
.
readUInt8
(
)
;
switch
(
first
)
{
case
0xFD
:
return
new
BN
(
this
.
readUInt16LE
(
)
)
;
case
0xFE
:
return
new
BN
(
this
.
readUInt32LE
(
)
)
;
case
0xFF
:
return
this
.
readUInt64LEBN
(
)
;
default
:
return
new
BN
(
first
)
;
}
}
;
BufferReader
.
prototype
.
reverse
=
function
(
)
{
var
buf
=
new
Buffer
(
this
.
buf
.
length
)
;
for
(
var
i
=
0
;
i
<
buf
.
length
;
i
++
)
{
buf
[
i
]
=
this
.
buf
[
this
.
buf
.
length
-
1
-
i
]
;
}
this
.
buf
=
buf
;
return
this
;
}
;
BufferReader
.
prototype
.
readReverse
=
function
(
len
)
{
if
(
_
.
isUndefined
(
len
)
)
{
len
=
this
.
buf
.
length
;
}
var
buf
=
this
.
buf
.
slice
(
this
.
pos
,
this
.
pos
+
len
)
;
this
.
pos
=
this
.
pos
+
len
;
return
BufferUtil
.
reverse
(
buf
)
;
}
;
module
.
exports
=
BufferReader
;
Back
|
FazBrowse Home
|
New Git URL