Skip to content

Commit

Permalink
script_number: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Jan 4, 2016
1 parent 29a1a83 commit 945bdfa
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/script_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ function decode (buffer, maxLength, minimal) {
}
}

// 32-bit?
if (length < 5) {
var result = 0
var result

for (var i = 0; i < length; ++i) {
result += buffer[i] << (8 * i)
}
// 40-bit
if (length === 5) {
var a = buffer.readUInt32LE(0)
var b = buffer.readUInt8(4)

if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1))))
return result
}
if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a)
return b * 0x100000000 + a

// 40-bit
var a = buffer.readUInt32LE(0)
var b = buffer.readUInt8(4)
// 32-bit
} else if (length === 4) {
result = buffer.readUInt32LE(0)

// TODO: refactor
var neg = false
if (b & 0x80) {
b &= ~0x80
neg = true
}
// 24-bit
} else if (length === 3) {
result = buffer.readUInt16LE(0)
result |= buffer.readUInt8(2) << 16

var r = b * 0x100000000 + a
// 16-bit
} else if (length === 2) {
result = buffer.readUInt16LE(0)

if (neg) {
r = -r
// 8-bit
} else {
result = buffer.readUInt8(0)
}

return r
if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1))))
return result
}

function scriptNumSize (i) {
Expand Down

0 comments on commit 945bdfa

Please sign in to comment.